Skocz do zawartości

Czytnik PN532 podłączony do Arduino Uno daje błąd


Pomocna odpowiedź

Napisano

Stworzyłem kod to formatowania taga RFID, niestety wyskakuje następujący błąd: 

Unable to authenticate block 0 to enable card formatting!

Format failed.


Czy ktoś wie jak mógłbym ten problem rozwiązać?

Poniżej podsyłam kod którego używam:

 


#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);
String tagId = "None";
byte nuidPICC[4];
 
void setup(void) 
{
 Serial.begin(9600);
 Serial.println("System initialized");
 nfc.begin();
 formatNFC();
}
 
void loop() {
  formatNFC();
}

void writeNFC() {
  Serial.println("\nPlace a formatted Mifare Classic NFC tag on the reader.");
    if (nfc.tagPresent()) {
        NdefMessage message = NdefMessage();
        message.addUriRecord("http://arduino.cc");

        bool success = nfc.write(message);
        if (success) {
          Serial.println("Success. Try reading this tag with your phone.");        
        } else {
          Serial.println("Write failed.");
        }
    }
    delay(5000);
}

void formatNFC() {
    
    Serial.println("\nPlace an unformatted Mifare Classic tag on the reader.");
    if (nfc.tagPresent()) {

        bool success = nfc.format();
        if (success) {
          Serial.println("\nSuccess, tag formatted as NDEF.");
        } else {
          Serial.println("\nFormat failed.");
        }

    }
    delay(5000);
}

 

funkcję writeNFC() proszę ignorować, gdyż normalnie mam ją zakomentowaną

46 minut temu, Igus napisał:
String tagId = "None";

Interesuje mnie do czego służy ta zmienna. Z resztą czy nie powinieneś tam wpisać swojego tagu karty? Nie wiem czy dobrze podpowiadam.

48 minut temu, Igus napisał:
NfcAdapter nfc = 

Ta linijka wygląda mi na jeszcze nie skończoną...

Coś Ci się chyba nie wyświetliło:

 

 

#include <Wire.h>

#include <PN532_I2C.h>

#include <PN532.h>

#include <NfcAdapter.h>

PN532_I2C pn532_i2c(Wire);

NfcAdapter nfc = NfcAdapter(pn532_i2c);

String tagId = "None";

byte nuidPICC[4];

 

void setup(void)

{

 Serial.begin(9600);

 Serial.println("System initialized");

 nfc.begin();

}

 

void loop() {

  readNFC();

}

 

void writeNFC() {

  Serial.println("\nPlace a formatted Mifare Classic NFC tag on the reader.");

    if (nfc.tagPresent()) {

        NdefMessage message = NdefMessage();

        message.addUriRecord("Kocham Dragona");

 

        bool success = nfc.write(message);

        if (success) {

          Serial.println("Success. Try reading this tag with your phone.");        

        } else {

          Serial.println("Write failed.");

        }

    }

    delay(5000);

}

 

void formatNFC() {

   

    Serial.println("\nPlace an unformatted Mifare Classic tag on the reader.");

    if (nfc.tagPresent()) {

 

        bool success = nfc.format();

        if (success) {

          Serial.println("\nSuccess, tag formatted as NDEF.");

        } else {

          Serial.println("\nFormat failed.");

        }

 

    }

    delay(5000);

}

 

void readNFC() {

    Serial.println("\nScan a NFC tag\n");

    if (nfc.tagPresent())

    {

        NfcTag tag = nfc.read();

        tag.print();

    }

    delay(5000);

}

 

void eraseNFC() {

    Serial.println("\nPlace a tag on the NFC reader to erase.");

 

    if (nfc.tagPresent()) {

 

        bool success = nfc.erase();

        if (success) {

            Serial.println("\nSuccess, tag contains an empty record.");        

        } else {

            Serial.println("\nUnable to erase tag.");

        }

 

    }

    delay(5000);

}

 

void readplusNFC() {

  Serial.println("\nScan a NFC tag\n");

 

  if (nfc.tagPresent())

  {

    NfcTag tag = nfc.read();

    Serial.println(tag.getTagType());

    Serial.print("UID: ");Serial.println(tag.getUidString());

 

    if (tag.hasNdefMessage()) // every tag won't have a message

    {

 

      NdefMessage message = tag.getNdefMessage();

      Serial.print("\nThis NFC Tag contains an NDEF Message with ");

      Serial.print(message.getRecordCount());

      Serial.print(" NDEF Record");

      if (message.getRecordCount() != 1) {

        Serial.print("s");

      }

      Serial.println(".");

 

      // cycle through the records, printing some info from each

      int recordCount = message.getRecordCount();

      for (int i = 0; i < recordCount; i++)

      {

        Serial.print("\nNDEF Record ");Serial.println(i+1);

        NdefRecord record = message.getRecord(i);

        // NdefRecord record = message; // alternate syntax

 

        Serial.print("  TNF: ");Serial.println(record.getTnf());

        Serial.print("  Type: ");Serial.println(record.getType()); // will be "" for TNF_EMPTY

 

        // The TNF and Type should be used to determine how your application processes the payload

        // There's no generic processing for the payload, it's returned as a byte[]

        int payloadLength = record.getPayloadLength();

        byte payload[payloadLength];

        record.getPayload(payload);

 

        // Print the Hex and Printable Characters

        Serial.print("  Payload (HEX): ");

        PrintHexChar(payload, payloadLength);

 

        // Force the data into a String (might work depending on the content)

        // Real code should use smarter processing

        String payloadAsString = "";

        for (int c = 0; c < payloadLength; c++) {

          payloadAsString += (char)payload[c];

        }

        Serial.print("  Payload (as String): ");

        Serial.println(payloadAsString);

 

        // id is probably blank and will return ""

        String uid = record.getId();

        if (uid != "") {

          Serial.print("  ID: ");Serial.println(uid);

        }

      }

    }

  }

  delay(3000);

}

 

void cleanNFC() {

 

    Serial.println("\nPlace a tag on the NFC reader to clean.");

 

    if (nfc.tagPresent()) {

 

        bool success = nfc.clean();

        if (success) {

            Serial.println("\nSuccess, tag restored to factory state.");

        } else {

            Serial.println("\nError, unable to clean tag.");

        }

 

    }

    delay(5000);

}

 

void writemultiNFC() {

    Serial.println("\nPlace a formatted Mifare Classic NFC tag on the reader.");

    if (nfc.tagPresent()) {

        NdefMessage message = NdefMessage();

        message.addTextRecord("Hello, Arduino!");

        message.addUriRecord("http://arduino.cc");

        message.addTextRecord("Goodbye, Arduino!");

        boolean success = nfc.write(message);

        if (success) {

            Serial.println("Success. Try reading this tag with your phone.");

        } else {

            Serial.println("Write failed");

        }

    }

    delay(3000);

}



 


#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);
String tagId = "None";
byte nuidPICC[4];
 
void setup(void) 
{
 Serial.begin(9600);
 Serial.println("System initialized");
 nfc.begin();
}
 
void loop() {
  readNFC();
}

void writeNFC() {
  Serial.println("\nPlace a formatted Mifare Classic NFC tag on the reader.");
    if (nfc.tagPresent()) {
        NdefMessage message = NdefMessage();
        message.addUriRecord("Kocham Dragona");

        bool success = nfc.write(message);
        if (success) {
          Serial.println("Success. Try reading this tag with your phone.");        
        } else {
          Serial.println("Write failed.");
        }
    }
    delay(5000);
}

void formatNFC() {
    
    Serial.println("\nPlace an unformatted Mifare Classic tag on the reader.");
    if (nfc.tagPresent()) {

        bool success = nfc.format();
        if (success) {
          Serial.println("\nSuccess, tag formatted as NDEF.");
        } else {
          Serial.println("\nFormat failed.");
        }

    }
    delay(5000);
}

void readNFC() {
    Serial.println("\nScan a NFC tag\n");
    if (nfc.tagPresent())
    {
        NfcTag tag = nfc.read();
        tag.print();
    }
    delay(5000);
}

void eraseNFC() {
    Serial.println("\nPlace a tag on the NFC reader to erase.");

    if (nfc.tagPresent()) {

        bool success = nfc.erase();
        if (success) {
            Serial.println("\nSuccess, tag contains an empty record.");        
        } else {
            Serial.println("\nUnable to erase tag.");
        }

    }
    delay(5000);
}

void readplusNFC() {
  Serial.println("\nScan a NFC tag\n");

  if (nfc.tagPresent())
  {
    NfcTag tag = nfc.read();
    Serial.println(tag.getTagType());
    Serial.print("UID: ");Serial.println(tag.getUidString());

    if (tag.hasNdefMessage()) // every tag won't have a message
    {

      NdefMessage message = tag.getNdefMessage();
      Serial.print("\nThis NFC Tag contains an NDEF Message with ");
      Serial.print(message.getRecordCount());
      Serial.print(" NDEF Record");
      if (message.getRecordCount() != 1) {
        Serial.print("s");
      }
      Serial.println(".");

      // cycle through the records, printing some info from each
      int recordCount = message.getRecordCount();
      for (int i = 0; i < recordCount; i++)
      {
        Serial.print("\nNDEF Record ");Serial.println(i+1);
        NdefRecord record = message.getRecord(i);
        // NdefRecord record = message[i]; // alternate syntax

        Serial.print("  TNF: ");Serial.println(record.getTnf());
        Serial.print("  Type: ");Serial.println(record.getType()); // will be "" for TNF_EMPTY

        // The TNF and Type should be used to determine how your application processes the payload
        // There's no generic processing for the payload, it's returned as a byte[]
        int payloadLength = record.getPayloadLength();
        byte payload[payloadLength];
        record.getPayload(payload);

        // Print the Hex and Printable Characters
        Serial.print("  Payload (HEX): ");
        PrintHexChar(payload, payloadLength);

        // Force the data into a String (might work depending on the content)
        // Real code should use smarter processing
        String payloadAsString = "";
        for (int c = 0; c < payloadLength; c++) {
          payloadAsString += (char)payload[c];
        }
        Serial.print("  Payload (as String): ");
        Serial.println(payloadAsString);

        // id is probably blank and will return ""
        String uid = record.getId();
        if (uid != "") {
          Serial.print("  ID: ");Serial.println(uid);
        }
      }
    }
  }
  delay(3000);
}

void cleanNFC() {

    Serial.println("\nPlace a tag on the NFC reader to clean.");

    if (nfc.tagPresent()) {

        bool success = nfc.clean();
        if (success) {
            Serial.println("\nSuccess, tag restored to factory state.");
        } else {
            Serial.println("\nError, unable to clean tag.");
        }

    }
    delay(5000);
}

void writemultiNFC() {
    Serial.println("\nPlace a formatted Mifare Classic NFC tag on the reader.");
    if (nfc.tagPresent()) {
        NdefMessage message = NdefMessage();
        message.addTextRecord("Hello, Arduino!");
        message.addUriRecord("http://arduino.cc");
        message.addTextRecord("Goodbye, Arduino!");
        boolean success = nfc.write(message);
        if (success) {
            Serial.println("Success. Try reading this tag with your phone.");
        } else {
            Serial.println("Write failed");
        }
    }
    delay(3000);
}

 

18 godzin temu, anonimowy napisał:

Interesuje mnie do czego służy ta zmienna. Z resztą czy nie powinieneś tam wpisać swojego tagu karty? Nie wiem czy dobrze podpowiadam.

To jest zmienna do zapisywania ID taga.

18 godzin temu, anonimowy napisał:

Ta linijka wygląda mi na jeszcze nie skończoną...

U mnie jest: 
NfcAdapter nfc = NfcAdapter(pn532_i2c);

NfcAdapter nfc = NfcAdapter(pn532_i2c);

 

Bądź aktywny - zaloguj się lub utwórz konto!

Tylko zarejestrowani użytkownicy mogą komentować zawartość tej strony

Utwórz konto w ~20 sekund!

Zarejestruj nowe konto, to proste!

Zarejestruj się »

Zaloguj się

Posiadasz własne konto? Użyj go!

Zaloguj się »
×
×
  • Utwórz nowe...