uint32_t 类型的 C++ 相等性不比较
C++ equality for uint32_t type not comparing
我正在读取 RFID 卡并尝试比较 ID,因为一旦卡通过 reader,reader 实际上不止一次读取它,所以我需要丢弃重复项。
更新:以下代码获取卡片 ID
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;
}
在 switch 语句的情况下,我正在测试标签的相等性,如下所示:
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);
问题是,即使 reader 一次读取同一张卡片 3 次或 4 次,它们也永远不会相等,因此 tagCounter 正在计算重复次数。 tag 和 priorTag 都是 uint32_t 类型,因此应该可以与 == 进行比较,但它在这种情况下不起作用。
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
priorTag = tag;
Serial.println("******************************");
Serial.print("Prior tag: "); Serial.println(priorTag);
Serial.print("Current tag: "); Serial.println(tag);
你能解释一下当这四行代码被执行时,你怎么可能在这里看到两个不同的值吗?
变量 priorTag
被声明为过于局部,在您的循环内。因此它在每次迭代开始时始终具有值 0。
将变量 priorTag
的声明移到循环外。
我正在读取 RFID 卡并尝试比较 ID,因为一旦卡通过 reader,reader 实际上不止一次读取它,所以我需要丢弃重复项。
更新:以下代码获取卡片 ID
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;
}
在 switch 语句的情况下,我正在测试标签的相等性,如下所示:
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);
问题是,即使 reader 一次读取同一张卡片 3 次或 4 次,它们也永远不会相等,因此 tagCounter 正在计算重复次数。 tag 和 priorTag 都是 uint32_t 类型,因此应该可以与 == 进行比较,但它在这种情况下不起作用。
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
priorTag = tag;
Serial.println("******************************");
Serial.print("Prior tag: "); Serial.println(priorTag);
Serial.print("Current tag: "); Serial.println(tag);
你能解释一下当这四行代码被执行时,你怎么可能在这里看到两个不同的值吗?
变量 priorTag
被声明为过于局部,在您的循环内。因此它在每次迭代开始时始终具有值 0。
将变量 priorTag
的声明移到循环外。