Search code examples
c++comparisonequalityuint32-t

C++ equality for uint32_t type not comparing


I am reading RFID cards and trying to compare the ID's as once the card passes the reader, the reader actually reads it more than once so I need to discard the duplicates.

UPDATED : The following code gets the cards ID's

uint32_t IndentifyTag(Adafruit_PN532 rfidReader)
{
    uint8_t tagID[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
    uint8_t tagIDLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
    uint32_t cardid = tagID[0];
    boolean success = rfidReader.readPassiveTargetID(PN532_MIFARE_ISO14443A, &tagID[0], &tagIDLength);
    if (success) {
        // Display some basic information about the card for testing
        Serial.println("Found an ISO14443A card");
        Serial.print("  UID Length: "); Serial.print(tagIDLength, DEC);     Serial.println(" bytes");
        Serial.print("  UID Value: "); rfidReader.PrintHex(tagID, tagIDLength);

        if (tagIDLength == 4)
        {
            // Mifare Classic card 
            cardid <<= 8;
            cardid |= tagID[1];
            cardid <<= 8;
            cardid |= tagID[2];
            cardid <<= 8;
            cardid |= tagID[3];
            Serial.print("Mifare Classic card #");
            Serial.println(cardid);
        }
        Serial.println("");
    }
    return cardid;
}

And in a switch statement's case, I am testing the tags for equality as follows:

uint32_t priorTag = 0000000;
tag = IndentifyTag(entryRFIDReader);

if (tag == priorTag)
{               
    Serial.println("These tags are the same!!!");
}
if (tag != priorTag)
{
    Serial.println("I got here!");
    tagCount += 1;
}
priorTag = tag;
SSerial.println("tagCount = "); Serial.println(tagCount);

The problem is even if the reader reads the same card 3 or 4 times in one pass, they are never equal and therefore the tagCounter is counting the dupes. Both tag and priorTag are of type uint32_t so should be comparable with ==, but it is not working in this context.

Found an ISO14443A card
UID Length: 4 bytes
UID Value: 0x92 0x77 0x88 0xBB
Mifare Classic card #7833787

I got here!
tagCount = 1
Found an ISO14443A card
UID Length: 4 bytes
UID Value: 0x92 0x77 0x88 0xBB
Mifare Classic card #7833787

I got here!
tagCount = 2
Found an ISO14443A card
Mifare Classic card #7833787

I got here!
tagCount = 3
Found an ISO14443A card
Mifare Classic card #7833787

I got here!
tagCount = 4

Solution

  • The variable priorTag is declared too local, inside the loop you have. So it always has the value 0 at the beginning of each iteration.

    Move the declaration of the variable priorTag outside the loop.