Read data from USB RFID reader using Python

8k views Asked by At

I'm trying to read data from USB RFID reader using Python script. The data was write into a RFID sticker using NFC tool on my phone. So when I scan the RFID sticker at the RFID reader, Python script should display the data that I write into the RFID sticker.

For example, I write a string into RFID sticker with "123456".

I want it to display the string.

I refer to this guide to read the RFID reader: https://github.com/charlysan/pyrfidhid

But this does not work. I think it doesn't work because the script used for different reader model. I tried to find the RFID reader info on my Linux by using these command.

sudo lsusb

Bus 001 Device 004: ID ffff:0035 Sycreader USB HID READER

sudo lsusb -vd ffff:0035

Bus 001 Device 003: ID ffff:0035 Sycreader USB HID READER
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass            0 
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0xffff 
  idProduct          0x0035 
  bcdDevice            1.10
  iManufacturer           1 Sycreader
  iProduct                2 USB HID READER
  iSerial                 3 20170320
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength       0x001b
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0xa0
      (Bus Powered)
      Remote Wakeup
    MaxPower              200mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           0
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      0 
      bInterfaceProtocol      0 
      iInterface              4 USB VENDER HID
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.10
          bCountryCode           33 US
          bNumDescriptors         1
          bDescriptorType        34 Report
          wDescriptorLength      38
          Report Descriptor: (length is 38)
            Item(Global): Usage Page, data= [ 0xa0 0xff ] 65440
                            (null)
            Item(Local ): Usage, data= [ 0x01 ] 1
                            (null)
            Item(Main  ): Collection, data= [ 0x01 ] 1
                            Application
            Item(Global): Report ID, data= [ 0x01 ] 1
            Item(Local ): Usage, data= [ 0x03 ] 3
                            (null)
            Item(Global): Report Size, data= [ 0x08 ] 8
            Item(Global): Report Count, data= [ 0xff ] 255
            Item(Main  ): Feature, data= [ 0x02 ] 2
                            Data Variable Absolute No_Wrap Linear
                            Preferred_State No_Null_Position Non_Volatile Bitfield
            Item(Global): Report ID, data= [ 0x02 ] 2
            Item(Local ): Usage, data= [ 0x03 ] 3
                            (null)
            Item(Global): Report Size, data= [ 0x08 ] 8
            Item(Global): Report Count, data= [ 0xff ] 255
            Item(Main  ): Feature, data= [ 0x02 ] 2
                            Data Variable Absolute No_Wrap Linear
                            Preferred_State No_Null_Position Non_Volatile Bitfield
            Item(Global): Report ID, data= [ 0x03 ] 3
            Item(Local ): Usage, data= [ 0x03 ] 3
                            (null)
            Item(Global): Report Size, data= [ 0x08 ] 8
            Item(Global): Report Count, data= [ 0xff ] 255
            Item(Main  ): Feature, data= [ 0x02 ] 2
                            Data Variable Absolute No_Wrap Linear
                            Preferred_State No_Null_Position Non_Volatile Bitfield
            Item(Main  ): End Collection, data=none
can't get debug descriptor: Resource temporarily unavailable
Device Status:     0x0000
  (Bus Powered)

sudo tree /dev/input

/dev/input
├── by-id
│   ├── usb-Logitech_USB_Optical_Mouse-event-mouse -> ../event7
│   ├── usb-Logitech_USB_Optical_Mouse-mouse -> ../mouse0
│   ├── usb-SONiX_USB_DEVICE-event-if01 -> ../event6
│   └── usb-SONiX_USB_DEVICE-event-kbd -> ../event2
├── by-path
│   ├── pci-0000:03:00.0-usb-0:7:1.0-event-mouse -> ../event7
│   ├── pci-0000:03:00.0-usb-0:7:1.0-mouse -> ../mouse0
│   ├── pci-0000:27:00.3-usb-0:4:1.0-event-kbd -> ../event2
│   └── pci-0000:27:00.3-usb-0:4:1.1-event -> ../event6
├── event0
├── event1
├── event10
├── event11
├── event12
├── event13
├── event14
├── event15
├── event16
├── event17
├── event18
├── event2
├── event3
├── event4
├── event5
├── event6
├── event7
├── event8
├── event9
├── mice
└── mouse0

2 directories, 29 files

But this command sudo tree /dev/input do not showing any device related to my RFID reader.

Hardware:

  • USB RFID reader EM4100
  • RFID sticker NXP MIFARE Ultralight (Ultralight C)

From my understanding, USB should be used as serial library in Python, but the reader act as a HID device. So I tried installing Python library using this command.

sudo pip install hid

Anyone know how to make the Python script or any guide that might related? I already open this issue on author's GitHub: https://github.com/charlysan/pyrfidhid/issues/8

Thanks.

1

There are 1 answers

0
Pieterjan On

I don't have time to fully explain the code, but this code connects to the actual HID endpoint, and reads the incoming data. If your device acts like a keyboard (Keyboard Emulated Device), this is what you need:

import usb.core 
import usb.util 
import time 
import requests 
import json 
import RPi.GPIO as GPIO 
 
USB_IF      = 0 # Interface 
USB_TIMEOUT = 5 # Timeout in MS 
USB_VENDOR  = 0x1c34 # Vendor-ID:  
USB_PRODUCT = 0x7241 # Product-ID 
 
# Find the HID device by vender/product ID
dev = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT) 
 
# Get and store the endpoint 
endpoint = dev[0][(0,0)][0] 
 
if dev.is_kernel_driver_active(USB_IF) is True: 
    dev.detach_kernel_driver(USB_IF) 
 
# Claim the device 
usb.util.claim_interface(dev, USB_IF) 

# Configure the Raspberry Pi GPIO
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(11, GPIO.OUT) 
 
receivedNumber = 0 
while True: 
    control = None 
     
    try: 
        # Read a character from the device 
        control = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, USB_TIMEOUT)
        
        # Here you have to analyze what's coming in.
        # In my case you had to check the first byte (command)
        if control[0] == 2:
            # Convert ascii to a number, there's probably better ways to do so.
            receivedDigit = control[2] - 29 
        
            if receivedDigit == 10: 
                receivedDigit = 0
                
            # Append the digit to the number
            receivedNumber = 10 * receivedNumber + receivedDigit 
        
        # Check if the received character is CRLF
        if (( control[0] == 0 )) & (( control[2] == 40 )) & (( not receivedNumber == 0 )): 
            resp = requests.post("http://127.0.0.1/verify-access.php",data={'cardNumber':receivedNumber});
            print "card: " + str(receivedNumber)             
            ret_data = json.loads(resp.text) 
            print ret_data["message"] 
            print ret_data["can_enter"] 
            if ret_data["can_enter"] == "1":
                print "student: " + ret_data["student"]
                
                # Start the machine
                GPIO.output(11, GPIO.HIGH) 
                time.sleep(1) 
                GPIO.output(11, GPIO.LOW) 
            print 
            receivedNumber = 0 
    except KeyboardInterrupt: 
        GPIO.cleanup() 
    except: 
        pass 
     
    time.sleep(0.001) # Let CTRL+C actually exit