123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760 |
- 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.RFMatchs.Serens
- {
- public class SerenRfMatch : RfMatchBase
- {
- public override EnumRfMatchTuneMode TuneMode1
- {
- get;
- set;
- }
- public override EnumRfMatchTuneMode TuneMode2
- {
- get;
- set;
- }
- public EnumRfMatchTuneMode LoadMode1
- {
- get;
- set;
- }
- public EnumRfMatchTuneMode LoadMode2
- {
- get;
- set;
- }
- public override float LoadPosition1
- {
- get;
- set;
- }
- public override float LoadPosition2
- {
- get;
- set;
- }
- public override float TunePosition1
- {
- get;
- set;
- }
- public bool Connect()
- {
- return _connection.Connect();
- }
- public bool Disconnect()
- {
- return _connection.Disconnect();
- }
- public override bool IsConnected => Connection != null && Connection.IsConnected && !_connection.IsCommunicationError;
- public override AITRfMatchData DeviceData
- {
- get
- {
- AITRfMatchData data = new AITRfMatchData()
- {
- DeviceName = Name,
- DeviceSchematicId = DeviceID,
- DisplayName = Display,
- LoadPosition1 = LoadPosition1,
- LoadPosition2 = LoadPosition2,
- LoadPosition1SetPoint = LoadPosition1Setpoint,
- TunePosition1 = TunePosition1,
- TunePosition2 = TunePosition2,
- TunePosition1SetPoint = TunePosition1Setpoint,
- TuneMode1 = TuneMode1,
- TuneMode2 = TuneMode2,
- BiasPeak = BiasPeak,
- DCBias = DCBias,
- };
- return data;
- }
- }
- public override bool IsStable
- {
- get
- {
- if (_scStableCriteria == null || _scStableCriteria.DoubleValue == 0)
- return false;
- if (LoadPosition1Setpoint == 0 || TunePosition1Setpoint == 0)
- return true;
- if (100 * LoadPosition1 / LoadPosition1Setpoint < _scStableCriteria.DoubleValue)
- _stableTimer.Stop();
- if (100 * TunePosition1 / TunePosition1Setpoint < _scStableCriteria.DoubleValue)
- _stableTimer.Stop();
- if (_stableTimer.IsIdle() && 100 * TunePosition1 / TunePosition1Setpoint >= _scStableCriteria.DoubleValue &&
- 100 * LoadPosition1 / LoadPosition1Setpoint >= _scStableCriteria.DoubleValue)
- _stableTimer.Start(_stableTime * 1000);
- return _stableTimer.IsTimeout();
- }
- }
- public SerenRfMatchConnection Connection
- {
- get { return _connection; }
- }
- private SerenRfMatchConnection _connection;
- private EnumRfMatchTuneMode _tuneMode1;
- private EnumRfMatchTuneMode _tuneMode2;
- 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 LinkedList<HandlerBase> _executionHandler = new LinkedList<HandlerBase>();
- private LinkedList<HandlerBase> _idleHandler = new LinkedList<HandlerBase>();
- private object _locker = new object();
- private bool _enableLog = true;
- public int MagnitudeError { get; set; }
- public int PhaseError { get; set; }
- public int DCVoltage { get; set; }
- public int RFVoltage { get; set; }
- public string MatchType { get; set; } //ATS or MC2
- private string _scRoot;
- private bool _isPresetEnabled;
- private float _loadPresetPosition;
- private float _tunePresetPosition;
- public SerenRfMatch(string module, string name, string scRoot, string type = "ATS") : base(module, name)
- {
- MatchType = type;
- _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 SerenRfMatchConnection(portName, baudRate, dataBits, parity, stopBits);
- _connection.EnableLog(_enableLog);
- if (_connection.Connect())
- {
- EV.PostInfoLog(Module, $"{Module}.{Name} connected");
- InitStatus();
- }
- _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);
- _enableLog = SC.GetValue<bool>($"{_scRoot}.EnableLogMessage");
- _connection = new SerenRfMatchConnection(portName, bautRate, dataBits, parity, stopBits);
- _connection.EnableLog(_enableLog);
- if (_connection.Connect())
- {
- EV.PostInfoLog(Module, $"{Module}.{Name} connected");
- InitStatus();
- }
- DATA.Subscribe($"{Module}.{Name}.IsConnected", () => IsConnected);
- DATA.Subscribe($"{Module}.{Name}.Address", () => SC.GetStringValue($"{_scRoot}.Address"));
- DATA.Subscribe($"{Module}.{Name}.MagnitudeError", () => MagnitudeError);
- DATA.Subscribe($"{Module}.{Name}.PhaseError", () => PhaseError);
- DATA.Subscribe($"{Module}.{Name}.DCVoltage", () => DCVoltage);
- DATA.Subscribe($"{Module}.{Name}.RFVoltage", () => RFVoltage);
- DATA.Subscribe($"{Module}.{Name}.IsPresetEnable", () => _isPresetEnabled);
- DATA.Subscribe($"{Module}.{Name}.LoadPresetPosition", () => _loadPresetPosition);
- DATA.Subscribe($"{Module}.{Name}.TunePresetPosition", () => _tunePresetPosition);
- OP.Subscribe($"{Module}.{Name}.MatchEnablePreset", (out string reason, int time, object[] args) =>
- {
- reason = "";
- bool enable = Convert.ToBoolean(args[0]);
- string text = enable ? "enable" : "disable";
- if (!PerformMatchMode1EnablePreset(out reason, time, enable))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not {text} preset mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} {text} match preset mode");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.MatchMode1", (out string reason, int time, object[] args) =>
- {
- reason = "";
- if (!Enum.TryParse((string)args[0], out EnumRfMatchTuneMode mode))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not mode, {args[0]} is not a valid mode value");
- return false;
- }
- if (!PerformMatchMode1(out reason, time, mode))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set match mode1 to {mode}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.SetMatchLoad1", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- if (!PerformSetMatchLoad1(out reason, time, value))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set match load1 to {value}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.SetMatchLoadPresetPosition", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- SC.SetItemValue($"{Module}.{Name}.MatchLoadPresetPosition", value);
- if (!PerformSetMatchLoadPresetPosition(out reason, time, value))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set preset load position, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set match load preset position to {value}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.SetMatchTunePresetPosition", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- SC.SetItemValue($"{Module}.{Name}.MatchTunePresetPosition", value);
- if (!PerformSetMatchTunePresetPosition(out reason, time, value))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set preset tune position, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set match tune preset position to {value}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.SetMatchTune1", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- if (!PerformSetMatchTune1(out reason, time, value))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set match tune1 to {value}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.MatchMode2", (out string reason, int time, object[] args) =>
- {
- reason = "";
- if (!Enum.TryParse((string)args[0], out EnumRfMatchTuneMode mode))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not mode, {args[0]} is not a valid mode value");
- return false;
- }
- if (!PerformMatchMode2(out reason, time, mode))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set match mode2 to {mode}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.SetMatchLoad2", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- if (!PerformSetMatchLoad2(out reason, time, value))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set match load2 to {value}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.SetMatchTune2", (out string reason, int time, object[] args) =>
- {
- reason = "";
- float value = Convert.ToSingle((string)args[0]);
- if (!PerformSetMatchTune2(out reason, time, value))
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
- return false;
- }
- EV.PostInfoLog(Module, $"{Module}.{Name} set match tune2 to {value}");
- return true;
- });
- OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) =>
- {
- Disconnect();
- Reset();
- Connect();
- return true;
- });
- _thread = new PeriodicJob(300, OnTimer, $"{Module}.{Name} MonitorHandler", true);
- return true;
- }
- private bool OnTimer()
- {
- try
- {
- _connection.MonitorTimeout();
- if (!_connection.IsConnected || _connection.IsCommunicationError)
- {
- lock (_locker)
- {
- _executionHandler.Clear();
- _idleHandler.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}");
- }
- }
- return true;
- }
- HandlerBase handler = null;
- if (!_connection.IsBusy)
- {
- lock (_locker)
- {
- if (_executionHandler.Count == 0)
- {
- if (_idleHandler.Count == 0)
- {
- _idleHandler.AddLast(new SerenRfMatchGetMagnitudeErrorHandler(this));
- _idleHandler.AddLast(new SerenRfMatchGetPhaseErrorHandler(this));
- _idleHandler.AddLast(new SerenRfMatchGetDCVoltageHandler(this));
- _idleHandler.AddLast(new SerenRfMatchGetRFVoltageHandler(this));
- _idleHandler.AddLast(new SerenRfMatchGetTunePositionHandler(this));
- _idleHandler.AddLast(new SerenRfMatchGetLoadPositionHandler(this));
- _idleHandler.AddLast(new SerenRfMatchGetTuneControlModeHandler(this));
- }
- else
- {
- handler = _idleHandler.First.Value;
- _idleHandler.RemoveFirst();
- }
- }
- else
- {
- handler = _executionHandler.First.Value;
- _executionHandler.RemoveFirst();
- }
- }
- if (handler != null)
- {
- _connection.Execute(handler);
- }
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- return true;
- }
- public override void Monitor()
- {
- try
- {
- _connection.EnableLog(_enableLog);
- _trigCommunicationError.CLK = _connection.IsCommunicationError;
- if (_trigCommunicationError.Q)
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
- }
- }
- 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();
- }
- internal void NoteError(string reason)
- {
- _trigWarningMessage.CLK = true;
- if (_trigWarningMessage.Q)
- {
- EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
- }
- }
- internal void NoteLoadControlMode(bool isAuto)
- {
- LoadMode1 = isAuto ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual;
- }
- internal void NotePresetMode(string mode)
- {
- _isPresetEnabled = mode == "1";
- }
- internal void NoteTuneControlMode(bool isAuto)
- {
- TuneMode1 = isAuto ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual;
- }
- internal void NoteLoadPosition(float position)
- {
- LoadPosition1 = position;
- }
- internal void NoteTunePosition(float position)
- {
- TunePosition1 = position;
- }
- internal void NoteLoadPresetPosition(float position)
- {
- _loadPresetPosition = position;
- }
- internal void NoteTunePresetPosition(float position)
- {
- _tunePresetPosition = position;
- }
- internal void NoteMagnitudeError(int error)
- {
- MagnitudeError = error;
- }
- internal void NotePhaseError(int error)
- {
- PhaseError = error;
- }
- internal void NoteDCVoltage(int voltage)
- {
- DCVoltage = voltage;
- }
- internal void NoteRFVoltage(int voltage)
- {
- RFVoltage = voltage;
- }
- private bool PerformSetMatchTune1(out string reason, int time, float value)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformSetMatchLoad1(out string reason, int time, float value)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformSetMatchLoadPresetPosition(out string reason, int time, float value)
- {
- reason = string.Empty;
- lock (_locker)
- {
- _executionHandler.AddLast(new SerenRfMatchSetLoadPreset1PositionHandler(this, value));
- _executionHandler.AddLast(new SerenRfMatchGetLoadPreset1PositionHandler(this));
- }
- return true;
- }
- private bool PerformSetMatchTunePresetPosition(out string reason, int time, float value)
- {
- reason = string.Empty;
- lock (_locker)
- {
- _executionHandler.AddLast(new SerenRfMatchSetTunePreset1PositionHandler(this, value));
- _executionHandler.AddLast(new SerenRfMatchGetTunePreset1PositionHandler(this));
- }
- return true;
- }
- private bool PerformMatchMode1(out string reason, int time, EnumRfMatchTuneMode mode)
- {
- SetLoadMode1(mode);
- SetTuneMode1(mode);
- reason = string.Empty;
- return true;
- }
- private bool PerformMatchMode1EnablePreset(out string reason, int time, bool enable)
- {
- reason = string.Empty;
- lock (_locker)
- {
- _executionHandler.AddLast(new SerenRfMatchEnablePresetHandler(this, enable));
- _executionHandler.AddLast(new SerenRfMatchGetPresetHandler(this));
- }
- return true;
- }
- private bool PerformMatchMode2(out string reason, int time, EnumRfMatchTuneMode mode)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformSetMatchLoad2(out string reason, int time, float value)
- {
- reason = string.Empty;
- return true;
- }
- private bool PerformSetMatchTune2(out string reason, int time, float value)
- {
- reason = string.Empty;
- return true;
- }
- public void InitStatus()
- {
- lock (_locker)
- {
- _executionHandler.AddLast(new SerenRfMatchGetPresetHandler(this));
- _executionHandler.AddLast(new SerenRfMatchGetTuneControlModeHandler(this));
- _executionHandler.AddLast(new SerenRfMatchGetTunePreset1PositionHandler(this));
- _executionHandler.AddLast(new SerenRfMatchGetLoadPreset1PositionHandler(this));
- }
- }
- public override void EnablePreset1(bool enable)
- {
- PerformMatchMode1EnablePreset(out _, 0, enable);
- }
- public override void SetLoadMode1(EnumRfMatchTuneMode mode)
- {
- lock (_locker)
- {
- _executionHandler.AddLast(new SerenRfMatchSetLoadControlModeHandler(this, mode == EnumRfMatchTuneMode.Auto));
- }
- }
- public override void SetTuneMode1(EnumRfMatchTuneMode mode)
- {
- lock (_locker)
- {
- _executionHandler.AddLast(new SerenRfMatchSetTuneControlModeHandler(this, mode == EnumRfMatchTuneMode.Auto));
- _executionHandler.AddLast(new SerenRfMatchGetTuneControlModeHandler(this));
- }
- }
- public override void SetLoadPresetPosition(float position)
- {
- PerformSetMatchLoadPresetPosition(out _, 0, position);
- }
- public override void SetTunePresetPosition(float position)
- {
- PerformSetMatchTunePresetPosition(out _, 0, position);
- }
- public override void SetLoad1(float position)
- {
- LoadPosition1Setpoint = position;
- lock (_locker)
- {
- if (MatchType == "ATS")
- {
- //_lstHandler.AddLast(new SerenRfMatchSetLoadControlModeHandler(this, false));
- _executionHandler.AddLast(new SerenRfMatchSetLoadPositionHandler(this, position));
- }
- else
- {
- // _lstHandler.AddLast(new SerenRfMatchSetLoadControlModeHandler(this, false));
- _executionHandler.AddLast(new SerenRfMatchSetLoadPreset1PositionHandler(this, position));
- _executionHandler.AddLast(new SerenRfMatchGotoHandler(this));
- }
- }
- }
- public override void SetTune1(float position)
- {
- TunePosition1Setpoint = position;
- lock (_locker)
- {
- if (MatchType == "ATS")
- {
- // _lstHandler.AddLast(new SerenRfMatchSetTuneControlModeHandler(this, false));
- _executionHandler.AddLast(new SerenRfMatchSetTunePositionHandler(this, position));
- }
- else
- {
- // _lstHandler.AddLast(new SerenRfMatchSetTuneControlModeHandler(this, false));
- _executionHandler.AddLast(new SerenRfMatchSetTunePreset1PositionHandler(this, position));
- _executionHandler.AddLast(new SerenRfMatchGotoHandler(this));
- }
- }
- }
- public void GetTune1()
- {
- lock (_locker)
- {
- _executionHandler.AddLast(new SerenRfMatchGetTunePositionHandler(this));
- }
- }
- public void GetLoad1()
- {
- lock (_locker)
- {
- _executionHandler.AddLast(new SerenRfMatchGetLoadPositionHandler(this));
- }
- }
- public void GetLoad1Mode()
- {
- lock (_locker)
- {
- _executionHandler.AddLast(new SerenRfMatchGetLoadControlModeHandler(this));
- }
- }
- }
- }
|