AdTecRF.cs 32 KB

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