Rfid rc522: store content in a variable

Hello, everybody!
My rfid card have two values recorded in it. Each one is a number with 11 positions.
The exemple sketch "rfid_read_personal_data" show the correct values on the serial monitor, but I can't store these values in a variable.
Exemple of content in rfid card (as serial monitor shows):
Value 1:
00376754486
Value 2
00709081323

To show one of these values on serial monitor, the exemple sketch uses this:

  byte buffer1[18];

  block = 4;
  len = 18;

  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  status = mfrc522.MIFARE_Read(block, buffer1, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Reading failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  for (uint8_t i = 0; i < 16; i++)
  {
    if (buffer1[i] != 32)
    {
      Serial.write(buffer1[i]);
    }
  }

How can I store each value in the respective variable? Char, string or int would be perfect to my use.

Thank you very mutch!

When you receive a character put it in an array of chars and increment the index to the array ready for the next character and put a '\0' in there to turn it into a C style string. Keep doing this until you get an end of data character, probably a '\r' or '\n'. Your data is now in the string. It could be copied to another string, compared to another string or converted to another data type if required

You didn't posted a complete sketch and from your description it is unclear to me
which lines of code print the 11-digit number?

Is it the

Serial.println(mfrc522.GetStatusCodeName(status));

or is it the

 for (uint8_t i = 0; i < 16; i++)
  {
    if (buffer1[i] != 32)
    {
      Serial.write(buffer1[i]);
    }
  }

It would be very helpful if you first would add some comment that is printed on the serial monitor too
and then post the copy&pasted characters that you receive in the serial-monitor

by addinng comment I mean something like this

  Serial.print( F("content of buffer is#") );
  for (uint8_t i = 0; i < 16; i++)
  {
    if (buffer1[i] != 32)
    {
      Serial.write(buffer1[i]);
    }
  }  
  Serial.println( F("#") );

So I just can guess but not know for sure that your output would look like this

content of buffer is#00376754486#

The leading "#" and the trailing "#" would clearly indicate what characters are printed

As you seem a newcomer I recommend using a library like PString or SafeString to work with strings

as you didn't posted your full sketch I can't compile the code-snippet
Which means that a type stays undiscovered

#include "SafeString.h" // my SafeString library from the Arduino library manager

const size_t maxStrLength = 18; 
createSafeString(myRFIDStr, maxCmdLength + 1); 

byte buffer1[maxStrLength];

  block = 4;
  len = 18;

  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  status = mfrc522.MIFARE_Read(block, buffer1, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Reading failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  myRFIDStr = "";
  for (uint8_t i = 0; i < 16; i++)
  {
    if (buffer1[i] != 32)
    {
      Serial.write(buffer1[i]);
      myRFIDStr += buffer1[i];
    }
  }

  Serial.print( F("content of buffer is#") );
  for (uint8_t i = 0; i < 16; i++)
  {
    if (buffer1[i] != 32)
    {
      Serial.write(buffer1[i]);
    }
  }  
  Serial.println( F("#") );

  Serial.print( F("content of variable myRFIDStr is #") );
  Serial.print(myRFIDStr);
  Serial.println( F("#") );
  

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.