Page 1 of 1

fd_fgets for grabbing lines from fds returned by OpenSerial?

Posted: Wed Oct 15, 2008 7:24 am
by Ridgeglider
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;
}

Re: fd_fgets for grabbing lines from fds returned by OpenSerial?

Posted: Thu Oct 16, 2008 4:24 pm
by rnixon
There are only two other alternatives I can think of at the moment:

1. Write your own serial driver that handles cr/lf (probably not a good option)

2. You can read all available characters into a buffer, and then parse the buffer looking for the lf. Not a whole lot better, but probably less overhead.