I just recently wrote an article at the Gumbo Labs blog on interfacing to the Parallax RFID reader. Here is the final code if you are only looking for that:
/**
* author Benjamin Eckel
* date 10-17-2009
*
*/
#define RFID_ENABLE 2 //to RFID ENABLE
#define CODE_LEN 10 //Max length of RFID tag
#define VALIDATE_TAG 1 //should we validate tag?
#define VALIDATE_LENGTH 200 //maximum reads b/w tag read and validate
#define ITERATION_LENGTH 2000 //time, in ms, given to the user to move hand away
#define START_BYTE 0x0A
#define STOP_BYTE 0x0D
char tag[CODE_LEN];
void setup() {
Serial.begin(2400);
pinMode(RFID_ENABLE,OUTPUT);
}
void loop() {
enableRFID();
getRFIDTag();
if(isCodeValid()) {
disableRFID();
sendCode();
delay(ITERATION_LENGTH);
} else {
disableRFID();
Serial.println("Got some noise");
}
Serial.flush();
clearCode();
}
/**
* Clears out the memory space for the tag to 0s.
*/
void clearCode() {
for(int i=0; i<CODE_LEN; i++) {
tag[i] = 0;
}
}
/**
* Sends the tag to the computer.
*/
void sendCode() {
Serial.print("TAG:");
//Serial.println(tag);
for(int i=0; i<CODE_LEN; i++) {
Serial.print(tag[i]);
}
}
/**************************************************************/
/******************** RFID Functions ***********************/
/**************************************************************/
void enableRFID() {
digitalWrite(RFID_ENABLE, LOW);
}
void disableRFID() {
digitalWrite(RFID_ENABLE, HIGH);
}
/**
* Blocking function, waits for and gets the RFID tag.
*/
void getRFIDTag() {
byte next_byte;
while(Serial.available() <= 0) {}
if((next_byte = Serial.read()) == START_BYTE) {
byte bytesread = 0;
while(bytesread < CODE_LEN) {
if(Serial.available() > 0) { //wait for the next byte
if((next_byte = Serial.read()) == STOP_BYTE) break;
tag[bytesread++] = next_byte;
}
}
}
}
/**
* Waits for the next incoming tag to see if it matches
* the current tag.
*/
boolean isCodeValid() {
byte next_byte;
int count = 0;
while (Serial.available() < 2) { //there is already a STOP_BYTE in buffer
delay(1); //probably not a very pure millisecond
if(count++ > VALIDATE_LENGTH) return false;
}
Serial.read(); //throw away extra STOP_BYTE
if ((next_byte = Serial.read()) == START_BYTE) {
byte bytes_read = 0;
while (bytes_read < CODE_LEN) {
if (Serial.available() > 0) { //wait for the next byte
if ((next_byte = Serial.read()) == STOP_BYTE) break;
if (tag[bytes_read++] != next_byte) return false;
}
}
}
return true;
}

2 comments ↓
Hey mate,that is a rily awesome code thanks for that,I am doing my final year project using parallax rfid reader and arduino.The rfid reader reads the tag and compares it with the tags stored in its eeprom,if ther is a match it sends the tag to the nxt as a single byte,if there is no match it sends an empty bytes.Can yu please help with the part of writing and comparing the olridy stored in the eeprom id and the present tht has been read.thank u
Hey,
I need to use this RFID reader for my project. Can you tell me where I can get the code for serial structure that you used in your code. Did you write your own code or used a standard source code?
Leave a Comment