Setting incoming bytes to variables

Hello I am still very new to arduino and was hoping someone could help... sorry if this has been asked before.
I have a program sending data to arduino in this format - “AB” byte1 byte2 byte3 byte4 byte5 byte6 byte7 0x0D where AB is start of data identifier byte1 is reserved and byte2 to byte7 are 8bit binary number and 0xD is single byte Carriage Return data terminator.

Here is what i have done so and i need a way to ensure that the variable byte0 is set to AB and so on.

void loop() {
  
 if( Serial.available() >= 9 ) {  // command length is 9 bytes
 //read the next 9 bytes
   byte0 = Serial.read();
   byte1 = Serial.read();
   byte2 = Serial.read();
   byte3 = Serial.read();
   byte4 = Serial.read();
   byte5 = Serial.read();
   byte6 = Serial.read();
   byte7 = Serial.read();
   byte8 = Serial.read();
  
 }  

}

What is to stop any of the data bytes from looking like "AB"?
By the way "AB" is a three byte string, 0x41, 0x42, 0x00
So it is a bit of a poor protocol.
You have nothing to synchronise the reading with the header, you need something.

Why not use a single byte array? Would make the code smaller and easier to deal with

byte buffer [9];

void loop() {
  
 if( Serial.available() >= 9 ) {  // command length is 9 bytes
 //read the next 9 bytes
   for (int i = 0 ; i < 9 ; i++)
     buffer[i] = Serial.read();
  
 }  
  if (buffer[0] == 0xAB && buffer[8] == 0x0D) 
  {
    ....
  }

}

If "AB" is a byte 0xAB and not a string, just read data until you see it and then read the next 8 bytes, something like this:-

byte buffer [9];

void loop() {
  
 if( Serial.available() > 1 ) {  // command length is 9 bytes
   buffer[0] = Serial.read();
   if(buffer[0] == 0xAB){
     int i = 1;
     while(i < 9){
     while(Serial.available() == 0) { } // do nothing until data is in
     buffer[i] = Serial.read();
     i++
     }
   }
  
 }  
 
 // rest of loop
 
}

Thanks guys for your response. the full description for the data format being sent to arduino is on this site BFF Motion Driver Short User Guide . I will try your suggestions now

the full description for the data format being sent to arduino is on this site

Yes it is not very clear what they mean by "AB" my early concerns about this being poor are not aswaged, as there is nothing to stop any of the bytes lining up to look like a header.

I have an insane passion for verbose code. I'm trying to quit, but it's difficult :wink:

// Recognize this byte sequence:
//
// 'A' 'B' byte1 byte2 byte3 byte4 byte5 byte6 byte7 0x0D
//
// collect byte1 through byte7 and do something with them

enum FSAStatus { WAIT_FOR_SOM1, WAIT_FOR_SOM2, WAIT_FOR_EOM, ERROR } fsaStatus;

const int SOM1 = 'A';     // Start Of Message character 1
const int SOM2 = 'B';     // Start Of Message character 2
const int EOM = 0x0D;  // End Of Message marker

const short NUM_BYTES = 7;      // number of bytes we are expecting
unsigned short byteCount;         // number of bytes collected so far
byte data[NUM_BYTES];             //


// do something with the collected data
void processData() {
    unsigned short i;
    
    for (i = 0; i < NUM_BYTES - 1; i++) {
        Serial.print(data[i], HEX);
        Serial.print(", ");
    }
    Serial.println(data[i], HEX);
}


void setup() {
    Serial.begin(9600);
    fsaStatus = WAIT_FOR_SOM1;
}


void loop() {
    
    if (Serial.available() > 0) {
        int ch = Serial.read();
        
        Serial.println(ch, HEX);
        
        switch (fsaStatus) {
            // we are waiting for 'A'
            case WAIT_FOR_SOM1:
                if (ch == SOM1) {               // received
                    fsaStatus = WAIT_FOR_SOM2;  // we are now waiting for 'B'
                }
                // discard any other char, keep waiting for SOM1
                break;
            
            // we are waiting for 'B'
            case WAIT_FOR_SOM2:
                if (ch == SOM2) {               // received
                    fsaStatus = WAIT_FOR_EOM;   // we wait for the end of message marker
                    byteCount = 0;              // and start counting the bytes of the message "body"
                }
                else {
                    // error: unexpected char after 'A'
                    Serial.println("Unexpected char after 'A'.");
                    fsaStatus = WAIT_FOR_SOM1;
                }
                break;
            
            // we are collecting bytes while waiting for 0x0D
            case WAIT_FOR_EOM:
                if (ch == EOM) {    // end of message, did we receive all the bytes we were expecting ?
                    if (byteCount == NUM_BYTES) {
                        processData();         // yes, do something with the received bytes
                        fsaStatus = WAIT_FOR_SOM1;    // start waiting for next message
                    }
                    else if (byteCount < NUM_BYTES) {
                        // error: too few bytes
                        Serial.println("Too few bytes.");
                        fsaStatus = WAIT_FOR_SOM1;
                    }
                }
                else if (byteCount < NUM_BYTES) {    // still collecting data
                    data[byteCount] = (byte)ch;
                    byteCount++;
                }
                else {                          // too many bytes
                    Serial.println("Too many bytes.");
                    fsaStatus = WAIT_FOR_SOM1;
                }
                break;
        }
    }
}

Yet another take on what "AB" could mean.
It could also mean a string, either a null terminated one, or one with the first byte the string length.
Either way th OP needs to read what is actually coming from the device.