Go Down

Topic: Read RFID tag ref from SD card and check match (Read 1 time) previous topic - next topic

Mogaraghu

Sep 30, 2017, 07:11 am Last Edit: Sep 30, 2017, 07:23 am by Mogaraghu
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 ??

Code: [Select]

// 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();
}

sterretje

#1
Sep 30, 2017, 09:25 am Last Edit: Sep 30, 2017, 09:27 am by sterretje
First of all, your sprintf will place 8 characters in actualRFID; problems guaranteed

A memcmp() might be a better option if the data is stored in binary format on the card; no conversions, just comparing 4 bytes.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

johnwasser

Code: [Select]
  return result; 

  dataFile.close();


Don't you get a compiler warning because you have code that will never be reached?
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

Delta_G

Code: [Select]
  return result;

  dataFile.close();


Don't you get a compiler warning because you have code that will never be reached?
We don't need no stinking compiler warnings.  That's what the forum is for.  Try the code, post it and wait for someone to tell you how to fix it.  The old days of learning the language and learning how to write program logic are long gone.  Just type out some random stuff and post it on a forum and wait for someone who knows to fix it for you. 

But what will these kids do when we're all gone and nobody is left that knows how to correct it?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Mogaraghu

Code: [Select]
  return result;

  dataFile.close();


Don't you get a compiler warning because you have code that will never be reached?
Problem of selective posting to discuss a specific point. OK this is no defence for the goof up.  Thanks for pointing out.

The compiler warning is set to Default level in my IDE 1.8.1 and maybe the warning scrolled up ....

Blue Eyes

I would compare each character as it is read from the file and stop as soon as there was a mismatch.

Code: [Select]


#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);
}

Mogaraghu

I would compare each character as it is read from the file and stop as soon as there was a mismatch.

Code: [Select]


#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);
}


Looks so simple and functional and not using sprintf()

Exactly the kind of suggestion I was looking to learn. What is so difficult for a novice is basic for an expert. Let me check this out ..

Thanks for your time.

Go Up