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]