// RunTestDlg.cpp : 实现文件 // #include "stdafx.h" #include "RunTest.h" #include "RunTestDlg.h" #include "IniReader.h" #include ".\runtestdlg.h" #include "InsertDeviceDlg.h" #include "CheckProcess.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialog { public: CAboutDlg(); // 对话框数据 enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: DECLARE_MESSAGE_MAP() // virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult); }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() // CRunTestDlg 对话框 CRunTestDlg::CRunTestDlg(CWnd* pParent /*=NULL*/) : CDialog(CRunTestDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); ZeroMemory(m_hItem,sizeof(m_hItem)); m_nSelectPort = -1; m_nSelectAddr = -1; m_strDevice = ""; m_bRunTest = FALSE; } void CRunTestDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_TREE1, m_treeView); DDX_Control(pDX, IDC_RICHEDIT21, m_richedit); DDX_Control(pDX, IDC_BUTTON6, m_btnClearScreen); //DDX_Control(pDX, IDC_BUTTON5, m_btnModifyPort); //DDX_Control(pDX, IDC_BUTTON3, m_btnRunTest); } BEGIN_MESSAGE_MAP(CRunTestDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_WM_TIMER() ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1) ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2) ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedButton3) ON_MESSAGE(WM_SHOWWRITEDATA,OnShowWriteData) ON_MESSAGE(WM_SHOWREADDATA,OnShowReadData) ON_BN_CLICKED(IDC_BUTTON4, OnBnClickedButton4) ON_BN_CLICKED(IDC_BUTTON5, OnBnClickedButton5) ON_BN_CLICKED(IDC_BUTTON6, OnBnClickedButton6) END_MESSAGE_MAP() // CRunTestDlg 消息处理程序 BOOL CRunTestDlg::OnInitDialog() { CDialog::OnInitDialog(); // 将\“关于...\”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 // TODO: 在此添加额外的初始化代码 InitSTButtons(); GetMainWnd(this->m_hWnd); CreateSerialImpl(); SetTimer(0,1500,NULL); ////////////////// CString strPath; TCHAR szFull[_MAX_PATH]; TCHAR szDrive[_MAX_DRIVE]; TCHAR szDir[_MAX_DIR]; ::GetModuleFileName(NULL, szFull, sizeof(szFull)/sizeof(TCHAR)); _tsplitpath(szFull, szDrive, szDir, NULL, NULL); _tcscpy(szFull, szDrive); _tcscat(szFull, szDir); strPath = CString(szFull); CString strIni; strIni.Format("%s\\config.ini",strPath); CIniReader IniReader(strIni); m_strDepotPath.Format("%s\\%s",strPath,IniReader.GetKeyValueText("name","DeviceLibrary")); m_strRutDepotPath.Format("%s\\%s",strPath,IniReader.GetKeyValueText("name","RunUnitLibrary")); //////////////////////////////// m_imgList.Create(IDB_BITMAP1,16, 6, RGB(255,255,255)); m_treeView.SetImageList(&m_imgList,TVSIL_NORMAL); hRunRootItem = m_treeView.InsertItem("当前运行时设备库设备",2,2); InitTreeInfo(); return TRUE; // 除非设置了控件的焦点,否则返回 TRUE } void CRunTestDlg::InitTreeInfo() { CMarkup xml; if( !xml.Load(m_strRutDepotPath) ) return; xml.FindElem(); xml.IntoElem(); while ( xml.FindElem("run_object") ) { STTreeInfo tTreeInfo = {0}; CString strPort; strPort.Format("COM%s",xml.GetAttrib("port")); tTreeInfo.hPort = m_treeView.InsertItem(strPort,5,5,hRunRootItem); while( xml.FindChildElem("commands") ) { CString strChild; strChild.Format("addr %s:%s",xml.GetChildAttrib("addr"),xml.GetChildAttrib("name")); HTREEITEM hChild = m_treeView.InsertItem(strChild, 4,4,tTreeInfo.hPort); tTreeInfo.vthChild.push_back(hChild); } vtSTTreeInfo.push_back(tTreeInfo); } } void CRunTestDlg::InitSTButtons() { short shBtnColor = 30; m_btnClearScreen.SetIcon(IDI_ICON1); m_btnClearScreen.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, shBtnColor); m_btnClearScreen.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0)); /*m_btnModifyPort.SetIcon(IDI_ICON2); m_btnModifyPort.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, shBtnColor); m_btnModifyPort.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0)); m_btnRunTest.SetIcon(IDI_ICON3); m_btnRunTest.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, shBtnColor); m_btnRunTest.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0));*/ } LRESULT CRunTestDlg::OnShowWriteData(WPARAM wParam,LPARAM lParam) { if( lParam == NULL) return 0; BYTE szMsg[1024] = {0}; memcpy(szMsg, (BYTE*)lParam, (int)wParam); LOG4C((LOG_NOTICE,"Write[%d]:%s",(int)wParam,szMsg)); CString strTemp = ""; CString strWriteMsg = ""; for ( int i = 0; i < (int)wParam; i++) { strTemp.Format("%02X ",szMsg[i]); strWriteMsg += strTemp; } strWriteMsg += "\n\n"; CTime time = CTime::GetCurrentTime(); CString strTime; strTime.Format("%d-%d-%d %d:%d:%d",time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute(), time.GetSecond()); strTemp = ""; //strTemp = "写命令 "; //strTemp += strTime; strTemp.Format("写命令 %s [长度=%d]\n",strTime,(int)wParam); #if 1 CHARFORMAT cf; cf.cbSize = sizeof(CHARFORMAT); m_richedit.GetDefaultCharFormat(cf); cf.dwMask=CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_BOLD; cf.dwEffects = (unsigned long)~(CFE_UNDERLINE | CFE_BOLD | CFE_AUTOCOLOR); //cf.dwEffects ^= CFE_AUTOCOLOR; cf.crTextColor = RGB(0, 0, 0); cf.yHeight = 230; memset(cf.szFaceName, 0, LF_FACESIZE); //memcpy(cf.szFaceName, "楷体_GB2312", sizeof("楷体_GB2312")); memcpy(cf.szFaceName, "宋体", sizeof("宋体")); m_richedit.SetSelectionCharFormat(cf); m_richedit.SetSel(-1,-1); m_richedit.ReplaceSel(strTemp); #endif //Sleep(10); //strTemp += "\n"; //strTemp += strWriteMsg; //strTemp +="\r"; LOG4C((LOG_NOTICE,"Write[%d]:%s",(int)wParam,strWriteMsg)); #if 1 //CHARFORMAT cf; memset(&cf,0,sizeof(CHARFORMAT)); cf.cbSize = sizeof(CHARFORMAT); m_richedit.GetDefaultCharFormat(cf); cf.dwMask=CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_BOLD; cf.dwEffects = (unsigned long)~(CFE_UNDERLINE | CFE_BOLD | CFE_AUTOCOLOR); //cf.dwEffects ^= CFE_AUTOCOLOR; cf.crTextColor = RGB(255, 0, 0); cf.yHeight = 220; memset(cf.szFaceName, 0, LF_FACESIZE); //memcpy(cf.szFaceName, "楷体_GB2312", sizeof("楷体_GB2312")); memcpy(cf.szFaceName, "宋体", sizeof("宋体")); m_richedit.SetSelectionCharFormat(cf); m_richedit.SetSel(-1,-1); m_richedit.ReplaceSel(strWriteMsg); // m_richedit.PostMessage(WM_VSCROLL, SB_BOTTOM, 0); //int nFirstVisible = m_richedit.GetFirstVisibleLine(); //// Scroll the edit control so that the first visible line //// is the first line of text. //if (nFirstVisible > 0) //{ // m_richedit.LineScroll( 1, 0); //} #endif return 0; } // ShowReadData中进行数据校验; LRESULT CRunTestDlg::OnShowReadData(WPARAM wParam,LPARAM lParam) { if ( wParam == NULL) return 0; STRecvMessage *pRecvMessage = (STRecvMessage*)wParam; int nEr = -1; if ( strcmp(pRecvMessage->szCheck,"EMERSON") == 0) { nEr = CCheckProcess::GetReturnCheck_Emerson(pRecvMessage->byRecv,pRecvMessage->iRLen); } if ( strcmp(pRecvMessage->szCheck,"RTUCRC") == 0) { nEr = CCheckProcess::GetReturnCheck_Rtu(pRecvMessage->byRecv,pRecvMessage->iRLen); } if ( strcmp(pRecvMessage->szCheck,"fad") == 0) { nEr = CCheckProcess::GetReturnCheck_fad(pRecvMessage->byRecv,pRecvMessage->iRLen); } if ( strcmp(pRecvMessage->szCheck,"newave") == 0) { nEr = CCheckProcess::GetReturnCheck_newave(pRecvMessage->byRecv,pRecvMessage->iRLen); } if ( strcmp(pRecvMessage->szCheck,"stulz6000") == 0) { nEr = CCheckProcess::GetReturnCheck_stulz6000(pRecvMessage->byRecv,pRecvMessage->iRLen); } if ( strcmp(pRecvMessage->szCheck,"cm+") == 0) { nEr = CCheckProcess::GetReturnCheck_CM(pRecvMessage->byRecv,pRecvMessage->iRLen); } BYTE szMsg[1024] = {0}; memcpy(szMsg, pRecvMessage->byRecv, pRecvMessage->iRLen); CString strTemp = ""; CString strWriteMsg = ""; for ( int i = 0; i < pRecvMessage->iRLen; i++) { strTemp.Format("%02X ",szMsg[i]); strWriteMsg += strTemp; } strWriteMsg += "\n\n"; CTime time = CTime::GetCurrentTime(); CString strTime; strTime.Format("%d-%d-%d %d:%d:%d",time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute(), time.GetSecond()); strTemp = ""; if ( nEr != 0) { strTemp.Format("读命令 %s [长度=%d],ErrorCode=返回错误\n",strTime,pRecvMessage->iRLen); } else { strTemp.Format("读命令 %s [长度=%d],ErrorCode=返回正确\n",strTime,pRecvMessage->iRLen); } #if 1 CHARFORMAT cf; cf.cbSize = sizeof(CHARFORMAT); m_richedit.GetDefaultCharFormat(cf); cf.dwMask=CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_BOLD; cf.dwEffects = (unsigned long)~(CFE_UNDERLINE | CFE_BOLD | CFE_AUTOCOLOR); //cf.dwEffects ^= CFE_AUTOCOLOR; cf.crTextColor = RGB(0, 0, 0); cf.yHeight = 230; memset(cf.szFaceName, 0, LF_FACESIZE); // memcpy(cf.szFaceName, "楷体_GB2312", sizeof("楷体_GB2312")); memcpy(cf.szFaceName, "宋体", sizeof("宋体")); m_richedit.SetSelectionCharFormat(cf); m_richedit.SetSel(-1,-1); m_richedit.ReplaceSel(strTemp); #endif //Sleep(10); //strTemp += "\n"; //strTemp += strWriteMsg; //strTemp +="\r"; LOG4C((LOG_NOTICE,"Read[%d]:%s",pRecvMessage->iRLen,strWriteMsg)); #if 1 //CHARFORMAT cf; memset(&cf,0,sizeof(CHARFORMAT)); cf.cbSize = sizeof(CHARFORMAT); m_richedit.GetDefaultCharFormat(cf); cf.dwMask=CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_BOLD; cf.dwEffects = (unsigned long)~(CFE_UNDERLINE | CFE_BOLD | CFE_AUTOCOLOR); //cf.dwEffects ^= CFE_AUTOCOLOR; cf.crTextColor = RGB(0, 0, 255); cf.yHeight = 220; memset(cf.szFaceName, 0, LF_FACESIZE); //memcpy(cf.szFaceName, "楷体_GB2312", sizeof("楷体_GB2312")); memcpy(cf.szFaceName, "宋体", sizeof("宋体")); m_richedit.SetSelectionCharFormat(cf); m_richedit.SetSel(-1,-1); m_richedit.ReplaceSel(strWriteMsg); /*int nFirstVisible = m_richedit.GetFirstVisibleLine(); if (nFirstVisible > 0) { m_richedit.LineScroll( 1, 0); }*/ #endif return 0; } void CRunTestDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序, // 这将由框架自动完成。 void CRunTestDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); // 使图标在工作矩形中居中 int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } //当用户拖动最小化窗口时系统调用此函数取得光标显示。 HCURSOR CRunTestDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } void CRunTestDlg::OnTimer(UINT nIDEvent) { // 获取当前树结点数量 [3/29/2013 Zero.t] int nAddrCount=0; int nPortCount = vtSTTreeInfo.size(); if ( nPortCount == 0 ) { GetDlgItem(IDC_BUTTON3)->EnableWindow(FALSE);//禁用测试按钮; GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);//禁用删除按钮; } //else // GetDlgItem(IDC_BUTTON3)->EnableWindow();//启用测试按钮; STTreeInfo tTreeInfo; for ( int i = 0; i < nPortCount; i++) { tTreeInfo = vtSTTreeInfo[i]; nAddrCount += tTreeInfo.vthChild.size(); } CString strShowCount; strShowCount.Format("当前串口总数=%d个,设备总数=%d个",nPortCount,nAddrCount); CString strGet; GetDlgItemText(TX_SHOWCOUNT,strGet); if ( strGet != strShowCount ) { SetDlgItemText(TX_SHOWCOUNT,strShowCount); } //UpdateData(TRUE); // 获取当前树结点数量 [3/29/2013 Zero.t] #if 0 // 使用数据池,获取数据; if ( g_vtRecv.size() == 0) { LOG4C((LOG_NOTICE,"数据池无记录")); return ; } STRecvMessage tRecvMessage = {0}; CString strTemp; vector ::const_iterator it_rmg; for ( it_rmg = g_vtRecv.begin(); it_rmg != g_vtRecv.end(); it_rmg++) { strTemp = ""; EnterCriticalSection( &g_cs_vt ); tRecvMessage = *it_rmg; LeaveCriticalSection( &g_cs_vt ); CString str; for ( int i = 0; i < tRecvMessage.iRLen; i++) { str.Format("%02X ",tRecvMessage.byRecv[i]); strTemp += str; } if ( strTemp == "" ) { LOG4C((LOG_NOTICE,"数据池COM%d,Addr=%d,ID=%d:该返回NULL",tRecvMessage.iPort,tRecvMessage.iAddr,tRecvMessage.dwSID)); } else { LOG4C((LOG_NOTICE,"数据池COM%d,Addr=%d,ID=%d:该返回%s\n",tRecvMessage.iPort,tRecvMessage.iAddr,tRecvMessage.dwSID,strTemp)); } strTemp += "\r\n"; } #endif CTime tm = CTime::GetCurrentTime(); int nMinute = tm.GetMinute(); if ( nMinute%10 == 0 ) { int nSecond = tm.GetSecond(); if ( nSecond/5 < 2 ) { m_richedit.SetWindowText(""); } } CDialog::OnTimer(nIDEvent); } BOOL CRunTestDlg::DestroyWindow() { // TODO: 在此添加专用代码和/或调用基类 if ( pSerialImpl ) { delete pSerialImpl; pSerialImpl = NULL; } return CDialog::DestroyWindow(); } BOOL CRunTestDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) { if ( !::IsWindow(m_treeView)) { return CDialog::OnNotify(wParam, lParam, pResult); } LPNMHDR pNmhdr = (LPNMHDR)lParam; int idCtrl = LOWORD(wParam); // 获取控件ID; // 如果消息对象是该资源(树); if( idCtrl == IDC_TREE1 ) { CPoint point; ::GetCursorPos(&point); m_treeView.ScreenToClient(&point); switch( pNmhdr->code ) { case NM_CLICK: // 单击; { // Get Click item's UINT uFlags; HTREEITEM hItem = m_treeView.HitTest(point, &uFlags); if((hItem != NULL) && (TVHT_ONITEM & uFlags)) { m_treeView.SelectItem(hItem); } // [3/26/2013 Zero.t] CString strPort = m_treeView.GetItemText(hItem); if ( strPort.Find("COM") == -1) { HTREEITEM hRoot = m_treeView.GetParentItem(hItem); if ( hRoot ) { strPort = m_treeView.GetItemText(hRoot); strPort = strPort.Mid(3); m_nSelectPort = atoi(strPort.GetBuffer()); strPort.ReleaseBuffer(); CString strAddr = m_treeView.GetItemText(hItem); int iIndex = strAddr.FindOneOf(":"); strAddr = strAddr.Mid(5,iIndex-5); m_nSelectAddr = atoi(strAddr.GetBuffer()); strAddr.ReleaseBuffer(); CString strDevice = m_treeView.GetItemText(hItem); iIndex = strDevice.FindOneOf(":"); m_strDevice = strDevice.Mid(iIndex+1); } else { m_nSelectPort = -1; GetDlgItem(IDC_BUTTON3)->EnableWindow(FALSE);//禁用测试按钮; GetDlgItem(IDC_BUTTON5)->EnableWindow(FALSE);//修改串口属性 GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);//禁用删除按钮; return true; } if ( m_bRunTest == FALSE) { GetDlgItem(IDC_BUTTON2)->EnableWindow();//禁用删除按钮; GetDlgItem(IDC_BUTTON3)->EnableWindow();//禁用测试按钮; } // else // GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);//禁用删除按钮; GetDlgItem(IDC_BUTTON5)->EnableWindow(FALSE);//修改串口属性 return true; } else { m_nSelectAddr = -1; strPort = strPort.Mid(3); m_nSelectPort = atoi(strPort.GetBuffer()); strPort.ReleaseBuffer(); CObjectdepot tag; //tag.SetDevicesDepotPath(m_strDepotPath); tag.SetRuntimeDevicesDepotPath(m_strRutDepotPath); STRunPhysical tRunPhysical = tag.LookupSubPhysical(m_nSelectPort); SetDlgItemInt(IDC_EDIT1,tRunPhysical.iRate); SetDlgItemInt(IDC_EDIT10,tRunPhysical.iRsTm); SetDlgItemInt(IDC_EDIT11,tRunPhysical.iIntl); //GetDlgItem(IDC_BUTTON2)->EnableWindow();//禁用删除按钮; if ( m_bRunTest == FALSE) GetDlgItem(IDC_BUTTON2)->EnableWindow();//禁用删除按钮; if ( !m_bRunTest ) { GetDlgItem(IDC_BUTTON5)->EnableWindow(TRUE);//修改串口属性 } // }else // GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);//禁用删除按钮; } if( !m_bRunTest ) { GetDlgItem(IDC_BUTTON3)->EnableWindow(TRUE); } //}else // GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);//禁用删除按钮; // [3/26/2013 Zero.t] return true;//Already process the notify } case NM_RCLICK: // 右击; { // Get RightClick item's UINT uFlags; HTREEITEM hItem = m_treeView.HitTest(point, &uFlags); m_treeView.SelectItem(hItem); if((hItem != NULL) && (TVHT_ONITEM & uFlags)) { GetCursorPos(&m_mousePoint); // 保存当前鼠标位置; HTREEITEM hSelectedItem = m_treeView.GetSelectedItem(); if ( !m_treeView.ItemHasChildren(hSelectedItem)) { } } return true;//Already process the notify } case NM_DBLCLK: // 左键 双击; { // 双击选项, // 1.如果是设备选项,应该显示设备详细信息; // 2.如果是根元素,展开选项; // showdetail(); // UINT uFlags; HTREEITEM hItem = m_treeView.HitTest(point, &uFlags); m_treeView.SelectItem(hItem); if((hItem != NULL) && (TVHT_ONITEM & uFlags)) { GetCursorPos(&m_mousePoint); // 保存当前鼠标位置; HTREEITEM hSelectedItem = m_treeView.GetSelectedItem(); if ( !m_treeView.ItemHasChildren(hSelectedItem)) { } } return true; } } return false; // no match } return CDialog::OnNotify(wParam, lParam, pResult); } // 添加设备; void CRunTestDlg::OnBnClickedButton1()//添加设备; { // TODO: 在此添加控件通知处理程序代码 CInsertDeviceDlg dlg; if ( dlg.DoModal() == IDOK ) { BOOL bHasPort = FALSE; STTreeInfo tSTTreeInfo = {0}; // 1.查找串口是否已存在容器中; vector ::iterator it_tree; for ( it_tree = vtSTTreeInfo.begin(); it_tree != vtSTTreeInfo.end(); it_tree++) { tSTTreeInfo = *it_tree; CString strPort; strPort.Format("COM%s",dlg.m_strPort); if ( strPort == m_treeView.GetItemText(tSTTreeInfo.hPort) ) { bHasPort = TRUE; break; } } // 2.不存在; if ( !bHasPort ) { // ZeroMemory(&tSTTreeInfo,sizeof(STTreeInfo));//严禁使用ZeroMemory函数对容器指针进行清空; CString strPort; strPort.Format("COM%s",dlg.m_strPort); tSTTreeInfo.hPort = m_treeView.InsertItem(strPort,5,5,hRunRootItem); CString strChild; if ( dlg.m_bMulti == FALSE ) { strChild.Format("addr %s:%s",dlg.m_strAddr,dlg.m_strName); HTREEITEM hChild = m_treeView.InsertItem(strChild,4,4,tSTTreeInfo.hPort); #if 1 // 此处同样出错,push_back时产生了越界行为.是因为ZeroMemory(&tSTTreeInfo,sizeof(STTreeInfo));了, // 造成容器的大小被设置为0; tSTTreeInfo.vthChild.push_back(hChild); vtSTTreeInfo.push_back(tSTTreeInfo); #endif }else { for ( int i = dlg.m_nStarAddr; i <= dlg.m_nEndAddr;i++) { strChild.Format("addr %d:%s",i,dlg.m_strName); HTREEITEM hChild = m_treeView.InsertItem(strChild,4,4,tSTTreeInfo.hPort); tSTTreeInfo.vthChild.push_back(hChild); } vtSTTreeInfo.push_back(tSTTreeInfo); } } else // 3.存在; { CString strChild; if ( dlg.m_bMulti == FALSE ) { strChild.Format("addr %s:%s",dlg.m_strAddr,dlg.m_strName); HTREEITEM hChild = m_treeView.InsertItem(strChild,4,4,tSTTreeInfo.hPort); tSTTreeInfo.vthChild.push_back(hChild); }else { for ( int i = dlg.m_nStarAddr; i <= dlg.m_nEndAddr;i++) { strChild.Format("addr %d:%s",i,dlg.m_strName); HTREEITEM hChild = m_treeView.InsertItem(strChild,4,4,tSTTreeInfo.hPort); tSTTreeInfo.vthChild.push_back(hChild); } } *it_tree = tSTTreeInfo; } } GetDlgItem(IDC_BUTTON2)->EnableWindow();//测试 } void CRunTestDlg::OnBnClickedButton2()// 删除选择; { // TODO: 在此添加控件通知处理程序代码 HTREEITEM hChildItem = m_treeView.GetSelectedItem(); HTREEITEM hParantItem = m_treeView.GetParentItem(hChildItem); CObjectdepot tag; tag.SetDevicesDepotPath(m_strDepotPath); tag.SetRuntimeDevicesDepotPath(m_strRutDepotPath); //if ( hParantItem == NULL) // 根结点,该串口下的所有设备都删除; if( m_treeView.GetParentItem(hParantItem) == NULL ) { CString strPort = m_treeView.GetItemText(hChildItem); strPort = strPort.Mid(3); tag.RemoveSubElement(atoi(strPort.GetBuffer())); vector ::iterator it; for ( it = vtSTTreeInfo.begin(); it != vtSTTreeInfo.end();it++ ) { STTreeInfo tTreeInfo = *it; if ( tTreeInfo.hPort != hChildItem ) continue; vtSTTreeInfo.erase(it); m_treeView.DeleteItem(tTreeInfo.hPort); break; } if ( m_treeView.GetCount() == 0) { GetDlgItem(IDC_BUTTON3)->EnableWindow(FALSE);//测试 GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);//测试 } return; } CString strPort = m_treeView.GetItemText(hParantItem); CString strAddr = m_treeView.GetItemText(hChildItem); strPort = strPort.Mid(3); int iIndex = strAddr.FindOneOf(":"); strAddr = strAddr.Mid(5,iIndex-5); tag.RemoveSubElement(atoi(strPort.GetBuffer()), atoi(strAddr.GetBuffer())); strPort.ReleaseBuffer(); strAddr.ReleaseBuffer(); BOOL bOver = FALSE; vector ::iterator it; for ( it = vtSTTreeInfo.begin(); it != vtSTTreeInfo.end();it++ ) { STTreeInfo tTreeInfo = *it; if ( tTreeInfo.hPort != hParantItem ) continue; int Sum = tTreeInfo.vthChild.size(); HTREEITEM hChild; vector ::iterator itChild; for ( itChild = tTreeInfo.vthChild.begin(); itChild != tTreeInfo.vthChild.end();) { hChild = *itChild; if ( hChild == hChildItem) { itChild = tTreeInfo.vthChild.erase(itChild); *it = tTreeInfo; m_treeView.DeleteItem(hChildItem); bOver = TRUE; Sum = tTreeInfo.vthChild.size(); //goto End; break; }else { itChild++; } } if ( Sum == 0 ) { vtSTTreeInfo.erase(it); m_treeView.DeleteItem(tTreeInfo.hPort); break; } if ( bOver ) break; } //End: //; if ( m_treeView.GetCount() == 0) { GetDlgItem(IDC_BUTTON3)->EnableWindow(FALSE);//停止 GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);//测试 } } void CRunTestDlg::OnBnClickedButton3()// 测试运行; { CObjectdepot tag; tag.SetDevicesDepotPath(m_strDepotPath); tag.SetRuntimeDevicesDepotPath(m_strRutDepotPath); #if 0 vector vtRunComInfo; tag.GetAllSendCommand(vtRunComInfo); STRunComInfo tRunComInfo; vector ::iterator it; for ( it = vtRunComInfo.begin(); it != vtRunComInfo.end(); it++ ) { tRunComInfo = *it; pSerialImpl->OpenSerialPort( tRunComInfo.iPort, tRunComInfo.tRunPhysical.iRate, tRunComInfo.tRunPhysical.iDBit, tRunComInfo.tRunPhysical.iSBit, tRunComInfo.tRunPhysical.iPaty, tRunComInfo.tRunPhysical.iRsTm, tRunComInfo.tRunPhysical.iIntl); STSendCommand tSendCommand; vector::iterator itChild; for( itChild = tRunComInfo.vtSendCommand.begin(); itChild != tRunComInfo.vtSendCommand.end(); itChild++) { tSendCommand = *itChild; LOG4C((LOG_NOTICE,"插入命令:COM%d::%d",tRunComInfo.iPort,tSendCommand.iAddr)); pSerialImpl->InserCommand(tRunComInfo.iPort,tSendCommand.iAddr,tSendCommand.bySend,tSendCommand.iSLen,tSendCommand.szMark,tSendCommand.szCheck,tSendCommand.bRecv); ZeroMemory(&tSendCommand,sizeof(STSendCommand)); } pSerialImpl->AutoCollect(tRunComInfo.iPort); } #endif #if 1 if ( m_nSelectPort == -1 || m_nSelectPort == 0) { MessageBox("串口不存在"); return; } STRunComInfo tRunComInfo; tag.GetSendCommand(m_nSelectPort,m_nSelectAddr,tRunComInfo); BOOL bRet = pSerialImpl->OpenSerialPort( tRunComInfo.iPort, tRunComInfo.tRunPhysical.iRate, tRunComInfo.tRunPhysical.iDBit, tRunComInfo.tRunPhysical.iSBit, tRunComInfo.tRunPhysical.iPaty, tRunComInfo.tRunPhysical.iRsTm, tRunComInfo.tRunPhysical.iIntl); if ( bRet == FALSE) { MessageBox("串口打开失败,可能被占用或不存在"); return; } STSendCommand tSendCommand; vector::iterator itChild; for( itChild = tRunComInfo.vtSendCommand.begin(); itChild != tRunComInfo.vtSendCommand.end(); itChild++) { tSendCommand = *itChild; //LOG4C((LOG_NOTICE,"插入命令:COM%d::%d",tRunComInfo.iPort,tSendCommand.iAddr)); pSerialImpl->InserCommand(tRunComInfo.iPort,tSendCommand.iAddr,tSendCommand.bySend,tSendCommand.iSLen,tSendCommand.szMark,tSendCommand.szCheck,tSendCommand.bRecv); ZeroMemory(&tSendCommand,sizeof(STSendCommand)); } pSerialImpl->AutoCollect(tRunComInfo.iPort); #endif // [3/26/2013 Zero.t] GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);//添加 GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);//删除 GetDlgItem(IDC_BUTTON3)->EnableWindow(FALSE);//测试 GetDlgItem(IDC_BUTTON5)->EnableWindow(FALSE);//修改串口属性 GetDlgItem(IDC_BUTTON4)->EnableWindow(TRUE);//停止 m_bRunTest = TRUE; // [3/26/2013 Zero.t] TRACE("+++++++++++"); } /***************************************************************************** * 写入日志文件 * 函 数 名:WriteLogTxt * 功 能 写入日志 * 说 明: * 参 数: * 创 建 人:fjf * 创建时间:2009-09-09 * 修 改 人: * 修改时间: *****************************************************************************/ bool CRunTestDlg::WriteLogTxt(/*CString key, CString time,*/ CString value) { //读写文件全名 //CFileException &e; if (m_sFullName == _T("")) { AfxMessageBox("请设置日志保存路径!"); return FALSE; } //操作文件 // try // { m_sfFile.Open(m_sFullName,CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite); m_sfFile.SeekToEnd(); //m_sfFile.WriteString(time + _T("/r/n")); //m_sfFile.WriteString(value + _T("/r/n")); m_sfFile.WriteString( value ); m_sfFile.Close(); /*}*/ // catch (CFileException &e) // { // CString error; // error.Format(_T("%d"),e.m_cause); // AfxMessageBox(_T("无法写入文件!错误码:" + error)); // return false; // } return true; } /***************************************************************************** * 读取日志 * 函 数 名:ReadLogTxt * 功 能 读取日志内容 * 说 明: * 参 数:key:读取键 * rlist:日志列表控件 * 创 建 人:fjf * 创建时间:2009-09-09 * 修 改 人: * 修改时间: *****************************************************************************/ CString CRunTestDlg::ReadLogTxt(CString key,CListCtrl&rlist) { //读写文件全名 CString value ;//返回值 CString myStr = _T(""); CFileException e; // try // { //打开文档 if (!m_sfFile.Open(m_sFullName,CFile::modeCreate | CFile::modeNoTruncate| CFile::modeRead ,&e)) { CString error; error.Format(_T("%d"),e.m_cause); AfxMessageBox(_T("没有日志读取文件!错误码:" + error)); return _T(""); } while (this->m_sfFile.ReadString(value)) { //读入并保存字符 myStr = value; m_sfFile.ReadString(value); myStr += value; //添加项目 rlist.InsertItem(0,_T(""),1); rlist.SetItemText(0,1,myStr); } //关闭文件句柄 m_sfFile.Close(); // } // catch (CFileException &e) // { // CString error; // error.Format(_T("%d"),e.m_cause); // AfxMessageBox(_T("无法读取文件!错误码:" + error)); // return _T(""); // } return myStr; } void CRunTestDlg::OnBnClickedButton4()//停止测试; { // TODO: 在此添加控件通知处理程序代码 pSerialImpl->CloseSerialPort(); // [3/26/2013 Zero.t] GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);//添加 GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE);//删除 GetDlgItem(IDC_BUTTON3)->EnableWindow(TRUE);//测试 //(CWnd*)GetDlgItem(IDC_BUTTON5)->EnableWindow(TRUE);//修改串口属性 GetDlgItem(IDC_BUTTON4)->EnableWindow(FALSE);//停止 // 清空列表; //m_richedit.SetWindowText(""); m_bRunTest = FALSE; // [3/26/2013 Zero.t] } void CRunTestDlg::OnBnClickedButton5() { // TODO: 在此添加控件通知处理程序代码 CString str; GetDlgItemText(IDC_BUTTON5,str); if ( str == "修改串口属性" ) { GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE); GetDlgItem(IDC_EDIT10)->EnableWindow(TRUE); GetDlgItem(IDC_EDIT11)->EnableWindow(TRUE); SetDlgItemText(IDC_BUTTON5,"应用串口属性"); }else if ( str == "应用串口属性" ) { CObjectdepot tag; tag.SetDevicesDepotPath(m_strDepotPath); tag.SetRuntimeDevicesDepotPath(m_strRutDepotPath); GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE); GetDlgItem(IDC_EDIT10)->EnableWindow(FALSE); GetDlgItem(IDC_EDIT11)->EnableWindow(FALSE); int iRate = GetDlgItemInt(IDC_EDIT1); int iRstm = GetDlgItemInt(IDC_EDIT10); int iAFre = GetDlgItemInt(IDC_EDIT11); tag.ModifySubPhysical(m_nSelectPort,iRate,-1,-1,-1,iRstm,iAFre); SetDlgItemText(IDC_BUTTON5,"修改串口属性"); } } void CRunTestDlg::OnBnClickedButton6() { // TODO: 在此添加控件通知处理程序代码 m_richedit.SetWindowText(""); } void CRunTestDlg::OnOK() { // TODO: 在此添加专用代码和/或调用基类 //CDialog::OnOK(); }