我正在尝试使用以下代码向我的 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 没有收到错误消息,有没有人知道为什么会这样或有什么提示?
提前致谢!
编辑:当我将端点更改为 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();
}
}
}
最佳答案
是的 Fidor 是对的,0x01 用于写入,0x81 用于读取。
IN 和 OUT 是从主机 Controller 的角度来看的。
您可以从 IN 端点读取并写入 OUT 端点。唯一的异常(exception)是与方向无关的控制传输。
关于java - 使用 usb4java 向 USB 设备发送消息 - 输入/输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38991903/