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”);
}
}
Tasty SoftwareI couldn’t think of anything from Windows. David is a total geek. I’m sure glad I’m not like that….
That’s very good David. Very artistic.
Thanks Tom. I do try. Did you like my use of memset instead of the old bzero call?