5

I try to read from a usb device through usb4java api but I get an error: USB error 5: Unable to read data: Entity not found can anybody help me? It has to be noted that the endpoint_in i get it from LibUsb.ENDPOINT_IN and i pass it to the read funtion The device is found is claimed but I cannot move forward to read from the device.

My code is given below:




import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import javax.swing.JOptionPane;

import org.usb4java.BufferUtils;
import org.usb4java.Context;
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 {
 private static byte endpoint;
   // private static final byte IN_ENDPOINT = 0;
private static Device device;
private static   Context context = new Context();
private static DeviceHandle handle;

    public static void main(String[] args) {

        // Create the libusb context
    LibUsb.init(null);
        findDevice(context, (short) (0x046D), (short) (0xC016));
        // Deinitialize the libusb context
        LibUsb.exit(context);

    }

    public static void findDevice(Context context, short vendorId, short productId) {

        // Initialize the libusb context
        int result = LibUsb.init(context);
        if (result < 0) {
            throw new LibUsbException("Unable to initialize libusb", result);
        }

        // Read the USB device list
        DeviceList list = new DeviceList();
        result = LibUsb.getDeviceList(context, list);
        if (result < 0) {
            throw new LibUsbException("Unable to get device list", result);
        }

        try {
            // Iterate over all devices and list them
            for (Device device : list) {

                int address = LibUsb.getDeviceAddress(device);
                int busNumber = LibUsb.getBusNumber(device);
                DeviceDescriptor descriptor = new DeviceDescriptor();
                result = LibUsb.getDeviceDescriptor(device, descriptor);

                if (result < 0) {
                    throw new LibUsbException(
                            "Unable to read device descriptor", result);
                }
                System.out.format(
                        "Bus %03d, Device %03d: Vendor %04x, Product %04x%n",
                        busNumber, address, descriptor.idVendor(),
                        descriptor.idProduct());

                if (result != LibUsb.SUCCESS) {
                    throw new LibUsbException("Unable to read device descriptor", result);
                }
                if (descriptor.idVendor() == vendorId && descriptor.idProduct() == productId) {

                    System.out.println("Device Found");
                    getDeviceHandle(device);
                    LibUsb.claimInterface(handle, 0);
                }

            }
        } finally {
            // Ensure the allocated device list is freed
            LibUsb.freeDeviceList(list, true);
        }
    }

    public static void getDeviceHandle(Device device) {
         endpoint = (byte)LibUsb.getDeviceAddress(device);
          handle = new DeviceHandle();
          JOptionPane.showMessageDialog(null, "endpoint="+endpoint);

        int result = LibUsb.open(device, handle);


        if (result != LibUsb.SUCCESS) {
            throw new LibUsbException("Unable to open USB device", result);
        }

        try {
            // Use device handle here
            claimDevice(handle, 0);


        } finally {
            LibUsb.close(handle);
        }
    }

    public static void claimDevice(DeviceHandle handle, int interfaceNumber) {


        int result = LibUsb.claimInterface(handle, interfaceNumber);

        if (result != LibUsb.SUCCESS) {
            throw new LibUsbException("Unable to claim interface", result);
        }
        try {

            System.out.println("Device Claimed");
            //sendData(handle);
           // read(handle,1000);
            JOptionPane.showMessageDialog(null, "interface="+interfaceNumber);
           read(handle, 2048);






        } finally {
            result = LibUsb.releaseInterface(handle, interfaceNumber);
            if (result != LibUsb.SUCCESS) {
                throw new LibUsbException("Unable to release interface", result);
            }
        }
    }

    @SuppressWarnings("unused")







    public static void sendData(DeviceHandle handle) {

    char[] initEP = new char[]{0x1b, '@'};
    char[] cutPaper = new char[]{0x1d, 'V', 1};

      String initStr = new String(initEP);
      String cutStr = new String(cutPaper);
        String text = "blabla \n\n\n";

        ByteBuffer buffer = ByteBuffer.allocateDirect(8);
        buffer.put(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
        // SEND INIT BYTE
       int transfered = LibUsb.controlTransfer(handle,
             (byte) (LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_INTERFACE),
             (byte) 0x01, (short) 2, (short) 1, buffer, 5000);

     if (transfered < 0) {
           throw new LibUsbException("Control transfer failed", transfered);
        }

        // SENDT TEXT
        buffer = ByteBuffer.wrap(text.getBytes());

       transfered = LibUsb.controlTransfer(handle,
                (byte) (LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_INTERFACE),
                (byte) 0x01, (short) 2, (short) 1, buffer, 5000);

        if (transfered < 0) {
            throw new LibUsbException("Control transfer failed", transfered);
        }

      /*  // SENDT CUT BYTE
        buffer = ByteBuffer.wrap(text.getBytes());

        transfered = LibUsb.controlTransfer(handle,
                (byte) (LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_INTERFACE),
                (byte) 0x09, (short) 2, (short) 1, buffer, 5000);

        if (transfered < 0) {
            throw new LibUsbException("Control transfer failed", transfered);
        } */
        System.out.println(transfered + " bytes sent");
    }

    public static ByteBuffer read(DeviceHandle handle, int size)
    {


         ByteBuffer buffer = BufferUtils.allocateByteBuffer(size).order(ByteOrder.LITTLE_ENDIAN);
         IntBuffer transferred = BufferUtils.allocateIntBuffer();

       // IntBuffer transferred = BufferUtils.allocateIntBuffer();
        long TIMEOUT = 5000;


        JOptionPane.showMessageDialog(null, "endpoint_in="+ LibUsb.ENDPOINT_IN);
        JOptionPane.showMessageDialog(null, "endpoint_out="+ LibUsb.ENDPOINT_OUT);
        JOptionPane.showMessageDialog(null, "handle="+handle);

        int result = LibUsb.bulkTransfer(handle, (byte)-128, buffer,
            transferred, TIMEOUT);

        JOptionPane.showMessageDialog(null, result);

        if (result != LibUsb.SUCCESS)
        {
            throw new LibUsbException("Unable to read data", result);
        }
        else{System.out.println(transferred.get() + " bytes read from device");

        }


        return buffer;
         }

}

The device is an usb mouse is that the problem?

In order to write i use this code (the write function that I call in my main function):

public static ByteBuffer write(DeviceHandle handle, int size) { ByteBuffer buffer = ByteBuffer.allocateDirect(8); buffer.put(new byte[] { 'p' });

    String text="p";

    //ByteBuffer buffer = ByteBuffer.wrap(text.getBytes());

    IntBuffer transferred = BufferUtils.allocateIntBuffer();
    long TIMEOUT = 5000;
    int result = LibUsb.bulkTransfer(handle, (byte) 0x01, buffer,
            transferred, TIMEOUT);


    if (result != LibUsb.SUCCESS)
    {
        throw new LibUsbException("Unable to write data", result);
    }
    else{System.out.println(transferred.get() + " bytes written to the device");

    }



    return buffer;


}

but I get USB error 5: Unable to write data: Entity not found

Why the entity is not found since I use endpoint 0x01...
javac31
  • 91
  • 2
  • 11
  • Your code works better than mine. You used some different calls to get the VENDOR_ID and PRODUCT_ID. So you answered my question, so I +1 this question. Again your code seemed to work fine, again the IDs did not match. – cliff2310 Jun 07 '16 at 23:29
  • Can you read something from the usb device withthe code? – javac31 Jun 08 '16 at 06:10
  • Are you absolutly positive that your device has an endpoint 0x81? Can you show the interfacedescriptor? – dryman Jun 08 '16 at 09:47
  • Why endpoint 0x81 i cannot understand what you mean – javac31 Jun 08 '16 at 10:43
  • With endpoint set to 0x81 I get USB error 1: Unable to read data: Input/Output Error which must be normal if there are no data to read from the usb device actually it is a usb mouse... I now try to write to the usb port but how i do it??? – javac31 Jun 08 '16 at 11:18
  • Mr Dryman do you know how can I write to the usb port ? The endpoint should be 1 is that true? – javac31 Jun 09 '16 at 10:29
  • Somehow SO didn't notify me of changes here. I'm sorry for the late answer. If you want to write yes endpoint 0x01 is _most likely_ the right one but from you code I figured you wanted to read that is why I was asking about 0x81. But the question remains: Can you post the interface or at least the endpoint descriptor? We don't really know if there actually is an endpoint 0x01 before looking at the descriptor. From the exception I guess there simply is no endpoint 0x01 to write to. (Just as question: What do you want to tell a mouse? Mice are normally read devices aren't they?) – dryman Jun 10 '16 at 08:01
  • Thank you very much for your attention, how can I print the interface or the endpoint descriptor?the interface that I try to print is just an integer that this function LibUsb.claimInterface(handle, 0); returns,can you please explain how this works... – javac31 Jun 10 '16 at 09:20
  • The mouse that I try to communicate is the only device that does not through this exception: USB error 12: Unable to open USB device: Operation not supported or unimplemented on this platform. So i try to experiment with the mouse :-). – javac31 Jun 10 '16 at 09:21
  • Printing the device descriptor i get these lines of message:Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 2.00 bDeviceClass 0 Per Interface bDeviceSubClass 0 bDeviceProtocol 0 bMaxPacketSize0 8 idVendor 0x046d idProduct 0xc016 bcdDevice 3.40 iManufacturer 1 iProduct 2 iSerial 0 bNumConfigurations 1 – javac31 Jun 10 '16 at 09:44
  • Another one question that I have is that if we can use this api to communicate through a usb to serial converter with a device that only has serial port for communication. This is a radioacitvity sensor that I want to communicate with and by pressing 0 i get the status of the sensor.Can i write read with usb4java api??? – javac31 Jun 10 '16 at 10:05
  • Now the only thing that I get as far as the mouse is concerned is this error:USB error 1: Input/Output Error for writing and for reading with -127 and 0x81 endpoints respectively. All the other devices I have tried to communicate with, printers, the sensor, etc give me this error:USB error 12: Unable to open USB device: Operation not supported or unimplemented on this platform. – javac31 Jun 10 '16 at 12:05
  • This is only the device descriptor. We need a list of the endpoint descriptors. Every device may have multiple configurations. Every configuration multiple interfaces and each interface multiple endpoints. How to get down there I don't know because I am not familiar with usb4java but only libusb and USB in general. I suggest the class DescriptorUtils and their dump methods. – dryman Jun 10 '16 at 14:42
  • The problem with the opening is probably a missing backend on Windows. What driver backend if any did you install for the device? And about the serial usb converter: Yes you can if you replace the driver with a libusb compatible driver backend. All of this assumes you are working on Windows. – dryman Jun 10 '16 at 14:44
  • Thank you again for your help...I tried this:DescriptorUtils pape=null; JOptionPane.showMessageDialog(null,pape.dump(descriptor)); but I get the device descriptor information, I cannot access the endpoint descriptor... – javac31 Jun 10 '16 at 16:21
  • Can you try what this code prints (better use pastebin or the like)? https://github.com/usb4java/usb4java-examples/blob/master/src/main/java/org/usb4java/examples/DumpDevices.java – dryman Jun 13 '16 at 13:34
  • Hallo, I tried it and I get the following:Device 002/001 Parent: 002/000 Speed: Unknown Unable to open device: Operation not supported or unimplemented on this platform. Continuing without device handle.Exception in thread "main" org.usb4java.LibUsbException: USB error 5: Unable to read config descriptor: Entity not found at DumpDevices.dumpConfigurationDescriptors(DumpDevices.java:40) at DumpDevices.dumpDevice(DumpDevices.java:116) at DumpDevices.main(DumpDevices.java:156) – javac31 Jun 14 '16 at 07:01
  • Bus 002, Device 001: Vendor 8086, Product 3b3c Bus 001, Device 001: Vendor 8086, Product 3b34 Bus 001, Device 003: Vendor 046d, Product c016 Device Found Device Claimed Device 001/003 Connected to port: 2 Parent: 001/002 Speed: Low Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 2.00 bDeviceClass 0 Per Interface bDeviceSubClass 0 bDeviceProtocol 0 bMaxPacketSize0 8 idVendor 0x046d idProduct 0xc016 – javac31 Jun 14 '16 at 08:08
  • bcdDevice 3.40 iManufacturer 1 Logitech iProduct 2 Optical USB Mouse iSerial 0 bNumConfigurations 1 Configuration Descriptor: bLength 9 bDescriptorType 2 wTotalLength 34 bNumInterfaces 1 bConfigurationValue 1 iConfiguration 0 bmAttributes 0xa0 (Bus Powered) Remote Wakeup bMaxPower 100mA extralen 0 extra: Interface: numAltsetting 1 – javac31 Jun 14 '16 at 08:09
  • Interface Descriptor: bLength 9 bDescriptorType 4 bInterfaceNumber 0 bAlternateSetting 0 bNumEndpoints 1 bInterfaceClass 3 HID bInterfaceSubClass 1 bInterfaceProtocol 2 iInterface 0 extralen 9 extra: 09 21 10 01 00 01 22 34 00 Endpoint Descriptor: bLength 7 bDescriptorType 5 bEndpointAddress 0x81 EP 1 IN – javac31 Jun 14 '16 at 08:09
  • bmAttributes 3 Transfer Type Interrupt Synch Type None Usage Type Data wMaxPacketSize 4 bInterval 10 extralen 0 extra: Bus 001, Device 005: Vendor 090c, Product 37b3 Bus 001, Device 002: Vendor 8087, Product 0020 Bus 002, Device 002: Vendor 8087, Product 0020 – javac31 Jun 14 '16 at 08:09
  • My last 4 comments is the result of dumping my device. Where can I move forward now to??? – javac31 Jun 14 '16 at 08:10
  • Thanks again for your help!!! – javac31 Jun 14 '16 at 11:23
  • Sadly this device has no write endpoint. So you can not write to it, only read (like I guess every mouse is). so all that is left is the question what driver backend if any you use (see my second comment of Jun 10). – dryman Jun 14 '16 at 16:05
  • Unfortunately I dont have the instrument, I will have it on September but until then I should be familiar with usb programming in order to accomplish this task that's the reason i try with other devices...but unfortunately I get this error:USB error 12: Unable to open USB device: Operation not supported or unimplemented on this platform except from mouse that goes one step further with this error when I try to read:USB error 1: Unable to read data: Input/Output Error – javac31 Jun 16 '16 at 07:07
  • Yes, then you have to tell me which backend if any you use. Most likely you simply don't have a backend installed. If you can still use your mouse as mouse on windows you can not open it with libusb. To install a backend you can use Zadig. – dryman Jun 17 '16 at 07:11
  • By the term backend you mean the driver for the mouse?I tried to replace the driver for the mouse with zadig but i cannot use it from the same usb port anymore...what I am gonna do i am in serious trouble i think :-( – javac31 Jun 22 '16 at 09:35
  • And when trying with another instrument (an another radioactivity sensor) I get this error:USB error 12: Unable to open USB device: Operation not supported or unimplemented on this platform :-( – javac31 Jun 23 '16 at 09:08
  • Of course you can not use it if you replace the driver. But without it you can not access it with libusb. Either you can access the usb device or Windows can. Did you use zadig with the radioactivity sensor? – dryman Jun 28 '16 at 08:22
  • I used zadig with the radioactivity sensor that has the serial port communication and i write to it but when I try to read I get the buffer but I cannot printout the buffer, have you got any idea how to get this buffer into a string or a char array? – javac31 Jun 28 '16 at 08:41
  • the code is this: write4(handle, "0".getBytes()); Buffer papeole= read(handle,100000); the 0 char that I write to is the get status command so I should read the status of the sensor which is a string.But how I can do that?I only have this Buffer papeole.Can you help me??? – javac31 Jun 28 '16 at 10:33
  • The only thing I can name (if papeole is a ByteBuffer) is <> Just use your encoding or stuff. So you need to get a byte array and then interpret it as string with the right encoding. – dryman Jun 29 '16 at 07:50
  • I try to read the response from the sensor but the result is a chinese string :-( it should be a normal string with information about the status of the sensor. Moreover the write operation does not work because i try to write the 'p' character which powers up the sensor but the sensor does not do anything. :-( – javac31 Jun 29 '16 at 11:47
  • The thing is I don't know how the sensor works. Libusb is only for transfering data not interpreting it. I suggest you use an usb sniffer with the old and the new driver and compare the data that is sent and received to try to find the difference. If it is the case that both transfer the same data or you skip the sniffer you should try different encodings and reading the the documentation of the sensor. Last resort would be looking at the bytes and trying to make sense of them. But with all that I can not really help you because I don't know anything about the device. – dryman Jun 30 '16 at 11:16
  • Could you please recommend me an usb sniffer? – javac31 Jul 01 '16 at 07:34
  • The only permanently free usb sniffer I know is Free USB Analyzer from HHD Software. But it is restricted to 5 sessions of 10 minute monitoring each day. A good shareware with 30 day trail is USBlyzer. In my opinion its better but still it is only trail. – dryman Jul 01 '16 at 10:02
  • Hii, I stopped writing because I will have the sensor on September I hope we will continue our conversation then :-) – javac31 Jul 18 '16 at 07:36
  • Mr Dryman are you available??? – javac31 Aug 31 '16 at 08:15

1 Answers1

1
  • Download libusbK form the below URL.
  • Remove Zadig driver.
  • Install libusbK driver.

URL: https://sourceforge.net/projects/libusbk/

Harshil
  • 463
  • 1
  • 8
  • 28