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.
TwoLaserHead-PushJig/LaiPuLaser/PciPortMgr.cpp

280 lines
11 KiB
C++

#include "StdAfx.h"
#include "PciPortMgr.h"
#include "Propertie.h"
#include "PropertieMgr.h"
#include "Motor.h"
#include "AuthorityMgr.h"
#define PCI_PORT_CNT 16 //莱普pci 卡端口的个数
CPciPortMgr *gPciPortMgr = new CPciPortMgr;
CPciPortMgr::CPciPortMgr(void)
{
//初始化pci 的端口控制容器
IniPciPortVec();
m_OutPortDefaultStateL = 0;//输出端口低八位的默认状态
m_OutPortDefaultStateH = 0;//输出端口高八位的默认状态
}
CPciPortMgr::~CPciPortMgr(void)
{
}
void CPciPortMgr::Ini()
{
//初始化端口控制类型
IniPortCtrlType();
//初始化端口变化的观察者
IniObserver();
}
//初始化pci 的端口控制容器
void CPciPortMgr::IniPciPortVec()
{
m_PciPortCnt = PCI_PORT_CNT*2;//输入输出在一起
for(int i=0;i<m_PciPortCnt;i++)
{
m_PciPortVec.push_back(make_pair(PCI_PORT_NULL,false));
}
}
//初始化端口控制类型
void CPciPortMgr::IniPortCtrlType()
{
//输出端口-------------------------------------
m_OutPortCtrlType.push_back(PCI_PORT_NULL);
//X 轴
m_OutPortCtrlType.push_back(PCI_PORT_PLUS_X);
m_OutPortCtrlType.push_back(PCI_PORT_DIR_X);
//Y 轴
m_OutPortCtrlType.push_back(PCI_PORT_DIR_Y);
m_OutPortCtrlType.push_back(PCI_PORT_PLUS_Y);
//Z 轴
m_OutPortCtrlType.push_back(PCI_PORT_DIR_Z);
m_OutPortCtrlType.push_back(PCI_PORT_PLUS_Z);
//激光
m_OutPortCtrlType.push_back(PCI_PORT_LASER);
//辅助激光
m_OutPortCtrlType.push_back(PCI_PORT_LASER_2);
//抽风
m_OutPortCtrlType.push_back(PCI_PORT_EXHAUST_FAN);
//真空1
m_OutPortCtrlType.push_back(PCI_PORT_VACUUM_OUT1);
//真空2
m_OutPortCtrlType.push_back(PCI_PORT_VACUUM_OUT2);
//塔灯
m_OutPortCtrlType.push_back(PCI_PORT_LIGHT_R);
m_OutPortCtrlType.push_back(PCI_PORT_LIGHT_Y);
m_OutPortCtrlType.push_back(PCI_PORT_LIGHT_G);
m_OutPortCtrlType.push_back(PCI_PORT_ALARM);
//吹气
m_OutPortCtrlType.push_back(PCI_PORT_AIR);
//门
m_OutPortCtrlType.push_back(PCI_PORT_DOOR);
//PLC
m_OutPortCtrlType.push_back(PCI_PORT_PLC_1);
m_OutPortCtrlType.push_back(PCI_PORT_PLC_2);
m_OutPortCtrlType.push_back(PCI_PORT_PLC_3);
m_OutPortCtrlType.push_back(PCI_PORT_PLC_4);
m_OutPortCtrlType.push_back(PCI_PORT_PLC_5);
//输入端口-------------------------------------
m_InPortCtrlType.push_back(PCI_PORT_NULL);
m_InPortCtrlType.push_back(PCI_PORT_LIMIT_SWITCH_X_F);
m_InPortCtrlType.push_back(PCI_PORT_LIMIT_SWITCH_X_N);
m_InPortCtrlType.push_back(PCI_PORT_LIMIT_SWITCH_Y_F);
m_InPortCtrlType.push_back(PCI_PORT_LIMIT_SWITCH_Y_N);
m_InPortCtrlType.push_back(PCI_PORT_LIMIT_SWITCH_Z_F);
m_InPortCtrlType.push_back(PCI_PORT_LIMIT_SWITCH_Z_N);
m_InPortCtrlType.push_back(PCI_PORT_VACUUM_IN1);
m_InPortCtrlType.push_back(PCI_PORT_VACUUM_IN2);
m_InPortCtrlType.push_back(PCI_PORT_AIR_CYLINDER_1);
m_InPortCtrlType.push_back(PCI_PORT_AIR_CYLINDER_2);
m_InPortCtrlType.push_back(PCI_PORT_DOOR_CHECK);
m_InPortCtrlType.push_back(PCI_PORT_GRATING);
m_InPortCtrlType.push_back(PCI_PORT_STOP);
m_InPortCtrlType.push_back(PCI_PORT_FIBER1);
m_InPortCtrlType.push_back(PCI_PORT_FIBER2);
}
//初始化端口变化的观察者
void CPciPortMgr::IniObserver()
{
//因为电机端口要高效率使用,所以特殊处理
CMotor *pMotorX = CMotor::GetMotor(MOTOR_X);
CMotor *pMotorY = CMotor::GetMotor(MOTOR_Y);
CMotor *pMotorZ = CMotor::GetMotor(MOTOR_Z);
pMotorX->SetPciPortType(PCI_PORT_DIR_X,PCI_PORT_PLUS_X,PCI_PORT_LIMIT_SWITCH_X_F,PCI_PORT_LIMIT_SWITCH_X_N);
pMotorY->SetPciPortType(PCI_PORT_DIR_Y,PCI_PORT_PLUS_Y,PCI_PORT_LIMIT_SWITCH_Y_F,PCI_PORT_LIMIT_SWITCH_Y_N);
pMotorZ->SetPciPortType(PCI_PORT_DIR_Z,PCI_PORT_PLUS_Z,PCI_PORT_LIMIT_SWITCH_Z_F,PCI_PORT_LIMIT_SWITCH_Z_N);
//电机马达为端口改变的观察者
Attach(pMotorX);
Attach(pMotorY);
Attach(pMotorZ);
}
#if 1//属性设置
CMFCPropertyGridProperty *CPciPortMgr::CreatGridProperty()
{
CString PropertyName;//属性名称
CString Description;//描述
CString Path = _T("PciPort");;//存储路径
CString Name;
//-------------------------------------------------------------------------------//
PropertyName = _T("PCI 端口设置");
CMFCPropertyGridProperty* pGroup = new CMFCPropertyGridProperty(PropertyName);
//-------------------------------------------------------------------------------//
if(gAuthorityMgr->CheckAuthority(_FACTORY))
{
{
//添加属性变量映射
Name = _T("m_OutPortDefaultStateL");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_OutPortDefaultStateL);
pPropertie->SetType(_PROP_TYPE_INT);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("默认输出低");
Description = _T("输出端口低八位的默认状态,二进制的每一位控制一个端口");
CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_OutPortDefaultStateL, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
{
//添加属性变量映射
Name = _T("m_OutPortDefaultStateH");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_OutPortDefaultStateH);
pPropertie->SetType(_PROP_TYPE_INT);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("默认输出高");
Description = _T("输出端口高八位的默认状态,二进制的每一位控制一个端口");
CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_OutPortDefaultStateH, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
for(int i=0;i<m_PciPortCnt;i++)
{
CString Idx;
if(i<PCI_PORT_CNT)
{
Idx.Format("%ld",i);
PropertyName = "out 端口" + Idx;
}
else
{
Idx.Format("%ld",i%PCI_PORT_CNT);
PropertyName = "in 端口" + Idx;
}
Idx.Format("%ld",i);
CMFCPropertyGridProperty* pGroup1 = new CMFCPropertyGridProperty(PropertyName);
{
//添加属性变量映射
Name = "m_PciPortVec_first" + Idx;//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&(m_PciPortVec[i].first));
pPropertie->SetType(_PROP_TYPE_STRING);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("控制内容");
Description = _T("端口控制的内容");
CMFCPropertyGridProperty* p1 = new CMFCPropertyGridProperty(PropertyName,(_variant_t)m_PciPortVec[i].first, Description);
//插入端口可以控制的类型
if(i<PCI_PORT_CNT)
InsertOutCtrlType(p1);
else
InsertInCtrlType(p1);
p1->AllowEdit(FALSE);//不可修改
pGroup1->AddSubItem(p1);
gDevicePropertieMgr.Insert(p1, pPropertie);
}
{
//添加属性变量映射
Name = "m_PciPortVec_second" + Idx;//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_PciPortVec[i].second);
pPropertie->SetType(_PROP_TYPE_BOOL);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("是否反向");
Description = _T("端口反向后写入和读取都会取相反的值");
CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_PciPortVec[i].second, Description);
pGroup1->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
pGroup->AddSubItem(pGroup1);
}
}
//-------------------------------------------------------------------------------//
return pGroup;
}
void CPciPortMgr::ExportPar(ofstream *pFile)
{
(*pFile)<<"[模块] [CPciPortMgr]------------------------------------------------"<<endl;
for(int i=0;i<m_PciPortCnt;i++)
{
(*pFile)<<"[端口号]"<<i<<endl;
(*pFile)<<"[控制内容]"<<m_PciPortVec[i].first<<endl;
(*pFile)<<"[是否反向]"<<m_PciPortVec[i].second<<endl;
}
}
//当设置发生改变时通知所有的观察者
void CPciPortMgr::OnPropertyChanged()
{
NotifyObservers();
}
#endif
//插入out 端口可以控制的类型到属性设置
void CPciPortMgr::InsertOutCtrlType(CMFCPropertyGridProperty* p)
{
vector<CString>::iterator iter = m_OutPortCtrlType.begin();
vector<CString>::iterator iter_end = m_OutPortCtrlType.end();
for(;iter!=iter_end;iter++)
{
p->AddOption(*iter);
}
}
//插入in 端口可以控制的类型到属性设置
void CPciPortMgr::InsertInCtrlType(CMFCPropertyGridProperty* p)
{
vector<CString>::iterator iter = m_InPortCtrlType.begin();
vector<CString>::iterator iter_end = m_InPortCtrlType.end();
for(;iter!=iter_end;iter++)
{
p->AddOption(*iter);
}
}
//查询控制类型对应的端口号
SPciPort CPciPortMgr::GetCtrlPort(CString CtrlContent)
{
SPciPort PciPort;
vector<pair<CString,bool>>::iterator iter = m_PciPortVec.begin();
vector<pair<CString,bool>>::iterator iter_end = m_PciPortVec.end();
for(int i=0;iter!=iter_end;iter++,i++)
{
if(CtrlContent == (*iter).first)
{
PciPort.num = i%16;//注意(设置8 还是等于0的)
PciPort.bReverse = (*iter).second;
break;
}
}
return PciPort;//没找到
}