My application is not properly receiving data from the COM port. This used to work. I don't know what happened. I know that the proper data is being sent/received over the line because I can see it on my protocol analyzer.
The PC gets into the WAIT_OBJECT_0 + 1
state, but the buffer contents are always zero. I know this is a lot, but if anyone can point out what I'm doing wrong, I'd really appreciate it. I can add/remove details as requested. Thanks.
EDIT: Additional Information
I have been able to verify that the PC makes the call to ReadFileEx
, and it "succeeds." However, the PC never gets into FileIOCompletionRoutine
. Any ideas? (I removed the error-handling from the code to make life simpler.) Also, from what I read on the MSDN website, it looks like FileIOCompletionRoutine
will get called asynchronously in its own thread. Is that correct? Thanks.
EDIT: Final Solution
This is what I came up with. Obviously, the initialization and error-handling code is not here. We can't make things too easy. :)
// Load event handles.
pHandles[0] = s_hSerialPortRxThreadExitEvent;
// OVERLAPPED structure event handle is loaded in loop.
while ( blContinue )
{
// Wait for a communications event.
if ( !::WaitCommEvent( s_hSerialPort, &dwEventMask, &s_ov ) )
{
if ( ::GetLastError() != ERROR_IO_PENDING )
{
blContinue = FALSE;
continue;
}
else if ( ::WaitForSingleObject( pHandles[0], 0 ) == WAIT_OBJECT_0 )
{
// The thread-exit event has been signaled. Get out of here.
blContinue = FALSE;
continue;
}
}
else
{
// Load OVERLAPPED structure event handle.
pHandles[1] = s_ov.hEvent;
}
if ( dwEventMask & EV_RXCHAR )
{
if ( !::ReadFile( s_hSerialPort, pBuf, RX_BUF_SIZE, NULL, &s_ov ) )
{
if ( ::GetLastError() == ERROR_IO_PENDING )
{
// Wait for events.
dwObjectWaitState = ::WaitForMultipleObjects( 2, pHandles, FALSE, INFINITE );
// Switch on event.
switch ( dwObjectWaitState )
{
case WAIT_OBJECT_0: // thread exit event signaled
blContinue = FALSE;
continue;
case WAIT_OBJECT_0 + 1: // OVERLAPPED structure event signalled
// Reset event first to mitigate underrun condition.
if ( !::ResetEvent( pHandles[1] ) )
{
blContinue = FALSE;
continue;
}
// Get the OVERLAPPED result.
if ( !::GetOverlappedResult( s_hSerialPort, &s_ov, &dwBytesRead, FALSE ) )
{
blContinue = FALSE;
continue;
}
break;
default: // Error
blContinue = FALSE;
continue;
}
}
}
else if ( !::GetOverlappedResult( s_hSerialPort, &s_ov, &dwBytesRead, FALSE ) )
{
blContinue = FALSE;
continue;
}
// If bytes were read...
if ( dwBytesRead > 0 )
{
// Copy received data from local buffer to thread-safe serial port buffer.
::EnterCriticalSection( &s_csRxRingBuffer );
blSuccess = s_pobjRxRingBuffer->Add( pBuf, dwBytesRead, TRUE );
::LeaveCriticalSection( &s_csRxRingBuffer );
if ( !blSuccess )
{
blContinue = FALSE;
continue;
}
// Set the received data event.
if ( !::SetEvent( s_phEventIds[RECEIVE_EVENT] ) )
{
blContinue = FALSE;
continue;
}
}
}
if ( dwEventMask & EV_TXEMPTY )
{
// Set the transmit complete event.
if ( !::SetEvent( s_phEventIds[TRANSMIT_EVENT] ) )
{
blContinue = FALSE;
continue;
}
}
} // end while ( blContinue );