To read and write simultaneously you should use overlapped I/O. Then the read and write calls will not block but return immediately. End of the I/O operation is indicated by events. See
Overlapped I/O and I/O Manager Driver Developer Tips[
^] in the MSDN.
I recommend the usage of worker threads because you can wait for the completion of I/O and detect specific serial events like status line changings and errors (see
SetCommMask()
function). Using two threads for reading and writing or only one handling all depends on the specific communication protocol and data flow (e.g. if there is some kind of acknowledgement).
Serial I/O, especially when overlapped and using threads, is an advanced topic. You should read about overlapped I/O and worker threads. Then look for some articles here and elsewhere in the web for examples. You may find one that can be used for your implementation with minor changings. Finding one that completely fits your requirements is rather unlikely.
If you have specific questions about a topic or need help with your code, come back here again. At the moment, your question is too complex to answer it in short.
[UPDATE]
Some information on serial communication:
- Use
CreateFile()
, ReadFile()
, WriteFile()
, and CloseHandle()
to perform I/O operations. - See Communications Functions[^] for a list of serial communication specific functions.
- Use at least
SetCommState()
after opening the port to pass settings. - Read about overlapped I/O (see above link).
- Read about worker threads.
- Use
SetCommMask()
and WaitCommEvent()
inside threads to detect and handle specific events. - Use
WaitForMultipleObjects()
inside threads to wait for events.