使用usb4java将消息发送到USB设备 - 输入/输出错误

时间:2016-08-17 08:42:47

标签: java usb libusb-1.0 usb4java libusb-win32

我正在尝试使用以下代码向我的USB设备(Silicon Labs USB-UART Bridge)发送消息:

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

执行时:

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);
}

我收到此错误:

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)

但是我的readMessage没有得到Errormessage,有没有人知道为什么会这样或者有提示?

提前致谢!

编辑:当我将端点更改为0x01但是那个是EP 1 OUT,0x81是EP 1 IN时,代码工作正常,那么我应该在哪里发送我的消息呢?

编辑2: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();
        }
    }

}

2 个答案:

答案 0 :(得分:0)

是Fidor是对的0x01用于写入,0x81用于读取。 IN和OUT是从主控制器的角度来看的。

您可以从IN端点读取并写入OUT端点。唯一的例外是Control Transfer,它与方向无关。

答案 1 :(得分:0)

对不起,这里是我得到的描述符:

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