我正在尝试从通过 USB 连接到带有usb4java库的 Windows 10pro 机器的 RFID 阅读器读取消息。
我设法声明了接口,打开了管道并为数据注册了监听器,但是监听器从未被触发。阅读器充当键盘,它读取的任何内容最终都会出现在活动应用程序中,例如我打开的 IDE,而不是在listener中。
UsbInterface usbInterface = activeInteface(device);
// there is only one endpoint in the list
UsbEndpoint endpoint = (UsbEndpoint)usbInterface.getUsbEndpoints().get(0);
UsbPipe pipe = endpoint.getUsbPipe();
try {
usbInterface.claim();
// true
System.out.println("claimed usb interface: " + usbInterface.isClaimed());
pipe.open();
// true
System.out.println("pipe open: " + pipe.isOpen());
pipe.addUsbPipeListener(new MessageListener());
// true
System.out.println("pipe active: " + pipe.isActive());
// keep main thread alive, async call should be done from another thread i guess
Thread.sleep(15000);
}
catch (Exception any) {System.out.println(any);}
}
和听众:
private static class MessageListener implements UsbPipeListener {
@Override
public void errorEventOccurred(UsbPipeErrorEvent event) {
System.out.println(event.toString() + " , " +event.getUsbException());
}
@Override
public void dataEventOccurred(UsbPipeDataEvent event) {
// this code block never triggers
System.out.println("listener ...);
int actualLength = event.getActualLength();
System.out.println("length: " + actualLength);
byte[] data = event.getData();
System.out.println("data length " + data.length);
}
}
我还在上面的块中尝试了同步读取而不是异步,如下所示:
byte[] buffer = new data[8];
// this fails on its own, don't even need to read something with RFID reader
int received = pipe.syncSubmit(buffer);
失败:
USB error1: Transfer error on interrupt endpoint: Input/Output error
库支持一些特定于 Windows 的属性:org.usb4java.javax.useUSBDK = true
但是当我尝试将其设置为异常时失败。
我对 USB 设备的经验为 0,所以不知道如何从这里开始。代码有问题吗,我需要 USBDK 还是设备不支持 libUSB 驱动程序?遗憾的是,这不是我的设备,我无权访问设备的文档,因此无法确定是否是设备驱动程序问题。