Home > Articles, Embedded Systems > A simple function implementation for parsing RFID Tags in Arduino.

A simple function implementation for parsing RFID Tags in Arduino.

In this article I’ll show you how you can handle / parse the following RFID Tags from Sparkfun Electronics :

1) RFID Glass Capsule (125kHz) – Product ID: SEN-09416

2) RFID Button – 16mm (125kHz) – Product ID: SEN-09417

3) RFID Tag (125kHz) – Product ID: COM-08310

For these RFID Tags below there exist some RFID Readers :

1) RFID Reader ID-20 (125 kHz) – Product ID: SEN-08628

2) RFID Reader ID-12 (125 kHz) – Product ID: SEN-08419

Please, take account that the data structure of the RFID Tags is the following :

RFID Tags Data Format

The following function tries to parse an RFID Tag according to the data structure given :

// Try to handle / parse an RFID tag.
bool
RFIDTagHandled ()
{
  byte value = 0;       // temporary data received from RFID reader.
  byte code[6];         // code + checksum data of RFID tag received.
  byte checksum = 0;    // checksum data of RFID tag received.
  byte bytesRead = 0;   // number of received data from RFID reader.
  byte tempByte = 0;    // temporary value used for checksum calculation.

  bool handled = false; // flag indicating if an RFID tag was handled.

  const byte RFID_SIZE = 10; // the code size (digits) of the RFID tag.
  const byte CSUM_SIZE = 2;  // the csum size (digits) of the RFID tag.

  // if there are any data coming from the RFID reader.
  if (RFID.available () > 0) {
    // disable all interrupts.
    detachInterruptsISRs ();

    // check for the STX header (0x02 ASCII value).
    if (0x02 == (value = RFID.read ())) {
      // read the RFID digits & the checksum digits.
      while (bytesRead < (RFID_SIZE + CSUM_SIZE)) {
        // if there are any data coming from the RFID reader.
        if (RFID.available () > 0) {
          // get a byte from the RFID reader.
          value = RFID.read ();

          // check for ETX | STX | CR | LF.
          if ((0x0D == value) ||
              (0x0A == value) ||
              (0x03 == value) ||
              (0x02 == value)) {
            // stop reading - there is an error.
            break;
          }

          // store the RFID code digits to an external array.
          if (bytesRead < RFID_SIZE)
            RFIDCode[bytesRead] = value;

          // convert hex tag ID.
          if ((value >= '0') && (value <= '9'))
            value = value - '0';
          else if ((value >= 'A') && (value <= 'F'))
            value = 10 + value - 'A';

          // every two hex-digits, add byte to code.
          if (bytesRead & 1 == 1) {
            // make some space for this hex-digit by shifting
            // the previous hex-digit with 4 bits to the left.
            code[bytesRead >> 1] = (value | (tempByte << 4));

            if (bytesRead >> 1 != 5)
              // if this is checksum byte, calculate the checksum (XOR).
              checksum ^= code[bytesRead >> 1];
          }
          else
            tempByte = value;

          // ready to read next digit.
          bytesRead++;
        }
      }

      // handle the RFID digits & the checksum digits.
      if (bytesRead == (RFID_SIZE + CSUM_SIZE)) {
        // check if the RFID code is correct.
        if (code[5] == checksum)
          // set that the tag was handled.
          handled = true;
      }
    }

    // enable all external interrupts.
    attachInterruptsISRs ();
  }

  return handled;
}

For the functions detachInterruptsISRs and attachInterruptsISRs you should provide an implementation for disabling and enabling any interrupts used in your sketch. If you do not use any interrupts you may remove the lines that refer to these functions.

Also, take account that the RFID is a SoftwareSerial object that reads data from the RFID Readers referred.

Here is a way of using the function :

...
// RFID 10-digits code (+1 for string termination).
char RFIDCode[11] = {'\0'};

// try to handle an RFID Tag.
if (RFIDTagHandled ()) {
  // use of RFIDCode here...
}
...

If you don’t want to use the global variable RFIDCode for storing the code of the RFID Tag, refactor the function in order to use a String object as a parameter, pass it by reference, and store in it the code.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: