How to read RFID tags from SparkFun RFID USB Reader with Python 3

When you have an RFID starter kit from Sparkfun, you will be able to read RFID tags through serial.

Previously, we saw how to use an ESP32 board to read RFID tags from a SparkFun RFID USB Reader.

Given that, I was able to build a ESP32 prototype to scan tag ids from RFID cards.

In order for that ESP32 prototype to recognise what each of my tag card represents, I need to label the ids. Whenever my ESP32 prototype gets a tag id, it will query a tag catalogue to see what that tag id represent.

Since it is easier to label the tag cards from a computer, I built a Python 3 application to read the RFID tags from the SparkFun RFID reader.

So how we can read RFID tags from SparkFun RFID USB Reader with Python 3?

If you are looking for a way to read RFID tags from SparkFun RFID USB Reader with Python 3, then this post is for you.

Connecting the SparkFun RFID USB reader to a computer

Before the application can read from the SparkFun RFID USB reader, we need to connect it to a computer with a mini USB cable.

SparkFun RFID USB reader connected to mini USB cable

Finding the path to the serial device on macOS

After I had connected the RFID reader to my MacBook, I started a terminal program and typed the following command:

ls /dev/tty.*

When the command returned an output, I found /dev/tty.usbserial-AB0JPNCG listed as one of the items.

If you are using Linux, then you should be able to use the same command to find the path to the serial device.

In case you are using Windows, use the device manager to find the COM port to communicate with your RFID reader.

Installing the Python 3 library to help read data from serial

After getting the serial port to read from, I proceeded to install the PySerial library into my Python 3 environment:

pip install pySerial

Writing a sample Python 3 program to read RFID tags from SparkFun RFID USB Reader

Given that, I created the following Python 3 script and saved it as

import argparse
import serial

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('--port', help='Serial port to read tags from', required=True)
    args = parser.parse_args()

    serial_port = args.port
    with serial.Serial(serial_port, 9600) as rfid_reader:
            tag = rfid_reader.readline().decode('UTF-8').strip()

As shown above, I use the argparse library to help me build a command line application.

After importing the Python libraries, an Argument Parser object is created to read command line arguments. In this situation, I had created a way for the serial port path to be included as a command line argument.

After getting the serial port into the serial_port variable, a Serial object is created.

Once we have created the Serial object as rfid_reader, we get into an infinite loop to read from the serial port.

Whenever the SparkFun RFID USB reader detects a tag id, rfid_reader.readline() will unblock and return the tag id. Given that tag id, we decode it as an UTF-8 string, strip off trialing whitespaces and save the result into the tag variable.

After we have a value for the tag variable, we print the tag id to standard output.

Running the application to read RFID tags from SparkFun RFID USB Reader

So how can I run this application?

In order to start the application, I will run the following command in a terminal program:

python --port /dev/tty.usbserial-AB0JPNCG

As mentioned earlier, the application will get into an infinite loop. After that, any tag id read by the RFID reader will be printed to standard output.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.