Reputation: 4554
I have a program that performs overlapped I/O on a serial port and I am seeing cases where data that has been received is not being read by the application. My code is using SetCommMask()
and WaitCommEvent()
as follows:
if(!waiting_for_comm_event)
{
comm_event_ready = false;
if(!SetCommMask(port_handle, EV_RXCHAR | EV_ERR | EV_RLSD))
throw OsException(my_strings[strid_set_comm_mask_failed].c_str());
rcd = WaitCommEvent(port_handle, &event_mask, &event_control);
last_error = GetLastError();
if(rcd && event_mask != 0)
comm_event_ready = true;
else if(last_error != ERROR_IO_PENDING)
throw OsException(my_strings[strid_wait_comm_event_failed].c_str());
else
waiting_for_comm_event = true;
}
This code is invoked after I have written any data in my output buffer. There is a possibility that data could be received while this writing is taking place. Will WaitCommEvent()
report the condition where there is already data waiting to be read?
Upvotes: 1
Views: 1311
Reputation: 283733
If you have to ask here, you don't want to know the answer.
No, really -- it means you're relying on undocumented behavior that could change in future Windows versions.
You can instead use overlapped I/O to start tracking incoming data and events before you send the data that triggers them.
Two approaches (I use a ReadFileEx
variant of the first in my own serial code):
WaitCommEvent
.WaitForMultipleEvents
to proceed.or
WaitCommEvent
.In either case, incoming data and errors, even before WriteFile
completes, will be detected.
Upvotes: 3