/**
-
- This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
- for further details and other examples.
- NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
- Released into the public domain.
-
- This sample shows how to read and write data blocks on a MIFARE Classic PICC
- (= card/tag).
- BEWARE: Data will be written to the PICC, in sector #1 (blocks #4 to #7).
- Typical pin layout used:
-
-
MFRC522 Arduino Arduino Arduino Arduino Arduino
-
Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
- Signal Pin Pin Pin Pin Pin Pin
-
- RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
- SPI SS SDA(SS) 10 53 D10 10 10
- SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
- SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
- SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
/**
-
Initialize.
*/
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 cardkey.keyByte[0] =0x20;
key.keyByte[1] =0x15;
key.keyByte[2] =0x12;
key.keyByte[3] =0x02;
key.keyByte[4] =0x36;
key.keyByte[5] =0x00;
Serial.println(F("扫描Mifare Classic PICC以演示读写。"));
Serial.print(F("使用AB密码进行读写:"));
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE); //将key.keyByte,打印出来
Serial.println();
Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1")); //当心:数据将写入区块
}
/**
-
Main loop.
*/
void loop() {
// 如果传感器/读卡器上没有新卡,则重置环路。这可以在空闲时保存整个进程。
if ( ! mfrc522.PICC_IsNewCardPresent())
return;// 选择其中一张卡
if ( ! mfrc522.PICC_ReadCardSerial())
return;// 显示picc的一些详细信息(即:标签/卡)
Serial.print(F(“Card UID:”));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));// 检查兼容性
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F(“此示例仅适用于Mifare经典卡。”));
return;
}// I我们使用的样本在本/第二部门
// 即:扇区1,覆盖区块4至区块7
byte sector = 1;
byte blockAddr = 4;
byte blockAddr2 = 5;
byte dataBlock1[] = {
0x01, 0x02, 0x03, 0x04, // 1, 2, 3, 4,
0x05, 0x06, 0x07, 0x08, // 5, 6, 7, 8,
0x09, 0x0a, 0xff, 0x0b, // 9, 10, 255, 11,
0x0c, 0x0d, 0x0e, 0x0f // 12, 13, 14, 15
};byte trailerBlock = 7;
MFRC522::StatusCode status;
byte buffer[18];
byte size = sizeof(buffer);// Authenticate using key A
Serial.println(F(“使用密钥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;
}// 显示当前的整个扇区
Serial.println(F(“扇区中的当前数据:”));
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println();// 从块中读取数据
Serial.print(F(“从块读取数据”)); Serial.print(blockAddr);
Serial.println(F(" …"));
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.print(F(“数据块 “)); Serial.print(blockAddr); Serial.println(F(”:”));
dump_byte_array(buffer, 16); Serial.println();
Serial.println();
Serial.print(F(“数据块 “)); Serial.print(blockAddr2); Serial.println(F(”:”));
dump_byte_array(buffer, 16); Serial.println();
Serial.println();// Authenticate using key B
Serial.println(F(“使用密钥B再次验证…”));
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() 失败:: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}// 将数据写入块
Serial.print(F(“将数据写入块 “)); Serial.print(blockAddr);
Serial.println(F(” …”));
dump_byte_array(dataBlock1, 16); Serial.println(); //第4块数据内容正式写入status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, dataBlock1, 16);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr2, dataBlock1, 16);if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.println();//从块中读取数据(同样,现在应该是我们所写的内容)
Serial.print(F(“从块中读取数据 “)); Serial.print(blockAddr);
Serial.println(F(” …”));
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.print(F(“Data in block “)); Serial.print(blockAddr); Serial.println(F(”:”));
dump_byte_array(buffer, 16); Serial.println();
//检查块中的数据是否为我们编写的数据
//通过计算相等的字节数
Serial.println(F(“检验结果…”));
byte count = 0;
for (byte i = 0; i < 16; i++) {
// Compare buffer (= what we’ve read) with dataBlock (= what we’ve written)
if (buffer[i] == dataBlock1[i])
count++;
}
Serial.print(F(“匹配的字节数 = “)); Serial.println(count);
if (count == 16) {
Serial.println(F(“成功 ?”));
} else {
Serial.println(F(“Failure, no match ?”));
Serial.println(F(” perhaps the write didn’t work properly…”));
}
Serial.println();
// Dump the sector data
Serial.println(F("Current data in sector:"));
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println();
// 停顿 PICC
mfrc522.PICC_HaltA();
// 停止加密
mfrc522.PCD_StopCrypto1();
Serial.println(“打印buffer数据”);
for (byte i = 0; i < 16; i++) {
// Compare buffer (= what we've read) with dataBlock (= what we've written)
Serial. print(buffer[i],HEX);
}
}
/**
- Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
完整程序
/**
* ----------------------------------------------------------------------------
* This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
* for further details and other examples.
*
* NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
*
* Released into the public domain.
* ----------------------------------------------------------------------------
* This sample shows how to read and write data blocks on a MIFARE Classic PICC
* (= card/tag).
*
* BEWARE: Data will be written to the PICC, in sector #1 (blocks #4 to #7).
*
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
/**
* Initialize.
*/
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
key.keyByte[0] =0x20;
key.keyByte[1] =0x15;
key.keyByte[2] =0x12;
key.keyByte[3] =0x02;
key.keyByte[4] =0x36;
key.keyByte[5] =0x00;
Serial.println(F("扫描Mifare Classic PICC以演示读写。"));
Serial.print(F("使用AB密码进行读写:"));
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE); //将key.keyByte,打印出来
Serial.println();
Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1")); //当心:数据将写入区块
//以下为指示灯操作(工作状态的指示灯)
}
/**
* Main loop.
*/
void loop() {
// 如果传感器/读卡器上没有新卡,则重置环路。这可以在空闲时保存整个进程。
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// 选择其中一张卡
if ( ! mfrc522.PICC_ReadCardSerial())
return;
// 显示picc的一些详细信息(即:标签/卡)
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
// 检查兼容性
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("此示例仅适用于Mifare经典卡。"));
return;
}
// I我们使用的样本在本/第二部门
// 即:扇区1,覆盖区块4至区块7
byte sector = 1;
byte blockAddr = 4;
byte blockAddr2 = 5;
byte dataBlock1[] = {
0x01, 0x02, 0x03, 0x04, // 1, 2, 3, 4,
0x05, 0x06, 0x07, 0x08, // 5, 6, 7, 8,
0x09, 0x0a, 0xff, 0x0b, // 9, 10, 255, 11,
0x0c, 0x0d, 0x0e, 0x0f // 12, 13, 14, 15
};
byte trailerBlock = 7;
MFRC522::StatusCode status;
byte buffer[18];
byte size = sizeof(buffer);
// Authenticate using key A
Serial.println(F("使用密钥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;
}
// 显示当前的整个扇区
Serial.println(F("扇区中的当前数据:"));
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println();
// 从块中读取数据
Serial.print(F("从块读取数据")); Serial.print(blockAddr);
Serial.println(F(" ..."));
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.print(F("数据块 ")); Serial.print(blockAddr); Serial.println(F(":"));
dump_byte_array(buffer, 16); Serial.println();
Serial.println();
Serial.print(F("数据块 ")); Serial.print(blockAddr2); Serial.println(F(":"));
dump_byte_array(buffer, 16); Serial.println();
Serial.println();
// Authenticate using key B
Serial.println(F("使用密钥B再次验证..."));
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() 失败:: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// 将数据写入块
Serial.print(F("将数据写入块 ")); Serial.print(blockAddr);
Serial.println(F(" ..."));
dump_byte_array(dataBlock1, 16); Serial.println(); //第4块数据内容正式写入
status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, dataBlock1, 16);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr2, dataBlock1, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.println();
//从块中读取数据(同样,现在应该是我们所写的内容)
Serial.print(F("从块中读取数据 ")); Serial.print(blockAddr);
Serial.println(F(" ..."));
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
dump_byte_array(buffer, 16); Serial.println();
//检查块中的数据是否为我们编写的数据
//通过计算相等的字节数
Serial.println(F("检验结果..."));
byte count = 0;
for (byte i = 0; i < 16; i++) {
// Compare buffer (= what we've read) with dataBlock (= what we've written)
if (buffer[i] == dataBlock1[i])
count++;
}
Serial.print(F("匹配的字节数 = ")); Serial.println(count);
if (count == 16) {
Serial.println(F("成功 :-)"));
} else {
Serial.println(F("Failure, no match :-("));
Serial.println(F(" perhaps the write didn't work properly..."));
}
Serial.println();
// Dump the sector data
Serial.println(F("Current data in sector:"));
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println();
// 停顿 PICC
mfrc522.PICC_HaltA();
// 停止加密
mfrc522.PCD_StopCrypto1();
Serial.println("打印buffer数据");
for (byte i = 0; i < 16; i++) {
// Compare buffer (= what we've read) with dataBlock (= what we've written)
Serial. print(buffer[i],HEX);
}
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
```c++