Other Comm functions may be invoked for a device by calling the EscapeCommFunction. This call directs the device to execute a function specified by a function code. For example, this can be used to suspend character transmission with the SETBREAK function and to resume character transmission with the CLRBREAK function. These particular functions may also be invoked by calling SetCommBreak and ClearCommBreak. EscapeCommFunction can also be used to implement manual modem control. For example, the CLRDTR and SETDTR functions can be used to implement manual DTR flow control. Note that an error occurs if this function is used to manipulate the DTR line when the device has been configured to enable DTR handshaking, or the RTS line if RTS handshaking is enabled.
An application can use SetCommMask to specify a set of events to be monitored for a particular communications resource. It can then use WaitCommEvent to wait for one of the specified events to occur. This could be used, for example, if you need to know when the CTS and DSR signals change state. The reference page for SetCommMask provides a complete list of the events in which an application can express interest. You must create an Event object which is then used as a parameter of WaitCommEvent . When one of the events specified in the event mask occurs, the Event object is signalled and WaitCommEvent returns with a filled in event mask indicating the event that satisfied the wait. GetCommMask can be used to determine the current set of events being monitored for a device. Calling SetCommMask for a device while a wait is pending on that device, will cause WaitCommEvent to return with an error indication.
Note that WaitCommEvent only detects events that occur after the wait was started. For example, the EV_RXCHAR event is specified to monitor when any character is received and placed in the receive queue. Existing characters in the receive queue when WaitCommEvent is called will not satisfy the wait. Also, many of the events occur when a signal (CTS, DSR, etc.) changes state. But this does not tell you what their current state is. The GetCommModemStatus call can be used to query the current state of the Clear-To-Send (CTS), Data-Set-Ready (DSR), Receive-Line-Signal-Detect (RLSD), and Ring indicator signals.
The following example code opens the serial port for overlapped I/O, sets the event mask to monitor CTS and DSR signals, and then waits for an event to occur. Overlapped I/O is typically used when a process is using WaitCommEvent because otherwise the other threads of the process would be unable to perform I/O operations during the wait.
HANDLE hCom, hEvent;
DWORD dwEvtMask;
BOOL bSuccess;
hCom = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL
);
if (hCom == INVALID_HANDLE_VALUE) {
/* deal with error */
}
// set the event mask
bSuccess = SetCommMask(hCom, EV_CTS | EV_DSR);
if (!bSuccess) {
/* deal with error */
}
/* create event object for use in WaitCommEvent */
hEvent = CreateEvent(NULL, // no security attributes
FALSE, // auto reset event
FALSE, // not signalled
NULL // no name
);
assert(hEvent);
if (WaitCommEvent(hCom, &dwEvtMask, hEvent)) {
if (dwEvtMask & EV_DSR) {
/* . . . */
}
if (dwEvtMask & EV_CTS) {
/* . . . */
}
}