1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
| #include <SoftwareSerial.h>
/*****MiFare commands*****/
char LED_ON[] = {0xAA,0xBB,0x06,0x00,0x00,0x00,0x07,0x01,0x01,0x07}; //Command to turn on MiFare module onboard LED
char LED_OFF[] = {0xAA,0xBB,0x06,0x00,0x00,0x00,0x07,0x01,0x00,0x06}; //Command to turn off MiFare module onboard LED
char ANTENNA_ON[] = {0xAA,0xBB,0x06,0x00,0x00,0x00,0x0C,0x01,0x01,0x0C}; //Command to turn off MiFare module onboard antenna
char READ_DEV[] = {0xAA,0xBB,0x05,0x00,0x00,0x00,0x03,0x01,0x02}; //Command to read MiFare module device number
char CARD_REQ[] = {0xAA,0xBB,0x06,0x00,0x00,0x00,0x01,0x02,0x52,0x51}; //Command to request card type of MiFare card
char ANTI_COL[] = {0xAA,0xBB,0x05,0x00,0x00,0x00,0x02,02,00}; //Command for anti-collision
char CARD_SEL[] = {0xAA,0xBB,0x09,0x00,0x00,0x00,0x03,0x02,0x00,0x00,0x00,0x00,0x00}; //Command to select specific MiFare card for access
//Byte 0 to byte 12 of this command is initialized with 0x00, since have to obtain the NUID of the MiFare card scanned
//Byte 8 to byte 11 of this command will filled with NUID (started with the lowest byte) of the MiFare card desire to access
//Byte 12 of this command is checksum from byte 4 to byte 11 of this command may change for NUID different from that in this case
//This array will be filled during the subroutine of the get_nuid() in this program
//char AUTHEN_SEC0[] = {0xAA,0xBB,0x0D,0x00,0x00,0x00,0x07,0x02,0x60,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x65};
//char AUTHEN_SEC1[] = {0xAA,0xBB,0x0D,0x00,0x00,0x00,0x07,0x02,0x60,0x04,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x61};
// BYTE Key_A[]={0x4D,0x49,0x46,0x41,0x52,0x45}; //MIFARE 4D4946415245
// BYTE Key_B[]={0x45,0x52,0x41,0x46,0x49,0x4D}; //ERAFIM 45524146494D
char AUTHEN_SEC0[] = {0xAA,0xBB,0x0D,0x00,0x00,0x00,0x07,0x02,0x60,0x00,0x45,0x52,0x41,0x46,0x49,0x4D, 0x65}; //Command to authenticate Sector 0 in memory of MiFare card
char AUTHEN_SEC1[] = {0xAA,0xBB,0x0D,0x00,0x00,0x00,0x07,0x02,0x61,0x04,0x45,0x52,0x41,0x46,0x49,0x4D, 0x64}; //Command to authenticate Sector 1 in memory of MiFare card
char AUTHEN_SEC1_mod[] = {0xAA,0xBB,0x0D,0x00,0x00,0x00,0x07,0x02,0x60,0x04,0x4D,0x49,0x46,0x41,0x52,0x45}; //Command to authenticate Sector 1 in memory of MiFare card
char READ_0[] = {0xAA,0xBB,0x06,0x00,0x00,0x00,0x08,0x02,0x00,0x0A}; //Command to read Block 0 (manufacturer block in Sector 0) in memory of MiFare card
char WRITE_4[] = {0xAA,0xBB,0x16,0x00,0x00,0x00,0x09,0x02,0x04,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x0F}; //Command to write data into Block 4 (in Sector 1) in memory of MiFare card
//Data to be written (started with the lowest byte) is from byte 9 to 24,
//total of 16 bytes data
//Data is 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A
//0x0B,0x0C,0x0D,0x0E,0x0F in this case
//The last checksum byte of this command may change for data to written different from
//that in this case
char READ_4[] = {0xAA,0xBB,0x06,0x00,0x00,0x00,0x08,0x02,0x04,0x0E}; //Command to read data in Block 4 (in Sector 1) in memory of MiFare card
/*****************************/
unsigned char reply_buffer[26]; //Buffer to store data received from MiFare module when a certain command is sent (maximum is 26 bytes in this case)
unsigned char NUID[4]; //Array for storing NUID of a MiFare card
char mode = 0; //Variable storing current mode of the program
SoftwareSerial RFIDSerial(10, 11);
void setup()
{
Serial.begin(9600); //Initiallize UART serial communication with baudrate = 19200bps (default baudrate of MiFare module)
RFIDSerial.begin(19200);
Serial.print("MiFare Demo"); //Print welcome message "MiFare Demo" on LCD
}
void loop()
{
String answer;
while(1)
{
led_on();
Serial.print("reponse on :");
for (int i=0; i<sizeof(reply_buffer);i++) Serial.print(reply_buffer[i],HEX);
Serial.println("");
delay(1000);
led_off();
Serial.print("reponse off :");
for (int i=0; i<sizeof(reply_buffer) ;i++) Serial.print(reply_buffer[i],HEX);
Serial.println("");
delay(1000);
// read dev num ;
read_dev();
Serial.print("reponse read DEV :");
for (int i=0; i<sizeof(reply_buffer) ;i++) Serial.print(reply_buffer[i],HEX);
Serial.println("");
delay(1000);
// get NUID
get_nuid();
Serial.print("NUID :");
for (int i=0; i<sizeof(NUID) ;i++) Serial.print(NUID[i],HEX);
Serial.println("");
delay(1000);
// authentif 4 ;
authen_sec1();
Serial.print("reponse authen_sec1 :");
for (int i=0; i<sizeof(reply_buffer) ;i++) Serial.print(reply_buffer[i]);
Serial.println("");
delay(1000);
/*
// authentif 4 - modif ;
authen_sec1_mod();
Serial.print("reponse authen_sec1_mod :");
for (int i=0; i<sizeof(reply_buffer) ;i++) Serial.print(reply_buffer[i]);
Serial.println("");
delay(1000);
*/
/*
// read 0 ;
read_0();
Serial.print("reponse read 4 :");
for (int i=0; i<sizeof(reply_buffer) ;i++) Serial.print(reply_buffer[i]);
Serial.println("");
delay(1000);
*/
/*
// read 4 ;
read_4();
Serial.print("reponse read 4 :");
for (int i=0; i<sizeof(reply_buffer) ;i++) Serial.print(reply_buffer[i]);
Serial.println("");
delay(1000);
*/
}
}
///*********************************************************************************************************************//
//Turn on MiFare module LED subroutine
void led_on(void)
{
for(int i = 0; i<10; i++)
{
RFIDSerial.write(LED_ON[i]);
}
for(int i = 0; i<10; i++)
{
while(!RFIDSerial.available());
reply_buffer[i] = RFIDSerial.read();
}
}
//Turn on MiFare module LED subroutine
void led_off(void)
{
for(int i = 0; i<10; i++)
{
RFIDSerial.write(LED_OFF[i]);
}
for(int i = 0; i<10; i++)
{
while(!RFIDSerial.available());
reply_buffer[i] = RFIDSerial.read();
}
}
//Read MiFare device number subroutine
void read_dev(void)
{
for(int i = 0; i<9; i++)
{
RFIDSerial.write(READ_DEV[i]);
}
for(int i = 0; i<12; i++)
{
while(!RFIDSerial.available());
reply_buffer[i] = RFIDSerial.read();
}
}
//Turn on antenna of MiFare module subroutine
void antenna_on(void)
{
for(int i = 0; i<10; i++)
{
RFIDSerial.write(ANTENNA_ON[i]);
}
for(int i = 0; i<10; i++)
{
while(!RFIDSerial.available());
reply_buffer[i] = RFIDSerial.read();
}
}
//Request MiFare card type subroutine
void card_req(void)
{
for(int i = 0; i<10; i++)
{
RFIDSerial.write(CARD_REQ[i]);
}
for(int i = 0; i<12; i++)
{
while(!RFIDSerial.available());
reply_buffer[i] = RFIDSerial.read();
}
}
//Perform Anti-Collison subroutine
void anti_col(void)
{
for(int i = 0; i<9; i++)
{
RFIDSerial.write(ANTI_COL[i]);
}
for(int i = 0; i<14; i++)
{
while(!RFIDSerial.available());
reply_buffer[i] = RFIDSerial.read();
}
//Byte 9 to byte 12 of replied bytes contains NUID of the MiFare card
//Store them in NUID[] array
for(int i = 0; i<4; i++)
{
NUID[i] = reply_buffer[9+i];
}
}
//Select MiFare card subroutine
void card_sel(void)
{
for(int i = 0; i<13; i++)
{
RFIDSerial.write(CARD_SEL[i]);
}
for(int i = 0; i<11; i++)
{
while(!RFIDSerial.available());
reply_buffer[i] = RFIDSerial.read();
}
}
//Authenticate Sector 0 in memory of MiFare card subroutine
void authen_sec0(void)
{
for(int i = 0; i<17; i++)
{
RFIDSerial.write(AUTHEN_SEC0[i]);
}
for(int i = 0; i<10; i++)
{
while(!RFIDSerial.available());
reply_buffer[i] = RFIDSerial.read();
}
}
//Authenticate Sector 1 in memory of MiFare card subroutine
void authen_sec1(void)
{
for(int i = 0; i<17; i++)
{
RFIDSerial.write(AUTHEN_SEC1[i]);
}
for(int i = 0; i<10; i++)
{
while(!RFIDSerial.available());
reply_buffer[i] = RFIDSerial.read();
}
}
//Authenticate Sector 1 in memory of MiFare card subroutine
void authen_sec1_mod(void)
{
int XOR = 0x01;
XOR = NUID[0] ^ NUID[1] ^ NUID[2] ^ NUID[3];
for(int i = 0; i<17; i++)
{
RFIDSerial.write(AUTHEN_SEC1_mod[i]);
}
RFIDSerial.write(XOR);
Serial.print("Test XOR =");
Serial.print(XOR,HEX);
Serial.println("");
for(int i = 0; i<10; i++)
{
while(!RFIDSerial.available());
reply_buffer[i] = RFIDSerial.read();
}
}
//Read data from Block 0 (manufaturer block) in memory of MiFare card subroutine
void read_0 (void)
{
Serial.println("OK 1");
get_nuid();
Serial.println("OK 2");
card_sel(); //Select MiFare card
Serial.println("OK 3");
authen_sec0(); //Authenticate Sector 0 in memory of MiFare card
for(int i = 0; i<10; i++)
{
RFIDSerial.write(READ_0[i]);
}
for(int i = 0; i<26; i++)
{
while(!RFIDSerial.available());
reply_buffer[i] = RFIDSerial.read();
}
}
//Read data from Block 4 in memory of MiFare card subroutine
void read_4(void)
{
Serial.println("OK 1");
get_nuid();
Serial.println("OK 2");
card_sel(); //Select MiFare card
Serial.println("OK 3");
authen_sec1(); //Authenticate Sector 0 in memory of MiFare card
Serial.println("OK 4");
for(int i = 0; i<10; i++)
{
RFIDSerial.write(READ_4[i]);
}
for(int i = 0; i<26; i++)
{
while(!RFIDSerial.available());
reply_buffer[i] = RFIDSerial.read();
}
}
//Write data into from Block 4 in memory of MiFare card subroutine
void write_4(void)
{
get_nuid();
card_sel(); //Select MiFare card
authen_sec1(); //Authenticate Sector 0 in memory of MiFare card
for(int i = 0; i<26; i++)
{
RFIDSerial.write(WRITE_4[i]);
}
for(int i = 0; i<10; i++)
{
while(!RFIDSerial.available());
reply_buffer[i] = RFIDSerial.read();
}
}
//Get NUID of MiFare card subroutine
void get_nuid(void)
{
unsigned char xor_temp = 0;
antenna_on(); //On the MiFare module onboard antenna
card_req(); //Request card type of MiFare card
anti_col(); //Perform anti-collision
for(int i = 0; i<4; i++) //Fill up byte 8 to byte 11 of CARD_SEL[] with NUID
{
CARD_SEL[i+8] = NUID[i];
}
for(int i = 0; i<8; i++) //Calculate the checksum of byte 4 to byte 11 of the CARD_SEL[]
{
xor_temp ^= CARD_SEL[4+i];
}
CARD_SEL[12] = xor_temp; //Fill the checksum in byte 12 of CARD_SEL[]
} |
Partager