At KeyWedge, I posted my code that connects to a serial port and then simulates keystrokes into your application.
Now, there were issues where the reconnect did not automatically take place after a Suspend/Resume and you had to invoke the main window and select File-Hide just to let KeyWedge reconnect. This happens on devices that provide the communication port all the time and don't close it during a suspend.
I have added some code to watch the Windows Mobile Power Message Queue for RESUME messages and then do an automatic reconnect. The other option would have been to send some data through the port periodically to check if it is still working. But this may disturb the attached device.
The main change is in PowerMsgQueue.cpp. It implements the msg queue to receive power broadcast messages like done in an MSDN example code. If a resume message is encountered, the code sends a WM_USER
message to the main window, which will do a SuspendComm
/ResumeComm
cycle. Very easy.
In PowerMsgQueue.cpp, we have code that recognizes the resume:
case PBT_TRANSITION:
nclog(L"Power Notification Message: PBT_TRANSITION\n");
nclog(L"Flags: %lx\n", ppb->Flags);
nclog(L"Length: %d\n", ppb->Length);
wsprintf(szPBtype, L"trans.: ");
if(ppb->Flags & 0x12010000)
{
nclog(L"PwrMsgQueue: got 'ON|PASSWORD|BACKLIGHTON'...\n");
iPost = PostMessage(hwndMain, WM_USER_RESUMECOMM, 0, 0);
nclog(L"PostMessage WM_USER_RESUMECOMM returned %i\n", iPost);
}
break;
case PBT_RESUME:
nclog(L"Power Notification Message: PBT_RESUME\n");
wsprintf(szPBtype, L"resume: ");
iPost = PostMessage(hwndMain, WM_USER_RESUMECOMM, 0, 0);
nclog(L"PostMessage WM_USER_RESUMECOMM returned %i\n", iPost);
nclog(L"Power: PBT_RESUME\n");
break;
To not block the queue, I use PostMessage
to inform the main window of the Resume. Here is the simple code block in WndProc
of the main window:
case WM_USER_RESUMECOMM: nclog(L"WndProc: received WM_USER_RESUMECOMM: restarting COMM\n");
suspendCOMM();
Sleep(100);
resumeCOMM(); break;
The msgQueue
thread is created in InitInstance
of the main window:
hwndMain=hWnd;
if(startMsgThread()==0)
nclog(L"InitInstance: startMsgThread() OK\r\n");
else
nclog(L"InitInstance: startMsgThread() FAILED\r\n");
resumeCOMM();
ShowWindow(hWnd, SW_HIDE);UpdateWindow(hWnd);
Have fun coding!
Josef