AdTecRF.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. using System;
  2. using System.Collections;
  3. using System.Text.RegularExpressions;
  4. using Aitex.Core.Common.DeviceData;
  5. using Aitex.Core.RT.DataCenter;
  6. using Aitex.Core.RT.Event;
  7. using Aitex.Core.RT.IOCore;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.RT.OperationCenter;
  10. using Aitex.Core.RT.SCCore;
  11. using Aitex.Core.RT.Tolerance;
  12. using Aitex.Core.Util;
  13. using MECF.Framework.Common.Communications;
  14. using MECF.Framework.Common.DataCenter;
  15. using MECF.Framework.Common.Device.Bases;
  16. using MECF.Framework.Common.Equipment;
  17. using VirgoCommon;
  18. using VirgoRT.Modules;
  19. namespace VirgoRT.Devices
  20. {
  21. #region RFG
  22. static class AdTecRfMessage
  23. {
  24. public static string ANALOG = "";
  25. public const string EOF = "\r";
  26. public const char DELIMITER = ' ';
  27. public const string MANUAL = "MANUAL";
  28. public const string RS232 = "***";
  29. public const string SET_POWER = " W";
  30. public const string RF_ON = "G";
  31. public const string RF_OFF = "S";
  32. public const string ERR_RES = "N";
  33. public const string QUERY = "Q";
  34. public const string RESET = "RESET";
  35. public const string CHK_COMM = "HS";
  36. }
  37. class AdTecGenerator : RfPowerBase
  38. {
  39. // ----------------------------Fields--------------------------
  40. //
  41. private int QUERY_INTERVAL = 5000;
  42. private const string INFO_PATTERN = @"(\d{7})\s(\d{5})\s(\d{5})\s(\d{5})\s(\d{5})";
  43. private double _total;
  44. private double _fromLast;
  45. private bool _cmdRFPowerOn = false;
  46. private readonly AsyncSerialPort _serial;
  47. private readonly DeviceTimer _timerQueryStatus = new DeviceTimer();
  48. private readonly DeviceTimer _timerTotal = new DeviceTimer();
  49. private readonly DeviceTimer _timerFromLast = new DeviceTimer();
  50. private readonly DeviceTimer _timerRFTurnOn = new DeviceTimer();
  51. private DateTime _powerOnStartTime;
  52. private TimeSpan _powerOnElapsedTime;
  53. private readonly RD_TRIG _rfOnTrigger = new RD_TRIG();
  54. private readonly R_TRIG _ErrTrigger = new R_TRIG();
  55. private readonly R_TRIG _trigPMNeeded = new R_TRIG();
  56. private readonly RD_TRIG _trigOnOff = new RD_TRIG();
  57. private ToleranceChecker _checkerPower;
  58. private ToleranceChecker _checkerReflectPower;
  59. private readonly double _scPowerAlarmTime;
  60. private readonly double _scPowerAlarmRange;
  61. private readonly double _scPowerWarningTime;
  62. private readonly double _scPowerWarningRange;
  63. private readonly double _scReflectPowerAlarmTime;
  64. private readonly double _scReflectPowerAlarmRange;
  65. private readonly double _scPowerRange;
  66. private readonly DIAccessor _diIntlk;
  67. private StatsDataItemRFAndPump _statRFOnTime;
  68. private object _lockerCmdPowerOn = new object();
  69. // --------------------------Properties------------------------
  70. //
  71. public bool ConnectedStatus { get; set; }
  72. public override float ScalePower => (float)_scPowerRange;
  73. public GeneratorStatus Status { get; set; }
  74. public string LastPMTime
  75. {
  76. get
  77. {
  78. return _statRFOnTime != null ? _statRFOnTime.LastPMTime.ToString() : "";
  79. }
  80. }
  81. public double DaysFromLastPM
  82. {
  83. get
  84. {
  85. return _statRFOnTime == null ? 0 : _statRFOnTime.fromLastPM;
  86. }
  87. set
  88. {
  89. if (_statRFOnTime != null)
  90. _statRFOnTime.fromLastPM = value;
  91. }
  92. }
  93. public double TotalDays
  94. {
  95. get
  96. {
  97. return _statRFOnTime != null ? _statRFOnTime.Total : 0;
  98. }
  99. set
  100. {
  101. if (_statRFOnTime != null)
  102. _statRFOnTime.Total = value;
  103. }
  104. }
  105. public double PMIntervalDays
  106. {
  107. get
  108. {
  109. return _statRFOnTime != null ? _statRFOnTime.PMInterval : 0;
  110. }
  111. }
  112. public bool IsPMNeeded
  113. {
  114. get
  115. {
  116. return DaysFromLastPM > PMIntervalDays;
  117. }
  118. }
  119. public bool EnableAlarm
  120. {
  121. get
  122. {
  123. return _statRFOnTime == null || _statRFOnTime.AlarmEnable;
  124. }
  125. }
  126. public override bool IsPowerOn
  127. {
  128. get => Status == GeneratorStatus.ON;
  129. set { }
  130. }
  131. public override bool IsError
  132. {
  133. get => Status == GeneratorStatus.ERROR;
  134. set { }
  135. }
  136. [Subscription("PowerOnTime")]
  137. public string PowerOnTime
  138. {
  139. get
  140. {
  141. if (_cmdRFPowerOn)
  142. _powerOnElapsedTime = DateTime.Now - _powerOnStartTime;
  143. return $"{(int)_powerOnElapsedTime.TotalHours:00}:{_powerOnElapsedTime.Minutes:00}:{(_powerOnElapsedTime.Seconds > 0 ? (_powerOnElapsedTime.Seconds + 1) : 0):00}";
  144. }
  145. }
  146. public bool RFInterlock => _diIntlk == null || _diIntlk.Value;
  147. private float _forwardPower;
  148. public override float ForwardPower
  149. {
  150. get
  151. {
  152. return _forwardPower;
  153. }
  154. set
  155. {
  156. _forwardPower = CalibrationData(value, false);
  157. }
  158. }
  159. public new AITRfData DeviceData =>
  160. new AITRfData
  161. {
  162. Module = Module,
  163. DeviceName = Name,
  164. ScalePower = ScalePower,
  165. ForwardPower = ForwardPower,
  166. ReflectPower = ReflectPower,
  167. IsRfOn = IsPowerOn,
  168. PowerSetPoint = PowerSetPoint,
  169. PowerOnElapsedTime = PowerOnTime,
  170. IsInterlockOk = RFInterlock,
  171. WorkMode = (int)RfMode.ContinuousWaveMode,
  172. DisplayName ="Source RF",
  173. };
  174. // --------------------------Constructor-----------------------
  175. //
  176. public AdTecGenerator(ModuleName mod, string name) : base(mod.ToString(), name)
  177. {
  178. var portNum = SC.GetStringValue($"{mod}.{name}.Port");
  179. this.Status = GeneratorStatus.Unknown;
  180. _serial = new AsyncSerialPort(portNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\r\r");
  181. _scPowerAlarmTime = SC.GetValue<double>($"{Module}.{Name}.PowerAlarmTime");
  182. _scPowerAlarmRange = SC.GetValue<double>($"{Module}.{Name}.PowerAlarmRange");
  183. _scPowerWarningTime = SC.GetValue<double>($"{Module}.{Name}.PowerWarningTime");
  184. _scPowerWarningRange = SC.GetValue<double>($"{Module}.{Name}.PowerWarningRange");
  185. _scReflectPowerAlarmTime = SC.GetValue<double>($"{Module}.{Name}.ReflectPowerAlarmTime");
  186. _scReflectPowerAlarmRange = SC.GetValue<double>($"{Module}.{Name}.ReflectPowerAlarmRange");
  187. _scPowerRange = SC.GetValue<double>($"{Module}.{Name}.PowerRange");
  188. _scEnableCalibration = SC.GetConfigItem($"{Module}.{Name}.EnableCalibration");
  189. _scCalibrationTable = SC.GetConfigItem($"{Module}.{Name}.CalibrationTable");
  190. _scRFPhysicalMaxPower = SC.GetConfigItem($"{Module}.{Name}.RFPhysicalMaxPower");
  191. _scCurrentRFMaxPower = SC.GetConfigItem($"{Module}.{Name}.CurrentRFMaxPower");
  192. _diIntlk = IO.DI[$"{Module}.DI_Generator_Hardware_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. EV.PostAlarmLog(this.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. EV.PostAlarmLog(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. EV.PostAlarmLog(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. EV.PostAlarmLog($"{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. EV.PostAlarmLog($"{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. lock (_lockerCmdPowerOn)
  347. {
  348. if (IsPowerOn)
  349. {
  350. _timerRFTurnOn.Stop();
  351. }
  352. else
  353. {
  354. if (_cmdRFPowerOn && _timerRFTurnOn.IsTimeout())
  355. {
  356. EV.PostAlarmLog($"{Module}", "RF Turn On Failed");
  357. _timerRFTurnOn.Stop();
  358. }
  359. }
  360. }
  361. base.Monitor();
  362. }
  363. public override void Terminate()
  364. {
  365. _serial?.Close();
  366. }
  367. public override void Reset()
  368. {
  369. _rfOnTrigger.RST = true;
  370. _ErrTrigger.RST = true;
  371. _trigPMNeeded.RST = true;
  372. this.SendCmd(AdTecRfMessage.RESET);
  373. this.Status = GeneratorStatus.OFF;
  374. _cmdRFPowerOn = false;
  375. base.Reset();
  376. }
  377. public void SetRfMode(RfMode mode)
  378. {
  379. throw new NotImplementedException();
  380. }
  381. public override void SetCommunicationMode(int mode)
  382. {
  383. CommunicationType t1 = (CommunicationType)mode;
  384. switch (t1)
  385. {
  386. case CommunicationType.Analogue:
  387. this.SendCmd(AdTecRfMessage.ANALOG);
  388. break;
  389. case CommunicationType.RS232:
  390. this.SendCmd(AdTecRfMessage.RS232);
  391. break;
  392. default:
  393. throw new ArgumentOutOfRangeException("Communication mode error");
  394. }
  395. }
  396. public override void SetPower(float val)
  397. {
  398. if (!(this.ControlMode == EnumRfPowerControlMode.RS232Mode)) SetCommunicationMode(1);
  399. ushort a = !_scEnableCalibration.BoolValue ? (ushort)val : (ushort)CalibrationData(val, true);
  400. if (SendCmd($"{a:D4}{AdTecRfMessage.SET_POWER}"))
  401. {
  402. PowerSetPoint = val;
  403. }
  404. }
  405. public override bool SetPowerOnOff(bool on, out string str)
  406. {
  407. if (!(this.ControlMode == EnumRfPowerControlMode.RS232Mode)) SetCommunicationMode(1);
  408. str = "";
  409. if (on)
  410. {
  411. lock (_lockerCmdPowerOn)
  412. {
  413. _cmdRFPowerOn = true;
  414. _timerRFTurnOn.Start(SC.GetValue<int>($"{Module}.Rf.RFTurnOnTimeout") * 1000);
  415. }
  416. SendCmd(AdTecRfMessage.RF_ON);
  417. QUERY_INTERVAL = 500;
  418. }
  419. else
  420. {
  421. lock (_lockerCmdPowerOn)
  422. {
  423. _cmdRFPowerOn = false;
  424. _timerRFTurnOn.Stop();
  425. }
  426. SendCmd(AdTecRfMessage.RF_OFF);
  427. ForwardPower = 0;
  428. ReflectPower = 0;
  429. QUERY_INTERVAL = 500;
  430. _ErrTrigger.RST = true;
  431. }
  432. _timerQueryStatus.Start(QUERY_INTERVAL);
  433. return true;
  434. }
  435. //----------------------------------Private Method-------------------------------
  436. //
  437. private void SerialPortDataReceived(string str)
  438. {
  439. if (string.IsNullOrEmpty(str))
  440. {
  441. EV.PostAlarmLog(Module, "AdTec RFG 无数据反馈");
  442. return;
  443. }
  444. string str2 = str.Trim('\r');
  445. if (str2 == AdTecRfMessage.ERR_RES)
  446. {
  447. EV.PostWarningLog(Module, $"AdTEC 收到 [{str2}]");
  448. return;
  449. }
  450. try
  451. {
  452. //LOG.Info($"{Module} Generator rec [{str2}]");
  453. Match match1 = Regex.Match(str2, INFO_PATTERN);
  454. if (!match1.Success)
  455. {
  456. //EV.PostWarningLog(Module, "AdTec 数据格式错误");
  457. if (!SC.GetValue<bool>("System.IsSimulatorMode"))
  458. {
  459. LOG.Write($"{Module}, AdTec 数据格式错误");
  460. }
  461. return;
  462. }
  463. string[] str1 =
  464. {
  465. match1.Groups[1].Value,
  466. match1.Groups[2].Value,
  467. match1.Groups[3].Value,
  468. match1.Groups[4].Value,
  469. match1.Groups[5].Value
  470. };
  471. this.ParseQueryData(str1);
  472. }
  473. catch (Exception ex)
  474. {
  475. LOG.Write(ex);
  476. }
  477. }
  478. private void ParseQueryData(string[] strInfo)
  479. {
  480. // Control mode
  481. string s2 = strInfo[0].Substring(0, 1);
  482. this.ControlMode = (EnumRfPowerControlMode)Convert.ToUInt16(s2);
  483. // output mode
  484. string s0 = strInfo[0].Substring(1, 1);
  485. this.WorkMode = (EnumRfPowerWorkMode)Convert.ToUInt16(s0);
  486. // ON/OFF
  487. char s1 = strInfo[0][2];
  488. if (s1 == '1')
  489. {
  490. Status = GeneratorStatus.ON;
  491. }
  492. else if (s1 == '0')
  493. {
  494. Status = GeneratorStatus.OFF;
  495. }
  496. // error code
  497. string alarm = strInfo[0].Substring(5, 2);
  498. byte errCode = Convert.ToByte(alarm);
  499. _ErrTrigger.CLK = errCode > 0;
  500. if (_ErrTrigger.Q)
  501. {
  502. string code = errCode == 1 ? "Ref Over" :
  503. errCode == 2 ? "Ref Limit" :
  504. errCode == 3 ? "Cur Over" :
  505. errCode == 4 ? "Cur Limit" :
  506. errCode == 5 ? "Temp Over" :
  507. errCode == 6 ? "Temp Sensor Short" :
  508. errCode == 7 ? "Temp Sensor Open" :
  509. errCode == 8 ? "Sensor Error" :
  510. errCode == 9 ? "Fwd Power Over" :
  511. errCode == 10 ? "RF ON Timer" :
  512. errCode == 11 ? "RS232C error" :
  513. errCode == 12 ? "Amp Unbalance" :
  514. errCode == 14 ? "Fan error" :
  515. errCode == 15 ? "Coolant Error" :
  516. errCode == 16 ? "Voltage Error" :
  517. errCode == 17 ? "Fwd Power Down" :
  518. errCode == 22 ? "PD Over" :
  519. errCode == 23 ? "PD Limit" :
  520. errCode == 26 ? "Dew Condensation" :
  521. errCode == 29 ? "SW Failure" :
  522. errCode == 99 ? "Safety Lock" : string.Empty;
  523. if (!string.IsNullOrEmpty(code))
  524. {
  525. if (errCode == 99)
  526. EV.PostInfoLog(Module, "Source Generator " + code);
  527. else
  528. EV.PostAlarmLog(Module, "Source Generator " + code);
  529. }
  530. }
  531. // forward power
  532. this.ForwardPower = Convert.ToUInt64(strInfo[2]);
  533. // reflect power
  534. this.ReflectPower = Convert.ToUInt64(strInfo[3]);
  535. }
  536. private void SerialPortErrorOccurred(string obj)
  537. {
  538. Status = GeneratorStatus.ERROR;
  539. EV.PostAlarmLog(Module, $"AdTec RFG 串口出错, [{obj}]");
  540. }
  541. private bool SendCmd(string str)
  542. {
  543. if (str != AdTecRfMessage.QUERY)
  544. {
  545. EV.PostInfoLog(Module, $"Generator send [{str}]");
  546. }
  547. return _serial.Write(str + "\r");
  548. }
  549. }
  550. #endregion RFG
  551. #region match
  552. static class AdTecMatchMessage
  553. {
  554. public const string PRESET = "G";
  555. public const string AUTO = "L";
  556. public const string MANUAL = "M";
  557. public const string PRESET_MEM = "P";
  558. public const string START_QUERY = "S3";
  559. public const string STOP_QUERY = "SP";
  560. public const string WRITE_POS = "$APGR";
  561. public const string READ_POS = "$APRR";
  562. }
  563. class AdTecMatch : RfMatchBase
  564. {
  565. // ----------------------------Fields--------------------------
  566. //
  567. private readonly AsyncSerialPort _serial;
  568. private const ushort S3_HEAD_LENGTH = 2;
  569. private readonly DeviceTimer _timerQueryStatus = new DeviceTimer();
  570. private int QUERY_INTERVAL = 1000;
  571. //private int _scMatchPresetMode;
  572. //private int _scMatchMode;
  573. //private readonly SCConfigItem _scMatchPositionC1;
  574. //private readonly SCConfigItem _scMatchPositionC2;
  575. //private readonly bool _scEnableC1C2Position;
  576. // --------------------------Properties------------------------
  577. //
  578. [Subscription("MatchWorkMode")]
  579. public EnumRfMatchTuneMode WorkMode { get; set; }
  580. public float C1 { get; set; }
  581. public float C2 { get; set; }
  582. [Subscription("VPP")]
  583. public ushort VPP { get; set; }
  584. public override AITRfMatchData DeviceData
  585. {
  586. get
  587. {
  588. return new AITRfMatchData
  589. {
  590. };
  591. }
  592. }
  593. // --------------------------Constructor-----------------------
  594. //
  595. public AdTecMatch(ModuleName mod,string name) : base(mod.ToString(), name)
  596. {
  597. string test = VirgoDevice.Match.ToString();
  598. var portNum = SC.GetStringValue($"{mod}.{name}.Port");
  599. //if (name == "Match")
  600. //{
  601. // portNum = SC.GetStringValue($"{mod}.match.Port");
  602. //}
  603. _serial = new AsyncSerialPort(portNum, 9600, 8);
  604. EV.PostInfoLog(Module, $"Get 射频 {name},{portNum}");
  605. //_scMatchPresetMode = SC.GetValue<int>($"{Module}.Rf.PresetMode");
  606. //_scMatchMode = SC.GetValue<int>($"{Module}.Rf.MatchMode");
  607. //_scMatchPositionC1 = SC.GetConfigItem($"{Module}.Rf.MatchPositionC1");
  608. //_scMatchPositionC2 = SC.GetConfigItem($"{Module}.Rf.MatchPositionC2");
  609. //_scEnableC1C2Position = SC.GetValue<bool>($"{Module}.Rf.EnableC1C2Position");
  610. }
  611. ~AdTecMatch()
  612. {
  613. _serial?.Close();
  614. }
  615. public override bool Initialize()
  616. {
  617. base.Initialize();
  618. if (_serial.Open())
  619. {
  620. _serial.OnDataChanged += SerialPortDataReceived;
  621. _serial.OnErrorHappened += SerialPortErrorOccurred;
  622. }
  623. else
  624. {
  625. EV.PostAlarmLog(Module, "Match 串口无法打开");
  626. return false;
  627. }
  628. DATA.Subscribe($"{Module}.{Name}.C1", () => TunePosition1);
  629. DATA.Subscribe($"{Module}.{Name}.C2", () => TunePosition2);
  630. OP.Subscribe($"{Module}.{Name}.SetC1", (func, args) =>
  631. {
  632. return true;
  633. });
  634. OP.Subscribe($"{Module}.{Name}.SetC2", (func, args) =>
  635. {
  636. return true;
  637. });
  638. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) =>
  639. {
  640. SetMatchPositionC1((float)Convert.ToDouble(param[0]), out reason);
  641. return true;
  642. });
  643. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) =>
  644. {
  645. SetMatchPositionC2((float)Convert.ToDouble(param[0]), out reason);
  646. return true;
  647. });
  648. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) =>
  649. {
  650. SetMatchPosition((float)Convert.ToDouble(param[0]), (float)Convert.ToDouble(param[1]), out reason);
  651. return true;
  652. });
  653. EV.PostInfoLog(Module, $"注册 {Name}的SetMatchProcessMode");
  654. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
  655. {
  656. SetMatchMode((string)param[0], out reason);
  657. return true;
  658. });
  659. _timerQueryStatus.Start(QUERY_INTERVAL);
  660. this.SendCmd(AdTecMatchMessage.START_QUERY);
  661. return true;
  662. }
  663. public override void Monitor()
  664. {
  665. try
  666. {
  667. if (_timerQueryStatus.IsTimeout())
  668. {
  669. this.SendCmd(AdTecMatchMessage.READ_POS);
  670. _timerQueryStatus.Start(QUERY_INTERVAL);
  671. }
  672. }
  673. catch (Exception ex)
  674. {
  675. LOG.Write(ex);
  676. }
  677. }
  678. public override void Terminate()
  679. {
  680. this.SendCmd(AdTecMatchMessage.STOP_QUERY);
  681. }
  682. public override void Reset()
  683. {
  684. //SendCmd(AdTecMatchMessage.STOP_QUERY);
  685. }
  686. /// <summary>
  687. ///
  688. /// </summary>
  689. /// <param name="c1,c2">百分比数字</param>
  690. /// <param name="c2"></param>
  691. public override void SetMatchPosition(float c1, float c2, out string reason)
  692. {
  693. //base.SetMatchPosition(c1, c2, out reason);
  694. ////this.SetWorkMode(EnumRfMatchTuneMode.Manual);
  695. //this.SetPosition(c1, c2);
  696. ////this.SetPresetMemory(0);
  697. ////this.SetWorkMode(EnumRfMatchTuneMode.Auto);
  698. //TunePosition1 = c1;
  699. //TunePosition2 = c2;
  700. reason = "";
  701. }
  702. public void SetPresetMode(RfMatchPresetMode mode)
  703. {
  704. }
  705. // -----------------------Private Method-------------------------
  706. //
  707. private void SerialPortDataReceived(string strOrg)
  708. {
  709. if (string.IsNullOrWhiteSpace(strOrg))
  710. {
  711. EV.PostAlarmLog(Module, "收到 Match 数据为空");
  712. return;
  713. }
  714. string[] sContent = strOrg.Split('\r');
  715. foreach (var sItem in sContent)
  716. {
  717. string sItem1 = sItem.TrimStart('\n');
  718. if (sItem1.Contains(AdTecMatchMessage.START_QUERY))
  719. {
  720. // BYTE 3,4; bit 7
  721. string s0 = sItem1.Substring(2 + S3_HEAD_LENGTH, 2);
  722. ushort status0 = Convert.ToUInt16(s0);
  723. byte[] a1 = BitConverter.GetBytes(status0);
  724. BitArray ba1 = new BitArray(a1);
  725. this.WorkMode = ba1[7] ? EnumRfMatchTuneMode.Manual : EnumRfMatchTuneMode.Auto;
  726. TuneMode1 = WorkMode;
  727. TuneMode2 = WorkMode;
  728. string sVpp = sItem1.Substring(42 + S3_HEAD_LENGTH, 3);
  729. this.VPP = Convert.ToUInt16(sVpp, 16);
  730. }
  731. else if (sItem1.Contains(AdTecMatchMessage.READ_POS))
  732. {
  733. string s1 = sItem1.Substring(5);
  734. string sLoad = s1.Substring(0, 3);
  735. string sPhase = s1.Substring(3, 3);
  736. this.TunePosition1 = Convert.ToUInt64(sLoad, 16) * 0.1f;
  737. this.TunePosition2 = Convert.ToUInt64(sPhase, 16) * 0.1f;
  738. }
  739. }
  740. }
  741. private void SerialPortErrorOccurred(string str)
  742. {
  743. EV.PostAlarmLog(Module, $"AdTec Match error [{str}]");
  744. }
  745. private void SendCmd(string str)
  746. {
  747. _serial?.Write(str + "\r");
  748. //EV.PostInfoLog(Module.ToString(), $"Match send [{str}]");
  749. }
  750. private void SetPosition(float c1val, float c2val)
  751. {
  752. ushort val1 = (ushort)(c1val * 10);
  753. ushort val2 = (ushort)(c2val * 10);
  754. string cmd = AdTecMatchMessage.WRITE_POS + val1.ToString("X3") + val2.ToString("X3");
  755. this.SendCmd(cmd);
  756. }
  757. private void SetWorkMode(EnumRfMatchTuneMode mode)
  758. {
  759. this.SendCmd(mode == EnumRfMatchTuneMode.Auto ? AdTecMatchMessage.AUTO :
  760. mode == EnumRfMatchTuneMode.Manual ? AdTecMatchMessage.MANUAL : "");
  761. }
  762. private void SetPresetMemory(byte gear)
  763. {
  764. this.SendCmd(AdTecMatchMessage.PRESET_MEM + gear.ToString());
  765. }
  766. }
  767. #endregion match
  768. }