Read serial data with 8 bytes start marker and length of frame

Hello everybody,

I’m new to arduino and this kind of coding so go easy with me :slight_smile:
I’m trying to read data from my sensor over uart.
My goal is to extract a specific value in the payload.
It’s important to say that I am working on extremely high baud rate of 921600 and each time I read a long frame - it could be up to 500 bytes.

The data format of the frame as follows:
First the Main Header: 52 bytes

Each frame header has a start marker of the following 8 byte array:

MagicNumber[0] == 0x02 && MagicNumber[1] == 0x01 && MagicNumber[2] == 0x04 && MagicNumber[3] == 0x03 &&
MagicNumber[4] == 0x06 && MagicNumber[5] == 0x05 && MagicNumber[6] == 0x08 && MagicNumber[7] == 0x07)

I don’t have an end marker to all the frame, but I do know that inside this header, the sum of the bytes in position 21-24 will tell the frame length in bytes, including header.

After the main header comes the payload header combined from 8 bytes.
The first 4 bytes will tell the type of the message - 06 or 07 or 08, and the next 4 bytes will tell the length of the payload message, including payload header.

What I need is to read message type 07 and extract the value of the first 4 byte.

I started to check the data output from Robin2 code in another similar post:

// Example to receive data with a start marker and length byte
//   not tested

const byte numBytes = 500;
byte receivedBytes[numBytes];
const byte typeBytePosition = 2; // 2nd position after the start byte
const byte requestType = 0x65;
const byte responseType = 0x75;
const byte requestLength = 52;
const byte responseLength = 52;

boolean newData = false;

void setup() {
    Serial.begin(921600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvBytesWithStartMarker();
    showNewData();
}

void recvBytesWithStartMarker() {
    static boolean recvInProgress = false;
    static int ndx = 0;
    static byte numBytesReceived = 0;
    static byte mesgLen = 0;
    const byte startMarker = 0x02;
    byte rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();
        numBytesReceived ++;

        if (recvInProgress == true) {
            if (numBytesReceived == typeBytePosition) {
                if (rc == requestType) {
                    mesgLen = requestLength;
                }
                if (rc == responseType) {
                    mesgLen = responseLength;
                }
                ndx = 0; // enable saving of data
            }
            if (ndx >= 0) {
                receivedBytes[ndx] = rc;
                ndx++;
            }
            if (ndx >= mesgLen) { // got the whole message
                receivedBytes[ndx] = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
            numBytesReceived = 0;
            ndx = -1; // prevent saving data for the moment
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        for (byte n = 0; n < numBytes; n++) {
            Serial.print(receivedBytes[n], HEX);
            Serial.print(' ');
        }
        Serial.println();
        newData = false;
    }
}

I can see the data, but I don’t understand how to adapt it for what I need.

I’ll appreciate your help.
Thanks.

Bullman:
I can see the data, but I don't understand how to adapt it for what I need.

I don't understand that. Do you mean that you are receiving the entire message correctly but you don't know how to identify part of it?

Can you post an example of the message that is sent and an example of what you are receiving?

Also I find this confusing

MagicNumber[0] == 0x02 && MagicNumber[1] == 0x01 && MagicNumber[2] == 0x04 && MagicNumber[3] == 0x03 &&
MagicNumber[4] == 0x06 && MagicNumber[5] == 0x05 && MagicNumber[6] == 0x08 && MagicNumber[7] == 0x07)

Do you just mean that the first 8 bytes are always

0x02, 0x01, 0x04, 0x03, 0x06, 0x05, 0x08, 0x07

More confusion with this

After the main header comes the payload header combined from 8 bytes.
The first 4 bytes will tell the type of the message - 06 or 07 or 08,

If the "type byte" comes after the first 52 bytes why are you looking for it in the second byte - i.e. immediately after the start byte.

Why would it need 4 bytes to tell the type if it can be contained in a single byte?

It would make life a whole lot easier if you were to post a link to the datasheet for the sensor you are using. Then we might actually know what you are trying to create.

...R

Hi Robin,

Thanks for your quick reply.
I attached a pdf of the data sheet that explain the format.

Here is an answer to your questions:
I am not receiving the entire message correctly, but I do see part of the message.
An example of the full and correct messages can be found in the end of the attached pdf.
Here is an example for the data output that I can see right now with your code:

This just in ... F F FF 2 1 4 3 6 5 8 7 4 0 0 2 42 16 A 0 F5 30 49 2D 8D 0 0 0 8D 5C 0 0 0 0 0 0 4E 0 0 0 72 4E 0 0 50 0 0 0 27 A 0 0 2 0 9 C3 7 0 0 0 4C 0 0 0 F 0 0 0 9E C7 68 3E 36 EC AA 3E D2 D1 68 3E 80 DC 31 BB 82 3C 16 3F D2 D1 91 BE 6 C5 26 41 BA C5 2A BE 33 C7 7A BE BB C5 2A D2 2A 3F BD EE 0 40 8 0 0 0 D 0 0 0 F F F F F 2 1 4 3 6 5 8 7 4 0 0 2 42 16 A 0 97 C3 E1 2D 80 0 0 0 8E 5C 0 0 0 0 0 0 4E 0 0 0 BA 4E 0 0 23 0 0 0 36 6 0 0 1 0 B2 33 7 0 0 0 4C 0 0 0 F 0 0 0 2 2C 75 3E C3 AB AA 3E 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 C5 26 41 BA C5 2A BE 33 C7 7A BE BB C5 2A BE 2A 3F 93 9A 2 40 2 1 4 3 6 5 8 7

Yes, I ment that the first 8 bytes are always:

0x02, 0x01, 0x04, 0x03, 0x06, 0x05, 0x08, 0x07

The “type byte” indeed comes after the first 52 bytes in the second header also called as the TLV header. I am not looking for it in the second byte, I just didn’t change the position because I wanted first to check if I see any data.
I believe that the datasheet of the sensor that I attached will spread some light over what I am trying to do.

Thanks.

SensorDataFormats.pdf (187 KB)

I'm not going to waste any more time on this if you won't explain what the whole project is about. That PDF does not even describe what the sensor is.

...R

Robin, I thank you for your willing to help.
Sorry if If I wasn't clear. I will give as much information about what I am trying to do.

My sensor is a radar.
The attached pdf is part of the section that explains how a UART stream of Frame will be output each time the radar shoot his beam.
For every frame, a packet is sent consisting of a fixed sized Frame Header - this is the 52 bytes header and then a variable number of TLVs (TLV ist type, length and value) depending on what was detected in that scene.

Before each of TLV type, there is a TLV header that explained what type of TLV is to come and what his length.
The TLVs can be of types representing three cases: point cloud - number 06, target list object - number 07, and associated points - number 08.

My goal is first to know how to use the following magic number 0x02, 0x01, 0x04, 0x03, 0x06, 0x05, 0x08, 0x07 that comes in the 52 bytes frame header as a start marker in order to put my radar into a sync and to be sure that I am getting all of the frame that the radar is sending.

Afterwards to make parse like this:
Read the frame header - 52 bytes >>read the TLV header - 8 bytes >> read the first 4 bytes of that TLV header >> if it's = 07 then read byte 1-4 in this array and save to value >> else do nothing.

Hope it's clearer now.
Thank you for your time.

Put the magic numbers in an array. Create an index. If the indexed member is equal to the incoming byte, increment the index. Else set the index to the first member. Repeat. When the index reaches the last member, you know you have received the consecutive bytes 2,1,4,3,6,5,8,7.

Hi @aarg,
Thank you for your help.
As I mentioned I am new to arduino so I will appreciate any help with the code.

Did you ment something like this ?

byte MagicNumber [] = {0x02, 0x01, 0x04, 0x03,0x06, 0x05, 0x08, 0x07};

I'll appreciate your help with the index equal to and the increment syntax.

Yes, that is correct. I don't have time to write anything for you. The testing would go wherever the bytes are received. You should have a state variable, "seeking_preamble" to differentiate the search from receiving subsequent bytes.

Bullman:
My sensor is a radar.

Please may we have a link to the datasheet for the sensor. Some other reader might like to replicate your project.

…R

Bullman:
I’ll appreciate your help with the index equal to and the increment syntax.

I think this is how I would approach it. I have deliberately created separate functions for each part so the commonalities will be obvious.

void loop() {
    checkForSync();
    getTypeAndLength();
    processTypeAndLength();
    getVdata();
    
    // other code
}

//============

void checkForSync() {
    if (syncFound == false) {
        byte syncValues[] = {0x02, 0x01, 0x04, 0x03,0x06, 0x05, 0x08, 0x07};
        static byte syncNdx = 0;
        byte numSyncBytes = 8;
        while (Serial.available > 0) {
            rb = Serial.read();
            if (rb == syncValues[syncNdx]) {
                syncNdx++;
                if (syncNdx > numSyncBytes) {
                    // we are at the end of the sync values
                    syncFound = true;
                    typeLenFound = false;
                    break;  // out of the WHILE
                }
            }
            else {    // byte did not match the next sync item
                syncNdx = 0;
            }
        }
    }
}

//============

void getTypeAndLength() {
    if (syncFound == true and typeLenFound == false) {
        byte numTypeLenBytes = 8;
        byte numTypeBytes = 4;
        static typeLenCount = 0;
        while (Serial.available > 0) {
            if (typeLenCount < numTypeBytes) {
                typeArray[typeLenCount] = Serial.read();
            }
            else if (typeLenCount < numTypeLenBytes) {
                lenArray[typeLenCount - 4] = Serial.read()
            }
            typeLenCount ++;
            if (typeLenCount >= numTypeLenBytes) {
                typeLenFound = true;
                break;
            }
        }
    }
}

//============

void processTypeAndLength() {
    if (typeLenFound == true) {
        // code to figure out the type and the length
        dataLength = xxx; // assuming this is a global variable for convenience
        waitingForData = true;
    }
}

//============

void getVdata() {
    static byte dataNdx = 0;
    if (waitingForData == true) {
        while (Serial.available > 0) {
            dataArray[dataNdx] = Serial.read();
            dataNdx++;
            if (dataNdx >= dataLength) {
                newData = true;
                waitingForData = false;
                break;
            }
        }
    }
}

…R

Hi @Robin2,

Thank you very much for your help. I think it’s a good start point.

Robin2:
Please may we have a link to the datasheet for the sensor. Some other reader might like to replicate your project.

…R

Sure, here is a link to the radar data sheet and use case page: http://www.ti.com/lit/ug/tidue71c/tidue71c.pdf
On page 17, you can see the Output Results Format as I have attached on my previous post.

As for your code, I added some missing variables and also my remarks:

// Receiving binary data with SyncNumber

byte syncFound = 0; //is is fine to add it here?
static boolean typeLenFound = false;; //added for the while loop. is it fine?

boolean newData = false;

void setup() {                      //added for the Serial comm
    Serial.begin(921600);
    Serial.println("<Ready>");
}

void loop() {
    checkForSync();
    getTypeAndLength();
    processTypeAndLength();
    getVdata();
   
    // other code
}

//============

void checkForSync() {
    if (syncFound == false) {
        byte syncValues[] = {0x02, 0x01, 0x04, 0x03,0x06, 0x05, 0x08, 0x07};
        static byte syncNdx = 0;
        byte numSyncBytes = 8;
        byte rb; //added for the while loop
        while (Serial.available > 0) {
            rb = Serial.read();
            if (rb == syncValues[syncNdx]) {
                syncNdx++;
                if (syncNdx > numSyncBytes) {
                    // we are at the end of the sync values
                    syncFound = true;
                    typeLenFound = false;
                    break;  // out of the WHILE
                }
            }
            else {    // byte did not match the next sync item
                syncNdx = 0;
            }
        }
    }
}

//============

void getTypeAndLength() {
    if (syncFound == true and typeLenFound == false) {
        byte numTypeLenBytes = 8;
        byte numTypeBytes = 4;
        byte typeArray = 0; //added for the loop. is it fine?
        byte lenArray = 0; //added for the loop. is it fine?
        
        while (Serial.available > 0) {
            if (typeLenCount < numTypeBytes) { //where you defined typeLenCount? Which kind of data does is stored? 
                typeArray[typeLenCount] = Serial.read(); 
            }
            else if (typeLenCount < numTypeLenBytes) {
                lenArray[typeLenCount - 4] = Serial.read();
            }
            typeLenCount ++;
            if (typeLenCount >= numTypeLenBytes) {
                typeLenFound = true;
                break;
            }
        }
    }
}

//============

void processTypeAndLength() {
    if (typeLenFound == true) {
        // code to figure out the type and the length
        dataLength = xxx; // assuming this is a global variable for convenience
        //** HERE I should check if typeArray = 07? **//
        waitingForData = true;
    }
}

//============

void getVdata() {
    static byte dataNdx = 0;
    if (waitingForData == true) {
        while (Serial.available > 0) {
            dataArray[dataNdx] = Serial.read();
            dataNdx++;
            if (dataNdx >= dataLength) {
                newData = true;
                waitingForData = false;
                break;
            }
        }
    }
}

I understand the checkForSync() for the sync.

I don’t understand in getTypeAndLength() the typeLenCount variable - where you defined typeLenCount? wich kind of data does is stored? How do you know that you are checking the 8 byte TLV header and not the first 8 bytes of the frame header (the first 52 bytes of the stream)?

In addition, you created the typeArray variable I assume for storing the value, but I need also to check if it is equal to 07 is it done in the processTypeAndLength() ?

Thanks.

Bullman:
I don't understand in getTypeAndLength() the typeLenCount variable - where you defined typeLenCount? wich kind of data does is stored? How do you know that you are checking the 8 byte TLV header and not the first 8 bytes of the frame header (the first 52 bytes of the stream)?

I used the name getTypeAndLength as a shorthand for the business of receiving the 8 bytes that define the type and length of the message. You may prefer to call it getTLV()

I think I forgot to initialise typeLenCount - it is just the index from 0 to 7 through the 8 bytes

I presume (but I may be wrong) that the 8 byte TLV header comes immediately after the SYNC bytes

In addition, you created the typeArray variable I assume for storing the value, but I need also to check if it is equal to 07 is it done in the processTypeAndLength() ?

I approach this sort of problem on the basis that when I have captured the data (in this case the 4 bytes for TYPE) I can then examine the data to see what it contains. In this case you may only be interested in one of the 4 bytes but it seems easier to collect them all first.

...R

I presume (but I may be wrong) that the 8 byte TLV header comes immediately after the SYNC bytes

No, the 8 byte TLV header is not coming immediately after the SYNC bytes, it is always come after the first 52 byte of the frame header.
This is why it didn’t make sense to me when you started the getTLVheader() (I changed the name according to the business logic) with the if (typeLenCount < numTypeBytes) because there is no reference for starting from byte 53 .
Which brings me to a conclusion that in checkForSync() there should be another variable that stores the initial 52 bytes? Or should it be preliminary check in the getTLVheader() ?

I approach this sort of problem on the basis that when I have captured the data (in this case the 4 bytes for TYPE)

As for the processTypeAndLength() , I still don’t understand how you figured out the receiving type and length of the TLV according to the getTLVheader()
Is dataLength ment to store the 4 bytes of the length of the TLV or the type of the TLV like 07?
And if so how can it be done with one variable?

Attached is the updated code:

// Receiving binary data with SyncNumber

byte syncFound = 0; //is is fine to add it here?
static boolean typeLenFound = false;; //added for the while loop. is it fine?
boolean newData = false;

void setup() {                      //added for the Serial comm
    Serial.begin(921600);
    Serial.println("<Ready>");
}

void loop() {
    checkForSync();
    getTLVheader();
    processTypeAndLength();
    getVdata();
   
    // other code
}

//============

void checkForSync() {
    if (syncFound == false) {
        byte syncValues[] = {0x02, 0x01, 0x04, 0x03,0x06, 0x05, 0x08, 0x07};
        static byte syncNdx = 0;
        byte numSyncBytes = 8;
        byte rb; //added for the while loop
        while (Serial.available > 0) {
            rb = Serial.read();
            if (rb == syncValues[syncNdx]) {
                syncNdx++;
                if (syncNdx > numSyncBytes) {
                    // we are at the end of the sync values
                    syncFound = true;
                    typeLenFound = false;
                    break;  // out of the WHILE
                }
            }
            else {    // byte did not match the next sync item
                syncNdx = 0;
            }
        }
    }
}

//============

void getTLVheader() {
    if (syncFound == true and typeLenFound == false) {
        byte numTypeLenBytes = 8;
        byte numTypeBytes = 4;
        byte typeArray = 0; //added for the loop. is it fine?
        byte lenArray = 0; //added for the loop. is it fine?
        byte typeLenCount = 0;
        
        
        while (Serial.available > 0) {
            if (typeLenCount < numTypeBytes) { //where you defined typeLenCount? Which kind of data does is stored? 
                typeArray[typeLenCount] = Serial.read(); 
            }
            else if (typeLenCount < numTypeLenBytes) {
                lenArray[typeLenCount - 4] = Serial.read();
            }
            typeLenCount ++;
            if (typeLenCount >= numTypeLenBytes) {
                typeLenFound = true;
                break;
            }
        }
    }
}

//============

void processTypeAndLength() {
    if (typeLenFound == true) {
        // code to figure out the type and the length
        dataLength = xxx; // assuming this is a global variable for convenience
        //** HERE I should check if typeArray = 07? **//
        waitingForData = true;
    }
}

//============

void getVdata() {
    static byte dataNdx = 0;
    if (waitingForData == true) {
        while (Serial.available > 0) {
            dataArray[dataNdx] = Serial.read();
            dataNdx++;
            if (dataNdx >= dataLength) {
                newData = true;
                waitingForData = false;
                break;
            }
        }
    }
}

By the way. Sorry but I had a problem with my user, so I had to creat a new one.

Thanks.

bullman2:
No, the 8 byte TLV header is not coming immediately after the SYNC bytes, it is always come after the first 52 byte of the frame header.

Sorry, I got that mixed up.

Do you want to save the data in the frame header or just skip over it? Either way you can just create another function in the style I have suggested - perhaps called getFrameHeader() - and move to that immediately after the SYNC bytes and from getFrameHeader() to getTypeAndLength().

That's why I structured the code the way I did, so it is easy to re-configure.

As for the processTypeAndLength() , I still don't understand how you figured out the receiving type and length of the TLV according to the getTLVheader()
Is dataLength ment to store the 4 bytes of the length of the TLV or the type of the TLV like 07?

I'm just going on the description in the document in the link in Reply #2. I believe it says that the TLV section has two 4-byte numbers (unsigned longs in Arduino speak). The first thing is to get the bytes - that's all my program tries to do. I think you can convert them to values like this (but I'm not a C++ expert). The ampersand indicates to use the address of the array

unsigned long typeValue = &typeArray;
unsigned long lenValue = &lenArray;

...R

As Julius Caesar once said, and it happens to be also an algorithmic method too - divide and conquer :slight_smile:
I think the best way to approach this kind of data is to go step by step.
So yes, the first step should be to save the data in the frame header and check that the SYNC is working properly. And in order to that we need to check if the checkForSync() give us 52 legitimized bytes that start with the right syncValues every time and then to move on to the rest of the functions.

I didn’t know how to write the getFrameHeader() so I added inside the checkForSync() some for loop with byte array variable and then print it in string in order to see the output.

This is the code:

void checkForSync() {
    byte FrameheaderData[52]; //for getting the frame header
    if (syncFound == false) {
        byte syncValues[] = {0x02, 0x01, 0x04, 0x03,0x06, 0x05, 0x08, 0x07};
        static byte syncNdx = 0;
        byte numSyncBytes = 8;
        byte rb; //added for the while loop
        while (Serial.available() > 0 ) {
            rb = Serial.read();
            if (rb == syncValues[syncNdx]) {
                for(int syncNdx=0; syncNdx<52; syncNdx++) //increment and print to show frame header data
                {
                   Serial.print("[" + String(syncNdx) + "]-");
                   FrameheaderData[syncNdx] = Serial.read();
                   Serial.print(String(FrameheaderData[syncNdx]) + "\t");
                   delay(100);
                }
                if (syncNdx > numSyncBytes) {
                    // we are at the end of the sync values
                    syncFound = true;
                    typeLenFound = false;
                    break;  // out of the WHILE
                }
            }
            else {    // byte did not match the next sync item
                syncNdx = 0;
            }
        }
    }
}

And this is what I get:

[0]-1   [1]-4   [2]-3   [3]-6   [4]-5   [5]-8   [6]-7   [7]-4   [8]-0   [9]-0   [10]-2  [11]-66 [12]-22 [13]-10 [14]-0  [15]-26 [16]-235        [17]-91 [18]-170        [19]-128        [20]-0  [21]-0  [22]-0  [23]-50 [24]-16 [25]-0  [26]-0       [27]-0  [28]-0  [29]-0  [30]-0  [31]-78 [32]-0  [33]-0  [34]-0  [35]-200        [36]-80 [37]-0  [38]-0  [39]-7  [40]-0  [41]-0  [42]-0  [43]-165        [44]-5  [45]-0  [46]-0  [47]-1  [48]-0  [49]-175        [50]-218        [51]-7       [0]-2   [1]-2   [2]-2   [3]-2   [4]-2   [5]-2   [6]-2   [7]-2   [8]-2   [9]-2   [10]-2  [11]-2  [12]-2  [13]-2  [14]-2

As you can see on the first frame that received from the radar is working almost fine (don’t know why the first 0x02 is missing from the array) but at the second one, it got messed up. Perhaps I did something wrong in the code or possibly there is a better solution.

I hope you could show me the way.

Thanks.

bullman2:
I didn't know how to write the getFrameHeader() so I added inside the checkForSync() some for loop with byte array variable and then print it in string in order to see the output.

That approach just makes the code complicated and difficult to debug.

Make a copy of getTypeAndLength() and change its name to getFrameHeader() and then change the code in the function so it saves the remaining 44 bytes into one array rather than saving parts into two separate arrays. And at the end make it set the condition for getTypeAndLength() to operate - maybe waitingForTypeLen = true; which would require an equivalent change at the top of getTypeAndLength()

You will also need to change the end of checkForSync() so it causes getFrameHeader() to operate. Perhaps instead of typeLenFound = false; you could put waitingForHeader = true;

The idea behind my suggestion in Reply #9 was to present a structured approach that can be extended and adapted to different circumstances. May I suggest more study? :slight_smile:

...R

Ok I see your point…
Well, here are the changes I made in the code from your reply #9 :

// Receiving binary data with SyncNumber
byte syncFound = 0; //is is fine to add it here?
static boolean waitingForHeader = false;; //added for the while loop. is it fine?

void setup() {                      //added for the Serial comm
    Serial.begin(921600);
    Serial.println("<Ready>");
}

void loop() {
    checkForSync();
    getFrameHeader();
    getTypeAndLength();
    processTypeAndLength();
    getVdata();
   
    // other code
}

//============

void checkForSync() {
    if (syncFound == false) {
        byte syncValues[] = {0x02, 0x01, 0x04, 0x03,0x06, 0x05, 0x08, 0x07};
        static byte syncNdx = 0;
        byte numSyncBytes = 8;
        byte rb; //added for the while loop
        while (Serial.available > 0) {
            rb = Serial.read();
            if (rb == syncValues[syncNdx]) {
                syncNdx++;
                if (syncNdx > numSyncBytes) {
                    // we are at the end of the sync values
                    syncFound = true;
                    waitingForHeader = true;
                    break;  // out of the WHILE
                }
            }
            else {    // byte did not match the next sync item
                syncNdx = 0;
            }
        }
    }
}

//============

void getFrameHeader() {
    if (syncFound == true and waitingForHeader == true) {
        byte numTypeLenBytes = 8;
        byte numTypeBytes = 4;
        byte typeArray = 0; //added for the loop. is it fine?
        byte lenArray = 0; //added for the loop. is it fine?
        static byte typeLenCount = 0; // where is should be define and how?
       
        while (Serial.available > 0) {
            if (typeLenCount < numTypeBytes) { //where you defined typeLenCount? Which kind of data does is stored?
                typeArray[typeLenCount] = Serial.read();
            }
            else if (typeLenCount < numTypeLenBytes) {
                lenArray[typeLenCount - 44] = Serial.read();
            }
            typeLenCount ++;
            if (typeLenCount >= numTypeLenBytes) {
                typeLenFound = true;
                break;
            }
        }
    }
}

However, the issue that I have from the start is on the typeLenCount and I wrote it at my first remark.
I tried to define it myself, but I am getting in the compiler the next error: “expression must have pointer-to-object type” and I don’t know what to do with this variable. I need your help with the code here.

Thanks.

You need to post the complete program so I can try to compile it myself.

…R

// Receiving binary data with SyncNumber
byte syncFound = 0; //is is fine to add it here?
static boolean waitingForHeader = false;; //added for the while loop. is it fine?

void setup() {                      //added for the Serial comm
    Serial.begin(921600);
    Serial.println("<Ready>");
}

void loop() {
    checkForSync();
    getFrameHeader();
    getTypeAndLength();
    processTypeAndLength();
    getVdata();
   
    // other code
}

//============

void checkForSync() {
    if (syncFound == false) {
        byte syncValues[] = {0x02, 0x01, 0x04, 0x03,0x06, 0x05, 0x08, 0x07};
        static byte syncNdx = 0;
        byte numSyncBytes = 8;
        byte rb; //added for the while loop
        while (Serial.available > 0) {
            rb = Serial.read();
            if (rb == syncValues[syncNdx]) {
                syncNdx++;
                if (syncNdx > numSyncBytes) {
                    // we are at the end of the sync values
                    syncFound = true;
                    waitingForHeader = true;
                    break;  // out of the WHILE
                }
            }
            else {    // byte did not match the next sync item
                syncNdx = 0;
            }
        }
    }
}

//============

void getFrameHeader() {
    if (syncFound == true and waitingForHeader == true) {
        byte numTypeLenBytes = 8;
        byte numTypeBytes = 4;
        byte typeArray = 0; //added for the loop. is it fine?
        byte lenArray = 0; //added for the loop. is it fine?
        static byte typeLenCount = 0; // where is should be define and how?
       
        while (Serial.available > 0) {
            if (typeLenCount < numTypeBytes) { //where you defined typeLenCount? Which kind of data does is stored?
                typeArray[typeLenCount] = Serial.read();
            }
            else if (typeLenCount < numTypeLenBytes) {
                lenArray[typeLenCount - 44] = Serial.read();
            }
            typeLenCount ++;
            if (typeLenCount >= numTypeLenBytes) {
                typeLenFound = true;
                break;
            }
        }
    }
}

//============

void getTypeAndLength() {
    if (syncFound == true and typeLenFound == false) {
        byte numTypeLenBytes = 8;
        byte numTypeBytes = 4;
        byte typeArray = 0; //added for the loop. is it fine?
        byte lenArray = 0; //added for the loop. is it fine?
       
        while (Serial.available > 0) {
            if (typeLenCount < numTypeBytes) { //where you defined typeLenCount? Which kind of data does is stored?
                typeArray[typeLenCount] = Serial.read();
            }
            else if (typeLenCount < numTypeLenBytes) {
                lenArray[typeLenCount - 4] = Serial.read();
            }
            typeLenCount ++;
            if (typeLenCount >= numTypeLenBytes) {
                typeLenFound = true;
                break;
            }
        }
    }
}

//============

void processTypeAndLength() {
    if (typeLenFound == true) {
        // code to figure out the type and the length
        dataLength = xxx; // assuming this is a global variable for convenience
        //** HERE I should check if typeArray = 07? **//
        waitingForData = true;
    }
}

//============

void getVdata() {
    static byte dataNdx = 0;
    if (waitingForData == true) {
        while (Serial.available > 0) {
            dataArray[dataNdx] = Serial.read();
            dataNdx++;
            if (dataNdx >= dataLength) {
                newData = true;
                waitingForData = false;
                break;
            }
        }
    }
}

I just tried compiling that program and it has a lot of silly errors. Please correct them.

...R