0

My application uses serial I/O with overlapped events. For some reason, ::WaitCommEvent fails consistently on the second pass through the loop with ERROR_INVALID_PARAMETER. If anyone can explain what I need to do differently, it would be greatly appreciated. My serial port initialization/open code and the thread function follows. It should be noted that the init/open code is executed after the thread function is started, which is what the call to ::WaitForSingleObject for.

Additionally, I was wondering if something like ::WaitForSingleObject( pobjSerialPort->m_hSerialPort, INFINITE ); would be valid as a non-blocking means of determining when the serial port is open.

Serial Port Initialization:

DWORD CSerialPort::Open( const wchar_t * portName )
{
    DCB dcb = {0};
    DWORD dwError = ERROR_SUCCESS;

    do
    {
        if ( this->IsOpen() != FALSE )
        {
            TRACE(_T("CSerialPort::Open : Warning: Attempted to re-open serial port that is already open.\r\n"));
            continue;
        }

        // Overwrite port name if specified.
        if ( portName != NULL )
        {
            this->m_pwcPortName.clear();
            this->m_pwcPortName.append( SP_NAME_PREFIX );
            this->m_pwcPortName.append( portName );
        }

        ASSERT(this->m_pwcPortName.length() > 0);

        // Open the serial port.
        if ( (this->m_hSerialPort = ::CreateFile(
            m_pwcPortName.c_str(),          // Formatted serial port name
            GENERIC_READ | GENERIC_WRITE,   // Access: Read and write
            0,                              // Share: No sharing
            NULL,                           // Security: None
            OPEN_EXISTING,                  // COM port already exists
            FILE_FLAG_OVERLAPPED,           // Asynchronous I/O
            NULL                            // No template file for COM port
            )) == INVALID_HANDLE_VALUE )
        {
            dwError = ::GetLastError();
            TRACE(_T("CSerialPort::Open : Failed to get the handle to the serial port.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        // Initialize the DCB structure with COM port parameters.
        if ( !::BuildCommDCB( _T("baud=38400 parity=N data=8 stop=1"), &dcb ) )
        {
            dwError = ::GetLastError();
            TRACE(_T("CSerialPort::Open : Failed to build the DCB structure.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        // Set the serial port communications events mask.
        if ( !::SetCommMask( this->m_hSerialPort, SP_COMM_MASK ) )
        {
            dwError = ::GetLastError();
            TRACE(_T("CSerialPort::Open : Failed to set comm. events to be monitored.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        // Set serial port parameters.
        if ( !::SetCommState( this->m_hSerialPort, &dcb ) )
        {
            dwError = ::GetLastError();
            TRACE(_T("CSerialPort::Open : Failed to set the comm state.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        // Set the serial port communications timeouts.
        this->m_ct.ReadIntervalTimeout = MAXDWORD;
        this->m_ct.ReadTotalTimeoutMultiplier = 0;
        this->m_ct.ReadTotalTimeoutConstant = 0;
        this->m_ct.WriteTotalTimeoutMultiplier = 0;
        this->m_ct.WriteTotalTimeoutConstant = 0;

        if ( !::SetCommTimeouts( this->m_hSerialPort, &(this->m_ct) ) )
        {
            dwError = ::GetLastError();
            TRACE(_T("CSerialPort::Open : Failed to set the comm timeout values.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        // Create thread to receive data.
        if ( (this->m_hSpRxThread = CreateThread(
            NULL,                           // No security attributes.
            0,                              // Use default initial stack size.
            reinterpret_cast<LPTHREAD_START_ROUTINE>(SerialPortRxThreadFn), // Function to execute in new thread.
            this,                           // Thread parameters.
            0,                              // Use default creation settings.
            NULL                            // Thread ID is not needed.
            )) == NULL )
        {
            dwError = ::GetLastError();
            TRACE(_T("CSerialPort::Open : Failed to create serial port receive thread.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        // Create thread to transmit data.
        if ( (this->m_hSpTxThread = CreateThread(
            NULL,                           // No security attributes.
            0,                              // Use default initial stack size.
            reinterpret_cast<LPTHREAD_START_ROUTINE>(SerialPortTxThreadFn), // Function to execute in new thread.
            this,                           // Thread parameters.
            0,                              // Use default creation settings.
            NULL                            // Thread ID is not needed.
            )) == NULL )
        {
            dwError = ::GetLastError();
            TRACE(_T("CSerialPort::Open : Failed to create serial port transmit thread.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        if ( !::SetEvent( this->m_hSpPortOpenEvent ) )
        {
            dwError = ::GetLastError();
            TRACE(_T("CSerialPort::Open : Failed to set event object signal state.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }
    }
    while ( 0 );

    return dwError;
}

Serial Port Communications Events Thread:

static DWORD SerialPortCommEvtsThreadFn( void * pParam )
{
    CSerialPort * pobjSerialPort = NULL;
    BOOL blContinue = TRUE;
    DWORD dwError = ERROR_SUCCESS;
    DWORD dwEventMask = 0;
    DWORD dwObjectWaitState;
    OVERLAPPED ovComm = { 0 };
    int i = 0;
    static HANDLE pHandles[SPCM_MAX_EVENTS + SP_ONE_ITEM]; // +1 for overlapped event

    // Validate parameters.
    if ( pParam == NULL )
    {
        dwError = ERROR_INVALID_PARAMETER;
        TRACE(_T("SerialPortTxThreadFn : Invalid parameter.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
        return dwError;
    }

    pobjSerialPort = (CSerialPort *)pParam;

    // Load event handles.
    pHandles[i++] = pobjSerialPort->GetCommHandle( SPCM_THREAD_EXIT_EVT_ID );
    pHandles[i++] = pobjSerialPort->GetCommHandle( SPCM_PORT_OPEN_EVT_ID );
    pHandles[i++] = pobjSerialPort->GetCommHandle( SPCM_PORT_CLOSED_EVT_ID );

    while ( (blContinue != FALSE) && (dwError == ERROR_SUCCESS) )
    {
        // Wait for serial port to open.
        if ( (dwObjectWaitState = ::WaitForSingleObject( pobjSerialPort->GetCommHandle( SPCM_PORT_OPEN_EVT_ID ), INFINITE )) != WAIT_OBJECT_0 )
        {
            dwError = ( dwObjectWaitState == WAIT_FAILED ) ? ::GetLastError() : dwObjectWaitState;
            TRACE(_T("SerialPortCommEvtsThreadFn : Failed waiting for event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        if ( ovComm.hEvent == NULL )
        {
            // Create event object for serial port communications events OVERLAPPED structure.
            if ( (ovComm.hEvent = ::CreateEvent(
                NULL,                           // No security
                TRUE,                           // Create a manual-reset event object
                FALSE,                          // Initial state is non-signaled
                NULL                            // No name specified
                )) == NULL )
            {
                dwError = ::GetLastError();
                TRACE(_T("SerialPortCommEvtsThreadFn : Failed to create event object.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                continue;
            }

            pHandles[i++] = ovComm.hEvent;
        }
        else
        {
            i++;
        }

        // Wait for a communications event.
        if ( !::WaitCommEvent( pobjSerialPort->m_hSerialPort, &dwEventMask, &ovComm ) )
        {
            if ( (dwError = ::GetLastError()) != ERROR_IO_PENDING )
            {
                TRACE(_T("SerialPortCommEvtsThreadFn : Failed waiting for communications event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                continue;
            }
            else
            {
                dwError = ERROR_SUCCESS;
            }
        }
        else
        {
            if ( (dwError = HandleCommOvEvent( pobjSerialPort, dwEventMask )) != ERROR_SUCCESS )
            {
                TRACE(_T("SerialPortCommEvtsThreadFn : Failed handling communications event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                continue;
            }

            continue;
        }

        dwObjectWaitState = ::WaitForMultipleObjects( i--, pHandles, FALSE, INFINITE );

        switch ( dwObjectWaitState )
        {
        case WAIT_OBJECT_0 + SPCM_THREAD_EXIT_EVT_ID:
            blContinue = FALSE;
            break;

        case WAIT_OBJECT_0 + SPCM_PORT_OPEN_EVT_ID:
            if ( !::ResetEvent( pobjSerialPort->GetCommHandle( SPCM_PORT_CLOSED_EVT_ID ) ) )
            {
                dwError = ::GetLastError();
                TRACE(_T("SerialPortCommEvtsThreadFn : Failed to reset event object signal state.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                continue;
            }
            break;

        case WAIT_OBJECT_0 + SPCM_PORT_CLOSED_EVT_ID:
            if ( !::ResetEvent( pobjSerialPort->GetCommHandle( SPCM_PORT_OPEN_EVT_ID ) ) )
            {
                dwError = ::GetLastError();
                TRACE(_T("SerialPortCommEvtsThreadFn : Failed to reset event object signal state.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                continue;
            }
            break;

        case WAIT_OBJECT_0 + SPCM_MAX_EVENTS:
            if ( (dwError = HandleCommOvEvent( pobjSerialPort, dwEventMask )) != ERROR_SUCCESS )
            {
                TRACE(_T("SerialPortCommEvtsThreadFn : Failed handling communications event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                continue;
            }

            ::CloseHandle( ovComm.hEvent );
            ::memset( &ovComm, 0, sizeof(OVERLAPPED) );
            break;

        default:
            dwError = ( dwObjectWaitState == WAIT_FAILED ) ? ::GetLastError() : dwObjectWaitState;
            TRACE(_T("SerialPortCommEvtsThreadFn : Failed waiting for event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            break;
        }
    }

    return dwError;
}

Handle Comm Events Function

static DWORD HandleCommOvEvent( CSerialPort * pobjSerialPort, DWORD dwEvtMask )
{
    DWORD dwError = ERROR_SUCCESS;

    do
    {
        // Validate parameters.
        if ( pobjSerialPort == NULL )
        {
            dwError = ERROR_INVALID_PARAMETER;
            TRACE(_T("HandleCommEvent : Invalid parameter.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        // Handle the transmit complete event.
        if ( dwEvtMask & EV_TXEMPTY )
        {
            if ( (dwError = HandleTxDoneCommEvent( pobjSerialPort )) != ERROR_SUCCESS )
            {
                TRACE(_T("HandleCommEvent : Failed handling transmit complete event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                continue;
            }
        }

        // Handle the received data event.
        if ( dwEvtMask & EV_RXCHAR )
        {
            if ( (dwError = HandleRxDataCommEvent( pobjSerialPort )) != ERROR_SUCCESS )
            {
                TRACE(_T("HandleCommEvent : Failed handling received data event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                continue;
            }
        }
    }
    while ( 0 );

    return dwError;
}

Handle Received Data Comm Event Function

static DWORD HandleRxDataCommEvent( CSerialPort * pobjSerialPort )
{
    DWORD dwError = ERROR_SUCCESS;

    do
    {
        // Validate parameters.
        if ( pobjSerialPort == NULL )
        {
            dwError = ERROR_INVALID_PARAMETER;
            TRACE(_T("HandleRxDataCommEvent : Invalid parameter.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        if ( !::SetEvent( pobjSerialPort->GetRxHandle( SPRX_RECEIVED_DATA_EVT_ID ) ) )
        {
            dwError = ::GetLastError();
            TRACE(_T("HandleRxDataCommEvent : Failed setting event object signal state.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }
    }
    while ( 0 );

    return dwError;
}

Receive Thread Function

static DWORD SerialPortRxThreadFn( void * pParam )
{
    CSerialPort * pobjSerialPort = NULL;
    BOOL blContinue = TRUE;
    DWORD dwError = ERROR_SUCCESS;
    DWORD dwEventMask = 0;
    DWORD dwObjectWaitState;
    OVERLAPPED ovComm = { 0 };
    int i = 0;
    static BYTE pBuf[SP_RX_BUF_SIZE];
    static HANDLE pHandles[SPRX_MAX_EVENTS + SP_ONE_ITEM]; // +1 for overlapped event

    ASSERT(s_pobjRxBuffer != NULL);

    // Validate parameters.
    if ( pParam == NULL )
    {
        dwError = ERROR_INVALID_PARAMETER;
        TRACE(_T("SerialPortRxThreadFn : Invalid parameter.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
        return dwError;
    }

    pobjSerialPort = (CSerialPort *)pParam;

    // Load event handles.
    pHandles[i++] = pobjSerialPort->GetRxHandle( SPRX_THREAD_EXIT_EVT_ID );
    pHandles[i++] = pobjSerialPort->GetRxHandle( SPRX_RECEIVED_DATA_EVT_ID );

    while ( (blContinue != FALSE) && (dwError == ERROR_SUCCESS) )
    {
        if ( ovComm.hEvent == NULL )
        {
            // Create event object for serial port communications events OVERLAPPED structure.
            if ( (ovComm.hEvent = ::CreateEvent(
                NULL,                           // No security
                TRUE,                           // Create a manual-reset event object
                FALSE,                          // Initial state is non-signaled
                NULL                            // No name specified
                )) == NULL )
            {
                dwError = ::GetLastError();
                TRACE(_T("SerialPortRxThreadFn : Failed to create event object.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                continue;
            }

            pHandles[i++] = ovComm.hEvent;
        }
        else
        {
            i++;
        }

        dwObjectWaitState = ::WaitForMultipleObjects( i--, pHandles, FALSE, INFINITE );

        switch ( dwObjectWaitState )
        {
        case WAIT_OBJECT_0 + SPRX_THREAD_EXIT_EVT_ID:
            blContinue = FALSE;
            break;

        case WAIT_OBJECT_0 + SPRX_RECEIVED_DATA_EVT_ID:
            if ( !::ReadFile( pobjSerialPort->m_hSerialPort, pBuf, SP_RX_BUF_SIZE, NULL, &ovComm ) )
            {
                if ( (dwError = ::GetLastError()) != ERROR_IO_PENDING )
                {
                    TRACE(_T("SerialPortRxThreadFn : Failed reading data from serial port.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                    continue;
                }
            }
            else
            {
                if ( (dwError = HandleReceivedDataOvEvent( pobjSerialPort, &ovComm, pBuf )) != ERROR_SUCCESS )
                {
                    TRACE(_T("SerialPortRxThreadFn : Failed handling serial port received data event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                    continue;
                }
            }
            break;

        case WAIT_OBJECT_0 + SPRX_MAX_EVENTS:
            if ( (dwError = HandleReceivedDataOvEvent( pobjSerialPort, &ovComm, pBuf )) != ERROR_SUCCESS )
            {
                TRACE(_T("SerialPortRxThreadFn : Failed handling serial port received data event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                continue;
            }

            ::CloseHandle( ovComm.hEvent );
            ::memset( &ovComm, 0, sizeof(OVERLAPPED) );
            break;

        default:
            dwError = ( dwObjectWaitState == WAIT_FAILED ) ? ::GetLastError() : dwObjectWaitState;
            TRACE(_T("SerialPortRxThreadFn : There is a problem with the OVERLAPPED structure's event handle.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            break;
        }
    }

    return dwError;
}
Jim Fell
  • 13,750
  • 36
  • 127
  • 202

3 Answers3

2

Looks like you're trying to use the same OVERLAPPED structure for both WaitCommEvent and ReadFile. That should be causing you no end of problems.


Documentation citation: When performing multiple simultaneous overlapped operations on a single thread, the calling thread must specify an OVERLAPPED structure for each operation


An attempt to fix it (not tested, not even compiled). Stuff you need to fill in specific to your project is marked with // TODO.

static DWORD SerialPortCommEvtsThreadFn( void * pParam )
{
    CSerialPort * pobjSerialPort = NULL;
    BOOL blContinue = TRUE;
    DWORD dwError = ERROR_SUCCESS;
    DWORD dwEventMask = 0;
    DWORD dwObjectWaitState;
    OVERLAPPED ovWaitComm = { 0 };
    OVERLAPPED ovRead = { 0 };
    const DWORD numHandles = SPCM_MAX_EVENTS + 2;
    HANDLE pHandles[numHandles];

    // Validate parameters.
    if ( pParam == NULL )
    {
        dwError = ERROR_INVALID_PARAMETER;
        TRACE(_T("SerialPortTxThreadFn : Invalid parameter.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
        return dwError;
    }
    pobjSerialPort = static_cast<CSerialPort *>(pParam);

    ovWaitComm.hEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
    ovRead.hEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
    if (!ovWaitComm.hEvent || !ovRead.hEvent) {
        dwError = ::GetLastError();
        TRACE(_T("SerialPortCommEvtsThreadFn : Failed to create event objects.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
        return dwError;
    }

    // Load event handles.
    pHandles[SPCM_THREAD_EXIT_EVT_ID ] = pobjSerialPort->GetCommHandle( SPCM_THREAD_EXIT_EVT_ID );
    pHandles[SPCM_PORT_OPEN_EVT_ID ] = pobjSerialPort->GetCommHandle( SPCM_PORT_OPEN_EVT_ID );
    pHandles[SPCM_PORT_CLOSED_EVT_ID ] = pobjSerialPort->GetCommHandle( SPCM_PORT_CLOSED_EVT_ID );
    pHandles[numHandles - 2] = ovWaitComm.hEvent;
    pHandles[numHandles - 1] = ovRead.hEvent;

    // Wait for serial port to open.
    if ( (dwObjectWaitState = ::WaitForSingleObject( pobjSerialPort->GetCommHandle( SPCM_PORT_OPEN_EVT_ID ), INFINITE )) != WAIT_OBJECT_0 ) {
        dwError = ( dwObjectWaitState == WAIT_FAILED ) ? ::GetLastError() : dwObjectWaitState;
        TRACE(_T("SerialPortCommEvtsThreadFn : Failed waiting for event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
        return dwError;
    }

    // TODO: SetCommTimeouts

    // Wait for a communications event.
    if ( !::WaitCommEvent( pobjSerialPort->m_hSerialPort, &dwEventMask, &ovWaitComm ) {
        if ( (dwError = ::GetLastError()) != ERROR_IO_PENDING ) {
            TRACE(_T("SerialPortCommEvtsThreadFn : Failed waiting for communications event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            return dwError;
        }
    }

    // TODO: Here, call ReadFile, passing &ovRead

    dwError = ERROR_SUCCESS;
    while ( blContinue && (dwError == ERROR_SUCCESS) )
    {
        dwObjectWaitState = ::WaitForMultipleObjects( numHandles, pHandles, FALSE, INFINITE );
        switch ( dwObjectWaitState )
        {
        case WAIT_OBJECT_0 + SPCM_THREAD_EXIT_EVT_ID:
            blContinue = FALSE;
            break;

        case WAIT_OBJECT_0 + SPCM_PORT_OPEN_EVT_ID:
            if ( !::ResetEvent( pobjSerialPort->GetCommHandle( SPCM_PORT_CLOSED_EVT_ID ) ) )
            {
                dwError = ::GetLastError();
                TRACE(_T("SerialPortCommEvtsThreadFn : Failed to reset event object signal state.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                continue;
            }
            break;

        case WAIT_OBJECT_0 + SPCM_PORT_CLOSED_EVT_ID:
            if ( !::ResetEvent( pobjSerialPort->GetCommHandle( SPCM_PORT_OPEN_EVT_ID ) ) )
            {
                dwError = ::GetLastError();
                TRACE(_T("SerialPortCommEvtsThreadFn : Failed to reset event object signal state.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                continue;
            }
            break;

        case WAIT_OBJECT_0 + numHandles - 2:
            if ( (dwError = HandleCommOvEvent( pobjSerialPort, dwEventMask )) != ERROR_SUCCESS )
            {
                TRACE(_T("SerialPortCommEvtsThreadFn : Failed handling communications event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
                continue;
            }

            // TODO: call WaitCommEvent again, if HandleCommOvEvent didn't already
            break;

        case WAIT_OBJECT_0 + numHandles - 1:
            // TODO: do something with the received data, it's now in the buffer supplied to ReadFile
            // TODO: call ReadFile again
            break;

        default:
            dwError = ( dwObjectWaitState == WAIT_FAILED ) ? ::GetLastError() : dwObjectWaitState;
            TRACE(_T("SerialPortCommEvtsThreadFn : Failed waiting for event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            break;
        }
    }

    return dwError;
}
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Can you please elaborate? I thought they were suppose to be using the same `OVERLAPPED` structure. – Jim Fell May 16 '11 at 19:18
  • @Jim: Oh absolutely not. The `OVERLAPPED` structure is used by the kernel to store progress of the operation. From the time you start an operation, until the operation completes (tested with `GetOverlappedResult` or `WaitForSingleObject`), the `OVERLAPPED` buffer is owned by the kernel and you should not do anything with it, and especially you cannot free it nor pass it to any API functions (exception: you can and should pass its address to `GetOverlappedResult` and `CancelIoEx`). `OVERLAPPED` is the exact analogue of the `aiocb` (async io control block) datatype in linux. – Ben Voigt May 16 '11 at 21:33
  • This should be self-evident, since if you passed the same `OVERLAPPED` buffer to multiple operations, how does `GetOverlappedResult` know which operation to check the result of? – Ben Voigt May 16 '11 at 21:34
  • The calls to `WaitCommEvent` and `ReadFile` *are* part of the same operation. `ReadFile` is used to read data from the COM port, and `WaitCommEvent` is used to determine the I/O status of that COM port. – Jim Fell May 17 '11 at 13:11
  • @Jim: Part of the same task according to the way you think about it, but not part of the same operations, as operation is defined by the Win32 API. `WaitCommEvent` starts an overlapped operation. So does `ReadFile`. These two operations need two distinct OVERLAPPED buffers. – Ben Voigt May 17 '11 at 16:24
  • I am *very* hesitant to rewrite the serial-I/O for my application because it seems like every time I even tweak it, it breaks, and it takes a couple days just to get it working again. How do I handle the two events for the two `OVERLAPPED` structures? If you could point me to an example of how this is done with asynchronous serial-I/O, or provide some pseudo-code on your answer, it would be very helpful. – Jim Fell May 17 '11 at 17:51
  • @Jim: In your updated code I see only one operation (`WaitCommEvent`), the call to `ReadFile` is gone. Are you wanting me to add that the right way? – Ben Voigt May 19 '11 at 15:47
  • @Ben Voigt: I've posted additional code. In `HandleCommOvEvent` an event is signaled to instruct the received data thread function to read the data out of the serial port. A different `OVERLAPPED` structure is used by `::ReadFile`. However, it's getting that far. – Jim Fell May 19 '11 at 16:24
  • @Ben Voigt (continued): The first time through the loop in `SerialPortCommEvtsThreadFn` the thread waits for the serial port to open, it creates the manual-reset event for the `OVERLAPPED` structure, `::WaitCommEvent` returns false with `ERROR_IO_PENDING`, and the first event returned by `WaitForMultipleObjects` is `SPCM_PORT_OPEN_EVT_ID`, which is expected because the serial port will have always just opened on the first pass. – Jim Fell May 19 '11 at 16:28
  • @Ben Voigt (continued): The second time through the loop `::WaitForSingleObject` returns immediately (port already open), variable `i` is simply incremented (overlapped event already created), and `::WaitCommEvent` returns false. This is where `::GetLastError` returns `ERROR_INVALID_PARAMETER`. – Jim Fell May 19 '11 at 16:30
  • @Ben Voigt (correction): The PC never gets to `::ReadFile` before the `ERROR_INVALID_PARAMETER` from `::WaitCommEvent` occurs. – Jim Fell May 19 '11 at 16:33
  • @Jim: You also can't have two `WaitCommEvent` operations running using the same `OVERLAPPED` buffer. Don't call `WaitCommEvent` on every pass through the loop. Call it when the previous `WaitCommEvent` completes. I've shown this in my answer. – Ben Voigt May 19 '11 at 16:36
  • @Ben Voigt: Okay, fixing the call to `::WaitCommEvent` seems to have fixed that problem. Thanks. Now I'm working on ironing out the call to `::WriteFile`, for which I'll be posting soon. – Jim Fell May 19 '11 at 17:15
  • @Jim: Not sure if you intend the bounty to remain unclaimed... accepting an answer does not award the bounty. – Ben Voigt May 19 '11 at 17:19
  • @Ben Voigt: It won't let me award the bounty until tomorrow. Here's the link to my new question: http://stackoverflow.com/questions/6062529/serial-port-write-failing – Jim Fell May 19 '11 at 18:14
0

It looks like you're calling GetLastError() when you haven't checked that there's been an error.

Jay
  • 13,803
  • 4
  • 42
  • 69
  • What do you mean? As I understand it, when doing overlapped I/O, if `WaitCommEvent` returns false, then `GetLastError` should return `ERROR_IO_PENDING`, but I'm getting `ERROR_INVALID_PARAMETER`. `WaitCommEvent` is a misleading name, per the MSDN documentation, it more-or-less returns immediately. I know there was a serious problem because the code was just spinning trying to handle the `ERROR_INVALID_PARAMETER` error, which was preventing the other threads in my application from running. – Jim Fell May 16 '11 at 18:44
0

You are calling

WaitCommEvent(pobjSerialPort->m_hSerialPort, &dwEventMask, &(pobjSerialPort->m_ovEvents))

The 1st and 3rd parameters of WaitCommEvent are input parameters, However you did not supply their initialization code.

How do you initialize m_hSerialPort? Do you call CreateFile with valid params and verify there is no error? How do you initialize m_ovEvents? Do you call CreateEvent with valid params and verify there is no error?

Since you're getting ERROR_INVALID_PARAMETER, I think your problem is in the initialization of one of these parameters.

Lior Kogan
  • 19,919
  • 6
  • 53
  • 85