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.

681 lines
20 KiB
C++

#include "StdAfx.h"
#include "LaserDeviceMgr.h"
#include "Propertie.h"
#include "PropertieMgr.h"
#include "AuthorityMgr.h"
#include "CStringFuc.h"
#include "ExceptionMsg.h"
#include "LogMgr.h"
#include "GlobalFunction.h"
#define MAX_CUR 7.8 //最大电流值
#define DelayTime 200
CLaserDeviceMgr *gCLaserDeviceMgr = new CLaserDeviceMgr;
CLaserDeviceMgr::CLaserDeviceMgr(void)
{
m_bUse = true;
m_ComPort = 11;//通信用com 口编号
m_ComBaudrate = 9600;//通信波特率(innolase 19200)(SpectraPhysics 9600)
m_Power = 7;//功率 百分比%
m_Fre = 50000;//激光频率
//m_DeviceType = _LASER_INNOLASE;//激光器类型
m_DeviceType = _LASER_SPECTRA_PHYSICS;//激光器类型
m_LaserSleepTimeMinutes = 30;//绿光激光器休眠计时(分钟)
m_CurLeftTimes = "00:00:00";
m_bExitAppCloseLaser = true;//退出软件时关闭激光器
}
CLaserDeviceMgr::~CLaserDeviceMgr(void)
{
//TurnOffEmission();
}
void CLaserDeviceMgr::Ini()
{
if(m_DeviceType==_LASER_INNOLASE)
{
//打开串口
if(m_ComMgr.Open(m_ComPort,m_ComBaudrate))
{
//StartReadInfo();
}
else
{
gLogMgr->WriteDebugLog("激光器串口打开失败!");
}
}
}
#if 1//属性设置
CMFCPropertyGridProperty *CLaserDeviceMgr::CreatGridProperty()
{
CString PropertyName;//属性名称
CString Description;//描述
CString Path = _T("SpectraPhysicsLaser");//存储路径
CString Name;
//-------------------------------------------------------------------------------//
PropertyName = _T("激光器");
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("是否使用激光器外控功能");
CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_bUse, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
{
//添加属性变量映射
Name = _T("m_bExitAppCloseLaser");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_bExitAppCloseLaser);
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_bExitAppCloseLaser, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
{
//添加属性变量映射
Name = _T("m_Com");//变量名字
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("激光器和PC 通信使用的串口编号");
CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_ComPort, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
{
//添加属性变量映射
Name = _T("m_ComBaudrate");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_ComBaudrate);
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_ComBaudrate, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
{
//添加属性变量映射
Name = _T("m_Diode");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_Power);
pPropertie->SetType(_PROP_TYPE_DOUBLE);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("激光功率");
Description = _T("激光器功率");
CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_Power, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
{
//添加属性变量映射
Name = _T("m_Fre");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_Fre);
pPropertie->SetType(_PROP_TYPE_INT);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("激光频率");
Description = _T("激光器的频率(hz)");
CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_Fre, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
{
//添加属性变量映射
Name = _T("m_LaserSleepTimeMinutes");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_LaserSleepTimeMinutes);
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_LaserSleepTimeMinutes, Description);
pGroup->AddSubItem(p1);
gDevicePropertieMgr.Insert(p1, pPropertie);
}
}
//-------------------------------------------------------------------------------//
return pGroup;
}
void CLaserDeviceMgr::ExportPar(ofstream *pFile)
{
(*pFile)<<"[模块] [CLaserDeviceMgr]------------------------------------------------"<<endl;
(*pFile)<<"[是否使用]"<<m_bUse<<endl;
(*pFile)<<"[串口号]"<<m_ComPort<<endl;
}
#endif
#if 1
void CLaserDeviceMgr::WriteWorkFileExt(vector<CLab> &LabVec)
{
}
void CLaserDeviceMgr::ReadWorkFile(CLabVecRang &LabVecRang)
{
}
#endif
#if 1
//开始加工之前的操作
void CLaserDeviceMgr::OnWorkStart()
{
//结束激光器休眠倒计时
//StopLaserSleepTime();
SetParToLaserDevice(true);
}
//加工结束之后的操作
void CLaserDeviceMgr::OnWorkEnd()
{
//开始激光器休眠倒计时
//StartLaserSleepTime();
}
void CLaserDeviceMgr::SetParToLaserDevice(bool bException)
{
if(m_bUse)
{
//设置参数
if(m_DeviceType==_LASER_SPECTRA_PHYSICS)
{
SetParToLaserSpectraPhysics();
}
else if(m_DeviceType==_LASER_INNOLASE)
{
SetParToLaserInnolase(bException);
}
}
}
#endif
#if 1//美国光谱物理
//设置激光器参数---光谱
void CLaserDeviceMgr::SetParToLaserSpectraPhysics()
{
gLogMgr->WriteDebugLog("func ; CLaserDeviceMgr---->SetParToLaserSpectraPhysics");
CCommPortMgr com;
if(com.Open(m_ComPort))
{
{//打开emission
CString str1 = _T("ON");
CString str = str1+char(13)+char(10);
com.Write(str);
Sleep(DelayTime);
}
{//检查emission
CString str1 = _T("?D");
CString str = str1+char(13)+char(10);
com.Write(str);
Sleep(DelayTime);
CString ret = com.ReadStr();
if(ret == "0")
{
CString str = _T("激光器emission 打开失败");
CExceptionMsg Msg;
Msg.SetMsg(str);
throw Msg;
}
}
//设置电流
{
CString str1 = Db2CString(m_Power*MAX_CUR*0.01);
CString str = "C:"+ str1+char(13)+char(10);
com.Write(str);
Sleep(DelayTime);
}
//设置频率
{
CString str1 = Int2CString(m_Fre*1000);
CString str = "Q:"+ str1+char(13)+char(10);
com.Write(str);
Sleep(DelayTime);
}
}
else
{
CString str = _T("激光器串口通信失败");
gLogMgr->WriteDebugLog(str);
}
}
//获取激光器当前状态
CString CLaserDeviceMgr::GetLaserState()
{
CString State;
CCommPortMgr com;
//打开串口
if(com.Open(m_ComPort))
{
CString str1 = _T("?F");
CString str = str1+char(13)+char(10);
com.Write(str);
Sleep(DelayTime);
//读取反馈
State = com.ReadStr();
com.Close();
}
else
{
State = _T("串口通信失败");
}
return State;
}
//获得当前热机时间
CString CLaserDeviceMgr::GetWarmTime()
{
CString time = _T("0");
CCommPortMgr com;
//打开串口
if(com.Open(m_ComPort))
{
CString str1 = _T("WARMUPTIME?");
CString str = str1+char(13)+char(10);
com.Write(str);
Sleep(DelayTime);
//读取反馈
time = com.ReadStr();
com.Close();
}
return time;
}
void CLaserDeviceMgr::TurnOnEmission()
{
gLogMgr->WriteDebugLog("func ; CLaserDeviceMgr---->TurnOnEmission");
CCommPortMgr com;
//打开串口
if(com.Open(m_ComPort)==false)
{
return;
}
//打开emission
{
CString str1 = _T("ON");
CString str = str1+char(13)+char(10);
com.Write(str);
Sleep(DelayTime);
}
com.Close();
}
void CLaserDeviceMgr::TurnOffEmission()
{
CCommPortMgr com;
//打开串口
if(com.Open(m_ComPort)==false)
{
return;
}
//打开emission
{
CString str1 = _T("OFF");
CString str = str1+char(13)+char(10);
com.Write(str);
Sleep(DelayTime);
}
com.Close();
}
#endif
#if 1//德国innolase 激光器(bException 出错时是否抛出异常)
void CLaserDeviceMgr::SetParToLaserInnolase(bool bException)
{
gLogMgr->WriteDebugLog("func ; CLaserDeviceMgr---->SetParToLaserInnolase");
//参数没有变化时不要再设置
if(IsTwoDbEqual(m_CurLaserCurr,m_Power) && IsTwoDbEqual(m_CurLaserFre,m_Fre))
{
gLogMgr->WriteDebugLog("par not change");
return;
}
SendMsgToDevice(_INNOLAS_CURRENT,true,m_Power,bException);
SendMsgToDevice(_INNOLAS_FRE,true,m_Fre,bException);//m_Fre 单位是khz
Sleep(100);//再发一次
SendMsgToDevice(_INNOLAS_CURRENT,true,m_Power,bException);
SendMsgToDevice(_INNOLAS_FRE,true,m_Fre,bException);
//记录当前值
m_CurLaserCurr = m_Power;
m_CurLaserFre = m_Fre;
//Sleep(1000);
//检查Innolase 的参数是否设置成功
//CheckInnolaseLaserPar(m_Power,m_Fre,bException);
}
//检查Innolase 的参数是否设置成功
void CLaserDeviceMgr::CheckInnolaseLaserPar(double Power,double Fre,bool bException)
{
//读取激光器实际的参数值
ReadLaserPar();
//从激光器读取
double GetPower = GetCurLaserCurr();
double GetFre = GetCurLaserFre();
CString str;
gLogMgr->WriteDebugLog("检查532 的参数是否设置成功");
str.Format("GetPower = %lf,SetPower = %lf",GetPower,Power);
gLogMgr->WriteDebugLog(str);
str.Format("GetFre = %lf,SetFre = %lf",GetFre,Fre);
gLogMgr->WriteDebugLog(str);
double eps = 0.1;
if(bException)
{
if(!IsTwoDbEqual(GetPower,Power,eps) || !IsTwoDbEqual(GetFre,Fre,eps))
{
CExceptionMsg Msg;
CString str("532 绿光激光器参数设定失败!");
Msg.SetMsg(str);
throw Msg;
}
}
}
//设置激光器的参数(bException 失败时抛出异常)
double CLaserDeviceMgr::SendMsgToDevice(INNOLAS_LASER_OP_TYPE type,bool bSet,double val,bool bException)
{
double GetVal = 0;//读取值
bool ret = true;
CString str;
//打开串口
if(m_ComMgr.IsOpen())
{
CInnolasOpPar OpPar;
OpPar.bSet = bSet;
OpPar.SetVal = val;
OpPar.Type = type;
SendMsgToDeviceExt(m_ComMgr,OpPar);
if(!bSet)//读取
{
Sleep(100);
char ReadBuf[256];
memset(ReadBuf,0,256);
m_ComMgr.ReadBuf(ReadBuf,256);
int ValIdx1 = 8;//保存值的位数
int ValIdx2 = 9;
unsigned char ValChar1 = ReadBuf[ValIdx1];
unsigned char ValChar2 = ReadBuf[ValIdx2];
unsigned int Val1 = ValChar1;
unsigned int Val2 = ValChar2;
GetVal = HexByte2Int(Val1,Val2);
GetVal /=GetLaserUnit(OpPar.Type);//转换倍率
}
}
else
{
ret = false;
//str = "激光器串口连接异常!";
}
if(bException && !ret && !gExitApp)
{
CExceptionMsg Msg;
Msg.SetMsg(str);
throw Msg;
}
return GetVal;
}
//发送消息到设备
void CLaserDeviceMgr::SendMsgToDeviceExt(CCommPortMgr&Comm,CInnolasOpPar &OpPar)
{
unsigned char buf[256];
memset(buf,0,256);
int idx = 0;
//Startbyte 2 byte ,Value is 0xFF00
buf[idx++] = 0xff;
buf[idx++] = 0x00;
//Length of command ,1 byte 字节数
buf[idx++] = GetLenOfCmd(OpPar.Type);
//Message ID 2 byte, 范围0 ~ 0x7FFF
buf[idx++] = 0x00;
buf[idx++] = GetMsgID(OpPar.Type);
//Command type,1 byte,0x57 command (set) 0x47 query (get)
buf[idx++] = GetOpType(OpPar.bSet);
//Opcode 2 byte
buf[idx++] = GetOpCode1(OpPar.Type);
buf[idx++] = GetOpCode2(OpPar.Type);
//Parameter ,0...245 byte
//if(OpPar.bSet)
{
int IntVal = OpPar.SetVal * GetLaserUnit(OpPar.Type);
char HexVal[128];
memset(HexVal,0,128);
IntTo2ByteHex(IntVal,HexVal);
if(OpPar.Type == _INNOLAS_SYS_ENABLE)
{
buf[idx++] = 0x01;//启动
}
else
{
buf[idx++] = HexVal[0];
buf[idx++] = HexVal[1];
}
}
//Checksum,1 byte,Addition mod 256 (校验和)
int Checksum = 0;
for(int k=0;k<idx;k++)
{
Checksum += (int)buf[k];
}
Checksum %= 256;
buf[idx++] = (char)Checksum;
//Endbyte ,1 byte
buf[idx++] = char(13);//<CR>
Comm.WriteBuf(buf,idx);
}
//获取指令字节数
unsigned char CLaserDeviceMgr::GetLenOfCmd(INNOLAS_LASER_OP_TYPE Type)
{
unsigned c = 0x09;
if(Type == _INNOLAS_SYS_ENABLE)
{
c = 0x08;
}
return c;
}
//获取MSG ID
unsigned char CLaserDeviceMgr::GetMsgID(INNOLAS_LASER_OP_TYPE Type)
{
int id = (int)Type;
unsigned char c = (unsigned char)id;
return c;
}
//操作类型
unsigned char CLaserDeviceMgr::GetOpType(bool bSet)
{
return (bSet)?0x57:0x47;
}
//操作code 的第1 个字节
unsigned char CLaserDeviceMgr::GetOpCode1(INNOLAS_LASER_OP_TYPE Type)
{
unsigned char c = 0x00;
return c;
}
//操作code 的第2 个字节
unsigned char CLaserDeviceMgr::GetOpCode2(INNOLAS_LASER_OP_TYPE Type)
{
unsigned char c = 0x00;
if(Type == _INNOLAS_FRE)
{
c = 0x81;
}
else if(Type == _INNOLAS_SYS_ENABLE)
{
c = 0xc1;
}
else if(Type == _INNOLAS_CURRENT)
{
c = 0x70;
}
else if(Type == _INNOLAS_SYS_STATE)
{
c = 0xa4;
}
return c;
}
//获取参数的单位倍率
double CLaserDeviceMgr::GetLaserUnit(INNOLAS_LASER_OP_TYPE Type)
{
double n = 1;
if(Type == _INNOLAS_FRE)
{
n = 10;
}
else if(Type == _INNOLAS_CURRENT)
{
n = 100;
}
return n;
}
//读取激光器实际的参数值
void CLaserDeviceMgr::ReadLaserPar()
{
if(gExitApp)
return;
m_CurLaserCurr = SendMsgToDevice(_INNOLAS_CURRENT,false,0,false);//电流
m_CurLaserFre= SendMsgToDevice(_INNOLAS_FRE,false,0,false);//频率
}
#endif
#if 1//激光器休眠控制
UINT LaserSleepThread(LPVOID pParam)
{
CLaserDeviceMgr *p = (CLaserDeviceMgr *)pParam;
p->LaserSleepTimeExt();
return 0;
}
//开始激光器休眠倒计时
void CLaserDeviceMgr::StartLaserSleepTime()
{
m_LaserSleepTime.StartBackTime(m_LaserSleepTimeMinutes*60);
m_bStopLaserSleepTime = false;//用于结束线程
AfxBeginThread(LaserSleepThread,this);
}
//结束激光器休眠倒计时
void CLaserDeviceMgr::StopLaserSleepTime()
{
m_bStopLaserSleepTime = true;//用于结束线程
}
void CLaserDeviceMgr::LaserSleepTimeExt()
{
while(1)
{
Sleep(600);
if(gExitApp)
return;
if(m_bStopLaserSleepTime)
break;
if(m_LaserSleepTime.IsBackTimeEnd())//到时间结束
{
//激光电流设置为0
if(m_DeviceType==_LASER_INNOLASE)
{
SendMsgToDevice(_INNOLAS_CURRENT,true,0,false);
}
break;
}
m_CurLeftTimes = m_LaserSleepTime.GetLeftTimes();
}
}
void CLaserDeviceMgr::OnExitApp()
{
if(m_bExitAppCloseLaser)
{
//退出软件时电流设置为0
if(m_DeviceType==_LASER_INNOLASE)
{
SendMsgToDevice(_INNOLAS_CURRENT,true,0,true);
}
if(m_DeviceType==_LASER_SPECTRA_PHYSICS)
{
TurnOffEmission();
}
}
}
#endif
#if 1//读取设备状态
UINT ReadInfoThread(LPVOID pParam)
{
CLaserDeviceMgr *p = (CLaserDeviceMgr *)pParam;
p->ReadInfoExt();
return 0;
}
void CLaserDeviceMgr::StartReadInfo()
{
AfxBeginThread(ReadInfoThread,this);
}
void CLaserDeviceMgr::ReadInfoExt()
{
while(1)
{
Sleep(200);
if(gExitApp)
return;
//激光参数
ReadLaserPar();
}
}
#endif