我有一个RPi2 (Python2.7)和两个射频识别阅读器。一个阅读器是一个USB串行十六进制MF7 (连接到一个串行端口)和另一个是RFID- an 522(连接到GPIO引脚)。RC522按照在pimylifeup.com up.com/raspberry-pi-rfid-pi 522上找到的指令正确连接。两个读取器都工作和读取标记,但是它们的输出字符串对于相同的标记是不同的。
我知道数据结构(串行ASCII)和波特率: 9600,N,8,1,从Gigatek - 链接读取.我的脚本从串口读取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()我可以确定来自串行读取器的字符串的长度和数字格式,但是我不能在RC522上这样做。我在这些页面( Github.com/mxgxw/MFRC522 522-python,Github.com/pimylifeup/MFRC522 522-python )上找到了一些关于库的信息,但是无法解释输出块的数据结构,我非常希望能在这方面提供一些帮助。
更新1.7.2019
如下所示,是这两项产出和SPI状态。
USB读取器的输出采用HEX和DEC数字格式:
rfid_read.py -> rfid reader on port /dev/ttyUSB1
rfid_read.py -> tag hex AC8C5E0A
rfid_read.py -> tag dec 2894880266相同标记的RC522读取器的输出如下:
Hold a tag near the reader
44535950452SPI状态:
pi@raspberrypi:~ $ lsmod | grep spi
spidev 20480 0
spi_bcm2835 20480 0发布于 2019-07-10 13:53:11
我尝试了一种不同的方法,按照raspberrypi-spy.co.uk/2018/02/rc522-rfid-tag-read-raspberry-pi上的说明使用库Github.com/mxgxw/MFRC522 522-python。对我所拥有的标记使用这个库的输出UID是:
Card read UID: 86,209,188,187而相同标记的GIGATEK MF7读取器的输出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 taghttps://stackoverflow.com/questions/56808495
复制相似问题