I am reading data using two no of serial ports. My data is nothing but count down time generated by a clock generator. After Every one second the new data arrives.
Code:
int main(int argc, char **argv)
{
fd1 = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd1 == -1)
{
perror("open_port: Unable to open /dev/ttyS1 - ");
return 0;
}
else
{
x=fcntl(fd1,F_GETFL,0);
fcntl(fd1, F_SETFL, 0);
}
initport(fd1, 9600);
printf("baud=%d\n", getbaud(fd1));
fd2 = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd2 == -1)
{
perror("open_port: Unable to open /dev/ttyS2 - ");
return 0;
}
else
{
y=fcntl(fd2,F_GETFL,0);
fcntl(fd2, F_SETFL, 0);
}
initport(fd2, 9600);
printf("baud=%d\n", getbaud(fd2));
while(1)
{
FD_ZERO(&input);
FD_SET(fd1, &input);
FD_SET(fd2, &input);
max_fd = (fd2 > fd1 ? fd2 : fd1) + 1;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
n = select(max_fd, &input, NULL, NULL, &timeout);
if (n < 0)
perror("select failed");
else if (n == 0)
{
puts("TIMEOUT");
}
else
{
if (FD_ISSET(fd1, &input))
{
if(readport(fd1,serial1 ))
{
printf("read Failed\n");
exit(0);
}
else
{
if(serial1[0]==1)
printf(" \n the serial 1 data %s",serial1);
}
}
if(FD_ISSET(fd2, &input))
{
if(readport(fd2,serial2 ))
{
printf("read Failed\n");
exit(0);
}
else
{
if(serial2[0]==1)
printf(" \n the serial 2 data %s",serial2);
}
}
}//else of select
}//while
close(fd1);
close(fd2);
}//main
Both of serial ports receive the same data like 01:05:11 which will be counting down.
The code works fine but when i remove input from one serial port and again put it back then it should get the time which is comming.But both serial port are not reading data simultaneously but the are reading in alternate ie if one serial port reads 01:05:11 the other reads 00:00:00 and vice versa. I want that both ports should read simultaneously.
another problem is that when i am removing one serial port the removal of serial port from fd_set is not reflecting immediately but after 20-30 seconds. e.g if two fds are there and i remove one input the only one fd should be there but it shows two fds for some time and after 20-30 second it show only one fd
Please tell where is problem in the code.