Advertisement
a5768549

RFID.h

Jun 5th, 2020
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.99 KB | None | 0 0
  1. #include <MFRC522.h>
  2. #define SS_PIN    21
  3. #define RST_PIN   22
  4.  
  5. String Card_code;
  6. int scan_to_post = 0;
  7.  
  8. MFRC522 RFID(SS_PIN, RST_PIN);   // 創建新的RFID實例
  9. MFRC522::MIFARE_Key key;
  10. void RFID_init()
  11. {
  12.   SPI.begin();
  13.   RFID.PCD_Init();
  14.  
  15.   for (byte i = 0; i < 6; i++)
  16.   {
  17.     key.keyByte[i] = 0xFF;
  18.   }
  19.  
  20. }
  21. void dump_byte_array(byte *buffer, byte bufferSize)
  22. {
  23.   Card_code = "";
  24.   String code_temp[4];
  25.   for (byte i = 0; i < bufferSize; i++)
  26.   {
  27.     code_temp[i] = buffer[i];
  28.     Card_code += code_temp[i];
  29.   }
  30.   scan_to_post = 1;
  31.   Serial.println(Card_code);
  32.   Serial.println();
  33.  
  34. }
  35.  
  36. void read_RFID()
  37. {
  38.   if ( ! RFID.PICC_ReadCardSerial())
  39.   {
  40.     delay(50);
  41.     return;
  42.   }
  43.   Serial.println("RFID Tag Detected...");
  44.   Serial.print(F("Card UID:"));
  45.   dump_byte_array(RFID.uid.uidByte, RFID.uid.size);
  46. }
  47.  
  48. int money_dec(byte *buffer ,byte bufferSize)
  49. {
  50.   String tmp[3];
  51.   for (byte i = 0; i < bufferSize; i++)
  52.   {
  53.     tmp[i] = String(buffer[i], HEX);
  54.   }
  55.   tmp[3] = tmp[0] + tmp[1];
  56.   long money_dec = strtol(tmp[3].c_str(), NULL, 16);
  57.   return money_dec;
  58. }
  59.  
  60. int dump_byte_array_RW(byte *buffer, byte bufferSize)
  61. {
  62.     for (byte i = 0; i < bufferSize; i++)
  63.     {
  64.         Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  65.         Serial.print(buffer[i], HEX);
  66.     }
  67.     int money = money_dec(buffer,bufferSize);
  68.     return money;
  69. }
  70.  
  71.  
  72. int read_RFID_sector()
  73. {
  74.     byte sector         = 1;
  75.     byte blockAddr      = 4;
  76.    
  77.     byte buffer[18];
  78.     byte size = sizeof(buffer);
  79.     RFID.PICC_DumpMifareClassicSectorToSerial(&(RFID.uid), &key, sector);
  80.     Serial.println();
  81.     //read
  82.     RFID.MIFARE_Read(blockAddr, buffer, &size);
  83.     Serial.print(F("Data in block "));
  84.     Serial.print(blockAddr);
  85.     Serial.println(F(":"));
  86.     int money = dump_byte_array_RW(buffer, 2);
  87.     Serial.println();
  88.  
  89.     RFID.PICC_HaltA();
  90.     // Stop encryption on PCD
  91.     RFID.PCD_StopCrypto1();
  92.     return money;
  93. }
  94.  
  95. int write_rfid(int money)
  96. {
  97.   // Show some details of the PICC (that is: the tag/card)
  98.     Serial.print(F("Card UID:"));
  99.     dump_byte_array_RW(RFID.uid.uidByte, RFID.uid.size);
  100.     Serial.println();
  101.     Serial.print(F("PICC type: "));
  102.     MFRC522::PICC_Type piccType = RFID.PICC_GetType(RFID.uid.sak);
  103.     Serial.println(RFID.PICC_GetTypeName(piccType));
  104.  
  105.     // Check for compatibility
  106.     if (    piccType != MFRC522::PICC_TYPE_MIFARE_MINI
  107.         &&  piccType != MFRC522::PICC_TYPE_MIFARE_1K
  108.         &&  piccType != MFRC522::PICC_TYPE_MIFARE_4K)
  109.     {
  110.         Serial.println(F("This sample only works with MIFARE Classic cards."));
  111.         return '0';
  112.     }
  113.    
  114.    
  115.     byte sector         = 1;
  116.     byte blockAddr      = 4;
  117.     String tmp[3];
  118.     tmp[2] =  String(money, HEX);
  119.     tmp[0] = tmp[2].substring(0,2);
  120.     tmp[1] = tmp[2].substring(2);
  121.    
  122.     byte tmp1 = strtol(tmp[0].c_str(), NULL, 16);
  123.     byte tmp2 = strtol(tmp[1].c_str(), NULL, 16);
  124.     byte dataBlock[]    =
  125.     {
  126.         tmp1, tmp2, 0x00, 0x00,
  127.         0x00, 0x00, 0x00, 0x00,
  128.         0x00, 0x00, 0x00, 0x00,
  129.         0x00, 0x00, 0x00, 0x00
  130.     };
  131.    
  132.     MFRC522::StatusCode status;
  133.     byte buffer[18];
  134.     byte size = sizeof(buffer);
  135.    
  136.     Serial.println(dataBlock[0]);
  137.     Serial.println(dataBlock[1]);
  138.     byte trailerBlock   = 7;
  139.  
  140.     // Authenticate using key A
  141.     Serial.println(F("Authenticating using key A..."));
  142.     status = (MFRC522::StatusCode) RFID.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(RFID.uid));
  143.     if (status != MFRC522::STATUS_OK)
  144.     {
  145.         Serial.print(F("PCD_Authenticate() failed: "));
  146.         Serial.println(RFID.GetStatusCodeName(status));
  147.         return '1';
  148.     }
  149.  
  150.     // Show the whole sector as it currently is
  151.     Serial.println(F("Current data in sector:"));
  152.     RFID.PICC_DumpMifareClassicSectorToSerial(&(RFID.uid), &key, sector);
  153.     Serial.println();
  154.  
  155.     // Read data from the block
  156.     Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
  157.     Serial.println(F(" ..."));
  158.     status = (MFRC522::StatusCode) RFID.MIFARE_Read(blockAddr, buffer, &size);
  159.     if (status != MFRC522::STATUS_OK)
  160.     {
  161.         Serial.print(F("MIFARE_Read() failed: "));
  162.         Serial.println(RFID.GetStatusCodeName(status));
  163.     }
  164.     Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
  165.     dump_byte_array_RW(buffer, 16); Serial.println();
  166.     Serial.println();
  167.  
  168.     // Authenticate using key B
  169.     Serial.println(F("Authenticating again using key B..."));
  170.     status = (MFRC522::StatusCode) RFID.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(RFID.uid));
  171.     if (status != MFRC522::STATUS_OK) {
  172.         Serial.print(F("PCD_Authenticate() failed: "));
  173.         Serial.println(RFID.GetStatusCodeName(status));
  174.         return '2';
  175.     }
  176.  
  177.     // Write data to the block
  178.     Serial.print(F("Writing data into block ")); Serial.print(blockAddr);
  179.     Serial.println(F(" ..."));
  180.     dump_byte_array_RW(dataBlock, 16); Serial.println();
  181.     status = (MFRC522::StatusCode) RFID.MIFARE_Write(blockAddr, dataBlock, 16);
  182.     if (status != MFRC522::STATUS_OK)
  183.     {
  184.         Serial.print(F("MIFARE_Write() failed: "));
  185.         Serial.println(RFID.GetStatusCodeName(status));
  186.     }
  187.     Serial.println();
  188.  
  189.     // Read data from the block (again, should now be what we have written)
  190.     Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
  191.     Serial.println(F(" ..."));
  192.     status = (MFRC522::StatusCode) RFID.MIFARE_Read(blockAddr, buffer, &size);
  193.     if (status != MFRC522::STATUS_OK)
  194.     {
  195.         Serial.print(F("MIFARE_Read() failed: "));
  196.         Serial.println(RFID.GetStatusCodeName(status));
  197.     }
  198.     Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
  199.     dump_byte_array_RW(buffer, 16); Serial.println();
  200.  
  201.     // Check that data in block is what we have written
  202.     // by counting the number of bytes that are equal
  203.     Serial.println(F("Checking result..."));
  204.     byte count = 0;
  205.     for (byte i = 0; i < 16; i++)
  206.     {
  207.         // Compare buffer (= what we've read) with dataBlock (= what we've written)
  208.         if (buffer[i] == dataBlock[i])
  209.             count++;
  210.     }
  211.     Serial.print(F("Number of bytes that match = ")); Serial.println(count);
  212.     if (count == 16)
  213.     {
  214.         Serial.println(F("Success :-)"));
  215.     }
  216.     else
  217.     {
  218.         Serial.println(F("Failure, no match :-("));
  219.         Serial.println(F("  perhaps the write didn't work properly..."));
  220.     }
  221.     Serial.println();
  222.  
  223.     // Dump the sector data
  224.     Serial.println(F("Current data in sector:"));
  225.     RFID.PICC_DumpMifareClassicSectorToSerial(&(RFID.uid), &key, sector);
  226.     Serial.println();
  227.  
  228.     // Halt PICC
  229.     RFID.PICC_HaltA();
  230.     // Stop encryption on PCD
  231.     RFID.PCD_StopCrypto1();
  232. }
  233.  
  234. void Detect_RFID()
  235. {
  236.   if ( ! RFID.PICC_IsNewCardPresent())
  237.   {
  238.     return;
  239.   }
  240.   read_RFID();
  241.   int money = read_RFID_sector();
  242.   money -= 60;
  243.   delay(1000);
  244.   write_rfid(money);
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement