fd_fgets for grabbing lines from fds returned by OpenSerial?

Discussion to talk about software related topics only.
Post Reply
Ridgeglider
Posts: 513
Joined: Sat Apr 26, 2008 7:14 am

fd_fgets for grabbing lines from fds returned by OpenSerial?

Post 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;
}
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

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

Post 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.
Post Reply