| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047 | 
							- using System;
 
- using System.Collections;
 
- using System.Collections.Generic;
 
- using System.IO;
 
- using System.Text;
 
- using System.Text.RegularExpressions;
 
- using System.Threading;
 
- using Aitex.Core.Common.DeviceData;
 
- using Aitex.Core.RT.ConfigCenter;
 
- 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 Aitex.Sorter.Common;
 
- using MECF.Framework.Common.Communications;
 
- using MECF.Framework.Common.DataCenter;
 
- using MECF.Framework.Common.Device.Bases;
 
- using MECF.Framework.Common.Equipment;
 
- using VirgoCommon;
 
- using VirgoRT.Modules;
 
- namespace VirgoRT.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 _checkerReflectPower;
 
-         private readonly DIAccessor _diIntlk;
 
-         private bool _isPowerOn;
 
-         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 address) : base(mod.ToString(), VirgoDevice.BiasRf.ToString())
 
-         {
 
-             if (SC.GetValue<bool>("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");
 
-             _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}.BiasRfOnTime");
 
-             _timerQueryStatus.Start(QUERY_INTERVAL);
 
-             _checkerPower = new ToleranceChecker(_scPowerAlarmTime.DoubleValue);
 
-             _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);
 
-             });
 
-             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));
 
-             DATA.Subscribe($"{Module}.BiasMatch.C1", () => CLoad);
 
-             DATA.Subscribe($"{Module}.BiasMatch.C2", () => CTune);
 
-             DATA.Subscribe($"{Module}.BiasMatch.VPP", () => VPP);
 
-             DATA.Subscribe($"{Module}.BiasMatch.MatchProcessMode", () => (int)MatchMode);
 
-             Reset();
 
-             SendCmd(Mode.Write, CometRFCommand.ControlMode, 0);
 
-             SendCmd(Mode.Write, CometRFCommand.MatchinMode, 2);
 
-             Thread thread = new Thread(() =>
 
-             {
 
-                 while (true)
 
-                 {
 
-                     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)
 
-                 {
 
-                     if (!SC.GetValue<bool>("System.IsSimulatorMode"))
 
-                         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);
 
-                 _checkerReflectPower.Reset(_scReflectPowerAlarmTime.DoubleValue);
 
-             }
 
-             if (_rfOnTrigger.M)
 
-             {
 
-                 TotalDays = _total + _timerTotal.GetElapseTime() / 1000 / 60 / 60;
 
-                 DaysFromLastPM = _fromLast + _timerFromLast.GetElapseTime() / 1000 / 60 / 60;
 
-                 _checkerPower.Monitor(ForwardPower, PowerSetPoint - _scPowerAlarmRange.DoubleValue, PowerSetPoint + _scPowerAlarmRange.DoubleValue, _scPowerAlarmTime.DoubleValue);
 
-                 if (_checkerPower.Trig)
 
-                 {
 
-                     EV.PostAlarmLog($"{Module}",
 
-                         $"{Display} Forward power {ForwardPower:0} out of range[{PowerSetPoint - _scPowerAlarmRange.DoubleValue:0},{PowerSetPoint + _scPowerAlarmRange.DoubleValue:0}] in {_scPowerAlarmTime.DoubleValue:0} seconds");
 
-                     SetPowerOnOff(false, out _);
 
-                 }
 
-                 _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}.BiasRfOnTime", $"{Module} 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<byte> buffer = new List<byte>();
 
-             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();
 
-         }
 
-     }
 
- }
 
 
  |