Home > Geekspeak > Circular Buffer For Reading One Line At A Time

Circular Buffer For Reading One Line At A Time

January 30th, 2004

Here is the code I just wrote for a circular buffer to read one line at a time.
memset(buffer,’\0′,BUFF_SIZE);
j=0;
for( ; ; ) {
memset( tmp,’\0′, BUFF_SIZE );
GetAccessToFD( SERIAL );
n = read( serialfd, tmp, BUFF_SIZE);
ReleaseFD( SERIAL );

if( n == 0 || errno == EAGAIN ) {
//error in reading of port
//No Data Do nothing.
}
else if(n > 0) {
for (i=0; i<=n; i++)
{
if (tmp[i] == ‘\n’)
{
buffer[j] = ‘\0′; //Null Terminate String.
ProcessSerialCommand(buffer);
memset(buffer,’\0′,BUFF_SIZE);
j=0;
}
else if (tmp[i] != ‘\0′)
{
buffer[j] = tmp[i];
j++;
}
}
}
else {
printf(”Error Reading in ExecuteSerialReceive\n”);
}
}

Geekspeak

  1. January 30th, 2004 at 12:09 | #1

    Tasty SoftwareI couldn’t think of anything from Windows. David is a total geek. I’m sure glad I’m not like that….

  2. February 1st, 2004 at 21:50 | #2

    That’s very good David. Very artistic.

  3. David
    February 2nd, 2004 at 07:52 | #3

    Thanks Tom. I do try. Did you like my use of memset instead of the old bzero call?

  1. No trackbacks yet.