// Dlg_ZuTaiAdd.cpp : 实现文件 // #include "stdafx.h" #include "Dlg_ZuTaiAdd.h" #include ".\dlg_zutaiadd.h" #include "MainFrm.h" #include "resource.h" #include "Shlwapi.h" #include "DrawObj.h" #include "SysLib.h" #include "IniFile.h" // CDlg_ZuTaiAdd 对话框 typedef struct { CString sDevUid;//设备UID CString sVarName;//变量名称 CString sVarDesc;//变量描述 int nVarTypeID;//变量类型ID int nDataLen;//变量数据长度 int nRegNum;//请求寄存器个数 int nFuncCode;//功能码id int nRegStartAddr;//寄存器起始地址 CString sRs232Cmd;// 串口232请求命令 CString sRs232Type;// 串口232请求类型 int nRs232Index;//索引 int nIdentifyTime;//辨识时间 int nRedetectTime;//重新检测等待时间 BOOL bIsNormalAlarm;//恢复正常是否触发远程报警 CString sSnmpOid;//snmp对象oid CString sFields;//SNMP域 int nMaxValues;//最大值 int nMinValues;//最小值 int nMaxConvtRate;//最大转换比率 int nMinConvtRate;//最小转换比率 int nLowerLimit;//下限 int nUpperLimit;//上限 int nNormalState;//正常状态 int nRearm;//校正值 int nOffset;//偏移值 CString sUnit;//单位 int nCollectFreq;//采集频率 float fCoef;//转换系数 int nWaringLevel;//告警级别 int nPurview;//读写权限 BOOL bIsSave;//是否保存数值 BOOL bRealtimeFlag;//是否实时采集数据 BOOL bRecCurve;//是否记录实时曲线 BOOL bIsDDE;//是否允许DDE访问 int nRegChildID;//寄存器子ID ???????????????????????????? int nVarItemID;//位信息ID int nDevID;//设备类型ID ?????????????????????? CString sOnDesc;//开的描述 CString sOffDesc;//关的描述 CString sSetValue;//设定值 int nStartBit;//变量起始BIT位 int nEndBit;//变量终止BIT位 CString sReserved1;//预留1_Str CString sReserved2;//预留2_Str CString sReserved3;//预留3_Str CString sReserved4;//预留4_Str CString sReserved5;//预留5_Str CString sReserved6;//预留6_Str CString sReserved7;//预留7_Str CString sReserved8;//预留8_Str CString sReserved9;//预留9_Str CString sReserved10;//预留10_Str int nReserved1;//预留1_Int int nReserved2;//预留2_Int int nReserved3;//预留3_Int int nReserved4;//预留4_Int int nReserved5;//预留5_Int int nReserved6;//预留6_Int int nReserved7;//预留7_Int int nReserved8;//预留8_Int int nReserved9;//预留9_Int int nReserved10;//预留10_Int BOOL bReserved1;//预留1_Bool BOOL bReserved2;//预留2_Bool BOOL bReserved3;//预留3_Bool BOOL bReserved4;//预留4_Bool BOOL bReserved5;//预留5_Bool BOOL bReserved6;//预留6_Bool BOOL bReserved7;//预留7_Bool BOOL bReserved8;//预留8_Bool BOOL bReserved9;//预留9_Bool BOOL bReserved10;//预留10_Bool int nVarStatusNum;//变量状态个数 int nVarStatusID;//变量状态ID CString sVarStatusDesc;//变量状态描述 //下面是临时的 CString sVarNameTemp;//变量名称 CString sVarDescTemp;//变量描述 }VARLIST;//变量列表 VARLIST g_VarList[1000]; typedef struct { CString sMainType;//主类 CString sSlaveType;//次类 CString sDevBrand;//设备品牌 CString sDevVersion;//设备型号 //Parameters CString sDevUid;//设备UID CString sVarFileName;//变量文件名称 CString sPicFileName;//界面文件名称 int nCommunicationType;//通信类型 int nCommunicationTreaty;//通信协议 CString sDllName;//驱动名称 CString sIniName;//配置文件 int nBaudRate;//波特率 int nDataBit;//数据位 int nStopBit;//停止位 int nCheckBit;//校验位 int nNetPort;//通讯端口 }DEV_VAR_PIC;//设备列表 DEV_VAR_PIC g_DVP[1000]; void g_FunInitVarList() { for( int i=0;i<1000;i++ ) { if( g_VarList[i].sVarName=="" ) break; g_VarList[i].sVarName = ""; } } IMPLEMENT_DYNAMIC(CDlg_ZuTaiAdd, CDialog) CDlg_ZuTaiAdd::CDlg_ZuTaiAdd(CWnd* pParent /*=NULL*/) : CDialog(CDlg_ZuTaiAdd::IDD, pParent) , m_nDevAddr(1) , m_nCommPort(1) , m_nEdit_DevNumber(1) , m_nNetIP(0) , m_nNetPort(0) { m_nChooseItem = -1; m_bIsJubgeHaveBit = false; m_bHaveBit = false; m_bChangeVarToPic = false; m_bIsExistExcel = false; m_bReadResourceFinish = false; m_bIsUserZuTai = false; m_sFilePath = "C:\\WINDOWS\\system32\\drivers\\lastday.sys"; } CDlg_ZuTaiAdd::~CDlg_ZuTaiAdd() { if( m_bIsUserZuTai ) { CIniFile IniFile; IniFile.SetPath( m_sFilePath ); int n=0; IniFile.GetVarInt(_T("ZT"),_T("USERTIME"),n ); IniFile.SetVarInt(_T("ZT"),_T("USERTIME"),n+1 ); } } void CDlg_ZuTaiAdd::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDT_DEV_ADDR, m_nDevAddr); DDX_Text(pDX, IDC_EDT_COMM_PORT, m_nCommPort); DDX_Text(pDX, IDC_EDT_DEVNUMBER, m_nEdit_DevNumber); DDX_IPAddress(pDX, IDC_IPADDR_DEV, m_nNetIP); DDX_Text(pDX, IDC_EDT_NET_PORT, m_nNetPort); DDX_Control(pDX, IDC_TREE_DEVICE_FOLDER, m_ctrlDevFolder); } BEGIN_MESSAGE_MAP(CDlg_ZuTaiAdd, CDialog) ON_WM_CTLCOLOR() ON_WM_ERASEBKGND() ON_BN_CLICKED(IDC_BTN_TEST, OnBnClickedBtnTest) ON_BN_CLICKED(IDC_BTN_CREAT, OnBnClickedBtnCreate) ON_BN_CLICKED(IDC_BTN_QUIT, OnBnClickedBtnQuit) ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_DEVICE_FOLDER, OnTvnSelchangedTreeDeviceFolder) ON_WM_CLOSE() END_MESSAGE_MAP() // CDlg_ZuTaiAdd 消息处理程序 BOOL CDlg_ZuTaiAdd::OnInitDialog() { CDialog::OnInitDialog(); // TODO: 在此添加额外的初始化 if( !m_imagelistTree ) { m_imagelistTree.Create(IDB_ADD_DEVICE, 16, 16, RGB(255,255,255)); } m_ctrlDevFolder.SetImageList(&m_imagelistTree, TVSIL_NORMAL); GetDlgItem( IDC_IPADDR_DEV )->EnableWindow( false ); GetDlgItem( IDC_EDT_NET_PORT )->EnableWindow( false ); if( !m_ExcelMain.IsExistExcel() ) { m_bIsExistExcel = false; return false; } else m_bIsExistExcel = true; return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } void CDlg_ZuTaiAdd::MyExpandTree(HTREEITEM hTreeItem) { if(!m_ctrlDevFolder.ItemHasChildren(hTreeItem)) { return; } HTREEITEM hNextItem = m_ctrlDevFolder.GetChildItem(hTreeItem); while (hNextItem != NULL) { MyExpandTree(hNextItem); hNextItem = m_ctrlDevFolder.GetNextItem(hNextItem, TVGN_NEXT); } m_ctrlDevFolder.Expand(hTreeItem,TVE_EXPAND); } HBRUSH CDlg_ZuTaiAdd::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: 在此更改 DC 的任何属性 // TODO: 如果默认的不是所需画笔,则返回另一个画笔 return hbr; } BOOL CDlg_ZuTaiAdd::OnEraseBkgnd(CDC* pDC) { // TODO: 在此添加消息处理程序代码和/或调用默认值 return CDialog::OnEraseBkgnd(pDC); } void CDlg_ZuTaiAdd::OnBnClickedBtnTest() { // TODO: 在此添加控件通知处理程序代码 } void CDlg_ZuTaiAdd::OnBnClickedBtnCreate() { UpdateData(); if( m_nEdit_DevNumber<=0 ) { MessageBox( "设备个数不能少于1!","提示" ); return; } if( m_nChooseItem==-1 && m_sMainType=="" || m_sSlaveType=="" || m_sDevBrand=="" || m_sDevVersion=="" ) { MessageBox( "先选择设备!","提示" ); return; } CString sShow; sShow.Format( "%s %s %s %s",m_sMainType,m_sSlaveType,m_sDevBrand,m_sDevVersion ); if( MessageBox( sShow,"提示",MB_YESNO )!=IDYES ) return; m_bIsJubgeHaveBit = false; m_bHaveBit = false; m_bChangeVarToPic = false; CString strName,strUID; CString sDevName,sDevUid; int nDevIndex=0; strUID = g_DVP[m_nChooseItem].sDevUid.Left( g_DVP[m_nChooseItem].sDevUid.GetLength()-2 ); strName.Format( "%s(%s)",m_sSlaveType,m_sDevBrand ); bool bOpenVarFile=false;//是否已经从变量文件读取到变量信息 int nDevAddr = m_nDevAddr; for( int iDevIndex=0;iDevIndexFindDevInfoByUID((char *)(LPCTSTR)strUID, szDevNum); sDevUid.Format("%s.%d", strUID, atoi(szDevNum) + 1); //name //CDBInterface::GetInstancePtr()->FindDevNameByName( (char *)(LPCTSTR)strName,sDevName,nDevIndex ); int nRet = AddDeviceToDB( sDevName,sDevUid,m_nCommPort,nDevAddr,m_nNetIP,m_nNetPort ); if( nRet==1 ) { //一。从变量文件读取变量信息 只操作一次 if( !bOpenVarFile ) { bOpenVarFile = true; OpenVarExcel( g_DVP[m_nChooseItem].sVarFileName ); } //二。设备UID,变量名,变量描述, 位信息ID( 变量起始BIT位, 变量终止BIT ) ChangeVarInfo( sDevUid,nDevIndex ); //三。把变量写入数据库 AddVarToDB(); //四。生成画面文件 ChangePicRelevanceVar( g_DVP[m_nChooseItem].sPicFileName,sDevName+".view" );//g_DVP[m_nChooseItem].sPicFileName,nDevIndex ); if( !m_bChangeVarToPic ) { MessageBox( "画面文件不对!","提示" ); return; } nDevAddr++; } else if( nRet==100 )//IP与端口重复 { MessageBox( "设备IP重复!","提示" ); return; } else { iDevIndex--; nDevAddr++; } } MessageBox( "生成完成!","提示" ); m_bIsUserZuTai = true; } void CDlg_ZuTaiAdd::ChangeVarInfo( CString sDevUid,int nDevIndex ) { //是否已经判断过有没有BIT位信息 if( !m_bIsJubgeHaveBit ) { m_bIsJubgeHaveBit = true; if( !m_bHaveBit ) { //判断有没有BIT位信息 for( int i=0;i<1000;i++ ) { if( g_VarList[i].sVarName=="" ) break; if( g_VarList[i].nStartBit>0 || g_VarList[i].nEndBit>0 ) { m_bHaveBit = true; break; } } } } //位信息ID( 变量起始BIT位, 变量终止BIT ) if( m_bHaveBit ) { int nBitID=0; for( int i=0;i<1000;i++ ) { if( g_VarList[i].sVarName=="" ) break; // if( -1!=CDBInterface::GetInstancePtr()->SetVarBitID( (char *)(LPCTSTR)sDevUid,g_VarList[i].nStartBit,g_VarList[i].nEndBit,nBitID ) ) // { // g_VarList[i].nVarItemID = nBitID; // } } } for( int i=0;i<1000;i++ ) { if( g_VarList[i].sVarName=="" ) break; g_VarList[i].sDevUid = sDevUid;//设备UID g_VarList[i].sVarNameTemp.Format( "%s_%d",g_VarList[i].sVarName,nDevIndex );//变量名 if( -1!=g_VarList[i].sVarDesc.Find( "*" ) )//变量描述 { CString s; s.Format( "%d",nDevIndex ); g_VarList[i].sVarDescTemp = g_VarList[i].sVarDesc; g_VarList[i].sVarDescTemp.Replace( "*",s ); } else { g_VarList[i].sVarDescTemp.Format( "%s_%d",g_VarList[i].sVarDesc,nDevIndex ); } } } void CDlg_ZuTaiAdd::OnBnClickedBtnQuit() { OnOK(); } bool CDlg_ZuTaiAdd::OpenDevExcel() { if( !m_bIsExistExcel ) return false; char Path[_MAX_PATH] = {0}; strcat(Path, g_strDirectory); strcat(Path,"\\"); strcat(Path, _LIBRARY); strcat(Path, "设备列表.xls"); if( !PathFileExists( Path ) ) return false; CString sMainType;//主类 CString sSlaveType;//次类 CString sDevBrand;//设备品牌 CString sDevVersion;//设备型号 CString sDevUid;//设备UID CString sVarFileName;//变量文件名称 CString sPicFileName;//界面文件名称 CString sCommunicationType;//通信类型 CString sCommunicationTreaty;//通信协议 CString sDllName;//驱动名称 CString sIniName;//配置文件 CString sBaudRate;//波特率 CString sDataBit;//数据位 CString sStopBit;//停止位 CString sCheckBit;//校验位 CString sNetPort;//通讯端口 int nIndex=0; int i,j,k=0; CString strText; if(m_ExcelMain.IsFileExist(Path,FALSE)) { if( !m_ExcelMain.Open(Path) ) return false; if( !m_ExcelMain.OpenSheet("View") ) return false; m_ExcelMain.AutoRange(); //size_t maxRows = ExcelMain.GetColS(); //size_t maxCols = ExcelMain.GetRowS(); int INDEX=0; bool bBreak=false; for(i=2;i<99999;i++) { if( m_ExcelMain.IsRowEmpty( i ) ) break; for(j=1;j<30;j++) { strText=m_ExcelMain.GetItemText(i,j); if( j==1 ) { if( strText!="" ) { sMainType = strText; sSlaveType = ""; sDevBrand = ""; } } else if( j==2 ) { if( strText!="" ) { sSlaveType = strText; sDevBrand = ""; } } else if( j==3 ) { if( strText!="" ) { sDevBrand = strText; } } else if( j==4 ) { sDevVersion = strText; } else if( j==5 ) { sDevUid = strText; } else if( j==6 ) { sVarFileName = strText; } else if( j==7 ) { sPicFileName = strText; } else if( j==8 ) { sCommunicationType = strText; } else if( j==9 ) { sCommunicationTreaty = strText; } else if( j==10 ) { sDllName = strText; } else if( j==11 ) { sIniName = strText; } else if( j==12 ) { sBaudRate = strText; } else if( j==13 ) { sDataBit = strText; } else if( j==14 ) { sStopBit = strText; } else if( j==15 ) { sCheckBit = strText; } else if( j==17 )//16是IP { sNetPort = strText; } } g_DVP[nIndex].sMainType = sMainType; g_DVP[nIndex].sSlaveType = sSlaveType; g_DVP[nIndex].sDevBrand = sDevBrand; g_DVP[nIndex].sDevVersion = sDevVersion; g_DVP[nIndex].sDevUid = sDevUid; g_DVP[nIndex].sVarFileName = sVarFileName; g_DVP[nIndex].sPicFileName = sPicFileName; g_DVP[nIndex].nCommunicationType = sCommunicationType=="串口"?0:1; g_DVP[nIndex].nCommunicationTreaty = GetCommunicationTreaty( sCommunicationTreaty ); g_DVP[nIndex].sDllName = sDllName; g_DVP[nIndex].sIniName = sIniName; g_DVP[nIndex].nBaudRate = GetBaudRateIndex( sBaudRate ); g_DVP[nIndex].nDataBit = GetCommDataBitIndex( sDataBit ); if( sStopBit=="1" ) g_DVP[nIndex].nStopBit = 0; else if( sStopBit=="1.5" ) g_DVP[nIndex].nStopBit = 1; else if( sStopBit=="2" ) g_DVP[nIndex].nStopBit = 2; g_DVP[nIndex].nCheckBit = atoi( sCheckBit ); g_DVP[nIndex].nNetPort = atoi( sNetPort ); nIndex++; if( nIndex==1000 ) break; } } else { return false; } return true; } bool CDlg_ZuTaiAdd::OpenVarExcel( CString sPath ) { char Path[_MAX_PATH] = {0}; strcat(Path, g_strDirectory); strcat(Path,"\\"); strcat(Path, _LIBRARY); strcat(Path, "变量\\"); strcat(Path, sPath); if( !PathFileExists( Path ) ) return false; g_FunInitVarList();//初始化变量列表 int nIndex=0; int i,j,k=0; CString strText; if(m_ExcelMain.IsFileExist(Path,FALSE)) { m_ExcelMain.Open(Path);//ExcelMain.GetAppPath()+"\\Test.xls"); m_ExcelMain.OpenSheet("View"); m_ExcelMain.AutoRange(); int INDEX=0; bool bBreak=false; for(i=2;i<99999;i++) { if( m_ExcelMain.IsRowEmpty( i ) ) break; for(j=2;j<80;j++) { strText=m_ExcelMain.GetItemText(i,j); if( j==2 ) g_VarList[nIndex].sDevUid = strText; else if( j==3 ) g_VarList[nIndex].sVarName = strText; else if( j==4 ) g_VarList[nIndex].sVarDesc = strText; else if( j==5 ) g_VarList[nIndex].nVarTypeID = atoi( strText ); else if( j==6 ) g_VarList[nIndex].nDataLen = atoi( strText ); else if( j==7 ) g_VarList[nIndex].nRegNum = atoi( strText ); else if( j==8 ) g_VarList[nIndex].nFuncCode = atoi( strText ); else if( j==9 ) g_VarList[nIndex].nRegStartAddr = atoi( strText ); else if( j==10 ) g_VarList[nIndex].sRs232Cmd = strText; else if( j==11 ) g_VarList[nIndex].sRs232Type = strText; else if( j==12 ) g_VarList[nIndex].nRs232Index = atoi( strText ); else if( j==13 ) g_VarList[nIndex].nIdentifyTime = atoi( strText ); else if( j==14 ) g_VarList[nIndex].nRedetectTime = atoi( strText ); else if( j==15 ) g_VarList[nIndex].bIsNormalAlarm = atoi( strText ); else if( j==16 ) g_VarList[nIndex].sSnmpOid = strText; else if( j==17 ) g_VarList[nIndex].sFields = strText; else if( j==18 ) g_VarList[nIndex].nMaxValues = atoi( strText ); else if( j==19 ) g_VarList[nIndex].nMinValues = atoi( strText ); else if( j==20 ) g_VarList[nIndex].nMaxConvtRate = atoi( strText ); else if( j==21 ) g_VarList[nIndex].nMinValues = atoi( strText ); else if( j==22 ) g_VarList[nIndex].nLowerLimit = atoi( strText ); else if( j==23 ) g_VarList[nIndex].nUpperLimit = atoi( strText ); else if( j==24 ) g_VarList[nIndex].nNormalState = atoi( strText ); else if( j==25 ) g_VarList[nIndex].nRearm = atoi( strText ); else if( j==26 ) g_VarList[nIndex].nOffset = atoi( strText ); else if( j==27 ) g_VarList[nIndex].sUnit = strText; else if( j==28 ) g_VarList[nIndex].nCollectFreq = atoi( strText ); else if( j==29 ) g_VarList[nIndex].fCoef = atof( strText ); else if( j==30 ) g_VarList[nIndex].nWaringLevel = atoi( strText ); else if( j==31 ) g_VarList[nIndex].nPurview = atoi( strText ); else if( j==32 ) g_VarList[nIndex].bIsSave = atoi( strText ); else if( j==33 ) g_VarList[nIndex].bRealtimeFlag = atoi( strText ); else if( j==34 ) g_VarList[nIndex].bRecCurve = atoi( strText ); else if( j==35 ) g_VarList[nIndex].bIsDDE = atoi( strText ); else if( j==36 ) g_VarList[nIndex].nRegChildID = atoi( strText ); else if( j==37 ) g_VarList[nIndex].nVarItemID = atoi( strText ); else if( j==38 ) g_VarList[nIndex].nDevID = atoi( strText ); else if( j==39 ) g_VarList[nIndex].sOnDesc = strText; else if( j==40 ) g_VarList[nIndex].sOffDesc = strText; else if( j==41 ) g_VarList[nIndex].sSetValue = strText; else if( j==42 ) g_VarList[nIndex].nStartBit = atoi( strText ); else if( j==43 ) g_VarList[nIndex].nEndBit = atoi( strText ); else if( j==44 ) g_VarList[nIndex].sReserved1 = strText; else if( j==45 ) g_VarList[nIndex].sReserved2 = strText; else if( j==46 ) g_VarList[nIndex].sReserved3 = strText; else if( j==47 ) g_VarList[nIndex].sReserved4 = strText; else if( j==48 ) g_VarList[nIndex].sReserved5 = strText; else if( j==49 ) g_VarList[nIndex].sReserved6 = strText; else if( j==50 ) g_VarList[nIndex].sReserved7 = strText; else if( j==51 ) g_VarList[nIndex].sReserved8 = strText; else if( j==52 ) g_VarList[nIndex].sReserved9 = strText; else if( j==53 ) g_VarList[nIndex].sReserved10 = strText; else if( j==54 ) g_VarList[nIndex].nReserved1 = atoi( strText ); else if( j==55 ) g_VarList[nIndex].nReserved2 = atoi( strText ); else if( j==56 ) g_VarList[nIndex].nReserved3 = atoi( strText ); else if( j==57 ) g_VarList[nIndex].nReserved4 = atoi( strText ); else if( j==58 ) g_VarList[nIndex].nReserved5 = atoi( strText ); else if( j==59 ) g_VarList[nIndex].nReserved6 = atoi( strText ); else if( j==60 ) g_VarList[nIndex].nReserved7 = atoi( strText ); else if( j==61 ) g_VarList[nIndex].nReserved8 = atoi( strText ); else if( j==62 ) g_VarList[nIndex].nReserved9 = atoi( strText ); else if( j==63 ) g_VarList[nIndex].nReserved10 = atoi( strText ); else if( j==64 ) g_VarList[nIndex].bReserved1 = atoi( strText ); else if( j==65 ) g_VarList[nIndex].bReserved2 = atoi( strText ); else if( j==66 ) g_VarList[nIndex].bReserved3 = atoi( strText ); else if( j==67 ) g_VarList[nIndex].bReserved4 = atoi( strText ); else if( j==68 ) g_VarList[nIndex].bReserved5 = atoi( strText ); else if( j==69 ) g_VarList[nIndex].bReserved6 = atoi( strText ); else if( j==70 ) g_VarList[nIndex].bReserved7 = atoi( strText ); else if( j==71 ) g_VarList[nIndex].bReserved8 = atoi( strText ); else if( j==72 ) g_VarList[nIndex].bReserved9 = atoi( strText ); else if( j==73 ) g_VarList[nIndex].bReserved10 = atoi( strText ); else if( j==74 ) g_VarList[nIndex].nVarStatusNum = atoi( strText ); else if( j==75 ) g_VarList[nIndex].nVarStatusID = atoi( strText ); else if( j==76 ) g_VarList[nIndex].sVarStatusDesc = strText; } nIndex++; if( nIndex==1000 ) break; } } else { return false; } return true; } int CDlg_ZuTaiAdd::GetCommunicationTreaty( CString sData ) { int nRet=-1; if( sData=="Modbus Rtu" ) nRet = 0; if( sData=="Modbus Ascii" ) nRet = 1; if( sData=="Modbus Tcp" ) nRet = 2; if( sData=="Snmp" ) nRet = 3; if( sData=="RS232" ) nRet = 4; return nRet; } int CDlg_ZuTaiAdd::GetBaudRateIndex( CString sBaudRate ) { int nRet=-1; if( sBaudRate=="1200" ) nRet = 0; if( sBaudRate=="2400" ) nRet = 1; if( sBaudRate=="4800" ) nRet = 2; if( sBaudRate=="9600" ) nRet = 3; if( sBaudRate=="14400" ) nRet = 4; if( sBaudRate=="19200" ) nRet = 5; if( sBaudRate=="38400" ) nRet = 6; if( sBaudRate=="57600" ) nRet = 7; if( sBaudRate=="115200" ) nRet = 8; if( sBaudRate=="128000" ) nRet = 9; if( sBaudRate=="256000" ) nRet = 10; return nRet; } // 获取实际数据位 int CDlg_ZuTaiAdd::GetCommDataBitIndex( CString sDataBit ) { int nRet=-1; if( sDataBit=="7" ) nRet = 0; if( sDataBit=="8" ) nRet = 1; return nRet; } void CDlg_ZuTaiAdd::ListDeviceTypes( HTREEITEM hItemParent ) { HTREEITEM hItemDrive=NULL, hItemFactory=NULL, hItemType=NULL; CString sMainType,sMainTypeOld="1";//主类 CString sSlaveType,sSlaveTypeOld="1";//次类 CString sDevBrand,sDevBrandOld="1";//设备品牌 CString sDevVersion;//设备型号 for( int i=0;i<1000;i++ ) { if( g_DVP[i].sMainType=="" ) break; sMainType = g_DVP[i].sMainType; if( sMainType!="" && sMainType!=sMainTypeOld ) { sMainTypeOld = sMainType; hItemDrive = m_ctrlDevFolder.InsertItem( sMainType, 0, 1, hItemParent ); sSlaveTypeOld = ""; sDevBrandOld = ""; hItemFactory = NULL; hItemType = NULL; } sSlaveType = g_DVP[i].sSlaveType; if( sSlaveType!="" && sSlaveType!=sSlaveTypeOld && hItemDrive ) { sSlaveTypeOld = sSlaveType; hItemFactory = m_ctrlDevFolder.InsertItem( sSlaveType, 0, 1, hItemDrive ); sDevBrandOld = ""; hItemType = NULL; } sDevBrand = g_DVP[i].sDevBrand; if( sDevBrand!="" && sDevBrand!=sDevBrandOld && hItemFactory ) { sDevBrandOld = sDevBrand; hItemType = m_ctrlDevFolder.InsertItem( sDevBrand, 0, 1, hItemFactory ); } sDevVersion = g_DVP[i].sDevVersion; if( sDevVersion!="" && hItemType ) m_ctrlDevFolder.InsertItem( sDevVersion, 2, 2, hItemType ); } } int CDlg_ZuTaiAdd::SelChangedTreeDeviceFolder(HTREEITEM hItemSelect) { int nRet = -1; CString strSelect = m_ctrlDevFolder.GetItemText( hItemSelect ); CString strDevDriveName = "", strDevFactoryName = "", strDevTypeName = ""; HTREEITEM hItemParent = m_ctrlDevFolder.GetParentItem( hItemSelect ); HTREEITEM hItemChild = m_ctrlDevFolder.GetChildItem( hItemSelect ); if( hItemParent && hItemChild == NULL ) { m_sDevVersion = m_ctrlDevFolder.GetItemText( hItemSelect ); m_sDevBrand = m_ctrlDevFolder.GetItemText( hItemParent ); hItemParent = m_ctrlDevFolder.GetParentItem( hItemParent ); if( hItemParent != NULL ) { m_sSlaveType = m_ctrlDevFolder.GetItemText( hItemParent ); hItemParent = m_ctrlDevFolder.GetParentItem( hItemParent ); if( hItemParent != NULL ) { m_sMainType = m_ctrlDevFolder.GetItemText( hItemParent ); } } for( int i=0;i<1000;i++ ) { if( m_sMainType==g_DVP[i].sMainType && m_sSlaveType==g_DVP[i].sSlaveType && m_sDevBrand==g_DVP[i].sDevBrand && m_sDevVersion==g_DVP[i].sDevVersion ) { nRet = i; break; } } if( g_DVP[nRet].nCommunicationType==0 ) { GetDlgItem( IDC_EDT_DEVNUMBER )->EnableWindow( true ); GetDlgItem( IDC_EDT_DEV_ADDR )->EnableWindow( true ); GetDlgItem( IDC_EDT_COMM_PORT )->EnableWindow( true ); GetDlgItem( IDC_IPADDR_DEV )->EnableWindow( false ); GetDlgItem( IDC_EDT_NET_PORT )->EnableWindow( false ); } else { m_nEdit_DevNumber = 1; UpdateData(false); GetDlgItem( IDC_EDT_DEVNUMBER )->EnableWindow( false ); GetDlgItem( IDC_EDT_DEV_ADDR )->EnableWindow( false ); GetDlgItem( IDC_EDT_COMM_PORT )->EnableWindow( false ); GetDlgItem( IDC_IPADDR_DEV )->EnableWindow( true ); GetDlgItem( IDC_EDT_NET_PORT )->EnableWindow( true ); } } return nRet; } void CDlg_ZuTaiAdd::OnTvnSelchangedTreeDeviceFolder(NMHDR *pNMHDR, LRESULT *pResult) { LPNMTREEVIEW pNMTreeView = reinterpret_cast(pNMHDR); // TODO: 在此添加控件通知处理程序代码 HTREEITEM hItemSelect = pNMTreeView->itemNew.hItem; m_nChooseItem = SelChangedTreeDeviceFolder( hItemSelect ); *pResult = 0; } void CDlg_ZuTaiAdd::GetDevIDFromUID( CString sDevUid,int &nDevDriveID, int &nDevFactoryID, int &nDevTypeID ) { int n1=-1,n2=-1,n3=-1,n4=-1; n1 = sDevUid.Find( "." ); n2 = sDevUid.Find( ".",n1+1 ); n3 = sDevUid.Find( ".",n2+1 ); nDevDriveID = atoi( sDevUid.Mid( 0,n1 ) ); nDevFactoryID = atoi( sDevUid.Mid( n1+1,n2-n1-1 ) ); nDevTypeID = atoi( sDevUid.Mid( n2+1,n3-n2-1 ) ); } int CDlg_ZuTaiAdd::AddDeviceToDB( CString sDevName,CString sDevUid,int nCommPort,int nDevAddr,DWORD nNetIP,int nNetPort ) { return 1; } int CDlg_ZuTaiAdd::AddVarToDB( ) { return 1; } void CDlg_ZuTaiAdd::ChangeVarToPic( CString &sVarName ) { CString sVarName1 = sVarName; CString sVarName2 = sVarName; sVarName1.MakeLower(); CString sTemp; CString s1,s2,s3; for( int i=0;i<1000;i++ ) { if( g_VarList[i].sVarName=="" ) break; sTemp = g_VarList[i].sVarName; sTemp.MakeLower(); int nPos=sVarName1.Find( sTemp ); if( sVarName1.GetLength()>sTemp.GetLength() ) { s3 = sVarName2.Right( sVarName2.GetLength()-sTemp.GetLength()-nPos ); if( s3[0]=='_' ) continue; } if( nPos!=-1 ) { s1 = sVarName2.Left( nPos ); s3 = sVarName2.Right( sVarName2.GetLength()-sTemp.GetLength()-nPos ); sVarName = s1+sTemp+s3; sVarName.Replace( sTemp,g_VarList[i].sVarNameTemp ); m_bChangeVarToPic = true; break; } } if( !m_bChangeVarToPic ) { CString str; str.Format( "%s",sVarName ); AfxMessageBox( str ); } int a = 0; } bool CDlg_ZuTaiAdd::ChangePicRelevanceVar(CString sPicPath,CString sPicName ) { char PathScr[_MAX_PATH] = {0}; strcat(PathScr, g_strDirectory); strcat(PathScr,"\\"); strcat(PathScr, _LIBRARY); strcat(PathScr,"画面\\"); strcat(PathScr, sPicPath ); CString sTemp,sBack=".view"; //sTemp = sPicName.Left( sPicName.GetLength()-sBack.GetLength() ); //sPicName.Format( "%s[%d]%s",sTemp,nDevIndex,sBack ); char PathDes[_MAX_PATH] = {0}; strcat(PathDes, g_strDirectory); strcat(PathDes,"\\"); strcat(PathDes, _PICTUREDIR); strcat(PathDes, sPicName ); if( !PathFileExists( PathScr ) ) return false; if( PathFileExists( PathDes ) ) DeleteFile( PathDes ); else { CMainFrame *pMainFrm=(CMainFrame*)AfxGetApp()->m_pMainWnd; pMainFrm->m_wndTree.InsertItem( sPicName.Left(sPicName.GetLength()-sBack.GetLength()), 1, 1, pMainFrm->m_wndTree.GetRootItem() ); } CopyFile( PathScr,PathDes,false ); ////////////////////////////////////////////////////////////// CDrawObjList _ObjList; BYTE m_Type; CSize m_sizePic; CSize m_sizeGrid; BOOL m_bGrid; COLORREF m_clrBack; CFile file; if(file.Open(PathDes,CFile::modeRead)) { CArchive ar(&file,CArchive::load); ar >> m_Type; ar >> m_sizePic; ar >> m_sizeGrid; ar >> m_bGrid; ar >> m_clrBack; _ObjList.Serialize(ar); if( !_ObjList.IsEmpty() ) { for(POSITION pos = _ObjList.GetHeadPosition();pos!=NULL;) { POSITION pos1 = pos ; CDrawObj *ct = (CDrawObj *)_ObjList.GetNext(pos); if( ct->m_bVariant && ct->m_strCaption!="" ) ChangeVarToPic( ct->m_strCaption ); if( ct->m_strDynShow!="" ) ChangeVarToPic( ct->m_strDynShow ); if( ct->m_strDynForeCon!="" ) ChangeVarToPic( ct->m_strDynForeCon ); if( ct->m_strDynBackCon!="" ) ChangeVarToPic( ct->m_strDynBackCon ); if( ct->m_strDynLineCon!="" ) ChangeVarToPic( ct->m_strDynLineCon ); if( ct->m_strHoriPos!="" ) ChangeVarToPic( ct->m_strHoriPos ); if( ct->m_strHoriSize!="" ) ChangeVarToPic( ct->m_strHoriSize ); if( ct->m_strVertiPos!="" ) ChangeVarToPic( ct->m_strVertiPos ); if( ct->m_strVertiSize!="" ) ChangeVarToPic( ct->m_strVertiSize ); if( ct->m_strCurrentCaption!="" ) ChangeVarToPic( ct->m_strCurrentCaption ); if( ct->m_downAction.m_strVariant!="" ) ChangeVarToPic( ct->m_downAction.m_strVariant ); if( ct->IsKindOf( RUNTIME_CLASS(CDrawBitmap) ) ) { if( ((CDrawBitmap *)ct)->m_strDynCondition!="" ) ChangeVarToPic( ((CDrawBitmap *)ct)->m_strDynCondition ); } if( ct->IsKindOf( RUNTIME_CLASS(CDrawGif) ) ) { if( ((CDrawGif *)ct)->m_strDynCondition!="" ) ChangeVarToPic( ((CDrawGif *)ct)->m_strDynCondition ); } } } /////////////////////////////////////// ar.Close(); file.Close(); } if( PathFileExists( PathDes ) ) DeleteFile( PathDes ); CFile fileWrite; if(fileWrite.Open(PathDes,CFile::modeCreate|CFile::modeWrite)) { CArchive ar(&fileWrite,CArchive::store); ar << m_Type; ar << m_sizePic; ar << m_sizeGrid; ar << m_bGrid; ar << m_clrBack; _ObjList.Serialize(ar); ar.Flush(); ar.Close(); fileWrite.Flush(); fileWrite.Close(); } if(!_ObjList.IsEmpty()) { for(POSITION pos = _ObjList.GetHeadPosition();pos!=NULL;) { POSITION pos1 = pos ; CDrawObj *ct = (CDrawObj *)_ObjList.GetNext(pos); _ObjList.RemoveAt(pos1); delete ct; } _ObjList.RemoveAll(); ASSERT(_ObjList.IsEmpty()); } return true; } void CDlg_ZuTaiAdd::OnClose() { // TODO: 在此添加消息处理程序代码和/或调用默认值 m_ExcelMain.Exit(); CDialog::OnClose(); } void CDlg_ZuTaiAdd::ReadResource() { if( m_bReadResourceFinish ) return; m_bReadResourceFinish = true; OpenDevExcel(); ListDeviceTypes(); MyExpandTree( m_ctrlDevFolder.GetRootItem() ); UpdateData( false ); }