123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649 |
- 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.RFs.Mkss
- {
- public class MksRfPower : RfPowerBase
- {
- public override bool IsError { get; set; }
- public override bool IsPowerOn
- {
- get { return _isOn; }
- }
- //public override EnumRfPowerRegulationMode RegulationMode
- //{
- // get { return _regulationMode; }
- //}
- public bool Connect()
- {
- return _connection.Connect();
- }
- public bool Disconnect()
- {
- return _connection.Disconnect();
- }
- public override bool IsConnected => Connection != null && Connection.IsConnected && !_connection.IsCommunicationError;
- public override float ForwardPower
- {
- get { return _forwardPower; }
- }
- public override float ReflectPower
- {
- get { return _reflectPower; }
- }
- public override float Frequency { get; set; }
- public override float PulsingFrequency { get; set; }
- public override float PulsingDutyCycle { get; set; }
- public string ControlSource { get; set; }
- public string RegulationSource { get; set; }
- public string SetpointSource { get; set; }
- public override AITRfPowerData DeviceData
- {
- get
- {
- AITRfPowerData data = new AITRfPowerData()
- {
- DeviceName = Name,
- Module = Module,
- DeviceSchematicId = DeviceID,
- DisplayName = Display,
- UnitPower = "W",
- ForwardPower = _originalPowerSetPoint > 0 ? CalibrationData(ForwardPower, false) : 0,
- ReflectPower = _originalPowerSetPoint > 0 ? ReflectPower : 0,
- PowerSetPoint = _originalPowerSetPoint,
- RegulationMode = RegulationMode,
- ScalePower = PowerRange,
- IsRfOn = IsPowerOn,
- IsRfAlarm = IsError,
- IsInterlockOk = true,
- Frequency = Frequency,
- PulsingFrequency = PulsingFrequency,
- PulsingDutyCycle = PulsingDutyCycle,
- };
- return data;
- }
- }
- public MksRfPowerConnection Connection
- {
- get { return _connection; }
- }
- private MksRfPowerConnection _connection;
- private byte _deviceAddress;
- private float _powerSetPoint;
- private float _reflectPower;
- private bool _isOn;
- private float _forwardPower;
- private float _voltage;
- 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 PeriodicJob _thread;
- //private bool _isInterlockNotOK;
- private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
- private object _locker = new object();
- private bool _enableLog = true;
- private bool _isHaloInstalled;
- private string _scRoot;
- public MksRfPower(string module, string name, string scRoot) : base(module, name)
- {
- _scRoot = scRoot;
- }
- public bool Initialize(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
- {
- base.Initialize();
- _enableLog = true;
- _connection = new MksRfPowerConnection(portName, baudRate, dataBits, parity, stopBits);
- _connection.EnableLog(_enableLog);
- if (_connection.Connect())
- {
- EV.PostInfoLog(Module, $"{Module}.{Name} connected");
- }
- _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
- return true;
- }
- public override bool Initialize()
- {
- base.Initialize();
- string portName = SC.GetStringValue($"{_scRoot}.Address");
- int bautRate = SC.GetValue<int>($"{_scRoot}.BaudRate");
- int dataBits = SC.GetValue<int>($"{_scRoot}.DataBits");
- Enum.TryParse(SC.GetStringValue($"{_scRoot}.Parity"), out Parity parity);
- Enum.TryParse(SC.GetStringValue($"{_scRoot}.StopBits"), out StopBits stopBits);
- _deviceAddress = (byte)SC.GetValue<int>($"{_scRoot}.Address");
- _enableLog = SC.GetValue<bool>($"{_scRoot}.EnableLogMessage");
- _scPowerScale = SC.GetConfigItem($"{_scRoot}.PowerScale");
- _connection = new MksRfPowerConnection(portName, bautRate, dataBits, parity, stopBits);
- _connection.EnableLog(_enableLog);
- if (_connection.Connect())
- {
- EV.PostInfoLog(Module, $"{Module}.{Name} connected");
- }
- _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
- lock (_locker)
- {
- _lstHandler.AddLast(new MksRfPowerSetEchoOffHandler(this, false));
- }
- OP.Subscribe($"{Module}.{Name}.WaveModulation", (out string reason, int time, object[] args) =>
- {
- reason = "";
- //if (!Enum.TryParse((string)args[0], out EnumRpsControlMode mode))
- //{
- // EV.PostAlarmLog(Module, $"{Module}.{Name} Can not mode, {args[0]} is not a valid mode value");
- // return false;
- //}
- if (!PerformWaveModulation(out reason, time, ""))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set mode to {args[0]}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.PowerMode", (out string reason, int time, object[] args) =>
- {
- reason = "";
- string mode = (string)args[0];
- if (!PerformPowerMode(out reason, time, mode))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set mode to {mode}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.SetHighPower", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- if (!PerformSetHighPower(out reason, time, value))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set high power to {value}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.SetLowPower", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- if (!PerformSetLowPower(out reason, time, value))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set low power to {value}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.PulseFreq", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- if (!PerformPulseFreq(out reason, time, value))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set pulse frequency to {value}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.PulseDutyCycle", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- if (!PerformPulseDutyCycle(out reason, time, value))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set pulse duty cycle to {value}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.RampMode", (out string reason, int time, object[] args) =>
- {
- reason = "";
- if (!PerformRampMode(out reason, time, ""))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set ramp mode to ");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.RampUp", (out string reason, int time, object[] args) =>
- {
- reason = "";
- if (!PerformRampUp(out reason, time, ""))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set ramp up");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.RampDown", (out string reason, int time, object[] args) =>
- {
- reason = "";
- if (!PerformRampDown(out reason, time, ""))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set ramp down");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.WarningPower", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- if (!PerformWarningPower(out reason, time, value))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set warning power to {value}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.AlarmPower", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- if (!PerformAlarmPower(out reason, time, value))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set alarm power to {value}");
- return true;
- });
- DATA.Subscribe($"{Module}.{Name}.IsConnected", () => IsConnected);
- DATA.Subscribe($"{Module}.{Name}.Address", () => SC.GetStringValue($"{_scRoot}.Address"));
- OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) =>
- {
- Disconnect();
- Reset();
- Connect();
- return true;
- });
- return true;
- }
- private bool PerformWaveModulation(out string reason, int time, string mode)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformAlarmPower(out string reason, int time, float value)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformWarningPower(out string reason, int time, float value)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformPowerMode(out string reason, int time, string mode)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformSetHighPower(out string reason, int time, float value)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformSetLowPower(out string reason, int time, float value)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformPulseFreq(out string reason, int time, float value)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformPulseDutyCycle(out string reason, int time, float value)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformRampMode(out string reason, int time, string v)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformRampUp(out string reason, int time, string mode)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformRampDown(out string reason, int time, string mode)
- {
- reason = string.Empty;
- return true;
- }
- private bool OnTimer()
- {
- try
- {
- _connection.MonitorTimeout();
- if (!_connection.IsConnected || _connection.IsCommunicationError)
- {
- lock (_locker)
- {
- _lstHandler.Clear();
- }
- _trigRetryConnect.CLK = !_connection.IsConnected;
- if (_trigRetryConnect.Q)
- {
- _connection.SetPortAddress(SC.GetStringValue($"{ScBasePath}.{Name}.Address"));
- if (!_connection.Connect())
- {
- EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
- }
- else
- {
- }
- }
- return true;
- }
- HandlerBase handler = null;
- if (!_connection.IsBusy)
- {
- lock (_locker)
- {
- if (_lstHandler.Count == 0)
- {
- _lstHandler.AddLast(new MksRfPowerQueryStatusHandler(this));
- }
- 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.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
- }
- 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>($"{_scRoot}.EnableLogMessage");
- _trigRetryConnect.RST = true;
- base.Reset();
- }
- public override void CheckTolerance()
- {
- if (!EnableAlarm || PowerSetPoint == 0 || (RecipeIgnoreTime > 0 && _recipeIgnoreTimer.GetElapseTime() < RecipeIgnoreTime * 1000))
- return;
- _toleranceAlarmChecker.Monitor(ForwardPower, (PowerSetPoint * (1 - AlarmRange / 100)), (PowerSetPoint * (1 + AlarmRange / 100)), AlarmTime);
- _toleranceWarningChecker.Monitor(ForwardPower, (PowerSetPoint * (1 - WarningRange / 100)), (PowerSetPoint * (1 + WarningRange / 100)), WarningTime);
- if (_PrThreshold > 0)
- _prThresholdChecker.Monitor(ReflectPower, 0, _PrThreshold, PrThresholdMonitorTime);
- //if (_toleranceAlarmChecker.Trig)
- //{
- // EV.PostMessage(Module, EventEnum.ToleranceAlarm, Module, Display,
- // String.Format("Out of range in {0} seconds", AlarmTime.ToString("0")));
- //}
- //else if (_toleranceWarningChecker.Trig)
- //{
- // EV.PostMessage(Module, EventEnum.ToleranceWarning, Module, Display,
- // String.Format("Out of range in {0} seconds", WarningTime.ToString("0")));
- //}
- }
- public override void SetPower(float power)
- {
- PowerSetPoint = power;
- lock (_locker)
- {
- _lstHandler.AddLast(new MksRfPowerSetPowerHandler(this, (int)PowerSetPoint));
- }
- }
- public override bool SetPowerOnOff(bool isOn, out string reason)
- {
- _isOn = isOn;//有指令能读状态之后,根据读回的状态显示
- reason = "";
- lock (_locker)
- {
- _lstHandler.AddLast(new MksRfPowerSwitchOnOffHandler(this, isOn));
- }
- return true;
- }
- public override void SetRegulationMode(EnumRfPowerRegulationMode mode)
- {
- lock (_locker)
- {
- _lstHandler.AddLast(new MksRfPowerSetRegulationModeHandler(this, _deviceAddress, mode));
- }
- }
- public void QueryStatus()
- {
- lock (_locker)
- {
- _lstHandler.AddLast(new MksRfPowerQueryStatusHandler(this));
- }
- }
- internal void NoteError(string reason)
- {
- _trigWarningMessage.CLK = true;
- if (_trigWarningMessage.Q)
- {
- EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
- }
- }
- internal void NoteReflectPower(float data)
- {
- _reflectPower = data;
- }
- internal void NoteVoltage(float data)
- {
- _voltage = data;
- }
- internal void NoteFrequency(float data)
- {
- Frequency = data;
- }
- //internal void NoteRegulationModeSetPoint(EnumRfPowerRegulationMode regMode)
- //{
- // _regulationMode = regMode;
- //}
- internal void NotePowerSetPoint(int power)
- {
- _powerSetPoint = power;
- }
- internal void NoteForwardPower(int power)
- {
- _forwardPower = power;
- }
- }
- }
|