(R/W RFID) How to read data that was written in RFID tag
Analog_M0n
Posts: 1
Hi there,
I have here with me a RFID Read/Write module and a EM4x50 RFID R/W 54mm x 85mm rectangular tag.
I am using the RFID Module with an Arduino UNO and have been able to write data into the RFID tag using the code shown below. However, I need help with reading that data that was written into the RFID tag. Please assist. Thanks.
Writting code:
Reading code (for referance):
Both codes are from http://playground.arduino.cc/Learning/ParallaxRFIDreadwritemodule
I have here with me a RFID Read/Write module and a EM4x50 RFID R/W 54mm x 85mm rectangular tag.
I am using the RFID Module with an Arduino UNO and have been able to write data into the RFID tag using the code shown below. However, I need help with reading that data that was written into the RFID tag. Please assist. Thanks.
Writting code:
[COLOR=#000000][FONT=courier new]#include <SoftwareSerial.h>[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] #define RFID_WRITE 0x02[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] #define txPin 6[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] #define rxPin 8[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] #define whichSpace 4[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] #define first 1 // first, second, thrid, and fourth are four arbitrary values which will be written to the RFID tag at address whichSpace[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] #define second 26[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] #define third 3[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] #define fourth 27[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]SoftwareSerial mySerial(rxPin, txPin);[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]void setup()[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]{[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] Serial.begin(9600);[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] Serial.println("RFID Write Test");[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] mySerial.begin(9600);[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] pinMode(txPin, OUTPUT); [/FONT][/COLOR] [COLOR=#000000][FONT=courier new] pinMode(rxPin, INPUT); [/FONT][/COLOR] [COLOR=#000000][FONT=courier new]}[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]void suppressAll() //Keeps error code & the "write confirmation" codes from being printed in the serial monitor [/FONT][/COLOR] [COLOR=#000000][FONT=courier new]{[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] if(mySerial.available() > 0)[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] { mySerial.read();[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] suppressAll();[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] }[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]} [/FONT][/COLOR] [COLOR=#000000][FONT=courier new] void loop()[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]{[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] int val;[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] mySerial.print("!RW");[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] mySerial.write(byte(RFID_WRITE));[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] mySerial.write(byte(whichSpace));[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] mySerial.write(byte(first));[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] mySerial.write(byte(second));[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] mySerial.write(byte(third));[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] mySerial.write(byte(fourth));[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]if(mySerial.available() > 0) { [/FONT][/COLOR] [COLOR=#000000][FONT=courier new] val = mySerial.read();[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] if (val == 1) //If data was written successfully[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] { Serial.println("Data written succesfully!");[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] suppressAll();[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] }[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] else suppressAll(); //If an error occured during writing, discard all data recieved from the RFID writer[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] }[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]delay(250);[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]}[/FONT][/COLOR]
Reading code (for referance):
[COLOR=#000000][FONT=courier new]#include <SoftwareSerial.h>[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]#define RFID_READ 0x01[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]#define txPin 6[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]#define rxPin 8[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]SoftwareSerial mySerial(rxPin, txPin);[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]int val;[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]int runs = 0;[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]void setup()[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]{[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] Serial.begin(9600);[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] Serial.println("RFID Read/Write Test");[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] mySerial.begin(9600);[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] pinMode(txPin, OUTPUT); [/FONT][/COLOR] [COLOR=#000000][FONT=courier new] pinMode(rxPin, INPUT); [/FONT][/COLOR] [COLOR=#000000][FONT=courier new]}[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]void suppressAll() //suppresses the "null result" from being printed if no RFID tag is present[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]{[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] if(mySerial.available() > 0)[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] { mySerial.read();[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] suppressAll();[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] }[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]}[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]void loop()[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]{[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] int val;[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] mySerial.print("!RW");[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] mySerial.write(byte(RFID_READ));[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] mySerial.write(byte(32));[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] if(mySerial.available() > 0)[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] { [/FONT][/COLOR] [COLOR=#000000][FONT=courier new] val = mySerial.read(); //The mySerial.read() procedure is called, but the result is not printed because I don't want the "error message: 1" cluttering up the serial monitor[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] if (val != 1) //If the error code is anything other than 1, then the RFID tag was not read correctly and any data collected is meaningless. In this case since we don't care about the resultant values they can be suppressed[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] {suppressAll();} [/FONT][/COLOR] [COLOR=#000000][FONT=courier new] } [/FONT][/COLOR] [COLOR=#000000][FONT=courier new] if(mySerial.available() > 0) { [/FONT][/COLOR] [COLOR=#000000][FONT=courier new] val = mySerial.read();[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] Serial.print("1st:");[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] Serial.println(val, HEX);[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] }[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]if(mySerial.available() > 0) { [/FONT][/COLOR] [COLOR=#000000][FONT=courier new] val = mySerial.read();[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] Serial.print("2nd:");[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] Serial.println(val, HEX);[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] }[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]if(mySerial.available() > 0) { [/FONT][/COLOR] [COLOR=#000000][FONT=courier new] val = mySerial.read();[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] Serial.print("3rd:");[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] Serial.println(val, HEX);[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] }[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]if(mySerial.available() > 0) { [/FONT][/COLOR] [COLOR=#000000][FONT=courier new] val = mySerial.read();[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] Serial.print("4th:");[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] Serial.println(val, HEX);[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] Serial.println("-----------------");[/FONT][/COLOR] [COLOR=#000000][FONT=courier new] } [/FONT][/COLOR] [COLOR=#000000][FONT=courier new]delay(750);[/FONT][/COLOR] [COLOR=#000000][FONT=courier new]}[/FONT][/COLOR]
Both codes are from http://playground.arduino.cc/Learning/ParallaxRFIDreadwritemodule
Comments
Take a look at the read and write commands on page 4 of the RFIDReadWrite documentation. For both read and write you need to send 3 things - first the 3 bytes "!RW", second the read or write command (0x01 or 0x02), and finally the address byte (1 to 33).
On a read command the RFIDr/w will return 5 bytes of data. The first byte is the status and the next 4 are the data.
A write command is followed by sending the 4 bytes of data to be written. A read should then be done to get the status byte.