MksRfMatch.cs 16 KB

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