【发布时间】:2022-11-10 05:57:49
【问题描述】:
- 我正在尝试使用
RFID-RC522、Arduino 和 RFID 卡实现卡系统。 - 我可以打印来自 RFID 标签的信息,但我怎样才能只打印卡的
UID而不是 python 中的全部信息? - 代码
import serial
import time
device = '/dev/ttyACM0' #this will have to be changed to the serial port you are using
try:
print("Trying...",device)
arduino = serial.Serial(device, 9600)
except:
print("Failed to connect on",device)
while True:
data=arduino.readline()
pieces=data.splitlines()[0]
print("Card UID=", pieces)
- 上面的代码给我的输出为
Trying... /dev/ttyACM0
Card UID= b'Firmware Version: 0x92 = v2.0'
Card UID= b'Scan PICC to see UID, SAK, type, and data blocks...'
Card UID= b'Card UID: 09 DF 98 B3'
Card UID= b'Card SAK: 08'
Card UID= b'PICC type: MIFARE 1KB'
Card UID= b'Sector Block 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 AccessBits'
Card UID= b' 15 63 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] '
Card UID= b' 62 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] '
Card UID= b' 61 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] '
Card UID= b' 60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] '
【问题讨论】:
-
if pieces.startswith("Card UID"): print(pieces.split(":")[1]) -
@SembeiNorimaki 这给出了错误
File "ardu.py", line 14, in <module> if pieces.startswith("Card UID"): TypeError: startswith first arg must be bytes or a tuple of bytes, not str -
你需要
b:pieces.startswith(b"Card UID") -
@Timus 仍然给出错误
File "ardu.py", line 15, in <module> print(pieces.split(":")[1]) TypeError: a bytes-like object is required, not 'str' -
恕我直言同样的错误:
b":"而不是":"