API函数串口问题,大神救急
OVERLAPPED ov;
memset(&ov,0,sizeof(ov));
ov.hEvent = CreateEvent(NULL,true,False,NULL);
BOOL bSent = true;
BOOL bResult =true;
DWORD dwEvtMask = 0;
DWORD TheSentByte = 0;
DWORD dwError = 0;
BOOL thend = SetCommMask(h_S,EV_TXEMPTY);
if(!thend)
MessageBox(NULL,"error","error",MB_OK);
COMSTAT comstats;
memset(&comstats,0,sizeof(comstats));
if(bSent)
{
bResult = ::WriteFile(h_S,
m_Buffer,
theBytes,
&TheSentByte,
NULL);
while(1)
{
if(!WaitCommEvent(h_S,&dwEvtMask,&ov))
{
dwError = GetLastError();
if(dwError == ERROR_IO_PENDING)
{
bResult = ::GetOverlappedResult(h_S,
&ov,
&TheSentByte,
true);
if(!bResult)
continue;
}
}
else
{
if((dwEvtMask&EV_TXEMPTY) == EV_TXEMPTY)
break;
}
}
return true;
}
else
return false;
if(bResult)
return true;
}
为什么向上面这样WaitCommEvent函数总是返回error,而用Getlasterror函数得到的是ERROR_IO_PENDING,,。。。。不懂啊,为什么会这样啊‘
[解决办法]
其实你已经把数据从串口发送出去了,这个错误可以不用管
我曾经用VC做过一个串口通信的小软件,当时用的是CSerialPort类,我记得在该类的把判断串口是否为空的错误处理代码注释掉了,可能是写这个类的大牛也处理不了这个问题,或者他们认为没必要处理