06-19-07 12:22 PM
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_uar
t
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 tru
e
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
[ Post a follow-up to this message ]
|