CserialPort类的简单用法

CserialPort类的用法

打开串口:
         if (m_Comm.InitPort(this,3,115200,'n',8,1,EV_RXCHAR, 512))  //接收数据类型为     
                                                         //EV_RXCHAR,缓冲区大小为512
    {
        m_Comm.StartMonitoring();           //启动线程
        m_IsOpen = true;
    }
    else
    {
        AfxMessageBox("没有发现此串口或被占用");
    }

关闭串口:

    if(m_IsOpen)          //如果串口是打开的,则关闭串口
    {
        m_Comm.ClosePort();
    }

发送数据:
    if(!m_IsOpen)
    {
        AfxMessageBox(_T("串口没有打开!"));
    }
    else
    {
        m_Comm.WriteToPort(m_strSend);//发送字符串
    }

接收数据:
LRESULT CTestDlg::OnReceiveChar(UINT ch, LONG port)
{
    CString str;
    str.Format(_T("%c"),ch);
    m_edContext.SetSel(m_edContext.GetWindowTextLength(),m_edContext.GetWindowTextLength());
    m_edContext.ReplaceSel(str);
    return 0L;
}

 

 

posted @ 2015-12-30 00:12  星雷热忱  阅读(1075)  评论(0编辑  收藏  举报