void CCommonDlg::Onpara() // 读24c16 里的网络参数;
{
// TODO: Add your control notification handler code here
OVERLAPPED osWrite = {0};
    DWORD dwWritten;
OVERLAPPED osReader = {0};
CString str;
DWORD dwRead;
byte b[18];
byte a[18];
a[0]=0x3e;
a[1]=0x04;
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (osWrite.hEvent == NULL)   return;
    WriteFile(hComm, a, 18, &dwWritten, &osWrite);
if ( GetOverlappedResult(hComm, &osWrite, &dwWritten, TRUE) ) 

osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osReader.hEvent == NULL) return ;
Sleep(100);
ReadFile(hComm, b, 18, &dwRead, &osReader);
if( GetOverlappedResult(hComm, &osReader, &dwRead, FALSE))
{
// 处理读回的 24c16 里的 参数;

str.Format("%d.%d.%d.%d",b[0],b[1],b[2],b[3]);
m_ip=str;
UpdateData(FALSE); }
else
{
AfxMessageBox ("Read error ");
}

}// OK
    else 
{
AfxMessageBox ("error");
}

}调试时 截面上能显示 正常的内容  192.168.7.62  即 m_ip = 192.168.7.62
  
但非调试状态 就执行进了 AfxMessageBox ("Read error ");什么 故障???

解决方案 »

  1.   

    Sleep(100);???应该用WaitCommEvent
    调试时正常,而非调试时不正常,因为两者时序不一样。
      

  2.   

    第一次用VC 不懂啊 呵呵  WaitCommEvent  该怎么用??? 谢谢 揭贴及时 呵呵
      

  3.   

    WaitForSingleObject(osReader.hEvent,1000);我改成这个了  还是调试时能显示   非调试时 就 不显示了  如何是好啊
      

  4.   

    ClearCommError(m_hFile,&dwErrorFlags,&ComStat ) ;
    if (!SetCommMask(m_hFile, EV_RXCHAR))
    {
    SaveMsg("Fail to set comm mask to EV_RXCHAR");
    continue;
    }
    WaitCommEvent(m_hFile, &dwEvtMask, &m_ReadOs);
    if ((dwEvtMask & EV_RXCHAR) == EV_RXCHAR)
    {
    ...
    }