I'm trying to read some RFID tags using Innovations ID-20 reader. I've created this code:
#include <SoftwareSerial.h>
SoftwareSerial id20(2,3); // virtual serial port
char tag[13];
void setup()
{
Serial.begin(9600);
id20.begin(9600);
}
void loop () {
while (id20.available()<13){ //wait until all the 12 bytes are buffered
}
for (int i=0; i<13; ){
tag[i]=id20.read(); //write the bytes into the array
i++;
}
Serial.println(tag); //print the array
for (int i=0; i<13; i++){ //clear the array
tag[i]=0;
}
}
The result should be 010D429BBF6A, but, the first time I put the card over the reader I get:
010D429BBF6A 010D429BBF6A
The second time
010D429BB 010D429BBF6A
010D429BB
The third time
F6A
010D42 010D429BBF6A
010D429BBF6A
010D42
And so on.
What did I get wrong?
I have Arduino UNO and I use IDE 1.0.5
Thanks in advance.
Does the data from the RFID reader include a null terminator? If not, you should add one before you print. Be sure to make the buffer long enough to store the 13 characters AND the null terminator.
I've tried to modify the code as you described. The result is better, but not at all.
#include <SoftwareSerial.h>
SoftwareSerial id20(2,3); // virtual serial port
char tag[13];
void setup()
{
Serial.begin(9600);
id20.begin(9600);
}
void loop () {
while (id20.available()<13){ //wait until all the 12 bytes are buffered
}
for (int i=0; i<14; ){
if(i==13) { //inserts a null character
tag[12]=00;
i++;
}
else if(i!=0||i!=13){
tag[i-1]=id20.read(); //write the bytes into the array
i++;
}
else if (i==0) i++; //avoids the first bit, which is not data
}
Serial.println(tag); //print the array
for (int i=0; i<13; i++){ //clear the array
tag[i]=0;
}
}
[code]With this modified code I don't get any strange characters, but it happens a periodic behaviour which I cannot explain to myself.
[code]#include <SoftwareSerial.h>
SoftwareSerial id20(2,3); // virtual serial port
void setup()
{
Serial.begin(9600);
id20.begin(9600);
}
void loop () {
char tag[14];
while (id20.available()<14){ //wait until all the 12 bytes are buffered
}
for (int i=0; i<14; ){
if(i==13) { //inserts a null character
tag[13]='\0';
i++;
}
else if (i<13) {
tag[i]=id20.read();
i++;
}
}
Serial.println(tag); //print the array
}
[/code]1 -> No reading
2 -> 010D429BBF6A (which is the exact code)
3 ->
010D429BB (one blank character, then starts another line, partial code, two blank characters)
4 -> F6A
010D42
5 -> 9BBF6A
010
6 -> D429BBF6A
(Empty line)
010D429BBF6A (again the exact code after 4 cicles)[/code]
Then I get the exact code again after 9 cicles and so on, alternating between 4 and 9 cicles.
The result is the same, but now it reads the code from the first cycle.
I used 14 because I was trying different numeric values to make it work and since the result was identical I forgot to rewrite the correct value.
I don't have any ideas.
Thanks for the advice
If I try to read another card during the cycles while I'm getting the errors, I get part of the final code of the previous card and on the second line the first bytes of the card actually being read.
F2A (end of the previous card code)
010D4 (start of the card being read now, which is the same as the other because they were sold together)
Don't I have to clean the buffer each time after Serial.println?
It's what I supposed, too. Now I've tried this and it works fine. So I'll study its behaviour to understand how to correct my own code.
I'll tell you what I find out. Thanks again
Usually the RFID reader will include one or more terminator characters after the ID. Typically these will be CR (13) and LF (10). What I would do is read and buffer characters until I read a 10 or 13. If the number of buffered characters matches the length of an ID, put in a null and process the ID. After that, reset the buffer (set the counter back to 0). Be sure to also reset the buffer if you get too many characters for an ID.