I am working on Non Overlapped serial I/O application.
I have only one thread to receive the data.For send i dont have any thread.
I just call Write file when i need to send .
My problem is, When I have my receive thread wait for an event using WaitCommEvent, it ties up the serial port. Then my sending function can't access the port with WriteFile. I knew waiting for a comm event would block the receiving thread but didn't expect that it would block use of the port.
Any suggestions on how to do non-overlapped communication, where the send fucntion can write while the receive thread waits for a comm event?
You should be able to open more than once if you specify the FILE_SHARE_READ and FILE_SHARE_WRITE flags.
You cannot do that with a communication resource (MSDN [^])
The CreateFile function can create a handle to a communications resource, such as the serial port COM1. For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING, the dwShareMode parameter must be zero (exclusive access), and the hTemplateFile parameter must be NULL. Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke [My articles]
yeah, I am unable to open it twice even if I mention the shared attributes.
There is no other option for non overlapped I/O, if I have to WriteFile, when
my receive thread wait for an event using WaitCommEvent?
Even If I have to send data in the same receive thread,
I am unable to do as WaitCommEvent never returns.
My code block is:
if (!SetCommMask( pc->hCommn, EV_RXCHAR))
for ( ; ; )
if (WaitCommEvent(pc->hCommn, &dwCommEvent, NULL))
if (ReadFile(pc->hCommn, &chRead, 1, &dwRead, NULL))
// A byte has been read; process it.
// An error occurred in the ReadFile call.
else// Error in WaitCommEvent.
Where can I call my send fucntion in this thread, if I have some data to send.