before the function call and dwCreationDisposition is CREATE_ALWAYS or OPEN_ALWAYS, a call to GetLastError returns ERROR_ALREADY_EXISTS, even when the function succeeds. If a file does not exist before the call, GetLastError returns 0 (zero).如果资源不存在的话也是返回0的
{
if(hComm==INVALID_HANDLE_VALUE)
{
hComm=CreateFile(sPortname.utf16(),GENERIC_READ |GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);//FILE_FLAG_OVERLAPPED重叠io操作
DWORD ErrInf;
if (hComm == INVALID_HANDLE_VALUE)
{
ErrInf=GetLastError();
switch(ErrInf)
{
case 0:
ErrInf=0xFFFFFFFE;//自定义的一个错误
break;
case 5:
qDebug()<<"Access denied"; //端口占用
break;
case 2:
qDebug()<<"The system cannot find the file specified";//找不到要打开的端口
break;
}
qDebug()<<"hComm Err:"<<ErrInf;
//mutex.unlock();
emit ErrDIVsig(ErrInf);
return;//如果hcom都无法找到,那么还是return掉比较好,后面的设置意义全无了
}
//设置串口参数
dcb.DCBlength = sizeof(DCB);
GetCommState(hComm,&dcb);
//dcb.BaudRate=38400;
dcb.BaudRate=nBaudRate;//这里变为自由设置,临时改一下用于调试
dcb.Parity=NOPARITY;
dcb.ByteSize=8;
dcb.StopBits=ONESTOPBIT;
if(!SetCommState(hComm,&dcb))
{
//return false;
ErrInf=0xFFFFFFFE;//自定义的一个错误
//mutex.unlock();
emit ErrDIVsig(ErrInf);
qDebug()<<"sci setcommstate err";
return;
}
//设置超时
COMMTIMEOUTS Timeouts; Timeouts.ReadIntervalTimeout = 500;//cserials带的参数
Timeouts.ReadTotalTimeoutMultiplier = 500;
Timeouts.ReadTotalTimeoutConstant = 500;
Timeouts.WriteTotalTimeoutMultiplier = 500;
Timeouts.WriteTotalTimeoutConstant = 500; if(!SetCommTimeouts(hComm,&Timeouts))
{
//return false;
ErrInf=0xFFFFFFFE;//自定义的一个错误
//mutex.unlock();
emit ErrDIVsig(ErrInf);
qDebug()<<"sci setcommtimeouts err";
return;
}
//设置缓存
if(!SetupComm(hComm,1000,1000))
{
//return false;
ErrInf=0xFFFFFFFE;//自定义的一个错误
//mutex.unlock();
emit ErrDIVsig(ErrInf);
qDebug()<<"sci set buffer err";
return;
} PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);//一次清空所有缓存 if (WROverLap.hEvent!= NULL)
{
ResetEvent(WROverLap.hEvent);
}
else
{
WROverLap.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}
//mutex.unlock();
ErrInf=0;//设置为0的时候,串口无错误
OutTimeCounter=nOutTimeCounter;//先去掉,不可调
// emit ErrDIVsig(ErrInf);
emit DeviceIsOpened(); qDebug()<<"sciThread:"<<QThread::currentThreadId(); //显示线程信息
//QMessageBox::critical( 0, "sciopen","end") ;
}}
{
szWriteBuffer=new char; if (hComm==INVALID_HANDLE_VALUE)
{
qDebug()<<"sci is not opend";
return false;
}
BackupSends=SD;//备份发送的数据
//
SendOut=SD.pOut;
DWORD nSizeToWrite =(DWORD)SD.nWriteLen;
szWriteBuffer=SD.pOut.constData();
RWtype=SD.RWtype;
nRLen=SD.nReadLenth; BOOL bWrite = TRUE;
BOOL bResult = TRUE; RevIn.resize(nRLen);
for (int i=0;i<nRLen;i++)//接受区全部给0
{
RevIn[i]=0;
} DWORD BytesSent = 0; ResetEvent(WROverLap.hEvent); if (bWrite)
{
WROverLap.Offset = 0;
WROverLap.OffsetHigh = 0; PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
bResult = WriteFile(hComm,szWriteBuffer,nSizeToWrite,&BytesSent,&WROverLap);//这里看起来调试的话,经常是出false,eer:997,重叠 I/O 操作在进行中。 if (!bResult)
{
DWORD dwError = GetLastError();
switch (dwError)
{
case ERROR_IO_PENDING:
{
BytesSent = 0;
bWrite = FALSE;
break;
}
default:
{
break;
}
}
}
}
if (!bWrite)
{
bWrite = TRUE;
bResult = GetOverlappedResult(hComm,&WROverLap,&BytesSent,TRUE);//经常到了这里,可以获得正确的返回值,所以程序可以继续?
if (!bResult)
{
//port->ProcessErrorMessage("GetOverlappedResults() in WriteFile()");
}
} if (BytesSent != nSizeToWrite)
{
qDebug()<<"WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n"<< BytesSent<<nSizeToWrite;
//返回错误代码
rD.nErr=0xDDDDDDDD;//意思是发生送超时
rD.pIn=RevIn;
rD.SendOuts=BackupSends;
//mutex.unlock();
emit DeviceRevDataSig(rD);
return false;
}
else
{
return true;
}
//删除定义的缓存地址
delete szWriteBuffer;
}