|
-
March 28th, 2007, 06:03 AM
#1
Serial port WaitForMultipleObjects() problem
The call to WaitForMultipleObjects() does not return, it seems the event for the serial port does not get signalled. I was under the impression that sending data to the port will cause the event to be signalled. Am i doing something wrong here?
Code:
UINT CMyserialportView::ReaderThread(LPVOID p)
{
TRACE("\nThread ReaderThread() >");
CSerialParameters* parms = (CSerialParameters *)p;
OVERLAPPED ovl = {0};
ovl.hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
if(ovl.hEvent == NULL)
{ /* failed */
DWORD err = ::GetLastError();
parms->m_pWndNotifyee->PostMessage(UWM_READER_SHUTTING_DOWN, (WPARAM)err);
return 0;
} /* failed */
DWORD shutdown; // reason for shutdown
HANDLE waiters[2];
waiters[0] = parms->m_hShutdown;
waiters[1] = ovl.hEvent;
#define MAX_BUFFER_SIZE 100
BOOL running = TRUE;
DWORD bytesRead;
while(running)
{ /* read loop */
BYTE buffer[MAX_BUFFER_SIZE];
BOOL ok = ::ReadFile(parms->m_hCom, buffer, MAX_BUFFER_SIZE - 1, &bytesRead, &ovl);
if(!ok)
{ /* error */
DWORD err = ::GetLastError();
if(err != ERROR_IO_PENDING)
{ /* read error */
shutdown = err;
running = FALSE;
continue;
} /* read error */
// otherwise, it is ERROR_IO_PENDING
DWORD result = ::WaitForMultipleObjects(2, waiters, FALSE, INFINITE);
//DWORD result = ::WaitForSingleObject(ovl.hEvent, INFINITE);
switch(result)
{ /* wait */
case WAIT_OBJECT_0: // shutdown
::CancelIo(parms->m_hCom);
shutdown = ERROR_SUCCESS; // clean shutdown
running = FALSE;
continue;
case WAIT_OBJECT_0 + 1: // I/O complete
ok = ::GetOverlappedResult(parms->m_hCom, &ovl, &bytesRead, TRUE);
if(!ok)
{ /* GetOverlappedResult failed */
DWORD err = ::GetLastError();
running = FALSE;
continue;
} /* GetOverlappedResult failed */
break;
default:
{ /* trouble */
shutdown = ::GetLastError();
ASSERT(FALSE); // failure
running = FALSE;
continue;
} /* trouble */
} /* wait */
} /* error */
// if we get here, either the ReadFile worked immediately, or we waited for completion
if(bytesRead == 0)
continue; // nothing was read
buffer[bytesRead] = '\0'; // assumes 8-bit characters without embedded NUL
CString* s = new CString((LPCSTR)buffer);
parms->m_pWndNotifyee->PostMessage(UWM_DATA_READ, (WPARAM)s);
} /* read loop */
parms->m_pWndNotifyee->PostMessage(UWM_READER_SHUTTING_DOWN, (WPARAM)shutdown);
::CloseHandle(ovl.hEvent);
return 0; // we're gone. You may choose to do something different
} // CMyClass::ReaderThread
Time is fun when you're having flies 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|