SerenRfMatchHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using Aitex.Core.Common.DeviceData;
  2. using MECF.Framework.Common.Communications;
  3. using System;
  4. using System.Linq;
  5. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFMatchs.Serens
  6. {
  7. public abstract class SerenRfMatchHandler : HandlerBase
  8. {
  9. public SerenRfMatch Device { get; }
  10. private string _command;
  11. protected SerenRfMatchHandler(SerenRfMatch device, string command)
  12. : base($"{command}\r")
  13. {
  14. Device = device;
  15. _command = command;
  16. }
  17. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  18. {
  19. SerenRfMatchMessage response = msg as SerenRfMatchMessage;
  20. ResponseMessage = msg;
  21. if (response.RawMessage.Length >= 1)
  22. {
  23. ParseData(response);
  24. }
  25. SetState(EnumHandlerState.Acked);
  26. SetState(EnumHandlerState.Completed);
  27. transactionComplete = true;
  28. return true;
  29. }
  30. protected virtual void ParseData(SerenRfMatchMessage msg)
  31. {
  32. }
  33. }
  34. public class SerenRfMatchGotoHandler : SerenRfMatchHandler
  35. {
  36. public SerenRfMatchGotoHandler(SerenRfMatch device)
  37. : base(device, "GOTO")
  38. {
  39. Name = $"Goto the preset position";
  40. }
  41. }
  42. public class SerenRfMatchSetLoadControlModeHandler : SerenRfMatchHandler
  43. {
  44. public SerenRfMatchSetLoadControlModeHandler(SerenRfMatch device, bool isAuto)
  45. : base(device, isAuto ? "ALD" : "MLD")
  46. {
  47. if (isAuto)
  48. {
  49. Name = $"Set Load Control Mode to Auto";
  50. }
  51. else
  52. {
  53. Name = $"Set Load Control Mode to Manual";
  54. }
  55. }
  56. }
  57. public class SerenRfMatchEnablePresetHandler : SerenRfMatchHandler
  58. {
  59. public SerenRfMatchEnablePresetHandler(SerenRfMatch device, bool enable)
  60. : base(device, enable ? "INT" : "OFF")
  61. {
  62. if (enable)
  63. {
  64. Name = $"Enable preset mode";
  65. }
  66. else
  67. {
  68. Name = $"Disable preset mode";
  69. }
  70. }
  71. }
  72. public class SerenRfMatchGetPresetHandler : SerenRfMatchHandler
  73. {
  74. public SerenRfMatchGetPresetHandler(SerenRfMatch device )
  75. : base(device, "QPMD" )
  76. {
  77. Name = $"Query preset mode";
  78. }
  79. protected override void ParseData(SerenRfMatchMessage msg)
  80. {
  81. if (msg.RawMessage.StartsWith("0"))
  82. Device.NotePresetMode("0");
  83. if (msg.RawMessage.StartsWith("1"))
  84. Device.NotePresetMode("1");
  85. if (msg.RawMessage.StartsWith("2"))
  86. Device.NotePresetMode("2");
  87. }
  88. }
  89. public class SerenRfMatchGetLoadControlModeHandler : SerenRfMatchHandler
  90. {
  91. public SerenRfMatchGetLoadControlModeHandler(SerenRfMatch device)
  92. : base(device, "QAML")
  93. {
  94. Name = $"Get Load Control Mode";
  95. }
  96. protected override void ParseData(SerenRfMatchMessage msg)
  97. {
  98. if (msg.RawMessage.StartsWith( "A"))
  99. Device.NoteLoadControlMode(true);
  100. if (msg.RawMessage.StartsWith("M"))
  101. Device.NoteLoadControlMode(false);
  102. }
  103. }
  104. public class SerenRfMatchSetTuneControlModeHandler : SerenRfMatchHandler
  105. {
  106. public SerenRfMatchSetTuneControlModeHandler(SerenRfMatch device, bool isAuto)
  107. : base(device, isAuto ? "ATN" : "MTN")
  108. {
  109. if (isAuto)
  110. {
  111. Name = $"Set Tune Control Mode to Auto";
  112. }
  113. else
  114. {
  115. Name = $"Set Tune Control Mode to Manual";
  116. }
  117. }
  118. }
  119. public class SerenRfMatchGetTuneControlModeHandler : SerenRfMatchHandler
  120. {
  121. public SerenRfMatchGetTuneControlModeHandler(SerenRfMatch device)
  122. : base(device, "QAMT")
  123. {
  124. Name = $"Get Tune Control Mode";
  125. }
  126. protected override void ParseData(SerenRfMatchMessage msg)
  127. {
  128. if (msg.RawMessage.StartsWith("A"))
  129. Device.NoteTuneControlMode(true);
  130. if (msg.RawMessage.StartsWith("M"))
  131. Device.NoteTuneControlMode(false);
  132. }
  133. }
  134. //Reports the current Load capacitor position, in Percent x10.
  135. public class SerenRfMatchGetLoadPositionHandler : SerenRfMatchHandler
  136. {
  137. public SerenRfMatchGetLoadPositionHandler(SerenRfMatch device )
  138. : base(device, device.MatchType=="ATS" ? "CPLP" : "LPS?")
  139. {
  140. Name = "Get Load Position";
  141. }
  142. protected override void ParseData(SerenRfMatchMessage msg)
  143. {
  144. if (Device.MatchType == "ATS")
  145. {
  146. if (int.TryParse(msg.RawMessage, out int pos))
  147. Device.NoteLoadPosition((float)(pos / 10.0));
  148. }
  149. else
  150. {
  151. if (int.TryParse(msg.RawMessage, out int pos))
  152. Device.NoteLoadPosition((float)(pos));
  153. }
  154. }
  155. }
  156. //Reports the current Tune capacitor position, in Percent x10.
  157. public class SerenRfMatchGetTunePositionHandler : SerenRfMatchHandler
  158. {
  159. public SerenRfMatchGetTunePositionHandler(SerenRfMatch device)
  160. : base(device, device.MatchType=="ATS" ? "CPTP" : "TPS?")
  161. {
  162. Name = "Get Tune Position";
  163. }
  164. protected override void ParseData(SerenRfMatchMessage msg)
  165. {
  166. if (Device.MatchType == "ATS")
  167. {
  168. if (int.TryParse(msg.RawMessage, out int pos))
  169. Device.NoteTunePosition((float)(pos / 10.0));
  170. }
  171. else
  172. {
  173. if (int.TryParse(msg.RawMessage, out int pos))
  174. Device.NoteTunePosition((float)(pos ));
  175. }
  176. }
  177. }
  178. //set load position
  179. public class SerenRfMatchSetLoadPositionHandler : SerenRfMatchHandler
  180. {
  181. public SerenRfMatchSetLoadPositionHandler(SerenRfMatch device, float position)
  182. : base(device, BuildMessage(position))
  183. {
  184. Name = "set load position";
  185. }
  186. private static string BuildMessage(float position)
  187. {
  188. position = Math.Max(0, Math.Min(100, position));
  189. int value = (int)(position * 10) ;
  190. return $"{value} MVLD";
  191. }
  192. }
  193. //set load preset1 position
  194. //Command syntax: X<sp>MPLS<CR>
  195. //Where: “X” is the desired Load Capacitor Preset Position 1 value, in percent.
  196. //1 to 5 digits, range: 0.0 to 100
  197. public class SerenRfMatchSetLoadPreset1PositionHandler : SerenRfMatchHandler
  198. {
  199. public SerenRfMatchSetLoadPreset1PositionHandler(SerenRfMatch device, float position)
  200. : base(device, BuildMessage(device, position))
  201. {
  202. Name = "set load preset1 position";
  203. }
  204. private static string BuildMessage(SerenRfMatch device, float position)
  205. {
  206. if (device.MatchType == "ATS")
  207. {
  208. position = Math.Max(0, Math.Min(100, position));
  209. return $"{position:F1} MPLS";
  210. }
  211. else
  212. {
  213. int pos = (int)Math.Max(2, Math.Min(98, position));
  214. return $"{pos} MPL";
  215. }
  216. }
  217. }
  218. public class SerenRfMatchGetLoadPreset1PositionHandler : SerenRfMatchHandler
  219. {
  220. public SerenRfMatchGetLoadPreset1PositionHandler(SerenRfMatch device)
  221. : base(device, "QP1L")
  222. {
  223. Name = "get load preset1 position";
  224. }
  225. protected override void ParseData(SerenRfMatchMessage msg)
  226. {
  227. if (int.TryParse(msg.RawMessage, out int data))
  228. Device.NoteLoadPresetPosition((float)(data/10.0));
  229. }
  230. }
  231. //set tune position
  232. public class SerenRfMatchSetTunePositionHandler : SerenRfMatchHandler
  233. {
  234. public SerenRfMatchSetTunePositionHandler(SerenRfMatch device, float position)
  235. : base(device, BuildMessage(device, position))
  236. {
  237. Name = "set tune position";
  238. }
  239. private static string BuildMessage(SerenRfMatch device, float position)
  240. {
  241. if (device.MatchType == "ATS")
  242. {
  243. position = Math.Max(0, Math.Min(100, position));
  244. int value = (int)(position * 10);
  245. return $"{value} MVTN";
  246. }
  247. else
  248. {
  249. float pos = (float)Math.Max(2, Math.Min(98, position));
  250. return $"{pos:F1} MPT";
  251. }
  252. }
  253. }
  254. public class SerenRfMatchSetTunePreset1PositionHandler : SerenRfMatchHandler
  255. {
  256. public SerenRfMatchSetTunePreset1PositionHandler(SerenRfMatch device, float position)
  257. : base(device, BuildMessage(device, position))
  258. {
  259. Name = "set tune preset position";
  260. }
  261. private static string BuildMessage(SerenRfMatch device, float position)
  262. {
  263. if (device.MatchType == "ATS")
  264. {
  265. position = Math.Max(0, Math.Min(100, position));
  266. return $"{position:F1} MPTS";
  267. }
  268. else
  269. {
  270. float pos = (float) Math.Max(2, Math.Min(98, position));
  271. return $"{pos:F1} MPT";
  272. }
  273. }
  274. }
  275. public class SerenRfMatchGetTunePreset1PositionHandler : SerenRfMatchHandler
  276. {
  277. public SerenRfMatchGetTunePreset1PositionHandler(SerenRfMatch device)
  278. : base(device, "QP1T")
  279. {
  280. Name = "get tune preset1 position";
  281. }
  282. protected override void ParseData(SerenRfMatchMessage msg)
  283. {
  284. if (int.TryParse(msg.RawMessage, out int data))
  285. Device.NoteTunePresetPosition((float)(data / 10.0));
  286. }
  287. }
  288. public class SerenRfMatchGetMagnitudeErrorHandler : SerenRfMatchHandler
  289. {
  290. public SerenRfMatchGetMagnitudeErrorHandler(SerenRfMatch device)
  291. : base(device, "MAG")
  292. {
  293. Name = "Get Magnitude Error";
  294. }
  295. protected override void ParseData(SerenRfMatchMessage msg)
  296. {
  297. if (int.TryParse(msg.RawMessage, out int error))
  298. Device.NoteMagnitudeError(error);
  299. }
  300. }
  301. public class SerenRfMatchGetPhaseErrorHandler : SerenRfMatchHandler
  302. {
  303. public SerenRfMatchGetPhaseErrorHandler(SerenRfMatch device)
  304. : base(device, "PHS")
  305. {
  306. Name = "Get Phase Error";
  307. }
  308. protected override void ParseData(SerenRfMatchMessage msg)
  309. {
  310. if (int.TryParse(msg.RawMessage, out int error))
  311. Device.NotePhaseError(error);
  312. }
  313. }
  314. public class SerenRfMatchGetDCVoltageHandler : SerenRfMatchHandler
  315. {
  316. public SerenRfMatchGetDCVoltageHandler(SerenRfMatch device)
  317. : base(device, device.MatchType=="ATS" ? "QDP":"0?")
  318. {
  319. Name = "Get DC Voltage";
  320. }
  321. protected override void ParseData(SerenRfMatchMessage msg)
  322. {
  323. if (int.TryParse(msg.RawMessage, out int voltage))
  324. Device.NoteDCVoltage(voltage);
  325. }
  326. }
  327. public class SerenRfMatchGetRFVoltageHandler : SerenRfMatchHandler
  328. {
  329. public SerenRfMatchGetRFVoltageHandler(SerenRfMatch device)
  330. : base(device, device.MatchType == "ATS" ? "QRP":"0?")
  331. {
  332. Name = "Get RF Voltage";
  333. }
  334. protected override void ParseData(SerenRfMatchMessage msg)
  335. {
  336. if (int.TryParse(msg.RawMessage, out int voltage))
  337. Device.NoteRFVoltage(voltage);
  338. }
  339. }
  340. }