4

I am trying to send a message to my USB device (Silicon Labs USB-UART Bridge) using this code:

public void sendMessage(UsbInterface iface, String message,
        int i){

    UsbPipe pipe = null;

    try {
        iface.claim(new UsbInterfacePolicy() {
            @Override
            public boolean forceClaim(UsbInterface usbInterface) {
                return true;
            }
        });

        UsbEndpoint endpoint = (UsbEndpoint) iface.getUsbEndpoint((byte) i);
        pipe = endpoint.getUsbPipe();
        pipe.open();

        int sent = pipe.syncSubmit(message.getBytes());

        System.out.println(sent + " bytes sent");
        pipe.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            iface.release();
        } catch (UsbClaimException e) {
            e.printStackTrace();
        } catch (UsbNotActiveException e) {
            e.printStackTrace();
        } catch (UsbDisconnectedException e) {
            e.printStackTrace();
        } catch (UsbException e) {
            e.printStackTrace();
        }
    }
}//sendMessage

When I execute it:

public static void main(final String[] args) throws UsbException {

    Usb4JavaHigh usb4java = new Usb4JavaHigh();
    UsbDevice usbDevice = usb4java.findDevice((short) (0x10C4), (short) (0xEA60));

    usb4java.sendMessage(usb4java.getDeviceInterface(usbDevice, 0), "01FF05FB13", 0x81);
    usb4java.readMessage(usb4java.getDeviceInterface(usbDevice, 0), 0x01);
}

I get this Error:

javax.usb.UsbPlatformException: USB error 1: Transfer error on bulk endpoint: Input/Output Error
at org.usb4java.javax.ExceptionUtils.createPlatformException(ExceptionUtils.java:39)
at org.usb4java.javax.IrpQueue.transferBulk(IrpQueue.java:239)
at org.usb4java.javax.IrpQueue.transfer(IrpQueue.java:197)
at org.usb4java.javax.IrpQueue.read(IrpQueue.java:126)
at org.usb4java.javax.IrpQueue.processIrp(IrpQueue.java:76)
at org.usb4java.javax.AbstractIrpQueue.process(AbstractIrpQueue.java:104)
at org.usb4java.javax.AbstractIrpQueue$1.run(AbstractIrpQueue.java:73)
at java.lang.Thread.run(Unknown Source)

However I get no Errormessage for my readMessage, does anyone know a reason why this is so or has got a hint?

Thanks in advance!

Edit: The code works out when I change the Endpoint to 0x01 but that one is the EP 1 OUT, 0x81 is the EP 1 IN, so thats where I am supposed to send my messagesor not?

Edit 2: Code of readMessage:

public void readMessage(UsbInterface iface, 
        int j){

    UsbPipe pipe = null;

    try {
        iface.claim(new UsbInterfacePolicy() {
            @Override
            public boolean forceClaim(UsbInterface usbInterface) {
                return true;
            }
        });

        UsbEndpoint endpoint = (UsbEndpoint) iface.getUsbEndpoint((byte) j); // there can be more 1,2,3..
        pipe = endpoint.getUsbPipe();
        pipe.open();

        /*pipe.addUsbPipeListener(new UsbPipeListener()
        {            
            @Override
            public void errorEventOccurred(UsbPipeErrorEvent event)
            {
                UsbException error = event.getUsbException();
                error.printStackTrace();
            }

            @Override
            public void dataEventOccurred(UsbPipeDataEvent event)
            {
                byte[] data = event.getData();

                System.out.println(data + " bytes received");
                for(int i =0 ; i<data.length; i++){System.out.print(data[i]+" ");}
            }
        });*/

        byte[] data = new byte[8];
        int received = pipe.syncSubmit(data);
        System.out.println(received + " bytes received");
        for(int i =0 ; i<data.length; i++){System.out.print(data[i]+" ");}//*/

        pipe.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            iface.release();
        } catch (UsbClaimException e) {
            e.printStackTrace();
        } catch (UsbNotActiveException e) {
            e.printStackTrace();
        } catch (UsbDisconnectedException e) {
            e.printStackTrace();
        } catch (UsbException e) {
            e.printStackTrace();
        }
    }

}
pilgrimm
  • 43
  • 1
  • 5
  • "The code works out when I change the Endpoint to 0x01 but that one is the EP 1 OUT" - I suspect a confusion of terms. I guess "out" is seen from client's perspective. That is **your** _out_ - which is the pipe's _in_ ... – Fildor Aug 17 '16 at 09:29

2 Answers2

0

Yes Fidor is right 0x01 is for writing and 0x81 for reading. IN and OUT is from the perspective of the host controller.

You can read from IN endpoints and write to OUT endpoints. Only exception is Control Transfer which is direction independent.

dryman
  • 660
  • 6
  • 16
  • Well when I try to read from 0x81 I get the exact same errormessage for my readMessage. sendMessage seems to work though. – pilgrimm Aug 17 '16 at 11:36
  • Exact same means also USB error 1? Have you looked into the endpointdescriptors of these endpoints do exist? – dryman Aug 17 '16 at 11:43
  • Yes, the same error as described above, I got my Endpoint descriptors using a device dump, those are the descriptors: – pilgrimm Aug 17 '16 at 11:57
  • I can't see any. Have you clicked Add Comment? – dryman Aug 17 '16 at 12:42
  • So the endpoint is correct. There has to be something the matter with the transfer. Can you edit the question with the readMessage source code? – dryman Aug 17 '16 at 13:37
  • I found another way by treating my Port as COM Port and thus avoiding usb4java but thanks a lot for your help so far! :) – pilgrimm Aug 17 '16 at 15:54
  • OK I thought you already know this is a COM emulation device and just wanted to write your own driver. :) I'm glad to hear you found a solution. – dryman Aug 18 '16 at 08:18
0

Sorry here are the descriptors I got:

Endpoint Descriptor:
  bLength                  7
  bDescriptorType          5
  bEndpointAddress      0x01  EP 1 OUT
  bmAttributes             2
    Transfer Type             Bulk
    Synch Type                None
    Usage Type                Data
  wMaxPacketSize          64
  bInterval                0

Endpoint Descriptor:
  bLength                  7
  bDescriptorType          5
  bEndpointAddress      0x81  EP 1 IN
  bmAttributes             2
    Transfer Type             Bulk
    Synch Type                None
    Usage Type                Data
  wMaxPacketSize          64
  bInterval                0
pilgrimm
  • 43
  • 1
  • 5