Arduino inconsistent byte variable values

Hey guys, I’ve got a really weird problem thats got me stumped.

I’ve got a setup where I’m trying to send packets of data from a C# app on my computer over the network to an ESP8266, and then pass the packets on to an arduino over Serial from the ESP. I’ve got the application talking to the ESP and the ESP is sending the packets over serial, and I’ve verified that parts all working correctly.

But I keep getting really weird stuff happening when the packet gets to the arduino side. Sometimes the packet is received perfectly with no problems, and sometimes its like one of the received bytes is off by one and I get invalid data.

Here is my arduino code (I have quite a few serial prints in here as I was trying to debug it):

#define DEBUG
#include <SoftwareSerial.h>
SoftwareSerial ESPserial(2, 3); // RX | TX
 
void setup() 
{
    Serial.begin(115200);     // communication with the host computer
    //while (!Serial)   { ; }
 
    // Start the software serial for communication with the ESP8266
    ESPserial.begin(115200);
     
    Serial.println("Arduino Setup Complete.");        
}
 
void loop() 
{
    // listen for communication from the ESP8266 
    if ( ESPserial.available() )   
    {
      int maxBuffer = 1024;
      byte bufferBytes[maxBuffer];  
          #ifdef DEBUG 
          Serial.print("bufferBytes[0] = ");
          Serial.println(bufferBytes[0]);
          Serial.print("bufferBytes[1] = ");
          Serial.println(bufferBytes[1]);
          Serial.print("bufferBytes[2] = ");
          Serial.println(bufferBytes[2]);
          Serial.print("bufferBytes[3] = ");
          Serial.println(bufferBytes[3]);
          Serial.print("bufferBytes[4] = ");
          Serial.println(bufferBytes[4]);
          Serial.print("bufferBytes[5] = ");
          Serial.println(bufferBytes[5]);
          Serial.print("bufferBytes[6] = ");
          Serial.println(bufferBytes[6]);
          Serial.print("bufferBytes[7] = ");
          Serial.println(bufferBytes[7]);
          Serial.print("bufferBytes[8] = ");
          Serial.println(bufferBytes[8]);
          #endif
      
      ESPserial.readBytes(bufferBytes,maxBuffer);
      int headerBytePos = 0;
      for (int i = 0; i <  maxBuffer; i++)
      {
        if (bufferBytes[i] == 2)
        {
          headerBytePos = i;
          #ifdef DEBUG 
          Serial.print("headerBytePos = ");
          Serial.println(headerBytePos);
          #endif
          break;
        }
      }
      unsigned int messageLength = calculateLength(bufferBytes[headerBytePos+1], bufferBytes[headerBytePos+2]);
      #ifdef DEBUG 
      Serial.print("messageLength = ");
      Serial.println(messageLength);
      #endif
      byte CRCByte = bufferBytes[headerBytePos + 3 + messageLength];
      #ifdef DEBUG 
      Serial.print("CRCByte = ");
      Serial.println(CRCByte);
      #endif
      byte messageData[messageLength];
      for (int i =0; i < messageLength; i++)
      {
        messageData[i] = bufferBytes[headerBytePos + 3 + i];         
      }

          #ifdef DEBUG 
          Serial.print("messageData[0] = ");
          Serial.println(messageData[0]);
          Serial.print("messageData[1] = ");
          Serial.println(messageData[1]);
          Serial.print("messageData[2] = ");
          Serial.println(messageData[2]);
          Serial.print("messageData[3] = ");
          Serial.println(messageData[3]);
          Serial.print("messageData[4] = ");
          Serial.println(messageData[4]);          
          #endif
      
      if (CRCByte == calculateCRC(messageData, messageLength))
      {
        //Valid Packet Data
        #ifdef DEBUG      
        Serial.println("Valid Packet Recieved");
        Serial.write(messageData, messageLength);
        #endif
      }
      else
      {
        #ifdef DEBUG 
        Serial.println("Invalid Data Recieved");
        #endif
      }

      
    }
    
}

//Calculates the CRC byte of a package
byte calculateCRC (byte package[], int messageLength)
{
  byte returnByte =0;
  for (int i =0; i < messageLength; i++)
  {
    #ifdef DEBUG 
    Serial.print("returnByte = ");
    Serial.print(returnByte);
    Serial.print(" ^ ");
    Serial.print(package[i]);
    Serial.print(" = "); 
    #endif    
    returnByte = returnByte ^ package[i]; 
    #ifdef DEBUG     
    Serial.println(returnByte);
    #endif   
  }  

  #ifdef DEBUG 
  Serial.print("Calculated CRC = ");
  Serial.println(returnByte);
  #endif
  return returnByte;
}

//Calculates packet length from MSB and LSB length bytes
unsigned int calculateLength (byte MSB, byte LSB)
{  
  #ifdef DEBUG 
  Serial.print("MSB = ");
  Serial.println(MSB);
  Serial.print("LSB = ");
  Serial.println(LSB);
  #endif
  
  return (MSB * 256) + LSB;
}

The packet I sent from the PC is 9 bytes
{
2 ← packet header byte
0 ← packet length MSB
5 ← packet length LSB
49 ← data
50 ← data
51 ← data
52 ← data
53 ← data
49 ← CRC byte (for error checking the data was correct)
}

Here is what the Serial output looks like when this works as intended:

bufferBytes[0] = 2
bufferBytes[1] = 0
bufferBytes[2] = 5
bufferBytes[3] = 49
bufferBytes[4] = 50
bufferBytes[5] = 51
bufferBytes[6] = 52
bufferBytes[7] = 52
bufferBytes[8] = 49
headerBytePos = 0
MSB = 0
LSB = 5
messageLength = 5
CRCByte = 49
messageData[0] = 49
messageData[1] = 50
messageData[2] = 51
messageData[3] = 52
messageData[4] = 53
returnByte = 0 ^ 49 = 49
returnByte = 49 ^ 50 = 3
returnByte = 3 ^ 51 = 48
returnByte = 48 ^ 52 = 4
returnByte = 4 ^ 53 = 49
Calculated CRC = 49
Valid Packet Recieved
12345

But then often when I send the exact same packet I get an output like this, which seems to make no sense:

bufferBytes[0] = 2
bufferBytes[1] = 0
bufferBytes[2] = 5
bufferBytes[3] = 49
bufferBytes[4] = 50
bufferBytes[5] = 51
bufferBytes[6] = 52
bufferBytes[7] = 53
bufferBytes[8] = 49
headerBytePos = 0
MSB = 0
LSB = 5
messageLength = 5
CRCByte = 48
messageData[0] = 49
messageData[1] = 50
messageData[2] = 51
messageData[3] = 52
messageData[4] = 53
returnByte = 0 ^ 49 = 49
returnByte = 49 ^ 50 = 3
returnByte = 3 ^ 51 = 48
returnByte = 48 ^ 52 = 4
returnByte = 4 ^ 53 = 49
Calculated CRC = 49
Invalid Data Recieved

In this example CRCByte somehow became 48 but it was clearly 49 on the incoming packet.
CRCByte = byfferBytes[headerBytePos(0) + 3 + messageLength(5)] = bufferBytes[8] = 49 = 48 ??? WTF

And sometimes it also happens where its incorrect the other way:

bufferBytes[0] = 2
bufferBytes[1] = 0
bufferBytes[2] = 5
bufferBytes[3] = 49
bufferBytes[4] = 50
bufferBytes[5] = 51
bufferBytes[6] = 52
bufferBytes[7] = 53
bufferBytes[8] = 48
headerBytePos = 0
MSB = 0
LSB = 5
messageLength = 5
CRCByte = 49
messageData[0] = 49
messageData[1] = 51
messageData[2] = 51
messageData[3] = 52
messageData[4] = 53
returnByte = 0 ^ 49 = 49
returnByte = 49 ^ 51 = 2
returnByte = 2 ^ 51 = 49
returnByte = 49 ^ 52 = 5
returnByte = 5 ^ 53 = 48
Calculated CRC = 48
Invalid Data Recieved

In this example it looks like the 9th byte in the packet came in as 48 (bufferBytes[8] = 48) but CRCByte became 49? and the calculated CRC byte was wrong because it had the wrong message data {49, 51, 51, 52, 53}

I really don’t understand whats happening here its like my bytes are off by 1 in some instances? Does anyone have any insight?

You're printing values from the bufferBytes array before initializing it or assigning values to it.

Whoops, good catch.
That makes things clearer, but I’m still getting some weird data:

bufferBytes[0] = 2
bufferBytes[1] = 0
bufferBytes[2] = 5
bufferBytes[3] = 49
bufferBytes[4] = 51
bufferBytes[5] = 51
bufferBytes[6] = 52
bufferBytes[7] = 53
bufferBytes[8] = 49
headerBytePos = 0
MSB = 0
LSB = 5
messageLength = 5
CRCByte = 49
messageData[0] = 49
messageData[1] = 51
messageData[2] = 51
messageData[3] = 52
messageData[4] = 53
returnByte = 0 ^ 49 = 49
returnByte = 49 ^ 51 = 2
returnByte = 2 ^ 51 = 49
returnByte = 49 ^ 52 = 5
returnByte = 5 ^ 53 = 48
Calculated CRC = 48
Invalid Data Recieved

bufferBytes[4] = 51 should be 50.

Is it maybe that my baud rate is too high (115200)?

Yes, definitely a baud rate issue combined with that serial output being in the wrong spot through me off. Thanks for the help. SOLVED