LCD Smartie essentially was calling the DISPLAYDLL_Init function twice upon resuming, and the second call was always DURING the first call. My code was not prepared for this, and thus I was trying to open the COM port twice, failing the second time, but in the mean time locking the COM port and preventing any further action.
*** So, the tip is: RIGHT before opening the COM port, check if you already opened it. It was not good enough to check at the beginning of the DISPLAYDLL_Init function.
While identifying this problem, I came across the reason why this happens.
- you call some function in DISPLAYDLL_Init
- which calls PeekMessage (directly or indirectly)
- which causes the PowerBroadcast message to be RE-SENT to LCD Smartie (because it hasn't consumed it yet...)
- which makes LCD Smartie realize that it should re-initialize the display
- which causes the DISPLAYDLL_Init function to be called again, from the same thread
The functions that caused me problems were CoInitializeEx and IWbemLocator::ConnectServer (used for WMI queries)
The function bsearch (binary search) calls PeekMessage as well, so even mundane functions may cause this chain of events!
The best fixes for this would be within LCD Smartie itself:
- Run DISPLAYDLL_Init in a separate thread (allowing LCD Smartie to consume the PowerBroadcast message immediately)
- Record that a specific PowerBroadcast message has already been received and ignore it