//读写扇区的例子:
#include <SPI.h>
#include <RFID.h>
RFID rfid(53,49); //D10--读卡器MOSI引脚、D5--读卡器RST引脚
//4字节卡序列号,第5字节为校验字节
unsigned char serNum[5];
//写卡数据,3为数据块012,16为长度
unsigned char writeDate[3][16] ={
{'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},
{'1', '1', '1', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
};
//原扇区A密码,16个扇区,每个扇区密码6Byte
unsigned char sectorKeyA[16][16] = {
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},//0~16扇区原密码
{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();
}
void loop()
{
//unsigned char i,tmp;//i为数据块号0.1.2
unsigned tmp;
unsigned char status;
unsigned char str[MAX_LEN];
// unsigned char RC_size;
unsigned char blockAddr; //选择操作的块地址0~63。但只能写4的倍数+3;比如7,11,15,19---
//找卡
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);
//写数据卡
// for(int i=0;i<3;i++)//循环写入数据块012,不循环就单独写i值
// {
// blockAddr = 15; //扇区3,控制块3
// if (rfid.auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[ blockAddr/4], rfid.serNum) == MI_OK) //认证
// {
//// 写密码
// status = rfid.write(blockAddr, sectorNewKeyA[blockAddr/4]);
// //写数据
//
// blockAddr = blockAddr - 3+i; //数据块i
// tmp=(blockAddr+3-i)/4;
// status = rfid.write(blockAddr, writeDate[i]);
// if(status == MI_OK)
// {
//
// Serial.print("扇区:");
// Serial.print(tmp,DEC);
// Serial.print("数据块:");
// Serial.println(i);
// Serial.println("Write card OK!");
// }
// }
// }
//读卡
for(int i=2;i<3;i++)
{
blockAddr = 15; ////扇区3,控制块3
status = rfid.auth(PICC_AUTHENT1A, blockAddr, sectorNewKeyA[blockAddr/4], rfid.serNum);
if (status == MI_OK) //认证
{
//读数据
blockAddr = blockAddr - 3+i;
if( rfid.read(blockAddr, str) == MI_OK)
{
Serial.print("Read from the card ,the data is : ");
Serial.println((char *)str);
Serial.print("扇区:");
Serial.print(blockAddr/4,DEC);
Serial.print("数据块:");
Serial.println(i);
}
}
}
rfid.halt();//去掉本行连续读写
}
//验证密码+数据后,再对arduino设定的sj进行减5操作
#include <SPI.h>
#include <RFID.h>
RFID rfid(53,49); //D10--读卡器MOSI引脚、D5--读卡器RST引脚
//4字节卡序列号,第5字节为校验字节
unsigned char serNum[5];
//写卡数据
unsigned char writeDate[16]={'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 0, 0};
unsigned char aa[16]= {'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 0, 0};
char dy;
unsigned int sj=1000;
//原扇区A密码,16个扇区,每个扇区密码6Byte
unsigned char sectorKeyA[16][16] = {
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
// {0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
// {0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
// {0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
};
//新扇区A密码,16个扇区,每个扇区密码6Byte
//改密码只要改第3行前面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},//读改密码只要改前面6个ff任意一个
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},//写改密码只要改前面6个ff任意一个
};
void setup()
{
Serial.begin(9600);
SPI.begin();
rfid.init();
}
void loop()
{
unsigned char i,tmp;
unsigned char status;
unsigned char str[MAX_LEN];
unsigned char RC_size;
unsigned char blockAddr; //选择操作的块地址0~63
//找卡
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);
//写数据卡
blockAddr = 7; //数据块7//报废扇区2,1,16
if (rfid.auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[ blockAddr/4], rfid.serNum) == MI_OK) //认证
{
// 写数据
status = rfid.write(blockAddr, sectorNewKeyA[blockAddr/4]);
Serial.print("set the new card password, and can modify the data of the Sector: ");
Serial.println(blockAddr/4,DEC);
//写数据
blockAddr = blockAddr - 3 ; //数据块4
status = rfid.write(blockAddr, writeDate);
if(status == MI_OK)
{
Serial.println("Write card OK!");
}
}
//读卡
blockAddr = 7;
status = rfid.auth(PICC_AUTHENT1A, blockAddr, sectorNewKeyA[blockAddr/4], rfid.serNum);
if (status == MI_OK) //认证
{
//读数据
blockAddr = blockAddr - 3 ;
if( rfid.read(blockAddr, str) == MI_OK)
{
Serial.print("Read from the card ,the data is : ");
Serial.println((char *)str);
for(int a=0;a<15;a++)
{
dy=1;
if(aa[a]!=str[a])
{
dy=0;
break;
}
}
if(dy==1)
{
sj-=5;
Serial.println(sj);
}
}
}
rfid.halt();
}
|