using Aitex.Common.Util; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Log; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; using DocumentFormat.OpenXml.Wordprocessing; using MECF.Framework.Common.Beckhoff.ModuleIO; using MECF.Framework.Common.Layout; using MECF.Framework.Common.TwinCat; using MECF.Framework.Common.Utilities; using System; using System.Collections.Generic; using System.IO; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Markup; namespace MECF.Framework.Common.Device.LinMot { public class LinMotDeviceConfigManager : Singleton { #region 常量 private string LINMOT_DEVICELIST = "LinMotDeviceList"; #endregion /// /// 模块数据更新委托字典 /// private Dictionary _linmotActionDic = new Dictionary(); /// /// 模块设备字典 /// private Dictionary _linmotDeviceDic = new Dictionary(); /// /// 模块串口设备字典 /// private Dictionary _linmotSerialDevice = new Dictionary(); /// /// 地址模块字典(key-母级模块,value-linmot device模块名称 /// private Dictionary _linmotDeviceAddressModuleDic = new Dictionary(); /// /// 配置文件对象 /// private LinMotDeviceConfigCfg linMotDeviceConfigCfg = new LinMotDeviceConfigCfg(); /// /// 初始化 /// public void Initialize() { bool isSimulate = SC.GetValue("System.IsSimulatorMode"); string xmlPath = ""; if (isSimulate) { xmlPath = PathManager.GetCfgDir() + "Devices\\LinmotCfg-Simulator.xml"; } else { xmlPath = PathManager.GetCfgDir() + "Devices\\LinmotCfg.xml"; } try { linMotDeviceConfigCfg = CustomXmlSerializer.Deserialize(new FileInfo(xmlPath)); if (linMotDeviceConfigCfg != null) { foreach (LinmotDeviceConfig config in linMotDeviceConfigCfg.LinmotDeviceConfigs) { DATA.Subscribe($"{config.Name}.{LINMOT_DEVICELIST}", () => config, SubscriptionAttribute.FLAG.IgnoreSaveDB); LinMotSerialDevice linMotSerialDevice = new LinMotSerialDevice(config.Name, config.Port, config.BaudRate, (StopBits)config.StopBit, config.Data, SerialPortUtil.GetParity(config.Parity), true); linMotSerialDevice.Initialize(); InitialLinMotDevice(config, linMotSerialDevice); } LinMotErrorCodeManager.Instance.Initialize(); } } catch { LOG.WriteLog(eEvent.ERR_LINMOT, "Linmot", "Load linmot xml failed"); } } /// /// 初始化LinMot设备 /// /// private void InitialLinMotDevice(LinmotDeviceConfig config,LinMotSerialDevice linMotSerialDevice) { if(config.LinmotDevices!=null) { foreach(LinMotDevice device in config.LinmotDevices) { _linmotSerialDevice[device.Name] = linMotSerialDevice; _linmotDeviceDic[device.Name] = device; _linmotDeviceAddressModuleDic[$"{config.Name}_{device.Address}"] = device.Name; _linmotActionDic[device.Name] = null; } } } /// /// 获取所有LinMot设备集合 /// /// public List GetLinMotDeviceList() { return _linmotDeviceDic.Keys.ToList(); } /// /// 初始化设备 /// /// public void InitialDevice(string name) { if(_linmotSerialDevice.ContainsKey(name)) { _linmotSerialDevice[name].Start(); } } /// /// 查询连接状态 /// /// /// public bool GetDeviceConnect(string name) { if(_linmotDeviceDic.ContainsKey(name)) { return _linmotSerialDevice[name].Connected; } return false; } /// /// 发送指令 /// /// /// /// public bool SendOperation(string name,byte id,LinMotOperation linMotOperation) { if(_linmotSerialDevice.ContainsKey(name)) { if (linMotOperation != LinMotOperation.ReadStatus) { LOG.WriteBackgroundLog(eEvent.INFO_LINMOT, name, $"{name} execute {linMotOperation}"); } return _linmotSerialDevice[name].SendOperation(id, linMotOperation); } else { LOG.WriteBackgroundLog(eEvent.ERR_LINMOT, name, $"dictionary does not contain {name}"); } return false; } /// /// 发送Curve指令 /// /// /// /// /// /// public bool SendCurveOperation(string name, byte id,ushort curveId, ushort timeScale) { if(_linmotSerialDevice.ContainsKey(name)) { return _linmotSerialDevice[name].SendCurveOperation(id, curveId, timeScale); } return false; } /// /// 发送CommandTable指令 /// /// /// /// /// /// public bool SendCommandTable(string name,byte id,short entryId) { if (_linmotSerialDevice.ContainsKey(name)) { return _linmotSerialDevice[name].SendCommandTableOpertaion(id, entryId); } else { LOG.WriteLog(eEvent.ERR_LINMOT, name, $"{name} linmot is not in dictionary"); } return false; } /// /// 更新RAM变量 /// /// /// /// /// /// /// public bool SendWriteRamIntValue(string name, byte id, byte lowByte,byte highByte, int intValue) { if (_linmotSerialDevice.ContainsKey(name)) { return _linmotSerialDevice[name].SendWriteRamIntValue(id,lowByte,highByte, intValue); } return false; } /// /// 发送GAI GotoPosition /// /// /// /// /// /// /// /// /// public bool SendVAIGotoPosition(string name,byte id,int position,int velocilty,int accel,int decel) { if(_linmotSerialDevice.ContainsKey(name)) { return _linmotSerialDevice[name].SendVAIGoToPositionOpertaion(id,position, velocilty,accel,decel); } return false; } /// /// 发送GAI GotoPosition After Actual Command /// /// /// /// /// /// /// /// /// public bool SendVAIGotoPositionAfterActualCommand(string name, byte id, int position, int velocilty, int accel, int decel) { if (_linmotSerialDevice.ContainsKey(name)) { return _linmotSerialDevice[name].SendVAIGoToPositionAfterAutalCommandOpertaion(id, position, velocilty, accel, decel); } return false; } /// /// 获取LinMot设备 /// /// /// public LinMotDevice GetLinMotDevice(string name) { if(_linmotDeviceDic.ContainsKey(name)) { return _linmotDeviceDic[name]; } return null; } /// /// 获取Id /// /// /// public int GetAddress(string name) { if(_linmotDeviceDic.ContainsKey(name)) { if(int.TryParse(_linmotDeviceDic[name].Address,out var address)) { return address; } } return -1; } /// /// 根据地址获取模块名称 /// /// /// /// public string GetModuleNameByAddress(string name,byte id) { if(_linmotDeviceAddressModuleDic.ContainsKey($"{name}_{id}")) { return _linmotDeviceAddressModuleDic[$"{name}_{id}"]; } return ""; } /// /// 注册变量数值发生变化回调 /// /// /// /// public void SubscribeModuleVariable(string moduleName, LinMotDelegate.OnUpdateModuleVariableValue onUpdateModuleVariableValue) { _linmotActionDic[moduleName] = onUpdateModuleVariableValue; } /// /// 更新数据 /// /// /// /// /// public void UpdateModuleData(string moduleName,short statusWord,short errorCode,int position) { if(_linmotActionDic.ContainsKey(moduleName)) { _linmotActionDic[moduleName].Invoke(statusWord, errorCode, position); } } /// /// 更新linmot速度相关的参数 /// /// /// public void UpdateSpeedData(string deviceName, string direction, double maxSpeed, int maxAcceleration,int maxDeceleration) { if (linMotDeviceConfigCfg != null) { foreach (LinmotDeviceConfig config in linMotDeviceConfigCfg.LinmotDeviceConfigs) { if (deviceName.Contains(config.Name)) { foreach(var item in config.LinmotDevices) { if(item.Name == deviceName) { if ("up".Equals(direction)) { item.LinMotDeviceData.UpMaxSpeed = maxSpeed * 1000; item.LinMotDeviceData.UpMaxAcceleration = maxAcceleration * 1000; item.LinMotDeviceData.UpMaxDeceleration = maxDeceleration * 1000; } else { item.LinMotDeviceData.DownMaxSpeed = maxSpeed * 1000; item.LinMotDeviceData.DownMaxAcceleration = maxAcceleration * 1000; item.LinMotDeviceData.DownMaxDeceleration = maxDeceleration * 1000; } } } } } } bool isSimulate = SC.GetValue("System.IsSimulatorMode"); string xmlPath = ""; if (isSimulate) { xmlPath = PathManager.GetCfgDir() + "Devices\\LinmotCfg-Simulator.xml"; } else { xmlPath = PathManager.GetCfgDir() + "Devices\\LinmotCfg.xml"; } try { CustomXmlSerializer.Serialize(linMotDeviceConfigCfg, xmlPath); } catch { LOG.WriteLog(eEvent.ERR_LINMOT, "Linmot", "Save linmot xml failed"); } } } }