做自己的串口调试工具-MFC

    之前一共说了几个软件库,串口通讯的,xml加载的,还有动态提示的,通过这三个库就可以实现一个自己的串口调试工具,成品如下

    技术分享

动态加载配置文件之后如下

技术分享

软件可以在xml中配置发送的字符串,不算是大工程,但是灵活使用了之前的几个类库,上几个核心代码

首先是遍历系统串口,如下

//搜索系统当前串口,并实时显示在选择框中
    BYTE i = 0;
    CString str;
    str.Empty();
    ((CComboBox*)GetDlgItem(IDC_COMBO_PORT_SELECT))->ResetContent();//清除显示项
    for(i = 0; i < 255; i++)
    {
        if(SIO_OK == sio_open(i))
        {
             sio_close(i);
             str.AppendFormat("COM%d",i);
             ((CComboBox *)GetDlgItem(IDC_COMBO_PORT_SELECT))->AddString(str);
             str.Empty();
        }
    }
    i = ((CComboBox *)GetDlgItem(IDC_COMBO_PORT_SELECT))->GetCount();
    if(i ==0)
    {
        GetDlgItem(IDC_BUTTON_SERIAL_CONTROL)->EnableWindow(FALSE);
    }
    else
    {
        GetDlgItem(IDC_BUTTON_SERIAL_CONTROL)->EnableWindow(TRUE);
    }

打开串口如下

// TODO: 在此添加控件通知处理程序代码
    if(serial_isopen == TRUE)//此时需要关闭串口
    {
        if(com_port > 0)
        {
            if(sio_lstatus(com_port) >= 0)
                sio_close(com_port);
            com_port = 0;
        }
        serial_isopen = FALSE;
        ((CComboBox*)GetDlgItem(IDC_COMBO_PORT_SELECT))->EnableWindow(TRUE);
        ((CComboBox*)GetDlgItem(IDC_COMBO_BAUD_SELECT))->EnableWindow(TRUE);
        ((CComboBox*)GetDlgItem(IDC_COMBO_STOP_SELECT))->EnableWindow(TRUE);
        ((CComboBox*)GetDlgItem(IDC_COMBO_CHECK_SELECT))->EnableWindow(TRUE);
        ((CComboBox*)GetDlgItem(IDC_COMBO_DATA_BIT_SELECT))->EnableWindow(TRUE);
        ((CButton*)GetDlgItem(IDC_BUTTON_SERIAL_CONTROL))->EnableWindow(TRUE);
        ((CButton*)GetDlgItem(IDC_BUTTON_SERIAL_CONTROL))->SetWindowText("打开串口");
    }
    else
    {
        //获取当前选择的串口号码
        int i = 0;
        CString str;
        int config;
         if(-1==i)
         {
             MessageBox("请选择一个串口","失败",MB_OK);
             return;
         }
         else
         {
             ((CComboBox*)GetDlgItem(IDC_COMBO_PORT_SELECT))->GetWindowText(str);
             str = str.Mid(3,str.GetLength());
             i = atoi(str);
             //检测配置信息
             if(baud == 0x80)
             {
                 MessageBox("波特率未选择","错误",MB_OK);
                 return;
             }
             if(data_length == 0x80)
             {
                 MessageBox("数据长度未选择","错误",MB_OK);
                 return;
             }
             if(stop_bit == 0x80)
             {
                 MessageBox("停止位未选择","错误",MB_OK);
                 return;
             }
             if(check_mode == 0x80)
             {
                 MessageBox("校验模式未选择","错误",MB_OK);
                 return;
             }
            config = data_length|stop_bit|check_mode;
             //开始串口配置
             if( sio_open(i) != SIO_OK)
             {
                 MessageBox("串口打开失败","提示",MB_OK);
                 this->serial_isopen = FALSE;
                 com_port = 0;
                 return;
             }
            //运行到这里代表打开成功
            com_port = i;
            serial_isopen = TRUE;
            sio_flowctrl(com_port,0x00);//关闭硬件流控制
            sio_lctrl(com_port,0x00);//关闭RTS DTR
            sio_ioctl(com_port,baud,config);
            sio_flush(com_port,2);

            ((CComboBox*)GetDlgItem(IDC_COMBO_PORT_SELECT))->EnableWindow(FALSE);
            ((CComboBox*)GetDlgItem(IDC_COMBO_BAUD_SELECT))->EnableWindow(FALSE);
            ((CComboBox*)GetDlgItem(IDC_COMBO_STOP_SELECT))->EnableWindow(FALSE);
            ((CComboBox*)GetDlgItem(IDC_COMBO_CHECK_SELECT))->EnableWindow(FALSE);
            ((CComboBox*)GetDlgItem(IDC_COMBO_DATA_BIT_SELECT))->EnableWindow(FALSE);
            ((CButton*)GetDlgItem(IDC_BUTTON_SERIAL_CONTROL))->SetWindowText("关闭串口");
         }
    }

加载xml文件代码

// 装载配置文件
    if(configFileLoadOk == false)//载入文件,载入后按钮变成卸载,下拉列表框变成禁用
    {
        int i = ((CComboBox*)(GetDlgItem(IDC_COMBO_CONFIG_FILE_SELECT)))->GetCurSel();
        CString fileName;
        if(i == -1)
        {
            MessageBox("请选择有效文件","提示",MB_OK);
            return;
        }
        else
        {
            fileName = strPath.GetAt(i);
        }
        //加载文件
        XMLError load = configXmlFile.LoadFile(fileName);
        if(load != XML_SUCCESS)
        {
            MessageBox("文件加载失败","提示",MB_OK);
            configFileLoadOk = false;
            return;
        }
        //文件加载成功
        XMLElement* pElement;
        XMLElement* hRoot;
        //加载根节点
        hRoot = configXmlFile.RootElement();
        if(!hRoot)
        {
            MessageBox("根节点错误","提示",MB_OK);
            return;
        } 
        //根节点加载成功
        buttonSendString->RemoveAll();
        //加载快捷按键信息
        pElement = hRoot->FirstChildElement("button_messages")->FirstChildElement("button");//寻找一个子元素
        CString index ;
        CString nameString ;
        CString promptMessage;
        CString serialSendString;
        CString enable;
        bool enableFlag;
        int indexValue;
        //获取全部button数据
        do 
        {
            index = pElement->FirstChildElement("index")->GetText();
            nameString = pElement->FirstChildElement("name")->GetText();
            promptMessage = pElement->FirstChildElement("promptmessage")->GetText();
            serialSendString = pElement->FirstChildElement("serialSendString")->GetText();
            enable = pElement->FirstChildElement("enable")->GetText();
            XMLUtil util;
            util.ToInt(index,&indexValue);
            util.ToBool(enable,&enableFlag);
            buttonSendString->Add(serialSendString);
            switch (indexValue)
            {
            case 1:
                GetDlgItem(IDC_BUTTON_SHORT1)->EnableWindow(enableFlag);
                if(enable)
                {
                    GetDlgItem(IDC_BUTTON_SHORT1)->SetWindowText(nameString);
                    m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT1));
                    m_toolstip.AddTool(GetDlgItem(IDC_BUTTON_SHORT1),promptMessage);
                }
                break;
            case 2:
                GetDlgItem(IDC_BUTTON_SHORT2)->EnableWindow(enableFlag);
                if(enable)
                {
                    GetDlgItem(IDC_BUTTON_SHORT2)->SetWindowText(nameString);
                    m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT2));
                    m_toolstip.AddTool(GetDlgItem(IDC_BUTTON_SHORT2),promptMessage);
                }
                break;
            case 3:
                GetDlgItem(IDC_BUTTON_SHORT3)->EnableWindow(enableFlag);
                if(enable)
                {
                    GetDlgItem(IDC_BUTTON_SHORT3)->SetWindowText(nameString);
                    m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT3));
                    m_toolstip.AddTool(GetDlgItem(IDC_BUTTON_SHORT3),promptMessage);
                }
                break;
            case 4:
                GetDlgItem(IDC_BUTTON_SHORT4)->EnableWindow(enableFlag);
                if(enable)
                {
                    GetDlgItem(IDC_BUTTON_SHORT4)->SetWindowText(nameString);
                    m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT4));
                    m_toolstip.AddTool(GetDlgItem(IDC_BUTTON_SHORT4),promptMessage);
                }
                break;
            case 5:
                GetDlgItem(IDC_BUTTON_SHORT5)->EnableWindow(enableFlag);
                if(enable)
                {
                    GetDlgItem(IDC_BUTTON_SHORT5)->SetWindowText(nameString);
                    m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT5));
                    m_toolstip.AddTool(GetDlgItem(IDC_BUTTON_SHORT5),promptMessage);
                }
                break;
            case 6:
                GetDlgItem(IDC_BUTTON_SHORT6)->EnableWindow(enableFlag);
                if(enable)
                {
                    GetDlgItem(IDC_BUTTON_SHORT6)->SetWindowText(nameString);
                    m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT6));
                    m_toolstip.AddTool(GetDlgItem(IDC_BUTTON_SHORT6),promptMessage);
                }
                break;
            case 7:
                GetDlgItem(IDC_BUTTON_SHORT7)->EnableWindow(enableFlag);
                if(enable)
                {
                    GetDlgItem(IDC_BUTTON_SHORT7)->SetWindowText(nameString);
                    m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT7));
                    m_toolstip.AddTool(GetDlgItem(IDC_BUTTON_SHORT7),promptMessage);
                }
                break;
            case 8:
                GetDlgItem(IDC_BUTTON_SHORT8)->EnableWindow(enableFlag);
                if(enable)
                {
                    GetDlgItem(IDC_BUTTON_SHORT8)->SetWindowText(nameString);
                    m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT8));
                    m_toolstip.AddTool(GetDlgItem(IDC_BUTTON_SHORT8),promptMessage);
                }
                break;
            }
            pElement = pElement->NextSiblingElement();
        } while (pElement);

        //加载列表框讯息
        listSendString->RemoveAll();
        pElement = hRoot->FirstChildElement("list_messages")->FirstChildElement("list");//寻找一个子元素
        UINT32 num = 0;
        do 
        {
            index = pElement->FirstChildElement("index")->GetText();
            nameString = pElement->FirstChildElement("name")->GetText();
            promptMessage = pElement->FirstChildElement("promptmessage")->GetText();
            serialSendString = pElement->FirstChildElement("serialSendString")->GetText();
            enable = pElement->FirstChildElement("enable")->GetText();
            XMLUtil util;
            util.ToInt(index,&indexValue);
            util.ToBool(enable,&enableFlag);
            if(enableFlag)
            {
                listSendString->Add(serialSendString);
                CString show;
                show.Empty();
                show.AppendFormat("%d  命令名: %s  功能: %s",num,nameString,promptMessage);
                ((CListBox*)GetDlgItem(IDC_LIST1))->AddString(show);
                num++;
            }
            pElement = pElement->NextSiblingElement();
        } while (pElement);
        
        int count = ((CListBox*)GetDlgItem(IDC_LIST1))->GetCount();
        if(count >  0)
        {
            GetDlgItem(IDC_BUTTON_SEND_SHORT_FUCTION)->EnableWindow(TRUE);
            GetDlgItem(IDC_BUTTON_SEND_SELECT_SHORT)->EnableWindow(TRUE);
            GetDlgItem(IDC_EDIT_FUNC_SEL)->EnableWindow(TRUE);
            GetDlgItem(IDC_BUTTON_ADD_FUNC)->EnableWindow(TRUE);
            GetDlgItem(IDC_BUTTON_DELFUNC)->EnableWindow(TRUE);
            GetDlgItem(IDC_BUTTON_DEFAULT)->EnableWindow(TRUE);
        }

        GetDlgItem(IDC_COMBO_CONFIG_FILE_SELECT)->EnableWindow(FALSE);
        GetDlgItem(IDC_BUTTON_LOAD_CONFIG_FILE)->SetWindowText("卸载");
        configFileLoadOk = true;
    }
    else
    {
        ((CListBox*)GetDlgItem(IDC_LIST1))->ResetContent();

        GetDlgItem(IDC_BUTTON_SHORT1)->SetWindowText("none");
        GetDlgItem(IDC_BUTTON_SHORT2)->SetWindowText("none");
        GetDlgItem(IDC_BUTTON_SHORT3)->SetWindowText("none");
        GetDlgItem(IDC_BUTTON_SHORT4)->SetWindowText("none");
        GetDlgItem(IDC_BUTTON_SHORT5)->SetWindowText("none");
        GetDlgItem(IDC_BUTTON_SHORT6)->SetWindowText("none");
        GetDlgItem(IDC_BUTTON_SHORT7)->SetWindowText("none");
        GetDlgItem(IDC_BUTTON_SHORT8)->SetWindowText("none");
        GetDlgItem(IDC_BUTTON_SHORT1)->EnableWindow(false);
        m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT1));
        GetDlgItem(IDC_BUTTON_SHORT2)->EnableWindow(false);
        m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT2));
        GetDlgItem(IDC_BUTTON_SHORT3)->EnableWindow(false);
        m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT3));
        GetDlgItem(IDC_BUTTON_SHORT4)->EnableWindow(false);
        m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT4));
        GetDlgItem(IDC_BUTTON_SHORT5)->EnableWindow(false);
        m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT5));
        GetDlgItem(IDC_BUTTON_SHORT6)->EnableWindow(false);
        m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT6));
        GetDlgItem(IDC_BUTTON_SHORT7)->EnableWindow(false);
        m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT7));
        GetDlgItem(IDC_BUTTON_SHORT8)->EnableWindow(false);
        m_toolstip.DelTool(GetDlgItem(IDC_BUTTON_SHORT8));
        GetDlgItem(IDC_COMBO_CONFIG_FILE_SELECT)->EnableWindow(true);
        GetDlgItem(IDC_BUTTON_LOAD_CONFIG_FILE)->SetWindowText("载入");

        GetDlgItem(IDC_BUTTON_SEND_SHORT_FUCTION)->EnableWindow(FALSE);
        GetDlgItem(IDC_BUTTON_SEND_SELECT_SHORT)->EnableWindow(FALSE);
        GetDlgItem(IDC_EDIT_FUNC_SEL)->EnableWindow(FALSE);
        GetDlgItem(IDC_BUTTON_ADD_FUNC)->EnableWindow(FALSE);
        GetDlgItem(IDC_BUTTON_DELFUNC)->EnableWindow(FALSE);
        GetDlgItem(IDC_BUTTON_DEFAULT)->EnableWindow(FALSE);
        configFileLoadOk = false;
    }

 

这些是核心代码,具体工程在下面

http://download.csdn.net/detail/dengrengong/8610539

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。