The RFID reader is a MFRC522 and using it with a Mega 2560. I have all the master tag references ( 4 byte wide ) stored in a SD card. When the user scans i get the scanned byte array in a readCard[] variable. And the code below is used for checking the match... since this is a function from a large program all variable declarations do not appear here.
Just wanted to know if there is a better way to do this comparison ??
// FUNCTION TO CHECK IF THE SCANNED RFID MATCHES WITH REQUIRED RFID
boolean rfidMatchDisp(unsigned long recIndex) {
char rec_char_RFID[5];
char act_char_RFID[5];
rec_char_RFID[4] = '\0'; // Null Terminate the strings.... required ??
act_char_RFID[4] = '\0';
int result = -1;
char letter ;
char actualRFID[5];
long rfidPosition = 0;
File dataFile = SD.open("TAGREF.CSV", FILE_READ); // Open the file
dataFile.seek(recIndex + 1); // recIndex is from another function...
rfidPosition = dataFile.position() + 1; // Index the RFID position for reading
dataFile.seek(rfidPosition);
for ( int i = 0; i < 4; i++ ) { // Read the four positions of RFID reference
letter = dataFile.read();
rec_char_RFID[i] = letter;
}
sprintf ( actualRFID, "%02X%02X%02X%02X", readCard[0], readCard[1], readCard[2], readCard[3]);
result = strcmp (rec_char_RFID, actualRFID);
return result;
dataFile.close();
}
I would compare each character as it is read from the file and stop as soon as there was a mismatch.
#include "SD.h"
void setup() {}
void loop() {}
char readCard[5];
boolean rfidMatchDisp(unsigned long recIndex) {
int result = true; // assume success
long rfidPosition = 0;
File dataFile = SD.open("TAGREF.CSV", FILE_READ); // Open the file
dataFile.seek(recIndex + 1); // recIndex is from another function...
rfidPosition = dataFile.position() + 1; // Index the RFID position for reading
dataFile.seek(rfidPosition);
// look at each character
for (int i = 0; i < 4; i++ ) {
// if not a match, set return value and stop looking
if ( dataFile.read() != readCard[i])
{
result = false;
break;
}
}
dataFile.close();
return (result);
}