-
Notifications
You must be signed in to change notification settings - Fork 4
Home
- Article about serial communication (on MSDN)
- Serial Communication in Windows
- System Error Codes (Windows)
- How to get alist of available serial ports
- How to enumerate hardware devices by using SetupDi calls
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 asynchronousWaitCommEvent
doesn't return immediatly onWAIT_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
- [com0com virtual com ports] (https://code.google.com/archive/p/powersdr-iq/downloads)