求助: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,谁知道怎么回事?
...全文
1690 9 打赏 收藏 转发到动态 举报
写回复
用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; }
Option Explicit '------------------------------------------------------------------------------- ' ' This VB module is a collection of routines to perform serial port I/O without ' using the Microsoft Comm Control component. This module uses the Windows API ' to perform the overlapped I/O operations necessary for serial communications. ' ' The routine can handle up to 4 serial ports which are identified with a ' Port ID. ' ' All routines (with the exception of CommRead and CommWrite) return an error ' code or 0 if no error occurs. The routine CommGetError can be used to get ' the complete error message. '------------------------------------------------------------------------------- '------------------------------------------------------------------------------- ' Public Constants '------------------------------------------------------------------------------- ' Output Control Lines (CommSetLine) Const LINE_BREAK = 1 Const LINE_DTR = 2 Const LINE_RTS = 3 ' Input Control Lines (CommGetLine) Const LINE_CTS = &H10& Const LINE_DSR = &H20& Const LINE_RING = &H40& Const LINE_RLSD = &H80& Const LINE_CD = &H80& '------------------------------------------------------------------------------- ' System Constants '------------------------------------------------------------------------------- Private Const ERROR_IO_INCOMPLETE = 996& Private Const ERROR_IO_PENDING = 997 Private Const GENERIC_READ = &H80000000 Private Const GENERIC_WRITE = &H40000000 Private Const FILE_ATTRIBUTE_NORMAL = &H80 Private Const FILE_FLAG_OVERLAPPED = &H40000000 Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Private Const OPEN_EXISTING = 3 ' COMM Functions Private Const MS_CTS_ON = &H10& Private Const MS_DSR_ON = &H20& Private Const MS_RING_ON = &H40& Private Const MS_RLSD_ON = &H80& Private Const PURGE_RXABORT = &H2 Private Const PURGE_RXCLEAR = &H8 Private Const PURGE_TXABORT = &H1 Private Const PURGE_TXCLEAR = &H4 ' COMM Escape Functions Private Const CLRBREAK = 9 Private Const CLRDTR

2,641

社区成员

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

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