In Introduction to Programming course, students are required to complete a project that solve a problem they investigated. One of the tool/gadget we provided is an RFID reader.
A year ago when we ran the course a group of student found out keeping the track running time is a huge challenge for the PE department. An RFID reader was purchased for students to solve this problem.
One critical challenge was the communication between the RFID reader and python. In order to solve it the serial connection has to be established and the data has to be interpreted.
As this is still an introductory level course, students are asked to focus on the data processing part and assume that there will be a function acquiring the RFID and they need to handle the logic. In other words, the hardware and serial communication part has to be written for them.
After weeks we finally have a solution!
Library used: pySerial, codec
Python version: 3.5 or up
Here’s the code, more information to come
import serial, sys, time import codecs ard = None def getCardId(rawCode): headerStr = "1b390100110010603000" returnId = -1 try: decodestr = codecs.encode(rawCode, 'hex') returnId = str(decodestr).split(headerStr) return returnId except: return -1 def initReader(): global ard port = '/dev/tty.SLAB_USBtoUART' ard = serial.Serial(port, baudrate=115200,timeout=5) def readRfid(): global ard rawCode = ard.read(24) return getCardId(rawCode) initReader() #an infinite loop that reads the rfids while True: print(readRfid())