AeRfMatchHandler.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Aitex.Core.Common.DeviceData;
  5. using MECF.Framework.Common.Communications;
  6. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFMatchs.AE
  7. {
  8. public abstract class AeRfMatchHandler : HandlerBase
  9. {
  10. public AeRfMatch Device { get; }
  11. private byte _address;
  12. private byte _command;
  13. protected AeRfMatchHandler(AeRfMatch device, byte address, byte command, byte[] data)
  14. : base(BuildMessage(address, command, data))
  15. {
  16. Device = device;
  17. _address = address;
  18. _command = command;
  19. }
  20. private static byte[] BuildMessage(byte address, byte command, byte[] data)
  21. {
  22. List<byte> buffer = new List<byte>();
  23. buffer.Add((byte)((address << 3) + (data == null ? 0 : data.Length)));
  24. buffer.Add(command);
  25. if (data != null && data.Length > 0)
  26. {
  27. buffer.AddRange(data);
  28. }
  29. buffer.Add(CalcSum(buffer, buffer.Count));
  30. return buffer.ToArray();
  31. }
  32. //host->unit, unit->host(ack), unit->host(csr), host->unit(ack)
  33. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  34. {
  35. AeRfMatchMessage response = msg as AeRfMatchMessage;
  36. ResponseMessage = msg;
  37. if (response.IsAck)
  38. {
  39. SetState(EnumHandlerState.Acked);
  40. }
  41. if (response.Address != _address || response.CommandNumber != _command)
  42. {
  43. transactionComplete = false;
  44. return false;
  45. }
  46. if (response.IsResponse)
  47. {
  48. if (response.DataLength >= 1)
  49. {
  50. ParseData(response);
  51. }
  52. SendAck();
  53. SetState(EnumHandlerState.Completed);
  54. transactionComplete = true;
  55. return true;
  56. }
  57. transactionComplete = false;
  58. return false;
  59. }
  60. protected virtual void ParseData(AeRfMatchMessage msg)
  61. {
  62. if (msg.Data[0] != 0)
  63. {
  64. var reason = TranslateCsrCode(msg.Data[0]);
  65. Device.NoteError(reason);
  66. }
  67. }
  68. public void SendAck()
  69. {
  70. Device.Connection.SendMessage(new byte[] { 0x06 });
  71. }
  72. private static byte CalcSum(List<byte> data, int length)
  73. {
  74. byte ret = 0x00;
  75. for (var i = 0; i < length; i++)
  76. {
  77. ret ^= data[i];
  78. }
  79. return ret;
  80. }
  81. protected string TranslateCsrCode(int csrCode)
  82. {
  83. string ret = csrCode.ToString();
  84. switch (csrCode)
  85. {
  86. case 0:
  87. ret = null;//"Command accepted";
  88. break;
  89. case 1:
  90. ret = "Control Code Is Incorrect";
  91. break;
  92. case 2:
  93. ret = "Output Is On(Change Not Allowed)";
  94. break;
  95. case 4:
  96. ret = "Data Is Out Of Range";
  97. break;
  98. case 7:
  99. ret = "Active Fault(s) Exist";
  100. break;
  101. case 9:
  102. ret = "Data Byte Count Is Incorrect";
  103. break;
  104. case 19:
  105. ret = "Recipe Is Active(Change Not Allowed)";
  106. break;
  107. case 50:
  108. ret = "The Frequency Is Out Of Range";
  109. break;
  110. case 51:
  111. ret = "The Duty Cycle Is Out Of Range";
  112. break;
  113. case 53:
  114. ret = "The Device Controlled By The Command Is Not Detected";
  115. break;
  116. case 99:
  117. ret = "Command Not Accepted(There Is No Such Command)";
  118. break;
  119. default:
  120. break;
  121. }
  122. return ret;
  123. }
  124. }
  125. //91 set active preset number
  126. public class AeRfMatchSetActivePresetHandler : AeRfMatchHandler
  127. {
  128. public AeRfMatchSetActivePresetHandler(AeRfMatch device, byte address, byte network, int number)
  129. : base(device, address, 91, BuildData(network, number))
  130. {
  131. Name = "set active preset number";
  132. }
  133. private static byte[] BuildData(byte network, int number)
  134. {
  135. return new byte[] { network, 0, (byte)number, 0 };
  136. }
  137. }
  138. //92 set preset
  139. public class AeRfMatchSetPresetHandler : AeRfMatchHandler
  140. {
  141. public AeRfMatchSetPresetHandler(AeRfMatch device, byte address, byte network, Presets data)
  142. : base(device, address, 92, BuildData(network, data))
  143. {
  144. Name = "set active preset number";
  145. }
  146. private static byte[] BuildData(byte network, Presets data)
  147. {
  148. List<byte> lstData = new List<byte>();
  149. lstData.AddRange(new byte[]{ network, 0, data.PreNo, data.TraSum });
  150. int tempdata = (int)(data.LoadData * 100);
  151. lstData.Add((byte)tempdata);
  152. lstData.Add((byte)(tempdata >> 8));
  153. tempdata = (int)(data.TuneData * 100);
  154. lstData.Add((byte)tempdata);
  155. lstData.Add((byte)(tempdata >> 8));
  156. for (int i = 0; i < data.TraSum; i++)
  157. {
  158. tempdata = (int)(data.TraData[i, 0] * 100);
  159. lstData.Add((byte)tempdata);
  160. lstData.Add((byte)(tempdata >> 8));
  161. tempdata = (int)(data.TraData[i, 1] * 100);
  162. lstData.Add((byte)tempdata);
  163. lstData.Add((byte)(tempdata >> 8));
  164. }
  165. return lstData.ToArray();
  166. }
  167. }
  168. //93 control mode
  169. public class AeRfMatchSetControlModeHandler : AeRfMatchHandler
  170. {
  171. public AeRfMatchSetControlModeHandler(AeRfMatch device, byte address, byte network, EnumRfMatchTuneMode mode)
  172. : base(device, address, 93, BuildData(network, mode))
  173. {
  174. Name = "Set control mode";
  175. }
  176. private static byte[] BuildData(byte network, EnumRfMatchTuneMode mode)
  177. {
  178. byte setpoint = 0;
  179. switch (mode)
  180. {
  181. case EnumRfMatchTuneMode.Auto:
  182. setpoint = 1;
  183. break;
  184. case EnumRfMatchTuneMode.Manual:
  185. setpoint = 2;
  186. break;
  187. }
  188. return new byte[]{network, 0, setpoint, 0};
  189. }
  190. }
  191. //94 enable presets
  192. public class AeRfMatchEnablePresetHandler : AeRfMatchHandler
  193. {
  194. public AeRfMatchEnablePresetHandler(AeRfMatch device, byte address, byte network, bool enable)
  195. : base(device, address, 94, BuildData(network, enable))
  196. {
  197. Name = "enable preset";
  198. }
  199. private static byte[] BuildData(byte network, bool enable)
  200. {
  201. return new byte[] { network, 0, enable?(byte)0x1: (byte)0x0, 0 };
  202. }
  203. }
  204. //98 enable motor move
  205. public class AeRfMatchEnableCapMoveHandler : AeRfMatchHandler
  206. {
  207. public AeRfMatchEnableCapMoveHandler(AeRfMatch device, byte address, byte network, bool enable)
  208. : base(device, address, 98, BuildData(network, enable))
  209. {
  210. Name = "enable motor move";
  211. }
  212. private static byte[] BuildData(byte network, bool enable)
  213. {
  214. return new byte[] { network, 0, enable ? (byte)0x1 : (byte)0x0, 0 };
  215. }
  216. }
  217. //112 set load position
  218. public class AeRfMatchSetLoadPositionHandler : AeRfMatchHandler
  219. {
  220. public AeRfMatchSetLoadPositionHandler(AeRfMatch device, byte address, byte network, float position)
  221. : base(device, address, 112, BuildData(network, position))
  222. {
  223. Name = "set load position";
  224. }
  225. private static byte[] BuildData(byte network, float position)
  226. {
  227. int iposi = (int)(position * 100);
  228. return new byte[] { network, 0, (byte)iposi, (byte)(iposi >> 8) };
  229. }
  230. }
  231. //122 set tune position
  232. public class AeRfMatchSetTunePositionHandler : AeRfMatchHandler
  233. {
  234. public AeRfMatchSetTunePositionHandler(AeRfMatch device, byte address, byte network, float position)
  235. : base(device, address, 122, BuildData(network, position))
  236. {
  237. Name = "set tune position";
  238. }
  239. private static byte[] BuildData(byte network, float position)
  240. {
  241. int iposi = (int)(position * 100);
  242. return new byte[] { network, 0, (byte)iposi, (byte)(iposi >> 8) };
  243. }
  244. }
  245. //161 query preset no
  246. public class AeRfMatchQueryPresetNumberHandler : AeRfMatchHandler
  247. {
  248. public AeRfMatchQueryPresetNumberHandler(AeRfMatch device, byte address)
  249. : base(device, address, 161, null)
  250. {
  251. Name = "query preset mode";
  252. }
  253. protected override void ParseData(AeRfMatchMessage response)
  254. {
  255. if (response.DataLength != 4)
  256. {
  257. Device.NoteError($"{Name}, return data length {response.DataLength}");
  258. }
  259. else
  260. {
  261. Device.NotePresetNumber(response.Data[2] + (response.Data[3] << 8));
  262. }
  263. }
  264. }
  265. //219 query status
  266. public class AeRfMatchQueryStatusHandler : AeRfMatchHandler
  267. {
  268. private AEStatusData _statusData;
  269. public AeRfMatchQueryStatusHandler(AeRfMatch device, byte address)
  270. : base(device, address, 219, null)
  271. {
  272. Name = "query status";
  273. }
  274. protected override void ParseData(AeRfMatchMessage response)
  275. {
  276. if (response.DataLength != 88)
  277. {
  278. Device.NoteError($"{Name}, return data length {response.DataLength}");
  279. }
  280. else
  281. {
  282. var retData = response.Data;
  283. _statusData = new AEStatusData();
  284. _statusData.LoadPosi1 = (retData[4] + (retData[5] << 8)) / 100f;
  285. _statusData.TunePosi1 = (retData[6] + (retData[7] << 8)) / 100f;
  286. _statusData.LoadPosi2 = (retData[12] + (retData[13] << 8)) / 100f;
  287. _statusData.TunePosi2 = (retData[14] + (retData[15] << 8)) / 100f;
  288. _statusData.BiasPeak = (retData[20] + (retData[21] << 8)) / 100f;
  289. _statusData.DCBias = (retData[22] + (retData[23] << 8)) / 100f;
  290. _statusData.ZScanII.R1 = BitConverter.ToSingle(retData.ToArray(), 40);
  291. _statusData.ZScanII.X1 = BitConverter.ToSingle(retData.ToArray(), 44);
  292. _statusData.ZScanII.Voltage1 = BitConverter.ToSingle(retData.ToArray(), 48) * 1.41421f;
  293. _statusData.ZScanII.Current1 = BitConverter.ToSingle(retData.ToArray(), 52);
  294. _statusData.ZScanII.Phase1 = BitConverter.ToSingle(retData.ToArray(), 56);
  295. _statusData.ZScanII.Power1 = BitConverter.ToSingle(retData.ToArray(), 60);
  296. _statusData.ZScanII.R2 = BitConverter.ToSingle(retData.ToArray(), 64);
  297. _statusData.ZScanII.X2 = BitConverter.ToSingle(retData.ToArray(), 68);
  298. _statusData.ZScanII.Voltage2 = BitConverter.ToSingle(retData.ToArray(), 72);
  299. _statusData.ZScanII.Current2 = BitConverter.ToSingle(retData.ToArray(), 76);
  300. _statusData.ZScanII.Phase2 = BitConverter.ToSingle(retData.ToArray(), 80);
  301. _statusData.ZScanII.Power2 = BitConverter.ToSingle(retData.ToArray(), 84);
  302. AnalysisAEMatchStatus(retData);
  303. Device.NoteStatus(_statusData);
  304. }
  305. }
  306. private void AnalysisAEMatchStatus(byte[] sts)
  307. {
  308. _statusData.Status2.Net1OutPutOn = ((sts[0] >> 0) & 0x01) == 0x01 ? true : false;
  309. _statusData.Status2.Net1OutPutTuned = ((sts[0] >> 1) & 0x01) == 0x01 ? true : false;
  310. _statusData.Status2.Net2OutPutOn = ((sts[0] >> 2) & 0x01) == 0x01 ? true : false;
  311. _statusData.Status2.Net2OutPutTuned = ((sts[0] >> 3) & 0x01) == 0x01 ? true : false;
  312. //RecvData.Status2.Net2OutPutOn = ((sts[0] >> 4) & 0x01) == 0x01 ? true : false;
  313. //RecvData.Status2.Net2OutPutTuned = ((sts[0] >> 5) & 0x01) == 0x01 ? true : false;
  314. _statusData.Status2.Net1PresetsActive = ((sts[0] >> 6) & 0x01) == 0x01 ? true : false;
  315. _statusData.Status2.Net1ExtPresetsSelected = ((sts[0] >> 7) & 0x01) == 0x01 ? true : false;
  316. _statusData.Status2.Low24VDetected = ((sts[1] >> 0) & 0x01) == 0x01 ? true : false;
  317. _statusData.Status2.OverTempDetected = ((sts[1] >> 1) & 0x01) == 0x01 ? true : false;
  318. _statusData.Status2.InterlockOpen = ((sts[1] >> 2) & 0x01) == 0x01 ? true : false;
  319. _statusData.Status2.FanFault = ((sts[1] >> 3) & 0x01) == 0x01 ? true : false;
  320. _statusData.Status2.Net1AutoMode = ((sts[1] >> 4) & 0x01) == 0x01 ? true : false;
  321. _statusData.Status2.Net1HostCtrlMode = ((sts[1] >> 5) & 0x01) == 0x01 ? true : false;
  322. _statusData.Status2.Net2AutoMode = ((sts[1] >> 6) & 0x01) == 0x01 ? true : false;
  323. _statusData.Status2.Net2HostCtrlMode = ((sts[1] >> 7) & 0x01) == 0x01 ? true : false;
  324. _statusData.Status2.AuxCapOutputTuned = ((sts[2] >> 0) & 0x01) == 0x01 ? true : false;
  325. _statusData.Status2.AuxCapAutoModed = ((sts[2] >> 1) & 0x01) == 0x01 ? true : false;
  326. _statusData.Status2.AuxCapPresetsActive = ((sts[2] >> 2) & 0x01) == 0x01 ? true : false;
  327. //RecvData.Status2.FanFault = ((sts[1] >> 3) & 0x01) == 0x01 ? true : false;
  328. //RecvData.Status2.Net1AutoMode = ((sts[1] >> 4) & 0x01) == 0x01 ? true : false;
  329. _statusData.Status2.Net1UserCtrlMode = ((sts[2] >> 5) & 0x01) == 0x01 ? true : false;
  330. _statusData.Status2.Net2UserCtrlMode = ((sts[2] >> 6) & 0x01) == 0x01 ? true : false;
  331. //RecvData.Status2.Net2HostCtrlMode = ((sts[2] >> 7) & 0x01) == 0x01 ? true : false;
  332. _statusData.Status2.Faults = ((sts[3] >> 0) & 0x01) == 0x01 ? true : false;
  333. _statusData.Status2.Warning = ((sts[3] >> 1) & 0x01) == 0x01 ? true : false;
  334. _statusData.Status2.InitMotorFailed = ((sts[3] >> 2) & 0x01) == 0x01 ? true : false;
  335. _statusData.Status2.Net2PresetsActive = ((sts[3] >> 3) & 0x01) == 0x01 ? true : false;
  336. _statusData.Status2.Net2ExtPresetsSelected = ((sts[3] >> 4) & 0x01) == 0x01 ? true : false;
  337. _statusData.Status2.VoltageOverLimitFault = ((sts[3] >> 5) & 0x01) == 0x01 ? true : false;
  338. //RecvData.Status2.Net2AutoMode = ((sts[3] >> 6) & 0x01) == 0x01 ? true : false;
  339. //RecvData.Status2.Net2HostCtrlMode = ((sts[3] >> 7) & 0x01) == 0x01 ? true : false;
  340. }
  341. }
  342. }