I am writing a C program that will read and write from/to a serial port. Each write will be followed by a read which will contain data based on the write. I will have about 16 different writes to perform, each followed by a read.
I am still new to serial programming and am trying to determine how to approach this. Should the program block during each write/read (basically having a big while(1) loop of writes and reads)?
Is it possible to have one thread sending writes while another thread performs the reads? Can a callback be created for allowing a thread to know when data is available to be read from the serial port?
Thanks for the help.
Edit:
OS: Linux
It sounds like a simple loop with a read and a write will be enough. If you want more control (like writing a keep-alive after X seconds of no input) use
selectorpoll; they allow you to “sense” if data is available, so areadwill not block (however, you should still read non-blocking in case something happens between the select and the read). Multithreading doesn’t make sense in this case.selectandpollalso allow you to test if writing will block; again, write non-blocking just to be sure. You generally don’t want your process to be hanging in some blocking system call, ever, apart fromselectorpoll, that is. (at least, I don’t)Use
ioctlto set various parameters on the serial line. Useto launch minicom (or any other terminal program) and see what they do to get things working, if you’re stuck. And as always, see
man select/poll/read/ioctlfor more information.