#include "StdAfx.h" #include "PlcIOMgr.h" #include "Propertie.h" #include "PropertieMgr.h" #include "AuthorityMgr.h" #include "FileMgr.h" #include "CStringFuc.h" #include "CommPortMgr.h" #include "LogMgr.h" #define OUT_PORT_FILE_PATH _T("\\Parameter\\PlcIOMgr\\PlcIO_Out.txt")//输出端口(控制) #define IN_PORT_FILE_PATH _T("\\Parameter\\PlcIOMgr\\PlcIO_In.txt")//输入端口(查询) #define OPEN_STR "A000000" //打开指令前缀 #define CLOSE_STR "B000000" //关闭指令前缀 #define OK_STR "OK" //反馈OK #define QUERY_STR "C000000" //查询指令前缀 #define QUERY_OK_STR "C100000" //查询反馈OK 前缀 CPlcIOMgr *gPlcIOMgr = new CPlcIOMgr; CPlcIOMgr::CPlcIOMgr(void) { m_ComPort = 6;//通信串口号 m_BaudRate = 115200;//通信波特率 m_bUse = false;//是否使用PLC I/O控制 m_bShowLog = false;//是否显示日志 } CPlcIOMgr::~CPlcIOMgr(void) { } CMFCPropertyGridProperty *CPlcIOMgr::CreatGridProperty() { CString PropertyName;//属性名称 CString Description;//描述 CString Path = _T("PlcIOMgr");//存储路径 CString Name; //-------------------------------------------------------------------------------// PropertyName = _T("PLC I/O"); CMFCPropertyGridProperty* pGroup = new CMFCPropertyGridProperty(PropertyName); //-------------------------------------------------------------------------------// if(gAuthorityMgr->CheckAuthority(_FACTORY)) { { //添加属性变量映射 Name = _T("m_bUse");//变量名字 CPropertie *pPropertie = new CPropertie; pPropertie->SetpVal((void*)&m_bUse); pPropertie->SetType(_PROP_TYPE_BOOL); pPropertie->SetpModule(this); pPropertie->SetPath(Path); pPropertie->SetName(Name); pPropertie->WriteRead(true);//读取保存的属性 //添加属性显示 PropertyName = _T("是否使用"); Description = _T("是否使用PLC I/O控制功能(重启软件后生效)"); CMFCPropertyGridProperty* p1 = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_bUse, Description); pGroup->AddSubItem(p1); gDevicePropertieMgr.Insert(p1, pPropertie); } { //添加属性变量映射 Name = _T("m_bShowLog");//变量名字 CPropertie *pPropertie = new CPropertie; pPropertie->SetpVal((void*)&m_bShowLog); pPropertie->SetType(_PROP_TYPE_BOOL); pPropertie->SetpModule(this); pPropertie->SetPath(Path); pPropertie->SetName(Name); pPropertie->WriteRead(true);//读取保存的属性 //添加属性显示 PropertyName = _T("显示日志"); Description = _T("是否显示日志信息"); CMFCPropertyGridProperty* p1 = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_bShowLog, Description); pGroup->AddSubItem(p1); gDevicePropertieMgr.Insert(p1, pPropertie); } { //添加属性变量映射 Name = _T("m_ComPort");//变量名字 CPropertie *pPropertie = new CPropertie; pPropertie->SetpVal((void*)&m_ComPort); pPropertie->SetType(_PROP_TYPE_INT); pPropertie->SetpModule(this); pPropertie->SetPath(Path); pPropertie->SetName(Name); pPropertie->WriteRead(true);//读取保存的属性 //添加属性显示 PropertyName = _T("通信串口"); Description = _T("通信用串口号"); CMFCPropertyGridProperty* p1 = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_ComPort, Description); pGroup->AddSubItem(p1); gDevicePropertieMgr.Insert(p1, pPropertie); } { //添加属性变量映射 Name = _T("m_BaudRate");//变量名字 CPropertie *pPropertie = new CPropertie; pPropertie->SetpVal((void*)&m_BaudRate); pPropertie->SetType(_PROP_TYPE_INT); pPropertie->SetpModule(this); pPropertie->SetPath(Path); pPropertie->SetName(Name); pPropertie->WriteRead(true);//读取保存的属性 //添加属性显示 PropertyName = _T("波特率"); Description = _T("串口通信用波特率"); CMFCPropertyGridProperty* p1 = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_BaudRate, Description); pGroup->AddSubItem(p1); gDevicePropertieMgr.Insert(p1, pPropertie); } } return pGroup; } void CPlcIOMgr::Ini() { if(!m_bUse) return; CFileMgr FileMgr; CString FilePath; FileMgr.GetFullFilePath(FilePath,OUT_PORT_FILE_PATH);//获取完整路径 ReadCtrlContentFile(m_OutPortVec,FilePath); FileMgr.GetFullFilePath(FilePath,IN_PORT_FILE_PATH);//获取完整路径 ReadCtrlContentFile(m_InPortVec,FilePath); } void CPlcIOMgr::ExportPar(ofstream *pFile) { (*pFile)<<"[模块] [CPlcIOMgr]------------------------------------------------"<ReadCtrlContentFile : ")+FilePath); gLogMgr->WriteDebugLog(LogStr); PortVec.clear(); CFileMgr FileMgr; CPlcIOPar PlcIOPar; vector vec; FileMgr.ReadFileToStringVec(FilePath,vec); int size = vec.size(); for(int i=0;iWriteDebugLog("Err---->not find plc port num of : " + CtrlContent); } return ""; } //切换端口状态(CtrlContent 是控制内容) bool CPlcIOMgr::SwitchPort(CString CtrlContent,bool bOpen) { //查询端口号 CString PortNum = GetPortNum(CtrlContent,m_OutPortVec); if(PortNum != "") { CCommPortMgr com; if(com.Open(m_ComPort,m_BaudRate)==false) { return false; } m_ExcuteTimes = 200; CString str1 = (bOpen?(OPEN_STR):(CLOSE_STR)); str1 += PortNum; CString str = str1+char(13)+char(10); CString CommRev; int Delay=50; int Total = 50; while(Total