April 17, 2005

The patch has been integrated in to CVS. The patch is no longer necessary. However, it will remain here for those who want to still use it. You will have to patch it against the CVS version right before the support was added :)

Overview

I purchased a RFID reader from Phidgets. While the product was shipping, I found that there was no linux support for the device. However, there was an existing project called libphidgets that had support for other products Phidgets makes. Mostly they supported servos and interface controllers. I decided that this was a good base, and that I should see if I couldn't add RFID support to the library.

Three days later, I finished what I set out to do.

Getting it

You can get the patch here.

To apply the patch, you must first checkout the cvs version of libphidgets. Information on doing that can be found on the libphidgets website. To apply the patch, use the wonderful patch utility:

(libphidgets)$ patch -p1 < rfid.diff

You can then compile libphidgets in the usual way:

$ ./autogen.sh $ make

Usage

The best source of information about how to use the patch can be found in the test program. It can be found in the test subdirectory of the libphidgets source tree.

However, if you don't feel like wading through test code, here is a quick rundown: (These are the basic functions to use the RFID Reader) All functions return PHIDGET_RET_SUCCESS on success unless documented as otherwise.

PhidgetRFID * phidget_new_PhidgetRFID (void)
This creates a new RFID Phidget. It should be called after phidgets_init(). Upon failure it returns a pointer to 0.

phidget_rfid_open(PhidgetRFID * rfid, int serial, int tries)
This opens the RFID reader with the given serial. To obtain the serial, use the lsusb tool. Tries is the number of of times it should try to open the reader before it returns an error. The rfid pointer should have been initialized with phidget_new_PhidgetRFID().

phidget_rfid_set_state(PhidgetRFID * rfid, bool enabled, bool onboard_led, bool external_led, bool plus_five)
This sets the state of the RFID reader ;). This enables the toggles that the reader supports. The toggles are: Whether the reader is on to read tags or not. Whether the onboard LED is turned on. Whether the port to an external LED is active. Finally, whether the +5V output on the reader is on or not. This function should be called after you open the RFID reader.

phidget_set_toggle(PhidgetRFID * rfid, phidget_rfid_toggle which, bool state)
This is another function to set the state of the reader. Instead of giving it all the values like phidget_rfid_set_state, one only has to give it which toggle they want to change, and whether it should be on or off. phidget_rfid_toggle is an enumeration with the following values:
PHIDGET_RFID_ENABLE PHIDGET_RFID_EXTERNAL_LED PHIDGET_RFID_ONBOARD_LED PHIDGET_RFID_PLUS_FIVE
The meaning should be self explanitory.

phidget_rfid_get_tag(PhidgetRFID * rfid, int timeout)
This is the function you have probably been waiting for. This function tries to read a tag from the reader in the given timeout (in milliseconds). Upon success, the tag is stored in rfid->l_tag and the time in which it was captured (seconds since the epoch) is stored in rfid->time. l_tag is a long long. time is a time_t type. If a card could not be read in the given timeout period, the function returns PHIDGET_RET_TIMEOUT.

phidget_rfid_close(PhidgetRFID * rfid)
This closes the RFID reader that was opened previously. Not much else to say.

phidget_delete_PhidgetRFID(PhidgetRFID ** rfid)
This destroys the PhidgetRFID structure that was created previously. Notice that it is a double pointer. It will set the pointer to 0, as well as free it.

These functions all depend on libphidgets itself being initialized properly. Please see the appropriate documentation to do so.