【问题标题】:How to just print the UID of RFID in Python Arduino如何在 Python Arduino 中打印 RFID 的 UID
【发布时间】: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
  • 你需要bpieces.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":" 而不是 ":"

标签: python arduino


【解决方案1】:

您可以将re 与多行re.M 和分组一起使用。 data.decode() 将二进制数据解码为字符串 - 由二进制数据前面的字母 b 表示。

>>> import re
>>> data = b"""Firmware Version: 0x92 = v2.0
... Scan PICC to see UID, SAK, type, and data blocks...
... Card UID: 09 DF 98 B3
... Card SAK: 08
... PICC type: MIFARE 1KB
... Sector Block   0  1  2  3   4  5  6  7   8  9 10 11  12 13 14 15  AccessBits
...   15     63   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ]
...          62   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ]
...          61   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ]
...          60   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ]"""
>>> for line in data.splitlines():
...     try:
...         card_uid = re.search('(?<=Card UID: ).*', line.decode(), re.M).group(0)
...         print("Card UID={}".format(card_uid))
...     except AttributeError:
...         pass
...
Card UID=09 DF 98 B3

【讨论】:

  • 就像给定的代码一样运行它运行良好。但是当我将它与我的代码结合起来时,我得到一个错误File "ardu.py", line 14, in &lt;module&gt; card_uid = re.search('(?&lt;=Card UID: ).*', pieces.decode(), re.M).group(0) AttributeError: 'NoneType' object has no attribute 'group'
  • 那是因为正则表达式不匹配,因此 group 方法调用会引发异常。现在我看到你有一段时间是真的,并且数据不断出现。我会根据它改变我的答案。
  • 固定的。您需要用我的回答中的整个 try/except 块替换您的 print 行。在这个try/except 块内,您需要将line.decode() 替换为pieces.decode()
  • 有用吗,@LavSharma?
【解决方案2】:

文件“ardu.py”,第 14 行,在 card_uid = re.search('(?<=Card UID: ).*', pieces.decode(), re.M).group(0) AttributeError: 'NoneType'对象没有属性“组”—— 拉夫夏尔马 3 月 24 日 15:29

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多