電梯卡之複製(寫卡篇)
發表於 : 2025年 3月 22日, 15:20
續上篇電梯卡之複製(讀卡篇),今天我要來將所紀錄的NUID卡號寫入新卡中,
完成複製。
首先,多準備一張CUID卡

然後利用以下程式寫入新的卡片中。
記得要先將程式中的NUID卡號改成自己要複製的卡片的卡號。
所需材料:ESP32 *1、RFID-RC522 *1、CUID卡 *1、杜邦線 *少許
電路圖:

程式列表:
完成複製。
首先,多準備一張CUID卡

然後利用以下程式寫入新的卡片中。
記得要先將程式中的NUID卡號改成自己要複製的卡片的卡號。
所需材料:ESP32 *1、RFID-RC522 *1、CUID卡 *1、杜邦線 *少許
電路圖:

程式列表:
代碼: 選擇全部
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 14 // Configurable, see typical pin layout above
#define SS_PIN 21 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
/* 你所紀錄的卡片寫在這裡 */
#define NEW_UID {0xDE, 0xAD, 0xBE, 0xEF}
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("警告: 這程式將會覆蓋你原有的卡號,請小心使用!"));
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. And if present, select one.
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
delay(50);
return;
}
// Now a card is selected. The UID and SAK is in mfrc522.uid.
// Dump UID
Serial.print(F("卡片 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.println();
// Set new UID
byte newUid[] = NEW_UID;
if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
Serial.println(F("新卡號已經寫入新卡中了."));
}
// Halt PICC and re-select it so DumpToSerial doesn't get confused
mfrc522.PICC_HaltA();
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
return;
}
// Dump the new memory contents
Serial.println(F("新卡號及內容:"));
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
delay(2000);
}