使用 RPi2 在 Python 中使用 RC522 和 USB SERIAL 读取标签 UID
Reading tags UID with RC522 and USB SERIAL in Python with RPi2
我有一个带有两个 RFID reader 的 RPi2 (Python 2.7)。一个 reader 是 USB SERIAL hex Gigatek MF7(连接到串行端口),另一个是 RFID-RC522(连接到 GPIO 引脚)。按照 pimylifeup.com/raspberry-pi-rfid-rc522 上的说明正确连接 RC522。 reader 都可以工作和读取标签,但对于同一标签,它们的输出字符串不同。
我知道数据结构(串行 ASCII)和波特率:9600,N,8,1,从 Gigatek 读取 - link。我的脚本从串行端口读取 12 个字符的字符串并从中提取 UID reply_rfid_1[1:9]
:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Convenient python function to read RFID tags with GIGATEK MF7
"""
############################
#from collections import defaultdict
#import csv
import sys
import serial
import time
from datetime import datetime
# define variables
global value_rfid, reply_rfid, refTime
# assign values
refTime = datetime.now()
# open the serial port /dev/ttyUSB1 and check if the port is really open
rfid_port_1 = serial.Serial("/dev/ttyUSB1", 9600) # omit if not in use
print 'rfid_read.py -> rfid reader on port', rfid_port_1.name
rfid_port_1.isOpen()
def Read_Tag():
# define variables
global value_rfid_1, reply_rfid_1, tag
# read port
while int((datetime.now()-refTime).seconds) < 5:
if (rfid_port_1.inWaiting() > 0):
reply_rfid_1 = rfid_port_1.read(12)
value_rfid_1 = str(reply_rfid_1[1:9])
tag = str(value_rfid_1)
print 'rfid_read.py -> tag hex', tag
tag = int(tag, 16)
print 'rfid_read.py -> tag dec ', tag
break
else:
tag = None
return tag
def Output_Tag():
if tag == None:
print 'rfid_read.py -> no tag'
def Flush_Port():
rfid_port_1.flushInput() # Clear input buffer
time.sleep(0.1)
Read_Tag()
Output_Tag()
Flush_Port()
exit()
要从 RC522 读取数据,我使用以下代码:
reader = SimpleMFRC522()
print("Hold a tag near the reader")
try:
id, text = reader.read()
print(id)
print(text)
finally:
GPIO.cleanup()
我可以确定来自 SERIAL reader 的字符串的长度和数字格式,但我不能为 RC522 做到这一点。我在这些页面上找到了一些关于这些库的信息 github.com/mxgxw/MFRC522-python, github.com/pimylifeup/MFRC522-python 但无法破译输出块的数据结构,我真的很感激在这件事上的一些帮助。
2019 年 1 月 7 日更新
如下所示是两个输出和 SPI 状态。
USB reader 的输出为 HEX 和 DEC 数字格式:
rfid_read.py -> rfid reader on port /dev/ttyUSB1
rfid_read.py -> tag hex AC8C5E0A
rfid_read.py -> tag dec 2894880266
RC522reader对于同一个TAG的输出是:
Hold a tag near the reader
44535950452
SPI状态:
pi@raspberrypi:~ $ lsmod | grep spi
spidev 20480 0
spi_bcm2835 20480 0
我按照 raspberrypi-spy.co.uk/2018/02/rc522-rfid-tag-read-raspberry-pi using the library github.com/mxgxw/MFRC522-python 中的说明尝试了不同的方法。将此库用于我拥有的标签的输出 UID 是:
Card read UID: 86,209,188,187
而同一标签的GIGATEK MF7 reader的输出UID是:
hex: BBBCD156
dec: 3149713750
结论 11. 7. 2019
使用 MFRC522-python
库的输出是正确的,但翻转过来并且不是正确的数字格式:
BB BC D1 56 = 187 188 209 86
因此我修改了库文件附带的 Read.py
标签 UID 读取脚本,以像我想要的那样将输出转换为十进制:
86 209 188 187 -> 56 D1 BC BB -> 3149713750
工作脚本:
def Read_Tag(self):
# read with MFRC522 on GPIO
# define variables
global value_rfid_2, reply_rfid_2, tag, refTime
# assign values
refTime = datetime.now()
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
# read port
while int((datetime.now()-refTime).seconds) < 5:
# Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# If a card is found
if status == MIFAREReader.MI_OK:
print "Card detected"
# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
# Print UID
print "tag UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
tag_endian = (uid[3], uid[2], uid[1], uid[0])
print 'tag endian:', tag_endian
tag_hex = hex(uid[3]), hex(uid[2]), hex(uid[1]), hex(uid[0])
print 'tag hex:', tag_hex
tag_str = str(hex(uid[3])[2:]), str(hex(uid[2])[2:]), str(hex(uid[1])[2:]), str(hex(uid[0])[2:])
print 'tag string:', tag_str
tag_str = str(hex(uid[3])[2:])+str(hex(uid[2])[2:])+str(hex(uid[1])[2:])+str(hex(uid[0])[2:])
print 'tag hex string concatenated:', tag_str
tag = int(tag_str, 16)
print 'tag dec:', tag
# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
# Select the scanned tag
MIFAREReader.MFRC522_SelectTag(uid)
# Authenticate
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
# Check if authenticated
if status == MIFAREReader.MI_OK:
MIFAREReader.MFRC522_Read(8)
MIFAREReader.MFRC522_StopCrypto1()
else:
print "Authentication error"
GPIO.cleanup() # Clear input buffer
time.sleep(0.1)
return tag
我有一个带有两个 RFID reader 的 RPi2 (Python 2.7)。一个 reader 是 USB SERIAL hex Gigatek MF7(连接到串行端口),另一个是 RFID-RC522(连接到 GPIO 引脚)。按照 pimylifeup.com/raspberry-pi-rfid-rc522 上的说明正确连接 RC522。 reader 都可以工作和读取标签,但对于同一标签,它们的输出字符串不同。
我知道数据结构(串行 ASCII)和波特率:9600,N,8,1,从 Gigatek 读取 - link。我的脚本从串行端口读取 12 个字符的字符串并从中提取 UID reply_rfid_1[1:9]
:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Convenient python function to read RFID tags with GIGATEK MF7
"""
############################
#from collections import defaultdict
#import csv
import sys
import serial
import time
from datetime import datetime
# define variables
global value_rfid, reply_rfid, refTime
# assign values
refTime = datetime.now()
# open the serial port /dev/ttyUSB1 and check if the port is really open
rfid_port_1 = serial.Serial("/dev/ttyUSB1", 9600) # omit if not in use
print 'rfid_read.py -> rfid reader on port', rfid_port_1.name
rfid_port_1.isOpen()
def Read_Tag():
# define variables
global value_rfid_1, reply_rfid_1, tag
# read port
while int((datetime.now()-refTime).seconds) < 5:
if (rfid_port_1.inWaiting() > 0):
reply_rfid_1 = rfid_port_1.read(12)
value_rfid_1 = str(reply_rfid_1[1:9])
tag = str(value_rfid_1)
print 'rfid_read.py -> tag hex', tag
tag = int(tag, 16)
print 'rfid_read.py -> tag dec ', tag
break
else:
tag = None
return tag
def Output_Tag():
if tag == None:
print 'rfid_read.py -> no tag'
def Flush_Port():
rfid_port_1.flushInput() # Clear input buffer
time.sleep(0.1)
Read_Tag()
Output_Tag()
Flush_Port()
exit()
要从 RC522 读取数据,我使用以下代码:
reader = SimpleMFRC522()
print("Hold a tag near the reader")
try:
id, text = reader.read()
print(id)
print(text)
finally:
GPIO.cleanup()
我可以确定来自 SERIAL reader 的字符串的长度和数字格式,但我不能为 RC522 做到这一点。我在这些页面上找到了一些关于这些库的信息 github.com/mxgxw/MFRC522-python, github.com/pimylifeup/MFRC522-python 但无法破译输出块的数据结构,我真的很感激在这件事上的一些帮助。
2019 年 1 月 7 日更新
如下所示是两个输出和 SPI 状态。
USB reader 的输出为 HEX 和 DEC 数字格式:
rfid_read.py -> rfid reader on port /dev/ttyUSB1
rfid_read.py -> tag hex AC8C5E0A
rfid_read.py -> tag dec 2894880266
RC522reader对于同一个TAG的输出是:
Hold a tag near the reader
44535950452
SPI状态:
pi@raspberrypi:~ $ lsmod | grep spi
spidev 20480 0
spi_bcm2835 20480 0
我按照 raspberrypi-spy.co.uk/2018/02/rc522-rfid-tag-read-raspberry-pi using the library github.com/mxgxw/MFRC522-python 中的说明尝试了不同的方法。将此库用于我拥有的标签的输出 UID 是:
Card read UID: 86,209,188,187
而同一标签的GIGATEK MF7 reader的输出UID是:
hex: BBBCD156
dec: 3149713750
结论 11. 7. 2019
使用 MFRC522-python
库的输出是正确的,但翻转过来并且不是正确的数字格式:
BB BC D1 56 = 187 188 209 86
因此我修改了库文件附带的 Read.py
标签 UID 读取脚本,以像我想要的那样将输出转换为十进制:
86 209 188 187 -> 56 D1 BC BB -> 3149713750
工作脚本:
def Read_Tag(self):
# read with MFRC522 on GPIO
# define variables
global value_rfid_2, reply_rfid_2, tag, refTime
# assign values
refTime = datetime.now()
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
# read port
while int((datetime.now()-refTime).seconds) < 5:
# Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# If a card is found
if status == MIFAREReader.MI_OK:
print "Card detected"
# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
# Print UID
print "tag UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
tag_endian = (uid[3], uid[2], uid[1], uid[0])
print 'tag endian:', tag_endian
tag_hex = hex(uid[3]), hex(uid[2]), hex(uid[1]), hex(uid[0])
print 'tag hex:', tag_hex
tag_str = str(hex(uid[3])[2:]), str(hex(uid[2])[2:]), str(hex(uid[1])[2:]), str(hex(uid[0])[2:])
print 'tag string:', tag_str
tag_str = str(hex(uid[3])[2:])+str(hex(uid[2])[2:])+str(hex(uid[1])[2:])+str(hex(uid[0])[2:])
print 'tag hex string concatenated:', tag_str
tag = int(tag_str, 16)
print 'tag dec:', tag
# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
# Select the scanned tag
MIFAREReader.MFRC522_SelectTag(uid)
# Authenticate
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
# Check if authenticated
if status == MIFAREReader.MI_OK:
MIFAREReader.MFRC522_Read(8)
MIFAREReader.MFRC522_StopCrypto1()
else:
print "Authentication error"
GPIO.cleanup() # Clear input buffer
time.sleep(0.1)
return tag