使用 Raspberry & python 读取 USB RFID

问题描述 投票:0回答:2

我正在尝试使用 python 读取 USB RFID 设备。 divce 在 HID 模式下工作,我可以找到它 /dev/hidraw0 我有一个标签,代码为“210054232F”(我可以看到外壳上印有代码,用 RFID 扫描它)

所以我尝试使用 python 脚本打开设备并捕获读取的代码,但我陷入困境......

这是Python代码:

import sys

fp = open('/dev/hidraw0', 'rb')

while True:
   buffer = fp.read(16)
   for c in buffer:
       if ord(c) > 0:
           print c
   print "\n"

这是输出(中间有很多方形和不可排序的字符): 如果我打印代码:

for c in buffer:
       if ord(c) > 0:
           print ord(c)

这是输出:

1 31 1

1 30 1

1 39 1

1 39 1

1 34 1

1 33 1

1 31 1

1 32 1

1 31 1

1 2 9 1

1 40 1

我找不到任何类型的模式来解码数据。

您有什么建议或其他方法来解决这个问题吗?

谢谢, 费德里科

python linux raspberry-pi rfid hid
2个回答
1
投票

尝试用不同类型打印数据,例如

int
char
甚至
list
。您的问题是所提供的数据不是您打印时的类型。这使得奇怪的符号。


0
投票

读卡器的行为类似于 USB 键盘,因此您需要从 USB 键盘扫描代码中组装值。

import sys
import struct

scan_codes=(1,2,3,4,5,6,7,8,9,0)
    
def read_tag(f):
    vle = 0
    while True:
        b = f.read(1)
        c = b[0]
        if c >= 30 and c <= 39:
            vle = vle*10 + scan_codes[c-30]
        elif c == 40:
            vbe = struct.unpack("<I", struct.pack(">I", vle))[0]
            return vbe

fp = open('/dev/hidraw0', 'rb')
            
while True:
    tag = read_tag(fp)
    print(f"{tag}, {hex(tag)[2:]}")        
© www.soinside.com 2019 - 2024. All rights reserved.