trying to parse a serial read array

I need help again. I have the data being in a complete message in my tempArray[ ]. If I serial print the full tempArray I can see the message is complete and all of the byte segments are the correct length.

This message contains a header of 27 data bytes plus a 0x00 followed by 30 byte segments for each satelite. Each segment starts with either a 0x01 or 0x02 and each ends with a 0x00 byte.
I want to take the 30 byte segments and save them to an external FRAM but first I am trying to serial print ln the section of the array.
I am trying to check for the 1 or 2 at the beginning and then check for the 0 at the end of the segment. However when I run this I get 2 satelites worth of data then it just runs on collecting bytes forever. Where am I going wrong here?

case pF5sCollectTheData:  //header is 27 bytes, 1 null 0x00, message 30 per sv last byte of 30 is 0x00
      TimerA = millis();
      if(n<z){
        Serial2_WaitForByte(&inbyte);
        if(inbyte == 0x03){
          if(tempArray[n] == 0x10){
            n++;
            tempArray[n] = inbyte;
            z=n+1;                  //sets z for next phase to 1 byte past 0x03
            state = F5parse;
          }
          else{   //if not end byte advance and store in array
            n++;
            tempArray[n] = inbyte;
          }
        }
        else{
          n++;
          tempArray[n] = inbyte;
        }
      }
      else{
        TimerA = millis();
        state = F5parse;
      }
      if (millis()-TimerA >= 400UL){
        Serial.println("time out error F5collect data");
        state = Fini;
      }
      break;


    case F5parse:          //check for and remove double 0x10 bytes sent from device
      Serial.println("F5 parse begin");
      for(n=0; n<z; n++){
        if(tempArray[n] == 0x10 && tempArray[n+1] == 0x10){ 
          for(w = n; w < z; w++){
            tempArray[w] = tempArray[w+1]; //recopy all bytes after first 0x10
          }
          z=w+1;
        }
      }
      for(n=0; n<z; n++){
        if(tempArray[n] == 0x03 && tempArray[n-1] == 0x10){  //shorten any extra to end bytes
          z = n+1;
        }
      }
      Serial.println("entering F5 save data state");
      for(n=0; n<28; n++) {      //header info
        //WriteF5h(addr,n*8,tempArray[n]);
        Serial.println(tempArray[n]);
      }
      Serial.println("split");
      TimerA = millis();
      state = F5saveData;

      if (millis()-TimerA >= 400UL){
        Serial.println("time out errorF5 parse data");
        state = Fini;
      }
      break;



    case F5saveData:   //go through tempArray

      for( n=28; n< z-30; n++){
        if(tempArray[n] == 0x01){  //glonass 30 bytes data good
          if(tempArray[n+29] == 0x00){
            for(int i=n; i<i+29; i++){
              //WriteF5hGLO(addr,i*8,tempArray[i]);
              Serial.println(tempArray[i]);
            }
            Serial.println("split");
            countGLO++;
          }  
        }
        if(tempArray[n] == 0x02){   //gps 30 bytes data good
          if(tempArray[n+29] == 0x00){
            for(int i=n; i<i+30; i++){
              //WriteF5hGPS(addr,i*8,tempArray[i]); 
              Serial.println(tempArray[i]); 
            }
            Serial.println("split");
            countGPS++;
          }
        }
      }
      state = Fini;
      if (millis()-TimerA >= 400UL){
        Serial.println("time out errorF5 save data");
        state = Fini;
      }
      break;

I have no idea what that's doing. Can you reduce the problem to a pre-declared byte array containing some sample data you want to manipulate and explain what you're trying to do to it? I mean, a sketch that just does that one operation you're having trouble with.

This is where I'm getting stuck. As far as sample data, any byte values will work in the data section but as an example:
(header data) 27 bytes any value, 0x00, (first sat) 0x01, 0x08, 27 bytes any value, 0x00, (second sat) 0x02, 0x08, 27 bytes any value, 0x00, (end bytes) 0x10, 0x03.

I want to sort the satelite data by if they start with 0x01 or 0x02 and save the 30 bytes of data.

case F5saveData:   //go through tempArray

      for( n=28; n< z-30; n++){
        if(tempArray[n] == 0x01){  //glonass 30 bytes data good
          if(tempArray[n+29] == 0x00){
            for(int i=n; i<i+29; i++){
              Serial.println(tempArray[i]);
            }
            countGLO++;
          }  
        }
        if(tempArray[n] == 0x02){   //gps 30 bytes data good
          if(tempArray[n+29] == 0x00){
            for(int i=n; i<i+30; i++){
              //WriteF5hGPS(addr,i*8,tempArray[i]); 
              Serial.println(tempArray[i]); 
            }
             countGPS++;
          }
        }
      }
      state = Fini;
           break;

JHEnt:
This is where I'm getting stuck.

Sorry, you aren't getting it.

I mean write a real complete sketch the compiles and runs that reproduces the problem. For example, perhaps something like:

byte data[] { 0x01, 0x08, ...};

void setup()
{
}

void loop()
{
    // do whatever it is you're trying to do to data, or show your attempt and explain in terms of this code what you're trying to achieve
}

How does Serial2_WaitForByte(&inbyte); report a timeout to the caller ? What gets written into inbyte if no data comes in after a certain amout of time ?

This will compile and run and gives me the same basic result out of the serial monitor

I want to start at the array position 28, check to see if the byte is a 0x01 or 0x02
if it's 1h or 2h then check the byte 29 array positions later
if it's a 0x00 then that is a good 30 byte segment
if the 30 bytes are good data then print the 30 bytes(for now, later save)
advance the next check to the next byte in the array past the printed 30 bytes

if the byte checked is not 0x01 or 0x02 then advance 1 position and start checking for 1h or 2h again.

unsigned long TimerA;

int c; //channels with data message 5D
int z = 150;
int countGPS;
int countGLO;
byte tempArray[] = {
  0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x00, 0x02,0x05,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x00, 0x01,0x08,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x00, 0x01,0x09,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x00, 0x02,0x03,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x00, 0x10,0x03};


void setup(){
  Serial.begin(115200);     // error reporting output
  Serial2.begin(115200);
}

bool Serial2_WaitForByte(byte* v)
{
  TimerA = millis();
  while ( Serial2.available() <=0)
  {
    if (millis()-TimerA >= 100UL){
      return false;
    }
  }
  *v = Serial2.read();
  return true ;
}

void inMessage()
{
  Serial.println("inMessage begin"); //debug
  int n;
  int i;

  for( n=28; n< z; n++){
    if(tempArray[n] == 0x01){  //glonass 30 bytes data good
      if(tempArray[n+29] == 0x00){
        for(int i=n; i<i+29; i++){
          Serial.println(tempArray[i]);
        }
        Serial.println("split");
        countGLO++;
      }  
    }
    if(tempArray[n] == 0x02){   //gps 30 bytes data good
      if(tempArray[n+29] == 0x00){
        for(int i=n; i<i+30; i++){
          Serial.println(tempArray[i]); 
        }
        Serial.println("split");
        countGPS++;
      }
    }
  }
}

void loop(){
  Serial.println("loop start"); //debug 
  inMessage(); 
}

This will compile and run and gives me the same basic result out of the serial monitor

Hard to believe...

        for(int i=n; i<i+29; i++){
          Serial.println(tempArray[i]);
        }

Starting with i equal 28, while i less than i+29, print a character and increment i. That is going to print a boatload of characters.

        for(int i=n; i<i+30; i++){
          Serial.println(tempArray[i]); 
        }

Same problem here.

I think you want the while part to be i<n+29 or 30.