紧急求救:为什么我的串口通讯发送比较大的数据包时系统的其它操作没反应呢?(有代码)
我的代码如下:
OVERLAPPED OSWrite;
memset( &OSWrite, 0, sizeof(OVERLAPPED));
OSWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if( OSWrite.hEvent == NULL ) return false;
COMSTAT comstat;
DWORD error_flags;
// 清除通讯错误
ClearCommError(ComHandle,&error_flags,&comstat);
if( error_flags > 0 )
{
// 清除发送缓冲区
PurgeComm(ComHandle,PURGE_TXABORT | PURGE_TXCLEAR) ;
}
unsigned long count=0,res=0;
bool bError = true;
bool status = WriteFile(ComHandle,buffer,length,&count,&OSWrite);
if( !status )
{
DWORD dwError = GetLastError();
if( dwError == ERROR_IO_PENDING )
{
// dwError = 995:由于线程退出或应用程序请求,已放弃 I/O 操作。
// 997:重叠 I/O 操作在进行中。
//if( dwError != 997 && dwError != 995 )
//{
// AnsiString strErr = "";
//}
while( dwError != 995 && !GetOverlappedResult( ComHandle,&OSWrite, &count, true ) )
{
dwError = GetLastError();
if( dwError == ERROR_IO_INCOMPLETE )
{
res += count;
continue;
}
else
{
bError = true;
}
Sleep(1);
Application->ProcessMessages();
}
res += count;
bError = (unsigned)length != res;
}
else
{
bError = true;
}
}
CloseHandle(OSWrite.hEvent);
return !bError;
这是我的发送函数,我发送和接收缓冲区设置成20480,数据包最大长度20k,大家帮帮忙吧!非常感谢,
我的接收是用消息通知读取数据的。
非常感谢