do i need to clear the buffer?

hi..im still a noobie.im going to control a servo using arduino hardware uart. i will send 4byes of command packet and the servo will return 2btyes of response.
my question is since the buffer has only 128byte(default). do i need to clear the rx buffer if i keep sending and returning byte?what command is for clearing the buffer or i need to write a function for clearing?Sorry another noob question..does the return current; automatically clear the buffer?

there goes my code with full of bugs!help me point my error if possible thanks.

#include <math.h>
#define HEADER 0xff
#define Speedlevel xxxx // hex value to represent the speedlevel
#define servoID xxxx // hex value to represent the servoID
#define position xxxx // any value from 0-255(byte/hex)that represents position

void setup()
{
Serial.begin(9600); // set up baudrate at 9600 bps
PosSend();
}

void loop()
{

}

void SendOperCommand(char Data1, char Data2)
{
char CheckSum;
CheckSum = (Data1^Data2)&0x7f;
serial.println(HEADER.BYTE);
serial.println(Data1.BYTE);
serial.println(Data2.BYTE);
serial.println(CheckSum.BYTE);
}

char PosSend(char ServoID, char SpeedLevel, char Position)
{
char Current;
SendOperCommand((SpeedLevel<<5)|ServoID, Position);
serial.read(); //read response packet?
Current =serial.read();
return Current;
}

no one able to help?

Data is removed from the RX buffer as you read it (with Serial.read() ).

-j

You could have some code in setup to ensure the receive buffer is clear when you startup, but I have not found this necessary
Serial.begin(9600);
while(Serial.available())
Serial.read();

Data is removed from the send buffer when it is sent so nothing needs to be done there.

GREAT!!! Thanks guys as u can see i am really a greenhorn

wait...one more stupid question

does the servo automatically response with 2 bytes as stated in the manual?does it mean that the rx buffer will accumulate if i didnt call for Serial.Read(); to clear it?

I think that flushing the buffer would be a good idea.
There's a "serialFlush" in wiring_serial.c (with the rest of the serial support routines), but it doesn't seem documented or "connected" to the "Serial.xxx" functions used by arduino, so I'm not sure of its status. You can also do something like:

while (Serial.read() > 0)
  delay(1); /* do nothing else */

Aside from "obvious" bugs like not captializing "Serial", and using "Header.BYTE" when you should have "Header, BYTE)", your sample code has a more subtle bug for you to be aware of. When you do "Serial.print()..." on a port at 9600bps, it will take approximately 2ms before the last character actually reaches the connected device, and another millisecond before you could possibly receive the first byte of response. So if your code says:

Serial.print(command);
return(Serial.read());

It will most likely ALWAYS return either -1 (because there won't be any data for another several milliseconds), or a response code that is actually "left over" in the buffers from a previous command. Dealing with the sort of "asynchronous" even from another device is one of the more challenging aspects of programming in the "physical computing" domain...

There's a "serialFlush" in wiring_serial.c

Yes, and it is directly called by public member HardwareSerial::flush(), so I would imagine that the recommended usage would be to call

Serial.flush();

in this situation.

Mikal

Data is removed from the RX buffer as you read it (with Serial.read() ).

so which one should i use to clear the rx buffer? serial.flush() or just serial.read() will do?its kinda contradicting.

does the servo automatically response with 2 bytes as stated in the manual?does it mean that the rx buffer will accumulate if i didnt call for Serial.Read(); to clear it?

can someone clear my doubts?

Just do the serial reads, this will remove a byte each time it is called. Any received data will build up in the buffer until you make the calls to Serial.read() to get the data out.

ROGERED...I will test out and update this thread again..thanks brother

Serial.flush() is the fastest way to clear the RX buffer, but looping on Serial.read() will do it too. I didn't realize flush() was documented, but it is: Serial.flush() - Arduino Reference.

does the servo automatically response with 2 bytes as stated in the manual?

Without seeing the manual, I would guess so. So if you're not interested in the servo data until a certain point, just do a Serial.flush() at that time and then wait for data to arrive using Serial.available().

Mikal

thanks for the replies ..i will update this thread again