How to empty input buffer

Hello all!

Currently I am running a program which is able to read in data values from a digital compass thru the serial port. My problem is that the values are not refreshed each time a new value comes in. I suspect this has something to do with the buffer. So what command do i use to constantly flush out the old input values in the buffer so that new values are able to be displayed?

Thanks in advance.


You’ll have to show a bit more code for us to be able to help…

Maybe setvbuf ()

If you want to flush your client fifo’s in the devc-* serial driver, tcflush() is what you want

This is part of my code. What I want to do is that every time the digital compass is moved, a new coordinate"$b" is supposed to come in but what happens now is that the new coordinate “$b” which comes in is not displayed, instead it keeps on displaying the previous coordinate"$a" . Only when I terminate the program and re-run it, the coordinate “$b” will then appear. So what flush command should I use and where should I insert it into my code? The values which I want to capture and display is about 17 characters long. The coordinates need to be refreshed and displayed every time the digital compass is moved.
Thanks in advance. :slight_smile:

int myReadFunc(int fd, void *data, unsigned mode)
int iFileDescriptor, iBytesRead, iExit=0;
int tempi;
char szMessage[256];
char char_buffer4[256];

//Read in any data if present
iBytesRead = read(iFileDescriptor, szMessage, 100);
for (tempi=0; tempi<sizeof(szMessage); tempi++)
printf(char_buffer4, “%c”, szMessage[tempi]);

 //Check if data was successfully read
    //Check if the first letter is $
    if(szMessage [0] =="$")

return (Pt_CONTINUE);

You are looping on the size of the buffer rather then on the bytes returned. read() does not assure that you will read sizeof(szMessage) (or the value you pass in) each time, but up to that many.

for( tempi=0; tempi<iBytesRead; tempi++ ) {
   printf( "%d", szMessage[tempi]);

Also, if you are trying to copy the values into a buffer (char_buffer4) you can just copy them in without using printf().

int buffer4count = 0;
while(iExit == 0) {
    // ....
    for( tempi=0; tempi<iBytesRead && buffer4count < sizeof(char_buffer4); tempi++ ) {
        char_buffer4[buffer4count] = szMessage[tempi];
        buffer4count ++;
    // ...