#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println("Place your card on the reader and open the Serial Monitor.");
}
void loop() {
// Look for new cards
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
// Show some details of the PICC (that is: the tag/card)
Serial.print("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.println();
Serial.println("Enter data to write (16 characters max) and press Enter:");
while (true) {
Serial.println("等待输入");
// Allow user to type 'exit' to stop writing
if (Serial.available()) {
char cmd = char(Serial.read());
if (cmd == 'w') {
writeblack();
} else {
reblack();
}
}
// Allow user to type 'exit' to stop writing
if (Serial.available()) {
String command = Serial.readString();
command.trim();
if (command.equals("exit")) {
Serial.println("Remove the card.");
while (mfrc522.PICC_IsNewCardPresent() || mfrc522.PICC_ReadCardSerial()) {
delay(500); // Wait until the card is removed
}
Serial.println("Place a new card on the reader.");
break;
} else if (command.equals("red")) {
//读取模块
}
}
delay(1000);
}
}
void writeblack() {
if (Serial.available()) {
String inputData = Serial.readString();
// Ensure the input data is not longer than 16 characters
if (inputData.length() > 16) {
inputData = inputData.substring(0, 16);
}
byte dataBlock[16];
inputData.getBytes(dataBlock, 17); // 16 chars + null terminator
byte sector = 1; // Sector to write to
byte blockAddr = 4; // Block address within sector
byte trailerBlock = sector * 4 + 3;
MFRC522::StatusCode status;
Serial.print(F("Authenticating using key A..."));
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
Serial.println(F("Writing data to block 4..."));
status = mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
Serial.println(F("Data was written successfully. Enter another data or type 'exit' to finish:"));
}
}
void reblack() {
MFRC522::StatusCode status;
byte buffer[18];
byte size = sizeof(buffer);
byte sector = 1; // Sector to write to
byte blockAddr = 4; // Block address within sector
byte trailerBlock = sector * 4 + 3;
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
// Authenticate using key A 使用密钥A进行授权
Serial.println(F("Authenticating using key A..."));
status = (MFRC522::StatusCode)mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = (MFRC522::StatusCode)mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("读取数据块失败: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return; //读取卡片失败,退出
}
Serial.print(F("Data in block "));
Serial.print(blockAddr);
Serial.print(F(":"));
printHex(buffer, 16);
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void printHex(byte* buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? "0" : "");
Serial.print(buffer[i], HEX);
Serial.print(" ");
}
}
本文链接:https://blog.nnwk.net/article/1549
有问题请留言。版权所有,转载请在显眼位置处保留文章出处,并留下原文连接
Leave your question and I'll get back to you as soon as I see it. All rights reserved. Please keep the source and links
友情链接:
子卿全栈
全部评论