作本身的串口調試工具-MFC

    以前一共說了幾個軟件庫,串口通信的,xml加載的,還有動態提示的,經過這三個庫就能夠實現一個本身的串口調試工具,成品以下工具

    

動態加載配置文件以後以下this

軟件能夠在xml中配置發送的字符串,不算是大工程,可是靈活使用了以前的幾個類庫,上幾個核心代碼spa

首先是遍歷系統串口,以下.net

//搜索系統當前串口,並實時顯示在選擇框中
    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文件代碼code

// 裝載配置文件
    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;
    }

 

這些是核心代碼,具體工程在下面orm

http://download.csdn.net/detail/dengrengong/8610539
相關文章
相關標籤/搜索