AdTecRF.cs 31 KB

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