Home > Archive > Unix Programming > June 2007 > Select returns without having any data in serial port buffer
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Select returns without having any data in serial port buffer
|
|
|
| Hi
I have opened a serial port and then blocked on a select system call .
but select returns even if no data is comming from other end. So when
I read the buffer after select returns, read gives the number of 0s in
buffer and this continues very fast.
void main()
{
int fd_uart =-1 ;
int retval,cErr;
fd_set read_fds;
int max_fd = 0;
/* intialize the data set */
if( fd_uart = open("/dev/ttyS0", O_RDWR | O_NOCTTY ) == -1)
{
exit(0);
}
FD_ZERO( &read_fds );
FD_SET(fd_uart, &read_fds );
max_fd = fd_uart;
while(1)
{ /* This select returns very fast*/
select( max_fd + 1, &read_fds, 0, 0, NULL);
printf("\nData\n");
}
}
| |
| Bin Chen 2007-06-19, 4:16 am |
| On Jun 19, 12:30 pm, alok <alok....@gmail.com> wrote:
> Hi
>
> I have opened a serial port and then blocked on a select system call .
> but select returns even if no data is comming from other end. So when
> I read the buffer after select returns, read gives the number of 0s in
> buffer and this continues very fast.
>
> void main()
> {
> int fd_uart =-1 ;
> int retval,cErr;
> fd_set read_fds;
> int max_fd = 0;
>
> /* intialize the data set */
> if( fd_uart = open("/dev/ttyS0", O_RDWR | O_NOCTTY ) == -1)
> {
> exit(0);
> }
>
> FD_ZERO( &read_fds );
> FD_SET(fd_uart, &read_fds );
> max_fd = fd_uart;
>
> while(1)
> { /* This select returns very fast*/
> select( max_fd + 1, &read_fds, 0, 0, NULL);
> printf("\nData\n");
> }
>
> }
You shouldn't coding like this, you should look at the return value to
check why select returns.
man select
| |
| Alan Curry 2007-06-19, 4:16 am |
| In article <1182227444.270607.280580@i38g2000prf.googlegroups.com>,
alok <alok.net@gmail.com> wrote:
>Hi
>
>I have opened a serial port and then blocked on a select system call .
>but select returns even if no data is comming from other end. So when
>I read the buffer after select returns, read gives the number of 0s in
>buffer and this continues very fast.
>
>void main()
In a C program, main returns int.
>{
> int fd_uart =-1 ;
> int retval,cErr;
> fd_set read_fds;
> int max_fd = 0;
>
> /* intialize the data set */
> if( fd_uart = open("/dev/ttyS0", O_RDWR | O_NOCTTY ) == -1)
The relative precedence of = and == is not what you think. You've set fd_uart
to either 0 or 1, depending on whether the open failed.
gcc -Wall says:
warning: suggest parentheses around assignment used as truth value
> {
> exit(0);
> }
If the open failed, fd_uart was set to 1 and then the if() condition was true
so you've exited. If you got this far, the open must have succeeded, and
fd_uart has been set to the result of the == operator, which is 0.
>
> FD_ZERO( &read_fds );
> FD_SET(fd_uart, &read_fds );
> max_fd = fd_uart;
>
> while(1)
> { /* This select returns very fast*/
> select( max_fd + 1, &read_fds, 0, 0, NULL);
Now you're selecting on file descriptor 0 (stdin) instead of the one you
opened.
--
Alan Curry
pacman@world.std.com
|
|
|
|
|