AdTecRF.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Collections;
  4. using System.Text.RegularExpressions;
  5. using Aitex.Core.Common.DeviceData;
  6. using Aitex.Core.RT.DataCenter;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.IOCore;
  9. using Aitex.Core.RT.Log;
  10. using Aitex.Core.RT.OperationCenter;
  11. using Aitex.Core.RT.SCCore;
  12. using Aitex.Core.RT.Tolerance;
  13. using Aitex.Core.Util;
  14. using Aitex.Core.RT.Device;
  15. using MECF.Framework.Common.Communications;
  16. using MECF.Framework.Common.DataCenter;
  17. using MECF.Framework.Common.Device.Bases;
  18. using MECF.Framework.Common.Equipment;
  19. using Venus_Core;
  20. using System.Collections.Generic;
  21. using System.Diagnostics;
  22. namespace Venus_RT.Devices
  23. {
  24. #region RFG
  25. static class AdTecRfMessage
  26. {
  27. public static string ANALOG = "";
  28. public const string EOF = "\r";
  29. public const char DELIMITER = ' ';
  30. public const string MANUAL = "MANUAL";
  31. public const string RS232 = "***";
  32. public const string SET_POWER = " W";
  33. public const string RF_ON = "G";
  34. public const string RF_OFF = "S";
  35. public const string ERR_RES = "N";
  36. public const string QUERY = "Q";
  37. public const string RESET = "RESET";
  38. public const string CHK_COMM = "HS";
  39. }
  40. class AdTecGenerator : RfPowerBase
  41. {
  42. // ----------------------------Fields--------------------------
  43. //
  44. private int QUERY_INTERVAL = 5000;
  45. private const string INFO_PATTERN = @"(\d{7})\s(\d{5})\s(\d{5})\s(\d{5})\s(\d{5})";
  46. private double _total;
  47. private double _fromLast;
  48. private bool _bRFPowerOn = false;
  49. private readonly AsyncSerialPort _serial;
  50. private readonly DeviceTimer _timerQueryStatus = new DeviceTimer();
  51. private readonly DeviceTimer _timerTotal = new DeviceTimer();
  52. private readonly DeviceTimer _timerFromLast = new DeviceTimer();
  53. private readonly DeviceTimer _timerRFTurnOn = new DeviceTimer();
  54. private DateTime _powerOnStartTime;
  55. private TimeSpan _powerOnElapsedTime;
  56. private readonly RD_TRIG _rfOnTrigger = new RD_TRIG();
  57. private readonly R_TRIG _ErrTrigger = new R_TRIG();
  58. private readonly R_TRIG _trigPMNeeded = new R_TRIG();
  59. private readonly RD_TRIG _trigOnOff = new RD_TRIG();
  60. private ToleranceChecker _checkerPower;
  61. private ToleranceChecker _checkerReflectPower;
  62. private readonly double _scPowerAlarmTime;
  63. private readonly double _scPowerAlarmRange;
  64. private readonly double _scPowerWarningTime;
  65. private readonly double _scPowerWarningRange;
  66. private readonly double _scReflectPowerAlarmTime;
  67. private readonly double _scReflectPowerAlarmRange;
  68. private readonly double _scPowerRange;
  69. private readonly DIAccessor _diIntlk;
  70. private StatsDataItemRFAndPump _statRFOnTime;
  71. private readonly VenusDevice _device;
  72. private Stopwatch monitorStopwatch = new Stopwatch();
  73. private JetPMBase _chamber;
  74. //private bool _intrtlockFlag = false;
  75. public bool ConnectedStatus { get; set; }
  76. public override float ScalePower => (float)_scPowerRange;
  77. public GeneratorStatus Status { get; set; }
  78. public string LastPMTime
  79. {
  80. get
  81. {
  82. return _statRFOnTime != null ? _statRFOnTime.LastPMTime.ToString() : "";
  83. }
  84. }
  85. public double DaysFromLastPM
  86. {
  87. get
  88. {
  89. return _statRFOnTime == null ? 0 : _statRFOnTime.fromLastPM;
  90. }
  91. set
  92. {
  93. if (_statRFOnTime != null)
  94. _statRFOnTime.fromLastPM = value;
  95. }
  96. }
  97. public double TotalDays
  98. {
  99. get
  100. {
  101. return _statRFOnTime != null ? _statRFOnTime.Total : 0;
  102. }
  103. set
  104. {
  105. if (_statRFOnTime != null)
  106. _statRFOnTime.Total = value;
  107. }
  108. }
  109. public double PMIntervalDays
  110. {
  111. get
  112. {
  113. return _statRFOnTime != null ? _statRFOnTime.PMInterval : 0;
  114. }
  115. }
  116. public bool IsPMNeeded
  117. {
  118. get
  119. {
  120. return DaysFromLastPM > PMIntervalDays;
  121. }
  122. }
  123. public bool EnableAlarm
  124. {
  125. get
  126. {
  127. return _statRFOnTime == null || _statRFOnTime.AlarmEnable;
  128. }
  129. }
  130. public override bool IsPowerOn
  131. {
  132. get => Status == GeneratorStatus.ON;
  133. set { }
  134. }
  135. public override bool IsError
  136. {
  137. get => Status == GeneratorStatus.ERROR;
  138. set { }
  139. }
  140. [Subscription("PowerOnTime")]
  141. public string PowerOnTime
  142. {
  143. get
  144. {
  145. if (_bRFPowerOn)
  146. _powerOnElapsedTime = DateTime.Now - _powerOnStartTime;
  147. return $"{(int)_powerOnElapsedTime.TotalHours:00}:{_powerOnElapsedTime.Minutes:00}:{(_powerOnElapsedTime.Seconds > 0 ? (_powerOnElapsedTime.Seconds + 1) : 0):00}";
  148. }
  149. }
  150. public bool RFInterlock => _diIntlk == null || _diIntlk.Value;
  151. private float _forwardPower;
  152. public override float ForwardPower
  153. {
  154. get
  155. {
  156. return _forwardPower;
  157. }
  158. set
  159. {
  160. _forwardPower = CalibrationData(value, false);
  161. }
  162. }
  163. public new AITRfData DeviceData =>
  164. new AITRfData
  165. {
  166. Module = Module,
  167. DeviceName = Name,
  168. ScalePower = ScalePower,
  169. ForwardPower = ForwardPower,
  170. ReflectPower = ReflectPower,
  171. IsRfOn = IsPowerOn,
  172. PowerSetPoint = PowerSetPoint,
  173. PowerOnElapsedTime = PowerOnTime,
  174. IsInterlockOk = RFInterlock,
  175. WorkMode = (int)RfMode.ContinuousWaveMode,
  176. DisplayName = "Source RF",
  177. };
  178. // --------------------------Constructor-----------------------
  179. //
  180. public AdTecGenerator(ModuleName mod, VenusDevice device) : base(mod.ToString(), device.ToString())
  181. {
  182. var portNum = SC.GetStringValue(device == VenusDevice.Rf ? $"{mod}.Rf.Port" : $"{mod}.BiasRf.Port");
  183. this.Status = GeneratorStatus.Unknown;
  184. _device = device;
  185. _serial = new AsyncSerialPort(portNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\r\r");
  186. _scPowerAlarmTime = SC.GetValue<double>($"{Module}.{Name}.PowerAlarmTime");
  187. _scPowerAlarmRange = SC.GetValue<double>($"{Module}.{Name}.PowerAlarmRange");
  188. _scPowerWarningTime = SC.GetValue<double>($"{Module}.{Name}.PowerWarningTime");
  189. _scPowerWarningRange = SC.GetValue<double>($"{Module}.{Name}.PowerWarningRange");
  190. _scReflectPowerAlarmTime = SC.GetValue<double>($"{Module}.{Name}.ReflectPowerAlarmTime");
  191. _scReflectPowerAlarmRange = SC.GetValue<double>($"{Module}.{Name}.ReflectPowerAlarmRange");
  192. _scPowerRange = SC.GetValue<double>($"{Module}.{Name}.PowerRange");
  193. _scEnableCalibration = SC.GetConfigItem($"{Module}.{Name}.EnableCalibration");
  194. _scCalibrationTable = SC.GetConfigItem($"{Module}.{Name}.CalibrationTable");
  195. _diIntlk = IO.DI[$"{Module}.DI_RF_Generator_Interlock"];
  196. SerachCommandList = new List<string>()
  197. {
  198. AdTecRfMessage.QUERY
  199. };
  200. intervalTime = 300;
  201. sendDataChangedEvent += SkyPump_sendDataChangedEvent;
  202. baseStopwatch.Start();
  203. baseTimer.Enabled = true;
  204. monitorStopwatch.Start();
  205. }
  206. private void SkyPump_sendDataChangedEvent(string obj)
  207. {
  208. var newstr = obj + "\r";
  209. _serial?.Write(newstr);
  210. }
  211. ~AdTecGenerator()
  212. {
  213. _serial?.Close();
  214. }
  215. public override bool Initialize()
  216. {
  217. base.Initialize();
  218. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  219. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetPowerOnOff}", (out string reason, int time, object[] param) =>
  220. {
  221. SetPowerOnOff(Convert.ToBoolean((string)param[0]), out reason);
  222. return true;
  223. });
  224. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetPower}", (out string reason, int time, object[] param) =>
  225. {
  226. reason = "";
  227. ushort val = Convert.ToUInt16(param[0]);
  228. SetPower(val);
  229. return true;
  230. });
  231. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetContinuousPower}", (out string reason, int time, object[] param) =>
  232. {
  233. reason = "";
  234. ushort val = Convert.ToUInt16(param[0]);
  235. SetPower(val);
  236. return true;
  237. });
  238. if (_serial.Open())
  239. {
  240. this.ConnectedStatus = true;
  241. _serial.OnDataChanged += SerialPortDataReceived;
  242. _serial.OnErrorHappened += SerialPortErrorOccurred;
  243. }
  244. else
  245. {
  246. this.ConnectedStatus = false;
  247. LOG.Write(eEvent.ERR_RF, Module, "AD TEC 射频发生器串口无法打开");
  248. return false;
  249. }
  250. _statRFOnTime = StatsDataManager.Instance.GetItemRFAndPump($"{Module}.RfOnTime");
  251. _timerQueryStatus.Start(QUERY_INTERVAL);
  252. _checkerPower = new ToleranceChecker(_scPowerAlarmTime);
  253. _checkerReflectPower = new ToleranceChecker(_scReflectPowerAlarmTime);
  254. SetCommunicationMode(1);
  255. return true;
  256. }
  257. public override void Monitor()
  258. {
  259. // 状态查询
  260. //if (_timerQueryStatus.IsTimeout())
  261. //{
  262. // this.SendCmd(AdTecRfMessage.QUERY);
  263. // _timerQueryStatus.Start(QUERY_INTERVAL);
  264. //}
  265. // power on triggered
  266. _rfOnTrigger.CLK = IsPowerOn;
  267. if (_rfOnTrigger.R)
  268. {
  269. _total = TotalDays;
  270. _fromLast = DaysFromLastPM;
  271. _timerTotal.Start(0);
  272. _timerFromLast.Start(0);
  273. _powerOnStartTime = DateTime.Now;
  274. _checkerPower.Reset(_scPowerAlarmTime);
  275. _checkerReflectPower.Reset(_scReflectPowerAlarmTime);
  276. _alarmChecker.Reset(_scPowerAlarmTime);
  277. _warningChecker.Reset(_scPowerWarningTime);
  278. }
  279. if (_rfOnTrigger.M)
  280. {
  281. TotalDays = _total + _timerTotal.GetElapseTime() / 1000 / 60 / 60;
  282. DaysFromLastPM = _fromLast + _timerFromLast.GetElapseTime() / 1000 / 60 / 60;
  283. //_checkerPower.Monitor(ForwardPower, PowerSetPoint - _scPowerAlarmRange, PowerSetPoint + _scPowerAlarmRange, _scPowerAlarmTime);
  284. //if (_checkerPower.Trig)
  285. //{
  286. // EV.PostAlarmLog($"{Module}",
  287. // $"{Display} Forward power {ForwardPower:0} out of range[{PowerSetPoint - _scPowerAlarmRange:0},{PowerSetPoint + _scPowerAlarmRange:0}] in {_scPowerAlarmTime:0} seconds");
  288. // SetPowerOnOff(false, out _);
  289. //}
  290. //if (_recipeAlarmRange > 0 && _recipeAlarmRange / 100.0 * PowerSetPoint < _scPowerAlarmRange)
  291. //{
  292. // if (_recipeIgnoreTimer.GetElapseTime() > _recipeIgnoreTimeMS)
  293. // {
  294. // _recipeAlarmChecker.Monitor(ForwardPower, PowerSetPoint - Math.Abs(_recipeAlarmRange / 100.0 * PowerSetPoint), PowerSetPoint + Math.Abs(_recipeAlarmRange / 100.0 * PowerSetPoint), _scPowerAlarmTime);
  295. // if (_recipeAlarmChecker.Trig)
  296. // {
  297. // LOG.Write(eEvent.ERR_RF, Module, Display + $" out of tolerance in {_scPowerAlarmTime:0} seconds");
  298. // SetPowerOnOff(false, out _);
  299. // }
  300. // }
  301. //}
  302. //else
  303. //{
  304. // _alarmChecker.Monitor(ForwardPower, PowerSetPoint - Math.Abs(_scPowerAlarmRange), PowerSetPoint + Math.Abs(_scPowerAlarmRange), _scPowerAlarmTime);
  305. // if (_alarmChecker.Trig)
  306. // {
  307. // LOG.Write(eEvent.ERR_RF, Module, Display + $" out of tolerance in {_scPowerAlarmTime:0} seconds");
  308. // }
  309. //}
  310. //if (_recipeWarningRange > 0 && _recipeWarningRange / 100.0 * PowerSetPoint < _scPowerWarningRange)
  311. //{
  312. // if (_recipeIgnoreTimer.GetElapseTime() > _recipeIgnoreTimeMS)
  313. // {
  314. // _recipeWarningChecker.Monitor(ForwardPower, PowerSetPoint - Math.Abs(_recipeWarningRange / 100.0 * PowerSetPoint), PowerSetPoint + Math.Abs(_recipeWarningRange / 100.0 * PowerSetPoint), _scPowerWarningTime);
  315. // if (_recipeWarningChecker.Trig)
  316. // {
  317. // EV.PostWarningLog(Module, Display + $" out of tolerance in {_scPowerWarningTime:0} seconds");
  318. // }
  319. // }
  320. //}
  321. //else
  322. //{
  323. // _warningChecker.Monitor(ForwardPower, PowerSetPoint - Math.Abs(_scPowerWarningRange), PowerSetPoint + Math.Abs(_scPowerWarningRange), _scPowerWarningTime);
  324. // if (_warningChecker.Trig)
  325. // {
  326. // EV.PostWarningLog(Module, Display + $" out of tolerance in {_scPowerWarningTime:0} seconds");
  327. // }
  328. //}
  329. //_checkerReflectPower.Monitor(ReflectPower, double.MinValue, _scReflectPowerAlarmRange, _scReflectPowerAlarmTime);
  330. //if (_checkerReflectPower.Trig)
  331. //{
  332. // LOG.Write(eEvent.ERR_RF, Module,
  333. // $"{Display} Reflect power {ReflectPower:0} out of range[0,{_scReflectPowerAlarmRange:0}] in {_scReflectPowerAlarmTime:0} seconds");
  334. // SetPowerOnOff(false, out _);
  335. //}
  336. }
  337. if (PMIntervalDays > 0)
  338. {
  339. _trigPMNeeded.CLK = IsPMNeeded;
  340. if (_trigPMNeeded.Q)
  341. {
  342. if (EnableAlarm)
  343. {
  344. LOG.Write(eEvent.ERR_RF, Module, "rf on time value larger than setting interval days");
  345. }
  346. }
  347. }
  348. if (_rfOnTrigger.T)
  349. StatsDataManager.Instance.Increase($"{Module}.RfOnTime", $"{Module} RfOnTime", DaysFromLastPM, TotalDays);
  350. if (!_rfOnTrigger.CLK)
  351. {
  352. ForwardPower = 0;
  353. ReflectPower = 0;
  354. }
  355. // 通信 checking, 2 second 一次
  356. //if (_timerComm.IsTimeout() && !_bQueryComm)
  357. //{
  358. // this.SendCmd(AdTecRfMessage.CHK_COMM);
  359. // _bQueryComm = true;
  360. // _timerComm.Start(CHK_COMM_INTERVAL);
  361. //}
  362. // RF Turn On & Off Timeout Check
  363. _trigOnOff.CLK = _bRFPowerOn;
  364. if (_trigOnOff.R) //RF Turn On
  365. {
  366. _timerRFTurnOn.Start(SC.GetValue<int>($"{Module}.Rf.RFTurnOnTimeout") * 1000);
  367. }
  368. if (_trigOnOff.M) //RF Stay On
  369. {
  370. if (_timerRFTurnOn.IsTimeout())
  371. {
  372. if (!IsPowerOn)
  373. {
  374. LOG.Write(eEvent.ERR_RF, Module, "RF Turn On Failed");
  375. _timerRFTurnOn.Stop();
  376. }
  377. }
  378. }
  379. if (_trigOnOff.T)
  380. {
  381. _timerRFTurnOn.Stop();
  382. }
  383. if (monitorStopwatch.ElapsedMilliseconds > 500)
  384. {
  385. if (IsPowerOn == true && _chamber != null && _chamber.RFInterlock(_device) == false)
  386. {
  387. SetPowerOnOff(false, out _);
  388. }
  389. monitorStopwatch.Restart();
  390. }
  391. base.Monitor();
  392. }
  393. public override void Terminate()
  394. {
  395. if (IsPowerOn)
  396. {
  397. SetPowerOnOff(false, out _);
  398. }
  399. _serial?.Close();
  400. }
  401. public override void Reset()
  402. {
  403. _rfOnTrigger.RST = true;
  404. _ErrTrigger.RST = true;
  405. _trigPMNeeded.RST = true;
  406. this.SendCmd(AdTecRfMessage.RESET);
  407. this.Status = GeneratorStatus.OFF;
  408. _bRFPowerOn = false;
  409. base.Reset();
  410. }
  411. public void SetRfMode(RfMode mode)
  412. {
  413. throw new NotImplementedException();
  414. }
  415. public override void SetCommunicationMode(int mode)
  416. {
  417. CommunicationType t1 = (CommunicationType)mode;
  418. switch (t1)
  419. {
  420. case CommunicationType.Analogue:
  421. this.SendCmd(AdTecRfMessage.ANALOG);
  422. break;
  423. case CommunicationType.RS232:
  424. this.SendCmd(AdTecRfMessage.RS232);
  425. break;
  426. default:
  427. throw new ArgumentOutOfRangeException("Communication mode error");
  428. }
  429. }
  430. public override void SetPower(float val)
  431. {
  432. if (!(this.ControlMode == EnumRfPowerControlMode.RS232Mode)) SetCommunicationMode(1);
  433. ushort a = !_scEnableCalibration.BoolValue ? (ushort)val : (ushort)CalibrationData(val, true);
  434. if (SendCmd($"{a:D4}{AdTecRfMessage.SET_POWER}"))
  435. {
  436. PowerSetPoint = val;
  437. }
  438. }
  439. public override bool SetPowerOnOff(bool on, out string str)
  440. {
  441. if (!(this.ControlMode == EnumRfPowerControlMode.RS232Mode)) SetCommunicationMode(1);
  442. str = "";
  443. _chamber = DEVICE.GetDevice<JetPMBase>(Module);
  444. if (on && !_chamber.CheckGeneratorAndHVInterlock(_device))
  445. {
  446. return false;
  447. }
  448. if (on)
  449. {
  450. SendCmd(AdTecRfMessage.RF_ON);
  451. _bRFPowerOn = true;
  452. _powerOnStartTime = DateTime.Now;
  453. QUERY_INTERVAL = 500;
  454. }
  455. else
  456. {
  457. SendCmd(AdTecRfMessage.RF_OFF);
  458. _bRFPowerOn = false;
  459. ForwardPower = 0;
  460. ReflectPower = 0;
  461. QUERY_INTERVAL = 500;
  462. _ErrTrigger.RST = true;
  463. }
  464. _timerQueryStatus.Start(QUERY_INTERVAL);
  465. return true;
  466. }
  467. //----------------------------------Private Method-------------------------------
  468. //
  469. private void SerialPortDataReceived(string str)
  470. {
  471. if (string.IsNullOrEmpty(str))
  472. {
  473. LOG.Write(eEvent.ERR_RF, Module, "AdTec RFG 无数据反馈");
  474. return;
  475. }
  476. string str2 = str.Trim('\r');
  477. if (str2 == AdTecRfMessage.ERR_RES)
  478. {
  479. LOG.Write(eEvent.WARN_RF, Module, $"AdTEC 收到 [{str2}]");
  480. return;
  481. }
  482. try
  483. {
  484. //LOG.Info($"{Module} Generator rec [{str2}]");
  485. Match match1 = Regex.Match(str2, INFO_PATTERN);
  486. if (!match1.Success)
  487. {
  488. //EV.PostWarningLog(Module, "AdTec 数据格式错误");
  489. if (!SC.GetValue<bool>("System.IsSimulatorMode"))
  490. {
  491. LOG.Write(eEvent.WARN_RF, Module, $"{Module}, AdTec 数据格式错误");
  492. }
  493. return;
  494. }
  495. string[] str1 =
  496. {
  497. match1.Groups[1].Value,
  498. match1.Groups[2].Value,
  499. match1.Groups[3].Value,
  500. match1.Groups[4].Value,
  501. match1.Groups[5].Value
  502. };
  503. this.ParseQueryData(str1);
  504. }
  505. catch (Exception ex)
  506. {
  507. LOG.WriteExeption(ex);
  508. }
  509. }
  510. private void ParseQueryData(string[] strInfo)
  511. {
  512. // Control mode
  513. string s2 = strInfo[0].Substring(0, 1);
  514. this.ControlMode = (EnumRfPowerControlMode)Convert.ToUInt16(s2);
  515. // output mode
  516. string s0 = strInfo[0].Substring(1, 1);
  517. this.WorkMode = (EnumRfPowerWorkMode)Convert.ToUInt16(s0);
  518. // ON/OFF
  519. char s1 = strInfo[0][2];
  520. if (s1 == '1')
  521. {
  522. Status = GeneratorStatus.ON;
  523. }
  524. else if (s1 == '0')
  525. {
  526. Status = GeneratorStatus.OFF;
  527. }
  528. // error code
  529. string alarm = strInfo[0].Substring(5, 2);
  530. byte errCode = Convert.ToByte(alarm);
  531. _ErrTrigger.CLK = errCode > 0;
  532. if (_ErrTrigger.Q)
  533. {
  534. string code = errCode == 1 ? "Ref Over" :
  535. errCode == 2 ? "Ref Limit" :
  536. errCode == 3 ? "Cur Over" :
  537. errCode == 4 ? "Cur Limit" :
  538. errCode == 5 ? "Temp Over" :
  539. errCode == 6 ? "Temp Sensor Short" :
  540. errCode == 7 ? "Temp Sensor Open" :
  541. errCode == 8 ? "Sensor Error" :
  542. errCode == 9 ? "Fwd Power Over" :
  543. errCode == 10 ? "RF ON Timer" :
  544. errCode == 11 ? "RS232C error" :
  545. errCode == 12 ? "Amp Unbalance" :
  546. errCode == 14 ? "Fan error" :
  547. errCode == 15 ? "Coolant Error" :
  548. errCode == 16 ? "Voltage Error" :
  549. errCode == 17 ? "Fwd Power Down" :
  550. errCode == 22 ? "PD Over" :
  551. errCode == 23 ? "PD Limit" :
  552. errCode == 26 ? "Dew Condensation" :
  553. errCode == 29 ? "SW Failure" :
  554. errCode == 99 ? "Safety Lock" : string.Empty;
  555. if (!string.IsNullOrEmpty(code))
  556. {
  557. if (errCode == 99)
  558. LOG.Write(eEvent.EV_DEVICE_INFO, Module, "Source Generator " + code);
  559. else
  560. LOG.Write(eEvent.ERR_RF, Module, "Source Generator " + code);
  561. }
  562. }
  563. // forward power
  564. this.ForwardPower = Convert.ToUInt64(strInfo[2]);
  565. // reflect power
  566. this.ReflectPower = Convert.ToUInt64(strInfo[3]);
  567. }
  568. private void SerialPortErrorOccurred(string obj)
  569. {
  570. Status = GeneratorStatus.ERROR;
  571. LOG.Write(eEvent.ERR_RF, Module, $"AdTec RFG 串口出错, [{obj}]");
  572. }
  573. private bool SendCmd(string str)
  574. {
  575. if (str != AdTecRfMessage.QUERY)
  576. {
  577. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Generator send [{str}]");
  578. }
  579. SetPointCommandQueue.Add(str);
  580. return true;
  581. //return _serial.Write(str + "\r");
  582. }
  583. }
  584. #endregion RFG
  585. #region match
  586. static class AdTecMatchMessage
  587. {
  588. public const string PRESET = "G";
  589. public const string AUTO = "L";
  590. public const string MANUAL = "M";
  591. public const string PRESET_MEM = "P";
  592. public const string START_QUERY = "S3";
  593. public const string STOP_QUERY = "SP";
  594. public const string WRITE_POS = "$APGR";
  595. public const string READ_POS = "$APRR";
  596. }
  597. class AdTecMatch : RfMatchBase
  598. {
  599. private readonly AsyncSerialPort _serial;
  600. private const ushort S3_HEAD_LENGTH = 2;
  601. private readonly DeviceTimer _timerQueryStatus = new DeviceTimer();
  602. private int QUERY_INTERVAL = 1000;
  603. //private int _scMatchPresetMode;
  604. //private int _scMatchMode;
  605. //private readonly SCConfigItem _scMatchPositionC1;
  606. //private readonly SCConfigItem _scMatchPositionC2;
  607. //private readonly bool _scEnableC1C2Position;
  608. // --------------------------Properties------------------------
  609. //
  610. [Subscription("MatchWorkMode")]
  611. public EnumRfMatchTuneMode WorkMode { get; set; }
  612. public float C1 { get; set; }
  613. public float C2 { get; set; }
  614. //[Subscription("VPP")]
  615. public ushort VPP { get; set; }
  616. public override AITRfMatchData DeviceData
  617. {
  618. get
  619. {
  620. return new AITRfMatchData
  621. {
  622. };
  623. }
  624. }
  625. // --------------------------Constructor-----------------------
  626. //
  627. public AdTecMatch(ModuleName mod, VenusDevice device) : base(mod.ToString(), device.ToString())
  628. {
  629. var portNum = SC.GetStringValue( $"{mod}.{device}.Port");
  630. _serial = new AsyncSerialPort(portNum, 9600, 8);
  631. //_scMatchPresetMode = SC.GetValue<int>($"{Module}.Rf.PresetMode");
  632. //_scMatchMode = SC.GetValue<int>($"{Module}.Rf.MatchMode");
  633. //_scMatchPositionC1 = SC.GetConfigItem($"{Module}.Rf.MatchPositionC1");
  634. //_scMatchPositionC2 = SC.GetConfigItem($"{Module}.Rf.MatchPositionC2");
  635. //_scEnableC1C2Position = SC.GetValue<bool>($"{Module}.Rf.EnableC1C2Position");
  636. SerachCommandList = new List<string>()
  637. {
  638. AdTecMatchMessage.READ_POS,
  639. AdTecMatchMessage.START_QUERY
  640. };
  641. intervalTime = 300;
  642. sendDataChangedEvent += AdTecMatch_sendDataChangedEvent;
  643. baseStopwatch.Start();
  644. baseTimer.Enabled = true;
  645. }
  646. private void AdTecMatch_sendDataChangedEvent(string obj)
  647. {
  648. this.SendCmd(obj);
  649. }
  650. ~AdTecMatch()
  651. {
  652. _serial?.Close();
  653. }
  654. public override bool Initialize()
  655. {
  656. base.Initialize();
  657. if (_serial.Open())
  658. {
  659. _serial.OnDataChanged += SerialPortDataReceived;
  660. _serial.OnErrorHappened += SerialPortErrorOccurred;
  661. }
  662. else
  663. {
  664. LOG.Write(eEvent.ERR_RF, Module, "Match 串口无法打开");
  665. return false;
  666. }
  667. DATA.Subscribe($"{Module}.{Name}.C1", () => TunePosition1);
  668. DATA.Subscribe($"{Module}.{Name}.C2", () => TunePosition2);
  669. DATA.Subscribe($"{Module}.{Name}.WorkMode", () => WorkMode.ToString());
  670. OP.Subscribe($"{Module}.{Name}.SetC1", (func, args) =>
  671. {
  672. return true;
  673. });
  674. OP.Subscribe($"{Module}.{Name}.SetC2", (func, args) =>
  675. {
  676. return true;
  677. });
  678. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) =>
  679. {
  680. SetMatchPositionC1((float)Convert.ToDouble(param[0]), out reason);
  681. return true;
  682. });
  683. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) =>
  684. {
  685. SetMatchPositionC2((float)Convert.ToDouble(param[0]), out reason);
  686. return true;
  687. });
  688. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) =>
  689. {
  690. SetMatchPosition((float)Convert.ToDouble(param[0]), (float)Convert.ToDouble(param[1]), out reason);
  691. return true;
  692. });
  693. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
  694. {
  695. SetMatchMode((string)param[0] == "Auto" ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual, out reason);
  696. return true;
  697. });
  698. _timerQueryStatus.Start(QUERY_INTERVAL);
  699. this.SendCmd(AdTecMatchMessage.START_QUERY);
  700. //LOG.Write(eEvent.ERR_RF, Module, "Initialize done.");
  701. return true;
  702. }
  703. public override void Monitor()
  704. {
  705. //try
  706. //{
  707. // if (_timerQueryStatus.IsTimeout())
  708. // {
  709. // this.SendCmd(AdTecMatchMessage.READ_POS);
  710. // _timerQueryStatus.Start(QUERY_INTERVAL);
  711. // }
  712. //}
  713. //catch (Exception ex)
  714. //{
  715. // LOG.WriteExeption(ex);
  716. //}
  717. }
  718. public override void Terminate()
  719. {
  720. this.SendCmd(AdTecMatchMessage.STOP_QUERY);
  721. }
  722. public override void Reset()
  723. {
  724. //SendCmd(AdTecMatchMessage.STOP_QUERY);
  725. }
  726. /// <summary>
  727. ///
  728. /// </summary>
  729. /// <param name="c1,c2">百分比数字</param>
  730. /// <param name="c2"></param>
  731. ///
  732. private void executeMatchPostion(float c1, float c2)
  733. {
  734. SetWorkMode(EnumRfMatchTuneMode.Manual);
  735. //await Task.Delay(200);
  736. SetPosition(c1, c2);
  737. //await Task.Delay(200);
  738. SetPresetMemory(0);
  739. //await Task.Delay(200);
  740. // SetWorkMode(EnumRfMatchTuneMode.Auto);
  741. }
  742. public override void SetMatchPosition(float c1, float c2, out string reason)
  743. {
  744. //LOG.Write(eEvent.WARN_RF, Module, $"AdTec Match error [{c1}, {c2}]");
  745. base.SetMatchPosition(c1, c2, out reason);
  746. executeMatchPostion(c1, c2);
  747. reason = "";
  748. }
  749. public void SetPresetMode(RfMatchPresetMode mode)
  750. {
  751. }
  752. // -----------------------Private Method-------------------------
  753. //
  754. private void SerialPortDataReceived(string strOrg)
  755. {
  756. if (string.IsNullOrWhiteSpace(strOrg))
  757. {
  758. LOG.Write(eEvent.ERR_RF, Module, "收到 Match 数据为空");
  759. return;
  760. }
  761. string[] sContent = strOrg.Split('\r');
  762. foreach (var sItem in sContent)
  763. {
  764. string sItem1 = sItem.TrimStart('\n');
  765. if (sItem1.Contains(AdTecMatchMessage.START_QUERY))
  766. {
  767. // BYTE 3,4; bit 7
  768. string s0 = sItem1.Substring(2 + S3_HEAD_LENGTH, 2);
  769. ushort status0 = Convert.ToUInt16(s0, 16);
  770. byte[] a1 = BitConverter.GetBytes(status0);
  771. BitArray ba1 = new BitArray(a1);
  772. this.WorkMode = ba1[7] ? EnumRfMatchTuneMode.Manual : EnumRfMatchTuneMode.Auto;
  773. TuneMode1 = WorkMode;
  774. TuneMode2 = WorkMode;
  775. string sVpp = sItem1.Substring(42 + S3_HEAD_LENGTH, 3);
  776. this.VPP = Convert.ToUInt16(sVpp, 16);
  777. }
  778. else if (sItem1.Contains(AdTecMatchMessage.READ_POS))
  779. {
  780. string s1 = sItem1.Substring(5);
  781. string sLoad = s1.Substring(0, 3);
  782. string sPhase = s1.Substring(3, 3);
  783. this.TunePosition1 = Convert.ToUInt64(sLoad, 16) * 0.1f;
  784. this.TunePosition2 = Convert.ToUInt64(sPhase, 16) * 0.1f;
  785. }
  786. }
  787. }
  788. private void SerialPortErrorOccurred(string str)
  789. {
  790. LOG.Write(eEvent.ERR_RF, Module, $"AdTec Match error [{str}]");
  791. }
  792. private void SendCmd(string str)
  793. {
  794. _serial?.Write(str + "\r");
  795. //EV.PostInfoLog(Module.ToString(), $"Match send [{str}]");
  796. }
  797. private void SetPosition(float c1val, float c2val)
  798. {
  799. ushort val1 = (ushort)(c1val * 10);
  800. ushort val2 = (ushort)(c2val * 10);
  801. string cmd = AdTecMatchMessage.WRITE_POS + val1.ToString("X3") + val2.ToString("X3");
  802. SetPointCommandQueue.Add(cmd);
  803. //this.SendCmd(cmd);
  804. }
  805. public override bool SetMatchMode(EnumRfMatchTuneMode enumRfMatchTuneMode, out string reason)
  806. {
  807. reason = string.Empty;
  808. SetWorkMode(enumRfMatchTuneMode);
  809. return true;
  810. }
  811. private void SetWorkMode(EnumRfMatchTuneMode mode)
  812. {
  813. string data = mode == EnumRfMatchTuneMode.Auto ? AdTecMatchMessage.AUTO :
  814. mode == EnumRfMatchTuneMode.Manual ? AdTecMatchMessage.MANUAL : "";
  815. //this.SendCmd(mode == EnumRfMatchTuneMode.Auto ? AdTecMatchMessage.AUTO :
  816. // mode == EnumRfMatchTuneMode.Manual ? AdTecMatchMessage.MANUAL : "");
  817. SetPointCommandQueue.Add(data);
  818. }
  819. private void SetPresetMemory(byte gear)
  820. {
  821. //this.SendCmd(AdTecMatchMessage.PRESET_MEM + gear.ToString());
  822. SetPointCommandQueue.Add(AdTecMatchMessage.PRESET_MEM + gear.ToString());
  823. }
  824. }
  825. #endregion match
  826. }