This article explains how I am reading RFID tags with Java on a Raspberry Pi for my Shed-Door-Pi project. It could also be used as an insight into opening and reading from a serial port using Pi4j.
For this first part, I will be demonstrating how to open the COM port and receive RFID key data from the reader. The second article will go into detail regarding parsing the data received and using it to determine whether a known RFID key has been scanned.
I am making the assumption that the reader is able to compile and run Java applications on their Raspberry Pi, with the Pi4j libraries. For more relevant information about this, please see the Pi4j Serial Communication example, http://pi4j.com/example/serial.html
Working source code from this article is available here.
Revision 1
My Setup
Starting Out
The Parallax USB RFID reader should work out-of-the box without the need to install any drivers. It operates by creating a virtual COM port over USB. Once connected, you should be able to view the port name that it is using by executing the following command on your Pi.
ls /dev | grep USB
The output on my Pi was “ttyUSB0″, the full COM port name is therefore /dev/ttyUSB0. Remember yours for later on!
Importing Libraries
This article only requires the “pi4j-core.jar” Pi4j library to run. We will be using the following imports.
import com.pi4j.io.serial.Serial;
import com.pi4j.io.serial.SerialDataEvent;
import com.pi4j.io.serial.SerialDataListener;
import com.pi4j.io.serial.SerialFactory;
Accessing Serial Interface
To open a COM port, we need to create a Serial object that allow us to communicate with the Pi’s serial (COM) ports. I have done this by creating an instance variable as follows.
private final Serial serial = SerialFactory.createInstance();
Instead of creating a Serial object directly, we ask the SerialFactory factory class to create one for us. For more information about the “factory pattern”, see this article on Wikipedia.
Lets Register a Basic Data Listener First
Before rushing off and opening up that COM port, lets first provide a SerialDataListener so we receive call-backs when data is received. You could do this by either creating an anonymous class as described below, or by implementing the SerialDataListener interface in the class that you are creating and passing “this” to the serial.addListener(…) method.
I suggest putting this method in the constructor of your class, or wherever you are initialising serial communication.
serial.addListener(new SerialDataListener()
{
@Override
public void dataReceived(SerialDataEvent event)
{
System.out.print("We received some data!");
}
});
This snippet creates an anonymous SerialDataListener class that implements the dataReceived(…) method. This method will be called whenever data is received from the COM port, in our case, it will be byte data identifying an RFID key when we place one near the reader. For now, the method will simply output “We received some data!” when this happens.
Opening The COM Port
Time to open the COM port and test that we are able to receive data. Replace “/dev/ttyUSB0″ below with the COM port you discovered earlier on. We are also passing a baud rate of 2400 bps as described in the RFID reader manual.
We can determine whether the serial.open(…) operation successfully opened the COM port by checking if the returned Integer is not -1. A failure to open the COM port will most likely be caused by using the incorrect port name, or if the port is already open. If the port is open to the RFID reader, the status LED on the reader will be red.
int result = serial.open( "/dev/ttyUSB0", 2400 );
if ( result == -1 )
{
System.out.println("Failed to open COM port!");
return;
}
else
{
System.out.println("COM port opened!");
}
Run the application so far. If the COM port was successfully opened you will receive “COM port opened!” in the console window. Now, try placing a RFID tag near the reader. A small yellow LED should flash on the reader and you should receive multiple “We received some data!” messages. Interesting…
My Application Terminates Instantly!?
You shouldn’t need to worry about doing this, while testing the application myself, it did not terminate straight away. It appears that the Pi4j library prevents this. I have included the following just in case this crops up on you!
If your application terminates straight away, you will need to add some kind of run-loop to keep it active. For example, you could use an infinite for-loop after opening the COM port.
for( ;; )
{}
Providing Meaningful Output
Being informed that we received some data is all well and good, but we want to see exactly what we received! In the RFID reader manual, the key transmitted is described as a sequence of bytes, such as “ 0x0A, 0×30, 0×46, 0×30, 0×31, 0×38, 0×34, 0×46, 0×30, 0×37, 0×41, 0x0D”, where 0x0a is the start byte, and 0x0d is the stop byte, leaving the remaining 10 bytes as the tag’s unique key. So lets output the data received in this format. Modify your SerialDataListener’s dataReceived(…) method as follows.
serial.addListener(new SerialDataListener()
{
@Override
public void dataReceived(SerialDataEvent event)
{
byte[] data = event.getData().getBytes(;
for ( int i=0; i < data.length; i++ )
{
System.out.printf( "0x%02x ", data[i] );
}
System.out.println(); // - Line break!
}
});
We are given a SerialDataEvent object with which we can collect the data, represented as a String, using the getData() method. We then convert this to a byte array using the getBytes() method on this String. This byte array, data, is then iterated and printed to the console in the same format as described in the manual.
Run the application again, you should now receive byte data in your console window when you scan a RFID tag. If you look carefully, you will be able to identify the tag’s unique key between the 0x0A and 0x0D bytes!
Closing Up
Don’t forget to close the COM port once you have finished using it! Otherwise, you may not be able to connect to it next time you run the application. Although, I’m fairly sure you will be able to if you run with superuser privileges, using the sudo command.
serial.close();
That’s all for this article! Stay tuned for part two where I will demonstrate how to extract an RFID tag’s unique key as well as comparing it to that of an expected key and determining whether it is valid.
Recent Comments