The Windows serial functions (referenced in AN197) were originally designed for use with real COM ports, which cannot be removed. With the virtual COM port, the USB device and COM port can suddenly disappear, causing some of the Windows functions, like EscapeCommFunction, to hang indefinitely. If the CP210x is disconnected while EscapeCommFunction is processing, the function itself never returns, so there is no way to recover the application.
To create more robust applications, use the IOCTL controls directly (which will not hang indefinitely) instead of the EscapeCommFunction.
For example, these function calls:
EscapeCommFunction(m_hSlave, SETRTS);
EscapeCommFunction(m_hSlave, SETDTR);
EscapeCommFunction(m_hSlave, CLRDTR);
EscapeCommFunction(m_hSlave, CLRRTS);
EscapeCommFunction(m_hSlave, SETRTS);
can be replaced with function calls like:
SERIAL_HANDFLOW shf = {0};
DWORD dwSize;
DeviceIOCTL(m_hSlave, IOCTL_SERIAL_GET_HANDFLOW, NULL, 0, &shf, sizeof(SERIAL_HANDFLOW), &dwSize);
DeviceIOCTL(m_hSlave, IOCTL_SERIAL_SET_HANDFLOW, NULL, 0, &shf, sizeof(SERIAL_HANDFLOW), &dwSize);
In order to use the DeviceIOCTL function, the project has to include 'devioctl.h' and 'ntddser.h' added. More information on these functions and the available IOCTL controls can be found in the MSDN library (www.msdn.com).
AN197 can be found on the Silicon Labs Applications webpage: https://www.silabs.com/products/mcu/Pages/ApplicationNotes.aspx.
EscapeComm and the CP210x