串口通訊進行時WriteFile函數報錯
GetLastError捕捉錯誤代碼如下:997 Overlapped I/O operation is in progress.  ERROR_IO_PENDING 我的問題是:
1、這種錯誤通常情況下是怎麽造成的?
2、爲什麽錯誤出現後,被程序操作的串口就只能接受數據而不能發送數據且reboot系統以後才變正常?請高手賜教!

解决方案 »

  1.   

    串口传送的 同步和异步问题
      采用异步方式 WriteFile 会立即返回出错的  但是这个情况是正常的
    后面要加一个等待程序
      

  2.   

    你用的是重叠I/O,肯定是这样的,说明你对串口编程一点不了解,我有具体程序,你来个E-MAIL我给你一个
      

  3.   

    xianghua_cq(寺院) 
    给我一个行吗?
    [email protected]
      

  4.   

    void CSerialPort::WriteChar(CSerialPort* port)
    {
    BOOL bWrite = TRUE;
    BOOL bResult = TRUE; DWORD BytesSent = 0; ResetEvent(port->m_hWriteEvent); // Gain ownership of the critical section
    EnterCriticalSection(&port->m_csCommunicationSync); if (bWrite)
    {
    // Initailize variables
    port->m_ov.Offset = 0;
    port->m_ov.OffsetHigh = 0; // Clear buffer
    PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); bResult = WriteFile(port->m_hComm, // Handle to COMM Port
    port->m_szWriteBuffer, // Pointer to message buffer in calling finction
    // strlen((char*)port->m_szWriteBuffer), // Length of message to send
    port->m_nWriteSize, // Length of message to send
    &BytesSent, // Where to store the number of bytes sent
    &port->m_ov); // Overlapped structure // deal with any error codes
    if (!bResult)  
    {
    DWORD dwError = GetLastError();
    switch (dwError)
    {
    case ERROR_IO_PENDING:
    {
    // continue to GetOverlappedResults()
    BytesSent = 0;
    bWrite = FALSE;
    break;
    }
    default:
    {
    // all other error codes
    port->ProcessErrorMessage("WriteFile()");
    }
    }

    else
    {
    LeaveCriticalSection(&port->m_csCommunicationSync);
    }
    } // end if(bWrite) if (!bWrite)
    {
    bWrite = TRUE;

    bResult = GetOverlappedResult(port->m_hComm, // Handle to COMM port 
      &port->m_ov, // Overlapped structure
      &BytesSent, // Stores number of bytes sent
      TRUE);  // Wait flag LeaveCriticalSection(&port->m_csCommunicationSync); // deal with the error code 
    // if (!bResult)  
    {
    // port->ProcessErrorMessage("GetOverlappedResults() in WriteFile()");
    }
    } // end if (!bWrite) // Verify that the data size send equals what we tried to send
    // if (BytesSent != strlen((char*)port->m_szWriteBuffer))
    {
    // TRACE("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n", BytesSent, strlen((char*)port->m_szWriteBuffer));
    }
    // ::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_TXEMPTY_DETECTED, (WPARAM) RXBuff, (LPARAM) port->m_nPortNr);
    ::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_TXEMPTY_DETECTED,0,(LPARAM) port->m_nPortNr);}