Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <MFRC522.h>
  3.  
  4. #define SS_PIN 10
  5. #define RST_PIN 9
  6. /*
  7.  * Pin connections for UNO:
  8.  * 9  - RST
  9.  * 10 - SDA
  10.  * 11 - MOSI
  11.  * 12 - MISO
  12.  * 13 - SCK
  13.  */
  14. MFRC522 mfrc522(SS_PIN, RST_PIN);
  15. MFRC522::MIFARE_Key key; // will hold card info
  16.  
  17. // Every 4th block is a trailer block that contains access and security info, modulo 4.
  18. const int blockSize = 16;
  19.  
  20. int writeBlockNumber = 2; // The block we want to write the data.
  21. int readBlockNumber = 2;
  22. byte writeBlockContent[blockSize] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xFF, 0x7, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFA }; // 16 character max
  23. byte readBlockContent[18] = { 0 };
  24.  
  25. // Will write the data to the block.
  26. int writeBlock(int block, byte data[]) {
  27.   int status;
  28.  
  29.   if ( (block + 1) % 4 == 0) {
  30.     Serial.println("Warning: Writing on a trailer block!");
  31.     return 0;
  32.   }
  33.  
  34.   int trailerBlock = block + (4 - block % 4) - 1;
  35.   status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &mfrc522.uid);
  36.   if ( status != MFRC522::STATUS_OK ) {
  37.     Serial.print("PCD_Authenticate() failed: ");
  38.     Serial.println(mfrc522.GetStatusCodeName(status));
  39.     return status;
  40.   }
  41.  
  42.   status = mfrc522.MIFARE_Write(block, data, blockSize);
  43.   if ( status != MFRC522::STATUS_OK ) {
  44.     Serial.print("MIFARE_Write() failed: ");
  45.     Serial.println(mfrc522.GetStatusCodeName(status));
  46.     return status;
  47.   }
  48.   return MFRC522::STATUS_OK;
  49. }
  50.  
  51. // Will try to dump the whole card info.
  52. void dumpCard() {
  53.   for(int i = 0; i < blockSize; ++i) {
  54.       Serial.print("Block ");
  55.       Serial.print(i);
  56.       Serial.print(" ");
  57.       if ( readBlock(i, readBlockContent) == MFRC522::STATUS_OK ) {
  58.         printReadBlock();
  59.       }
  60.   }
  61.   mfrc522.PICC_HaltA();
  62.   mfrc522.PCD_StopCrypto1();
  63. }
  64.  
  65. // Will read the block.
  66. // These functions must be called if you wish to read a new card.
  67. //    mfrc522.PICC_HaltA();
  68. //    mfrc522.PCD_StopCrypto1();
  69. int readBlock(int block, byte data[]) {
  70.   int status;
  71.   byte readSize = blockSize + 2;
  72.  
  73.   int trailerBlock = block + (4 - block % 4) - 1;
  74.   status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &mfrc522.uid);
  75.  
  76.   if ( status != MFRC522::STATUS_OK ) {
  77.     Serial.print("PCD_Authenticate() failed: ");
  78.     Serial.println(mfrc522.GetStatusCodeName(status));
  79.     return status;
  80.   }
  81.  
  82.   status = mfrc522.MIFARE_Read(block, data, &readSize);
  83.   if ( status != MFRC522::STATUS_OK ) {
  84.     Serial.print("MIFARE_Read() failed: ");
  85.     Serial.println(mfrc522.GetStatusCodeName(status));
  86.     return status;
  87.   }
  88.  
  89.   return MFRC522::STATUS_OK;
  90. }
  91.  
  92. // Will print the contents of the readBlockContent.
  93. void printReadBlock() {
  94.   for (int i = 0; i < blockSize; ++i) {
  95.     Serial.print(readBlockContent[i], HEX);
  96.     Serial.print(" ");
  97.   }
  98.   Serial.print(" -> ");
  99.   for (int i = 0; i < blockSize; ++i) {
  100.     Serial.write(readBlockContent[i]);
  101.   }
  102.  
  103.   Serial.println();
  104. }
  105.  
  106. // Will print the card id that is stored in mfrc522.uid.uidByte
  107. void printCardUid() {
  108.   Serial.print("Card ID: ");
  109.   for (int i = 0; i < mfrc522.uid.size; ++i) {
  110.     Serial.print(mfrc522.uid.uidByte[i], HEX);
  111.     Serial.print(" ");
  112.   }
  113.   Serial.println();
  114. }
  115.  
  116. void setup() {
  117.   Serial.begin(9600);
  118.   while (!Serial); // Wait for serial.
  119.   SPI.begin();
  120.  
  121.   mfrc522.PCD_Init();
  122.   Serial.println("Initialized...");
  123.  
  124.   // default access key STANDARD KEY 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
  125.   for (int i = 0; i < 6; ++i) {
  126.     key.keyByte[i] = 0xFF;
  127.   }
  128. }
  129.  
  130. void loop() {
  131.   if ( ! mfrc522.PICC_IsNewCardPresent() ) {
  132.       return;  // No card present, continue.
  133.   }
  134.   if ( ! mfrc522.PICC_ReadCardSerial() ) {
  135.       // On success this function will return 1 and set the uid struct mfrc522.uid
  136.       return;
  137.   }
  138.  
  139.   printCardUid();
  140.  
  141.   // Write data to block.
  142. //  writeBlock(15, writeBlockContent);
  143. //  if ( readBlock(13, readBlockContent) == MFRC522::STATUS_OK )
  144. //    printReadBlock();
  145.  
  146.   dumpCard();
  147.  
  148.   mfrc522.PICC_HaltA();
  149.   mfrc522.PCD_StopCrypto1();
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement