使用Java从USB HID RFID阅读器读取字符串

时间:2015-11-12 14:26:02

标签: java usb usb4java

我试图从通过USB连接的RFID读取器读取字符串。在我的appliaction中正确识别Reader。但我不知道如何将传输的字符读入字符串。

如果我不拆卸设备,则会像键盘一样打印字符串(正如您对HID所期望的那样)。我想要的是仅在我的Java应用程序中捕获该String。这就是我拆卸USB设备的原因。

例如,我的应用程序打印'''#$&'到控制台(见下面的代码)或类似的东西

[0,0,39,0,0,0,0,0,0,0,0,0,0,0,0,0,0,34,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,39,0,0,0,0,0,0,0,0,0,0,0,0,0,0,35,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 38,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30,0,0,0,0,0,0,0,0, 0,0,0,0,0]

但我想读的是05006790。

我认为我的尝试中有一个愚蠢的错误。我希望有人可以帮我弄清楚如何正确地将字节读入字符串。

非常感谢。

代码跟在这里

Context context = new Context();
    int result = LibUsb.init(context);

    DeviceList list = new DeviceList();
    result = LibUsb.getDeviceList(context, list);

    for (Device device: list)
    {
        int address = LibUsb.getDeviceAddress(device);
        int busNumber = LibUsb.getBusNumber(device);

        DeviceDescriptor descriptor = new DeviceDescriptor();
        DeviceHandle handle = new DeviceHandle();

        int resultOpen = LibUsb.open(device, handle);
        // if (resultOpen < 0) // handle = null;

        int resultDescriptor = LibUsb.getDeviceDescriptor(device, descriptor);
        // if (resultDescriptor< 0) // handle = null;

        if(descriptor.idVendor() == 0x8ff && descriptor.idProduct() == 0x0009)
        {
            System.out.println("found");
            LibUsb.detachKernelDriver(handle, 0);
        }
    }

    UsbServices services = UsbHostManager.getUsbServices();
    UsbDevice deviceHigh = findDevice(services.getRootUsbHub(), (short) 0x8ff, (short) 0x0009);


    if(deviceHigh != null)
    {
        System.out.println("found high");

        UsbConfiguration configuration = deviceHigh.getActiveUsbConfiguration();
        UsbInterface iface = configuration.getUsbInterface((byte) 0x00);

        iface.claim();

        UsbEndpoint endpoint = iface.getUsbEndpoint((byte) 0x81);
        UsbPipe pipe = endpoint.getUsbPipe();

        pipe.open();

        byte[] buffer = new byte[128];
        int rx = 0;
        rx = pipe.syncSubmit(buffer);

        System.out.printf("%d bytes received\n", rx);
        System.out.println(Arrays.toString(buffer));

        iface.release();
    }

0 个答案:

没有答案