Serial.flush() not working?

Hey guys, I'm having what I think is a bit of an issue with serial.flush. I'm trying to capture 5 bytes off of the serial port and do some work with them. If say 7 bytes come into the port and I determine that the first 5 are invalid, I'm trying to issue a serial.flush with the intent of clearing out all of the data that is in the serial buffer before my code picks it up, but it seems that when I issue a serial.flush, nothing happens. I still have plenty of data in the serial buffer to parse through. Here is the code, any thoughts?

/*

  • Robots hate me
  • Control a robot's movement via a serial connection
  • Assumes DC motors controlled with PWM

*/
#include <string.h>

//Setup
void setup()
{
Serial.begin(19200); //setup the serial port for 192008N1
Serial.flush();
}

void GetString(char *buf, int bufsize) //Get the command from the serial port
{

int i;
Serial.flush();
for (i=0; i<bufsize - 1; ++i) //start feeding the buf variable
{
while (Serial.available() == 0); // wait for character to arrive
buf = Serial.read(); //read data into buf
_ if (buf == '\0' ) // is it the terminator byte?_
* break;*
* }*
}
void loop()
{
* char cmd[3]; //The first two bytes of the serial buffer*
* char param[4]; //The last three bytes of the serial buffer*
* char buffer[6]; //The whole buffer*
* GetString(buffer, sizeof(buffer)); //capture the command from the serial port*
* cmd[0] = buffer[0];*
* cmd[1] = buffer[1];*
* cmd[2] = '\0'; //force a terminator byte*
* param[0] = buffer[2];*
* param[1] = buffer[3];*
* param[2] = buffer[4];*
* param[3] = '\0'; //force a terminator byte*
* if (strncmp(buffer, "LF", 2)== 0 ) //Left Forward*
* {*
* Serial.print("Left Forward ");*
* Serial.print(param);*
* Serial.println("%");*
* Serial.flush();*
* }*
* else if (strncmp(buffer, "LR", 2)== 0 ) //Left Reverse*
* {*
* Serial.print("Left Reverse ");*
* Serial.print(param);*
* Serial.println("%");*
* Serial.flush(); *
* }*
* else if (strncmp(buffer, "RF", 2)== 0 ) //Right Forward*
* {*
* Serial.print("Right Forward ");*
* Serial.print(param);*
* Serial.println("%");*
* Serial.flush();*
* }*
* else if (strncmp(buffer, "RR", 2)== 0 ) //Right Reverse *
* {*
* Serial.print("Right Reverse ");*
* Serial.print(param);*
* Serial.println("%");*
* Serial.flush();*
* }*
* else*
* {*
* Serial.println("crap input");*
* Serial.println(buffer);*
* Serial.flush();*
* return;*
* }*
}
[/quote]

I've got a feeling it has to do with timing (i.e. when you call Serial.flush()) and synchronization. You probably want to look for a start or end of record delimiter to know when to start filling the buffer.

I see you appear to be using '\0' as a record terminator. Keep in mind that, while C strings are terminated with a null character internally, they are not part of the string and are not transmitted when you print a string.

-j

interesting, that makes sense to me but I'm not real sure how to work around it. The condition I'm trying to address is what if I get noise on the serial line and way more then 5 bytes come in. Instead of looking for a terminator, maybe if I look for a preamble and then count 5 bytes?

Instead of looking for a terminator, maybe if I look for a preamble and then count 5 bytes?

That works. That's how NMEA GPS sentences work - they all start with a $. If the first character isn't a $, skip until you get one. NMEA also uses checksums, but that's another level of sophistication/complexity.

Have you actually experienced noise on the serial line? It's pretty rare to see data corruption on a standard (RS232/EIA232) serial line. If maybe you're using US$10 a pair RF modules, then I'd use a start delimiter and a checksum.

-j

I haven't seen any noise yet, but I plan on using XBee modules to control a tracked vehicle and I'm worried about it getting out of range and just getting partial data or something like that.

Maybe someone who is familiar with Xbee can chime in here and tell us if Xbee includes reliable delivery?

-j

It appears that timing was indeed the issue. I cranked the serial up to 115200 and added a 53ms delay when I detect bad data and that took care of it!