AeRfMatch.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using Aitex.Core.Common.DeviceData;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.OperationCenter;
  8. using Aitex.Core.RT.SCCore;
  9. using Aitex.Core.Util;
  10. using MECF.Framework.Common.Communications;
  11. using MECF.Framework.Common.Device.Bases;
  12. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFMatchs.AE
  13. {
  14. public class AeRfMatch : RfMatchBase
  15. {
  16. public override EnumRfMatchTuneMode TuneMode1
  17. {
  18. get
  19. {
  20. return _statusData.Status2.Net1AutoMode ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual;
  21. }
  22. }
  23. public override EnumRfMatchTuneMode TuneMode2
  24. {
  25. get
  26. {
  27. return _statusData.Status2.Net2AutoMode ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual;
  28. }
  29. }
  30. public override float LoadPosition1
  31. {
  32. get { return _statusData.LoadPosi1; }
  33. }
  34. public override float LoadPosition2
  35. {
  36. get { return _statusData.LoadPosi2; }
  37. }
  38. public override float TunePosition1
  39. {
  40. get { return _statusData.TunePosi1; }
  41. }
  42. public override float TunePosition2
  43. {
  44. get { return _statusData.TunePosi2; }
  45. }
  46. public override float BiasPeak
  47. {
  48. get { return _statusData.BiasPeak; }
  49. }
  50. public override float DCBias
  51. {
  52. get { return _statusData.DCBias; }
  53. }
  54. public override AITRfMatchData DeviceData
  55. {
  56. get
  57. {
  58. AITRfMatchData data = new AITRfMatchData()
  59. {
  60. DeviceName = Name,
  61. DeviceSchematicId = DeviceID,
  62. DisplayName = Display,
  63. LoadPosition1 = LoadPosition1,
  64. LoadPosition2 = LoadPosition2,
  65. TunePosition1 = TunePosition1,
  66. TunePosition2 = TunePosition2,
  67. TuneMode1 = TuneMode1,
  68. TuneMode2 = TuneMode2,
  69. BiasPeak = BiasPeak,
  70. DCBias = DCBias,
  71. };
  72. return data;
  73. }
  74. }
  75. public AeRfMatchConnection Connection
  76. {
  77. get { return _connection; }
  78. }
  79. private AeRfMatchConnection _connection;
  80. private byte _deviceAddress ;
  81. private EnumRfMatchTuneMode _tuneMode1;
  82. private EnumRfMatchTuneMode _tuneMode2;
  83. private RD_TRIG _trigRfOnOff = new RD_TRIG();
  84. private int _presetNumber;
  85. private R_TRIG _trigError = new R_TRIG();
  86. private R_TRIG _trigWarningMessage = new R_TRIG();
  87. private R_TRIG _trigCommunicationError = new R_TRIG();
  88. private R_TRIG _trigRetryConnect = new R_TRIG();
  89. private PeriodicJob _thread;
  90. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  91. private object _locker = new object();
  92. private bool _enableLog = true;
  93. private AEStatusData _statusData;
  94. public AeRfMatch(string module, string name) : base(module, name)
  95. {
  96. }
  97. public override bool Initialize()
  98. {
  99. base.Initialize();
  100. string portName = SC.GetStringValue($"{ScBasePath}.{Name}.Address");
  101. int bautRate = SC.GetValue<int>($"{ScBasePath}.{Name}.BaudRate");
  102. int dataBits = SC.GetValue<int>($"{ScBasePath}.{Name}.DataBits");
  103. Enum.TryParse(SC.GetStringValue($"{ScBasePath}.{Name}.Parity"), out Parity parity);
  104. Enum.TryParse(SC.GetStringValue($"{ScBasePath}.{Name}.StopBits"), out StopBits stopBits);
  105. _deviceAddress = (byte)SC.GetValue<int>($"{ScBasePath}.{Name}.DeviceAddress");
  106. _enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  107. _connection = new AeRfMatchConnection(portName, bautRate, dataBits, parity, stopBits);
  108. _connection.EnableLog(_enableLog);
  109. if (_connection.Connect())
  110. {
  111. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  112. }
  113. _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  114. OP.Subscribe($"{Module}.{Name}.MatchMode1", (out string reason, int time, object[] args) =>
  115. {
  116. reason = "";
  117. if (!Enum.TryParse((string)args[0], out EnumRfMatchTuneMode mode))
  118. {
  119. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not mode, {args[0]} is not a valid mode value");
  120. return false;
  121. }
  122. if (!PerformMatchMode1(out reason, time, mode))
  123. {
  124. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  125. return false;
  126. }
  127. EV.PostInfoLog(Module, $"{Module}.{Name} set match mode1 to {mode}");
  128. return true;
  129. });
  130. OP.Subscribe($"{Module}.{Name}.SetMatchLoad1", (out string reason, int time, object[] args) =>
  131. {
  132. reason = "";
  133. float value = Convert.ToSingle((string)args[0]);
  134. if (!PerformSetMatchLoad1(out reason, time, value))
  135. {
  136. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  137. return false;
  138. }
  139. EV.PostInfoLog(Module, $"{Module}.{Name} set match load1 to {value}");
  140. return true;
  141. });
  142. OP.Subscribe($"{Module}.{Name}.SetMatchTune1", (out string reason, int time, object[] args) =>
  143. {
  144. reason = "";
  145. float value = Convert.ToSingle((string)args[0]);
  146. if (!PerformSetMatchTune1(out reason, time, value))
  147. {
  148. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  149. return false;
  150. }
  151. EV.PostInfoLog(Module, $"{Module}.{Name} set match tune1 to {value}");
  152. return true;
  153. });
  154. OP.Subscribe($"{Module}.{Name}.MatchMode2", (out string reason, int time, object[] args) =>
  155. {
  156. reason = "";
  157. if (!Enum.TryParse((string)args[0], out EnumRfMatchTuneMode mode))
  158. {
  159. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not mode, {args[0]} is not a valid mode value");
  160. return false;
  161. }
  162. if (!PerformMatchMode2(out reason, time, mode))
  163. {
  164. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  165. return false;
  166. }
  167. EV.PostInfoLog(Module, $"{Module}.{Name} set match mode2 to {mode}");
  168. return true;
  169. });
  170. OP.Subscribe($"{Module}.{Name}.SetMatchLoad2", (out string reason, int time, object[] args) =>
  171. {
  172. reason = "";
  173. float value = Convert.ToSingle((string)args[0]);
  174. if (!PerformSetMatchLoad2(out reason, time, value))
  175. {
  176. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  177. return false;
  178. }
  179. EV.PostInfoLog(Module, $"{Module}.{Name} set match load2 to {value}");
  180. return true;
  181. });
  182. OP.Subscribe($"{Module}.{Name}.SetMatchTune2", (out string reason, int time, object[] args) =>
  183. {
  184. reason = "";
  185. float value = Convert.ToSingle((string)args[0]);
  186. if (!PerformSetMatchTune2(out reason, time, value))
  187. {
  188. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  189. return false;
  190. }
  191. EV.PostInfoLog(Module, $"{Module}.{Name} set match tune2 to {value}");
  192. return true;
  193. });
  194. return true;
  195. }
  196. private bool PerformSetMatchTune1(out string reason, int time, float value)
  197. {
  198. reason = string.Empty;
  199. return true;
  200. }
  201. private bool PerformSetMatchLoad1(out string reason, int time, float value)
  202. {
  203. reason = string.Empty;
  204. return true;
  205. }
  206. private bool PerformMatchMode1(out string reason, int time, EnumRfMatchTuneMode mode)
  207. {
  208. reason = string.Empty;
  209. return true;
  210. }
  211. private bool PerformMatchMode2(out string reason, int time, EnumRfMatchTuneMode mode)
  212. {
  213. reason = string.Empty;
  214. return true;
  215. }
  216. private bool PerformSetMatchLoad2(out string reason, int time, float value)
  217. {
  218. reason = string.Empty;
  219. return true;
  220. }
  221. private bool PerformSetMatchTune2(out string reason, int time, float value)
  222. {
  223. reason = string.Empty;
  224. return true;
  225. }
  226. private bool OnTimer()
  227. {
  228. try
  229. {
  230. _connection.MonitorTimeout();
  231. if (!_connection.IsConnected || _connection.IsCommunicationError)
  232. {
  233. lock (_locker)
  234. {
  235. _lstHandler.Clear();
  236. }
  237. _trigRetryConnect.CLK = !_connection.IsConnected;
  238. if (_trigRetryConnect.Q)
  239. {
  240. _connection.SetPortAddress(SC.GetStringValue($"{ScBasePath}.{Name}.Address"));
  241. if (!_connection.Connect())
  242. {
  243. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  244. }
  245. }
  246. return true;
  247. }
  248. HandlerBase handler = null;
  249. if (!_connection.IsBusy)
  250. {
  251. lock (_locker)
  252. {
  253. if (_lstHandler.Count == 0)
  254. {
  255. _lstHandler.AddLast(new AeRfMatchQueryStatusHandler(this, _deviceAddress));
  256. }
  257. if (_lstHandler.Count > 0)
  258. {
  259. handler = _lstHandler.First.Value;
  260. _lstHandler.RemoveFirst();
  261. }
  262. }
  263. if (handler != null)
  264. {
  265. _connection.Execute(handler);
  266. }
  267. }
  268. }
  269. catch (Exception ex)
  270. {
  271. LOG.Write(ex);
  272. }
  273. return true;
  274. }
  275. public override void Monitor()
  276. {
  277. try
  278. {
  279. _connection.EnableLog(_enableLog);
  280. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  281. if (_trigCommunicationError.Q)
  282. {
  283. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  284. }
  285. }
  286. catch (Exception ex)
  287. {
  288. LOG.Write(ex);
  289. }
  290. }
  291. internal void NotePresetNumber(int presetNumber)
  292. {
  293. _presetNumber = presetNumber;
  294. }
  295. internal void NoteStatus(AEStatusData data)
  296. {
  297. _statusData = data;
  298. }
  299. public override void Reset()
  300. {
  301. _trigError.RST = true;
  302. _trigWarningMessage.RST = true;
  303. _connection.SetCommunicationError(false, "");
  304. _trigCommunicationError.RST = true;
  305. _enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  306. _trigRetryConnect.RST = true;
  307. base.Reset();
  308. }
  309. internal void NoteError(string reason)
  310. {
  311. _trigWarningMessage.CLK = true;
  312. if (_trigWarningMessage.Q)
  313. {
  314. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  315. }
  316. }
  317. public override void SetPreSetsAndTrajectories1(Presets presets)
  318. {
  319. lock (_locker)
  320. {
  321. _lstHandler.AddLast(new AeRfMatchSetPresetHandler(this, _deviceAddress, 1, presets));
  322. }
  323. }
  324. public override void SetPreSetsAndTrajectories2(Presets presets)
  325. {
  326. lock (_locker)
  327. {
  328. _lstHandler.AddLast(new AeRfMatchSetPresetHandler(this, _deviceAddress, 2, presets));
  329. }
  330. }
  331. public override void SetActivePresetNo1(int presetNumber)
  332. {
  333. lock (_locker)
  334. {
  335. _lstHandler.AddLast(new AeRfMatchSetActivePresetHandler(this, _deviceAddress, 1, presetNumber));
  336. }
  337. }
  338. public override void SetActivePresetNo2(int presetNumber)
  339. {
  340. lock (_locker)
  341. {
  342. _lstHandler.AddLast(new AeRfMatchSetActivePresetHandler(this, _deviceAddress, 2, presetNumber));
  343. }
  344. }
  345. public override void EnablePreset1(bool enable)
  346. {
  347. lock (_locker)
  348. {
  349. _lstHandler.AddLast(new AeRfMatchEnablePresetHandler(this, _deviceAddress, 1, enable));
  350. }
  351. }
  352. public override void EnablePreset2(bool enable)
  353. {
  354. lock (_locker)
  355. {
  356. _lstHandler.AddLast(new AeRfMatchEnablePresetHandler(this, _deviceAddress, 2, enable));
  357. }
  358. }
  359. public override void EnableCapacitorMove1(bool enable)
  360. {
  361. lock (_locker)
  362. {
  363. _lstHandler.AddLast(new AeRfMatchEnableCapMoveHandler(this, _deviceAddress, 1, enable));
  364. }
  365. }
  366. public override void EnableCapacitorMove2(bool enable)
  367. {
  368. lock (_locker)
  369. {
  370. _lstHandler.AddLast(new AeRfMatchEnableCapMoveHandler(this, _deviceAddress, 2, enable));
  371. }
  372. }
  373. public override void SetTuneMode1(EnumRfMatchTuneMode mode)
  374. {
  375. lock (_locker)
  376. {
  377. _lstHandler.AddLast(new AeRfMatchSetControlModeHandler(this, _deviceAddress, 1, mode));
  378. }
  379. }
  380. public override void SetTuneMode2(EnumRfMatchTuneMode mode)
  381. {
  382. lock (_locker)
  383. {
  384. _lstHandler.AddLast(new AeRfMatchSetControlModeHandler(this, _deviceAddress, 2, mode));
  385. }
  386. }
  387. public override void SetLoad1(float position)
  388. {
  389. lock (_locker)
  390. {
  391. _lstHandler.AddLast(new AeRfMatchSetLoadPositionHandler(this, _deviceAddress, 1, position));
  392. }
  393. }
  394. public override void SetLoad2(float position)
  395. {
  396. lock (_locker)
  397. {
  398. _lstHandler.AddLast(new AeRfMatchSetLoadPositionHandler(this, _deviceAddress, 2, position));
  399. }
  400. }
  401. public override void SetTune1(float position)
  402. {
  403. lock (_locker)
  404. {
  405. _lstHandler.AddLast(new AeRfMatchSetTunePositionHandler(this, _deviceAddress, 1, position));
  406. }
  407. }
  408. public override void SetTune2(float position)
  409. {
  410. lock (_locker)
  411. {
  412. _lstHandler.AddLast(new AeRfMatchSetTunePositionHandler(this, _deviceAddress, 2, position));
  413. }
  414. }
  415. }
  416. }