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
The documentation tells you, that a call of
mfrc522.MIFARE_Read(blockAddr, buffer, &size)change the value ofsize.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),sizewould maybe containsize=0. Means your buffer is too small and function fails. Conclusion: after you callmfrc522.MIFARE_Read(blockAddr, buffer, &size), have to resetsizewithsize= sizeof(buffer);.Best
@matonga
Super thanks for this!!! I almost gave up. This solved all my troubles.
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};