我正在尝试使用 Python 脚本从 USB RFID 读取器读取数据。使用手机上的 NFC 工具将数据写入 RFID 贴纸。因此,当我在 RFID 读取器上扫描 RFID 贴纸时,Python 脚本应该显示我写入 RFID 贴纸的数据。
例如,我在RFID标签上写了一个字符串“123456”。
我希望它显示字符串。
我参考这个指南来读取RFID阅读器:https://github.com/charlysan/pyrfidhid
但这不起作用。我认为它不起作用,因为脚本用于不同的读者模型。我尝试使用这些命令在我的 Linux 上查找 RFID 阅读器信息。
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 树 /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
但是这个命令
sudo tree /dev/input
没有显示任何与我的RFID阅读器相关的设备。
硬件:
根据我的理解,USB应该用作Python中的串行库,但阅读器充当HID设备。所以我尝试使用这个命令安装Python库。
sudo pip install hid
有人知道如何制作Python脚本或任何可能相关的指南吗? 我已经在作者的 GitHub 上打开了这个问题:https://github.com/charlysan/pyrfidhid/issues/8
谢谢。
我没有时间完全解释代码,但此代码连接到实际的 HID 端点,并读取传入的数据。如果您的设备充当键盘(键盘模拟设备),那么这就是您所需要的:
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