Advertisement
gabbyshimoni

RFID read write

Jan 11th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.60 KB | None | 0 0
  1. #include <SPI.h>      //include the SPI bus library
  2. #include <MFRC522.h>  //include the RFID reader library
  3.  
  4. #define SS_PIN 10  //slave select pin
  5. #define RST_PIN 5  //reset pin
  6.  
  7. MFRC522 mfrc522(SS_PIN, RST_PIN);  // instatiate a MFRC522 reader object.
  8. MFRC522::MIFARE_Key key;          //create a MIFARE_Key struct named 'key', which will hold the card information
  9.  
  10. //this is the block number we will write into and then read.
  11. int block = 6;
  12.  
  13. byte blockcontent[16] = {"1234567890ABCDEF"};  //an array with 16 bytes to be written into one of the 64 card blocks is defined
  14. //byte blockcontent[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};  //all zeros. This can be used to delete a block.
  15.  
  16. //This array is used for reading out a block.
  17. byte readbackblock[18];
  18.  
  19. void setup()
  20. {
  21.   Serial.begin(9600);        // Initialize serial communications with the PC
  22.   SPI.begin();               // Init SPI bus
  23.   mfrc522.PCD_Init();        // Init MFRC522 card (in case you wonder what PCD means: proximity coupling device)
  24.   Serial.println("Scan a MIFARE Classic card");
  25.  
  26.   // Prepare the security key for the read and write functions.
  27.   for (byte i = 0; i < 6; i++) {
  28.     key.keyByte[i] = 0xFF;  //keyByte is defined in the "MIFARE_Key" 'struct' definition in the .h file of the library
  29.   }
  30. }
  31.  
  32. void loop()
  33. {
  34.   Serial.print("Enter number of points  ");
  35.   while (Serial.available() == 0);
  36.   int p = Serial.parseInt();
  37.  
  38.   int2ascii(p, blockcontent);
  39.  
  40.   // Look for new cards
  41.   if ( ! mfrc522.PICC_IsNewCardPresent()) {
  42.     return;
  43.   }
  44.  
  45.   // Select one of the cards
  46.   if ( ! mfrc522.PICC_ReadCardSerial())
  47.   {
  48.     return;
  49.   }
  50.   Serial.println("card selected");
  51.  
  52.   //the blockcontent array is written into the card block
  53.   writeBlock(block, blockcontent);
  54.  
  55.   //read the block back
  56.   readBlock(block, readbackblock);
  57.   //uncomment below line if you want to see the entire 1k memory with the block written into it.
  58.   //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
  59.  
  60.   //print the block contents
  61.   Serial.print("read block: ");
  62.   for (int j = 0 ; j < 16 ; j++)
  63.   {
  64.     Serial.write (readbackblock[j]);
  65.   }
  66.   Serial.println("");
  67. }
  68.  
  69.  
  70.  
  71. //Write specific block
  72. int writeBlock(int blockNumber, byte arrayAddress[])
  73. {
  74.   //this makes sure that we only write into data blocks. Every 4th block is a trailer block for the access/security info.
  75.   int largestModulo4Number = blockNumber / 4 * 4;
  76.   int trailerBlock = largestModulo4Number + 3; //determine trailer block for the sector
  77.   if (blockNumber > 2 && (blockNumber + 1) % 4 == 0) {
  78.     Serial.print(blockNumber);
  79.     Serial.println(" is a trailer block:");
  80.     return 2;
  81.   }
  82.   Serial.print(blockNumber);
  83.   Serial.println(" is a data block:");
  84.  
  85.   //authentication of the desired block for access
  86.   byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  87.   if (status != MFRC522::STATUS_OK) {
  88.     Serial.print("PCD_Authenticate() failed: ");
  89.     Serial.println(mfrc522.GetStatusCodeName(status));
  90.     return 3;//return "3" as error message
  91.   }
  92.  
  93.   //writing the block
  94.   status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16);
  95.   //status = mfrc522.MIFARE_Write(9, value1Block, 16);
  96.   if (status != MFRC522::STATUS_OK) {
  97.     Serial.print("MIFARE_Write() failed: ");
  98.     Serial.println(mfrc522.GetStatusCodeName(status));
  99.     return 4;//return "4" as error message
  100.   }
  101.   Serial.println("block was written");
  102. }
  103.  
  104.  
  105.  
  106. //Read specific block
  107. int readBlock(int blockNumber, byte arrayAddress[])
  108. {
  109.   int largestModulo4Number = blockNumber / 4 * 4;
  110.   int trailerBlock = largestModulo4Number + 3; //determine trailer block for the sector
  111.  
  112.   //authentication of the desired block for access
  113.   byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  114.  
  115.   if (status != MFRC522::STATUS_OK) {
  116.     Serial.print("PCD_Authenticate() failed (read): ");
  117.     Serial.println(mfrc522.GetStatusCodeName(status));
  118.     return 3;//return "3" as error message
  119.   }
  120.  
  121.   //reading a block
  122.   byte buffersize = 18;//we need to define a variable with the read buffer size, since the MIFARE_Read method below needs a pointer to the variable that contains the size...
  123.   status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize);//&buffersize is a pointer to the buffersize variable; MIFARE_Read requires a pointer instead of just a number
  124.   if (status != MFRC522::STATUS_OK) {
  125.     Serial.print("MIFARE_read() failed: ");
  126.     Serial.println(mfrc522.GetStatusCodeName(status));
  127.     return 4;//return "4" as error message
  128.   }
  129.   Serial.println("block was read");
  130. }
  131.  
  132.  
  133. // ***************** int to ASCII *********************
  134. void int2ascii(int num, byte blockdata[]) {
  135.   /*
  136.    * Written by: Gabby Shimoni
  137.    * Last Update; 11-Jan-2020
  138.    * This function gets an integer value to be converted and populated into byte block of data.
  139.    * The function starts by breaking the integer into its digits, and convert each digit to its
  140.    * ASCII equivalent.
  141.    * Each number converted is pushed into the block by pushing the other digits one place ahead
  142.    * At the end, the first places of the block will be populated with the integer digits
  143.   */
  144.   int l = 0;
  145.   Serial.println(num);
  146.   while (num % 10 != num) {
  147.     for (int j = l; j > 0; j--) {
  148.       blockdata[j ] = blockdata[j - 1];
  149.     }
  150.     blockdata[0] = (char)((num % 10) + 48);
  151.     l++;
  152.     num = num / 10;
  153.   }
  154.  
  155.   for (int j = l - 1; j >= 0; j--) {
  156.     blockdata[j + 1] = blockdata[j];
  157.   }
  158.   blockdata[0] = (char)(num + 48);
  159.  
  160.   for (int j = 0 ; j < 16 ; j++)
  161.   {
  162.     Serial.write (blockdata[j]);
  163.   }
  164.   Serial.println("UPDATED");
  165.  
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement