123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- using System;
- using System.Collections.Generic;
- using System.IO.Ports;
- using Aitex.Core.Common.DeviceData;
- using Aitex.Core.RT.DataCenter;
- using Aitex.Core.RT.Event;
- using Aitex.Core.RT.Log;
- using Aitex.Core.RT.OperationCenter;
- using Aitex.Core.RT.SCCore;
- using Aitex.Core.Util;
- using MECF.Framework.Common.Communications;
- using MECF.Framework.Common.Device.Bases;
- namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.DCPowers.AE
- {
- public class DxkdpDcPower : RfPowerBase
- {
- public override bool IsPowerOn
- {
- get { return _isOn; }
- }
- public override bool IsSetPowerOn
- {
- get { return _setOn; }
- }
- public override bool IsError
- {
- get { return _isError; }
- }
- public override EnumRfPowerRegulationMode RegulationMode
- {
- get { return _regulationMode; }
- }
- public override float ForwardPower
- {
- get { return _forwardPower; }
- }
- public override float ReflectPower
- {
- get { return _reflectPower; }
- }
- public override float PowerSetPoint
- {
- get { return _powerSetPoint; }
- }
- public float Voltage
- {
- get { return _voltage; }
- }
- public float Current
- {
- get { return _current; }
- }
- public override float Frequency { get; set; }
- public override float PulsingFrequency { get; set; }
- public override float PulsingDutyCycle { get; set; }
- public override AITRfPowerData DeviceData
- {
- get
- {
- AITRfPowerData data = new AITRfPowerData()
- {
- Module = Module,
- DeviceName = Name,
- DeviceSchematicId = DeviceID,
- DisplayName = Display,
- UnitPower = _unit,
- ForwardPower = ForwardPower,
- ReflectPower = ReflectPower,
- PowerSetPoint = PowerSetPoint,
- RegulationMode = RegulationMode,
- Voltage = Voltage,
- Current = Current,
- CurrentSetPoint = _setCurrent,
- VoltageSetPoint = _setVoltage,
- ScaleCurrent = _scaleCurrent,
- UnitCurrent = "A",
- UnitVoltage = "V",
- IsRfOn = IsPowerOn,
- IsRfAlarm = IsError,
- Frequency = Frequency,
- PulsingFrequency = PulsingFrequency,
- PulsingDutyCycle = PulsingDutyCycle,
- };
- return data;
- }
- }
- public DxkdpDcPowerConnection Connection
- {
- get { return _connection; }
- }
- private DxkdpDcPowerConnection _connection;
- private byte _deviceAddress;
- private float _powerSetPoint;
- private float _setVoltage;
- private float _setCurrent;
- private float _reflectPower;
- private float _scaleCurrent;
- private bool _isOn;
- private bool _isError;
- private string _errorCode;
- private float _forwardPower;
- private string _unit;
- private float _voltage;
- private float _current;
- private int _queryInterval;
- private bool _setOn;
- private int _vmss = 2;
- private int _cmss = 3;
- private float _sysVoltage;
- private float _sysCurrent;
- private int _polarity;
- private bool _remoteMode;
- private EnumRfPowerRegulationMode _regulationMode;
- private EnumRfPowerCommunicationMode _commMode;
- public Func<bool, bool> FuncCheckInterLock;
- public Func<bool, bool> FuncForceAction;
- private R_TRIG _trigForceAction = new R_TRIG();
- private RD_TRIG _trigRfOnOff = new RD_TRIG();
- private R_TRIG _trigError = new R_TRIG();
- private R_TRIG _trigWarningMessage = new R_TRIG();
- private R_TRIG _trigCommunicationError = new R_TRIG();
- private R_TRIG _trigRetryConnect = new R_TRIG();
- private R_TRIG _trigConnect = new R_TRIG();
- private PeriodicJob _thread;
- private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
- private object _locker = new object();
- private bool _enableLog = true;
- private bool _isHaloInstalled;
- public DxkdpDcPower(string module, string name) : base(module, name)
- {
- }
- string portName;
- public override bool Initialize()
- {
- base.Initialize();
- portName = SC.GetStringValue($"{Module}.{Name}.Address");
- //int bautRate = SC.GetValue<int>($"{ScBasePath}.{Name}.BaudRate");
- //int dataBits = SC.GetValue<int>($"{ScBasePath}.{Name}.DataBits");
- //Enum.TryParse(SC.GetStringValue($"{ScBasePath}.{Name}.Parity"), out Parity parity);
- //Enum.TryParse(SC.GetStringValue($"{ScBasePath}.{Name}.StopBits"), out StopBits stopBits);
- _unit = SC.GetStringValue($"{Module}.{Name}.Unit");
- _deviceAddress = (byte)SC.GetValue<int>($"{Module}.{Name}.DeviceAddress");
- _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
- _scaleCurrent = (float)SC.GetValue<double>($"{Module}.{Name}.ScaleCurrent");
- _queryInterval = SC.GetValue<int>($"{Module}.{Name}.QueryInterval");
- _connection = new DxkdpDcPowerConnection(portName);
- _connection.IsEnableHandlerRetry = true;
- _connection.EnableLog(_enableLog);
-
- if (_connection.Connect())
- {
- EV.PostInfoLog(Module, $"{Module}.{Name} connected");
- }
- _thread = new PeriodicJob(_queryInterval, OnTimer, $"{Module}.{Name} MonitorHandler", true);
- DATA.Subscribe($"{Module}.{Name}.Current", () => Current);
- DATA.Subscribe($"{Module}.{Name}.Voltage", () => Voltage);
- OP.Subscribe($"{Module}.{Name}.SetVoltage", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- if (!SetVoltage(out reason, time, value))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set Voltage, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set Voltage to {args[0]}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.SetCurrent", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- SetCurrent(value);
- EV.PostInfoLog(Module, $"{Module}.{Name} set Current to {value}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.SetCurrentForRecipe", (out string reason, int time, object[] args) =>
- {
- float value = Convert.ToSingle(args[0]);
- bool isOn = value > 0;
- reason = string.Empty;
- SetCurrent(value);
- if (isOn ^ IsPowerOn)
- {
- SetPowerOnOff(isOn, out _);
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set Current to {value}");
- return true;
- });
- return true;
- }
- private bool OnTimer()
- {
- try
- {
- _connection.MonitorTimeout();
- _trigConnect.CLK = _connection.IsConnected;
- if (_trigConnect.Q)
- {
- _lstHandler.AddLast(new DxkdpRfPowerSetModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
- }
- if (!_connection.IsConnected)
- {
- lock (_locker)
- {
- _lstHandler.Clear();
- }
- _trigRetryConnect.CLK = !_connection.IsConnected;
- if (_trigRetryConnect.Q)
- {
- _connection.SetPortAddress(SC.GetStringValue($"{Module}.{Name}.Address"));
- if (_connection.Connect() && _connection.IsConnected)
- {
- _connection.ForceClear();
- EV.PostInfoLog(Module, $"Reconnect with {_connection.Address}, {Module}.{Name}");
- }
- else
- {
- EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
- }
- }
- return true;
- }
- // 达到一定条件,强制关闭电源
- if (FuncForceAction != null)
- {
- _trigForceAction.CLK = FuncForceAction(IsPowerOn);
- if (_trigForceAction.Q)
- {
- SetCurrent(0);
- SetPowerOnOff(false, out string reason);
- //EV.PostAlarmLog(Module, $"Force set {Name} off for interlock");
- }
- }
- HandlerBase handler = null;
- if (!_connection.IsBusy)
- {
- lock (_locker)
- {
- if (_lstHandler.Count == 0)
- {
- if (!_remoteMode)
- {
- _lstHandler.AddLast(new DxkdpRfPowerSetModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
- }
- if (_setOn || _isOn)
- {
- //_lstHandler.AddLast(new DxkdpRfPowerQuerySysInformationHandler(this, _deviceAddress));
- _lstHandler.AddLast(new DxkdpRfPowerQueryStateHandler(this, _deviceAddress));
- _lstHandler.AddLast(new DxkdpRfPowerQueryPracticalHandler(this, _deviceAddress));
- }
- }
- if (_lstHandler.Count > 0)
- {
- handler = _lstHandler.First.Value;
- _lstHandler.RemoveFirst();
- }
- }
- if (handler != null)
- {
- _connection.Execute(handler);
- }
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- return true;
- }
- public override void Monitor()
- {
- try
- {
- _connection.EnableLog(_enableLog);
- _trigRfOnOff.CLK = _isOn;
- if (_trigRfOnOff.R)
- {
- EV.PostInfoLog(Module, $"{Module}.{Name} is on");
- }
- if (_trigRfOnOff.T)
- {
- EV.PostInfoLog(Module, $"{Module}.{Name} is off");
- }
- _trigError.CLK = IsError;
- if (_trigError.Q)
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} is error, error code {_errorCode:D3}");
- }
- _trigCommunicationError.CLK = _connection.IsCommunicationError;
- if (_trigCommunicationError.Q)
- {
- EV.PostWarningLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
- }
- //MonitorRampingPower();
- base.Monitor();
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- }
- public override void Reset()
- {
- _trigError.RST = true;
- _trigWarningMessage.RST = true;
- _connection.SetCommunicationError(false, "");
- _trigCommunicationError.RST = true;
- _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
- _trigRetryConnect.RST = true;
- _trigForceAction.RST = true;
- base.Reset();
- }
- public void SetCurrent(float current)
- {
- _setCurrent = current;
- lock (_locker)
- {
- UInt16 setCurrent = Convert.ToUInt16(current * Math.Pow(10, _cmss));
- if (current > 0)
- _lstHandler.AddLast(new DxkdpRfPowerSetElectricityHandler(this, _deviceAddress, setCurrent));
- else
- _lstHandler.AddFirst(new DxkdpRfPowerSetElectricityHandler(this, _deviceAddress, setCurrent));
- }
- }
- public bool SetVoltage(out string reason, int time, float voltage)
- {
- reason = string.Empty;
- lock (_locker)
- {
- UInt16 setVoltage = Convert.ToUInt16(voltage * Math.Pow(10, _vmss));
- _lstHandler.AddLast(new DxkdpRfPowerSetVoltageHandler(this, _deviceAddress, setVoltage));
- }
- return true;
- }
- public override bool SetPowerOnOff(bool isOn, out string reason)
- {
- reason = "";
- if (FuncCheckInterLock != null)
- {
- if (!FuncCheckInterLock(isOn))
- {
- return false;
- }
- }
- _setOn = isOn;
- lock (_locker)
- {
- if (isOn)
- _lstHandler.AddLast(new DxkdpRfPowerSwitchOnOffHandler(this, _deviceAddress, isOn));
- else
- _lstHandler.AddFirst(new DxkdpRfPowerSwitchOnOffHandler(this, _deviceAddress, isOn));
- }
- return true;
- }
- internal void NoteError(string reason)
- {
- _trigWarningMessage.CLK = true;
- if (_trigWarningMessage.Q)
- {
- EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
- }
- }
- internal void NoteStatus(byte data)
- {
- _isOn = data == 0x01;
- }
- internal void NoteMinVStepSize(int vmss)
- {
- _vmss = vmss;
- }
- internal void NoteMinCStepSize(int cmss)
- {
- _cmss = cmss;
- }
- internal void NoteSysCurrent(int sysCurrent)
- {
- _sysCurrent = (float)sysCurrent /(float)Math.Pow(10, _cmss);
- }
- internal void NoteSysVoltage(int sysVoltage)
- {
- _sysVoltage = (float)sysVoltage / (float)Math.Pow(10, _vmss);
- }
- internal void NoteVoltage(int voltage)
- {
- _voltage = (float)voltage / (float)Math.Pow(10, _vmss);
- }
- internal void NoteCurrent(int current)
- {
- _current = (float)current / (float)Math.Pow(10, _cmss);
- }
- internal void NoteMode(bool mode)
- {
- _remoteMode = mode;
- }
- }
- }
|