0

我正在开发一个应用程序,它应该将访问卡的使用记录到数据库中。我使用 usb4java 库与 USB RFID 阅读器设备进行通信。已更新其驱动程序以使用最新的 WinUsb v6.1.7600.16385 驱动程序。

添加了一个单独的线程来处理卡片数据的读取。

public class RfidReaderThread extends Thread {

  short VID = (short) 0xFFFF;
  short PID = (short) 0x0035;
  byte INTERFACE = (byte) 0;

  /**
   * If thread should abort.
   */
  private volatile boolean abort = false;

  /**
   * Aborts the event handling thread.
   */
  public void abort() {
    this.abort = true;
  }

  private UsbDevice device;
  private UsbPipe inputPipe;
  private Status status = Status.DISCONNECTED;
  private int maxSize=128;

  @Override
  public void run() {
    while (!this.abort) {
      try {
        Thread.sleep(100);
        if (status == Status.CONNECTED) {
          // read events
          try {
            byte[] data = new byte[maxSize];
            UsbIrp irp = inputPipe.createUsbIrp();
            irp.setData(data);
            inputPipe.asyncSubmit(irp);
            Thread.sleep(100);
            irp.complete();
          } catch (Exception e) { // disconnected
            log.debug("Read failed. Device disconnected");
            disconnect();
          }
        } else {
          if (tryConnect()) {
            status = Status.CONNECTED;
          } else {
            log.debug("Device not found");
          }
        }
      } catch (InterruptedException e) {
        log.error("Thread Interrupted Exception", e);
      } catch (UsbException e) {
        disconnect();
        log.error("USB connection Exception", e);
      }
    }
    disconnect();
  }


  private boolean tryConnect() throws UsbException {
    UsbServices services = UsbHostManager.getUsbServices();
    device = findDevice(services.getRootUsbHub(), VID, PID);
    if (device == null) {
      log.error("USB Reader not found");
      return false;
    }
    UsbInterface usbInterface = device.getActiveUsbConfiguration().getUsbInterface(INTERFACE);
    UsbEndpoint endpoint = (UsbEndpoint) usbInterface.getUsbEndpoints().get(0);
    log.debug("Endpoint -> direction: " + endpoint.getDirection() + ", type: " + endpoint.getType());
//    maxSize = endpoint.getUsbEndpointDescriptor().wMaxPacketSize();
    inputPipe = endpoint.getUsbPipe();
    usbInterface.claim(usbInterface1 -> true);
    // there is only one endpoint in the list
    // true
    log.debug("claimed usb interface: " + usbInterface.isClaimed());
    inputPipe.open();
    // true
    log.debug("pipe open: " + inputPipe.isOpen());
    inputPipe.addUsbPipeListener(inputPipeListener);
    // true
    log.debug("pipe active: " + inputPipe.isActive());
    // keep main thread alive, async call should be done from another thread i guess

    return true;
  }

  private boolean disconnect() {
    device = null;
    inputPipe = null;
    status = Status.DISCONNECTED;
    return true;
  }

  private UsbDevice findDevice(UsbHub hub, short vendorId, short productId) {
    for (UsbDevice device : (List<UsbDevice>) hub.getAttachedUsbDevices()) {
      UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
      if (desc.idVendor() == vendorId && desc.idProduct() == productId) {
        return device;
      }
      if (device.isUsbHub()) {
        device = findDevice((UsbHub) device, vendorId, productId);
        if (device != null) {
          return device;
        }
      }
    }
    return null;
  }


  public static UsbPipeListener inputPipeListener = new UsbPipeListener() {
    @Override
    public void errorEventOccurred(UsbPipeErrorEvent event) {
      log.error("USB Error occured", event.getUsbException());
    }

    @Override
    public void dataEventOccurred(UsbPipeDataEvent event) {
      log.debug("Data read, length:" + event.getActualLength());
      log.debug("Read data: " + Arrays.toString(event.getData()));
      log.debug(new String(event.getData()));
    }
  };
}

当我尝试读取卡片时,每次都会收到不同的数据。这是同一张卡的结果。

[DEBUG] 2021-11-30 19:28:28.880 [usb4java IRP Queue Processor] RfidReaderThread - Data read, length:128
[DEBUG] 2021-11-30 19:28:28.880 [usb4java IRP Queue Processor] RfidReaderThread - Read data: [0, 0, 39, 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, 0, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 0, 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, 0, 0, 31, 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, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[DEBUG] 2021-11-30 19:28:28.880 [usb4java IRP Queue Processor] RfidReaderThread -   '               '               '                                                            '                            
[DEBUG] 2021-11-30 19:28:31.207 [usb4java IRP Queue Processor] RfidReaderThread - Data read, length:128
[DEBUG] 2021-11-30 19:28:31.207 [usb4java IRP Queue Processor] RfidReaderThread - Read data: [0, 0, 35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 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, 0, 39, 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, 0, 31, 0, 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]

你能帮我理解,我做错了什么吗?

UPD我正在使用 EM4100/TK4100 阅读器。

4

0 回答 0