I'm using the UART_read() function in blocking mode, looking for the 1st two header bytes of a packet to uniquely identify that packet.
Based on the header test, I'll know how many more bytes to look for, in the case below it would be 6 more. This scheme works well, when only good, known packets come in, but I'm having a hard time dealing with garbage using the blocking mode, I cannot predict whether the byte stream will have as many or as few as the 2 or the 6 in the example below.
So I thought I could "flush" the uart stream of the garbage in a while loop reading 1 byte at a time until "num_char" returned 0, but that never happens.
num_char=UART_read(uart, &input[i++], 2); //parse 1st 2 bytes
if((input[i-1]==0x21)&&(input[i]==0x04)) //test for 0x21, 0x04
{
i++;
num_char=UART_read(uart, &input[i++],6); //get six more bytes
if(!Parse_IF1AM1(0))
ifam1_cnt++;
i=0;
data_ok=1;
}