0

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);
        }
    }
Camilo Guevara
  • 165
  • 1
  • 12
  • What do you use as transfer mode? For reliable bandwidth and response time you should use isochronous transfers. Of course the device has to support that. – dryman Aug 10 '16 at 11:29
  • And about the Access Violation: I don't know what is happening there. Can you maybe post example code that triggers this error? – dryman Aug 10 '16 at 11:30
  • I am using isochronous bulk transfers. And the Access Violation happens within the code I just added in the original question – Camilo Guevara Aug 10 '16 at 12:40
  • You use **async** bulk transfer. Isochronous is a totally different transfer type. The thing with bulk transfer is that it only uses the bandwidth that is available. If there is any other data on the bus bulk has to wait. If you want to have a bandwidth reserved for your communication you have to use isynchronous transfer (fillIsoTransfer) but the device has to support it. With your code I can see no error (it is from the examples so it should be alright), there has to be an error in the dll or the arguments. – dryman Aug 10 '16 at 15:28
  • If you can not figure out a problem with WinUSB and libusb0 works for you I would go with that solutation rather than searching hours for a bug that may lie within libusb1.0. – dryman Aug 10 '16 at 15:31
  • Dryman, what you say makes total sense. I just checked and my device seems to support isochronous transfers. I just have to implement. Totally worthed to try. Will let you know if works as soon as is implemented – Camilo Guevara Aug 10 '16 at 15:55

1 Answers1

1

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.

dryman
  • 660
  • 6
  • 16
  • I just found this in the libusbx sourceforgepage: "libusbx is OBSOLETE - use http://libusb.info" and points to the libusb library – Camilo Guevara Aug 10 '16 at 11:34
  • Yes that is exactly what I wrote. libusbx forked libusb, libusb failed and after that libusbx renamed itself to libusb. Mind the difference between libusb.org and libusb.info. – dryman Aug 10 '16 at 11:39