
The Data OutPut Format – ASCII STX (ASCII 02) DATA (10 ASCII) CHECK SUM (2 ASCII) CR (ASCII 13) LF (ASCII 10) ETX (ASCII 03)The communication starts with a start-of-communication (STX) byte (ASCII 02) and ends with end-of-communication (ETX) byte (ASCII 03). The STX byte is immediately followed by the ten-byte tag ID (unique signature), a checksum, a carriage return (ASCII 13), a linefeed (ASCII 10) and then the ETX byte.


/* RFID reader ID-12 for Arduino */
void setup()
{
// Open Serial Port
Serial.begin(9600);
}
void loop ()
{
byte i = 0;
byte val = 0;
byte code[6];
byte checksum = 0;
byte bytesread = 0;
byte tempbyte = 0;
if(Serial.available() > 0)
{
// Check for the STX Header (02 ASCII Value)
if((val = Serial.read()) == 2)
{
bytesread = 0;
// Read the RFID 10 digit code & the 2 digit checksum
while (bytesread < 12)
{
if( Serial.available() > 0)
{
val = Serial.read();
// Check for ETX | STX | CR | LF
if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02))
{
// Stop Reading - There is an Error.
break;
}
// Convert Hex Tag ID
if ((val >= '0') && (val <= '9'))
{
val = val - '0';
}
else if ((val >= 'A') && (val <= 'F'))
{
val = 10 + val - 'A';
}
// Every two hex-digits, add byte to code:
if (bytesread & 1 == 1)
{
// make some space for this hex-digit by
// shifting the previous hex-digit with 4 bits to the left:
code[bytesread >> 1] = (val | (tempbyte << 4));
if (bytesread >> 1 != 5)
{
// If we're at the checksum byte,
// Calculate the checksum... (XOR) - Exclusive OR
checksum ^= code[bytesread >> 1];
};
}
else
{
tempbyte = val;
};
// ready to read next digit
bytesread++;
}
}
// Print
if (bytesread == 12)
{
Serial.print("5-byte code: ");
for (i=0; i<5; i++)
{
if (code[i] < 16) Serial.print("0");
Serial.print(code[i], HEX);
Serial.print(" ");
}
Serial.println();
Serial.print("Checksum: ");
Serial.print(code[5], HEX);
Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
Serial.println();
}
bytesread = 0;
}
}
}
Unless otherwise stated, this code is released under the MIT License – Please use, change and share it.