用ReadFile读串口时出现ERROR_IO_PENDING错误,不知如何解决.

Andy84920 2005-10-19 11:46:43
我看了MSDN.处理是:

case ERROR_IO_PENDING:
{
// asynchronous i/o is still in progress

// do something else for a while
GoDoSomethingElse() ;

// check on the results of the asynchronous read
bResult = GetOverlappedResult(hFile, &gOverlapped,
&nBytesRead, FALSE) ;

// if there was a problem ...
if (!bResult)
{
// deal with the error code
switch (dwError = GetLastError())
{
case ERROR_HANDLE_EOF:
{
// we're reached the end of the file
//during asynchronous operation
}

// deal with other error cases
}
}
} // end case

好像是等待的时间很短就去读串口了,但我已经等待了2妙还是不行.要怎么样处理这个问题呢?
...全文
1283 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Andy84920 2005-10-19
  • 打赏
  • 举报
回复
而我用的处理办法是:

if( dwErrCode == ERROR_IO_PENDING )
{
while( !GetOverlappedResult(m_hCom,&m_ros,&dwRead,FALSE ) )
{
dwErrCode = GetLastError();
if( dwErrCode == ERROR_IO_INCOMPLETE )
{
if ((GetTickCount()-dwTime) > nTimeOut)
{
ClearCommError( m_hCom, &dwErrorFlags, &ComStat );
return QE_FAIL;
}
Sleep(1000) ;
continue;
}
else
{
ClearCommError( m_hCom, &dwErrorFlags, &ComStat ) ;
if ( dwErrorFlags > 0)
{
//g_pEventLog->ErrorEventParamLog("Read COM Error: %u,<CE-%u>",dwErrCode,dwErrorFlags ) ;
}
break ;
}
}
}

而用GetOverlappedResult返回的结果是true.那会是哪里的问题呢?
串口通信C程序 //Set CommTimeOuts BOOL SetCommTimeOuts ( VOID ) { //SetComm Input & Output Buffer SetupComm ( hSerial ,MAXLEN * 2 ,MAXLEN * 2 ); //Read TimeOut TimeOuts.ReadIntervalTimeout = MAXDWORD; //Set Total Read TimeOut as MAXDWORD (0xFFFFFFFF) TimeOuts.ReadTotalTimeoutConstant = 0; //Read TimeOut Const TimeOuts.ReadTotalTimeoutMultiplier = 0; //Return Riht now //Write TimeOut TimeOuts.WriteTotalTimeoutMultiplier = 50; //TotalTimeOut = TimeAgr*NumOfChars+const time TimeOuts.WriteTotalTimeoutConstant = 2000; //Set Write TimeOuts return ( FALSE != SetCommTimeouts ( hSerial,&TimeOuts ) ); } //Error MsgBox BOOL ErrMsg ( HWND hWnd,TCHAR *szErr ) { return ( FALSE != MessageBox ( hWnd,szErr,NULL,MB_OK | MB_ICONWARNING ) ); } //Read Comm BOOL ReadCom ( HWND hSet,BYTE InBuff[],INT BytesNum ) { DWORD dwBytesRead = 0; //Record The bytes already read INT iBytesToRead = 0; DWORD dwErrMask = 0; //Clear Memory ZeroMemory ( &ComState ,sizeof(ComState) ); ZeroMemory ( &OvLap ,sizeof(OvLap) ); OvLap.Offset = 0; OvLap.OffsetHigh = 0; OvLap.hEvent = CreateEvent (NULL,TRUE,FALSE,NULL); //Clear All sPort error ClearCommError (hSerial,&dwErrMask,&ComState); //Get The Bytes to read from buff iBytesToRead = min (2000,ComState.cbInQue); if ( 0 == iBytesToRead ) return FALSE; else; if ( FALSE == ReadFile (hSerial,InBuff,iBytesToRead,&dwBytesRead,&OvLap ) ) { if ( GetLastError () == ERROR_IO_PENDING ) { WaitForSingleObject (OvLap.hEvent,2000); PurgeComm (hSerial,PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); } else; } else; return (TRUE); } ///Write Comm BOOL WriteCom ( BYTE *szBuff,DWORD dwBytes ) { DWORD dwErrMask = 0 ; DWORD dwBytesWritten = 0; ZeroMemory ( &OvLap ,sizeof(OvLap) ); ZeroMemory ( &ComState,sizeof(ComState) ); OvLap.Offset = 0; OvLap.OffsetHigh = 0; OvLap.hEvent = CreateEvent (NULL,TRUE,FALSE,NULL); //Clear All sPort error ClearCommError (hSerial,&dwErrMask,&ComState); if ( FALSE == WriteFile ( hSerial,szBuff,dwBytes,&dwBytesWritten ,&OvLap ) ) { if ( GetLastError () == ERROR_IO_PENDING ) { WaitForSingleObject (OvLap.hEvent,2000) ; PurgeComm (hSerial,PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); } else; } else; return ( dwBytesWritten == dwBytes ); } TCHAR* Caps ( TCHAR *szStr ) { UINT i = 0; for ( i = 0 ; szStr[i] != '\0'; i ++ ) { if ( szStr[i] >= 'A' && szStr[i] <= 'Z' ) szStr[i] += 0x20; } return szStr; }

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧