微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我尝试使用 PN532 板和 Arduino Nano 读取标准 Mifare 卡,但它读取了我的代码

如何解决我尝试使用 PN532 板和 Arduino Nano 读取标准 Mifare 卡,但它读取了我的代码

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>

//Interfacing dengan mode SPI
#define PN532_SCK  (13)
#define PN532_MOSI (11)
#define PN532_SS   (10)
#define PN532_MISO (12)
Adafruit_PN532 nfc(PN532_SCK,PN532_MISO,PN532_MOSI,PN532_SS);

void setup(void) {
  Serial.begin(115200);
  nfc.begin();
  uint32_t versiondata = nfc.getFirmwareversion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  // configure board to read RFID tags
  nfc.SAMConfig();

  Serial.println("Waiting for an ISO14443A Card ...");
}


void loop(void) {
  uint8_t success;                          // Flag to check if there was an error with the PN532
  uint8_t uid[] = { 0,0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  uint8_t currentblock;                     // Counter to keep track of which block we're on
  bool authenticated = false;               // Flag to indicate if the sector is authenticated
  uint8_t data[16];                         // Array to store block data during reads

  // Keyb on NDEF and mifare Classic should be the same
  uint8_t keyuniversal[6] = { 0xFF,0xFF,0xFF};

  // Wait for an ISO14443A type cards (mifare,etc.).  When one is found
  // 'uid' will be populated with the UID,and uidLength will indicate
  // if the uid is 4 bytes (mifare Classic) or 7 bytes (mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_mifare_ISO14443A,uid,&uidLength);

  if (success) {
    // display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength,DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid,uidLength);
    Serial.println("");

    if (uidLength == 4 or 7)
    {
      // Now we try to go through all 16 sectors (each having 4 blocks)
      // authenticating each sector,and then dumping the blocks
      for (currentblock = 0; currentblock < 64; currentblock++)
      {
        // Check if this is a new block so that we can reauthenticate
        if (nfc.mifareclassic_IsFirstBlock(currentblock)) authenticated = false;

        // If the sector hasn't been authenticated,do so first
        if (!authenticated)
        {
          // Starting of a new sector ... try to to authenticate
          Serial.print("------------------------Sector ");Serial.print(currentblock/4,DEC);Serial.println("-------------------------");
          if (currentblock == 0)
          {
              // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for mifare Classic (non-NDEF!)
              // or 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 for NDEF formatted cards using key a,// but keyb should be the same for both (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF)
              success = nfc.mifareclassic_AuthenticateBlock (uid,uidLength,currentblock,1,keyuniversal);
          }
          else
          {
              // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for mifare Classic (non-NDEF!)
              // or 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 for NDEF formatted cards using key a,keyuniversal);
          }
          if (success)
          {
            authenticated = true;
          }
          else
          {
            Serial.println("Authentication error");
          }
        }
        // If we're still not authenticated just skip the block
        if (!authenticated)
        {
          Serial.print("Block ");Serial.print(currentblock,DEC);Serial.println(" unable to authenticate");
        }
        else
        {
          // Authenticated ... we should be able to read the block Now
          // Dump the data into the 'data' array
          success = nfc.mifareclassic_ReadDataBlock(currentblock,data);
          if (success)
          {
            // Read successful
            Serial.print("Block ");Serial.print(currentblock,DEC);
            if (currentblock < 10)
            {
              Serial.print("  ");
            }
            else
            {
              Serial.print(" ");
            }
            // Dump the raw data
            nfc.PrintHexChar(data,64);
          }
          else
          {
            // Oops ... something happened
            Serial.print("Block ");Serial.print(currentblock,DEC);
            Serial.println(" unable to read this block");
          }
        }
      }
    }
    else
    {
      Serial.println("Ooops ... this doesn't seem to be a mifare Classic card!");
    }
  }
  // Wait a bit before trying again
  Serial.println("\n\nSend a character to run the mem dumper again!");
  Serial.flush();
  while (!Serial.available());
  while (Serial.available()) {
  Serial.read();
  }
  Serial.flush();
}

我使用该代码并更改了我想在这一行中读取的数据长度

nfc.PrintHexChar(data,64);

当我尝试使用 64 字节(我不确定它是字节还是位)或更低时,它似乎没问题,但是当我尝试更大的数字时,它也会转储我的代码

这是我尝试转储 128 字节数据时的日志

Waiting for an ISO14443A Card ...
Found an ISO14443A card
  UID Length: 4 bytes
  UID Value: 0xD0 0x8E 0x1C 0x1B

------------------------Sector 0-------------------------
Block 0  D0 8E 1C 1B 59 08 04 00 62 63 64 65 66 67 68 69 D0 8E 1C 1B 00 00 00 FF FF FF FF FF FF 59 09 F8 08 00 00 00 69 00 90 00 00 FF 06 FA D5 00 00 FF 00 FF 00 00 00 37 04 80 00 FF FF FF FF FF FF 00 00 00 00 36 01 96 00 C3 00 9E 01 F4 00 D2 00 E6 00 00 00 00 00 CB 04 9E 04 FF 04 74 04 97 04 84 04 75 04 0D 0A 00 44 69 64 6E 27 74 20 66 69 6E 64 20 50 4E 35 33 78 20 62 6F 61 72 64 00 57 61  Ў..Y...bcdefghiЎ.....⸮⸮⸮⸮⸮⸮Y.Y....i.⸮..⸮.⸮⸮..⸮.⸮...7.⸮.⸮⸮⸮⸮⸮⸮....6.⸮.⸮.⸮.⸮.⸮.⸮.....⸮.⸮.⸮.t.⸮.⸮.u....Didn't find PN53x board.Wa
Block 1  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 D0 8E 1C 1B 00 00 00 FF FF FF FF FF FF 59 09 F8 08 00 00 00 69 00 90 00 00 FF 06 FA D5 00 00 FF 00 FF 00 00 00 37 04 80 00 FF FF FF FF FF FF 00 00 00 00 36 01 96 00 C3 00 9E 01 F4 00 D2 00 E6 00 00 00 00 00 CB 04 9E 04 FF 04 74 04 97 04 84 04 75 04 0D 0A 00 44 69 64 6E 27 74 20 66 69 6E 64 20 50 4E 35 33 78 20 62 6F 61 72 64 00 57 61  ................Ў.....⸮⸮⸮⸮⸮⸮Y.Y....i.⸮..⸮.⸮⸮..⸮.⸮...7.⸮.⸮⸮⸮⸮⸮⸮....6.⸮.⸮.⸮.⸮.⸮.⸮.....⸮.⸮.⸮.t.⸮.⸮.u....Didn't find PN53x board.Wa
Block 2  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 D0 8E 1C 1B 00 00 00 FF FF FF FF FF FF 59 09 F8 08 00 00 00 69 00 90 00 00 FF 06 FA D5 00 00 FF 00 FF 00 00 00 37 04 80 00 FF FF FF FF FF FF 00 00 00 00 36 01 96 00 C3 00 9E 01 F4 00 D2 00 E6 00 00 00 00 00 CB 04 9E 04 FF 04 74 04 97 04 84 04 75 04 0D 0A 00 44 69 64 6E 27 74 20 66 69 6E 64 20 50 4E 35 33 78 20 62 6F 61 72 64 00 57 61  ................Ў.....⸮⸮⸮⸮⸮⸮Y.Y....i.⸮..⸮.⸮⸮..⸮.⸮...7.⸮.⸮⸮⸮⸮⸮⸮....6.⸮.⸮.⸮.⸮.⸮.⸮.....⸮.⸮.⸮.t.⸮.⸮.u....Didn't find PN53x board.Wa
Block 3  00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF D0 8E 1C 1B 00 00 00 FF FF FF FF FF FF 59 09 F8 08 00 00 00 69 00 90 00 00 FF 06 FA D5 00 00 FF 00 FF 00 00 00 37 04 80 00 FF FF FF FF FF FF 00 00 00 00 36 01 96 00 C3 00 9E 01 F4 00 D2 00 E6 00 00 00 00 00 CB 04 9E 04 FF 04 74 04 97 04 84 04 75 04 0D 0A 00 44 69 64 6E 27 74 20 66 69 6E 64 20 50 4E 35 33 78 20 62 6F 61 72 64 00 57 61  ......⸮.⸮i⸮⸮⸮⸮⸮⸮Ў.....⸮⸮⸮⸮⸮⸮Y.Y....i.⸮..⸮.⸮⸮..⸮.⸮...7.⸮.⸮⸮⸮⸮⸮⸮....6.⸮.⸮.⸮.⸮.⸮.⸮.....⸮.⸮.⸮.t.⸮.⸮.u....Didn't find PN53x board.Wa

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?