select() official example code doesn't work on my PC??

Can anybody help me to solve this strange problem?

This code snippet copied out from QNX6.3 help system, but the select() function always return with zero (timed out) after 5 secs:
(“select timed out” )
Can anyone give a short explanation what can be the problem, please?
Any help are welcome…

It has compiled as follows:
qcc -o console console.c

/*

  • This example opens a console and a serial port for
  • read mode, and calls select() with a 5 second timeout.
  • It waits for data to be available on either descriptor.
    */

#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/select.h>

int main( void )
{
int console, serial;
struct timeval tv;
fd_set rfd;
int n;

if( ( console = open( "/dev/con1", O_RDONLY ) ) == -1
||    ( serial  = open( "/dev/ser1", O_RDONLY ) ) == -1 )
{
  perror( "open" );
  return EXIT_FAILURE;
}

/*
 * Clear the set of read file descriptors, and
 * add the two we just got from the open calls.
 */
FD_ZERO( &rfd );
FD_SET( console, &rfd );
FD_SET( serial,  &rfd );

/*
 *    Set a 5 second timeout.
 */
tv.tv_sec = 5;
tv.tv_usec = 0;

switch ( n = select( 1 + max( console, serial ),
       &rfd, 0, 0, &tv ) ) {
  case -1:
    perror( "select" );
    return EXIT_FAILURE;
  case  0:
    puts( "select timed out" );
    break;
  default:
    printf( "%d descriptors ready ...\n", n );
    if( FD_ISSET( console, &rfd ) )
      puts( " -- console descriptor has data pending" );
    if( FD_ISSET( serial, &rfd ) )
      puts( " -- serial descriptor has data pending" );
}
return EXIT_SUCCESS;

}

If it returns after 5 seconds it’s because it’s was told to, throught the tv variable.

That is clear, but when I have changed the tv value to 0 or 1 sec then the problem is ALSO the same.
select() always returned with 0 value (timeout). It is the case 0: code block in switch().
It seems to be there are no any free descriptors.
How is it possible?

Jozsef

Maybe you are doing something wrong, when you compile or edit the code?

Jozsef,

Do you actually have a device connected to serial port 1 that’s generating data? Otherwise the selector for /dev/ser1 isn’t going to become ready because there is nothing to read. The same goes for console 1.

Tim

There no any device on serial port(s).
Otherwise the select() function shows this same problem with socket() / connect() functions.

The compiling was very simple:
qcc -o console console.c //What could be wrong with it?

I have tried to check with this lines before select():

printf( “console= %d\n”,console );
printf( “serial= %d\n”,serial );

Results are:
console=3
serial=4

Is it OK?

Jozsef

Let me go over this carefully step by step.

  1. You are doing a select on a serial device
  2. The select has a timeout
  3. There is nothing connected to the serial port
  4. The select always times out

So there is no problem, the select is behaving exactly as
one would expect it to. If I missed something, please
let us know.

Hello maschoen,

Thank you very much for your reply.

  1. Yes, exactly. The example code is included above. Console and serial device only, no other.
    I have tried it with and without photon. The result was same.
  2. Yes, the select() always gives back 0 value, if tv.tv_sec = 5 or zero.
  3. There are four serial ports, the Port 1 is enabled (3F8-IRQ4)and Port 2-3-4 are disabled in BIOS. All are unconnected. The mainboard is Advantech PCM-9577F. OS is QNX6.3 SP2 Momentics PE.
  4. select() always comes back with timeout.

I am awaiting your kindly advice.

Regards,
Jozsef

On what issue do you wish advice? From your description, the code and hardware are performing as one would expect. If you wish a different outcome, you will have to explain why. You could remove the timeout, and then the select will wait forever or until there is some input.

Jozsef,

I don’t think you understand selects purpose.

If you want select to return with actual data from the serial port then you need to attach a serial device. The simplest one you can attach is an old serial mouse.

Then run your program and move the mouse or press a mouse button and select will return with one of the selectors active (the one for the serial port).

The purpose of select is to wait on data from several sources. If none of those sources (serial, console) have any data it will wait forever. Since you specified a timeout value (5 seconds), the forever part ends up being 5 seconds.

Tim

Hello Tim,

Thank you very much for your explanation, it is clear already.

Regards,
Jozsef