Select returns without having any data in serial port buffer
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Select returns without having any data in serial port buffer




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Select returns without having any data in serial port buffer  
alok


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-19-07 06:18 AM

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");
}


}






[ Post a follow-up to this message ]



    Re: Select returns without having any data in serial port buffer  
Bin Chen


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-19-07 06:18 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






[ Post a follow-up to this message ]



    Re: Select returns without having any data in serial port buffer  
Alan Curry


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
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 ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:25 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register