| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853 | 
							- using System;
 
- using System.Collections;
 
- using System.Collections.Concurrent;
 
- using System.Text.RegularExpressions;
 
- 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;
 
- using VirgoCommon;
 
- using VirgoRT.Modules;
 
- namespace VirgoRT.Devices
 
- {
 
-     #region RFG
 
-     static class AdTecRfMessage
 
-     {
 
-         public static string ANALOG = "";
 
-         public const string EOF       = "\r";
 
-         public const char DELIMITER   = ' ';
 
-         public const string MANUAL    = "MANUAL";
 
-         public const string RS232     = "***";
 
-         public const string SET_POWER = " W";
 
-         public const string RF_ON     = "G";
 
-         public const string RF_OFF    = "S";
 
-         public const string ERR_RES   = "N";
 
-         public const string QUERY     = "Q";
 
-         public const string RESET     = "RESET";
 
-         public const string CHK_COMM  = "HS";
 
-     }
 
-     class AdTecGenerator : RfPowerBase
 
-     {
 
-         // ----------------------------Fields--------------------------
 
-         //
 
-         private int QUERY_INTERVAL = 5000;
 
-         private const string INFO_PATTERN = @"(\d{7})\s(\d{5})\s(\d{5})\s(\d{5})\s(\d{5})";
 
-         private double _total;
 
-         private double _fromLast;
 
-         private bool _bRFPowerOn = false;
 
-         private readonly AsyncSerialPort   _serial;
 
-         private readonly DeviceTimer       _timerQueryStatus = new DeviceTimer();
 
-         private readonly DeviceTimer       _timerTotal = new DeviceTimer();
 
-         private readonly DeviceTimer       _timerFromLast = new DeviceTimer();
 
-         private readonly DeviceTimer       _timerRFTurnOn = new DeviceTimer();
 
-         private DateTime                   _powerOnStartTime;
 
-         private TimeSpan                   _powerOnElapsedTime;
 
-         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 ToleranceChecker           _checkerPower;
 
-         private ToleranceChecker           _checkerReflectPower;
 
-         private readonly double            _scPowerAlarmTime;
 
-         private readonly double            _scPowerAlarmRange;
 
-         private readonly double            _scReflectPowerAlarmTime;
 
-         private readonly double            _scReflectPowerAlarmRange;
 
-         private readonly double            _scPowerRange;
 
-         private readonly DIAccessor        _diIntlk;
 
-         private StatsDataItemRFAndPump     _statRFOnTime;
 
-         // --------------------------Properties------------------------
 
-         //
 
-         public bool ConnectedStatus { get; set; }
 
-         public override float ScalePower => (float)_scPowerRange;
 
-         public GeneratorStatus Status { get; 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;
 
-             }
 
-         }
 
-         public override bool IsPowerOn
 
-         {
 
-             get => Status == GeneratorStatus.ON;
 
-             set { }
 
-         }
 
-         public override bool IsError 
 
-         {
 
-             get => Status == GeneratorStatus.ERROR;
 
-             set { }
 
-         }
 
-         [Subscription("PowerOnTime")]
 
-         public string PowerOnTime
 
-         {
 
-             get
 
-             {
 
-                 if (_bRFPowerOn)
 
-                     _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,
 
-                 WorkMode           = (int)RfMode.ContinuousWaveMode,
 
-                 DisplayName        ="Source RF",
 
-             };
 
-         // --------------------------Constructor-----------------------
 
-         //
 
-         public AdTecGenerator(ModuleName mod, string name = "Rf") : base(mod.ToString(), name)
 
-         {
 
-             var portNum = SC.GetStringValue($"{mod}.{name}.Port");
 
-             this.Status = GeneratorStatus.Unknown;
 
-             _serial = new AsyncSerialPort(portNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\r\r");
 
-             _scPowerAlarmTime         = SC.GetValue<double>($"{Module}.{Name}.PowerAlarmTime");
 
-             _scPowerAlarmRange        = SC.GetValue<double>($"{Module}.{Name}.PowerAlarmRange");
 
-             _scReflectPowerAlarmTime  = SC.GetValue<double>($"{Module}.{Name}.ReflectPowerAlarmTime");
 
-             _scReflectPowerAlarmRange = SC.GetValue<double>($"{Module}.{Name}.ReflectPowerAlarmRange");
 
-             _scPowerRange             = SC.GetValue<double>($"{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"];
 
-         }
 
-         ~AdTecGenerator()
 
-         {
 
-             _serial?.Close();
 
-         }
 
-         public override bool Initialize()
 
-         {
 
-             ClearSendOrReceiveLog();
 
-             base.Initialize();
 
-             DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
 
-             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;
 
-             });
 
-             if (_serial.Open())
 
-             {
 
-                 this.ConnectedStatus = true;
 
-                 _serial.OnDataChanged += SerialPortDataReceived;
 
-                 _serial.OnErrorHappened += SerialPortErrorOccurred;
 
-             }
 
-             else
 
-             {
 
-                 this.ConnectedStatus = false;
 
-                 EV.PostAlarmLog(this.Module, "Open AD TEC serial port failed");
 
-                 return false;
 
-             }
 
-             _statRFOnTime = StatsDataManager.Instance.GetItemRFAndPump($"{Module}.RfOnTime");
 
-             _timerQueryStatus.Start(QUERY_INTERVAL);
 
-             _checkerPower = new ToleranceChecker(_scPowerAlarmTime);
 
-             _checkerReflectPower = new ToleranceChecker(_scReflectPowerAlarmTime);
 
-             SetCommunicationMode(1);
 
-             return true;
 
-         }
 
-         public override void Monitor()
 
-         {
 
-             // 状态查询
 
-             if (_timerQueryStatus.IsTimeout())
 
-             {
 
-                 this.SendCmd(AdTecRfMessage.QUERY);
 
-                 _timerQueryStatus.Start(QUERY_INTERVAL);
 
-             }
 
-             // power on triggered
 
-             _rfOnTrigger.CLK = IsPowerOn;
 
-             if (_rfOnTrigger.R)
 
-             {
 
-                 _total = TotalDays;
 
-                 _fromLast = DaysFromLastPM;
 
-                 _timerTotal.Start(0);
 
-                 _timerFromLast.Start(0);
 
-                 _powerOnStartTime = DateTime.Now;
 
-                 _checkerPower.Reset(_scPowerAlarmTime);
 
-                 _checkerReflectPower.Reset(_scReflectPowerAlarmTime);
 
-             }
 
-             if (_rfOnTrigger.M)
 
-             {
 
-                 TotalDays = _total + _timerTotal.GetElapseTime() / 1000 / 60 / 60;
 
-                 DaysFromLastPM = _fromLast + _timerFromLast.GetElapseTime() / 1000 / 60 / 60;
 
-                 _checkerPower.Monitor(ForwardPower, PowerSetPoint - _scPowerAlarmRange, PowerSetPoint + _scPowerAlarmRange, _scPowerAlarmTime);
 
-                 if (_checkerPower.Trig)
 
-                 {
 
-                     EV.PostAlarmLog($"{Module}",
 
-                         $"{Display} Forward power {ForwardPower:0} out of range[{PowerSetPoint - _scPowerAlarmRange:0},{PowerSetPoint + _scPowerAlarmRange:0}] in {_scPowerAlarmTime:0} seconds");
 
-                     SetPowerOnOff(false, out _);
 
-                 }
 
-                 _checkerReflectPower.Monitor(ReflectPower, double.MinValue, _scReflectPowerAlarmRange, _scReflectPowerAlarmTime);
 
-                 if (_checkerReflectPower.Trig)
 
-                 {
 
-                     EV.PostAlarmLog($"{Module}",
 
-                         $"{Display} Reflect power {ReflectPower:0} out of range[0,{_scReflectPowerAlarmRange:0}] in {_scReflectPowerAlarmTime: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}.RfOnTime", $"{Module} RfOnTime", DaysFromLastPM, TotalDays);
 
-             if (!_rfOnTrigger.CLK)
 
-             {
 
-                 ForwardPower = 0;
 
-                 ReflectPower = 0;
 
-             }
 
-             // 通信 checking, 2 second 一次
 
-             //if (_timerComm.IsTimeout() && !_bQueryComm)
 
-             //{
 
-             //    this.SendCmd(AdTecRfMessage.CHK_COMM);
 
-             //    _bQueryComm = true;
 
-             //    _timerComm.Start(CHK_COMM_INTERVAL);
 
-             //}
 
-             // RF Turn On & Off Timeout Check
 
-             _trigOnOff.CLK = _bRFPowerOn;
 
-             if (_trigOnOff.R)   //RF Turn On
 
-             {
 
-                 _timerRFTurnOn.Start(SC.GetValue<int>($"{Module}.Rf.RFTurnOnTimeout") * 1000);
 
-             }
 
-             if (_trigOnOff.M)   //RF Stay On
 
-             {
 
-                 if (_timerRFTurnOn.IsTimeout())
 
-                 {
 
-                     if (!IsPowerOn)
 
-                     {
 
-                         string sendOrReceivelog = string.Join("\r", _sendOrReceiveLog.ToArray());
 
-                         ClearSendOrReceiveLog();
 
-                         EV.PostInfoLog(Module, $"RF Turn On Failed logs, {sendOrReceivelog}");
 
-                         EV.PostAlarmLog($"{Module}", "RF Turn On Failed");
 
-                         _timerRFTurnOn.Stop();
 
-                     }
 
-                 }
 
-             }
 
-             if (_trigOnOff.T)
 
-             {
 
-                 _timerRFTurnOn.Stop();
 
-             }
 
-             base.Monitor();
 
-         }
 
-         public override void Terminate()
 
-         {
 
-             _serial?.Close();
 
-         }
 
-         public override void Reset()
 
-         {
 
-             _rfOnTrigger.RST = true;
 
-             _ErrTrigger.RST = true;
 
-             _trigPMNeeded.RST = true;
 
-             this.SendCmd(AdTecRfMessage.RESET);
 
-             this.Status = GeneratorStatus.OFF;
 
-             _bRFPowerOn = false;
 
-             ClearSendOrReceiveLog();
 
-         }
 
-         public void SetRfMode(RfMode mode)
 
-         {
 
-             throw new NotImplementedException();
 
-         }
 
-         public override void SetCommunicationMode(int mode)
 
-         {
 
-             CommunicationType t1 = (CommunicationType)mode;
 
-             switch (t1)
 
-             {
 
-             case CommunicationType.Analogue:
 
-                 this.SendCmd(AdTecRfMessage.ANALOG);
 
-                 break;
 
-             case CommunicationType.RS232:
 
-                 this.SendCmd(AdTecRfMessage.RS232);
 
-                 break;
 
-             default:
 
-                 throw new ArgumentOutOfRangeException("Communication mode error");
 
-             }
 
-         }
 
-         public override void SetPower(float val)
 
-         {
 
-             if (!(this.ControlMode == EnumRfPowerControlMode.RS232Mode)) SetCommunicationMode(1);
 
-             ushort a = !_scEnableCalibration.BoolValue ? (ushort)val : (ushort)CalibrationData(val, true);
 
-             if (SendCmd($"{a:D4}{AdTecRfMessage.SET_POWER}"))
 
-             {
 
-                 PowerSetPoint = val;
 
-             }
 
-         }
 
-         public override bool SetPowerOnOff(bool on, out string str)
 
-         {
 
-             if (!(this.ControlMode == EnumRfPowerControlMode.RS232Mode)) SetCommunicationMode(1);
 
-             str = "";
 
-             if (on)
 
-             {
 
-                 SendCmd(AdTecRfMessage.RF_ON);
 
-                 _bRFPowerOn = true;
 
-                 _powerOnStartTime = DateTime.Now;
 
-                 QUERY_INTERVAL = 500;
 
-             }
 
-             else
 
-             {
 
-                 SendCmd(AdTecRfMessage.RF_OFF);
 
-                 _bRFPowerOn = false;
 
-                 ForwardPower = 0;
 
-                 ReflectPower = 0;
 
-                 QUERY_INTERVAL = 500;
 
-                 _ErrTrigger.RST = true;
 
-             }
 
-             _timerQueryStatus.Start(QUERY_INTERVAL);
 
-             return true;
 
-         }
 
-         //----------------------------------Private Method-------------------------------
 
-         //
 
-         private void SerialPortDataReceived(string str)
 
-         {
 
-             if (string.IsNullOrEmpty(str))
 
-             {
 
-                 EV.PostAlarmLog(Module, "AdTec RFG receive message is empty");
 
-                 return;
 
-             }
 
-             string str2 = str.Trim('\r');
 
-             AppendSendOrReceiveLog("Receive", str2);
 
-             if (str2 == AdTecRfMessage.ERR_RES)
 
-             {
 
-                 EV.PostWarningLog(Module, $"AdTEC receive [{str2}]");
 
-                 return;
 
-             }
 
-             try
 
-             {
 
-                 //LOG.Info($"{Module} Generator rec [{str2}]");
 
-                 Match match1 = Regex.Match(str2, INFO_PATTERN);
 
-                 if (!match1.Success)
 
-                 {
 
-                     //EV.PostWarningLog(Module, "AdTec 数据格式错误");
 
-                     if (!SC.GetValue<bool>("System.IsSimulatorMode"))
 
-                     {
 
-                         LOG.Write($"{Module}, AdTec data in wrong format");
 
-                     }
 
-                     return;
 
-                 }
 
-                 string[] str1 =
 
-                 {
 
-                     match1.Groups[1].Value,
 
-                     match1.Groups[2].Value,
 
-                     match1.Groups[3].Value,
 
-                     match1.Groups[4].Value,
 
-                     match1.Groups[5].Value
 
-                 };
 
-                 this.ParseQueryData(str1);
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 LOG.Write(ex);
 
-             }
 
-         }
 
-         private void ParseQueryData(string[] strInfo)
 
-         {
 
-             // Control mode
 
-             string s2 = strInfo[0].Substring(0, 1);
 
-             this.ControlMode = (EnumRfPowerControlMode)Convert.ToUInt16(s2);
 
-             // output mode
 
-             string s0 = strInfo[0].Substring(1, 1);
 
-             this.WorkMode = (EnumRfPowerWorkMode)Convert.ToUInt16(s0);
 
-             // ON/OFF
 
-             char s1 = strInfo[0][2];
 
-             if (s1 == '1')
 
-             {
 
-                 Status = GeneratorStatus.ON;
 
-             }
 
-             else if (s1 == '0')
 
-             {
 
-                 Status = GeneratorStatus.OFF;
 
-             }
 
-             // error code
 
-             string alarm = strInfo[0].Substring(5, 2);
 
-             byte errCode = Convert.ToByte(alarm);
 
-             _ErrTrigger.CLK = errCode > 0;
 
-             if (_ErrTrigger.Q)
 
-             {
 
-                 string code = errCode == 1 ? "Ref Over" : 
 
-                     errCode == 2 ? "Ref Limit" :
 
-                     errCode == 3 ? "Cur Over" :
 
-                     errCode == 4 ? "Cur Limit" :
 
-                     errCode == 5 ? "Temp Over" :
 
-                     errCode == 6 ? "Temp Sensor Short" :
 
-                     errCode == 7 ? "Temp Sensor Open" :
 
-                     errCode == 8 ? "Sensor Error" :
 
-                     errCode == 9 ? "Fwd Power Over" :
 
-                     errCode == 10 ? "RF ON Timer" :
 
-                     errCode == 11 ? "RS232C error" :
 
-                     errCode == 12 ? "Amp Unbalance" :
 
-                     errCode == 14 ? "Fan error" :
 
-                     errCode == 15 ? "Coolant Error" :
 
-                     errCode == 16 ? "Voltage Error" :
 
-                     errCode == 17 ? "Fwd Power Down" :
 
-                     errCode == 22 ? "PD Over" :
 
-                     errCode == 23 ? "PD Limit" :
 
-                     errCode == 26 ? "Dew Condensation" :
 
-                     errCode == 29 ? "SW Failure" :
 
-                     errCode == 99 ? "Safety Lock" : string.Empty;
 
-                 if (!string.IsNullOrEmpty(code))
 
-                 {
 
-                     if (errCode == 99)
 
-                         EV.PostInfoLog(Module, "Source Generator " + code);
 
-                     else
 
-                         EV.PostAlarmLog(Module, "Source Generator " + code);
 
-                 }
 
-             }
 
-             // forward power
 
-             this.ForwardPower = Convert.ToUInt64(strInfo[2]);
 
-             // reflect power
 
-             this.ReflectPower = Convert.ToUInt64(strInfo[3]);
 
-         }
 
-         private void SerialPortErrorOccurred(string obj)
 
-         {
 
-             Status = GeneratorStatus.ERROR;
 
-             EV.PostAlarmLog(Module, $"AdTec RFG serial port error, [{obj}]");
 
-         }
 
-         private ConcurrentQueue<string> _sendOrReceiveLog = new ConcurrentQueue<string>();
 
-         private void AppendSendOrReceiveLog(string sendOrRecFlag, string content)
 
-         {
 
-             var count = _sendOrReceiveLog.Count;
 
-             for (var i= count; i > 99; i--)
 
-             {
 
-                 _sendOrReceiveLog.TryDequeue(out string _);
 
-             }
 
-             _sendOrReceiveLog.Enqueue($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} {sendOrRecFlag} [{content}]");
 
-         }
 
-         private void ClearSendOrReceiveLog()
 
-         {
 
-             if(!_sendOrReceiveLog.IsEmpty)
 
-             {
 
-                 var count = _sendOrReceiveLog.Count;
 
-                 for (var i = 0; i < count; i++)
 
-                 {
 
-                     _sendOrReceiveLog.TryDequeue(out string _);
 
-                 }
 
-             }
 
-         }
 
-         private bool SendCmd(string str)
 
-         {
 
-             if (str != AdTecRfMessage.QUERY)
 
-             {
 
-                 EV.PostInfoLog(Module, $"Generator send [{str}]");
 
-             }
 
-             AppendSendOrReceiveLog("Send", str);
 
-             return _serial.Write(str + "\r");
 
-         }
 
-     }
 
-     #endregion RFG
 
-     #region match
 
-     static class AdTecMatchMessage
 
-     {
 
-         public const string PRESET       = "G";
 
-         public const string AUTO         = "L";
 
-         public const string MANUAL       = "M";
 
-         public const string PRESET_MEM   = "P";
 
-         public const string START_QUERY  = "S3";
 
-         public const string STOP_QUERY   = "SP";
 
-         public const string WRITE_POS    = "$APGR";
 
-         public const string READ_POS     = "$APRR";
 
-     }
 
-     class AdTecMatch : RfMatchBase
 
-     {
 
-         // ----------------------------Fields--------------------------
 
-         //
 
-         private readonly AsyncSerialPort _serial;
 
-         private const ushort             S3_HEAD_LENGTH = 2;
 
-         private readonly DeviceTimer     _timerQueryStatus = new DeviceTimer();
 
-         private int QUERY_INTERVAL = 1000;
 
-         //private int                      _scMatchPresetMode;
 
-         //private int                      _scMatchMode;
 
-         //private readonly SCConfigItem    _scMatchPositionC1;
 
-         //private readonly SCConfigItem    _scMatchPositionC2;
 
-         //private readonly bool            _scEnableC1C2Position;
 
-         // --------------------------Properties------------------------
 
-         //
 
-         [Subscription("MatchWorkMode")]
 
-         public EnumRfMatchTuneMode WorkMode { get; set; }
 
-         public float C1 { get; set; }
 
-         public float C2 { get; set; }
 
-         [Subscription("VPP")]
 
-         public ushort VPP { get; set; }
 
-         public override AITRfMatchData DeviceData
 
-         {
 
-             get
 
-             {
 
-                 return new AITRfMatchData
 
-                 {
 
-                 };
 
-             }
 
-         }
 
-         // --------------------------Constructor-----------------------
 
-         //
 
-         public AdTecMatch(ModuleName mod) : base(mod.ToString(), VirgoDevice.Match.ToString())
 
-         {
 
-             var portNum = SC.GetStringValue($"{mod}.match.Port");
 
-             _serial = new AsyncSerialPort(portNum, 9600, 8);
 
-             //_scMatchPresetMode    = SC.GetValue<int>($"{Module}.Rf.PresetMode");
 
-             //_scMatchMode          = SC.GetValue<int>($"{Module}.Rf.MatchMode");
 
-             //_scMatchPositionC1    = SC.GetConfigItem($"{Module}.Rf.MatchPositionC1");
 
-             //_scMatchPositionC2    = SC.GetConfigItem($"{Module}.Rf.MatchPositionC2");
 
-             //_scEnableC1C2Position = SC.GetValue<bool>($"{Module}.Rf.EnableC1C2Position");
 
-         }
 
-         ~AdTecMatch()
 
-         {
 
-             _serial?.Close();
 
-         }
 
-         public override bool Initialize()
 
-         {
 
-             base.Initialize();
 
-             if (_serial.Open())
 
-             {
 
-                 _serial.OnDataChanged += SerialPortDataReceived;
 
-                 _serial.OnErrorHappened += SerialPortErrorOccurred;
 
-             }
 
-             else
 
-             {
 
-                 EV.PostAlarmLog(Module, "Open match serial port failed");
 
-                 return false;
 
-             }
 
-             DATA.Subscribe($"{Module}.{Name}.C1", () => C1);
 
-             DATA.Subscribe($"{Module}.{Name}.C2", () => C2);
 
-             OP.Subscribe($"{Module}.{Name}.SetC1", (func, args) =>
 
-             {
 
-                 return true;
 
-             });
 
-             OP.Subscribe($"{Module}.{Name}.SetC2", (func, args) =>
 
-             {
 
-                 return true;
 
-             });
 
-             OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) =>
 
-             {
 
-                 SetMatchPositionC1((float)Convert.ToDouble(param[0]), out reason);
 
-                 return true;
 
-             });
 
-             OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) =>
 
-             {
 
-                 SetMatchPositionC2((float)Convert.ToDouble(param[0]), out reason);
 
-                 return true;
 
-             });
 
-             OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) =>
 
-             {
 
-                 SetMatchPosition((float)Convert.ToDouble(param[0]), (float)Convert.ToDouble(param[1]), out reason);
 
-                 return true;
 
-             });
 
-             OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
 
-             {
 
-                 SetMatchMode((string)param[0], out reason);
 
-                 return true;
 
-             });
 
-             _timerQueryStatus.Start(QUERY_INTERVAL);
 
-             this.SendCmd(AdTecMatchMessage.START_QUERY);
 
-             return true;
 
-         }
 
-         public override void Monitor()
 
-         {
 
-             try
 
-             {
 
-                 if (_timerQueryStatus.IsTimeout())
 
-                 {
 
-                     this.SendCmd(AdTecMatchMessage.READ_POS);
 
-                     _timerQueryStatus.Start(QUERY_INTERVAL);
 
-                 }
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 LOG.Write(ex);
 
-             }
 
-         }
 
-         public override void Terminate()
 
-         {
 
-             this.SendCmd(AdTecMatchMessage.STOP_QUERY);
 
-         }
 
-         public override void Reset()
 
-         {
 
-             //SendCmd(AdTecMatchMessage.STOP_QUERY);
 
-         }
 
-         /// <summary>
 
-         ///
 
-         /// </summary>
 
-         /// <param name="c1,c2">百分比数字</param>
 
-         /// <param name="c2"></param>
 
-         public override void SetMatchPosition(float c1, float c2, out string reason)
 
-         {
 
-             //base.SetMatchPosition(c1, c2, out reason);
 
-             ////this.SetWorkMode(EnumRfMatchTuneMode.Manual);
 
-             //this.SetPosition(c1, c2);
 
-             ////this.SetPresetMemory(0);
 
-             ////this.SetWorkMode(EnumRfMatchTuneMode.Auto);
 
-             //TunePosition1 = c1;
 
-             //TunePosition2 = c2;
 
-             reason = "";
 
-         }
 
-         public void SetPresetMode(RfMatchPresetMode mode)
 
-         {
 
-         }
 
-         // -----------------------Private Method-------------------------
 
-         //
 
-         private void SerialPortDataReceived(string strOrg)
 
-         {
 
-             if (string.IsNullOrWhiteSpace(strOrg))
 
-             {
 
-                 EV.PostAlarmLog(Module, "Match receive message is empty");
 
-                 return;
 
-             }
 
-             string[] sContent = strOrg.Split('\r');
 
-             foreach (var sItem in sContent)
 
-             {
 
-                 string sItem1 = sItem.TrimStart('\n');
 
-                 if (sItem1.Contains(AdTecMatchMessage.START_QUERY))
 
-                 {
 
-                     // BYTE 3,4; bit 7
 
-                     string s0 = sItem1.Substring(2 + S3_HEAD_LENGTH, 2);
 
-                     ushort status0 = Convert.ToUInt16(s0);
 
-                     byte[] a1 = BitConverter.GetBytes(status0);
 
-                     BitArray ba1 = new BitArray(a1);
 
-                     this.WorkMode = ba1[7] ? EnumRfMatchTuneMode.Manual : EnumRfMatchTuneMode.Auto;
 
-                     TuneMode1 = WorkMode;
 
-                     TuneMode2 = WorkMode;
 
-                     string sVpp = sItem1.Substring(42 + S3_HEAD_LENGTH, 3);
 
-                     this.VPP = Convert.ToUInt16(sVpp, 16);
 
-                 }
 
-                 else if (sItem1.Contains(AdTecMatchMessage.READ_POS))
 
-                 {
 
-                     string s1 = sItem1.Substring(5);
 
-                     string sLoad = s1.Substring(0, 3);
 
-                     string sPhase = s1.Substring(3, 3);
 
-                     this.TunePosition1 = Convert.ToUInt64(sLoad, 16) * 0.1f;
 
-                     this.TunePosition2 = Convert.ToUInt64(sPhase, 16) * 0.1f;
 
-                 }
 
-             }
 
-         }
 
-         private void SerialPortErrorOccurred(string str)
 
-         {
 
-             EV.PostAlarmLog(Module, $"AdTec Match error [{str}]");
 
-         }
 
-         private void SendCmd(string str)
 
-         {
 
-             _serial?.Write(str + "\r");
 
-             //EV.PostInfoLog(Module.ToString(), $"Match send [{str}]");
 
-         }
 
-         private void SetPosition(float c1val, float c2val)
 
-         {
 
-             ushort val1 = (ushort)(c1val * 10);
 
-             ushort val2 = (ushort)(c2val * 10);
 
-             string cmd = AdTecMatchMessage.WRITE_POS + val1.ToString("X3") + val2.ToString("X3");
 
-             this.SendCmd(cmd);
 
-         }
 
-         private void SetWorkMode(EnumRfMatchTuneMode mode)
 
-         {
 
-             this.SendCmd(mode == EnumRfMatchTuneMode.Auto ? AdTecMatchMessage.AUTO :
 
-                 mode == EnumRfMatchTuneMode.Manual ? AdTecMatchMessage.MANUAL : "");
 
-         }
 
-         private void SetPresetMemory(byte gear)
 
-         {
 
-             this.SendCmd(AdTecMatchMessage.PRESET_MEM + gear.ToString());
 
-         }
 
-     }
 
-     #endregion match
 
- }
 
 
  |