AdTecRF.cs 28 KB

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