You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
261 lines
8.9 KiB
C++
261 lines
8.9 KiB
C++
#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]------------------------------------------------"<<endl;
|
|
(*pFile)<<"[m_bUse] = "<<m_bUse<<endl;
|
|
(*pFile)<<"[m_bShowLog] = "<<m_bShowLog<<endl;
|
|
(*pFile)<<"[m_BaudRate] = "<<m_BaudRate<<endl;
|
|
(*pFile)<<"[m_ComPort] = "<<m_ComPort<<endl;
|
|
(*pFile)<<"[m_ExcuteTimes] = "<<m_ExcuteTimes<<endl;
|
|
int size = m_OutPortVec.size();
|
|
for(int i=0;i<size;i++)
|
|
{
|
|
(*pFile)<<"[Out CtrlContent] = "<<m_OutPortVec[i].m_CtrlContent<<"[Out CtrlPort] = "<<m_OutPortVec[i].m_Port<<endl;
|
|
}
|
|
}
|
|
//读取控制内容的文件
|
|
void CPlcIOMgr::ReadCtrlContentFile(vector<CPlcIOPar> &PortVec,CString FilePath)
|
|
{
|
|
CString LogStr;
|
|
LogStr = (_T("Fuc---->ReadCtrlContentFile : ")+FilePath);
|
|
gLogMgr->WriteDebugLog(LogStr);
|
|
|
|
PortVec.clear();
|
|
CFileMgr FileMgr;
|
|
CPlcIOPar PlcIOPar;
|
|
vector<CString> vec;
|
|
FileMgr.ReadFileToStringVec(FilePath,vec);
|
|
int size = vec.size();
|
|
for(int i=0;i<size;i++)
|
|
{
|
|
if(i%2 == 0)
|
|
{
|
|
PlcIOPar.m_CtrlContent = vec[i];
|
|
LogStr = (_T("[CtrlContent] = ")+PlcIOPar.m_CtrlContent);
|
|
}
|
|
else
|
|
{
|
|
PlcIOPar.m_Port = vec[i];
|
|
PortVec.push_back(PlcIOPar);
|
|
LogStr.Format(_T("[port] = ")+PlcIOPar.m_Port);
|
|
}
|
|
if(m_bShowLog)
|
|
gLogMgr->WriteDebugLog(LogStr);
|
|
}
|
|
}
|
|
//查询控制内容CtrlContent 对应的端口号
|
|
CString CPlcIOMgr::GetPortNum(CString CtrlContent,vector<CPlcIOPar> &PortVec)
|
|
{
|
|
if(!m_bUse)
|
|
return "";
|
|
|
|
int size = PortVec.size();
|
|
for(int i=0;i<size;i++)
|
|
{
|
|
if(PortVec[i].m_CtrlContent == CtrlContent)
|
|
return PortVec[i].m_Port;
|
|
}
|
|
if(m_bShowLog)
|
|
{
|
|
gLogMgr->WriteDebugLog("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<m_ExcuteTimes)
|
|
{
|
|
com.Write(str);
|
|
Sleep(Delay);
|
|
CommRev = com.ReadStr();
|
|
if(CommRev == OK_STR)//反馈OK
|
|
{
|
|
break;
|
|
}
|
|
Total += Delay;
|
|
}
|
|
com.Close();
|
|
Sleep(200);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
//检查端口状态(WaitTime 是最长等待时间ms)
|
|
bool CPlcIOMgr::CheckPortState(CString PortNum,int WaitTime)
|
|
{
|
|
if(PortNum == "")
|
|
return false;
|
|
|
|
CCommPortMgr com;
|
|
if(com.Open(m_ComPort,m_BaudRate)==false)
|
|
{
|
|
return false;
|
|
}
|
|
CString str1 = QUERY_STR +PortNum;
|
|
CString SendStr = str1+char(13)+char(10);
|
|
CString OkStr = QUERY_OK_STR+PortNum;
|
|
CString CommRev;
|
|
|
|
int Delay=300;
|
|
int Total = 0;
|
|
while(Total<WaitTime)
|
|
{
|
|
com.Write(SendStr);
|
|
Sleep(Delay);
|
|
CommRev = com.ReadStr();
|
|
if(CommRev.Find(OkStr,0) != -1)
|
|
{
|
|
return true;
|
|
}
|
|
else if(CommRev.Find(SendStr,0) != -1)
|
|
{
|
|
return false;
|
|
}
|
|
Total += Delay;
|
|
}
|
|
com.Close();
|
|
Sleep(200);
|
|
return false;
|
|
} |