3

Under Windows 8.1, with LibUsb.getDeviceList(null,connectedDevices), I successfully get each and every USB device connected to my PC with the following code :

connectedDevices= new DeviceList();
if(LibUsb.getDeviceList(null,connectedDevices){
    selectedDevices= new LinkedList<Device>();
    deviceDescriptor= new DeviceDescriptor();
    pos=connectedDevices.iterator();
    while(pos.hasNext()){
        device=pos.next();
        if(LibUsb.getDeviceDescriptor(device,deviceDescriptor)==LibUsb.SUCCESS){
            selectedDevices.add(device);
            LibUsb.refDevice(device);
        }
    }
}
LibUsb.freeDeviceList(connectedDevices,false);

but when I want to retrieve a string descriptor for a specific device whose "iManufacturer" is not null with :

StringBuffer buffer=new StringBuffer(1000);
device=selectedDevices.getFirst();
LibUsb.getDeviceDescriptor(device, deviceDescriptor);
handle=new DeviceHandle();
if (LibUsb.open(device,handle)==LibUsb.SUCCESS) {   
    LibUsb.getStringDescriptorAscii(handle,deviceDescriptor.iManufacturer(),buffer);            
    manufacturerDescription=buffer.toString();                  
    LibUsb.close(handle);
}

I either get the LIBUSB.ERROR_NO_DEVICE error code in return to the openfunction, which I find strange for a device discovered only a few milliseconds before or the LIBSUSB.ERROR_INVALID_PARAM error code in return to getDescriptor. And using LibUsb.getStringDescriptor(handle,deviceDescriptor.iManufacturer(),(short)0x409,buffer)fails as well with the same error code...

With LibUsb.getStringDescriptor(handle,deviceDescriptor...), I succeed in getting the string for the manufacturer and the product for two out of seventeen devices which returns a non-null value for iManufacturerand iProduct. I thought the reason others weren't retrieved was linked to the 127 bytes limitation but using getStringDescriptorAscii(handle,deviceDesriptor...,buffer), with a buffer sized at 1000 bytes, isn't better.

What am I missing?

Zelig63
  • 1,592
  • 1
  • 23
  • 40
  • Comparison may be the way? `LibUsb.open(devicedescriptor,handle).equals(LibUsb.SUCCESS)` – Nikolas Charalambidis Aug 28 '16 at 07:41
  • I don't think so. The `open` function returns an simple `int`... – Zelig63 Aug 28 '16 at 09:36
  • Can you show the code part where you call `LibUsb.getDeviceList`? Is it maybe the case you call `LibUsb.freeDeviceList(connectedDevices, true)` before your attempt to open the device? – dryman Aug 30 '16 at 12:10
  • I see no bug in your device selection. You call refDevice so it should still be there when you want to open it. Have you tried different devices? Do all devices throw this error? Sidenote: On what OS do you work? – dryman Sep 02 '16 at 14:25
  • Question edited to reflect my last investigations. The `open` function succeed for some devices but then, the `getDescriptor` fails... – Zelig63 Sep 02 '16 at 17:16
  • The thing is I don't know if the problem may be that you don't use a libusb compatible driver on Windows. Windows is rellay restrictive if it comes to drivers. Maybe try Zadig on one of the devices that don't work (please beware of hubs and your mouese/keyboard because they won't work with Windows until you change the driver back) – dryman Sep 06 '16 at 08:30
  • Thanks for the idea. I think I'm going to test my code under Linux first. – Zelig63 Sep 06 '16 at 17:21
  • No improvement under Linux... (Debian) – Zelig63 Sep 17 '16 at 06:53

1 Answers1

0

I think you got the methods wrong. Using getStringDescriptor instead should fix it. But I am not aware of a language id 1000. Nevertheless the casting to byte of a short should have been a dead giveaway. ;)

dryman
  • 660
  • 6
  • 16
  • Using `getStringDescriptor`won't help : as the `open` function returns an error, the `getDescriptor`function is not even executed... For the second point, you are right that converting 1000 to a `byte`is a non sense, but as I was focused elsewhere, I didn't see it. Additionnaly, I have only found a documentation for the C LibUSB, and there seem to have some differences with Java LibUSB (especially and not surprinsingly, with the types of the arguments - C is not object oriented). Does a Java LibUsb documentation exist somewhere? – Zelig63 Aug 29 '16 at 16:38
  • Oh ok you never mentioned in your question where the exception occured so I assumed it happens in the `getStringDescriptor` method. You can find an api doc here: http://usb4java.org/apidocs/org/usb4java/LibUsb.html – dryman Aug 30 '16 at 12:09
  • Sorry! Question edited to specify it. And thanks for the link. – Zelig63 Aug 31 '16 at 15:12
  • Did you see the comment I made to the question at the same time? – dryman Sep 01 '16 at 07:50
  • Which comment? The one starting with "Oh ok..."? – Zelig63 Sep 01 '16 at 15:16
  • No the one at your question (not my answer) about how you call getDeviceList. – dryman Sep 02 '16 at 11:37
  • I have added my call to `getDeviceList` in the question. – Zelig63 Sep 02 '16 at 13:25