Arduino Forum

Using Arduino => Programming Questions => Topic started by: Jeff_85 on Apr 02, 2017, 02:08 am

Title: Compare byte arrary with char array (RFID)
Post by: Jeff_85 on Apr 02, 2017, 02:08 am

I have RFID and I get the UID with every reading. There are 4 bytes stored in array bytes. I have an SD card with the same brand and I want to consult in SD and validate. My problem: Uid is in byte array and my SD reading returns array of characters. I want to escape the String class. The reason I use SD and not eeprom is that I will have too much UID information on the card.

Any suggestions for comparing each byte of UID with the 8-character string in SD?

Thank you!
Title: Re: Compare byte arrary with char array (RFID)
Post by: econjack on Apr 02, 2017, 04:53 am
What does the code you've tried look like?
Title: Re: Compare byte arrary with char array (RFID)
Post by: holmes4 on Apr 02, 2017, 05:06 am
Look up the str**** group of functions

Mark
Title: Re: Compare byte arrary with char array (RFID)
Post by: Jeff_85 on Apr 02, 2017, 05:20 am
Here I read the UID.

Code: [Select]


const byte countTag = 5;       
unsigned char numTag[countTag] = {0};

void readTag(){
                                                 
                 
                  mfrc522.PCD_Init();                                           
                    if ( ! mfrc522.PICC_IsNewCardPresent())
                      {   
                            return;                                     
                      }                               
                                               
                      if ( ! mfrc522.PICC_ReadCardSerial()) 
                      {
                        return;
                      }

                 
                  for (byte i = 0; i < mfrc522.uid.size; i++)                 
                  {                                       
                     numTag[i] = (mfrc522.uid.uidByte[i]);                     
                     Serial.print(F(" HEX: "));
                     Serial.println(numTag[i], HEX);       
                  }
 
  //With reading ok, the output for example: AA BB CC EE FF
 
                findUser ();
                       
 
  mfrc522.PICC_HaltA(); 
  mfrc522.PCD_StopCrypto1();
}




The problem for me is that uid comes 4 bytes. In SD the UID is obtained as 8 characters. They print well on serial monitor, but I do not see how to compare them to validate.


Excerpt I get uid from SD:

Code: [Select]


void findUser (){ 

        const byte numVectorUser =  50;
        char vetorUserTag[numVectorUser] =  {0};
        unsigned char index =   0;       
        char cr;   
        char numberTag[10] =  {0}; 
       
        myFile3 = SD.open("users.txt");
        myFile3.seek(0);
       
         while(myFile3.available()){
            cr = myFile3.read();         
            vectorUserTag[index] = cr;
            Serial.print(vectorUserTag[index]);
            index++;
             
              //"," indicate the end of string
             
  if(cr == ','){           
                            index=0;           
               

                // Data coming from SD: AABBCCDDEE               
                //Here I want to evaluate if UID (4 bytes read) is equal to my record in SD (8 characters coming).
               
                }
          }
          myFile3.close();





thanks for your time
Title: Re: Compare byte arrary with char array (RFID)
Post by: sterretje on Apr 02, 2017, 08:42 am
First you need to create a nul terminated character array
Code: [Select]
    if (cr == ',')
    {
      // add nul terminator
      vectorUserTag[index]='\0';

    ...
    ...


Next you can use strtoul (https://linux.die.net/man/3/strtoul) to convert the character array to an unsigned long (from your example data, I assume it's 4 bytes). Demo code below
Code: [Select]
char uuidFromfile[] = "AABBCCDD";


void setup()
{
  Serial.begin(250000);

  unsigned long uuid;
  char *endptr;

  uuid = strtoul(uuidFromfile, &endptr, 16);
  Serial.println(uuid, HEX);

  ...
  ...

Next you can compare against the data that you go from the reader; unfortunately an unsigned long is stored in reverse order from what we need, so we reverse the order of the bytes that were read from the reader
Code: [Select]


  // uuid bytes from reader
  byte uuidMFRC[4] = {0xaa, 0xbb, 0xcc, 0xdd};

  // temporary variable to hold reversed uuid
  byte tmp[4];
  for (int cnt = 0; cnt < 4; cnt++)
  {
    tmp[3 - cnt] = uuidMFRC[cnt];
  }

And next the actual compare
Code: [Select]
  // compare reversed uuidMFRC with numeric uuid from file
  if (memcmp(tmp, (byte*)&uuid, 4) == 0)
  {
    Serial.println("match");
  }
  else
  {
    Serial.println("no match");
  }
}
Title: Re: Compare byte arrary with char array (RFID)
Post by: Jeff_85 on Apr 02, 2017, 11:17 pm
econjack, thanks for its time and response to the topic.

holmes4, his suggestion was the way.

sterretje, I understood your sketch and I will insert it in my need. Receive my thanks. :)