rfid: How to solve "A BUFFER IS NOT BIG ENOUGH" error ?

Hello Friends,

Step 2: Describe your environment

I m using an mfrc522 for one project.

My OS version : Windows 8.1 Pro Arduino IDE version : 1.8.3 MFRC522 Library version : 1.3.6 Arduino device : Arduino UNO 5V 16Mhz MFRC522 device : NXP RC522 25 02 (This text written on SMD IC) MIFARE Card : MIFARE1K|S50

Step 3: Describe the problem

The issue is; Normally my MRRC522 works well, but if we remove card from MFRC522 while operation is going on then some error occur like “Authentication fail”,“Timeout in Communication” then it will recover back.

But when it will shows “A BUFFER IS NOT BIG ENOUGH” then it will not recover back only shows this error every time.

I have tried mfrc522.PCD_Reset(); and mfrc522.PCD_Init(); function of Library but is not work, i have to compulsory restart the board altogether.

## Affected file(s) or example(s): My code is Here :

`#include <SPI.h>
#include <MFRC522.h>

constexpr uint8_t RST_PIN = 9;     // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 10;     // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;

byte sector         = 2;
byte blockAddr      = 8;
byte dataBlock[18]  ={0};
byte trailerBlock   = 11;
byte buffer[18]     ={0};
byte size           = sizeof(buffer);

/**
 * Initialize.
 */
void setup() 
{
    Serial.begin(9600); // Initialize serial communications with the PC
    while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    SPI.begin();        // Init SPI bus
    mfrc522.PCD_Init(); // Init MFRC522 card

    // Prepare the key (used both as key A and as key B)
    // using FFFFFFFFFFFFh which is the default at chip delivery from the factory
    for (byte i = 0; i < 6; i++)
    {
        key.keyByte[i] = 0xFF;
    }

    Serial.println(F("Scan a MIFARE Classic PICC to demonstrate read and write."));
    Serial.print(F("Using key (for A and B):"));
    dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
    Serial.println();
    
    Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
}

/**
 * Main loop.
 */
void loop() 
{
    // Look for new cards
    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;

    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;
        
    Serial.println(F("Authenticating using key A..."));
    status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK)
    {
        Serial.print(F("PCD_Authenticate() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
    }

    // Read data from the block
    Serial.print(F("Reading data from block "));
    Serial.print(blockAddr);
    Serial.println(F(" ..."));
    status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
    if (status != MFRC522::STATUS_OK) 
    {
        Serial.print(F("MIFARE_Read() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
    }
    Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
    dump_byte_array(buffer, 16); Serial.println();
    Serial.println();

    buffer[0]=buffer[0]+1;
    
    Serial.println(F("Authenticating again using key B..."));
    status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK)
    {
        Serial.print(F("PCD_Authenticate() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
    }
    
    // Write data to the block
    Serial.print(F("Writing data into block ")); Serial.print(blockAddr);
    Serial.println(F(" ..."));
    dump_byte_array(dataBlock, 16); Serial.println();
    status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, buffer, 16);
    if (status != MFRC522::STATUS_OK) {
        Serial.print(F("MIFARE_Write() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
    }
    Serial.println();

    // Read data from the block (again, should now be what we have written)
    Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
    Serial.println(F(" ..."));
    status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
    if (status != MFRC522::STATUS_OK) {
        Serial.print(F("MIFARE_Read() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
    }
    Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
    dump_byte_array(buffer, 16); Serial.println();
        
    // Check that data in block is what we have written
    // by counting the number of bytes that are equal
    Serial.println(F("Checking result..."));
    byte count = 0;
    for (byte i = 0; i < 16; i++) 
    {
        // Compare buffer (= what we've read) with dataBlock (= what we've written)
        if (buffer[i] == dataBlock[i])
            count++;
    }
    Serial.print(F("Number of bytes that match = ")); Serial.println(count);
    if (count == 16) 
    {
        Serial.println(F("Success :-)"));
    }
     
    else 
    {
        Serial.println(F("Failure, no match :-("));
        Serial.println(F("  perhaps the write didn't work properly..."));
    }
    Serial.println();
        
    // Dump the sector data
    Serial.println(F("Current data in sector:"));
    mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
    Serial.println();

    // Halt PICC
    mfrc522.PICC_HaltA();
    // Stop encryption on PCD
    mfrc522.PCD_StopCrypto1();
}

/**
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) 
{
    for (byte i = 0; i < bufferSize; i++) 
    {
        Serial.print(buffer[i] < 0x10 ? " 0" : " ");
        Serial.print(buffer[i], HEX);
    }
}

Steps to reproduce: Step 1 : Put card on RC522 Step 2 : Remove card from RC522 while operation is going on. do above steps faster again and again at some instant you will get “A Buffer is not big enough”.

Observed Results:

Authenticating using key A...
Reading data from block 8 ...
Data in block 8:
 01 02 03 04 05 06 07 08 08 09 FF 0B 0C 0D 0E 0F

Authenticating again using key B...
PCD_Authenticate() failed: Timeout in communication.
Authenticating using key A...
PCD_Authenticate() failed: Timeout in communication.
Authenticating using key A...
Reading data from block 8 ...
MIFARE_Read() failed: The CRC_A does not match.
Data in block 8:
 01 00 4A 24 05 06 07 08 08 09 FF 0B 0C 0D 0E 0F

Authenticating again using key B...
PCD_Authenticate() failed: Timeout in communication.

Authenticating using key A...
Reading data from block 8 ...
MIFARE_Read() failed: A buffer is not big enough.
Data in block 8:
 03 00 4A 24 05 06 07 08 08 09 FF 0B 0C 0D 0E 0F

Authenticating again using key B...
Writing data into block 8 ...
 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Reading data from block 8 ...
MIFARE_Read() failed: A buffer is not big enough.
Data in block 8:
 04 00 4A 24 05 06 07 08 08 09 FF 0B 0C 0D 0E 0F
Checking result...
Number of bytes that match = 1
Failure, no match :-(
  perhaps the write didn't work properly...

Current data in sector:
   2     11   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         10   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
          9   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
          8   04 00 4A 24  05 06 07 08  08 09 FF 0B  0C 0D 0E 0F  [ 0 0 0 ] 

Expected Results: want to solve crc error cleared without resetting/powering up the board ?

Appreciate all the help.

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 26

Most upvoted comments

The documentation tells you, that a call of mfrc522.MIFARE_Read(blockAddr, buffer, &size) change the value of size.

byte *bufferSize ///< Buffer size, at least 18 bytes. Also number of bytes returned if STATUS_OK. ) {

It seems that the documentation is not completely accurate and the value is changed also if STATUS_?? . Maybe somebody can have a closer look. Idk…

So if you call again mfrc522.MIFARE_Read(blockAddr, buffer, &size), size would maybe contain size=0. Means your buffer is too small and function fails. Conclusion: after you call mfrc522.MIFARE_Read(blockAddr, buffer, &size), have to reset size with size= sizeof(buffer);.

Best

@matonga

Hi, just wanted to say I had a similar problem. From time to time the MFRC522 module would not read cards anymore.

The solution was to move PCD_StopCrypto1. I was calling it after reading authenticated sector. Now I call PCD_StopCrypto1 before calling PICC_IsNewCardPresent. Yes, every time I call PICC_IsNewCardPresent, I call PCD_StopCrypto1 before, just in case.

while (1) {
  PCD_StopCrypto1 ( );
  if (PICC_IsNewCardPresent ( )) {
    // select the card, authenticate, read block, etc... halt the card
    // ...
  }
}

I left an automated rig passing four cards all night, and the next morning it was still reading the cards successfully. Hope this helps someone else because this library is great.

Super thanks for this!!! I almost gave up. This solved all my troubles.

What works for me is to stop the card before returning :


  status = mfrc522.MIFARE_Write(block, &data[16],  16);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("MIFARE_Write() failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));

    delay(100);
    mfrc522.PICC_HaltA();
    mfrc522.PCD_StopCrypto1();
    return false;
}

Hey @philippejadin & @AyushMarsian the ‘The CRC_A does not match’ error has nothing to do with the buffer size it means that the crc coproccesor on the MFRC522 chip must have found some error in the data(for some reasons that I don’t know, maybe manufacturing defect after all it is a very cheap chip). check this out ‘https://en.wikipedia.org/wiki/Cyclic_redundancy_check’ Though you can get over the problem without using the watchdog by using PCD_Reset() and PCD_init()

status = mfrc522.MIFARE_Read(block, buffer1, &len); if (status != MFRC522::STATUS_OK) { Serial.print(F("Reading failed: ")); Serial.println(mfrc522.GetStatusCodeName(status)); if(status==7) // status code 7 means the crc_a didn't match { mfrc522.PCD_Reset(); mfrc522.PCD_Init(); } return; } So now you don’t have to reset the whole board !

Massive thanks for this. I think you just saved me losing half my day.

@philippejadin Visit following URL for Download Arduino Code https://drive.google.com/open?id=1xYrcjoMfyNnwBfhA87w_-TNlJAarJDTX Can you explain me your problem, when you use watchdog for reset controller ? For more query related to this mail me on ankitghevariya358@gmail.com

Thank you so much @Rotzbua I’ll try this and come back

@AyushMarsian Up in the code byte buffer[18] ={0};

  1. What if you increase buffer size from 18 to…64, 128? Just to check.
  2. How many attempts are on your “Observed Results”?