How to convert Serial from RFID reader to String or Int?

Hey fellow programers.

First of all i would like to thank to this community. It has helped me many times searching through to posts and finding great examples and solutions you all posted.

However now i have a problem i can not figure out even with help of some older posts concerning my current problem.

Situation is following. I have arduino Mega2560 and RFID reader connected via RS232 to TTL.
When i swipe card and read serial port i get 10 bytes saving them in byte receivedchars[10].

Id of the card is located on positions 6,7 and 8.
In one example i got: receivedchars[4] = 136 ; receivedchars[5] = 86 ; receivedchars[6] = 153.
Now i should convert this to 8935065.
I can check this manually by printing those numbers in HEX getting: 88, 56, 99. If i put those numbers in windows calculator as HEX i get correct DEC number of 8935065.

How would you do this inside the code??
My current code is a little messy but this is it:



int TempRFID;
int startMarker = 2;  
int endMarker = 3;

const int numchars = 10;
byte receivedchars[numchars];
char numReceived = 0;
boolean newData = false;
//===================================
//=              SETUP              =
//===================================
// the setup routine runs once when you press reset:
void setup() {
  Serial.begin(115200);//moniteur serie
  Serial1.begin(9600);
  delay(3000);  //delai pour ouvrir COM8 apres televersement
  //========== LED =========
  Serial.println(F("+++ RxBinaryData +++"));

} // FIN de setup
//===================================
//=              LOOP               =
//===================================
void loop() {

  recvcharsWithStartEndMarkers();
  showNewData();


} //FIN de loop
//=================================================================================
//======================    AUTRES FONCTIONS     ==================================
//================================================================================
void recvcharsWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;

  byte rb;

  while (Serial1.available() > 0 && newData == false) {//Rx:  F2 40 05 65 FD 08 08 F6
    rb = Serial1.read();

    Serial.print(rb);


    if (recvInProgress == true) {
      if (rb != endMarker) {
        receivedchars[ndx] = rb;
        ndx++;
        if (ndx >= numchars) {
          ndx = numchars - 1;
        }
      }
      else {
        receivedchars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        numReceived = ndx;  // save the number for use when printing
        ndx = 0;
        newData = true;
      }
    }//FIN de recinprogress true

    else if (rb == startMarker) {
      recvInProgress = true;
    }
  }//FIN de while serial1
}// FIN DE FONCTION
//================================================================================
void showNewData() {
  if (newData == true) {
    Serial.print("Rx (HEX): ");
    for (char n = 0; n < numReceived; n++) {
      Serial.print(receivedchars[n]);
      Serial.print(' ');
    }//FIN de serial print N chars


    //pr2: display chars
    ///*
    Serial.print("\nchar START: "); Serial.print(startMarker); 
    Serial.print("\nchar 0: "); Serial.print(receivedchars[0]);
    Serial.print("\nchar 1: "); Serial.print(receivedchars[1]); 
    Serial.print("\nchar 2: "); Serial.print(receivedchars[2]); 
    Serial.print("\nchar 3: "); Serial.print(receivedchars[3]); 
    Serial.print("\nchar 4: "); Serial.print(receivedchars[4]); 
    Serial.print("\nchar 5: "); Serial.print(receivedchars[5]);
    Serial.print("\nchar 6: "); Serial.print(receivedchars[6]);
    Serial.print("\nchar 7: "); Serial.print(receivedchars[7]); 
    Serial.print("\nchar END: "); Serial.println(endMarker);
    //*/
    
    Serial.println();
    int nekaj = receivedchars[4];
    Serial.println(nekaj);
    TempRFID = 0;
    for (int n = 4; n < 7; n++){
      TempRFID = TempRFID*10 + (receivedchars[n] - '0');
      Serial.println(n); 
    }

    Serial.println(TempRFID);
//   

    newData = false;
  }//FIN de new data true
}// FIN DE FONCTION

Thank you all in advance

the math formula is (136 * 216) + (86 * 28) + 153
You can also do that through bit shifting in a uint32_t

1 Like
  uint32_t ID = receivedchars[4];
  ID <<= 8;
  ID += receivedchars[5];
  ID <<= 8;
  ID += receivedchars[6];
1 Like

Thank you both for very quick response. Both suggestions working like a charm.

I have one more question. I wanna move this code to ESP32, but when i do i start getting funny characters instead of what i receive with Arduino Mega. Everything else is the same... Is there some difference in Serial input?

Post the code and explain the setup

An ESP is 3.3V so that could be a (unwelcome) difference if you send 5V into the pins….

You are correct, it was power or lack of it to put it correctly.
I have to power RFID and RS232 from other power source(Arduino mega).
Seems like converter does not work properly on 3V3.
Now it works like a charm on 5V.
However i should now add voltage divider so i do not burn my ESP.

Thanks for help