目录
回到开头提到的两个问题:第一种情况读不全数据,// Try the mpages of the original Ultralight. Ultralight C has more pages.
第二种情况:直接无法读出数据,有时候甚至是一开始读得出,后来突然又读不出了。
一、前言
本文作为上一篇PN532 NFC Controler 的后续将围绕RC522展开讲述。与之前提到的PN532模块不同,RC522的驱动过程没有那么顺利。在读取UID的时候没问题,但是当我读取NFC内部的内存的时候遇到了两个问题:一个是无法全部读取;另一个情况是直接就读取失败。
如果感兴趣的话可以查看:PN532 NFC Controller串口通信 + Arduino IDE_pn532 串口测试-CSDN博客
二、项目概要
硬件说明:RC522模块 + ESP32S3
引脚名称 | 功能 |
---|---|
3.3V | 电源正 |
RST | 复位引脚,高电平有效 |
GND | 地,电源负 |
IRQ | 中断引脚,悬空不使用 |
MISO | SPI协议数据线 |
MOSI | SPI协议数据线 |
SCK | SPI时钟线 |
SDA | SPI片选端口 |
软件说明:Arduino IDE + NFC Tools Pro
NFC Tools Pro : 用于读取NFC内部的数据信息,主要是读取内存。
使用的库:miguelbalboa/rfid: Arduino RFID Library for MFRC522 (github.com)
三、实验测试
(一)读取uid
这一步基本上是拿到模块进行的第一个测试,我的测试是顺利的,Mifare Classic 和 Mifare Ultralight 两种卡都能完全读取里面的内存。
这个实验就不放全部的代码了。直接在库文件的代码里面找就有。
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));
/**
* 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);
}
}
(二)读取内存与写入内存
回到开头提到的两个问题:第一种情况读不全数据,// Try the mpages of the original Ultralight. Ultralight C has more pages.
这是因为在库文件的源代码里需要修改:把dump时page变量范围调大就行了(具体数值需要根据你自己手中的 NFC TOOL 读取内存得到的blocks数目来决定) 原本page是< 16 ,现在改成了 43 就可以把我手中的NFC内存全部读出来了。
第二种情况:直接无法读出数据,有时候甚至是一开始读得出,后来突然又读不出了。
这种情况下有一个简单的判断方法:你使用手机上的NFC工具把读不出数据的NFC卡格式化一下,再重新用RC522读取一下,看看是否恢复了。如果又可以了,那么就说明只是在读取过程中的验证密码环节出了问题。
原因探究:具体来讲:读取nfc就是 连接、寻卡、验证密码、读取这几个步骤。
展开来说,验证密码:选定要处理的卡片之后,读写器就确定要访问的扇区号,并对该扇区密码进行密码校验,在三次相互认证之后就可以通过加密流进行通讯。(在选择另一扇区时,则必须进行另一扇区密码校验。)
默认情况下,一开始NFC卡的所有block的Key都是0xFF FF FF FF FF FF,但是如果里面的数据发生更改写入,Key就会发生改变,而且有大概率同时存在两种Key。
可以看到下面代码的knowenKeys那一栏,是已知的几种key。第二个和第四个我遇到的次数多一些。
//the Keys are used for communicate with
byte knownKeys[NR_KNOWN_KEYS][MFRC522::MF_KEY_SIZE] = {
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // FF FF FF FF FF FF = factory default
{0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // A0 A1 A2 A3 A4 A5
{0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, // B0 B1 B2 B3 B4 B5
{0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}, // 4D 3A 99 C3 51 DD
{0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}, // 1A 98 2C 7E 45 9A
{0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // D3 F7 D3 F7 D3 F7
{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, // AA BB CC DD EE FF
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 00 00 00 00 00 00
};
弄清楚原因之后就简单了,只需要在遇到当前KEY无法正常读取的情况时,可以更换KEY再试着读取就行了,简单粗暴。但如果上述的几组key都没办法读出来的话,就说明,,,该直接询问厂家了。
最终完整测试代码如下:(注意MFRC522.h库需要修改后再使用,以免影响实验结果)
/*
* 2024.09.24 test successfully!
*
* Can use for read Mifare 1KB, also can use for Mifare Ultralight or ultralight C.
*
* 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.
// Number of known default keys (hard-coded)
// NOTE: Synchronize the NR_KNOWN_KEYS define with the defaultKeys[] array
#define NR_KNOWN_KEYS 8
//the Keys are used for communicate with
byte knownKeys[NR_KNOWN_KEYS][MFRC522::MF_KEY_SIZE] = {
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // FF FF FF FF FF FF = factory default
{0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // A0 A1 A2 A3 A4 A5
{0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, // B0 B1 B2 B3 B4 B5
{0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}, // 4D 3A 99 C3 51 DD
{0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}, // 1A 98 2C 7E 45 9A
{0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // D3 F7 D3 F7 D3 F7
{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, // AA BB CC DD EE FF
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 00 00 00 00 00 00
};
//the address and data of block which we want to write it
byte blockAddr = 14;
byte dataBlock[] = {
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 blockflag = 0;
void setup() {
Serial.begin(115200); // 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("Try the most used default keys to print blocks of Mifare Classic.");
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
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(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_UL)
{
//when the IC card is PICC_TYPE_MIFARE_UL, we would read now.
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
else
{
// Try the known default keys
MFRC522::MIFARE_Key key;
for (byte k = 0; k < NR_KNOWN_KEYS; k++)
{
// Copy the known key into the MIFARE_Key structure
for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++)
{
key.keyByte[i] = knownKeys[k][i];
}
// Try the key
if (try_key(&key))
{
break;
}
if ( ! mfrc522.PICC_IsNewCardPresent())break;
if ( ! mfrc522.PICC_ReadCardSerial())break;
}
}
}
bool try_key(MFRC522::MIFARE_Key *key)
{
bool result = false;
byte buffer[18];
byte block = 0;
MFRC522::StatusCode status;
for(block = blockflag ; block <= 63 ;block ++){
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));
blockflag = block;
return false;
break;
}
// Read block
byte byteCount = sizeof(buffer);
status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
else {
result = true;
// print out the last Key number.
/*
Serial.print(F("Success with key:"));
dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE);
Serial.println();
*/
// Dump block data
Serial.print(F("Block ")); Serial.print(block); Serial.print(F(":"));
dump_byte_array(buffer, 16);
Serial.println();
}
/* when the block number = blockAddr, will write something at the block space. */
if(block == blockAddr){
Serial.print(F("Writing data into block ")); Serial.print(blockAddr);
Serial.println(F(" ..."));
dump_byte_array(dataBlock, 16); Serial.println();
status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.println();
}
if(block == 63){
block = 0;
Serial.println("Read blocks successfully!");
}
}
mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
return result;
}
/*
* 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);
}
}
(三)内存数据分析
有了上述对NFC内存的读取写入作为基础,我就对数据所代表的具体信息做了一个小测试,对比结果如下:(以ASCII码的格式存储数据)
NFC不同类型芯片-写入数据后对应内存的不同
MifareClassic
MIFARE CLASSIC 1K 写入数据:abcde12345
17:46:09.745 -> Block 0: 23 44 16 2B 5A 08 04 00 62 63 64 65 66 67 68 69
17:46:09.811 -> Block 1: 14 01 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1
17:46:09.877 -> Block 2: 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1
17:46:09.945 -> Block 3: 00 00 00 00 00 00 78 77 88 C1 00 00 00 00 00 00
17:46:10.011 -> Block 4: 00 00 03 11 D1 01 0D 54 02 65 6E 61 62 63 64 65
17:46:10.073 -> Block 5: 31 32 33 34 35 FE 00 00 00 00 00 00 00 00 00 00
17:46:10.139 -> Block 6: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
17:46:10.170 -> Block 7: 00 00 00 00 00 00 7F 07 88 40 00 00 00 00 00 00
MIFARE 1K 写入数据:bcde12345
17:48:05.643 -> Block 0: 23 44 16 2B 5A 08 04 00 62 63 64 65 66 67 68 69
17:48:05.677 -> Block 1: 14 01 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1
17:48:05.744 -> Block 2: 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1
17:48:05.809 -> Block 3: 00 00 00 00 00 00 78 77 88 C1 00 00 00 00 00 00
17:48:05.877 -> Block 4: 00 00 03 10 D1 01 0C 54 02 65 6E 62 63 64 65 31
17:48:05.944 -> Block 5: 32 33 34 35 FE 00 00 00 00 00 00 00 00 00 00 00
17:48:05.977 -> Block 6: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
17:48:06.044 -> Block 7: 00 00 00 00 00 00 7F 07 88 40 00 00 00 00 00 00
MIFARE 1K 写入数据:cdefghijk12345
18:05:25.593 -> Block 0: 23 44 16 2B 5A 08 04 00 62 63 64 65 66 67 68 69
18:05:25.660 -> Block 1: 14 01 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1
18:05:25.725 -> Block 2: 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1 03 E1
18:05:25.759 -> Block 3: 00 00 00 00 00 00 78 77 88 C1 00 00 00 00 00 00
18:05:25.859 -> Block 4: 00 00 03 15 D1 01 11 54 02 65 6E 63 64 65 66 67
18:05:25.894 -> Block 5: 68 69 6A 6B 31 32 33 34 35 FE 00 00 00 00 00 00
MifareUltralight
NATG213 未写入数据
Page 0 1 2 3
0 04 3A BA 0C
1 A3 6B 26 81
2 6F 48 00 00
3 E1 10 12 00
4 01 03 A0 0C
5 34 03 00 FE
6 00 00 00 00
7 00 00 00 00
8 00 00 00 00
9 00 00 00 00
NATG213 写入数据:abcdefg125
Page 0 1 2 3
0 04 3A BA 0C
1 A3 6B 26 81
2 6F 48 00 00
3 E1 10 12 00
4 01 03 A0 0C
5 34 03 11 D1
6 01 0D 54 02
7 65 6E 61 62
8 63 64 65 66
9 67 31 32 35
10 FE 00 00 00
11 00 00 00 00
NATG213 写入数据:iiiifg125
Page 0 1 2 3
0 04 3A BA 0C
1 A3 6B 26 81
2 6F 48 00 00
3 E1 10 12 00
4 01 03 A0 0C
5 34 03 10 D1
6 01 0C 54 02
7 65 6E 69 69
8 69 69 66 67
9 31 32 35 FE
10 FE 00 00 00
对比上述数据很容易就可以发现规律。
四、总结
本文主要记录了我在调试RC522的过程中遇到的问题,以及相应的解决办法。
挖坑:后续会围绕NFC的应用来展开分享一些有趣的消防法,利用NFC自动打卡、获取时钟、恶作剧、辅助办公等等实用功能,估计会比较好玩。
如果您对我所介绍的内容有任何改进的建议也欢迎告诉我!如果本文对你有帮助的话,不妨点个赞。欢迎留言讨论问题,一起讨论问题、解决问题。
另外,本账号所有文章内容均为原创,转载请标明出处。
标签:03,00,NFC,Arduino,mfrc522,FF,Reader,E1,Block From: https://blog.csdn.net/m0_74001972/article/details/142533194