Hi everyone,
I need you help in my project. I have an RFID reader made by Chafon, and I try to use this over RS232. I receive every time a wrong code/response. The test TAG have ID like "E2801105200072DA390E0987" and I get this code over RS232 "-411267-411266-411241" with Arduino UNO.
This is my code:
#include <EEPROM.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#define DEBUG
#include <string.h>
int rxdata = 0;
char rxstate = 0;
unsigned char bReadLength = 0;
String strTagID = "";
String LastTag = "000000000000000000000000" ;
String ErrorTag = "1E000000000000000000001E" ;
SoftwareSerial Reader (3, 2); //Original 9, 8
const int IR = 10;
const int TrigerOut = 11;
//----------------------------------------------------------------------------
void setup() {
Serial.begin(9600); //Connect to PC
Reader.begin(9600); //Connect to Reader
Wire.begin(8);
Wire.onRequest(requestEvent);
pinMode(IR, INPUT);
pinMode(TrigerOut, OUTPUT);
digitalWrite(TrigerOut, HIGH);
}
//----------------------------------------------------------------------------
//Check the rev char is Legal tag data
unsigned char CheckIsHexChar(unsigned char bChar)
{
if (bChar >= '0' && bChar <= '9') return 1;
else if (bChar >= 'A' && bChar <= 'F') return 1;
else if (bChar >= 'a' && bChar <= 'f') return 1;
else return 0;
}
//----------------------------------------------------------------------------
void ReadTagCmd() {
if (digitalRead(7) == LOW) {
byte message[] = {0x53, 0x57, 0x00, 0x03, 0xff, 0x41, 0x13};
Reader.write(message, sizeof(message));
Serial.println("Read command sent...");
}
}
//----------------------------------------------------------------------------
void loop() {
ReadTagCmd();
while (Reader.available() > 0)
{
rxdata = (char(Reader.read()));
Serial.print(String(rxdata));
switch (rxstate)
{
case 0: //get head
if (String(rxdata) == ("0x02")) {
rxstate = 1;
strTagID = "";
bReadLength = 0;
}
break;
case 1: //Get TagID
if (String (rxdata) != ("0x03")) //Not End
{
if (CheckIsHexChar(rxdata) == 1) //check legal char
{
strTagID = strTagID + char (rxdata);
bReadLength++;
if (bReadLength == 24) {
//Serial.println(bReadLength);
if (LastTag != strTagID) {
LastTag = strTagID;
//Serial.println(strTagID);
} else {
Serial.print("Current: ");
Serial.println(strTagID);
}
}
}
else
{
strTagID = "";
rxstate = 0;
}
}
else
{
if ((bReadLength) % 2 == 0) //TagLength(HEX data) is odd
{
if (strTagID != "")
{
Serial.print("TagID: ");
Serial.println(String (strTagID));
}
}
else
{
Serial.print("Unlegal Data\r\n");
}
strTagID = ""; rxstate = 0; bReadLength = 0;
}
break;
}
delay(5);
}
delay(300);
}
//----------------------------------------------------------------------------
void requestEvent()
{
char buffer[25];
LastTag.toCharArray(buffer, 25);
Wire.write(buffer);
}
Thanks in advance.