RFID Programming Made Simple and Cheap
Radio Frequency Identification Device (RFID) has been getting a ton of coverage in the press ever since Wal-mart stated that they were going to require their suppliers to tag products. Over the past few years, the ability to use RFID has gotten not only cheaper, but also easier to use. By the end of this article, you'll see just how easy it has gotten to implement a passive RFID system that hooks into your applications. I use C# and .NET in my example, but you could also use Java or several other languages to do the same thing.
The RFID Hardware
Click here for a larger image.
Figure 1: A variety of passive RFID tags
The cost of RFID tags can vary depending on where you buy them and how many you purchase. Most of the tags in Figure 1 cost me between $1.20 and $3, but I purchased them individually. If you buy them in large quantities, the cost will obviously drop quickly. You can find these tags and other ones from a variety of sources. I obtained these from http://www.trossenrobotics.com/store/c/2964-EM4102.aspx.
In addition to passive versus active, you also have to know the radio wave range of the reader and tags you are using. The tags in Figure 1 are all passive tags that operate at the 125 KHz frequency. As such, they use a reader that can recognize this frequency. I used an RFID reader from Trossen Robotics . Figure 2 shows the small PhidgetsRFID passive tag reader.
Click here for a larger image.
Figure 2: The Phidgets RFID reader
This particular reader plugs into a USB port and will read passive tags that operate at 125khz. You can find this reader retails for about $65 US from either Phidgets or Trossen Robotics.
Programming RFID
Each RFID tag contains a unique ID and possibly other information. As mentioned with the readers, there are passive tags and active tags. A passive tag generally contains an antenna—actually a coil of wire—that when put near a reader creates a small charge that is enough to cause the tag to transmit its unique ID. This small charge, however, is minuscule, so a passive tag generally has to be within a couple of inches of a reader to work.
The unique tag ID is the key to using RFID in a program. The unique ID what sets one tag apart from all other tags. If you are tracking inventory, you can place a tag on an item and then associate the ID of the tag to that item. You've then given each item a unique key to access it by.
As an example, a credit card tag can be used as a membership card. Within your membership database, you simply need to add an additional field for the Tag ID. Before giving the card to the member, you can scan the card and associate the Tag ID to that member in your membership database. From that point forward, that unique RFID card will be associated to that member in your database. If you scan that card, you can use the retrieved unique tag ID to then search your membership database for the RFID tag number and then get the associated membership information.
This example should not sound too far off. If you have a "swipe card" that you use to access an office building, it is most likely that you are already using RFID in a manner similar to what I just described for a membership database.
Downloads
RFID in Action
To repeat the key point, using RFID is simply a matter of reading the associated tag to get the unique ID. Once you have the unique ID, it is simple data. You need a reader to do the reading and tags to read. In the rest of this article, I'll walk through what it takes to use the Phidgets RFID reader mentioned above to read standard 125 KHz tags.
The RFID Hardware
Reading a tag, however, does require a few more steps. For you to be able to tap into the hardware, you will need to install the Phidgets application programming interface (API). You can download this code from here. You also can download example programs and documentation from the site as well. While I'm using .NET on a Microsoft Windows XP machine, you will also find that there are APIs for Linux, the Mac, and other platforms as well.
Install the API by running the Phidgets21.msi file. This will install a dynamic link library that you will then be able to reference from your application. I used the 2.1 beta version of the library. If you use a different version, my code may not work because the libraries are not backward compatible. This version, however, seemed much easier to use than prior versions.
The RFID Program
Once you have the library installed, you are ready to begin building an RFID application. I'll be using Visual Studio 2005 and C# to build the application shown in Figure 3.
Figure 3: A simple RFID application
This application simply reads tags and displays their unique IDs in the form. If you read a tag, it will be shown in the Tag text field while it is being read, and then added to the listbox once the read is complete. In this way, the listbox will maintain a history of what has been read. Figure 4 shows the dialog after the reader has been turned on (by checking the box in the form) and after a few tags have been read.
Figure 4: Having read a few tags
To build this application, you start by creating a new C# Windows Application in Visual Studio 2005. You then should add a reference to the Phidgets API you installed earlier.
Add a reference by going to the Project menu and selecting Add Reference. Choose the COM tag and search for Phidgets Library 2.1. If it isn't there, use the Browse option and locate the DLL on you system. It is most likely to be at C:Program Files\Phidgets\PhidgetsCOM.dll. Once you've added this reference, you are ready to begin building the form.
Most of the other property values for the controls are left at the defaults. You can modify these in any way you want. I've included the full source code for this application in the download at the end of this article, so if you have trouble creating the form, just pull it from there.
Within the form class, which I called RFIDReader, I also set up three additional fields:
RFID rfid1;
string lastRFIDTag;
Int32 TagCtr;
RFID creates an RFID object called rfid1. This object will be assigned to the RFID reader so that we can pull information from it. The lastRFIDTag string field simply stores the unique ID from the last tag read. Finally, TagCtr is used to store a simple count of the number of tags read. As you could see in Figure 4, this counter is used to display the count on the listbox. After setting up these variables, the lastRFIDTag is set to blanks and the TagCtr should be set to 0 when you first initialize your form variables.
With a form set up and variables declared, you should do one more thing before getting to the fun code. Add a couple of references to the Phidgets namespaces to the top of your listing:
using Phidgets;
using Phidgets.Events;
Now the fun code begins. To use the RFID reader, you will need to create an RFID object and then open the reader. You also will want to create a few event handlers to control actions that occur from the reader. This can all be done in the Form_Load event with the following code:
private void Form1_Load(object sender, EventArgs e)
{
rfid1 = new RFID();
rfid1.Attach += new AttachEventHandler(rfid_Attach);
rfid1.Detach += new DetachEventHandler(rfid_Detach);
rfid1.RFIDTag += new TagEventHandler(rfid_Tag);
rfid1.RFIDTagLost += new TagEventHandler(rfid_TagLost);
rfid1.open();
}
private void antennaCheckBox_CheckedChanged(object sender,
EventArgs e)
{
rfid1.Antenna = cboxAntenna.Checked;
}
void rfid_Tag(object sender, TagEventArgs e)
{
txtTag.Text = e.Tag;
lastRFIDTag = txtTag.Text;
rfid1.LED = true; // light on
}
void rfid_TagLost(object sender, TagEventArgs e)
{
txtTag.Text = "";
rfid1.LED = false; // light off
lbPrevRFIDTags.Items.Insert(0,
string.Format("Tag: {0} - {1}", ++TagCtr, lastRFIDTag));
}
void rfid_Detach(object sender, DetachEventArgs e)
{
lblAttached.Text = "Not Attached";
}
void rfid_Attach(object sender, AttachEventArgs e)
{
Phidgets.RFID phid = (Phidgets.RFID)sender;
lblAttached.Text = " Attached: " + phid.Name;
lblSerial.Text = " Serial: " + phid.SerialNumber;
lblVersion.Text = " Version: " + phid.Version;
}
Conclusion
Listing 1: The PhidgetsForm.cs file:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Phidgets;
using Phidgets.Events;
namespace RFIDTest
{
public partial class RFIDReader : Form
{
RFID rfid1;
string lastRFIDTag;
Int32 TagCtr;
public RFIDReader()
{
InitializeComponent();
lastRFIDTag = "";
TagCtr = 0;
}
private void Form1_Load(object sender, EventArgs e)
{
rfid1 = new RFID();
rfid1.Attach += new AttachEventHandler(rfid_Attach);
rfid1.Detach += new DetachEventHandler(rfid_Detach);
rfid1.RFIDTag += new TagEventHandler(rfid_Tag);
rfid1.RFIDTagLost += new TagEventHandler(rfid_TagLost);
rfid1.open();
}
void rfid_Tag(object sender, TagEventArgs e)
{
txtTag.Text = e.Tag;
lastRFIDTag = txtTag.Text;
rfid1.LED = true; // light on
}
void rfid_TagLost(object sender, TagEventArgs e)
{
txtTag.Text = "";
rfid1.LED = false; // light off
//write held Tag ID to listview
lbPrevRFIDTags.Items.Insert(0,
string.Format("Tag: {0} - {1}", ++TagCtr, lastRFIDTag));
}
void rfid_Detach(object sender, DetachEventArgs e)
{
lblAttached.Text = "Not Attached";
}
void rfid_Attach(object sender, AttachEventArgs e)
{
Phidgets.RFID phid = (Phidgets.RFID)sender;
lblAttached.Text = "Attached: " + phid.Name;
lblSerial.Text = " Serial: " + phid.SerialNumber;
lblVersion.Text = " Version: " + phid.Version;
}
private void antennaCheckBox_CheckedChanged(
object sender, EventArgs e)
{
rfid1.Antenna = cboxAntenna.Checked;
}
}
}
Downloads
Computer Repair Brighton
ReplyDeletePC computer repairs, software and hardware fault-finding, tuition and data recovery services for the Brighton and Sussex area.