2

I have this code:

package prospa8wusb;

import javax.swing.JOptionPane;

import org.usb4java.Device;
import org.usb4java.DeviceDescriptor;
import org.usb4java.DeviceHandle;
import org.usb4java.DeviceList;
import org.usb4java.LibUsb;
import org.usb4java.LibUsbException;

public class Test {
    public static short VENDOR_ID = (short)0x04B8;
    public static short PRODUCT_ID = (short)0x0005;

    public static void main(String[] args) {
        LibUsb.init(null) ;
    Device device=findDevice(VENDOR_ID,PRODUCT_ID);

    DeviceHandle handle = new DeviceHandle();
    int result = LibUsb.open(device, handle);
    if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to open USB device", result);
    try
    {
        // Use device handle here
    }
    finally
    {
        LibUsb.close(handle);
    }

    }
    public static Device findDevice(short vendorId, short productId)
    {
        // Read the USB device list
        DeviceList list = new DeviceList();
        int result = LibUsb.getDeviceList(null, list);
        if (result < 0) throw new LibUsbException("Unable to get device list", result);

        try
        {
            // Iterate over all devices and scan for the right one
            for (Device device: list)
            {
                DeviceDescriptor descriptor = new DeviceDescriptor();
                result = LibUsb.getDeviceDescriptor(device, descriptor);
                if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to read device descriptor", result);
                if (descriptor.idVendor() == vendorId && descriptor.idProduct() == productId)

                    JOptionPane.showMessageDialog(null, "ok");
                    return device;
            }
        }
        finally
        {
            // Ensure the allocated device list is freed
            LibUsb.freeDeviceList(list, true);
        }

        System.out.println("Device not found");
        return null;

    }   
}

and I get this error: USB error 4: Unable to open USB device: No such device (it may have been disconnected) although the usb printer that I want to communicate is connected to the usb port. can somebody help me?

javac31
  • 91
  • 2
  • 11
  • Running you code in debug mode, I see that the call DeviceHandle handle = new DeviceHandle(); returns null. Now I do not have that device, so this could be expected. Make sure your VENDOR_ID and PRODUCT_ID numbers are good. You might try my code from http://stackoverflow.com/questions/36972811/usb4java-and-windows-7-64-bit-os-i-get-usb-error-8-or-13 It does not work for me, but … good luck – cliff2310 Jun 06 '16 at 23:00
  • 1
    I think the problem may be that you free the device before you use it. LibUsb.freeDeviceList(list, true) frees all devices in the list. Call LibUsb.refDevice(device) before returning and I think it should work. Of course you should call LibUsb.unrefDevice(device) after closing the handle. – dryman Jun 08 '16 at 09:37

1 Answers1

1

Comment this line

LibUsb.freeDeviceList(list, true);

You are freeing the device before using it.