Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, My app (developed in C++) supports multiple serial ports. All goes well regarding opening and configuring (baudrate, etc.) of the ports. Each port has a listen thread that will ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2009
    Posts
    3

    wait for serial I/O using select()


    Hi,

    My app (developed in C++) supports multiple serial ports. All goes well regarding opening and configuring (baudrate, etc.) of the ports. Each port has a listen thread that will wait using the select() API until some input arrives on that serial port.

    I want to wait indefinitely until some data arrives on that particular serial port. If no data arrives and the app terminates then I want to be able to "unblock" the select() and terminate the listen thread properly. Here is my problem, I cannot seem to unblock the select() to wake up the listen thread. I can close the file descripter successfully in the main thread but this no effect whatsoever on the select(). I tried to use an 'except file descripter set' argument in select() hoping some exception would occur when closing the file descripter, but no luck.

    Could somebody please provide me with an answer to my problem?

    And no, using a timeval struct to occasionally waking up the select() is not a desired option. In fact, I consider this bad design because why consume CPU power while the listen thread has nothing to do. Furthermore, what would be a good timeval. Short timeout will increase CPU consumption, and long timeout will result in slow response time regarding user experience.

    The pseudo code (per listen thread) looks something like this:

    fd_set readfs, exceptfs;

    FD_ZERO(&readfs);
    FD_ZERO(&exceptfs);

    FD_SET(_fd, &readfs);
    FD_SET(_fd, &exceptfs);

    select(_fd+1, &readfs, NULL, &exceptfs, NULL /* no timeout */);

    if (FD_ISSET(_fd, &readfs))
    {
    // handle incoming data from the serial port
    }


    Kind regards,
    Mark

  2. #2
    Just Joined!
    Join Date
    Feb 2009
    Posts
    3
    Never mind, I found a solution.

    To unblock a blocking select I now use a pipe. A pipe also uses file desciptors to write/read message. So the 'read file descripter set' has now two file descripters (one for input data coming from serial port and one for the reader part of the pipe). On termination of my app I send a message using the writer part of the pipe in the destructor. This will cause the select to unblock and afterwards I can properly terminate the listen thread.

  3. #3
    Just Joined!
    Join Date
    Feb 2010
    Posts
    4
    Hey is your code open-source? You mind sharing your select()->wait-for-serial-I/O problem? I am looking to do something similiar and there does not seem to be a lot of literature/code-examples out there with select(), rather poll().

    Very much appreciate it!


  4. #4
    oz
    oz is offline
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,733
    Hello, and welcome to the forums!

    Please start a new thread describing any problem that you are having. This thread is over a year old, and the OP hasn't logged in for more than a year, now.

    Thank you.
    oz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •