Ho to clear the Mega 2560 Serial1 data

How do you clear the data from the UART in the Mega?
I can read bytes from the UART but I cannot figure out how to clear those bytes after I have read them. The Searl1.Flush does not do it.

void setup() {
	// initialize both serial ports:
	Serial.begin(115200);
	Serial1.begin(38400);
	Serial.println("Init Complete");
}

bool WaitForSync()
{
	bool ret = false;
	if (Serial1.available()) 
	{
		byte static inByte1 = Serial1.read(); 
		if (inByte1 == 0xB5)
		{
			Serial.println("found B5");
		}
	}
	if (Serial1.available()) 
	{
		byte static inByte2 = Serial1.read();
		if (inByte2 == 0x62)
		{
			Serial.println("found 62");
			ret = true;
		}
	}
	return ret;
}

void loop() 
{
	if ( WaitForSync()== true)
	{
		Serial.println("found");
	}; 
}

This chunk of code simply repeats over and over that it has found B5 and 62 but I know for sure that Its only getting one set.
Thanks.......... Jim

How do you clear the data from the UART in the Mega?

if (Serial1.available())

This line answer your question very well. Serial1.available() > 0 means data available in buffer vice versa.

The value of variables defined as static persist between calls to the function. So, the first time the function is called the variables are created and values assigned to them. The second and subsequent times the variables already have values, hence your problem. Was there a reason for you to declare them as static ?

How do you clear the data from the UART in the Mega?
I can read bytes from the UART but I cannot figure out how to clear those bytes after I have read them.

The serial library handles that for you automatically. When you read the character the serial library manages the circular interrupt driven input buffer, updating it to reflect that you have read that buffer entry.

Lefty

Thanks UK Bob. The problem was the static variable.
Its hard to know what was going through my mind when I defined them as static.

Thanks... again :astonished: