如何使余额保持在RFID 522中?

how to make the balance remain stored in the RFID 522?

提问人:Benjamin Veras 提问时间:11/14/2023 最后编辑:GerhardhBenjamin Veras 更新时间:11/14/2023 访问量:40

问:

我有一个问题。我有一个 RFID 522,用于通过“串行”进行通信。当我刷卡时,它存储了 100 美元的价值,每次我再次刷卡时,它都会持续增加 +100 美元。但是,当我重置 Arduino 控制器 (ESP32) 并再次刷卡时,该值恢复为 100 美元,而不是继续与之前的余额一起累积。我想知道如何将余额永久存储在卡上,并随时使用。 该代码有效,但它只有未将该值永久存储在卡上的详细信息。

#include <SPI.h>
#include <MFRC522.h>

constexpr uint8_t RST_PIN = 17;  // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 5;    // Configurable, see typical pin layout above
int currentBalance = 0;
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
char c;

void setup() {
  Serial.begin(9600);  // Initialize serial communications with the PC
  SPI.begin();         // Init SPI bus
  mfrc522.PCD_Init();  // Init MFRC522 card
}

void loop() {
  // Keep the program in a continuous loop, waiting for the card to be swiped
  int newCard = Read();  // Attempt to read the card

  if (newCard != 0) {
    currentBalance += 100;  // Increment the balance by 100
    Write(currentBalance);  // Write the new balance to the card
    Serial.print("New balance: ");
    Serial.println(currentBalance);
  }
}

void Write(int balance) {
  // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;

  // Look for new cards
  while (!mfrc522.PICC_IsNewCardPresent()) {
    //return;
  }

  // Select one of the cards
  while (!mfrc522.PICC_ReadCardSerial()) {
    //return;
  }

  Serial.print(F("Card UID:"));  //Dump UID
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
  }
  Serial.print(F(" PICC type: "));  // Dump PICC type
  MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  Serial.println(mfrc522.PICC_GetTypeName(piccType));

  byte buffer[34];
  byte block = 1;

  sprintf((char *)buffer, "%d", balance);
  Serial.println(balance);

  MFRC522::StatusCode status;

  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("PCD_Authenticate() failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  } else {
    Serial.println(F("PCD_Authenticate() success: "));
  }

  // Write block
  status = mfrc522.MIFARE_Write(block, buffer, 16);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("MIFARE_Write() failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  } else {
    Serial.println(F("MIFARE_Write() success: "));
  }

  Serial.println(" ");
  mfrc522.PICC_HaltA();       // Halt PICC
  mfrc522.PCD_StopCrypto1();  // Stop encryption on PCD
}

int Read() {
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;

  //some variables we need
  byte block;
  byte len;
  MFRC522::StatusCode status;

  //-------------------------------------------

  // Look for new cards
  while (!mfrc522.PICC_IsNewCardPresent()) {
    //return;
  }

  // Select one of the cards
  while (!mfrc522.PICC_ReadCardSerial()) {
    //return;
  }

  Serial.println(F("**Card Detected:**"));

  //-------------------------------------------

  mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid));  //dump some details about the card

  //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));      //uncomment this to see all blocks in hex
  len = 18;
  //---------------------------------------- GET LAST NAME

  byte buffer2[18];
  block = 1;

  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid));  //line 834
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return currentBalance;
  }

  status = mfrc522.MIFARE_Read(block, buffer2, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Reading failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return currentBalance;
  }

  String text = "";
  for (uint8_t i = 0; i < 16; i++) {
    text += buffer2[i];
  }

  Serial.println(text);

  int currentBalance = text.toInt();
  currentBalance = 1;


  //----------------------------------------

  Serial.println(F("\n**End Reading**\n"));

  delay(1000);  //change value if you want to read cards faster

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
  return currentBalance;
}

我尝试创建新的函数、变量和其他东西,但不幸的是,我做不到。我使用了 AI 和几个论坛,但它们没有产生结果,所以我回到了我的基本代码。

C++ ESP32 Arduino-uno RFID

评论

0赞 Gerhardh 11/20/2023
如果一个答案解决了你的问题,你可能会考虑接受它和/或升级它。如果不完整,您可以询问更多详细信息。

答:

0赞 Gerhardh 11/14/2023 #1

你的变量是一团糟。

int currentBalance = 0;  // << global variable
...
void loop() {
  // Keep the program in a continuous loop, waiting for the card to be swiped
  int newCard = Read();  // Attempt to read the card

  if (newCard != 0) {
    currentBalance += 100;  // Increment the balance by 100
...
  }
}

在这里,您将从卡中读取的余额签入,但不使用它。相反,您始终递增全局变量。无论您从卡片上读到什么,这里都会丢失。 尝试newCard

    currentBalance = newCard + 100;  // Increment the balance by 100

相反。

在您的函数中,您完全迷失了方向:Read

int Read() {
...
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return currentBalance;
  }

如果出现错误,则返回全局变量的当前值。 在您的循环中,它被解释为一个新值,余额将递增。 我不确定这是否是故意的。 我认为错误意味着您无法增加任何东西。 你应该回来。0

...
// If there is no error found above, we get here:
  int currentBalance = text.toInt();
  currentBalance = 1;  // Discard value from the card.
...
  return currentBalance;
}

这将创建一个新的局部变量,该变量隐藏全局变量。您在此处分配的任何内容都不会最终出现在全局变量中。 此外,您从卡中读取的任何内容也会丢失,因为您立即用 .删除该行。1

混合使用全局变量和具有相同名称的局部变量是非常令人困惑的,永远不应该这样做。