1
votes

I have a RPi2 (Python 2.7) with two RFID readers. One reader is an USB SERIAL hex Gigatek MF7 (connected to a serial port) and the other is the RFID-RC522 (connected to GPIO pins). The RC522 is wired correctly following the instructions found on pimylifeup.com/raspberry-pi-rfid-rc522. Both readers work and read tags but their output strings are different for the same tag.

I know the data structure (Serial ASCII) and baud Rate: 9600,N,8,1, to read from the Gigatek - link. My script reads the 12 char string from a serial port and extracts the UID reply_rfid_1[1:9] from it:

#!/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()

To read from the RC522 i use the code below:

reader = SimpleMFRC522()

print("Hold a tag near the reader")

try:
    id, text = reader.read()
    print(id)
    print(text)

finally:
    GPIO.cleanup()

I can determine the length and numeric format of the string coming out of the SERIAL reader but I can not do it for RC522. I found some info about the libraries on these pages github.com/mxgxw/MFRC522-python, github.com/pimylifeup/MFRC522-python but could not decipher the data structure of the output block and I would really appreciate some help on this matter.

UPDATE 1. 7. 2019

As suggested below are the two outputs and the SPI status.

The output of the USB reader is in HEX and DEC numerical format:

rfid_read.py -> rfid reader on port /dev/ttyUSB1
rfid_read.py -> tag hex AC8C5E0A
rfid_read.py -> tag dec  2894880266

The output of the RC522 reader for the same TAG is:

Hold a tag near the reader
44535950452

The SPI status:

pi@raspberrypi:~ $ lsmod | grep spi
spidev                 20480  0
spi_bcm2835            20480  0
1
Can you add the output you got from the RC522? Have you enabled the SPI bus on your Pi? And verified your wiring is correct? If you haven't yet take a look at any good tutorial and double-checkMarcos G.
@MarcosG., I added the outputs. The RC522 is wired following the instructions on pimylifeup.com/raspberry-pi-rfid-rc522.parovelb
Thank you parovelb. Are you using Python 2.x for the Gigatek and Python 3.x for the RC522? Are you sure the Gigatek is giving you the right tag number. According to the manual you should be reading value_rfid = str(reply_rfid[1:8]), right now you're adding a \r to the tag number, I don't know if that's having an effect because you don't show the HEX to INT conversion on your code...Marcos G.
@MarcosG., I posted the whole script of the GIGATEK with the visible conversion HEX and DEC. Both scripts are written in Python 2.7. I also have a separate external M301 desktop RFID reader that outputs the same tag number as the GIGATEK script does. So I am not sure about the [1:8].parovelb

1 Answers

1
votes

I have tried a different approach following the instructions at raspberrypi-spy.co.uk/2018/02/rc522-rfid-tag-read-raspberry-pi using the library github.com/mxgxw/MFRC522-python. The output UID using this library for the tag I have is:

Card read UID: 86,209,188,187

While the output UID of the GIGATEK MF7 reader of the same tag is:

hex: BBBCD156
dec: 3149713750

CONCLUSION 11. 7. 2019 The output using the MFRC522-python library is correct but flipped around and not in a proper numeric format:

BB BC D1 56 = 187 188 209 86

Therefore I modified the Read.py tag UID reading script which comes with the library's files, to convert the output in decimal like I wanted to:

86 209 188 187 -> 56 D1 BC BB -> 3149713750

The working script:

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