如何让余额保持存储在RFID 522中?

问题描述 投票:0回答:1

我有一个问题。我有一个 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;
}

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

c++ arduino esp32 arduino-uno rfid
1个回答
0
投票

你的变量一团糟。

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
覆盖该值。删除该行。

混合使用同名的全局变量和局部变量是非常混乱的,绝对不应该这样做。

© www.soinside.com 2019 - 2024. All rights reserved.