using Aitex.Core.RT.Log; using Aitex.Core.Util; using DocumentFormat.OpenXml.InkML; using MECF.Framework.Common.CommonData.PowerSupplier; using MECF.Framework.Common.Communications; using MECF.Framework.Common.Net; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MECF.Framework.Common.Device.PowerSupplier { public class PowerSupplierModbusDevice : JetMessageTcpClient { #region 常量 private const short CURRENT_SETTING_ADDRESS = 0x0101; private const short OUTPUT_CONTROL_ADDRESS = 0x0110; private const short STEP_PERIOD_ADDRESS = 0x1400; private const short STEP_PERIOD_START_ADDRESS = 0x1640; private const short VOLTAGE_OUTPUT_ADDRESS = 0x0201; private const short POWER_CONTROL_ADDRESS = 0x0113; private const short POWER_RUN_MODEL_ADDRESS = 0x0111; /// /// 电源状态(00-cv输出,01-cc输出) /// private const short POWER_STATUS_ADDRESS = 0x0200; private const string SET_POINT = "SetPoint"; private const string CURRENT = "Current"; private const string VOLTAGE = "Voltage"; private const string ENABLED = "Enabled"; private const string POWER_STATUS = "PowerStatus"; private const string POWER_CONTROL = "PowerControl"; private const string POWER_RUN_MODEL = "PowerRunModel"; /// /// 步阶数据数量 /// private const int STEP_PERIOD_LENGTH = 6; #endregion #region 内部变量 private string _name; private string _lastErrorMsg = ""; #endregion /// /// 构造函数 /// /// /// public PowerSupplierModbusDevice(string name,string ip, int port) : base(ip, port) { ReceiveTimeout = 1000; SendTimeout = 1000; ReconnectInterval = 3000; ConnectTimeout = 1000; _name = name; } /// /// 连接 /// /// public void Start() { Connect(); } /// /// 设置通道输出开关控制 /// /// /// /// public bool SetChannelOutputSwitchControl(byte channel, bool enabled) { PowerSupplierCommand command = new PowerSupplierCommand(); command.Channel = channel; command.CommandCode = 0x06; command.Address = (ushort)(OUTPUT_CONTROL_ADDRESS); command.Datas = new ushort[] { enabled ? (ushort)01 : (ushort)00 }; return SetOperation(command); } /// /// 设置电源控制 /// /// /// /// public bool SetChannelPowerControl(byte channel,byte remoteControl) { PowerSupplierCommand command = new PowerSupplierCommand(); command.Channel = channel; command.CommandCode = 0x06; command.Address = (ushort)(POWER_CONTROL_ADDRESS); command.Datas = new ushort[] { remoteControl }; return SetOperation(command); } /// /// 设置电源运行模式 /// /// /// /// public bool SetChannelPowerRunmodelControl(byte channel,byte model) { PowerSupplierCommand command = new PowerSupplierCommand(); command.Channel = channel; command.CommandCode = 0x06; command.Address = (ushort)(POWER_RUN_MODEL_ADDRESS); command.Datas = new ushort[] { model }; return SetOperation(command); } /// /// 设置步阶数据 /// /// /// public bool SetStepPeriod(byte channel,List stepDatas,int scale) { PowerSupplierCommand command = new PowerSupplierCommand(); command.Channel = channel; command.CommandCode = 0x10; command.Address = (ushort)STEP_PERIOD_ADDRESS; command.RegisterCount =(ushort)(STEP_PERIOD_LENGTH * stepDatas.Count); command.Datas = new ushort[STEP_PERIOD_LENGTH * stepDatas.Count]; for(int i = 0;i /// 启动步阶 /// /// /// /// /// public bool StartStepPeriod(byte channel,ushort startStep,ushort endStep,ushort cycle) { PowerSupplierCommand command = new PowerSupplierCommand(); command.Channel = channel; command.CommandCode = 0x10; command.Address = (ushort)STEP_PERIOD_START_ADDRESS; command.RegisterCount = 3; command.Datas = new ushort[3] { startStep,endStep,cycle }; return SetOperation(command); } /// /// 设置电流 /// /// /// /// public bool SetCurrentValue(byte channel,ushort currentValue) { PowerSupplierCommand command = new PowerSupplierCommand(); command.Channel = channel; command.CommandCode = 0x06; command.Address = (ushort)(CURRENT_SETTING_ADDRESS); command.Datas = new ushort[] { currentValue }; return SetOperation(command); } /// /// 设置电源状态 /// /// /// /// public bool SetChannelPowerStatus(byte channel, byte powerStatus) { PowerSupplierCommand command = new PowerSupplierCommand(); command.Channel = channel; command.CommandCode = 0x06; command.Address = (ushort)(POWER_STATUS_ADDRESS); command.Datas = new ushort[] { powerStatus }; return SetOperation(command); } /// /// 获取通道输出开关控制 /// /// /// public void GetChannelOutput(byte channel) { PowerSupplierCommand applyCommand = new PowerSupplierCommand(); applyCommand.Channel = channel; applyCommand.CommandCode = 0x03; applyCommand.Address = (ushort)(OUTPUT_CONTROL_ADDRESS); applyCommand.RegisterCount = 1; applyCommand.Variables.Add(ENABLED,(0,1)); ApplyDataOperation(applyCommand); } /// /// 获取通道电源控制 /// /// /// public void GetChannelPowerControl(byte channel) { PowerSupplierCommand applyCommand = new PowerSupplierCommand(); applyCommand.Channel = channel; applyCommand.CommandCode = 0x03; applyCommand.Address = (ushort)(POWER_CONTROL_ADDRESS); applyCommand.RegisterCount = 1; applyCommand.Variables.Add(POWER_CONTROL, (0, 1)); ApplyDataOperation(applyCommand); } /// /// 获取通道电流设置数值 /// /// /// public void GetChannelCurrentSetting(byte channel) { PowerSupplierCommand applyCommand = new PowerSupplierCommand(); applyCommand.Channel = channel; applyCommand.CommandCode = 0x03; applyCommand.Address = (ushort)(CURRENT_SETTING_ADDRESS); applyCommand.RegisterCount = 1; applyCommand.Variables.Add(SET_POINT,(0,1)); ApplyDataOperation(applyCommand); } /// /// 获取电源状态设置数值 /// /// /// public void GetChannelPowerStatus(byte channel) { PowerSupplierCommand applyCommand = new PowerSupplierCommand(); applyCommand.Channel = channel; applyCommand.CommandCode = 0x03; applyCommand.Address = (ushort)(POWER_STATUS_ADDRESS); applyCommand.RegisterCount = 1; applyCommand.Variables.Add(POWER_STATUS, (0,1)); ApplyDataOperation(applyCommand); } /// /// 获取电源运行模式 /// /// /// public void GetChannelPowerRunModel(byte channel) { PowerSupplierCommand applyCommand = new PowerSupplierCommand(); applyCommand.Channel = channel; applyCommand.CommandCode = 0x03; applyCommand.Address = (ushort)(POWER_RUN_MODEL_ADDRESS); applyCommand.RegisterCount = 1; applyCommand.Variables.Add(POWER_RUN_MODEL, (0, 1)); ApplyDataOperation(applyCommand); } /// /// 申请电压和电流数值 /// /// /// public void GetChannelVoltageAndCurrent(byte channel) { PowerSupplierCommand applyCommand = new PowerSupplierCommand(); applyCommand.Channel = channel; applyCommand.CommandCode = 0x03; applyCommand.Address = (ushort)(VOLTAGE_OUTPUT_ADDRESS); applyCommand.RegisterCount = 4; applyCommand.Variables.Add(VOLTAGE,(0,2)); applyCommand.Variables.Add(CURRENT,(2,2)); ApplyDataOperation(applyCommand); } /// /// 设置操作 /// /// /// private bool SetOperation(PowerSupplierCommand command) { if (Connected) { NetResult netResult = SetData(command); if (!netResult.IsSuccess) { WriteErrMsg($"write write {command.Address.ToString("X2")} value {command.Datas[0]} failed,{netResult.Message}"); return false; } return true; } else { NetResult netResult = Connect(); if(netResult.IsSuccess) { netResult = SetData(command); if (!netResult.IsSuccess) { WriteErrMsg($"write {command.Address.ToString("X2")} value {command.Datas[0]} failed,{netResult.Message}"); return false; } return true; } else { WriteErrMsg("connect failed"); return false; } } } /// /// 申请数据操作 /// /// private void ApplyDataOperation(PowerSupplierCommand command) { if (!Connected) { NetResult connectResult = Connect(); if (!connectResult.IsSuccess) { WriteErrMsg("connect failed"); return; } } NetResult netResult = ApplyData(command); if (!netResult.IsSuccess) { WriteErrMsg($"apply {command.Address.ToString("X2")} failed,{netResult.Message}"); return; } if(netResult.Data.Datas!=null) { Dictionary dictionary = command.Variables; List keys = dictionary.Keys.ToList(); foreach(string item in keys) { var result = dictionary[item]; if(item==ENABLED) { PowerSupplierDeviceConfigManager.Instance.UpdateModuleVariable(_name, command.Channel, ENABLED, netResult.Data.Datas[result.Item1] == 0x01); } else { if(result.Item2==1) { PowerSupplierDeviceConfigManager.Instance.UpdateModuleVariable(_name, command.Channel, item, netResult.Data.Datas[result.Item1]); } else if(result.Item2==2) { int value = netResult.Data.Datas[result.Item1] * 0xFFFF + netResult.Data.Datas[result.Item1+1]; PowerSupplierDeviceConfigManager.Instance.UpdateModuleVariable(_name, command.Channel, item, value); } } } } } /// /// 写错误日志 /// /// private void WriteErrMsg(string msg) { if (msg!=_lastErrorMsg) { _lastErrorMsg = msg; LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, _name, msg); } } } }