如何获取字符串和字节进行比较?

时间:2019-05-28 17:41:11

标签: c++ arduino

我是Arduino和C ++编码的新手,我整天都在从事这个项目,但是却一无所获。我正在尝试将RFID阅读器的结果与数据库的结果进行比较,但是我一直遇到错误。

我尝试了多种解决此问题的方法,但是firebase库没有getbyte函数。

int UID1 = 2;
int UID2 = 2;
int UID3 = 2;
int UID4 = 2;
int UID5 = 2;
int UID6 = 2;

byte nuidPICC[4];

constexpr uint8_t RST_PIN = 5; // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 4;  // Configurable, see typical pin layout above

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;

// Init array that will store new NUID

void setup() {
  Serial.begin(115200);
  SPI.begin();     // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522

  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
  delay(2000);
  Serial.println('\n');

  wifiConnect();

  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

  delay(10);
}

void loop() {

  // Look for new cards
  if (!rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if (!rfid.PICC_ReadCardSerial())
    return;

  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  Serial.println(rfid.PICC_GetTypeName(piccType));

  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
      piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
      piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }

  if (rfid.uid.uidByte[0] != nuidPICC[0] ||
      rfid.uid.uidByte[1] != nuidPICC[1] ||
      rfid.uid.uidByte[2] != nuidPICC[2] ||
      rfid.uid.uidByte[3] != nuidPICC[3]) {
    Serial.println(F("A new card has been detected."));

    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }

    Serial.println(F("The NUID tag is:"));
    Serial.print(F("UID: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
  } else {
    Serial.println(F("Card read previously."));
  }
  // Halt PICC
  rfid.PICC_HaltA();
  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();

  Serial.print(Firebase.getString("UID1") + "\n");
  Serial.print(Firebase.getString("UID2") + "\n");
  Serial.print(Firebase.getString("UID3") + "\n");
  Serial.print(Firebase.getString("UID4") + "\n");
  Serial.print(Firebase.getString("UID5") + "\n");
  Serial.print(Firebase.getString("UID6") + "\n");

  analogWrite(UID1, Firebase.getString("UID1").toInt());
  analogWrite(UID2, Firebase.getString("UID2").toInt());
  analogWrite(UID3, Firebase.getString("UID3").toInt());
  analogWrite(UID4, Firebase.getString("UID4").toInt());
  analogWrite(UID5, Firebase.getString("UID5").toInt());
  analogWrite(UID6, Firebase.getString("UID6").toInt());
  delay(3000);

  if (WiFi.status() != WL_CONNECTED) {
    wifiConnect();
  }
  delay(10);

  if (Firebase.getString("UID1") = rfid.uid.uidByte()) {
    Serial.print("Match")
  }
}

这是我的错误:

  

fireabse-alpha1:118:22:错误:分配的类型不兼容     “字符串”到“字节[10] {aka无符号字符[10]}”

     

if(rfid.uid.uidByte = Firebase.getString(“ UID1”)){
                         ^
     fireabse-alpha1:120:1:错误:预期为';'在“}”令牌之前

     

}
      ^

1 个答案:

答案 0 :(得分:0)

不需要

Serial.println('\n');。因为名称中的ln表示它将打印换行符。

rfid.uid.uidByte10 bytes的数组。
Firebase.getString("UID1") returns arduino String

您无法将数组与String进行比较。但是您可以将String的元素与数组的元素进行比较。

String handle = Firebase.getString("UID1");
uint8_t lengthToCompare = rfid.uid.size;
if(handle.length() != lengthToCompare){ // this only works if you compare the whole string
    // not equal
}

bool equal = true;
for(int i=0;i<lengthToCompare;++i) {
    equal &= handle.c_str()[i] == rfid.uid.uidByte[i]; // compare if equal and variable 
                                                       // equal remains true if so
}
// equal now holds if the sequences where equal

如前所述,rfid.uid.uidByte = Firebase.getString("UID1")不是一个比较,而是一个赋值,这很糟糕,因为String被赋给了一个字节数组。比较运算符为==

也在StackOverflow上发布(或使用您的代码)时:clang-format.exe -i <filename>是您的朋友。 (当然,假设您使用的是Windows,我不想假设您的操作系统偏好设置)