【问题标题】:Arduino RC 522 write and read dataArduino RC 522 读写数据
【发布时间】:2021-08-02 17:00:33
【问题描述】:

我正在开发 Arduino RC 522 RFID 模块。我想在 RFID 卡中写入一些数据并读取该数据以操作继电器。那有可能吗?我想在 RFID CARD(13.5Mhz) 中存储整数数据。

【问题讨论】:

    标签: arduino rfid mfrc522


    【解决方案1】:

    为了写卡,您可以尝试使用此代码,将变量“blockData”更改为整数,长度应为 16 个字节,否则您必须更改数组和循环的尺寸。

    #include <SPI.h>
    #include <MFRC522.h>
    
    /*Using Hardware SPI of Arduino */
    /*MOSI (11), MISO (12) and SCK (13) are fixed */
    /*You can configure SS and RST Pins*/
    #define SS_PIN 10  /* Slave Select Pin */
    #define RST_PIN 7  /* Reset Pin */
    
    /* Create an instance of MFRC522 */
    MFRC522 mfrc522(SS_PIN, RST_PIN);
    /* Create an instance of MIFARE_Key */
    MFRC522::MIFARE_Key key;          
    
    /* Set the block to which we want to write data */
    /* Be aware of Sector Trailer Blocks */
    int blockNum = 2;  
    /* Create an array of 16 Bytes and fill it with data */
    /* This is the actual data which is going to be written into the card */
    byte blockData [16] = {"Electronics-Hub-"};
    
    /* Create another array to read data from Block */
    /* Legthn of buffer should be 2 Bytes more than the size of Block (16 Bytes) */
    byte bufferLen = 18;
    byte readBlockData[18];
    
    MFRC522::StatusCode status;
    
    void setup() 
    {
      /* Initialize serial communications with the PC */
      Serial.begin(9600);
      /* Initialize SPI bus */
      SPI.begin();
      /* Initialize MFRC522 Module */
      mfrc522.PCD_Init();
      Serial.println("Scan a MIFARE 1K Tag to write data...");
    }
    
    void loop()
    {
      /* Prepare the ksy for authentication */
      /* All keys are set to FFFFFFFFFFFFh at chip delivery from the factory */
      for (byte i = 0; i < 6; i++)
      {
        key.keyByte[i] = 0xFF;
      }
      /* Look for new cards */
      /* Reset the loop if no new card is present on RC522 Reader */
      if ( ! mfrc522.PICC_IsNewCardPresent())
      {
        return;
      }
      
      /* Select one of the cards */
      if ( ! mfrc522.PICC_ReadCardSerial()) 
      {
        return;
      }
      Serial.print("\n");
      Serial.println("**Card Detected**");
      /* Print UID of the Card */
      Serial.print(F("Card 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("\n");
      /* Print type of card (for example, MIFARE 1K) */
      Serial.print(F("PICC type: "));
      MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
      Serial.println(mfrc522.PICC_GetTypeName(piccType));
             
       /* Call 'WriteDataToBlock' function, which will write data to the block */
       Serial.print("\n");
       Serial.println("Writing to Data Block...");
       WriteDataToBlock(blockNum, blockData);
       
       /* Read data from the same block */
       Serial.print("\n");
       Serial.println("Reading from Data Block...");
       ReadDataFromBlock(blockNum, readBlockData);
       /* If you want to print the full memory dump, uncomment the next line */
       //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
       
       /* Print the data read from block */
       Serial.print("\n");
       Serial.print("Data in Block:");
       Serial.print(blockNum);
       Serial.print(" --> ");
       for (int j=0 ; j<16 ; j++)
       {
         Serial.write(readBlockData[j]);
       }
       Serial.print("\n");
    }
    
    
    
    void WriteDataToBlock(int blockNum, byte blockData[]) 
    {
      /* Authenticating the desired data block for write access using Key A */
      status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
      if (status != MFRC522::STATUS_OK)
      {
        Serial.print("Authentication failed for Write: ");
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
      }
      else
      {
        Serial.println("Authentication success");
      }
    
      
      /* Write data to the block */
      status = mfrc522.MIFARE_Write(blockNum, blockData, 16);
      if (status != MFRC522::STATUS_OK)
      {
        Serial.print("Writing to Block failed: ");
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
      }
      else
      {
        Serial.println("Data was written into Block successfully");
      }
      
    }
    
    void ReadDataFromBlock(int blockNum, byte readBlockData[]) 
    {
      /* Authenticating the desired data block for Read access using Key A */
      byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
    
      if (status != MFRC522::STATUS_OK)
      {
         Serial.print("Authentication failed for Read: ");
         Serial.println(mfrc522.GetStatusCodeName(status));
         return;
      }
      else
      {
        Serial.println("Authentication success");
      }
    
      /* Reading data from the Block */
      status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
      if (status != MFRC522::STATUS_OK)
      {
        Serial.print("Reading failed: ");
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
      }
      else
      {
        Serial.println("Block was read successfully");  
      }
      
    }
    

    使用此代码读取数据,将“mioCod”更改为卡的 UID

    void ceckRFID(){
      //check new cards
      if (!mfrc522.PICC_IsNewCardPresent())
      {
        return;
      }
      //If read UID
      if (!mfrc522.PICC_ReadCardSerial())
      {
        return;
      }
      
      String codLetto = ""; //clean the reading string
      
      for (byte i = 0; i < mfrc522.uid.size; i++) { //save the code into the string
        codLetto += String(mfrc522.uid.uidByte[i], HEX);
      }
      
      codLetto.toUpperCase(); //uppercase the string
      Serial.println(codLetto); //print the code
    
      if(codLetto == mioCod){ //if the code match with mioCod 
          Serial.println("Code recognized");
      }
      else{
        Serial.println("Wrong code");
      }
    }
    

    【讨论】:

    • 嗨,兄弟。我已经测试了写入和读取数据代码。它工作正常。但我面临的问题是,通过使用写入数据和读取数据代码,我可以检查存储在卡中的数据,这是一个数组,并且这些数据是使用“Serial.write()”打印的。转换或存储此序列号的任何可能性也是如此。将数据写入变量。所以我可以操作继电器。我不想在代码中使用 RFID 卡 UID。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多