求助:GetLastError() == ERROR_IO_PENDING 问题?

xmzh 2006-03-24 09:42:19
boolean serialWrite(HANDLE h, int bytesToWrite,char* buffer,int *bytesWritten)

{

OVERLAPPED os = {0};

int res;


memset(&os,0,sizeof(OVERLAPPED));

os.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

res = WriteFile(h,buffer,bytesToWrite,bytesWritten,&os);

if( fWriteStat == 0 )

{

if( GetLastError() == ERROR_IO_PENDING )

{

while ( GetOverlappedResult(h,&os,numberOfBytesWritten,TRUE) == 0 )

{

dwError=GetLastError();

if( dwError == ERROR_IO_INCOMPLETE )
{
continue;
}
else
{
return FALSE;
}

}

}

}

return TRUE;

}
运行半天就会始终返回false,谁知道怎么回事?
...全文
1753 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xmzh 2006-05-11
  • 打赏
  • 举报
回复
不发数据!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
一条晚起的虫 2006-05-11
  • 打赏
  • 举报
回复
while ( GetOverlappedResult(h,&os,numberOfBytesWritten,TRUE) == 0 ) //TRUE --> FALSE

{

dwError=GetLastError();

if( dwError == ERROR_IO_INCOMPLETE )
{
continue;
}
else
{
ClearCommError(,,);
//如果还不行,看看dwError到底是多少,根据dwError大致可以判断一下原因。
return FALSE;
}
xmzh 2006-04-14
  • 打赏
  • 举报
回复
关键是为什么运行一段时间它就不发数据了,端点显示在这个地方,总是返回false
robin_yao 2006-04-12
  • 打赏
  • 举报
回复
ERROR_IO_PENDING
这个本来就不是错误,表示系统还没读完,或者写完!!
BOOL GetOverlappedResult(
HANDLE hFile, // handle to file, pipe, or device
LPOVERLAPPED lpOverlapped, // overlapped structure
LPDWORD lpNumberOfBytesTransferred, // bytes transferred
BOOL bWait // wait option
);

参数
bWait
[in] Specifies whether the function should wait for the pending overlapped operation to be completed. If TRUE, the function does not return until the operation has been completed. If FALSE and the operation is still pending, the function returns FALSE and the GetLastError function returns ERROR_IO_INCOMPLETE.
steed_jet 2006-04-06
  • 打赏
  • 举报
回复
建议在写之前调用ClearCommError;
看看GetLastError返回的错误是什么错误根据提示再改进!
xmzh 2006-03-29
  • 打赏
  • 举报
回复
运行一段时间就返回false,谁知道怎么回事?
xmzh 2006-03-28
  • 打赏
  • 举报
回复
没有改善!a
wlwlxj 2006-03-26
  • 打赏
  • 举报
回复
看看候杰翻译的那本win32多线程,比较不错,讲的详细
anothervip 2006-03-24
  • 打赏
  • 举报
回复
GetOverlappedResult(h,&os,numberOfBytesWritten,TRUE) 把TRUE->FALSE
串口通信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; }

2,644

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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