How to Write an integer Value to MIFARE 1KB and Read it back?

Hello

i have been trying for a while to write an integer called yourvalue to MIFARE 1KB
card but i didn't reach a way to do that . can anyone give me a hand with my code?

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

/*Using Hardware SPI of Arduino */
/*MOSI (11), MISO (12) and SCK (13) are fixed */
/*You can configure SS and RST Pins*/
#define SS_PIN 3  /* Slave Select Pin */
#define RST_PIN 2  /* Reset Pin */

/* Create an instance of MFRC522 */
MFRC522 mfrc522(SS_PIN, RST_PIN);
/* Create an instance of MIFARE_Key */
MFRC522::MIFARE_Key key;          

/* Set the block to which we want to write data */
/* Be aware of Sector Trailer Blocks */
int blockNum = 2;  
/* Create an array of 16 Bytes and fill it with data */
/* This is the actual data which is going to be written into the card */
byte blockData [16] = {"Electronics-Hub-"};

/* Create another array to read data from Block */
/* Legthn of buffer should be 2 Bytes more than the size of Block (16 Bytes) */
byte bufferLen = 18;
byte readBlockData[18];
int yourValue;
MFRC522::StatusCode status;

void setup() 
{
  /* Initialize serial communications with the PC */
  Serial.begin(9600);
  /* Initialize SPI bus */
  SPI.begin();
  /* Initialize MFRC522 Module */
  mfrc522.PCD_Init();
  Serial.println("Scan a MIFARE 1K Tag to write data...");
}

void loop()
{
  /* Prepare the ksy for authentication */
  /* All keys are set to FFFFFFFFFFFFh at chip delivery from the factory */
  for (byte i = 0; i < 6; i++)
  {
    key.keyByte[i] = 0xFF;
  }
  /* Look for new cards */
  /* Reset the loop if no new card is present on RC522 Reader */
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  
  /* Select one of the cards */
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  Serial.print("\n");
  Serial.println("**Card Detected**");
  /* Print UID of the Card */
  Serial.print(F("Card UID:"));
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
  }
  Serial.print("\n");
  /* Print type of card (for example, MIFARE 1K) */
  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  Serial.println(mfrc522.PICC_GetTypeName(piccType));
         
   /* Call 'WriteDataToBlock' function, which will write data to the block */
   Serial.print("\n");
   Serial.println("Writing to Data Block...");
   WriteDataToBlock(blockNum, blockData);
   
   /* Read data from the same block */
   Serial.print("\n");
   Serial.println("Reading from Data Block...");
   ReadDataFromBlock(blockNum, readBlockData);
   /* If you want to print the full memory dump, uncomment the next line */
   //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
   
   /* Print the data read from block */
   Serial.print("\n");
   Serial.print("Data in Block:");
   Serial.print(blockNum);
   Serial.print(" --> ");
   yourValue = ((int)readBlockData[1])<<8 | readBlockData[0];
   Serial.write(yourValue);
//   for (int j=0 ; j<16 ; j++)
//   {
//     Serial.write(readBlockData[j]);
//   }
   Serial.print("\n");
}



void WriteDataToBlock(int blockNum, byte blockData[]) 
{
  /* Authenticating the desired data block for write access using Key A */
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK)
  {
    Serial.print("Authentication failed for Write: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println("Authentication success");
  }
blockData[0] = (byte)(1500 & 0xFF);
blockData[1] = (byte)(1500 >> 8);  
  
  /* Write data to the block */
  status = mfrc522.MIFARE_Write(blockNum, blockData, 16);
  if (status != MFRC522::STATUS_OK)
  {
    Serial.print("Writing to Block failed: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println("Data was written into Block successfully");
  }
  
}

void ReadDataFromBlock(int blockNum, byte readBlockData[]) 
{
  /* Authenticating the desired data block for Read access using Key A */
  byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));

  if (status != MFRC522::STATUS_OK)
  {
     Serial.print("Authentication failed for Read: ");
     Serial.println(mfrc522.GetStatusCodeName(status));
     return;
  }
  else
  {
    Serial.println("Authentication success");
  }

  /* Reading data from the Block */
  status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
  if (status != MFRC522::STATUS_OK)
  {
    Serial.print("Reading failed: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println("Block was read successfully");  
  }
  
}

Have you tried the library‘s example with your setup?

Yes and it is working fine. The only problem is it only save and read byte array which I can't deal with.

À byte array is just the memory representation of any structured data…

Bytes are stored at Address or your int / size of your int

You are completely right but mfrc522.MIFARE_Write function only write a byte array. If it is possible to use it to store int, that would be great.

I have not looked in details and I’m on my iPhone so can’t test, but just try this

Assuming you want to use the array

byte blockData [16];
int myData = 42;
int blockNum = 2;  
memset(blockData, 0, sizeof blockData); // clear the buffer https://www.cplusplus.com/reference/cstring/memset/?kw=memset
memcpy(blockData, &myData, sizeof myData);  // copy the bytes representing our integer at the start of the array https://www.cplusplus.com/reference/cstring/memcpy/
WriteDataToBlock(blockNum, blockData);  // write the block

But I’m pretty sure that this would work too

int myData = 42;
int blockNum = 2;  
WriteDataToBlock(blockNum, (byte*) &myData);

In the latter version You get the 2 or 4 bytes of your int (little endian order) written in the block number 2 and the rest to pad to 16 bytes is random garbage that was in memory after your integer whilst in the former you get your int in the same way and then only 0 because we had emptied the buffer before

But if the data after your int does not matter, writing 0 or garbage is just the same, you won’t use it anyway… so I’ll go for the latter

When you read back the buffer, you would use memcpy() in the reverse way to move the bytes into your int (you need the buffer for this)

byte readBlockData[18];
int myData;
ReadDataFromBlock(blockNum, readBlockData);
memcpy(&myData, readBlockData, sizeof myData);
1 Like

Sorry for my late reply and Many many thanks for you help. i used the second approach "WriteDataToBlock(blockNum, (byte*) &myData);" and it just works fine .

Really appreciated.

Great! Have fun

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