Why does the interface always get stuck? MFC for the communication with serial port with MSComm

235 Views Asked by At

I am working on a interface for communicating with serial port by using the MSComm ActiveX: get data from the sensor via serial port and display in the edit box in VS 2005.

But every time when I run the program, after getting certain data (about 1~2 seconds), the program gets stuck, which I have to use task manager to stop. It seems that the buffer is full, but I have cleared the buffer using put_InBufferCount(0) (if I used it correctly).

Another possible reason is the size of edit box SetLimitText(10000000000000000); But it doesnt work either.

Many thanks in advance!

Here is the code in OnCommMscomm1()

void CwtsDlg::OnCommMscomm1()
{
    // TODO: Add your message handler code here

    VARIANT variant_inp;
    COleSafeArray safearray_inp;
    LONG len,k;
    BYTE rxdata[2048]; //An 8-bit integer that is not signed.
    CString strtemp;

if(m_MSComm.get_CommEvent()==2) //2 means there is data in the buffer
    {
        CString m_strRe;
        variant_inp=m_MSComm.get_Input(); //Read buffer
        m_MSComm.put_InBufferCount(0);//clear the buffer
        safearray_inp=variant_inp; //ColeSafeArray Covert
        len=safearray_inp.GetOneDimSize(); //get safe data length
        for(k=0;k<len;k++)
            safearray_inp.GetElement(&k,rxdata+k);//convert to BYTE array
        for(k=0;k<len;k++) //convert array to Cstring
        {
            BYTE bt=*(char*)(rxdata+k); //character
                strtemp.Format(L"%02X ",bt); //put characters into temperary varibles with space
            m_strRe+=strtemp; //the data in this buffer
        }
        AppendLineToMultilineEditCtrl(m_editReceive,m_strRe);//append data to edit box
    }
}
0

There are 0 best solutions below