最近用VC写了一个串口小程序,将串口写成一个类,然后动态生成一个类的实例,以此类的实例作为类的全局变量在整个程序中以此来操作串口,我的程序需要不断监听串口(即读串口),所以程序开始我就在主窗体中开辟一线程或一定时器来不断地读串口,当读到的数据为空时则不处理,不为空则转到相应的处理。同时我在子窗口中需要对串口进行写操作,这个程序本来没有什么问题,但最近我发现,如果在子窗体中不断地对串口进行写操作,也就是说间隔时间非常短,比如说在一按钮的单击来写串口,当我快速地,不断地,重复地按这个按钮来写串口,这时就会出现错误,提示内存越界错误。我起初怀疑我的读写超时错误,后来我将读写超时均设置成最大值即0XFFFFFFFF。我的串口都是以重叠方式进行操作的,读操作和写操作分别用两个不同的overlapped来进行操作,但当我快速地写串口时,仍然会报ACCESS VIOLATION错误,。我在主程序中通过监听EV_RXCHAR事件来获得从串口收到的数据。我试过在出现错误后,我将主窗体中监听串口的线程关闭掉,然后再快速地写串口,此时又没有问题。于是我怀疑读写串口同步时可能发生问题,但我已经在串口类中以重叠方式对串口进行操作,应该没有问题,希望各位讨论.
其主要错误提示为:
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC00000FD: Stack Overflow.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MFC42D.DLL): 0xC0000005: Access Violation.
First-chance exception in SmartPhone.exe (MSVCRTD.DLL): 0xC0000005: Access Violation.
memory check error at 0x017BAFEF = 0x4F, should be 0xFD.
memory check error at 0x017BAFF0 = 0x4E, should be 0xFD.
memory check error at 0x017BAFF1 = 0x45, should be 0xFD.
memory check error at 0x017BAFF2 = 0x3A, should be 0xFD.

解决方案 »

  1.   

    很简单:
    while(1){
    if (!::WaitCommEvent(m_CommPort->GetHandle(), &EvtMask, &os))
    {  
       if ((EvtMask | EV_RXCHAR)==EV_RXCHAR)
       {
    m_strTemp+=m_CommPort->Read();
    ::Sleep(10);
    m_strTemp+=m_CommPort->Read();
             if(m_strTemp.IsEmpty())  return;
    TRACE("%s\r\n",m_strTemp);
             //另作处理
             .....................
            ......................................
            ...................................
         }
      }
    }
      

  2.   

    既然你已经用通过监听EV_RXCHAR事件来获得从串口收到的数据,那为什么还要设个定时期呢?
    而且,正常的程序不需要::Sleep(10);啊!
    还有,不需要用循环while(1)是不行的