fd_fgets for grabbing lines from fds returned by OpenSerial?
Posted: Wed Oct 15, 2008 7:24 am
We have fgets() for reading lines form stdin, and f_fgets() for reading lines from EFFS volumes. I'm looking for an easy way to do the same thing from the input to non-stdio serial ports accessed via an fd returned by call to OpenSerial() and wrote the following. Is there an easier way or an existing function? Just seems like reading char-by-char would be dog slow, but I guess this is required if you need to find the terminator LF char?
Code: Select all
char * fd_fgets( char *buf, int len, int fd ) {
int i = 0;
buf[i] = '\0';
const char Terminator = 0x0A;
bool bTerminator = FALSE;
while ( (i < len) && (buf[i] != EOF) && (!bTerminator) )
{
int n = read( fd, &buf[i], 1 );
if ( buf[i] == Terminator )
bTerminator = TRUE;
if ( n == 1 )
i++;
}
buf[i] = '\0';
return buf;
}