Circular Buffer For Reading One Line At A Time

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”);
}
}

4 Responses to “Circular Buffer For Reading One Line At A Time”

  1. footboot Says:

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

  2. Tom Says:

    That’s very good David. Very artistic.

  3. David Says:

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

Leave a Reply