Serial Input Problem

I have a gimbal that communicates to the Arduino Uno via RS232. I convert the RS232 to TTL for use on the Arduino. The output of the gimbal is Hex bytes and the syncword I’m looking for is 00 FF 5A 08 08 10.

//          Include Libraries
//#include <SoftwareSerial.h>

// --------------------------------------------------------------------------------------------------------------------------------------------
//          Variables
byte  Buffer[4]   = {};
byte  ByteIn      = 0;
float Pan_Angle   = 0;
float Tilt_Angle  = 0;

// --------------------------------------------------------------------------------------------------------------------------------------------
//          Constants
//int RXpin         = 4;
//int TXpin         = 5;
//
//SoftwareSerial SerialIn(RXpin,TXpin);
// --------------------------------------------------------------------------------------------------------------------------------------------
//          Setup
void setup()
{
  // Start Serial Output Comms:
  Serial.begin(57600);
  
//  // Start SoftwareSerial Port:
//  SerialIn.begin(57600);
}

// --------------------------------------------------------------------------------------------------------------------------------------------
//          Program
void loop()
{
  // Read Input and Scan for Syncword: 00FF5A080810
  if (Serial.available() > 0) {
    ByteIn = Serial.read();
    Serial.println(ByteIn,HEX);
    if (ByteIn == 0x00) {
      if (Serial.available() > 0) {
        ByteIn = Serial.read();
        if (ByteIn == 0xFF) {
          if (Serial.available() > 0) {
            ByteIn = Serial.read();
            if (ByteIn == 0x5A) {
              if (Serial.available() > 0) {
                ByteIn = Serial.read();
                if (ByteIn == 0x08) {
                  if (Serial.available() > 0) {
                    ByteIn = Serial.read();
                    if (ByteIn == 0x08) {
                      if (Serial.available() > 0) {
                        ByteIn = Serial.read();
                        if (ByteIn == 0x10) {
                        
                          // Header Complete: Wait for next 4 Bytes of Data and Read Into Buffer:
                          for (int i = 0; i < 5; i = i + 1)
                          {
                            Buffer[i] = Serial.read();
                          }
                           
                          // Calculate Pan and Tilt Angles:
                          int Pan_Angle_0  = Twos_Complement((int)Buffer[0] << 8 | (int)Buffer[1],16);
                          int Tilt_Angle_0 = Twos_Complement((int)Buffer[2] << 8 | (int)Buffer[3],16);
                          
                          // Scale Values:
                          Pan_Angle        = (float)Pan_Angle_0  / 100;
                          Tilt_Angle       = (float)Tilt_Angle_0 / 100;
                          
                          // Output Results:
                          Serial.print("Pan Angle = ");
                          Serial.println(Pan_Angle);
                          Serial.print("Tilt Angle = ");
                          Serial.println(Tilt_Angle);
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
// --------------------------------------------------------------------------------------------------------------------------------------------
int Twos_Complement(int Value,int NumBits)
{
  // Initialise Result:
  int Result = 0;

  // Get MSB Value:
  if (bitRead(Value,NumBits - 1) == 1)
  {
    // Calculate Two's Complement Value:
    Result = -1 * ~(Value - 1);
  }
  else
  {
    Result = Value;
  }
  
  // Return Result:
  return Result;
}
// --------------------------------------------------------------------------------------------------------------------------------------------

This code works fine as long as I don’t use the Softwareserial library for the incoming data. Also, what I cannot understand is, if I remove the line:
Serial.println(ByteIn,HEX);
it doesn’t work at all. Even if I say Serial.println(’ ‘) it still works. Also if I say Serial.print(’ '); it also doesn’t work.
I don’t want to output the incoming bytes all the time, only while debugging. But without it, the angles aren’t output at all.

Why would this be?

Serial comms is slooooow. Think about what happens here

 if (Serial.available() > 0) {
    ByteIn = Serial.read();
    Serial.println(ByteIn,HEX);
    if (ByteIn == 0x00) {
      if (Serial.available() > 0) {

if your second "Serial.available" returns zero.

(Hint: Why do you think the extra delay of printing a character causes your sketch to work?)

Ok, should I rather do this with a small delay? delay(10);

Another thing I'm worried about is a terminal program reading the incoming HEX values returns: F3 00 2F 00 0E 7F FF 0B 67 DC EC 00 00 00 15 23 00 FF 5A 08 08 10 0A C3 DC EC 00 00 04 0E 00 FF 5A 14 08 40 00 00 05 14 F0 80 (a subset copied from the terminal program). The syncword is clearly visible about halfway through. When I print the values out using the Arduino serial monitor, these values are nowhere to be found but the angles are still calculated correctly. How is this possible? Is the arduino terminal just not capable of printing out these values correctly or what?

Thanks for your help so far!

Ok, should I rather do this with a small delay?

No. Unless you have some need to do other things, simply WAIT for every character

int readChar ()
{
  while (Serial.available () == 0) {}
  return Serial.read ();
}

Great, it works fantastic. Thanks!