📬❄️📦Adafruit Holiday Shipping Deadlines 2019: Please place all UPS 3 Day orders by 11am ET Friday December 13 📬❄️📦
0

do i need to clear the buffer?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

do i need to clear the buffer?

by gr33nhorn on Sun Oct 19, 2008 2:51 am

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.
Code: Select all | TOGGLE FULL SIZE
#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;
}
gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Re: do i need to clear the buffer?

by gr33nhorn on Mon Oct 20, 2008 9:53 am

no one able to help me?
gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Re: do i need to clear the buffer?

by westfw on Mon Oct 20, 2008 11:17 am

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:
Code: Select all | TOGGLE FULL SIZE
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:
Code: Select all | TOGGLE FULL SIZE
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...
westfw
 
Posts: 1583
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Please be positive and constructive with your questions and comments.