Advertisement
Guest User

dump nfc

a guest
Sep 28th, 2023
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | Source Code | 0 0
  1. #include <Wire.h>
  2. #include <SPI.h>
  3. #include <MFRC522_I2C.h>
  4.  
  5. #define SDA_PIN 32
  6. #define SCL_PIN 33
  7.  
  8. #define MFRC522_I2C MFRC522
  9.  
  10. MFRC522 mfrc522(0x28);
  11.  
  12. void dump_byte_array(byte *buffer, byte bufferSize) {
  13.   for (byte i = 0; i < bufferSize; i++) {
  14.     Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  15.     Serial.print(buffer[i], HEX);
  16.   }
  17.   Serial.print(" |");
  18.   for (byte i = 0; i < bufferSize; i++) {
  19.     if (buffer[i] >= 32 && buffer[i] <= 126) {  // Check if the byte is a printable ASCII character
  20.       Serial.print((char)buffer[i]);
  21.     } else {
  22.       Serial.print('.');  // Print a dot for non-printable characters
  23.     }
  24.   }
  25.   Serial.print('|');
  26. }
  27.  
  28. void setup() {
  29.   Wire.begin(SDA_PIN, SCL_PIN);
  30.   SPI.begin();
  31.   mfrc522.PCD_Init();
  32.   Serial.println("Scan a card...");
  33. }
  34.  
  35. void loop() {
  36.   // Look for new cards
  37.   if (!mfrc522.PICC_IsNewCardPresent()) {
  38.     return;
  39.   }
  40.   // Select one of the cards
  41.   if (!mfrc522.PICC_ReadCardSerial()) {
  42.     return;
  43.   }
  44.   // Show some details of the PICC (that is: the tag/card)
  45.   Serial.print("Card UID:");
  46.   dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  47.   Serial.println();
  48.  
  49.   Serial.println("Card Data:");
  50.   int lineNumber = 0;
  51.   for (byte block = 0; block < 255; block++) {
  52.     byte buffer[18];
  53.     byte size = sizeof(buffer);
  54.     byte status = mfrc522.MIFARE_Read(block, buffer, &size);
  55.     if (status != MFRC522::STATUS_OK) {
  56.       Serial.print("MIFARE_Read() failed: ");
  57.       Serial.println(mfrc522.GetStatusCodeName(status));
  58.     }
  59.     if (status == MFRC522::STATUS_TIMEOUT) {
  60.       Serial.print("MIFARE_Read() failed: ");
  61.       Serial.println(mfrc522.GetStatusCodeName(status));
  62.       break;
  63.     }
  64.     Serial.print("[");
  65.     if (block < 16) {
  66.       Serial.print("0");
  67.     }
  68.     Serial.print(block, HEX);
  69.     Serial.print("] ");
  70.     dump_byte_array(buffer, 4);  // idk why but only first 4 bytes are shiftted
  71.     Serial.println();
  72.   }
  73.   Serial.println("Reading Finished");
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement