Weird Serial outputs from RFID tag data

Hello and good day! I am trying to make a code where I read data off an RFID then turn it into a integer and write a new integer back to the RFID.

The reason why I want it converted to integer is because I want to store monetary data on it, like a balance or load of some sort, and I want it to, for every time you use it, it deducts a certain amount of money out of your RFID.

I have this code from this public code that was written to allow people to write and then read data on to an RFID.

This is a cleaned-up version of the above code with my code that tries to do what I aimed to do as stated above.

#include <SPI.h>//include the SPI bus library
#include <MFRC522.h>//include the RFID reader library

#define SS_PIN 10  //slave select pin
#define RST_PIN 5  //reset pin
MFRC522 mfrc522(SS_PIN, RST_PIN);        // instatiate a MFRC522 reader object.
MFRC522::MIFARE_Key key;//create a MIFARE_Key struct named 'key', which will hold the card information


void setup() {
        Serial.begin(9600);        // Initialize serial communications with the PC
        SPI.begin();               // Init SPI bus
        mfrc522.PCD_Init();        // Init MFRC522 card (in case you wonder what PCD means: proximity coupling device)
        Serial.println("Scan a MIFARE Classic card");
       
        for (byte i = 0; i < 6; i++) {
                key.keyByte[i] = 0xFF;//keyByte is defined in the "MIFARE_Key" 'struct' definition in the .h file of the library
        }

}
char u[16] = {}; //For storing the read content
int block = 2;//this is the block number we will write into and then read. Do not write into 'sector trailer' block, since this can make the block unusable.
byte blockcontent[16] = {"111111111111112"};// There are 14 1s                    
byte readbackblock[18];//This array is used for reading out a block. The MIFARE_Read method requires a buffer that is at least 18 bytes to hold the 16 bytes of a block.

void loop()
{

        /*****establishing contact with a tag/card****/
        
    // Look for new cards (in case you wonder what PICC means: proximity integrated circuit card)
  if ( ! mfrc522.PICC_IsNewCardPresent()) {//if PICC_IsNewCardPresent returns 1, a new card has been found and we continue
    return;//if it did not find a new card is returns a '0' and we return to the start of the loop
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {//if PICC_ReadCardSerial returns 1, the "uid" struct (see MFRC522.h lines 238-45)) contains the ID of the read card.
    return;//if it returns a '0' something went wrong and we return to the start of the loop
  }
         Serial.println("card selected");
         
         /****writing and reading a block on the card****/
         
         writeBlock(block, blockcontent);//the blockcontent array is written into the card block
                  
         readBlock(block, readbackblock);//read the block back
         Serial.print("read block: ");
         for (int j=0 ; j<16 ; j++)//print the block contents
         {
           Serial.write(readbackblock[j]);//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor
           u[j] =Serial.read();//Attempts to keep each byte in array u
         }
         String s((char*)u);//Tries to turn char array into string
         int n = s.toInt(); //Turns string into int
         Serial.println("");
         Serial.println(s);//Returns 16 squares, and blockcontent[16]
         Serial.println((String)u);//Returns 16 squares, and blockcontent[16]
         Serial.println(n);//Returns 0
         Serial.println("");
}

int writeBlock(int blockNumber, byte arrayAddress[]) //function for writing
{
  //this makes sure that we only write into data blocks. Every 4th block is a trailer block for the access/security info.
  int largestModulo4Number=blockNumber/4*4;
  int trailerBlock=largestModulo4Number+3;//determine trailer block for the sector
  if (blockNumber > 2 && (blockNumber+1)%4 == 0){Serial.print(blockNumber);Serial.println(" is a trailer block:");return 2;}//block number is a trailer block (modulo 4); quit and send error code 2
  Serial.print(blockNumber);
  Serial.println(" is a data block:");
  
  /*****authentication of the desired block for access****/
  byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
         Serial.print("PCD_Authenticate() failed: ");
         Serial.println(mfrc522.GetStatusCodeName(status));
         return 3;//return "3" as error message
  }
  /****writing the block****/
        
  status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16);
  if (status != MFRC522::STATUS_OK) {
           Serial.print("MIFARE_Write() failed: ");
           Serial.println(mfrc522.GetStatusCodeName(status));
           return 4;//return "4" as error message
  }
  Serial.println("block was written");
}


int readBlock(int blockNumber, byte arrayAddress[]) //Function for reading the block that we wrote
{
  int largestModulo4Number=blockNumber/4*4;
  int trailerBlock=largestModulo4Number+3;//determine trailer block for the sector

  /*****authentication of the desired block for access****/
  byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
         Serial.print("PCD_Authenticate() failed (read): ");
         Serial.println(mfrc522.GetStatusCodeName(status));
         return 3;//return "3" as error message
  }

  /****reading a block****/
        
  byte buffersize = 18;//we need to define a variable with the read buffer size, since the MIFARE_Read method below needs a pointer to the variable that contains the size... 
  status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize);//&buffersize is a pointer to the buffersize variable; MIFARE_Read requires a pointer instead of just a number
  if (status != MFRC522::STATUS_OK) {
          Serial.print("MIFARE_read() failed: ");
          Serial.println(mfrc522.GetStatusCodeName(status));
          return 4;//return "4" as error message
  }
  Serial.println("block was read");
}

The problem lies within this part of the code:

writeBlock(block, blockcontent);//the blockcontent array is written into the card block
                  
         readBlock(block, readbackblock);//read the block back
         Serial.print("read block: ");
         for (int j=0 ; j<16 ; j++)//print the block contents
         {
           Serial.write(readbackblock[j]);//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor
           u[j] =Serial.read();//Attempts to keep each byte in array u
         }
         String s((char*)u);//Tries to turn char array into string
         int n = s.toInt(); //Turns string into int
         Serial.println("");
         Serial.println(s);//Returns 16 squares, and blockcontent[16]
         Serial.println((String)u);//Returns 16 squares, and blockcontent[16]
         Serial.println(n);//Returns 0
         Serial.println("");

This part tries to take the data that it reads off the RFID and put it into a char array labelled u using Serial.read().
I also try to turn it into a string and turn the subsequent string into an int, which I know is not the cleanest.

The input data is blockcontent[16] which is {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2} with 14 1s and 1 2 at the end.

The Serial.write() returns the correct data but the Serial.println() returns this:

⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮111111111111112
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮111111111111112
0

The mirrored question marks are all small vertical rectangles that look like [] this and there are 16 of them.

I tried messing around with the size of char array u, trying out u[17] and u[15] and, at the output, only the squares get outputted.

I also suspect that the .toInt() function did not work because the first character the function encounters is the squares so it doesn't convert the rest of it.

I personally don't even know if throwing data using Serial.write() and catching it using Serial.read() is even reliable...I would really like some advice on this.

Thank you in advance! All help would be appreciated!

For one thing i think this is wrong

//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor

Serial.write, writes 'binary' data to the serial port. for acsii you need Serial.print()

notsolowki:
For one thing i think this is wrong

//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor

Serial.write, writes 'binary' data to the serial port. for acsii you need Serial.print()

Not quite; if you try to print a char or byte, it will be treated as a number so you will see 65 instead of 'A'

    u[j] = Serial.read(); //Attempts to keep each byte in array u

That fills an element of u with -1 unless you send something from serial monitor.

Maybe that is supposed to be

    u[j] = readbackblock[j]; //Attempts to keep each byte in array u

Note 1:
Using String (capital S) objects on microcontrollers as used in Arduinos is in general a bad idea; it might result in unexpected behaviour at run-time.

Note 2:
Problem more than likely not in the code that you suspect :smiley:

Note 3:
Do not use single character variable names for global variables; there are 119 letters 'u' in your code and not all of them refer to the array.

CaptainCorndoge:
I personally don't even know if throwing data using Serial.write() and catching it using Serial.read() is even reliable...I would really like some advice on this.

Not only not reilable. NOT POSSIBLE. Serial.write() sends characters to the Serial Monitor screen. Serial.read() gets characters that YOU typed into the text entry line of Serial Monitor. Serial.read() does NOT read back the characters you sent to the screen.
Try this:

         for (int j=0 ; j<16 ; j++)//print the block contents
         {
           u[j] = readbackblock[j] + '0';  // Convert integer from 0 to 9 to ASCII digit
           Serial.write(u[j]);
         }