RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Fun with RFID : Page 5

You cannot avoid RFID as many large companies will require tags on their products in order to do business with them. Everyday items you purchase may have been tracked with RFID.

Running the PhidgetRFID Web Service
Phidget has made it easy for .NET programmers to use the PhidgetRFID reader. Developers can control the reader by the PhidgetWebService, a component that interacts with your PhidgetRFID reader. You must install and run the PhidgetWebService on the computer that has the PhidgetRFID connected. Once the PhidgetWebService is up and running, your program can then communicate with it in order to control the PhidgetRFID reader. You can obtain the Phidget library (containing the PhidgetWebService) from http://www.phidgetsusa.com (go to Downloads then choose Release) and download the PHIDGET.msi file.

Author's Note: Phidget's use of the term "WebService" is a little misleading here. PhidgetWebService is not an XML Web service such as most developers are familiar with. Rather, it is a Windows Service that runs in the background.

Once installed, you can find the PhidgetWebService in the C:\Program Files\Phidgets directory. You can invoke the PhidgetWebService Manager (a GUI version of the WebService) by running the PhidgetWebServiceManger.exe from this directory. Once it is up and running, you'll find it in the System Tray (see Figure 19).

Figure 19: The PhidgetWebService Manager in the System Tray.
Figure 20: The PhidgetWebService Manager.
Double-click on the icon to launch the PhidgetWebService Manager. Using the manager, you can change the settings of the WebService as well as manage your Phidget devices (not just the PhidgetRFID reader). As shown in Figure 20, the PhidgetWebService is listening at port 5001 and requires a password ("pass" is the default) to access it. With your PhidgetRFID reader attached to your computer, click Start. My reader has the unique serial number 6207.

Figure 21: The client and the reader need not be on the same computer.
One unique feature of the PhidgetWebService is that your client application need not necessarily be running on the same computer as the one with the reader connected. The client application uses sockets communication to talk to the PhidgetWebService, meaning that you can connect the PhidgetRFID reader to one computer while running the client application on another computer (as long as they can see each other on the network). Figure 21 shows one possible scenario. The advantage of this approach is that your client can be running on mobile platform devices (such as Pocket PC) as long as it can communicate with the host computer using sockets communication.

PhidgetRFID APIs
The PhidgetRFID reader, which is installed when you install the PhidgetWebService, exposes its functionality as APIs located in the PhidgetNET.dll library.

To use the PhidgetRFID, first import the PhidgetNET.dll library into your application. Right-click on the project name in Solution Explorer and then select "Add Reference…." In the Browse tab, navigate to C:\Program Files\Phidgets\ and select PhidgetNET.dll.

Coding the Application
Switch to the code-behind of Form1 and first declare a member variable representing the PhidgetRFID reader:

   Public Class Form1
   Dim WithEvents RFIDReader As PhidgetsNET.PhidgetRFID
When the form is loaded, instantiate the PhidgetReader variable and open a connection to the computer running the PhidgetWebService. The Form1_Load event code below accomplishes that:

   Private Sub Form1_Load(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load
      RFIDReader = New PhidgetsNET.PhidgetRFID
      RFIDReader.OpenRemoteIP("localhost", 5001, -1, "pass")
      ToolStripStatusLabel1.Text = "Not Connected"
   End Sub
Author's Note: Notice that I used localhost as my reader connected to my local computer. The 5001 parameter corresponds to the port number entered in the PhidgetWebService Manager (see Figure 20). The -1 parameter value refers to the first device that is found in the PhidgetWebService. Alternatively, you can use 6207 (the device serial number). The "pass" is the password (also set in the PhidgetWebService Manager).

For the PhidgetRFID, you need to service four important events:

  • Attach. Fired when a PhidgetRFID reader is attached to the computer running the PhidgetWebService.
  • Detach. Fired when a PhidgetRFID reader is detached from the computer running the PhidgetWebService.
  • Error. Fired when there is an error with the PhidgetRFID reader.
  • Tag. Fired when a tag is scanned using the PhidgetRFID reader.
In the Attach event, when a reader is connected, you will turn on the LED on the reader as well as enable the reader by using the SetOutputState() method of the PhidgetRFID class:

   Private Sub RFIDReader_Attach(ByVal sender As Object, _
      ByVal e As PhidgetsNET.AttachEventArgs) _
      Handles RFIDReader.Attach
      '--- display the status
      ToolStripStatusLabel1.Text = "Phidget RFID Reader Connected"
      '--- Enable onboard LED
       chkTurnOnLED.Checked = True
       RFIDReader.SetOutputState(2, True)
      '--- Enable RFID Reader 
      chkEnableReader.Checked = True
      RFIDReader.SetOutputState(3, True)
   End Sub
When you detach the reader, you simply update its status in the status bar.

   Private Sub RFIDReader_Detach(ByVal sender As Object, _
      ByVal e As PhidgetsNET.DetachEventArgs) _
      Handles RFIDReader.Detach
      '--- display the status 
      ToolStripStatusLabel1.Text = _
         "Phidget RFID Reader Not Connected"
   End Sub
The same goes for the Error event.

   Private Sub RFIDReader_Error(ByVal sender As Object, _
      ByVal e As PhidgetsNET.ErrorEventArgs) _
      Handles RFIDReader.Error
      '--- display the error 
      ToolStripStatusLabel1.Text = _
   End Sub
For the Tag event, you will invoke a delegate to display the tag ID.

   Private Sub RFIDReader_Tag(ByVal sender As Object, _
      ByVal e As PhidgetsNET.TagEventArgs) _
      Handles RFIDReader.Tag
      '--- when incoming data is 
      '--- received, update the 
      '--- TagID textbox 
      txtTagID.BeginInvoke(New myDelegate( _
        AddressOf updateTextBox), New Object() {e.getTag})
   End Sub
The delegate and the subroutine to update the textbox with the tag ID is defined as follows:

   '--- update the Tag ID textbox 
   Public Delegate Sub myDelegate(ByVal str As String)
   Public Sub updateTextBox(ByVal str As String)
      '--- update the textbox control 
      With txtTagID
         .Text = str
      End With
   End Sub
You can also turn on/off the LED on the reader during run time. This is serviced by the CheckChanged event of the chkTurnOnLED control.

   Private Sub chkTurnOnLED_CheckedChanged( _
      ByVal sender As System.Object, ByVal e As System.EventArgs) _
      Handles chkTurnOnLED.CheckedChanged
      '--- Enable/Disable onboard LED 
      RFIDReader.SetOutputState(2, chkTurnOnLED.Checked)
   End Sub
Figure 22: A tag scanned successfully.
Similarly, you can enable/disable the reader by servicing the CheckChanged event of the chkEnableReader control.

   Private Sub chkEnableReader_CheckedChanged( _
      ByVal sender As System.Object, ByVal e As System.EventArgs) _
      Handles chkEnableReader.CheckedChanged
      '--- Enable RFID Reader 
      RFIDReader.SetOutputState(3, chkEnableReader.Checked)
   End Sub
Testing the Application
That's it! You can now press F5 to test the application. Ensure you've used PhidgetWebService Manager to enable your PhidgetRFID reader. Figure 22 shows a tag scanned successfully.

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