//讀寫扇區的例子://全部扇區數據塊可單獨讀寫,也可全部扇區同時讀寫,可以復制全部扇區的內容,也可以重新賦值全部扇區的內容
#include <SPI.h>
#include <RFID.h>
RFID rfid(53,49); //D53--讀卡器MOSI引腳、D49--讀卡器RST引腳//2560板子
#define qh 43//功能切換讀取
#define d 44//LED燈,滅代表讀狀態,亮代表寫狀態
char b=0;
//4字節卡序列號,第5字節為校驗字節
unsigned char serNum[5];
//寫卡數據,45為數據塊數量,16為長度
//數據塊是1-15扇區相加,每個扇區3個數據塊故3*15=45;0號扇區固定不能改
unsigned char writeDate[45][16] ={
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1','1', '1', '1', '1'},
{'1', '2', '2','2', '2', '2','2', '2', '2','2', '2', '2','2', '2', '2', '2'},
{'1', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'2', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'2', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'2', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'3', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'3', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'3', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'4', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'4', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'4', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'5', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'5', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'5', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'6', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'6', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'6', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'7', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'7', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'7', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'8', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'8', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'8', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'9', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'9', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'9', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
};
unsigned char fz[45][16];
//原扇區A密碼,16個扇區,每個扇區密碼6Byte
unsigned char sectorKeyA[16][16] = {
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},//0~16扇區原密碼
//{0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
};
//新扇區A密碼,16個扇區,每個扇區密碼6Byte,一行代表一個扇區
//改密碼只要改前面6個ff任意一個, 0xff,0x07,0x80,0x69, 千萬不能動否則可能報廢扇區
unsigned char sectorNewKeyA[16][16] = {
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},//1-16扇區新密碼
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
};
//char aa[16];
void setup()
{
Serial.begin(9600);
SPI.begin();
rfid.init();
pinMode(qh, INPUT);
digitalWrite(qh, 1);
pinMode(d, OUTPUT);
}
void loop()
{
int j;
char tmp;
unsigned char status;
unsigned char str[MAX_LEN];
unsigned char blockAddr; //選擇操作的塊地址0~63。但只能寫4的倍數+3;比如7,11,15,19---
if(digitalRead(qh)==0) //功能按鍵操作
{
b=b^1;
digitalWrite(d, b);
delay(30);
while(!(digitalRead(qh)));
delay(30);
}
//找卡
rfid.isCard();
//讀取卡序列號
if (rfid.readCardSerial())
{
Serial.print("The card's number is : ");
Serial.print(rfid.serNum[0],HEX);
Serial.print(rfid.serNum[1],HEX);
Serial.print(rfid.serNum[2],HEX);
Serial.print(rfid.serNum[3],HEX);
Serial.print(rfid.serNum[4],HEX);
Serial.println(" ");
}
//選卡,返回卡容量(鎖定卡片,防止多次讀寫)
rfid.selectTag(rfid.serNum);
//復制數據卡
if(b==1)
{
for(int ii= 7;ii<64;ii=ii+4)
{
for(int i=ii-3;i<ii;i++)//循環寫入數據塊012,不循環就單獨寫i值
{
blockAddr = ii;
if (rfid.auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[ blockAddr/4], rfid.serNum) == MI_OK) //認證
{
// 寫密碼
status = rfid.write(blockAddr, sectorNewKeyA[blockAddr/4]);
//寫數據
blockAddr =i; //數據塊i
j=i-i/4-3;
// status = rfid.write(blockAddr, writeDate[j]);//如果使用賦值功能則打開此行
status = rfid.write(blockAddr, fz[j]);//如果使用復制功能則打開此行,與下面的
if(status == MI_OK)
{
Serial.print("扇區:");
Serial.print(i/4);
Serial.print("數據塊:");
Serial.println(i);
//Serial.println(j);
Serial.println("Write card OK!");
}
}
}
}
}
else
{
//讀卡
for(int ii= 7;ii<64;ii=ii+4)
{
for(int i=ii-3;i<ii;i++)
{
blockAddr = ii;
status = rfid.auth(PICC_AUTHENT1A, blockAddr, sectorNewKeyA[blockAddr/4], rfid.serNum);
if (status == MI_OK) //認證
{
//讀數據
blockAddr =i; //數據塊i
j=i-i/4-3;
/*********如果使用復制卡里的數據則打開,否則關閉,與上面的status = rfid.write(blockAddr, fz[j])同時使用***************/
if( rfid.read(blockAddr, str) == MI_OK)
{
for(int z=0;z<16;z++)//復制扇區數據塊
{
fz[j][z]=str[z];
}
/************************/
// Serial.println((char*)fz[i]);//打印復制的值,可省去
Serial.print("扇區:");
Serial.print(blockAddr/4);
Serial.print("數據塊:");
Serial.print(i);
Serial.print(":");
Serial.print("the data is : ");
Serial.println((char *)str);
}
}
}
}
}
rfid.halt();//去掉本行連續讀寫
}
|