This week, we publish a new version of the Java library. This new version comes with a new feature: native USB access. From now on, you can access the USB ports without running the VirtualHub.
Thanks to the new version of our Java library, you can access Yoctopuce modules connected to the machine running the Java code without using the VirtualHub. Use of the USB modules is the same as with C++, C#, Objective-C, Delphi, VB-Net, and Python languages.
To access the USB ports, you must simply replace your call to YAPI.RegisterHub("localhost") by a call to YAPI.RegisterHub("usb").
// sets up the API to use direct USB access
YAPI.RegisterHub("usb");
System.out.println("Device connected by USB");
YModule module = YModule.FirstModule();
while (module != null) {
System.out.println(module.get_serialNumber());
module = module.nextModule();
}
YAPI.FreeAPI();
} catch (YAPI_Exception ex) {
System.out.println("error:" + ex.getLocalizedMessage());
System.exit(1);
}
There is however a little trick...
Originally, the Java Virtual Machine (JVM) can't reach the machine USB ports. To work around this limitation, we used Java Native Interface (JNI) to load a dynamic library written in C.
Therefore, our new library is made of two parts:
- The Java code containing the objects of our library
- A yapi dynamic library for each operating system (Windows 32bits, Windows 64 bits, Linux 32bits, ...)
You can directly copy the Java code into you project and compile it, but you can also add the YoctoLib.jar file to your classpath.
However, you must install the dynamic library on the machine running the application. To do so, you must copy the yapi library in a directory listed in the java.library.path property of the JVM. This property is simply a list of directories in which the JVM looks for the dynamic libraries. By default, this property is initialized as follows:
under Windows | with the environment variable PATH |
under Linux | with the environment variable LD_LIBRARY_PATH |
under OS X | with the environment variable DYLD_LIBRARY_PATH |
If you want to use other directories, you can modify this property in the application code with the following piece of code:
But you can also specify this property when running from a command line with the "-D" option.
$ java -Djava.library.path=c:\example\yoctopuce\library MainClass
In the opposite to the Java code which works on any OS, each library is made of C code compiled to work on a very specific OS. This explains why we have several binaries for this library. You can either install only the correct version, or all the files, the code is smart enough to use the correct one.
The list of yapi dynamic library versions:
Windows 32 bits | yapi.dll |
Windows 64 bits | yapi64.dll |
Linux 32 bits | libyapi-i386.so |
Linux 64 bits | libyapi-amd64.so |
Linux ARM | libyapi-armhf.so |
Linux ARM 64 bits | libyapi-aarch64.so |
Mac OS X | libyapi.dylib |
The library archive contains only the already compiled binaries, but you can very well recompile them by downloading the C++ library.
When the application is running, the JVM starts by loading the Java code and by running it, without taking the dynamic library into account. It's only when there is the call to YAPI.RegisterHub("usb") that it searches all the directories of the "java.library.path" property to find the yapi dynamic library. If no compatible library is found, an exception is raised. Otherwise, the JVM loads the library and execution goes on.
The Java part of our library is designed to work with or without the yapi dynamic library. Obviously, if the dynamic library is not there, you cannot access the USB ports directly with the call to YAPI.RegisterHub("usb"), but you can still use the VirtualHub or connect your devices to a YoctoHub.
You can use the Java library with a YoctoHub, with the VirtualHub, or with the USB ports of the machine
Here you are, you can now directly access the modules that are connected to the USB ports of your machine.