简体   繁体   English

Java usb4java 从 windows 10 平台上的 usb 设备读取

[英]Java usb4java reading from usb device on windows 10 platform

I am trying to read a message from RFID reader connected via USB to windows 10pro machine with usb4java library.我正在尝试从通过 USB 连接到带有usb4java库的 windows 10pro 机器的 RFID 阅读器读取消息。

I have managed to claim the interface, opened pipe and registered listener for the data, however the listener is never triggered.我设法声明了接口,打开了 pipe 并为数据注册了侦听器,但是从未触发过侦听器。 The reader acts as keyboard and whatever it reads ends up in active application, such as IDE i have open, instead of in listener .阅读器充当键盘,它读取的任何内容最终都会出现在活动应用程序中,例如我打开的 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);}
 }

And the listener:和听众:

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

i have also tried synchronous read instead of asynchronous in the block above, like this:我还在上面的块中尝试了同步读取而不是异步读取,如下所示:

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

fails with:失败:

USB error1: Transfer error on interrupt endpoint: Input/Output error

There is some windows specific property that library supports: org.usb4java.javax.useUSBDK = true but this fails when i try to set it with an exception.库支持一些 windows 特定属性: org.usb4java.javax.useUSBDK = true但是当我尝试将其设置为异常时失败。

I have 0 experience with USB devices so not sure how to proceed from here.我有 0 次使用 USB 设备的经验,所以不确定如何从这里开始。 Is there something wrong with the code, do i need USBDK or device does not support libUSB driver?代码有问题吗,我需要 USBDK 还是设备不支持 libUSB 驱动程序? Sadly this is not my device and i don't have access to documentation of the device so cannot be sure if it is device driver issue.遗憾的是,这不是我的设备,我无法访问该设备的文档,因此无法确定它是否是设备驱动程序问题。

I know that this is 2 years old, but i've had similar issue and this was one of the first questions that i ran into looking for solution, which took me hours. 我知道这已经2岁了,但是我遇到了类似的问题,这是我遇到的第一个问题,寻求解决方案,这花了我几个小时。

So, basically, windows doesn't let to read/write keyboard devices directly, to do so, you have to override it's driver (That's why you're getting Input/Output error , and it's written in the hid4java's FAQ ). 因此,基本上,Windows不允许直接读取/写入键盘设备,为此,您必须覆盖它的驱动程序(这就是为什么您遇到Input/Output error ,并且它是在hid4java的FAQ中编写 )。

First way to override device driver is described in libusb wiki . libusb Wiki中描述了覆盖设备驱动程序的一种方法。 As far as i know you would have to install a new driver every time you connect the device to a new USB port, which is why i recommend you to read further. 据我所知,每次将设备连接到新的USB端口时,您都必须安装新的驱动程序,这就是为什么我建议您进一步阅读的原因。

Second way is what you've already mentioned, which is using UsbDk ( Usb Drivers Development Kit for Windows ). 第二种方法是您已经提到的,它使用的是UsbDk适用于Windows的USB驱动程序开发工具包 )。 It makes the device accessible for you by detaching the kernel driver and reattaching it back after you're done playing with it. 玩完之后,通过拆下内核驱动程序并将其重新连接,可以使您可以访问该设备。

In order to use it, you need to do two things: 为了使用它,您需要做两件事:

  1. Set the org.usb4java.javax.useUSBDK = true in you javax.usb.properties file as stated in the manual (this can also be done manually in low-level usb4java , see OPTION_USE_USBDK and setOption(Context, int) ). 按照手册中所述在您的javax.usb.properties文件中设置org.usb4java.javax.useUSBDK = true (这也可以在低级usb4java中手动完成,请参见OPTION_USE_USBDKsetOption(Context,int) )。
  2. Download and install UsbDk on your system (simplest way is to download x64 or x86 version msi installer which has GUI and is fully automated), which is sadly not in the manual (maybe it's obvious for some people, but took me amount of time that i am not proud of to realize). 在您的系统上下载并安装UsbDk (最简单的方法是下载具有GUI且完全自动化的x64或x86版本的msi安装程序),可惜的是不在手册中(也许对某些人来说很明显,但是花了我很多时间我不为实现这一目标感到骄傲。

Im guessing that the lack of second step is why OP has been getting an exception. 我猜测缺少第二步就是为什么OP一直例外。

Hope that this will help someone, knowing all this two days ago would save me a lot of headache. 希望这能对某人有所帮助,两天前就知道这一切将为我省去很多麻烦。

RFID readers operate in keyboard emulation mode by default.默认情况下,RFID 阅读器以键盘仿真模式运行。 You can normally get a tool from the manufacturer's website to configure the RFID reader.您通常可以从制造商的网站获得工具来配置 RFID 阅读器。 This will allow you to change the reader to HID mode.这将允许您将阅读器更改为 HID 模式。 This should resolve your issues.这应该可以解决您的问题。 Sorry for the late response but I hope it helps others.抱歉回复晚了,但我希望它能帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM