TxHighFrequencyRFHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using Aitex.Core.Common.DeviceData;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.RT.SCCore;
  10. using Aitex.Core.Util;
  11. using MECF.Framework.Common.Communications;
  12. using static MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.AdTecTxHigh.HighFrequencyRF;
  13. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.AdTecTxHigh
  14. {
  15. public abstract class HighFrequencyRFHandler : HandlerBase
  16. {
  17. public HighFrequencyRF Device { get; }
  18. private string _command;
  19. protected HighFrequencyRFHandler(HighFrequencyRF device, string command)
  20. : base(BuildMessage(command))
  21. {
  22. Device = device;
  23. _command = command;
  24. }
  25. private static byte[] BuildMessage(string command)
  26. {
  27. byte[] buffer = Encoding.ASCII.GetBytes(command);
  28. return buffer;
  29. }
  30. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  31. {
  32. HighFrequencyRFMessage response = msg as HighFrequencyRFMessage;
  33. ResponseMessage = msg;
  34. SetState(EnumHandlerState.Acked);
  35. if (response.IsResponse)
  36. {
  37. if (response.DataLength >= 1)
  38. {
  39. ParseData(response);
  40. }
  41. //SendAck();
  42. SetState(EnumHandlerState.Completed);
  43. transactionComplete = true;
  44. return true;
  45. }
  46. transactionComplete = false;
  47. return false;
  48. }
  49. protected static byte[] ToByteArray(string parameter)
  50. {
  51. if (parameter == null)
  52. return new byte[] { };
  53. return parameter.Split(',').Select(para => Convert.ToByte(para, 16)).ToArray();
  54. }
  55. protected virtual void ParseData(HighFrequencyRFMessage msg)
  56. {
  57. if (msg.Data[0] != 0)
  58. {
  59. var reason = TranslateCsrCode(msg.Data[0]);
  60. Device.NoteError(reason);
  61. }
  62. }
  63. //public void SendAck()
  64. //{
  65. // Device.Connection.SendMessage(new byte[] { 0x06 });
  66. //}
  67. private static byte CalcSum(List<byte> data, int length)
  68. {
  69. byte ret = 0x00;
  70. for (var i = 0; i < length; i++)
  71. {
  72. ret ^= data[i];
  73. }
  74. return ret;
  75. }
  76. protected string TranslateCsrCode(int csrCode)
  77. {
  78. string ret = csrCode.ToString();
  79. switch (csrCode)
  80. {
  81. case 0:
  82. ret = null;//"Command accepted";
  83. break;
  84. case 1:
  85. ret = "Control Code Is Incorrect";
  86. break;
  87. case 2:
  88. ret = "Output Is On(Change Not Allowed)";
  89. break;
  90. case 4:
  91. ret = "Data Is Out Of Range";
  92. break;
  93. case 7:
  94. ret = "Active Fault(s) Exist";
  95. break;
  96. case 9:
  97. ret = "Data Byte Count Is Incorrect";
  98. break;
  99. case 19:
  100. ret = "Recipe Is Active(Change Not Allowed)";
  101. break;
  102. case 50:
  103. ret = "The Frequency Is Out Of Range";
  104. break;
  105. case 51:
  106. ret = "The Duty Cycle Is Out Of Range";
  107. break;
  108. case 53:
  109. ret = "The Device Controlled By The Command Is Not Detected";
  110. break;
  111. case 99:
  112. ret = "Command Not Accepted(There Is No Such Command)";
  113. break;
  114. default:
  115. break;
  116. }
  117. return ret;
  118. }
  119. }
  120. //00 ControlMode
  121. public class HighFrequencyRFSetControlModeHandler : HighFrequencyRFHandler
  122. {
  123. public HighFrequencyRFSetControlModeHandler(HighFrequencyRF device, string command)
  124. : base(device, command)
  125. {
  126. Name = "Set control mode";
  127. }
  128. protected override void ParseData(HighFrequencyRFMessage response)
  129. {
  130. if (response.Data[0] == (byte)'\r')
  131. {
  132. //EV.PostInfoLog("PM1", "High RF Power set RS232C mode succeed");
  133. }
  134. }
  135. }
  136. //02 Setpoint
  137. public class HighFrequencyRFSetPointHandler : HighFrequencyRFHandler
  138. {
  139. public HighFrequencyRFSetPointHandler(HighFrequencyRF device, string command)
  140. : base(device, command)
  141. {
  142. Name = "Set Point";
  143. }
  144. protected override void ParseData(HighFrequencyRFMessage response)
  145. {
  146. if (response.Data[0] == (byte)'\r')
  147. {
  148. //EV.PostInfoLog("PM1", "High RF Power set RS232C mode succeed");
  149. }
  150. }
  151. }
  152. //public class HighFrequencyRFGetSetPointHandler : HighFrequencyRFHandler
  153. //{
  154. // public HighFrequencyRFGetSetPointHandler(HighFrequencyRF device, string command)
  155. // : base(device, command)
  156. // {
  157. // Name = "Get SetPoint";
  158. // }
  159. // protected override void ParseData(HighFrequencyRFMessage response)
  160. // {
  161. // Device.NotePowerSetPoint(response.Data[response.Data.Length - 2]);
  162. // }
  163. //}
  164. //03 00 off, AA on
  165. public class HighFrequencyRFSwitchOnOffHandler : HighFrequencyRFHandler
  166. {
  167. public HighFrequencyRFSwitchOnOffHandler(HighFrequencyRF device, string command)
  168. : base(device, command)
  169. {
  170. if (command.Contains("ACON"))
  171. Name = "Switch " + "On";
  172. else if (command.Contains("ACOFF"))
  173. Name = "Switch " + "Off";
  174. }
  175. protected override void ParseData(HighFrequencyRFMessage response)
  176. {
  177. if (response.Data[0] == (byte)'\r')
  178. {
  179. //EV.PostInfoLog("PM1", "High RF Power set RS232C mode succeed");
  180. }
  181. }
  182. }
  183. public class HighFrequencyRFQueryHandler : HighFrequencyRFHandler
  184. {
  185. public HighFrequencyRFQueryHandler(HighFrequencyRF device, string command)
  186. : base(device, command)
  187. {
  188. Name = "Qurey Status";
  189. }
  190. protected override void ParseData(HighFrequencyRFMessage response)
  191. {
  192. string str = Encoding.ASCII.GetString(response.Data);
  193. if (string.IsNullOrEmpty(str))
  194. {
  195. Device.NoteError($"High RF Power no data feedback");
  196. return;
  197. }
  198. string str2 = str.Trim('\r');
  199. if (str2 == Cmd.ERR_RES)
  200. {
  201. Device.NoteError($"High RF Power receive [{str2}]");
  202. return;
  203. }
  204. try
  205. {
  206. string pattern = @"(\d{7})\s(\d{5})\s(\d{5})\s(\d{5})\s(\d{5})";
  207. Match match1 = Regex.Match(str2, pattern);
  208. if (!match1.Success)
  209. {
  210. if (!SC.GetValue<bool>("System.IsSimulatorMode"))
  211. {
  212. Device.NoteError($"High RF Power data format incorrect [{str2}]");
  213. }
  214. return;
  215. }
  216. string[] str1 =
  217. {
  218. match1.Groups[1].Value,
  219. match1.Groups[2].Value,
  220. match1.Groups[3].Value,
  221. match1.Groups[4].Value,
  222. match1.Groups[5].Value
  223. };
  224. this.ParseQueryData(str1);
  225. }
  226. catch (Exception ex)
  227. {
  228. LOG.Write(ex);
  229. }
  230. }
  231. private void ParseQueryData(string[] strInfo)
  232. {
  233. // Control mode
  234. string s2 = strInfo[0].Substring(0, 1);
  235. Device.NoteCommMode((EnumHighRfPowerCommunicationMode)Convert.ToUInt16(s2));
  236. // output mode
  237. string s0 = strInfo[0].Substring(1, 1);
  238. var WorkMode = (EnumRfPowerWorkMode)Convert.ToUInt16(s0);
  239. // ON/OFF
  240. char s1 = strInfo[0][2];
  241. if (s1 == '1')
  242. {
  243. Device.NoteStatus(true);
  244. }
  245. else if (s1 == '0')
  246. {
  247. Device.NoteStatus(false);
  248. }
  249. // error code
  250. string alarm = strInfo[0].Substring(5, 2);
  251. byte errCode = Convert.ToByte(alarm);
  252. bool isError = false;
  253. string code = errCode == 1 ? "Ref Over" :
  254. errCode == 2 ? "Ref Limit" :
  255. errCode == 3 ? "Cur Over" :
  256. errCode == 4 ? "Cur Limit" :
  257. errCode == 5 ? "Temp Over" :
  258. errCode == 6 ? "Temp Sensor Short" :
  259. errCode == 7 ? "Temp Sensor Open" :
  260. errCode == 8 ? "Sensor Error" :
  261. errCode == 9 ? "Fwd Power Over" :
  262. errCode == 10 ? "RF ON Timer" :
  263. errCode == 11 ? "RS232C error" :
  264. errCode == 12 ? "Amp Unbalance" :
  265. errCode == 14 ? "Fan error" :
  266. errCode == 15 ? "Coolant Error" :
  267. errCode == 16 ? "Voltage Error" :
  268. errCode == 17 ? "Fwd Power Down" :
  269. errCode == 22 ? "PD Over" :
  270. errCode == 23 ? "PD Limit" :
  271. errCode == 26 ? "Dew Condensation" :
  272. errCode == 29 ? "SW Failure" :
  273. errCode == 99 ? "Safety Lock" : string.Empty;
  274. if (!string.IsNullOrEmpty(code))
  275. {
  276. //if (errCode == 99)
  277. // EV.PostInfoLog("PM1", "High RF Power: " + code);
  278. //else
  279. // EV.PostAlarmLog("PM1", "High RF Power: " + code);
  280. isError = true;
  281. }
  282. Device.NoteErrorStatus(isError, code);
  283. // setpoint power
  284. //Device.NotePowerSetPoint(Convert.ToUInt64(strInfo[1]));
  285. // forward power
  286. Device.NoteForwardPower(Convert.ToUInt64(strInfo[2]));
  287. // reflect power
  288. Device.NotedReflectPower(Convert.ToUInt64(strInfo[3]));
  289. }
  290. }
  291. }