RFID detecteert de eerste kaartboete, maar blijft een seconde hangen

Ik ben een programma aan het schrijven dat:

  1. Leest string (één regel) van de com poort
  2. Wacht tot een RFID-kaart aanwezig is
  3. leest een blok van de kaart

Hier is de code (deze gebruikt deze MFRC522-bibliotheek, ook de code is gebaseerd op makecourse.com RFID-zelfstudie):

#include  //include the SPI bus library
#include  //include the RFID reader library

#define SS_PIN 10  //slave select pin
#define RST_PIN 9  //reset pin
MFRC522 mfrc522(SS_PIN, RST_PIN);       //instatiate a MFRC522 reader object.
MFRC522::MIFARE_Key key; //create a MIFARE_Key struct named 'key', which will hold the card information

byte readbackblock[18]; //This array is used for reading out a block. The MIFARE_Read method requires a buffer that is at least 18 bytes to hold the 16 bytes of a block.

String inString = "";//COM port incoming data buffer

void setup() {
  Serial.begin(9600);       //Initialize serial communications with the PC
  SPI.begin();              //Init SPI bus
  mfrc522.PCD_Init();       //Init MFRC522 card (in case you wonder what PCD means: proximity coupling device)
  Serial.println("Scan a MIFARE Classic card");

 //Prepare the security key for the read and write functions - all six key bytes are set to 0xFF at chip delivery from the factory.
 //Since the cards in the kit are new and the keys were never defined, they are 0xFF
 //if we had a card that was programmed by someone else, we would need to know the key to be able to access it. This key would then need to be stored in 'key' instead.

  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;//keyByte is defined in the "MIFARE_Key" 'struct' definition in the .h file of the library
  }

}

void loop() {
  while (Serial.available() > 0) {
    int inChar = Serial.read();

    if (inChar != '\n') {
      inString += (char)inChar;
    } else {
     //New line

      Serial.println("Start");
      while (!initcard());//Detecting new card
      Serial.println("CardFound");
      readBlockToCom(2);//Reading a block
      Serial.println("Done");
      inString = "";
    }
  }
}

bool initcard()
{
 //Look for new cards (in case you wonder what PICC means: proximity integrated circuit card)
  if ( ! mfrc522.PICC_IsNewCardPresent()) {//if PICC_IsNewCardPresent returns 1, a new card has been found and we continue
    return false; //if it did not find a new card is returns a '0' and we return to the start of the loop
  }

 //Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {//if PICC_ReadCardSerial returns 1, the "uid" struct (see MFRC522.h lines 238-45)) contains the ID of the read card.
    return false; //if it returns a '0' something went wrong and we return to the start of the loop
  }
  return true;
}    

void readBlockToCom(int number)
{
  readBlock(number, readbackblock);//read the block back
  Serial.print("read block: ");
  for (int j = 0 ; j < 16 ; j++) //print the block contents
  {
    Serial.write (readbackblock[j]);//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor
  }
  Serial.println("");
}

int readBlock(int blockNumber, byte arrayAddress[]) 
{
  int largestModulo4Number=blockNumber/4*4;
  int trailerBlock=largestModulo4Number+3;//determine trailer block for the sector

  /*****************************************authentication of the desired block for access***********************************************************/
  byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  //byte PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);
  //this method is used to authenticate a certain block for writing or reading
  //command: See enumerations above -> PICC_CMD_MF_AUTH_KEY_A   = 0x60 (=1100000),     //this command performs authentication with Key A
  //blockAddr is the number of the block from 0 to 15.
  //MIFARE_Key *key is a pointer to the MIFARE_Key struct defined above, this struct needs to be defined for each block. New cards have all A/B= FF FF FF FF FF FF
  //Uid *uid is a pointer to the UID struct that contains the user ID of the card.
  if (status != MFRC522::STATUS_OK) {
         Serial.print("PCD_Authenticate() failed (read): ");
         Serial.println(mfrc522.GetStatusCodeName(status));
         return 3;//return "3" as error message
  }
  //it appears the authentication needs to be made before every block read/write within a specific sector.
  //If a different sector is being authenticated access to the previous one is lost.


  /*****************************************reading a block***********************************************************/

  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... 
  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
  if (status != MFRC522::STATUS_OK) {
          Serial.print("MIFARE_read() failed: ");
          Serial.println(mfrc522.GetStatusCodeName(status));
          return 4;//return "4" as error message
  }
  Serial.println("block was read");
}

Het probleem dat ik heb is dat

  1. Ik stuur eerste regel via com poort
  2. Pas een kaart toe op RFID-RC522
  3. Het programma leest het blok goed
  4. Maar dan stuur ik de tweede regel
  5. Arduino hangt op kaart detecteren

Dit is de uitvoer die ik ontvang:

Scan a MIFARE Classic card
Start
CardFound
block was read
read block: makecourse_____
Done
Start

Ik heb geprobeerd de kaart te verwijderen en opnieuw toe te passen of een andere kaart toe te passen, maar hij bevriest nog steeds op kaartdetectie, maar na enige tijd werkt hij wel.

0

1 antwoord

Dit moet je aan het einde van readBlock() toevoegen:

// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
4
toegevoegd
Deze code is toegevoegd na readBlockToCom (2); en het werkte. Dank je.
toegevoegd de auteur Richard Rast, de bron