using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Event; using Aitex.Core.RT.IOCore; using Aitex.Core.RT.Log; using Aitex.Core.RT.OperationCenter; using Aitex.Core.RT.SCCore; using Aitex.Core.RT.Tolerance; using Aitex.Core.Util; using MECF.Framework.Common.Communications; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.Device.Bases; using MECF.Framework.Common.Equipment; namespace JetVirgoPM.Devices { public enum Mode { Read = 0, Write } public enum MatchingAutoMode { Hold = 0, Preset } static class CometRFCommand { public const int Model = 11; public const int Type = 12; public const int SerialNumber = 13; public const int NominalPower = 15; public const int NominalFrequency = 16; public const int Command = 1001; public const int FrequencyMode = 1101; public const int ControlMode = 1201; public const int PowerSetPoint = 1206; public const int RFOnTime = 1701; public const int ForwardPowerLimit = 1702; public const int ReflectedPowerLimit = 1703; public const int MatchingMode = 1703; public const int State = 8000; public const int NumberOfErrors = 8100; public const int NumberOfWarnings = 8150; public const int ForwardPower = 8021; public const int ReflectedPower = 8022; public const int MatchinMode = 8201; public const int MatchingAutoMode = 8202; public const int ActCLoadPosition = 8213; public const int ActCTunePosition = 8214; public const int CLoadRefPosition = 8203; public const int CTuneRefPosition = 8204; public const int MatchingState = 9201; public const int MatchingActive = 9202; public const int CLoadPosition = 9203; public const int CTunePosition = 9204; public const int CLoad = 9205; public const int CTune = 9206; public const int CLoadRefPos = 9210; public const int CTuneRefPos = 9212; public const int ProcessControlMatching = 9251; } class CometRF : RfPowerBase { private double _total; private double _fromLast; private readonly SCConfigItem _scPowerAlarmTime; private readonly SCConfigItem _scPowerAlarmRange; private readonly SCConfigItem _scReflectPowerAlarmTime; private readonly SCConfigItem _scReflectPowerAlarmRange; private readonly SCConfigItem _scPowerRange; private int TransactionNumber = 0; private static byte ProtocolIdentifierHighByte = 0x00; private static byte ProtocolIdentifierLowByte = 0x00; private static byte _address = 0x0A; private static byte _read = 0x41; private static byte _write = 0x42; private static byte _readInvalid = 0xC1; private static byte _writeInvalid = 0xC2; private static byte[] _sendData; private const int QUERY_INTERVAL = 500; protected bool _commErr = false; protected bool _exceuteErr = false; protected string _addr; protected static Object _locker = new Object(); private int _currentCommandNumber = 0; public GeneratorStatus Status { get; set; } public GeneratorStatus StatusMatch { get; set; } public MatchingAutoMode MatchMode { get; set; } public int ErrorCode { get; set; } public bool Initalized { get; set; } public const string delimiter = "\r"; private string AlarmRobotError = "RF Error"; protected AsyncSocketDevice _socket; private DateTime _powerOnStartTime; private TimeSpan _powerOnElapsedTime; private readonly DeviceTimer _timerQueryStatus = new DeviceTimer(); private readonly DeviceTimer _timerTotal = new DeviceTimer(); private readonly DeviceTimer _timerFromLast = new DeviceTimer(); private readonly RD_TRIG _rfOnTrigger = new RD_TRIG(); private readonly R_TRIG _ErrTrigger = new R_TRIG(); private readonly R_TRIG _trigPMNeeded = new R_TRIG(); private readonly RD_TRIG _trigOnOff = new RD_TRIG(); private StatsDataItemRFAndPump _statRFOnTime; private ToleranceChecker _checkerPower; private ToleranceChecker _checkerWarningPower; private ToleranceChecker _checkerReflectPower; private readonly double _scPowerWarningTime; private readonly double _scPowerWarningRange; private readonly DIAccessor _diIntlk; private bool _isPowerOn; private ToleranceChecker _alarmChecker; private ToleranceChecker _warningChecker; protected float _recipeAlarmRange; protected float _recipeWarningRange; protected int _recipeIgnoreTimeMS; protected ToleranceChecker _recipeAlarmChecker = new ToleranceChecker(); protected ToleranceChecker _recipeWarningChecker = new ToleranceChecker(); protected DeviceTimer _recipeIgnoreTimer = new DeviceTimer(); public override float ScalePower => (float)_scPowerRange.DoubleValue; public string Address { get { return _addr; } } public bool IsConnected { get { if (_socket == null) return false; else return _socket.IsConnected; } } public bool Disconnect() { return true; } public bool Communication { get { return !_commErr || _commErr || _exceuteErr; } } public bool Error { get { return ErrorCode > 0; } } public int MatchProcessMode { get { return (int)MatchMode; } } public override bool IsPowerOn { get => Status == GeneratorStatus.ON; set { } } public override bool IsMatchOn { get => StatusMatch == GeneratorStatus.ON; set { } } public override bool IsError { get => Status == GeneratorStatus.ERROR; set { } } public override bool IsMatchError { get => StatusMatch == GeneratorStatus.ERROR; set { } } public string LastPMTime { get { return _statRFOnTime != null ? _statRFOnTime.LastPMTime.ToString() : ""; } } public double DaysFromLastPM { get { return _statRFOnTime == null ? 0 : _statRFOnTime.fromLastPM; } set { if (_statRFOnTime != null) _statRFOnTime.fromLastPM = value; } } public double TotalDays { get { return _statRFOnTime != null ? _statRFOnTime.Total : 0; } set { if (_statRFOnTime != null) _statRFOnTime.Total = value; } } public double PMIntervalDays { get { return _statRFOnTime != null ? _statRFOnTime.PMInterval : 0; } } public bool IsPMNeeded { get { return DaysFromLastPM > PMIntervalDays; } } public bool EnableAlarm { get { return _statRFOnTime == null || _statRFOnTime.AlarmEnable; } } [Subscription("PowerOnTime")] public string PowerOnTime { get { if (IsPowerOn) _powerOnElapsedTime = DateTime.Now - _powerOnStartTime; return $"{(int)_powerOnElapsedTime.TotalHours:00}:{_powerOnElapsedTime.Minutes:00}:{(_powerOnElapsedTime.Seconds > 0 ? (_powerOnElapsedTime.Seconds + 1) : 0):00}"; } } public bool RFInterlock => _diIntlk == null || _diIntlk.Value; private float _forwardPower; public override float ForwardPower { get { return _forwardPower; } set { _forwardPower = CalibrationData(value, false); } } public new AITRfData DeviceData => new AITRfData { Module = Module, DeviceName = Name, ScalePower = ScalePower, ForwardPower = ForwardPower, ReflectPower = ReflectPower, IsRfOn = IsPowerOn, PowerSetPoint = PowerSetPoint, PowerOnElapsedTime = PowerOnTime, IsInterlockOk = RFInterlock, IsMatchOn = IsMatchOn, MatchPositionC1 = CLoad, MatchPositionC2 = CTune, MatchVPP = VPP, MatchPresetMode = MatchProcessMode, MatchPositionC1SetPoint = CLoadSet, MatchPositionC2SetPoint = CTuneSet, WorkMode = (int)RfMode.ContinuousWaveMode, DisplayName = "Bias RF", }; public CometRF(ModuleName mod,string name, string address) : base(mod.ToString(), name) { //if (SC.GetValue("System.IsSimulatorMode")) //{ // address = "127.0.0.1:502"; //} Status = GeneratorStatus.Unknown; StatusMatch = GeneratorStatus.Unknown; MatchMode = MatchingAutoMode.Preset; _addr = address; _socket = new AsyncSocketDevice(address); _socket.OnDataChanged += new AsyncSocketDevice.MessageHandler(OnDataChanged); _socket.OnErrorHappened += new AsyncSocketDevice.ErrorHandler(OnErrorHandler); _scPowerAlarmTime = SC.GetConfigItem($"{Module}.{Name}.PowerAlarmTime"); _scPowerAlarmRange = SC.GetConfigItem($"{Module}.{Name}.PowerAlarmRange"); _scReflectPowerAlarmTime = SC.GetConfigItem($"{Module}.{Name}.ReflectPowerAlarmTime"); _scReflectPowerAlarmRange = SC.GetConfigItem($"{Module}.{Name}.ReflectPowerAlarmRange"); _scPowerRange = SC.GetConfigItem($"{Module}.{Name}.PowerRange"); _scEnableCalibration = SC.GetConfigItem($"{Module}.{Name}.EnableCalibration"); _scCalibrationTable = SC.GetConfigItem($"{Module}.{Name}.CalibrationTable"); _scRFPhysicalMaxPower = SC.GetConfigItem($"{Module}.{Name}.RFPhysicalMaxPower"); _scCurrentRFMaxPower = SC.GetConfigItem($"{Module}.{Name}.CurrentRFMaxPower"); _scPowerWarningTime = SC.GetValue($"{Module}.{Name}.PowerWarningTime"); _scPowerWarningRange = SC.GetValue($"{Module}.{Name}.PowerWarningRange"); _diIntlk = IO.DI[$"{Module}.DI_Generator_Hardware_Interlock"]; Initalized = false; } private void OnErrorHandler(ErrorEventArgsDevice args) { //SetPowerOnOff(false, out _); Status = GeneratorStatus.ERROR; StatusMatch = GeneratorStatus.ERROR; LOG.Error($"{Module} Comet RF Error {args.Reason}"); } public override bool Initialize() { base.Initialize(); DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData); Connect(); _statRFOnTime = StatsDataManager.Instance.GetItemRFAndPump($"{Module}.{Name}.RfOnTime"); _timerQueryStatus.Start(QUERY_INTERVAL); _checkerPower = new ToleranceChecker(_scPowerAlarmTime.DoubleValue); _checkerWarningPower = new ToleranceChecker(_scPowerWarningTime); _checkerReflectPower = new ToleranceChecker(_scReflectPowerAlarmTime.DoubleValue); OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) => { return Connect(); }); OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetPowerOnOff}", (out string reason, int time, object[] param) => { SetPowerOnOff(Convert.ToBoolean((string)param[0]), out reason); return true; }); OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetPower}", (out string reason, int time, object[] param) => { reason = ""; ushort val = Convert.ToUInt16(param[0]); SetPower(val); return true; }); OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetContinuousPower}", (out string reason, int time, object[] param) => { reason = ""; ushort val = Convert.ToUInt16(param[0]); SetPower(val); return true; }); OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) => { BiasRfMatchMode mode = (BiasRfMatchMode)Enum.Parse(typeof(BiasRfMatchMode), (string)param[0], true); reason = ""; if (mode == BiasRfMatchMode.Hold) SetMatchingAutoMode(false, out reason); else SetMatchingAutoMode(true, out reason); return true; }); OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) => { if (MatchMode == MatchingAutoMode.Hold) { reason = string.Empty; return true; } float c1 = (float)Convert.ToDouble((string)param[0]); reason = string.Format("Set RF match position c1 :{0}", c1); if (c1 <= 100 && c1 >= 0) { SendCmd(Mode.Write, CometRFCommand.CLoadRefPosition, (int)(c1 * 10)); } return true; }); OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) => { if (MatchMode == MatchingAutoMode.Hold) { reason = string.Empty; return true; } float c2 = (float)Convert.ToDouble((string)param[0]); reason = string.Format("Set RF match position c2 :{0}", c2); if (c2 <= 100 && c2 >= 0) { SendCmd(Mode.Write, CometRFCommand.CTuneRefPosition, (int)(c2 * 10)); } return true; }); OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) => { return SetMatchPosition(Convert.ToDouble((string)param[0]), Convert.ToDouble((string)param[1]), out reason); }); OP.Subscribe($"{Module}.{Name}.SetRecipeTolerance", (out string reason, int time, object[] param) => { reason = string.Empty; _recipeIgnoreTimeMS = Convert.ToInt32(param[0]) * 1000; _recipeWarningRange = Convert.ToSingle(param[1]); _recipeAlarmRange = Convert.ToSingle(param[2]); _recipeAlarmChecker.RST = true; _recipeWarningChecker.RST = true; if (_recipeIgnoreTimeMS > 0) _recipeIgnoreTimer.Start(0); return true; }); DATA.Subscribe($"{Module}.{Name}.CommunicationStatus", () => _socket == null ? false : _socket.IsConnected); //EV.Subscribe(new EventItem("Event", AlarmRobotError, "Robot error", EventLevel.Alarm, Aitex.Core.RT.Event.EventType.HostNotification)); _alarmChecker = new ToleranceChecker(_scPowerAlarmTime.DoubleValue); _warningChecker = new ToleranceChecker(_scPowerWarningTime); _checkerReflectPower = new ToleranceChecker(_scReflectPowerAlarmTime.DoubleValue); _recipeAlarmChecker = new ToleranceChecker(0); _recipeWarningChecker = new ToleranceChecker(0); DATA.Subscribe($"{Module}.{Name}Match.C1", () => CLoad); DATA.Subscribe($"{Module}.{Name}Match.C2", () => CTune); DATA.Subscribe($"{Module}.{Name}Match.VPP", () => VPP); DATA.Subscribe($"{Module}.{Name}Match.MatchProcessMode", () => (int)MatchMode); Reset(); SendCmd(Mode.Write, CometRFCommand.ControlMode, 0); SendCmd(Mode.Write, CometRFCommand.MatchinMode, 2); Thread thread = new Thread(() => { while (true) { if (!IsConnected) return; SendCmd(Mode.Read, CometRFCommand.State, 1); SendCmd(Mode.Read, CometRFCommand.ForwardPower, 1); SendCmd(Mode.Read, CometRFCommand.ReflectedPower, 1); SendCmd(Mode.Read, CometRFCommand.MatchingState, 1); SendCmd(Mode.Read, CometRFCommand.MatchingActive, 1); SendCmd(Mode.Read, CometRFCommand.CLoadPosition, 1); SendCmd(Mode.Read, CometRFCommand.CTunePosition, 1); SendCmd(Mode.Read, CometRFCommand.ProcessControlMatching, 1); //if(IsError) //{ // SendCmd(Mode.Read, CometRFCommand.NumberOfErrors, 1); // SendCmd(Mode.Read, CometRFCommand.NumberOfWarnings, 1); //} } }); thread.Start(); return true; } public virtual bool Connect() { _commErr = false; _socket?.Connect(this._addr); return true; } public void OnDataChanged(byte[] rawMessage) { try { int recTransactionNumber = BitConverter.ToInt32(new byte[] { rawMessage[1], rawMessage[0], 0x00, 0x00 }, 0); if (recTransactionNumber != TransactionNumber) { LOG.Error($"RF transaction number is different"); return; } if (rawMessage[6] != _address) { LOG.Error($"RF invalid address byte"); } if (rawMessage[7] == _read) { if (rawMessage[8] == 4) { int DataValue = BytesToInt(new byte[] { rawMessage[9], rawMessage[10], rawMessage[11], rawMessage[12] }, 0); byte[] abc = IntToBytes(DataValue); ParseReceiveData(_currentCommandNumber, DataValue); } else { string ReadData = Encoding.ASCII.GetString(rawMessage, 9, Convert.ToInt32(rawMessage[8])); ParseReceiveData(_currentCommandNumber, 0, ReadData); } } else if (rawMessage[7] == _write) { int CommandNumber = BitConverter.ToInt32(new byte[] { rawMessage[9], rawMessage[8], 0x00, 0x00 }, 0); int DataValue = BytesToInt(new byte[] { rawMessage[10], rawMessage[11], rawMessage[12], rawMessage[13] }, 0); ParseReceiveData(CommandNumber, DataValue); } else if (rawMessage[7] == _readInvalid) { int DataValue = BitConverter.ToInt32(new byte[] { rawMessage[8], 0x00, 0x00, 0x00 }, 0); LOG.Error($"{Module} [{Display} Read] invalid request, Command Number is {_currentCommandNumber.ToString()}, Exception Code is 0x{DataValue.ToString("X")}"); } else if (rawMessage[7] == _writeInvalid) { int DataValue = BitConverter.ToInt32(new byte[] { rawMessage[8], 0x00, 0x00, 0x00 }, 0); LOG.Error($"{Module} [{Display} Write] invalid request, Command Number is {_currentCommandNumber.ToString()}, Exception Code is 0x{DataValue.ToString("X")}"); } else { LOG.Error($"RF invalid function code"); } } //catch (ExcuteFailedException e) //{ // EV.PostMessage("RF", EventEnum.DefaultWarning, string.Format("executed failed. {0}", e.Message)); // OnError(); // _exceuteErr = false; //} //catch (InvalidPackageException e) //{ // EV.PostMessage("RF", EventEnum.DefaultWarning, string.Format("receive invalid package. {0}", e.Message)); // OnError(); //} catch (System.Exception ex) { _commErr = true; LOG.Write("RF failed:" + ex.ToString()); } } private void ParseReceiveData(int cometCommand, int value, string sValue = "") { switch (cometCommand) { case 8000: switch (value) { case 0: //LOG.Info($"{Module} [{Display} Read] Device is not ready"); break; case 1: //LOG.Info($"{Module} [{Display} Read] Device is ready, RF is off"); Status = GeneratorStatus.OFF; break; case 2: //LOG.Info($"{Module} [{Display} Read] Device is active, RF is on"); Status = GeneratorStatus.ON; break; case 3: //LOG.Info($"{Module} [{Display} Read] Device is in error state"); Status = GeneratorStatus.ERROR; break; case 4: //LOG.Info($"{Module} [{Display} Read] Device is in calibration state"); break; case 5: //LOG.Info($"{Module} [{Display} Read] Device is waiting for firmware update"); break; case 6: //LOG.Info($"{Module} [{Display} Read] Device is blocked for configuration update"); break; default: break; } break; case 8021: //LOG.Info($"{Module} [{Display} Read] RF Forward power is {value / 1000} W"); ForwardPower = value / 1000; break; case 8022: //LOG.Info($"{Module} [{Display} Read] RF Reflected power is {value / 1000} W"); ReflectPower = value / 1000; break; case 8100: //LOG.Info($"{Module} [{Display} Read] RF Number of Errors is {value}"); break; case 8101: LOG.Info($"{Module} [{Display} Read] RF Error 1 is {sValue}"); break; case 8102: LOG.Info($"{Module} [{Display} Read] RF Error 1 state is {value}"); break; case 8103: LOG.Info($"{Module} [{Display} Read] RF Error 2 is {sValue}"); break; case 8104: LOG.Info($"{Module} [{Display} Read] RF Error 2 state is {value}"); break; case 8105: LOG.Info($"{Module} [{Display} Read] RF Error 3 is {sValue}"); break; case 8106: LOG.Info($"{Module} [{Display} Read] RF Error 3 state is {value}"); break; case 8107: LOG.Info($"{Module} [{Display} Read] RF Error 4 is {sValue}"); break; case 8108: LOG.Info($"{Module} [{Display} Read] RF Error 4 state is {value}"); break; case 8109: LOG.Info($"{Module} [{Display} Read] RF Error 5 is {sValue}"); break; case 8110: LOG.Info($"{Module} [{Display} Read] RF Error 5 state is {value}"); break; case 8111: LOG.Info($"{Module} [{Display} Read] RF Error 6 is {sValue}"); break; case 8112: LOG.Info($"{Module} [{Display} Read] RF Error 6 state is {value}"); break; case 8113: LOG.Info($"{Module} [{Display} Read] RF Error 7 is {sValue}"); break; case 8114: LOG.Info($"{Module} [{Display} Read] RF Error 7 state is {value}"); break; case 8115: LOG.Info($"{Module} [{Display} Read] RF Error 8 is {sValue}"); break; case 8116: LOG.Info($"{Module} [{Display} Read] RF Error 8 state is {value}"); break; case 8150: LOG.Info($"{Module} [{Display} Read] RF Number of warnings is {value}"); break; case 8151: LOG.Info($"{Module} [{Display} Read] RF Warning 1 is {sValue}"); break; case 8152: LOG.Info($"{Module} [{Display} Read] RF Warning 2 is {sValue}"); break; case 8153: LOG.Info($"{Module} [{Display} Read] RF Warning 3 is {sValue}"); break; case 8154: LOG.Info($"{Module} [{Display} Read] RF Warning 4 is {sValue}"); break; case 8155: LOG.Info($"{Module} [{Display} Read] RF Warning 5 is {sValue}"); break; case 8156: LOG.Info($"{Module} [{Display} Read] RF Warning 6 is {sValue}"); break; case 8157: LOG.Info($"{Module} [{Display} Read] RF Warning 7 is {sValue}"); break; case 8158: LOG.Info($"{Module} [{Display} Read] RF Warning 8 is {sValue}"); break; case 8159: LOG.Info($"{Module} [{Display} Read] RF Warning 9 is {sValue}"); break; case 8160: LOG.Info($"{Module} [{Display} Read] RF Warning 10 is {sValue}"); break; case 8161: LOG.Info($"{Module} [{Display} Read] RF Warning 11 is {sValue}"); break; case 8162: LOG.Info($"{Module} [{Display} Read] RF Warning 12 is {sValue}"); break; case 8163: LOG.Info($"{Module} [{Display} Read] RF Warning 13 is {sValue}"); break; case 8164: LOG.Info($"{Module} [{Display} Read] RF Warning 14 is {sValue}"); break; case 8165: LOG.Info($"{Module} [{Display} Read] RF Warning 15 is {sValue}"); break; case 8166: LOG.Info($"{Module} [{Display} Read] RF Warning 16 is {sValue}"); break; case 9201: switch (value) { case 0: //LOG.Info($"{Module} [BiasMatch Read] No connected"); StatusMatch = GeneratorStatus.OFF; break; case 1: //LOG.Info($"{Module} [BiasMatch Read] Manual matching mode"); StatusMatch = GeneratorStatus.ON; break; case 2: //LOG.Info($"{Module} [BiasMatch Read] Automatic matching mode"); StatusMatch = GeneratorStatus.ON; break; case 11: //LOG.Info($"{Module} [BiasMatch Read] Manual matching mode (remote)"); StatusMatch = GeneratorStatus.ON; break; case 12: //LOG.Info($"{Module} [BiasMatch Read] Automatic matching mode (remote)"); StatusMatch = GeneratorStatus.ON; break; case 21: //LOG.Info($"{Module} [BiasMatch Read] Error matching mode"); StatusMatch = GeneratorStatus.ERROR; break; default: break; } break; case 9202: switch (value) { case 0: //LOG.Info($"{Module} [BiasMatch Read] holding position"); break; case 1: //LOG.Info($"{Module} [BiasMatch Read] moving"); break; default: break; } break; case 9203: //LOG.Info($"{Module} [BiasMatch Read] C load position is {value} %"); CLoad = value / 10f; break; case 9204: //LOG.Info($"{Module} [BiasMatch Read] C tune position is {value} %"); CTune = value / 10f; break; case 9205: //LOG.Info($"{Module} [BiasMatch Read] C load is {value} pF"); break; case 9206: //LOG.Info($"{Module} [BiasMatch Read] C tune is {value} pF"); break; case 9210: //LOG.Info($"{Module} [BiasMatch Read] C load ref pos is {value} %"); break; case 9212: //LOG.Info($"{Module} [BiasMatch Read] C tune ref pos {value} %"); break; case 9251: //LOG.Info($"{Module} [BiasMatch Read] Process control Matching {value} V"); VPP = value / 1000; break; case 1001: switch (value) { case 0: //LOG.Info($"{Module} [{Display} Write] Switch off RF"); break; case 1: //LOG.Info($"{Module} [{Display} Write] Switch on RF"); break; case 9: //LOG.Info($"{Module} [{Display} Write] RF Reset errors"); break; default: break; } break; case 1201: switch (value) { case 0: //LOG.Info($"{Module} [{Display} Write] RF Control mode is Forward power"); break; case 1: //LOG.Info($"{Module} [{Display} Write] RF Control mode is Load power"); break; case 2: //LOG.Info($"{Module} [{Display} Write] RF Control mode is Process control"); break; default: break; } break; case 1206: //LOG.Info($"{Module} [{Display} Write] RF Power Set point is {value / 1000} W"); PowerSetPoint = (_scEnableCalibration.BoolValue ? value : CalibrationData(value, false)) / 1000; break; case 8201: switch (value) { case 1: //LOG.Info($"{Module} [BiasMatch Write] Manual matching mode"); break; case 2: //LOG.Info($"{Module} [BiasMatch Write] Automatic matching mode"); break; default: break; } break; case 8202: switch (value) { case 0: //LOG.Info($"{Module} [BiasMatch Write] BiasMatch is Automatic hold mode"); MatchMode = MatchingAutoMode.Hold; break; case 1: //LOG.Info($"{Module} [BiasMatch Write] BiasMatch is Automatic preset mode"); MatchMode = MatchingAutoMode.Preset; break; default: break; } break; case 8203: //LOG.Info($"{Module} [{Display} Write] Set C load ref position is {value / 10} %"); CLoadSet = value / 10f; break; case 8204: //LOG.Info($"{Module} [{Display} Write] Set C tune ref position is {value / 10} %"); CTuneSet = value / 10f; break; default: break; } } public override void Monitor() { // power on triggered _rfOnTrigger.CLK = PowerSetPoint > 0 && _isPowerOn; if (_rfOnTrigger.R) { _total = TotalDays; _fromLast = DaysFromLastPM; _timerTotal.Start(0); _timerFromLast.Start(0); _powerOnStartTime = DateTime.Now; _checkerPower.Reset(_scPowerAlarmTime.DoubleValue); _checkerWarningPower.Reset(_scPowerWarningTime); _checkerReflectPower.Reset(_scReflectPowerAlarmTime.DoubleValue); } if (_rfOnTrigger.M) { TotalDays = _total + _timerTotal.GetElapseTime() / 1000 / 60 / 60; DaysFromLastPM = _fromLast + _timerFromLast.GetElapseTime() / 1000 / 60 / 60; if (_recipeAlarmRange > 0 && _recipeAlarmRange / 100.0 * PowerSetPoint < _scPowerAlarmRange.DoubleValue) { if (_recipeIgnoreTimer.GetElapseTime() > _recipeIgnoreTimeMS) { _recipeAlarmChecker.Monitor(ForwardPower, PowerSetPoint - Math.Abs(_recipeAlarmRange / 100.0 * PowerSetPoint), PowerSetPoint + Math.Abs(_recipeAlarmRange / 100.0 * PowerSetPoint), _scPowerAlarmTime.DoubleValue); if (_recipeAlarmChecker.Trig) { EV.PostAlarmLog(Module, Display + $" ForwardPower={ForwardPower }, PowerSetPoint ={PowerSetPoint} out of tolerance({PowerSetPoint - Math.Abs(_recipeAlarmRange / 100.0 * PowerSetPoint)},{PowerSetPoint + Math.Abs(_recipeAlarmRange / 100.0 * PowerSetPoint)}) in {_scPowerAlarmTime:0} seconds"); SetPowerOnOff(false, out _); } } } else { _alarmChecker.Monitor(ForwardPower, PowerSetPoint - Math.Abs(_scPowerAlarmRange.DoubleValue), PowerSetPoint + Math.Abs(_scPowerAlarmRange.DoubleValue), _scPowerAlarmTime.DoubleValue); if (_alarmChecker.Trig) { EV.PostAlarmLog(Module, Display + $" ForwardPower={ForwardPower}, PowerSetPoint={PowerSetPoint} out of tolerance({ PowerSetPoint - Math.Abs(_scPowerAlarmRange.DoubleValue)},{PowerSetPoint + Math.Abs(_scPowerAlarmRange.DoubleValue)}) in {_scPowerAlarmTime:0} seconds"); } } if (_recipeWarningRange > 0 && _recipeWarningRange / 100.0 * PowerSetPoint < _scPowerWarningRange) { if (_recipeIgnoreTimer.GetElapseTime() > _recipeIgnoreTimeMS) { _recipeWarningChecker.Monitor(ForwardPower, PowerSetPoint - Math.Abs(_recipeWarningRange / 100.0 * PowerSetPoint), PowerSetPoint + Math.Abs(_recipeWarningRange / 100.0 * PowerSetPoint), _scPowerWarningTime); if (_recipeWarningChecker.Trig) { EV.PostWarningLog(Module, Display + $" ForwardPower={ForwardPower}, PowerSetPoint={PowerSetPoint} out of tolerance({PowerSetPoint - Math.Abs(_recipeWarningRange / 100.0 * PowerSetPoint)},{PowerSetPoint + Math.Abs(_recipeWarningRange / 100.0 * PowerSetPoint)}) in {_scPowerWarningTime:0} seconds"); } } } else { _warningChecker.Monitor(ForwardPower, PowerSetPoint - Math.Abs(_scPowerWarningRange), PowerSetPoint + Math.Abs(_scPowerWarningRange), _scPowerWarningTime); if (_warningChecker.Trig) { EV.PostWarningLog(Module, Display + $" Pressure={ForwardPower}, PowerSetPoint={PowerSetPoint} out of tolerance({PowerSetPoint - Math.Abs(_scPowerWarningRange)},{PowerSetPoint + Math.Abs(_scPowerWarningRange)}) in {_scPowerWarningTime:0} seconds"); } } _checkerReflectPower.Monitor(ReflectPower, double.MinValue, _scReflectPowerAlarmRange.DoubleValue, _scReflectPowerAlarmTime.DoubleValue); if (_checkerReflectPower.Trig) { EV.PostAlarmLog($"{Module}", $"{Display} Reflect power {ReflectPower:0} out of range[0,{_scReflectPowerAlarmRange.DoubleValue:0}] in {_scReflectPowerAlarmTime.DoubleValue:0} seconds"); SetPowerOnOff(false, out _); } } if (PMIntervalDays > 0) { _trigPMNeeded.CLK = IsPMNeeded; if (_trigPMNeeded.Q) { if (EnableAlarm) { EV.PostAlarmLog($"{Module}", "rf on time value larger than setting interval days"); } } } if (_rfOnTrigger.T) StatsDataManager.Instance.Increase($"{Module}.{Name}.RfOnTime", $"{Module} {Name} RfOnTime", DaysFromLastPM, TotalDays); if (!_rfOnTrigger.CLK) { ForwardPower = 0; ReflectPower = 0; } base.Monitor(); } public override void Terminate() { _socket?.Dispose(); } public override void SetPower(float val) { var power = !_scEnableCalibration.BoolValue ? val : CalibrationData(val, true); SendCmd(Mode.Write, CometRFCommand.PowerSetPoint, (int)power * 1000); } public override bool SetPowerOnOff(bool on, out string str) { str = ""; _isPowerOn = on; SendCmd(Mode.Write, CometRFCommand.Command, on ? 1 : 0); return true; } public override bool SetMatchingAutoMode(bool on, out string str) { str = ""; SendCmd(Mode.Write, CometRFCommand.MatchingAutoMode, on ? 1 : 0); return true; } public override bool SetMatchPosition(double c1, double c2, out string reason) { reason = string.Empty; if (c1 <= 100 && c1 >= 0) { double c1FilterValue = c1; SendCmd(Mode.Write, CometRFCommand.CLoadRefPosition, (int)(c1 * 10)); reason = string.Format("Match position C1 set to {0} ", c1FilterValue); } if (c2 <= 100 && c2 >= 0) { double c2FilterValue = c2; SendCmd(Mode.Write, CometRFCommand.CTuneRefPosition, (int)(c2 * 10)); reason += string.Format("Match position C2 set to {0}", c2FilterValue); } return true; } private bool SendCmd(Mode mode, int command, int argument) { if (command == CometRFCommand.Command && argument == 0) { EV.PostInfoLog(Module, $"Bias Generator send RF off"); } else if (command == CometRFCommand.Command && argument == 1) { EV.PostInfoLog(Module, $"Bias Generator send RF on"); } if (command == CometRFCommand.PowerSetPoint) { EV.PostInfoLog(Module, $"Bias Generator send [{argument / 1000}W]"); } _currentCommandNumber = command; _sendData = BuildMessage(mode, command, argument); _socket?.Write(_sendData); Thread.Sleep(100); return true; } protected byte[] BuildMessage(Mode mode, int commandNumber, int argumentNumber) { List buffer = new List(); TransactionNumber++; if (TransactionNumber > 65535) TransactionNumber = 0; buffer.Add(BitConverter.GetBytes(TransactionNumber)[1]); buffer.Add(BitConverter.GetBytes(TransactionNumber)[0]); buffer.Add(ProtocolIdentifierHighByte); buffer.Add(ProtocolIdentifierLowByte); if (mode == Mode.Read) { buffer.Add(0x00); buffer.Add(0x06); } else if (mode == Mode.Write) { buffer.Add(0x00); buffer.Add(0x08); } buffer.Add(_address); if (mode == Mode.Read) { buffer.Add(_read); } else if (mode == Mode.Write) { buffer.Add(_write); } buffer.Add(BitConverter.GetBytes(commandNumber)[1]); buffer.Add(BitConverter.GetBytes(commandNumber)[0]); if (mode == Mode.Read) { buffer.Add(0x00); buffer.Add(0x01); } else if (mode == Mode.Write) { buffer.Add(BitConverter.GetBytes(argumentNumber)[3]); buffer.Add(BitConverter.GetBytes(argumentNumber)[2]); buffer.Add(BitConverter.GetBytes(argumentNumber)[1]); buffer.Add(BitConverter.GetBytes(argumentNumber)[0]); } //byte checkSum = 0; //for (int i = 0; i < buffer.Count; i++) //{ // checkSum += buffer[i]; //} //buffer.Add(checkSum); return buffer.ToArray(); } public int BytesToInt(byte[] src, int offset) { int value; value = (int)((src[offset + 3] & 0xFF) | ((src[offset + 2] & 0xFF) << 8) | ((src[offset + 1] & 0xFF) << 16) | ((src[offset] & 0xFF) << 24)); return value; } public byte[] IntToBytes(int value) { byte[] src = new byte[4]; src[0] = (byte)((value >> 24) & 0xFF); src[1] = (byte)((value >> 16) & 0xFF); src[2] = (byte)((value >> 8) & 0xFF); src[3] = (byte)(value & 0xFF); return src; } public override void Reset() { _rfOnTrigger.RST = true; _ErrTrigger.RST = true; _trigPMNeeded.RST = true; SendCmd(Mode.Write, CometRFCommand.Command, 9); Status = GeneratorStatus.OFF; StatusMatch = GeneratorStatus.OFF; _exceuteErr = false; if (_commErr) { Connect(); } } ~CometRF() { _socket?.Dispose(); } } }