RS232 to WinApi
Namiesto:
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
použiť:
HANDLE File;
HANDLE Event;
OVERLAPPED Overlapped;
File=CreateFile(...,FILE_FLAG_OVERLAPPED,...);
Event=CreateEvent(....);
Overlapped.hEvent=Event;
// start new async read request
ReadFile(File,....,&Overlapped);
while(1)
{
DWORD Result;
Result=MsgWaitForMultipleObjects(1,&Event,FALSE,INFINITE,
QS_ALLEVENTS | QS_ALLPOSTMESSAGE | QS_SENDMESSAGE);
if (Result==WAIT_OBJECT_0)
{
// get data ..
GetOverlappedResult(File,&Overlapped,....);
// copy data from Overlapped somewhere
// start new async read request
ReadFile(File,....,&Overlapped);
}
if (Result==WAIT_OBJECT_0+1)
{
while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{
if (Msg.message == WM_QUIT)
{
break;
}
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
if (Msg.message==WM_QUIT)
{
break;
}
}
}
// must cancel all pending requests !!!!
CancelIo(File);
http://forum.root.cz/index.php?topic=8440.0