使用RPi2在Python中使用RC522和USB SERIAL读取标签UID

时间:2019-06-28 14:09:54

标签: python raspberry-pi2 gpio rfid uid

我有一个带有两个RFID读取器的RPi2(Python 2.7)。一个读取器是USB SERIAL十六进制Gigatek MF7(连接到串行端口),另一个是RFID-RC522(连接到GPIO引脚)。遵循pimylifeup.com/raspberry-pi-rfid-rc522上的说明正确连接RC522。读取器都可以读取标签,但同一标签的输出字符串不同。

我知道要从Gigatek-link中读取的数据结构(串行ASCII)和波特率:9600,N,8,1。我的脚本从串行端口读取12个字符的字符串,并从其中提取UID reply_rfid_1[1:9]

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

"""
Convenient python function to read RFID tags with GIGATEK MF7

Created by B.Parovel on 3. 7. 2019
Last modified on 3. 7. 2019
"""

############################

#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阅读器出来的字符串的长度和数字格式,但是对于RC522,我不能这样做。我在这些页面github.com/mxgxw/MFRC522-pythongithub.com/pimylifeup/MFRC522-python上找到了有关库的一些信息,但是无法解密输出块的数据结构,对此我将非常感谢。

更新1. 7. 2019

下面建议的是两个输出和SPI状态。

USB阅读器的输出为十六进制和DEC数字格式:

pi@raspberrypi:~/Desktop/Biovalley $ python rfid_read.py
rfid_read.py -> rfid reader on port /dev/ttyUSB1
rfid_read.py -> tag hex AC8C5E0A
rfid_read.py -> tag dec  2894880266

RC522阅读器针对相同TAG的输出为:

pi@raspberrypi:~/Desktop/Biovalley $ python rfidreader.py 
Hold a tag near the reader
44535950452

SPI状态:

pi@raspberrypi:~/Desktop/Biovalley $ lsmod | grep spi
spidev                 20480  0
spi_bcm2835            20480  0

1 个答案:

答案 0 :(得分:0)

我按照raspberrypi-spy.co.uk/2018/02/rc522-rfid-tag-read-raspberry-pi中的说明使用库github.com/mxgxw/MFRC522-python尝试了另一种方法。使用此库存储我所拥有的标签的输出UID为:

Card read UID: 86,209,188,187

具有相同标签的GIGATEK MF7阅读器的输出UID为:

hex: BBBCD156
dec: 3149713750

结论11. 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_Bag_Tag(self):
    # read with MFRC522 on GPIO
    # define variables
    global value_rfid_2, reply_rfid_2, bag_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 "bag tag UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
            bag_tag_endian = (uid[3], uid[2], uid[1], uid[0])
            print 'bag tag endian:', bag_tag_endian
            bag_tag_hex = hex(uid[3]), hex(uid[2]), hex(uid[1]), hex(uid[0])
            print 'bag tag hex:', bag_tag_hex
            bag_tag_str = str(hex(uid[3])[2:]), str(hex(uid[2])[2:]), str(hex(uid[1])[2:]), str(hex(uid[0])[2:])
            print "bag tag hex string:", bag_tag_str
            bag_tag_str = str(hex(uid[3])[2:])+str(hex(uid[2])[2:])+str(hex(uid[1])[2:])+str(hex(uid[0])[2:])
            print "bag tag hex string concatenated:", bag_tag_str
            bag_tag = int(bag_tag_str, 16)
            print 'bag tag dec:', bag_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 bag_tag