If I use one serial port it doesn't work unless it also uses software serial

I have a program that talks to an application. I use a homemade mega 2560. I am using Serial0 for the main comms. Everything starts out good after the first three messages arrive and I respond to the messages until after the third message. Then its like I'm getting erroneous data but according to wireshark I am not. Here is the kicker. If I use a second uart, say Serial2 to print debug statements, it still does not work properly BUT.... if I use a software Serial to print debug statements, the program will work as designed. Bottom line, if I try to use one or more than one Serial uart (I'm using Max232 chips on the board) it will not work properly UNTIL I setup a software serial, then any uart port I use works properly. I will attach both ino files. You can compare and see that the "Test2" file is only using Serial and will not work properly but the one labeled "Works" is the exact same except I added a Software Serial for debugging...it works... Can someone help me wrap my head around this?

test2.ino (7.8 KB)
works.ino (8.5 KB)

What is so hard about posting code as code?

Test2.ino

// Test2.ino
#include <EEPROM.h>
const int HEADER_SIZE = 16;

uint32_t mySeqID = 1;
uint16_t Ones = 1;
uint16_t Tens = 1;
uint64_t address = 2;
byte lastposition = 0;
byte ETGTadd = 0xAA; //EEPROM location to hold address 170
byte EVersion = 0x00; //Version that is read from EEPROM
byte EVerLoc = 0xA8; //Version location in EEPROM 168
byte Version = 0x04; //Keep versions even numbers for easier Android coding
char ver1 = '0';
char ver2 = '4';
uint64_t naddress = 2;

void getEEAddress() {

  EEPROM.get(ETGTadd, naddress);

  if (naddress > 2800 || naddress == 0) //update address to 2 in EEPROM if not a correct address

  {
    address = 2;
    EEPROM.put(ETGTadd, address);
  }
  else {

    EEPROM.put(ETGTadd, naddress);
    address = naddress;
  }
}

void setup() {

  Serial.begin(9600);
  EEPROM.update(EVerLoc, ver1);
  EEPROM.update(EVerLoc + 1, ver2);
  getEEAddress();
  delay(500);
  Serial.write(0x63);
}

typedef union
{
  float mph;
  uint8_t bytes[4];
} FLOATUNION_t;

void caps(uint32_t seqID) {
  byte bufflen = 31;
  byte XBUFFER[bufflen] = "";
  //Message header
  XBUFFER[0] = 0x08;
  XBUFFER[1] = 0x3F;
  XBUFFER[2] = Ones >> 8;
  XBUFFER[3] = Ones;
  XBUFFER[4] = Tens >> 8;
  XBUFFER[5] = Tens;
  XBUFFER[6] = mySeqID >> 24;
  XBUFFER[7] = mySeqID >> 16;
  XBUFFER[8] = mySeqID >> 8;
  XBUFFER[9] = mySeqID;
  XBUFFER[10] = 0x00;
  XBUFFER[11] = 0x00;
  XBUFFER[12] = 0x00;
  XBUFFER[13] = 0x00;
  //length of message
  XBUFFER[14] = 0x00;
  XBUFFER[15] = 0x1F;

  XBUFFER[16] = 0x00;
  XBUFFER[17] = 0x64;
  XBUFFER[18] = seqID >> 24;
  XBUFFER[19] = seqID >> 16;
  XBUFFER[20] = seqID >> 8;
  XBUFFER[21] = seqID;
  XBUFFER[22] = address >> 56;
  XBUFFER[23] = address >> 48;
  XBUFFER[24] = address >> 40;
  XBUFFER[25] = address >> 32;
  XBUFFER[26] = address >> 24;
  XBUFFER[27] = address >> 16;
  XBUFFER[28] = address >> 8;
  XBUFFER[29] = address;
  XBUFFER[30] = 0x00;
  Serial.write(XBUFFER, sizeof XBUFFER);
  mySeqID += 1;
}

void HHStatus(uint32_t seqID, uint16_t mID, uint8_t exp)
{

  FLOATUNION_t speed;
  speed.mph = 0.0;
  byte bufflen = 51;
  byte XBUFFER[bufflen] = "";
  XBUFFER[0] = 0x08;
  XBUFFER[1] = 0x36;
  XBUFFER[2] = Ones >> 8; //0x00;
  XBUFFER[3] = Ones; //0x01;
  XBUFFER[4] = Tens >> 8; //0x00;
  XBUFFER[5] = Tens; //0x02;
  XBUFFER[6] = mySeqID >> 24;
  XBUFFER[7] = mySeqID >> 16;
  XBUFFER[8] = mySeqID >> 8;
  XBUFFER[9] = mySeqID;
  XBUFFER[10] = 0x00;
  XBUFFER[11] = 0x00;
  XBUFFER[12] = 0x00;
  XBUFFER[13] = 0x00;
  XBUFFER[14] = 0x00;
  XBUFFER[15] = 0x33; //49
  //end of header
  XBUFFER[16] = mID >> 8;
  XBUFFER[17] = mID;
  XBUFFER[18] = seqID >> 24;
  XBUFFER[19] = seqID >> 16;
  XBUFFER[20] = seqID >> 8;
  XBUFFER[21] = seqID;
  XBUFFER[22] = 0x00;
  XBUFFER[23] = 0x00;
  XBUFFER[24] = 0x00;
  XBUFFER[25] = exp;
  XBUFFER[26] = 0x00;
  XBUFFER[27] = 0x00;
  XBUFFER[28] = 0x00;
  XBUFFER[29] = 0x00;
  XBUFFER[30] = 0x00;
  XBUFFER[31] = speed.bytes[0];
  XBUFFER[32] = speed.bytes[1];
  XBUFFER[33] = speed.bytes[2];
  XBUFFER[34] = speed.bytes[3];
  XBUFFER[35] = 0x00;
  XBUFFER[36] = 0x00;
  XBUFFER[37] = 0x01;
  XBUFFER[38] = 0x00;
  XBUFFER[39] = 0x00;
  XBUFFER[40] = 0x01;
  XBUFFER[41] = 0x00;
  XBUFFER[42] = 0x00;
  XBUFFER[43] = 0x01;
  XBUFFER[44] = 0x00;
  XBUFFER[45] = 0x07;
  XBUFFER[46] = 0x01;
  XBUFFER[47] = 0x00;
  XBUFFER[48] = 0xFA;
  XBUFFER[49] = 0x00;
  XBUFFER[50] = 0x00;
  Serial.write(XBUFFER, sizeof XBUFFER);
  mySeqID += 1;
}

void Status(uint32_t seqID, uint16_t mID, byte exp) //message 2102
{
  FLOATUNION_t speed;
  speed.mph = 0.0;
  byte bufflen = 51;
  byte XBUFFER[bufflen] = "";
  XBUFFER[0] = 0x08;
  XBUFFER[1] = 0x36;
  XBUFFER[2] = Ones >> 8;
  XBUFFER[3] = Ones;
  XBUFFER[4] = Tens >> 8;
  XBUFFER[5] = Tens;
  XBUFFER[6] = mySeqID >> 24;
  XBUFFER[7] = mySeqID >> 16;
  XBUFFER[8] = mySeqID >> 8;
  XBUFFER[9] = mySeqID;
  XBUFFER[10] = 0x00;
  XBUFFER[11] = 0x00;
  XBUFFER[12] = 0x00;
  XBUFFER[13] = 0x00;
  XBUFFER[14] = 0x00;
  XBUFFER[15] = 0x33;
  //end of header
  XBUFFER[16] = mID >> 8;
  XBUFFER[17] = mID;
  XBUFFER[18] = seqID >> 24;
  XBUFFER[19] = seqID >> 16;
  XBUFFER[20] = seqID >> 8;
  XBUFFER[21] = seqID;
  XBUFFER[22] = 0x00;
  XBUFFER[23] = 0x00;
  XBUFFER[24] = 0x00;
  XBUFFER[25] = exp;
  XBUFFER[26] = 0x00;
  XBUFFER[27] = 0x00;
  XBUFFER[28] = 0x00;
  XBUFFER[29] = 0x00;
  XBUFFER[30] = 0x00;
  XBUFFER[31] = speed.bytes[0];
  XBUFFER[32] = speed.bytes[1];
  XBUFFER[33] = speed.bytes[2];
  XBUFFER[34] = speed.bytes[3];
  XBUFFER[35] = 0x00;
  XBUFFER[36] = 0x00;
  XBUFFER[37] = 0x01;
  XBUFFER[38] = 0x00;
  XBUFFER[39] = 0x00;
  XBUFFER[40] = 0x01;
  XBUFFER[41] = 0x00;
  XBUFFER[42] = 0x00;
  XBUFFER[43] = 0x01;
  XBUFFER[44] = 0x00;
  XBUFFER[45] = 0x07;
  XBUFFER[46] = 0x01;
  XBUFFER[47] = 0x00;
  XBUFFER[48] = 0xFA;
  XBUFFER[49] = 0x00;
  XBUFFER[50] = 0x00;
  Serial.write(XBUFFER, sizeof XBUFFER);
  mySeqID += 1;
}

void cmdAck(uint32_t seqID, uint16_t mID)
{
  byte bufflen = 23;
  byte XBUFFER[bufflen] = "";
  XBUFFER[0] = 0x08;
  XBUFFER[1] = 0x35;
  XBUFFER[2] = Ones >> 8;
  XBUFFER[3] = Ones;
  XBUFFER[4] = Tens >> 8;
  XBUFFER[5] = Tens;
  XBUFFER[6] = mySeqID >> 24;
  XBUFFER[7] = mySeqID >> 16;
  XBUFFER[8] = mySeqID >> 8;
  XBUFFER[9] = mySeqID;
  XBUFFER[10] = 0x00;
  XBUFFER[11] = 0x00;
  XBUFFER[12] = 0x00;
  XBUFFER[13] = 0x00;
  XBUFFER[14] = 0x00;
  XBUFFER[15] = 0x17;
  //end of header
  XBUFFER[16] = mID >> 8;
  XBUFFER[17] = mID;
  //Response SeqID
  XBUFFER[18] = seqID >> 24;
  XBUFFER[19] = seqID >> 16;
  XBUFFER[20] = seqID >> 8;
  XBUFFER[21] = seqID;
  XBUFFER[22] = 'S';
  Serial.write(XBUFFER, sizeof XBUFFER);
  mySeqID += 1;
}

void loop() {
  // Check if there's enough data in the buffer to read the header
  if (Serial.available() >= HEADER_SIZE) {
    // Read the 16-byte header
    byte header[HEADER_SIZE];
    for (int i = 0; i < HEADER_SIZE; i++) {
      header[i] = Serial.read();
    }

    // Extract the message ID from the header
    uint16_t messageId = (header[0] << 8) | header[1];
    uint32_t sequenceID = ((uint32_t)header[6] << 24) | ((uint32_t)header[7] << 16) | ((uint32_t)header[8] << 8) | header[9];

    // Check the message ID
    if (messageId == 100) {

      // If the message ID is 100, call the 'caps' function
      caps(sequenceID);
    } else if (messageId == 2100) {
      // If the message ID is 2100, extract the payload length from bytes 15 and 16 in the header
      uint16_t payloadLength = (header[14] << 8) | header[15];

      // Check if there is enough data in the buffer to read the entire payload
      if (Serial.available() >= payloadLength - HEADER_SIZE) {
        // Read the payload
        byte payload[payloadLength - HEADER_SIZE];
        for (int i = 0; i < payloadLength - HEADER_SIZE; i++) {
          payload[i] = Serial.read();
        }

        // Check the first byte of the payload
        if (payload[0] == 6) {
          // If the first byte is 6, call the 'HitSwitchStatus' function

          HHStatus(sequenceID, messageId, 0x00);
          delay(1000);
          cmdAck(sequenceID, messageId);
        } else if (payload[0] == 3) {



          if (payload[1] == 0x5A && lastposition == 1) {
            cmdAck(sequenceID, messageId);
          }
          else {
            cmdAck(sequenceID, messageId);
            Status(0, 0, 0x2D);

            delay(1000);
            lastposition = (payload[1] == 0x5A) ? 1 : 0;
            Status(sequenceID, messageId, payload[1]);
          }
        }
        else if (payload[0] == 2) {
          byte ud = (lastposition == 1) ? 0x5A : 0x00;
          Status(sequenceID, messageId, ud);
        }
      }
    } else {
      // If the message ID is not one of the specified values, clear the buffer

      while (Serial.available() > 0) {
        Serial.read(); // Clear the buffer
      }
      return; // Exit the if statement
    }
  }
}

.
.
works.ino

//works.ino
#include <SoftwareSerial.h>
#include <EEPROM.h>
const int HEADER_SIZE = 16;
SoftwareSerial mySerial(44, 46); //rx, tx
uint32_t mySeqID = 1;
uint16_t Ones = 1;
uint16_t Tens = 1;
uint64_t address = 2;
byte lastposition = 0;
byte ETGTadd = 0xAA; //EEPROM location to hold address 170
byte EVersion = 0x00; //Version that is read from EEPROM
byte EVerLoc = 0xA8; //Version location in EEPROM 168
byte Version = 0x04; //Keep versions even numbers for easier Android coding
char ver1 = '0';
char ver2 = '4';
uint64_t naddress = 2;


void getEEAddress() {

  EEPROM.get(ETGTadd, naddress);
  if (naddress > 2800 || naddress == 0) //update address to 2 in EEPROM if not a correct address
  {
    address = 2;
    EEPROM.put(ETGTadd, address);
  }
  else {
    EEPROM.put(ETGTadd, naddress);
    address = naddress;
  }
}

void setup() {

  Serial.begin(9600);
  mySerial.begin(9600);
  EEPROM.update(EVerLoc, ver1);
  EEPROM.update(EVerLoc + 1, ver2);
  getEEAddress();
  delay(500);
  Serial.write(0x63);
}

typedef union
{
  float mph;
  uint8_t bytes[4];
} FLOATUNION_t;

void caps(uint32_t seqID) {
  byte bufflen = 31;
  byte XBUFFER[bufflen] = "";
  //Message header
  XBUFFER[0] = 0x08;
  XBUFFER[1] = 0x3F;
  XBUFFER[2] = Ones >> 8;
  XBUFFER[3] = Ones;
  XBUFFER[4] = Tens >> 8;
  XBUFFER[5] = Tens;
  XBUFFER[6] = mySeqID >> 24;
  XBUFFER[7] = mySeqID >> 16;
  XBUFFER[8] = mySeqID >> 8;
  XBUFFER[9] = mySeqID;
  XBUFFER[10] = 0x00;
  XBUFFER[11] = 0x00;
  XBUFFER[12] = 0x00;
  XBUFFER[13] = 0x00;
  //length of message
  XBUFFER[14] = 0x00;
  XBUFFER[15] = 0x1F;
  XBUFFER[16] = 0x00;
  XBUFFER[17] = 0x64;
  XBUFFER[18] = seqID >> 24;
  XBUFFER[19] = seqID >> 16;
  XBUFFER[20] = seqID >> 8;
  XBUFFER[21] = seqID;
  XBUFFER[22] = address >> 56;
  XBUFFER[23] = address >> 48;
  XBUFFER[24] = address >> 40;
  XBUFFER[25] = address >> 32;
  XBUFFER[26] = address >> 24;
  XBUFFER[27] = address >> 16;
  XBUFFER[28] = address >> 8;
  XBUFFER[29] = address;
  XBUFFER[30] = 0x00;
  Serial.write(XBUFFER, sizeof XBUFFER);
  mySeqID += 1;
}

void HHStatus(uint32_t seqID, uint16_t mID, uint8_t exp)
{
  mySerial.println("ConfigHSS");
  FLOATUNION_t speed;
  speed.mph = 0.0;
  byte bufflen = 51;
  byte XBUFFER[bufflen] = "";
  XBUFFER[0] = 0x08;
  XBUFFER[1] = 0x36;
  XBUFFER[2] = Ones >> 8; //0x00;
  XBUFFER[3] = Ones; //0x01;
  XBUFFER[4] = Tens >> 8; //0x00;
  XBUFFER[5] = Tens; //0x02;
  XBUFFER[6] = mySeqID >> 24;
  XBUFFER[7] = mySeqID >> 16;
  XBUFFER[8] = mySeqID >> 8;
  XBUFFER[9] = mySeqID;
  XBUFFER[10] = 0x00;
  XBUFFER[11] = 0x00;
  XBUFFER[12] = 0x00;
  XBUFFER[13] = 0x00;
  XBUFFER[14] = 0x00;
  XBUFFER[15] = 0x33; //49
  //end of header
  XBUFFER[16] = mID >> 8;
  XBUFFER[17] = mID;

  XBUFFER[18] = seqID >> 24;
  XBUFFER[19] = seqID >> 16;
  XBUFFER[20] = seqID >> 8;
  XBUFFER[21] = seqID;
  XBUFFER[22] = 0x00;
  XBUFFER[23] = 0x00;
  XBUFFER[24] = 0x00;
  XBUFFER[25] = exp;
  XBUFFER[26] = 0x00;
  XBUFFER[27] = 0x00;
  XBUFFER[28] = 0x00;
  XBUFFER[29] = 0x00;
  XBUFFER[30] = 0x00;
  XBUFFER[31] = speed.bytes[0];
  XBUFFER[32] = speed.bytes[1];
  XBUFFER[33] = speed.bytes[2];
  XBUFFER[34] = speed.bytes[3];
  XBUFFER[35] = 0x00;
  XBUFFER[36] = 0x00;
  XBUFFER[37] = 0x01;
  XBUFFER[38] = 0x00;
  XBUFFER[39] = 0x00;
  XBUFFER[40] = 0x01;
  XBUFFER[41] = 0x00;
  XBUFFER[42] = 0x00;
  XBUFFER[43] = 0x01;
  XBUFFER[44] = 0x00;
  XBUFFER[45] = 0x07;
  XBUFFER[46] = 0x01;
  XBUFFER[47] = 0x00;
  XBUFFER[48] = 0xFA;
  XBUFFER[49] = 0x00;
  XBUFFER[50] = 0x00;
  Serial.write(XBUFFER, sizeof XBUFFER);
  mySeqID += 1;
  mySerial.println("Finished ConfigHSS");
}

void Status(uint32_t seqID, uint16_t mID, byte exp) //message 2102
{

  FLOATUNION_t speed;
  speed.mph = 0.0;
  byte bufflen = 51;
  byte XBUFFER[bufflen] = "";
  XBUFFER[0] = 0x08;
  XBUFFER[1] = 0x36;
  XBUFFER[2] = Ones >> 8;
  XBUFFER[3] = Ones;
  XBUFFER[4] = Tens >> 8;
  XBUFFER[5] = Tens;
  XBUFFER[6] = mySeqID >> 24;
  XBUFFER[7] = mySeqID >> 16;
  XBUFFER[8] = mySeqID >> 8;
  XBUFFER[9] = mySeqID;
  XBUFFER[10] = 0x00;
  XBUFFER[11] = 0x00;
  XBUFFER[12] = 0x00;
  XBUFFER[13] = 0x00;
  XBUFFER[14] = 0x00;
  XBUFFER[15] = 0x33;
  //end of header
  XBUFFER[16] = mID >> 8;
  XBUFFER[17] = mID;
  XBUFFER[18] = seqID >> 24;
  XBUFFER[19] = seqID >> 16;
  XBUFFER[20] = seqID >> 8;
  XBUFFER[21] = seqID;
  XBUFFER[22] = 0x00;
  XBUFFER[23] = 0x00;
  XBUFFER[24] = 0x00;
  XBUFFER[25] = exp;
  XBUFFER[26] = 0x00;
  XBUFFER[27] = 0x00;
  XBUFFER[28] = 0x00;
  XBUFFER[29] = 0x00;
  XBUFFER[30] = 0x00;
  XBUFFER[31] = speed.bytes[0];
  XBUFFER[32] = speed.bytes[1];
  XBUFFER[33] = speed.bytes[2];
  XBUFFER[34] = speed.bytes[3];
  XBUFFER[35] = 0x00;
  XBUFFER[36] = 0x00;
  XBUFFER[37] = 0x01;
  XBUFFER[38] = 0x00;
  XBUFFER[39] = 0x00;
  XBUFFER[40] = 0x01;
  XBUFFER[41] = 0x00;
  XBUFFER[42] = 0x00;
  XBUFFER[43] = 0x01;
  XBUFFER[44] = 0x00;
  XBUFFER[45] = 0x07;
  XBUFFER[46] = 0x01;
  XBUFFER[47] = 0x00;
  XBUFFER[48] = 0xFA;
  XBUFFER[49] = 0x00;
  XBUFFER[50] = 0x00;
  Serial.write(XBUFFER, sizeof XBUFFER);
  mySeqID += 1;
}

void cmdAck(uint32_t seqID, uint16_t mID)
{
  byte bufflen = 23;
  byte XBUFFER[bufflen] = "";
  XBUFFER[0] = 0x08;
  XBUFFER[1] = 0x35;
  XBUFFER[2] = Ones >> 8;
  XBUFFER[3] = Ones;
  XBUFFER[4] = Tens >> 8;
  XBUFFER[5] = Tens;
  XBUFFER[6] = mySeqID >> 24;
  XBUFFER[7] = mySeqID >> 16;
  XBUFFER[8] = mySeqID >> 8;
  XBUFFER[9] = mySeqID;
  XBUFFER[10] = 0x00;
  XBUFFER[11] = 0x00;
  XBUFFER[12] = 0x00;
  XBUFFER[13] = 0x00;
  XBUFFER[14] = 0x00;
  XBUFFER[15] = 0x17;
  //end of header
  XBUFFER[16] = mID >> 8;
  XBUFFER[17] = mID;
  //Response SeqID
  XBUFFER[18] = seqID >> 24;
  XBUFFER[19] = seqID >> 16;
  XBUFFER[20] = seqID >> 8;
  XBUFFER[21] = seqID;
  XBUFFER[22] = 'S';
  Serial.write(XBUFFER, sizeof XBUFFER);
  mySeqID += 1;
}


void loop() {
  // Check if there's enough data in the buffer to read the header
  if (Serial.available() >= HEADER_SIZE) {
    // Read the 16-byte header
    byte header[HEADER_SIZE];
    for (int i = 0; i < HEADER_SIZE; i++) {
      header[i] = Serial.read();
    }

    // Extract the message ID from the header
    uint16_t messageId = (header[0] << 8) | header[1]; mySerial.print("MessageID: "); mySerial.println(messageId);
    uint32_t sequenceID = ((uint32_t)header[6] << 24) | ((uint32_t)header[7] << 16) | ((uint32_t)header[8] << 8) | header[9]; mySerial.print("Seq is: "); mySerial.println(sequenceID);

    // Check the message ID
    if (messageId == 100) {
      mySerial.println("Sending response to 100");
      // If the message ID is 100, call the 'caps' function
      caps(sequenceID);
    } else if (messageId == 2100) {
      // If the message ID is 2100, extract the payload length from bytes 15 and 16 in the header
      uint16_t payloadLength = (header[14] << 8) | header[15];

      // Check if there is enough data in the buffer to read the entire payload
      if (Serial.available() >= payloadLength - HEADER_SIZE) {
        // Read the payload
        byte payload[payloadLength - HEADER_SIZE];
        for (int i = 0; i < payloadLength - HEADER_SIZE; i++) {
          payload[i] = Serial.read();
        }
        mySerial.print("right before checking payload byte: "); mySerial.println(payload[0]);
        // Check the first byte of the payload
        if (payload[0] == 6) {
          // If the first byte is 6, call the 'HitSwitchStatus' function
          mySerial.println("Sending ConigHitSensor");
          HHStatus(sequenceID, messageId, 0x00);
          delay(1000);
          cmdAck(sequenceID, messageId);
        } else if (payload[0] == 3) {
          if (payload[1] == 0x5A && lastposition == 1) {
            cmdAck(sequenceID, messageId);
          }
          else {
            cmdAck(sequenceID, messageId);
            Status(0, 0, 0x2D);
            mySerial.println("after Sending 2D");
            delay(1000);
            lastposition = (payload[1] == 0x5A) ? 1 : 0;
            //mySerial.println("Before sending 05A or 0x00");
            Status(sequenceID, messageId, payload[1]);
            mySerial.print("After sending, sent: "); mySerial.println(payload[1], HEX);
          }
        }
        else if (payload[0] == 2) {
          mySerial.println("sending status");
          byte ud = (lastposition == 1) ? 0x5A : 0x00;
          Status(sequenceID, messageId, ud);
        }
      }
    } else {
      // If the message ID is not one of the specified values, clear the buffer
      mySerial.println("Invalid message ID, clearing buffer...");
      while (Serial.available() > 0) {
        Serial.read(); // Clear the buffer
      }
      return; // Exit the if statement
    }
  }
}

They are kind of long....

I have found that if I comment out all of the Software Serial stuff and just add a delay(30) after

// Extract the message ID from the header
    uint16_t messageId = (header[0] << 8) | header[1];mySerial.print("MessageID: ");mySerial.println(messageId);
    uint32_t sequenceID = ((uint32_t)header[6] << 24) | ((uint32_t)header[7] << 16) | ((uint32_t)header[8] << 8) | header[9];mySerial.print("Seq is: ");mySerial.println(sequenceID);

that it works with just the uart Serial. I will now test it using a second uart as a print debug as see if it still works.

Okay, so I added Serial3 to replace all the mySerial stuff and it works properly with the delay(30) there. The Serial3 will not let it work properly without the delay(30). So it has to have the delay(30) or use software serial. That is odd to me.

One little thing I noticed, that may not be relevant, is that when you get to the point where you're clearing the buffer due to an invalid message(near line 311), you loop, watching for a zero available count; the problem with this sort of structure is that, if characters are still arriving, the loop acts much faster than incoming bytes are received, so you can incorrectly decide no more are coming, which then causes garbage to not be flushed. Your delay may be allowing the entire line to accumulate, so this clearing action actually works for you. Try moving the delay into that section of the code, to line 310 or thereabouts, see if it has the same effect there.
Does your incoming stream have a definitive end-of-line marker, like a carriage return or linefeed?

That's what I thought when I read @camsysca posting.
Best thing would be if you have a start-marker and and end-marker.
With start/end-marker you can start reading at any byte even in the middle of a message.
Any truncated message will be read-out from the buffer and will be thrown away.

Then if the next message arrives and really starts with the constant start-marker correct receiving begins and ends if the end-marker is read.

There is a tutorial about that written by robin2

best regards Stefan

This is just a diagnostic, to be clear - if it works, it means you're not sweeping out the garbage. Why you're getting unexpected lines, different question.

No, it seems to have to be at that location. 30 is the smallest I can go it seems. I'm thinking maybe it was working with the software serial because it must take a tad longer to run than the regular uart. This is actually a closed network that this goes on so no garbage data should be coming across. I just wait for a specific 100 message and respond with a caps message. I only get that once I turn on the board. After that I get another message that has a 16 byte head that tells me how long the payload is. I just loop through the data from there. If there is any left then it belongs to the next message. There really should be any "garbage" messages. I did put that there hoping if things got out of wack it would flush the buffer until it could actually read a correct message again. I can't see it happening but I don't think I need to say it won't....