I got a rfid reader and a arduino uno r3 talking over a uart connection.
The messeage from the rfid reader i not clean. I want the message to look like this, 0A007AA1AC
The first time I swipe the rfid chip I get this
-0A007AA1AC
but after that, if I swipe again, the response is not clean, and I can't extract the rfid code. this is 4 swipe with the rfid chip
-0A007AA1AC>-0A00-0A007AA1AC>>00A0-0A007AA1AC>>00A0-0A007AA1AC
if I break the lines up, it look something like this.
-0A007AA1AC>
-0A00-0A007AA1AC
>00A0-0A007AA1AC
>00A0-0A007AA1AC
what is wrong with the communication, and how do I get a clean response.
int readline(int readch, char *buffer, int len)
{
static int pos = 0;
int rpos;
if (readch > 0) {
switch (readch) {
case '\n': // Ignore new-lines
break;
case '\r': // Return on CR
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
default:
if (pos < len-1) {
buffer[pos++] = readch;
buffer[pos] = 0;
}
}
}
// No end of line has been found, so return -1.
return -1;
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
static char buffer[12];
if (readline(Serial.read(), buffer, 12) > 0) {
Serial.print(buffer);
}
}
I did put in the line break, for easier reading.
maybe it is old code not being erased, or maybe it is going to fast, so it can't write it fast enough. ??
int readline(int readch, char *buffer, int len)
{
static int pos = 0;
int rpos;
if (readch > 0) {
switch (readch) {
case '\n': // Ignore new-lines
break;
case '\r': // Return on CR
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
default:
if (pos < len-1) {
buffer[pos++] = readch;
buffer[pos] = 0;
}
}
}
// No end of line has been found, so return -1.
return -1;
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
static char buffer[12];
while (Serial.available()){
if (readline(Serial.read(), buffer, 12) > 0) {
Serial.print(buffer);
Serial.print("*dummytext*");
}
}
}
Looks like you are using the Uno serial port to talk to the RFID reader and to the serial monitor for debugging?
If so, you will get interference between them. From the user manual for the reader it has a command line interface on its serial port, and I guess it will echo back characters typed into it. So when you display the text received from the reader, that gets sent back to it, and it sends it to the Uno again.
Maybe you should install the SoftwareSerial library and use that for talking to the reader.
hey Hackscribble it's working, I remove the wire, that transmit back to the rfid reader, and now my arduino debug window only output -0A007AA1AC> over and over again