Updated the ReadAndWrite example

This commit is contained in:
mdxs
2014-11-27 13:30:31 +01:00
parent 3b96164ed6
commit b4fd5f74f7

View File

@@ -1,114 +1,134 @@
/* /**
* MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT. * ----------------------------------------------------------------------------
* The library file MFRC522.h has a wealth of useful info. Please read it. * This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
* The functions are documented in MFRC522.cpp. * for further details and other examples.
* *
* Based on code Dr.Leong ( WWW.B2CQSHOP.COM ) * NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
* Created by Miguel Balboa (circuitito.com), Jan, 2012.
* Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.)
* *
* Released into the public domain. * Released into the public domain.
* ----------------------------------------------------------------------------
* This sample shows how to read and write data blocks on a MIFARE Classic PICC
* (= card/tag).
* *
* This sample shows how to setup a block on a MIFARE Classic PICC to be in "Value Block" mode. * BEWARE: Data will be written to the PICC, in sector #1 (blocks #4 to #7).
* In Value Block mode the operations Increment/Decrement/Restore and Transfer can be used.
* *
----------------------------------------------------------------------------- empty_skull *
* Typical pin layout used:
- Aggiunti pin per arduino Mega
- Scritto semplice codice per la scrittura e lettura
- add pin configuration for arduino mega
- write simple read/write Code for new entry user
http://mac86project.altervista.org/
----------------------------------------------------------------------------- Nicola Coppola
* Pin layout should be as follows:
* Signal Pin Pin Pin
* Arduino Uno Arduino Mega MFRC522 board
* ------------------------------------------------------------ * ------------------------------------------------------------
* Reset 9 5 RST * MFRC522 Arduino Arduino Arduino
* SPI SS 10 53 SDA * Reader/PCD Uno Mega Nano v3
* SPI MOSI 11 51 MOSI * Signal Pin Pin Pin Pin
* SPI MISO 12 50 MISO * ------------------------------------------------------------
* SPI SCK 13 52 SCK * RST/Reset RST 9 5 D9
* * SPI SS SDA(SS) 10 53 D10
* The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com. * SPI MOSI MOSI 11 / ICSP-4 51 D11
* SPI MISO MISO 12 / ICSP-1 50 D12
* SPI SCK SCK 13 / ICSP-3 52 D13
*/ */
#include <SPI.h> #include <SPI.h>
#include <MFRC522.h> #include <MFRC522.h>
#define SS_PIN 10 #define RST_PIN 9 // Configurable, see typical pin layout above
#define RST_PIN 9 #define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
/**
* Initialize.
*/
void setup() { void setup() {
Serial.begin(9600); // Initialize serial communications with the PC Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card mfrc522.PCD_Init(); // Init MFRC522 card
//Serial.println("Scan a MIFARE Classic PICC to demonstrate Value Blocks.");
}
void loop() { // Prepare the key (used both as key A and as key B)
// using FFFFFFFFFFFFh which is the default at chip delivery from the factory
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) { for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF; key.keyByte[i] = 0xFF;
} }
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards Serial.println("Scan a MIFARE Classic PICC to demonstrate read and write.");
if ( ! mfrc522.PICC_ReadCardSerial()) { Serial.print("Using key (for A and B):");
return; dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
}
// Now a card is selected. The UID and SAK is in mfrc522.uid.
// Dump UID
Serial.print("Card UID:");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println(); Serial.println();
// Dump PICC type Serial.println("BEWARE: Data will be written to the PICC, in sector #1");
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); }
/**
* Main loop.
*/
void loop() {
// Look for new cards
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("Card UID:");
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print("PICC type: "); Serial.print("PICC type: ");
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType)); Serial.println(mfrc522.PICC_GetTypeName(piccType));
// Check for compatibility
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) { && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
//Serial.println("This sample only works with MIFARE Classic cards."); Serial.println("This sample only works with MIFARE Classic cards.");
return; return;
} }
// In this sample we use the second sector (ie block 4-7). the first sector is = 0 // In this sample we use the second sector,
// scegliere settore di lettura da 0 = primo settore // that is: sector #1, covering block #4 up to and including block #7
byte sector = 1; byte sector = 1;
// block sector 0-3(sector0) 4-7(sector1) 8-11(sector2) byte blockAddr = 4;
// blocchi di scrittura da 0-3(sector0) 4-7(sector1) 8-11(sector2) byte dataBlock[] = {
byte valueBlockA = 4; 0x01, 0x02, 0x03, 0x04, // 1, 2, 3, 4,
byte valueBlockB = 5; 0x05, 0x06, 0x07, 0x08, // 5, 6, 7, 8,
byte valueBlockC = 6; 0x08, 0x09, 0xff, 0x0b, // 9, 10, 255, 12,
0x0c, 0x0d, 0x0e, 0x0f // 13, 14, 15, 16
};
byte trailerBlock = 7; byte trailerBlock = 7;
byte status; byte status;
// Authenticate using key A. byte buffer[18];
// avvio l'autentificazione A byte size = sizeof(buffer);
//Serial.println("Authenticating using key A...");
// Authenticate using key A
Serial.println("Authenticating using key A...");
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid)); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: "); Serial.print("PCD_Authenticate() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
// Authenticate using key B.
// avvio l'autentificazione B // Show the whole sector as it currently is
//Serial.println("Authenticating again using key B..."); Serial.println("Current data in sector:");
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println();
// Read data from the block
Serial.print("Reading data from block "); Serial.print(blockAddr);
Serial.println(" ...");
status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Read() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.print("Data in block "); Serial.print(blockAddr); Serial.println(":");
dump_byte_array(buffer, 16); Serial.println();
Serial.println();
// Authenticate using key B
Serial.println("Authenticating again using key B...");
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid)); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: "); Serial.print("PCD_Authenticate() failed: ");
@@ -116,125 +136,63 @@ void loop() {
return; return;
} }
// Writing new value block A // Write data to the block
// Scrivo i valori per il settore A Serial.print("Writing data into block "); Serial.print(blockAddr);
Serial.println("Writing new value block A(4) : the first of the sector TWO "); Serial.println(" ...");
byte value1Block[] = { 1,2,3,4, 5,6,7,8, 9,10,255,12, 13,14,15,16, valueBlockA,~valueBlockA,valueBlockA,~valueBlockA }; dump_byte_array(dataBlock, 16); Serial.println();
status = mfrc522.MIFARE_Write(valueBlockA, value1Block, 16); status = mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: "); Serial.print("MIFARE_Write() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
} }
/* Serial.println();
// Writing new value block B // Read data from the block (again, should now be what we have written)
// Scrivo i valori per il settore B Serial.print("Reading data from block "); Serial.print(blockAddr);
Serial.println("Writing new value block B"); Serial.println(" ...");
byte value2Block[] = { 255,255,255,255, 0,0,0,0, 0,0,0,0, 255,255,255,255, valueBlockB,~valueBlockB,valueBlockB,~valueBlockB }; status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
status = mfrc522.MIFARE_Write(valueBlockB, value2Block, 16);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: "); Serial.print("MIFARE_Read() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
} }
Serial.print("Data in block "); Serial.print(blockAddr); Serial.println(":");
dump_byte_array(buffer, 16); Serial.println();
// Writing new value block D // Check that data in block is what we have written
// Scrivo i valori per il settore C // by counting the number of bytes that are equal
Serial.println("Writing new value block C"); Serial.println("Checking result...");
byte value3Block[] = { 255,255,255,255, 0,0,0,0, 0,0,0,0, 255,255,255,255, valueBlockC,~valueBlockC,valueBlockC,~valueBlockC }; byte count = 0;
status = mfrc522.MIFARE_Write(valueBlockC, value3Block, 16); for (byte i = 0; i < 16; i++) {
if (status != MFRC522::STATUS_OK) { // Compare buffer (= what we've read) with dataBlock (= what we've written)
Serial.print("MIFARE_Write() failed: "); if (buffer[i] == dataBlock[i])
Serial.println(mfrc522.GetStatusCodeName(status)); count++;
} }
Serial.print("Number of bytes that match = "); Serial.println(count);
*/ if (count == 16) {
Serial.println("Success :-)");
Serial.println("Read block A(4) : the first of the sector TWO");
byte buffer[18];
byte size = sizeof(buffer);
// change this: valueBlockA , for read anather block
// cambiate valueBlockA per leggere un altro blocco
status = mfrc522.MIFARE_Read(valueBlockA, buffer, &size);
Serial.print("Settore : 0 Valore :");
Serial.println(buffer[0]);
Serial.print("Settore : 1 Valore :");
Serial.println(buffer[1]);
Serial.print("Settore : 2 Valore :");
Serial.println(buffer[2]);
Serial.print("Settore : 3 Valore :");
Serial.println(buffer[3]);
Serial.print("Settore : 4 Valore :");
Serial.println(buffer[4]);
Serial.print("Settore : 5 Valore :");
Serial.println(buffer[5]);
Serial.print("Settore : 6 Valore :");
Serial.println(buffer[6]);
Serial.print("Settore : 7 Valore :");
Serial.println(buffer[7]);
Serial.print("Settore : 8 Valore :");
Serial.println(buffer[8]);
Serial.print("Settore : 9 Valore :");
Serial.println(buffer[9]);
Serial.print("Settore :10 Valore :");
Serial.println(buffer[10]);
Serial.print("Settore :11 Valore :");
Serial.println(buffer[11]);
Serial.print("Settore :12 Valore :");
Serial.println(buffer[12]);
Serial.print("Settore :13 Valore :");
Serial.println(buffer[13]);
Serial.print("Settore :14 Valore :");
Serial.println(buffer[14]);
Serial.print("Settore :15 Valore :");
Serial.println(buffer[15]);
//byte value1Block[] = { 1,2,3,4, 5,6,7,8, 9,10,255,12, 13,14,15,16, valueBlockA,~valueBlockA,valueBlockA,~valueBlockA };
if (
buffer[0] == 1 &&
buffer[1] == 2 &&
buffer[2] == 3 &&
buffer[3] == 4 &&
buffer[4] == 5 &&
buffer[5] == 6 &&
buffer[6] == 7 &&
buffer[7] == 8 &&
buffer[8] == 9 &&
buffer[9] == 10 &&
buffer[10] == 255 &&
buffer[11] == 12 &&
buffer[12] == 13 &&
buffer[13] == 14 &&
buffer[14] == 15 &&
buffer[15] == 16
){
// sel a scrittura è uguale alla lettura allora e stato un successo !!
Serial.println("Read block A(4) : the first of the sector TWO : success");
Serial.println(":-)");
} else { } else {
Serial.println("Failure, no match :-(");
// scrittura Fallita Serial.println(" perhaps the write didn't work properly...");
Serial.println("Read block A(4) : the first of the sector TWO : no match - write don't work fine ");
Serial.println(":-( ");
} }
Serial.println();
// Dump the sector data
// risponde successo Serial.println("Current data in sector:");
//Serial.println(mfrc522.GetStatusCodeName(status)); mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println();
// Dump the result
//mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
// Halt PICC // Halt PICC
mfrc522.PICC_HaltA(); mfrc522.PICC_HaltA();
// Stop encryption on PCD // Stop encryption on PCD
mfrc522.PCD_StopCrypto1(); mfrc522.PCD_StopCrypto1();
}
/**
* 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);
}
} }