void CMainFrame::OnTimer(UINT nIDEvent)
{
DWORD reslt=0;
byte *p_out=&outbuff[0];//outbuff[0],outbuff[1],outbuff[2]存放要发送的三个字节;
reslt=WriteComm(p_out,3);//调用下面的一个WriteComm子程序
if(reslt==0)
{ iLinkCount++; //通信失败记数加1
if(iLinkCount==50)
{
KillTimer(timeID);
UINT ooo=this->MessageBox("RS232通讯故障","错误",MB_RETRYCANCEL|MB_ICONSTOP);
if(ooo==IDRETRY)
timeID=SetTimer(3,100,NULL);
iLinkCount=0;
}
return;
}
iLinkCount=0;
CFrameWnd::OnTimer(nIDEvent);
}DWORD CMainFrame::WriteComm(byte *buf, DWORD dwLength)
{
BOOL fState;DWORD length=dwLength;COMSTAT ComStat;DWORD dwErrorFlags;ClearCommError(handle,&dwErrorFlags,&ComStat);fState=WriteFile(handle,buf,length,&length,&m_osWrite);if(!fState){
if(GetLastError()==ERROR_IO_PENDING){ GetOverlappedResult(handle,&m_osWrite,&length1,TRUE);// 等待}elselength=0;}
return length;}串口啥都没连的时候应该是通信故障啊,WriteFile应该是false啊!!!可WriteComm返回的是3!而不是0,这是为什么?
{
DWORD reslt=0;
byte *p_out=&outbuff[0];//outbuff[0],outbuff[1],outbuff[2]存放要发送的三个字节;
reslt=WriteComm(p_out,3);//调用下面的一个WriteComm子程序
if(reslt==0)
{ iLinkCount++; //通信失败记数加1
if(iLinkCount==50)
{
KillTimer(timeID);
UINT ooo=this->MessageBox("RS232通讯故障","错误",MB_RETRYCANCEL|MB_ICONSTOP);
if(ooo==IDRETRY)
timeID=SetTimer(3,100,NULL);
iLinkCount=0;
}
return;
}
iLinkCount=0;
CFrameWnd::OnTimer(nIDEvent);
}DWORD CMainFrame::WriteComm(byte *buf, DWORD dwLength)
{
BOOL fState;DWORD length=dwLength;COMSTAT ComStat;DWORD dwErrorFlags;ClearCommError(handle,&dwErrorFlags,&ComStat);fState=WriteFile(handle,buf,length,&length,&m_osWrite);if(!fState){
if(GetLastError()==ERROR_IO_PENDING){ GetOverlappedResult(handle,&m_osWrite,&length1,TRUE);// 等待}elselength=0;}
return length;}串口啥都没连的时候应该是通信故障啊,WriteFile应该是false啊!!!可WriteComm返回的是3!而不是0,这是为什么?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货