Thread: Best way to read character input from serial port (Linux)?

  1. #16
    Registered User
    Join Date
    Jun 2009
    Posts
    101
    Quote Originally Posted by itCbitC View Post
    Curious if you made it non-blocking by setting a timeout in the struct timeval member??
    I didn't have to do this because the machine is being queried via the serial port constantly. So, I just passed NULL as the last argument to select(). From what I can tell, this effectively throttles the reads off the serial port based on the speed that the remote machine is sending queries. And since it's running in its own thread, this doesn't tie up the main thread, which is constantly checking variables updated from the serial port reader thread (via a loop) and performing the main program functions.

  2. #17
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by synthetix View Post
    I didn't have to do this because the machine is being queried via the serial port constantly. So, I just passed NULL as the last argument to select(). From what I can tell, this effectively throttles the reads off the serial port based on the speed that the remote machine is sending queries. And since it's running in its own thread, this doesn't tie up the main thread, which is constantly checking variables updated from the serial port reader thread (via a loop) and performing the main program functions.
    Reason to have a timeout is to prevent the select from blocking otherwise it'll just sit there and wait for input exactly like the call below:
    Code:
    fcntl(fd_serialport, F_SETFL, 0);
    Is your application doing anything else besides reading from that serial port? Are there many ports to read from?
    The power of select() is in its ability to poll each file descriptor in turn, waiting for an event or else timing out.

  3. #18
    Registered User
    Join Date
    Jun 2009
    Posts
    101
    Quote Originally Posted by itCbitC View Post
    Is your application doing anything else besides reading from that serial port? Are there many ports to read from?
    The power of select() is in its ability to poll each file descriptor in turn, waiting for an event or else timing out.
    Yeah, it's taking that input and using it to decide what action to take elsewhere in the program. That's why I put it in its own thread. And I don't have a timeout set because the serial port is being read at a constant rate. If there's no serial input, there's nothing the program can do anyway. And if it does happen to hang up, it won't cause the entire program to stop because it's a separate thread. Although now that I think about it, I may just assign a timeout to it anyway "just in case." Seems like it would be a good coding practice, if nothing else.

    I did make a variant of this program that does absolutely nothing but send and receive serial port commands. Now, with this one I had to assign a timeout value to select() because the flow of data was not constant. It was a program I wrote to check for proper operation of the first program (this time server side instead of client side). Once commands are sent, the client only responds once, hence complete lockup (blocking) unless you timeout select().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sending data over UDP from serial port
    By forumguy in forum Linux Programming
    Replies: 0
    Last Post: 04-25-2009, 02:10 PM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. Problem with string and serial port
    By collinm in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 10:19 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. Need help or info about serial port communication
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 01-08-2002, 01:48 PM