Home/Support/Support Forum/use serial port from two threads
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

use serial port from two threads

0 votes
I want to write my own application that uses the serial port. I have to read from and write two the port (both at the same time). What's the best way to do this?

I was thinking about two threads that both do a blocking read or write call. Is this thread safe?
asked Jun 26, 2007 in NET+OS by compie Community Contributor (55 points)
recategorized Dec 18, 2013 by tuxembb

Please log in or register to answer this question.

8 Answers

0 votes
It depends on your application on how you want to organize things (i.e. one or two threads, etc). However, if you want to read to the serial port from Thread A and write in Thread B, I would recommend using non-blocking sockets and use select on the read thread. Be prepared to have to call write or read multiple times if the serial port is in use.
answered Jun 26, 2007 by charliek Veteran of the Digi Community (408 points)
0 votes
Ok, thanks for your advice.

But I don't really see the difference between:
* a blocking read()
* a blocking select() followed by a nonblocking read()

Is there a reason why the second method is better?
answered Jun 27, 2007 by compie Community Contributor (55 points)
0 votes
A blocking read ties up the serial port so you can not write to it. A blocking select returns when there's data to be read from the serial port. In the case of a blocking read, you must read some data before the call returns, thus if you have another thread (or you don't want this thread to block) trying to write, calls to write will fail. With a blocking select, you can continue to write to the serial port will you wait for data to arrive on the serial port to be read.
answered Jun 27, 2007 by charliek Veteran of the Digi Community (408 points)
0 votes
Ok, that's clear.

Last question: the documentation seems to indicate that select() only works on sockets. But can I use it on any file descriptor?

select
Category: Socket API Functions
Used to multiplex I/O requests among multiple sockets.
answered Jun 28, 2007 by compie Community Contributor (55 points)
0 votes
It works with the serial file descriptors. I'm pretty sure you'd have troubles with a normal file description (i.e., to a file) though.
answered Jun 28, 2007 by charliek Veteran of the Digi Community (408 points)
0 votes
I'm developing with netos 6.3 and I can't get the serial file descriptor to work correctly with select(). I'm only trying to call select for a receive file descriptor. For the majority of the time, select() is returning after a timeout, but tcgetbuffers() indicates that there is data to be read from the receive buffer. select() is seldom returning after detecting receive data.
answered Sep 6, 2007 by fyicc New to the Community (18 points)
0 votes
I am also facing the same problem. When I use select on the serial port it usually returns after timeout even though there is data present on the serial port. Does anyone have a fix for this problem? I am using netos 7.1
answered Nov 30, 2007 by ebuddy New to the Community (1 point)
0 votes
Look at this post and see if it helps any:

http://www.digi.com/support/forum/viewthread_thread,1407#5220
answered Dec 19, 2007 by ieftimovski New to the Community (24 points)
...