April 24, 2019
Hot Topics:

RFID Programming Made Simple and Cheap

  • August 29, 2006
  • By Bradley L. Jones
  • Send Email »
  • More Articles »

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();}

In this code, the first thing that happens is that an RFID object is created called rfid1. This will be used to attach to the RFID hardware. Before opening the hardware for use, event handlers are created for attaching to the RFID reader (rfid_Attach), detaching from the reader (rfid_Detach), having a tag put near the reader (rfid_Tag), and having a tag move out of the reader's range (rfid_TagLost). After adding these handlers to the rfid1 object, the open method is called to begin using the RFID object.

To use the RFID reader, in addition to being connected to your machine, it also has to be turned on. The cboxAntenna checkbox in the form shown in Figure 3 is to let you control turning the reader on and off. By checking the box, you will execute the event to turn on the reader. The code to accomplish follows:

private void antennaCheckBox_CheckedChanged(object sender,                                            EventArgs e){   rfid1.Antenna = cboxAntenna.Checked;}

As you can see, turning on the reader is simply a matter of setting the Antenna property of your RFID object to true. The value returned from cboxAntenna.Checked will either be true or false, so this will set the rfid1 Antenna property to true or false (on or off). Once it is on, you are ready to begin reading tags.

With the reader attached and turned on, the next important thing is to read a tag. This is done in the rfid_Tag event handler that was added to the rfid1 object in the Form_Load event. The code for this event hander follows:

void rfid_Tag(object sender, TagEventArgs e){   txtTag.Text = e.Tag;   lastRFIDTag = txtTag.Text;   rfid1.LED   = true;    // light on}

The tag's unique ID will be passed into the event handler in the TagEventArgs. As you can see, the tag ID comes out of e.Tag and is simply displayed in the txtTag text box by assigning it to the text property. Because this application is only displaying the tag ID, not much is happening here.

The second line of code within this event sets the lastRFIDTag field to the ID of the tag just read. This field will be used to update the listbox on the form with the Tag ID that was just read. This update, however, won't happen until the tag is moved off the reader. If you did the update here within the rfid_Tag event, the update could happen over and over as long as the tag remained near the reader. That is not the desired affect wanted—I only want to list the read tag once for each time it moves over and away from the reader.

The last line of code sets the LED property of the RFID reader to true. The Phidgets RFID reader has an LED light on it. By setting this property to true, the light will be turned on whenever the reader is reading a tag.

A tag may be read over and over as long as it is over the Reader. Once it is moved out of range of the reader, the rfid_TagLost event will happen. The code for the rfid_TagLost event handler follows:

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));}

In this routine, things are simply being cleaned up. The first line clears the textbox because no tag is currently being read. The second line turns off the LED light. The final line of code writes a line in the listbox showing the RFID tag information that was just read.

In your applications, the rfid_TagLost event would be the location where you could do your tag processing. The rfid_TagLost event will only happen once when the tag leaves the reader area. That makes this a better location for processing application logic than the rfid_Tag event. For example, for a system where members have an RFID membership card, this would be the area in the code where you would do a look up of your database system to find the unique Tag ID and then the associated member information.

You've now seen all the critical code—what little of it that was required. The sample also has two other event handlers and a few fields that have not been covered. These events and fields simply display information about the RFID reader such as whether it is attached as well as the serial number and version number of the reader: These events happen when the RFID reader attaches to or detaches from your system.

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;}


If this is your first time to see the code for reading RFID tags, you may be scratching your head wondering when it will get difficutlt. The answer is, it won't—at least not with the PhidgetsRFID reader and their newest library. It really is just a mater of hooking up a reader, flipping a few property switches, and reading tags. Knowing it is this simple allows you to focus more on how to use RFID rather than on how to code for RFID usage.

The Source Files

Download the Source: RFIDReader.zip 54 Kb

About the Author

Bradley Jones is a Microsoft MVP who works for Jupitermedia as an Executive Editor over many of the software development sites and channels. His experience includes development in C, C++, VB, some Java, C#, ASP, COBOL, and more as well as having been a developer, consultant, analyst, lead, and much more. His recent books include Teach Yourself the C# Language in 21 Days.

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;      }   }}

Page 3 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date