Test and Measurement Forum

Writing Java Applications using the PicoScope SDK and JNI

Post your Java discussions here

Writing Java Applications using the PicoScope SDK and JNI

Postby Hitesh » Wed Sep 14, 2011 8:24 am

We do not provide support for Java in our SDK; however, I’ve been experimenting with writing some code for the PicoScope 2000 series out of interest.

I have been using Netbeans IDE 7.0 with Java Development Kit 1.7 as well as Microsoft Visual C++ 2008 Express Edition.

  • The Java Native Interface (JNI) Tutorial provides an excellent introduction to using the JNI to call code written in another language: http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html
  • You will also need some basic knowledge of C or C++.
  • As the dll files themselves do not have entry points for the Java code to access functions, a wrapper dll will be required.
Below are some steps which outline how to open and close a PicoScope 2000 series unit:

1. The Java class:

Code: Select all
// Class to access the wrapper dll functions
public class PS2000JNIWrapAccessor
{
   //Native methods
   public native short ps2000OpenUnit();
   public native short ps2000CloseUnit(short handle);

   //Load our wrapper dll containing native code implementation
   static
    {
        System.loadLibrary("PS2000JNIWrap");
    }

    public PS2000JNIWrapAccessor()
    {
        // Empty Constuctor
    }
}

// Main class
public class PS2000
{
   private PS2000JNIWrapAccessor ps2000JniWrapAccessor;
   short ps2000Handle;
   
   public PS2000()
   {
      // New instance of class with native methods
      ps2000JniWrapAccessor = new PS2000JNIWrapAccessor();
      
      // Call method to open unit
      ps2000handle = ps2000JniWrapAccessor.ps2000OpenUnit();
      
      System.out.println(“Handle: ” + handle);
   }

   public short getPs2000Handle()
   {
      return ps2000Handle;
   }
   
   public PS2000JNIWrapAccessor getPs2000JniWrapAccessor()
   {
        return ps2000JniWrapAccessor;
   }

   public static void main(String[] args)
   {
      PS2000 ps2000 = new PS2000();
      
      // Perform some operations with the PicoScope...
      
      // When finished, close the unit
      short close_status = ps2000.getPs2000JniWrapAccessor().ps2000CloseUnit(ps2000.getPs2000Handle());
   
      System.out.println("Exit status: " + close_status);
      System.exit(0);
   }
}

2. Compile the program using the command line:
Code: Select all
javac PS2000.java

3. Generate the header file:

Code: Select all
javah –jni –o PS2000JNIWrap.h PS2000JNIWrapAccessor

Note: the above assumes the use of the default package – if you are using packages use the qualified name from the root class directory e.g.

Code: Select all
javah -jni -o .\com\picotech\picoscope\ps2000\PS2000JNIWrap.h com.picotech.picoscope.ps2000.PS2000JNIWrapAccessor

4. Header file Method signatures

Opening the header file generated should show something like this:

Code: Select all
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_picotech_picoscope_ps2000_PS2000JNIWrapAccessor */

#ifndef _Included_com_picotech_picoscope_ps2000_PS2000JNIWrapAccessor
#define _Included_com_picotech_picoscope_ps2000_PS2000JNIWrapAccessor
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     com_picotech_picoscope_ps2000_PS2000JNIWrapAccessor
* Method:    ps2000OpenUnit
* Signature: ()S
*/
JNIEXPORT jshort JNICALL Java_com_picotech_picoscope_ps2000_PS2000JNIWrapAccessor_ps2000OpenUnit(JNIEnv *, jobject);

/*
* Class:     com_picotech_picoscope_ps2000_PS2000JNIWrapAccessor
* Method:    ps2000CloseUnit
* Signature: (S)S
*/
JNIEXPORT jshort JNICALL Java_com_picotech_picoscope_ps2000_PS2000JNIWrapAccessor_ps2000CloseUnit(JNIEnv *, jobject, jshort);

#ifdef __cplusplus
}
#endif
#endif


5. Implement the native method

Create a new C project using an IDE of your choice:

  • Ensure that the include path in your project setup contains the following:

    • Location of the ‘ps2000.h’ file
    • C:\Program Files\Java\jdk1.<version number>\include (for jni.h)
    • C:\Program Files\Java\jdk1.<version number>\include\win32 (for jni_md.h)

Code: Select all
#include <jni.h>
#include "ps2000.h"

/**
*   Function to call ps2000_open_unit
*
**/
JNIEXPORT jshort JNICALL Java_com_picotech_picoscope_ps2000_PS2000JNIWrapAccessor_ps2000OpenUnit(JNIEnv * env, jobject jobj)
{
   return jsHandle = ps2000_open_unit();

}

/**
*   Function to call ps2000_close_unit
*
**/
JNIEXPORT jshort JNICALL Java_com_picotech_picoscope_ps2000_PS2000JNIWrapAccessor_ps2000CloseUnit(JNIEnv * env, jobject jobj, jshort js_handle)
{
      return ps2000_close_unit(js_handle);
}


6. Build the dll and run the program

Build the dll and place it together with the ps2000.dll in a directory (add the location to your PATH environment variable) or alternatively add the dlls to the directory where your main class resides – you’re now ready to run the program!

Code: Select all
java PS2000
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2058
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Writing Java Applications using the PicoScope SDK

Postby Cbeddoe » Wed Aug 07, 2013 7:27 pm

Hi Hitesh,

I've successfully written and tested my first basic JNI wrapper for a basic C Library I wrote and compiled with MS Visual Studio 2012.
I've also successfully compiled and run the USBPT104con.c example program.
I have a few basic questions that will help me out with creating the java class and C implementation for my PT104 Usb logger after studying your example.

1. -The USB Pt 104 documentation lists the return type for the 9 listed methods as Pico_Status which is an "unsigned long".
- "Unsigned Longs" apparently are not supported in java.
- In your Picoscope C implementation (Part 5) you call the method "ps2000_open_unit()" is in a different naming
format, it has a "short" return type and contains no arguments as compared to the PT104 function documentation is
listed as the following "Pico_Status" return type (unsigned long) for method "UsbPt104OpenUnit( short * handle,
char * serial)".

* Due to the incompatibility of Java with the "unsigned long" type, did you define methods in another C file that isn't shown where you call the native Picoscope methods and return a simplified type "short" output that can be then passed through JNI into Java?

I'm hoping that from my attempts at getting some of this other stuff figured out I'm developing at least some understanding of what I need to do to implement the PT-104 libraries correctly.

Best Regards,

Chris
Cbeddoe
Newbie
 
Posts: 0
Joined: Mon Apr 22, 2013 7:52 pm

Re: Writing Java Applications using the PicoScope SDK

Postby Hitesh » Thu Aug 08, 2013 11:17 am

Hi Chris,

The driver for the older PicoScope 2000 series devices does not use the PICO_STATUS data type but uses the short data type for return values.

If you are only dealing with 32-bit numbers, you can use the int data type - I used this for some initial work I did on Java code for the PicoScope 3000 series (A/B variants). I've attached my code below for reference - there is a jar file containing some reusable classes as well.

ps3000a.zip
Source files for PicoScope 3000 (A API)
(14.64 KiB) Downloaded 169 times


If you have any snippets of code which you wish to share with others please post them on this forum under a new topic.

I hope this helps.
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2058
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Writing Java Applications using the PicoScope SDK

Postby Cbeddoe » Thu Aug 08, 2013 2:00 pm

Hi Hitesh,

Thanks for the quick response.
I was hoping the solution wasn't going to be overly involved.
I'll be happy to share the code when I get done with it and I'm intending on doing a little write-up to go along with it.

Best Regards,

Chris
Cbeddoe
Newbie
 
Posts: 0
Joined: Mon Apr 22, 2013 7:52 pm

Re: Writing Java Applications using the PicoScope SDK

Postby Cbeddoe » Tue Jan 07, 2014 6:07 pm

Hi Hitesh,

I received your message a while ago asking if I had made progress in getting the JNI Wrapper working with respect to these posts. Unfortunately, i don't have the ability to reply to your message as the functionality appears to be disabled for my profile. I used the example libraries you posted and tried my best at converting them to work with the PT-104, unfortunately I've been swamped and haven't had the chance to write a basic C wrapper to try to test out my implementation of your libraries.
I'm hoping to be able to give it a try in the next month or so. I still have a few gray areas in my understanding of how to do this but i'm still hoping to get it figured out. I got a basic JNI wrapper to work previously but this is moderately more complicated so I expect it will take some more effort.

Thanks again for all of your help,

Chris
Cbeddoe
Newbie
 
Posts: 0
Joined: Mon Apr 22, 2013 7:52 pm

Re: Writing Java Applications using the PicoScope SDK

Postby Hitesh » Tue Jan 21, 2014 12:09 pm

Hi Chris,

Thanks for your reply.

If you need more detailed help, please e-mail support@picotech.com with your source code and we can take a further look.

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2058
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Writing Java Applications using the PicoScope SDK and JN

Postby giovanni » Fri Feb 13, 2015 8:59 am

Dear,

I would post my problem with SDK and JNI on a x64 PC.

I did the described steps in this post on a windows 32-bit machine in order to build my dll for the DrDaq and they work.
The problem came out when i did the same on a 64-bit machine.

Of course, in the 64-bit i have used the UsbDrDAQ.lib library placed in the x64 folder but in this case i have got the error

" Error 2 error LNK2019: unresolved external symbol _UsbDrDaqGetUnitInfo@20 referenced in function _Java_DrDaqDriver_DrDaqWrapper_init@8 C:\Users\Giovanni\documents\visual studio 2010\Projects\DrDaqDriverDll\DrDaqDriverDll\DrDaqDriverDll.obj DrDaqDriverDll"

I quite sure all the configuration are corrects (linker, builder, etc).

Please Help me!!!
giovanni
Newbie
 
Posts: 0
Joined: Tue Oct 07, 2014 8:34 am

Re: Writing Java Applications using the PicoScope SDK and JN

Postby Hitesh » Fri Feb 13, 2015 11:43 am

Hi giovanni,

Which compiler are you using - is it the Windows SDK 7.1 compiler?

Have you configured the path to the 64-bit lib file for the linker under Project Properties -> Configuration Properties -> Linker -> General?

Are you using the correct data types e.g. int8_t, int16_t in your wrapper code?

If the path setting does not resolve the issue, I can provide an updated lib file and driver if you e-mail support@picotech.com

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2058
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Writing Java Applications using the PicoScope SDK and JN

Postby giovanni » Mon Feb 16, 2015 1:03 pm

Dear,

i have checked the path setting and i can confirm that it is correct.

I am going to send you a mail at support@picotech.com.

Thank you!
giovanni
Newbie
 
Posts: 0
Joined: Tue Oct 07, 2014 8:34 am


Return to Java

Who is online

Users browsing this forum: No registered users and 0 guests