AdTecRF.cs 32 KB

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