Using MIFARE_Read() and MIFARE_Write()

I have successfully managed to use the MIFARE_Write function to store a servo's position onto an RFID card. I did this by converting the integer to a string, and copying it to a buffer which MIFARE_Write uses to store the data onto the card in the form of hex-represented ASCII ("31 37 38" representing "178").

After looking around in MFRC522.cpp, I have decided to make use of its MIFARE_Read function to "reverse" the process and obtain the hex values so they can be "reassembled" into an integer and used by the servo.

Here is what I have so far:

  //READ MODE
  } else {
    //read from tag
    block = 1;
    Serial.println(F("Authenticating using key A..."));
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK) {
      Serial.print(F("PCD_Authenticate() failed: "));
      Serial.println(mfrc522.GetStatusCodeName(status));
      return;
    }
    else Serial.println(F("PCD_Authenticate() success: "));

    // Read block
    status = mfrc522.MIFARE_Read(block, buffer, 16); //<----- I get an error here
    if (status != MFRC522::STATUS_OK) {
      Serial.print(F("MIFARE_Read() failed: "));
      Serial.println(mfrc522.GetStatusCodeName(status));
      return;
    }
    else Serial.println(F("MIFARE_Read() success: "));
    for (byte emptybuffer = 0; emptybuffer < 16; emptybuffer++)
    {
      analog_servo1_string[emptybuffer] = buffer[emptybuffer];
    }
    analog_servo1 = analog_servo1_string.toInt();
    servo1.write(analog_servo1);
  }

The error I get (at the line indicated by the comment) says:

error: invalid conversion from 'int' to 'byte* {aka unsigned char*}' [-fpermissive]

MFRC522.h:367:13: error:   initializing argument 3 of 'MFRC522::StatusCode MFRC522::MIFARE_Read(byte, byte*, byte*)' [-fpermissive]

StatusCode MIFARE_Read(byte blockAddr, byte *buffer, byte *bufferSize);

             ^

exit status 1
invalid conversion from 'int' to 'byte* {aka unsigned char*}' [-fpermissive]

According to the second line of the error, the issue seems to be specifically with the integer 16 that is being sent. But I don't understand why it wants "bufferSize" to be type "byte *", and I'm not sure how I would satisfy that either.

Thanks!

EDIT: I'd like to add that MIFARE_Write also asks for a byte as the "bufferSize" argument, yet, unlike MIFARE_Read, it does not return an error when 16 is provided.

I have not checked but I would assume that's because MIFARE_Read will actually modify that value to return how many bytes were actually written into your buffer so you need to pass the address of the buffer length.

the function will use that buffer length value to not overflow your buffer, and then if less bytes were returned store in that space how many it did write.

the function return code is probably an indication of success

J-M-L:
I have not checked but I would assume that's because MIFARE_Read will actually modify that value to return how many bytes were actually written into your buffer so you need to pass the address of the buffer length.

the function will use that buffer length value to not overflow your buffer, and then if less bytes were returned store in that space how many it did write.

the function return code is probably an indication of success

I ended up solving the problem, and what you just said basically sums up the solution I came to! :slight_smile:
I made a new byte variable and set its value to what I wanted (that's a new issue lol), and gave the function the address of said variable as the argument. This causes the program to compile with no errors, but when I use 16 as the buffer size the function displays an error via the Serial Monitor stating that "A buffer is not big enough". Changing this value to 18 results in "A MIFARE PICC responded with NAK", as does changing the value to sizeof(buffer). I have not put in any authentication code, so that may be my problem when getting the NAK error.

UPDATE: Correct me if I'm wrong, but I feel like trying to use MIFARE_Read is too direct/aggressive. Instead, I've been considering the use of the function PICC_DumpToSerial() as used in DumpInfo.ino. The only issue with that is of course that it is designed to display the contents of an RFID tag in the Serial Monitor, whereas I need the data of (a) certain block(s) to be sent to a servo. Would modifying MFRC522.cpp (the source file containing the aforementioned functions) to fit my needs be a viable idea, or does anyone know of a better approach?