I am working on an RFID sketch that logs an rfid tags serial number, the current temp and the time the tag was detected to an sd card, it all works fine BUT the tags id that is logged to the sd card is 3E000CAADF not 0000830175 as printed on the card, I know that CAADF in hex = 830175 in dec, but can not figure out how to collect the hex values together and transform this hex string into the decimal string I am looking for also how to deal woth the leading zeros.
I have read somewhere that I have to ignore the first two digits of the hex output ("3E") and then process the rest and this should give the right value that I need, this looks right to me as my hex above converts to the last part of the tags number.
The sketch stores the values i need as data[1], data[2] through to data[10] these have been used to verify the rfid tag and currently logged to sd, so I need to write a routine that will combine the values of data[3] through to data[10], then convert this to decimal before writing to the sd? insted of just writing straight data to the sd.
Can anyone help?
void read_and_log (){
//read the rfid tag if present
if (mySerial.available() > 0) {
data[counter] = mySerial.read();
counter++;
if(counter > 13) {
//we read the whole message, so reset counter
counter = 0;
//check if start of text and end of text is correct
if(data[0] == stx && data[13] == etx) {
//Turn LED on pin 8 ON
digitalWrite(8,HIGH);
Serial.println("Start and end of RFID Tag correctly received.");
//Hex ID blocks. Two transmitted Bytes form one Hex ID block.
//Hex ID blocks: 6 2 | E 3 | 0 8 | 6 C | E D
//Transmitted Bytes: 36H 32H | 45H 33H | 30H 38H | 36H 43H | 45H 44H
hexBlock1 = AsciiCharToNum(data[1])*16 + AsciiCharToNum(data[2]);
hexBlock2 = AsciiCharToNum(data[3])*16 + AsciiCharToNum(data[4]);
hexBlock3 = AsciiCharToNum(data[5])*16 + AsciiCharToNum(data[6]);
hexBlock4 = AsciiCharToNum(data[7])*16 + AsciiCharToNum(data[8]);
hexBlock5 = AsciiCharToNum(data[9])*16 + AsciiCharToNum(data[10]);
//Transmitted checksum.
hexChecksum = AsciiCharToNum(data[11])*16 + AsciiCharToNum(data[12]);
//id = hexBlock2, hexBlock3, hexBlock4, hexBlock5;
//XOR algorithm to calculate checksum of ID blocks.
hexCalculatedChecksum = hexBlock1 ^ hexBlock2 ^ hexBlock3 ^ hexBlock4 ^ hexBlock5;
if ( hexCalculatedChecksum == hexChecksum )
{
Serial.println("Calculated checksum matched transmitted checksum.");
//going to write it all to sd now
Serial.println("Will try to write RFID tag ID, checksum, time and temp to cp1.csv on SD now.");
// open cp1.csv. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("cp1.csv", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
//Write the RFID Card ID
dataFile.print("RFID Tag ID:,");
dataFile.print(data[1], BYTE);
dataFile.print(data[2], BYTE);
dataFile.print(data[3], BYTE);
dataFile.print(data[4], BYTE);
dataFile.print(data[5], BYTE);
dataFile.print(data[6], BYTE);
dataFile.print(data[7], BYTE);
dataFile.print(data[8], BYTE);
dataFile.print(data[9], BYTE);
dataFile.print(data[10], BYTE);
dataFile.print(",");
//Log the time from the rtc
int rtc_sec, rtc_min, rtc_hou, rtc_wee, rtc_dat, rtc_mon, rtc_yea;
delay(300); // There will be new values every 100ms
RX8025.getRtcTime(&rtc_sec, &rtc_min, &rtc_hou, &rtc_wee, &rtc_dat, &rtc_mon, &rtc_yea);
dataFile.print("Date:,");
dataFile.print(rtc_dat,DEC);
dataFile.print("/");
dataFile.print(rtc_mon,DEC);
dataFile.print("/");
dataFile.print(rtc_yea,DEC);
dataFile.print(",");
dataFile.print("Time:,");
dataFile.print(rtc_hou,DEC);
dataFile.print(":");
dataFile.print(rtc_min,DEC);
dataFile.print(":");
dataFile.print(rtc_sec,DEC);
//get temp anf print temp
res = Wire.requestFrom(72,2);
if (res == 2) {
msb = Wire.receive(); /* Whole degrees */
lsb = Wire.receive(); /* Fractional degrees */
val = ((msb) << 4); /* MSB */
val |= (lsb >> 4); /* LSB */
dataFile.print(",");
dataFile.print("Temp,");
dataFile.println(val*0.0625);
}
//close the cp1.csv file
dataFile.close();
// print to the serial port for monitoring this line can be removed along with
// 'void monitorout()' at the end of the script to reduce compiled sketch size, but
// only do this if you are sure every thing works and you DO NOT need a diag output
// when writing trhe data to the SD card
monitorout();
// flush newsoftserial and delay to prevent mutiple readings of same RFID tag
delay(2500);
mySerial.flush();
//turn led off
digitalWrite(8,LOW);
}
// if the file cp1.csv isn't open, pop up an error:
else {
Serial.println("Error opening data file - cp1.csv");
return;
}
}
else {
Serial.println("Calculated checksum didn't match transmitted checksum. Corrupt data!");
// don't do anything more:
return;
}
}
}
}
}