I am trying to find the proper way to use a USB device with java. Most of the posts from StackOverflow recommend to use usb4java, for which the latest activity seems to be in early 2014, so I am not sure if it is still active.
Anyways, digging a little bit into it, on the page, it says “It is based on the native libusb 1.0 library”. Then trying to install the device driver, I used Zadig as recommended (https://github.com/libusb/libusb/wiki/Windows#How_to_use_libusb_on_Windows).
I got usb4java to work by using the “libusb-win32” driver. But when you go to the “libusb-win32” website, it mentions that “libusb-win32 is a port of the USB library libusb-0.1”, and even more in here http://libusb.org/wiki/APIs#libusb-0.1legacyAPI in mentions that “Development status: libusb-0.1 is deprecated and will have no further changes or releases”
This is all very confusing, so first I would like to know if USB4Java uses libusb 1.0 or 0.1. And also is it recommended for any new development or what would be the way to go, there does not seem to be too much support for USB devices on using JAVA.
UPDATE:
Thanks dryman. Great explanation. My problem with usb4java combined with libusb-win32 is latency. My transactions take place every 10ms and I can't afford to lost data, that is why I had to start buffering and I have 15 buffers, so my max delay would be 150ms.While evaluating usb4java with libusb-win32 for long periods (hours), I see that from time to time I have latencies of more than 1s. I did try WinUSB but for some reason I get:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000006b6051c2, pid=6284, tid=6836
#
# JRE version: Java(TM) SE Runtime Environment (8.0_45-b14) (build 1.8.0_45-b14)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [libusb-1.0.dll+0x51c2]
Now that you mention that it might be more reliable, I will give it a try and I will try to fix it.
BTW: the access violation happens when I try submit the transfer (LibUsb.submitTransfer(transfer);) within the code below.
public static void write(DeviceHandle handle, byte[] data,
TransferCallback callback)
{
ByteBuffer buffer = BufferUtils.allocateByteBuffer(data.length);
buffer.put(data);
Transfer transfer = LibUsb.allocTransfer();
LibUsb.fillBulkTransfer(transfer, handle, OUT_ENDPOINT, buffer,
callback, null, TIMEOUT);
// System.out.println("Sending " + data.length + " bytes to device");
int result = LibUsb.submitTransfer(transfer);
if (result != LibUsb.SUCCESS)
{
throw new LibUsbException("Unable to submit transfer", result);
}
}
First a thing or two to the different libusbs. The thing is there existed a fair share of different implementations and drivers. The libusb original was forked to libusbx. libusb original failed and libusbx took over libusb as name. So there is even more confusion. There are 3 different drivers for Windows working with libusb1 (in chronical order): libusb0 (libusb-win32), libusbK and WinUSB.
Now about usb4java. As far as I know they use libusb1. libusb1 is still capable to work with the libusb0 driver through libusbK but I recommend using WinUSB instead because in my personal experience it works better and the guys from libusb recommend it themselfs too. The libusb0 support seems to be some kind of courtesy only.
You don't write anything whether you tried WinUSB or how it didn't work so I recommend you try it out or ask a question about your problems with WinUSB.