In the following C program for an embedded device, I am trying to display a dot (".") every time a user, on a remote computer connected by a serial cable to my device, enters some characters on her terminal program and hits the ENTER key.
What I am seeing is that once the first carriage return is detected, the printf displays dots in an infinite loop. I was expecting FD_ZERO et FD_CLR to "reset" the wait condition.
How to?
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
main()
{
int fd1; /* Input sources 1 and 2 */
fd_set readfs; /* File descriptor set */
int maxfd; /* Maximum file desciptor used */
int loop=1; /* Loop while TRUE */
/*
open_input_source opens a device, sets the port correctly, and
returns a file descriptor.
*/
fd1 = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd1<0)
{
exit(0);
}
maxfd =fd1+1; /* Maximum bit entry (fd) to test. */
/* Loop for input */
while (loop)
{
FD_SET(fd1, &readfs); /* Set testing for source 1. */
/* Block until input becomes available. */
select(maxfd, &readfs, NULL, NULL, NULL);
if (FD_ISSET(fd1, &readfs))
{
/* input from source 1 available */
printf(".");
FD_CLR(fd1, &readfs);
FD_ZERO( &readfs);
}
}
}
select()?