Skip to content
Christian Schwarz edited this page Jan 29, 2016 · 6 revisions

Resources

Linux

Windows

Overcome driver issues

If you close the serial port under Windows, you have to do some extra work, to avoid starvations, deadlocks or memory access errors that can occur in some cheap serial port drivers.

With the following steps we can bypass these ugly driver bugs:

  • CancelIo- cancels all outstanding I/O operations
  • PurgeComm - discards all characters from the out- and input buffer
  • SetCommMask - releases the WaitCommEvent function. This is necessary, because the asynchronous WaitCommEvent doesn't return immediatly on WAIT_FAILED during read operations. It can cause a memory access violation error, because the resources are disposed too early or it causes starvation.

After the port is closed via CloseHandle, we also have to wait for the termination of the serial connection. This is neccessary, because when the close operation returns, the pending I/O operations in the background are not canceled immediatly. We must wait until all I/O operations are finished. Only then we can dispose all allocated resources in the next step. The only way to find out that all pending I/O operations are finished and the port is really closed, is to re-open the port. If it is successfull, all pending operations should be terminated.

In order to await the termination of all pending I/O operations, we just try to open the serial port again and again, until the port is not busy anymore and the port can be successfully opened.

#Serial Testing

Clone this wiki locally