TxLowFrequencyRFHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.Common.DeviceData;
  6. using Aitex.Core.RT.Event;
  7. using MECF.Framework.Common.Communications;
  8. using static MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.AdTecTxLow.LowFrequencyRF;
  9. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.AdTecTxLow
  10. {
  11. public abstract class LowFrequencyRFHandler : HandlerBase
  12. {
  13. public LowFrequencyRF Device { get; }
  14. private byte[] _functionCode;
  15. private byte[] _executionCode;
  16. private byte[] _parameter;
  17. protected LowFrequencyRFHandler(LowFrequencyRF device, byte[] functionCode, byte[] executionCode, byte[] parameter)
  18. : base(BuildMessage(functionCode, executionCode, parameter))
  19. {
  20. Device = device;
  21. _functionCode = functionCode;
  22. _executionCode = executionCode;
  23. _parameter = parameter;
  24. }
  25. private static byte _header = 0x40;
  26. private static byte _command = 0x55;
  27. private static byte _stopCR = 0x0D;
  28. private static byte[] BuildMessage(byte[] functionCode, byte[] executionCode, byte[] parameter)
  29. {
  30. List<byte> buffer = new List<byte>();
  31. buffer.Add(_header);
  32. buffer.Add(_command);
  33. if (functionCode != null && functionCode.Length > 0)
  34. {
  35. buffer.AddRange(functionCode);
  36. }
  37. if (executionCode != null && executionCode.Length > 0)
  38. {
  39. buffer.AddRange(executionCode);
  40. }
  41. if (parameter != null && parameter.Length > 0)
  42. {
  43. buffer.AddRange(parameter);
  44. }
  45. buffer.Add(_stopCR);
  46. return buffer.ToArray();
  47. }
  48. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  49. {
  50. LowFrequencyRFMessage response = msg as LowFrequencyRFMessage;
  51. ResponseMessage = msg;
  52. SetState(EnumHandlerState.Acked);
  53. if (response.IsResponse)
  54. {
  55. if (response.DataLength >= 1)
  56. {
  57. ParseData(response);
  58. }
  59. //SendAck();
  60. SetState(EnumHandlerState.Completed);
  61. transactionComplete = true;
  62. return true;
  63. }
  64. transactionComplete = false;
  65. return false;
  66. }
  67. protected static byte[] ToByteArray(string parameter)
  68. {
  69. if (parameter == null)
  70. return new byte[] { };
  71. return parameter.Split(',').Select(para => Convert.ToByte(para, 16)).ToArray();
  72. }
  73. protected virtual void ParseData(LowFrequencyRFMessage msg)
  74. {
  75. if (msg.Data[0] != 0)
  76. {
  77. var reason = TranslateCsrCode(msg.Data[0]);
  78. //Device.NoteError(reason);
  79. }
  80. }
  81. //public void SendAck()
  82. //{
  83. // Device.Connection.SendMessage(new byte[] { 0x06 });
  84. //}
  85. private static byte CalcSum(List<byte> data, int length)
  86. {
  87. byte ret = 0x00;
  88. for (var i = 0; i < length; i++)
  89. {
  90. ret ^= data[i];
  91. }
  92. return ret;
  93. }
  94. protected string TranslateCsrCode(int csrCode)
  95. {
  96. string ret = csrCode.ToString();
  97. switch (csrCode)
  98. {
  99. case 0:
  100. ret = null;//"Command accepted";
  101. break;
  102. case 1:
  103. ret = "Control Code Is Incorrect";
  104. break;
  105. case 2:
  106. ret = "Output Is On(Change Not Allowed)";
  107. break;
  108. case 4:
  109. ret = "Data Is Out Of Range";
  110. break;
  111. case 7:
  112. ret = "Active Fault(s) Exist";
  113. break;
  114. case 9:
  115. ret = "Data Byte Count Is Incorrect";
  116. break;
  117. case 19:
  118. ret = "Recipe Is Active(Change Not Allowed)";
  119. break;
  120. case 50:
  121. ret = "The Frequency Is Out Of Range";
  122. break;
  123. case 51:
  124. ret = "The Duty Cycle Is Out Of Range";
  125. break;
  126. case 53:
  127. ret = "The Device Controlled By The Command Is Not Detected";
  128. break;
  129. case 99:
  130. ret = "Command Not Accepted(There Is No Such Command)";
  131. break;
  132. default:
  133. break;
  134. }
  135. return ret;
  136. }
  137. }
  138. //00 ControlMode
  139. public class LowFrequencyRFSetControlModeHandler : LowFrequencyRFHandler
  140. {
  141. public LowFrequencyRFSetControlModeHandler(LowFrequencyRF device, byte mode)
  142. : base(device, BuildPara('0', '0'), BuildPara('0', '0'), BuildData(mode))
  143. {
  144. Name = "Set control mode";
  145. }
  146. private static byte[] BuildPara(char a, char b)
  147. {
  148. return new byte[] { (byte)a, (byte)b };
  149. }
  150. private static byte[] BuildData(byte mode)
  151. {
  152. return mode == 0x01 ? BuildPara('0', '1') : BuildPara('0', '2');
  153. }
  154. }
  155. public class LowFrequencyRFGetControlModeHandler : LowFrequencyRFHandler
  156. {
  157. public LowFrequencyRFGetControlModeHandler(LowFrequencyRF device)
  158. : base(device, BuildPara('0', '0'), BuildPara('0', '1'), null)
  159. {
  160. Name = "Get control power";
  161. }
  162. private static byte[] BuildPara(char a, char b)
  163. {
  164. return new byte[] { (byte)a, (byte)b };
  165. }
  166. protected override void ParseData(LowFrequencyRFMessage response)
  167. {
  168. EnumLowRfPowerCommunicationMode mode = EnumLowRfPowerCommunicationMode.Manual;
  169. if (response.Data[6] == (byte)'0' && response.Data[7] == (byte)'1')
  170. {
  171. mode = EnumLowRfPowerCommunicationMode.Manual;
  172. }
  173. else if (response.Data[6] == (byte)'0' && response.Data[7] == (byte)'2')
  174. {
  175. mode = EnumLowRfPowerCommunicationMode.RS232C;
  176. }
  177. Device.NoteCommMode(mode);
  178. }
  179. }
  180. //01 SetPoint Limit
  181. public class LowFrequencyRFSetPointLimitHandler : LowFrequencyRFHandler
  182. {
  183. public LowFrequencyRFSetPointLimitHandler(LowFrequencyRF device, int point)
  184. : base(device, BuildPara('0', '1'), BuildPara('0', '0'), BuildData(point))
  185. {
  186. Name = "SetPoint Limit";
  187. }
  188. private static byte[] BuildPara(char a, char b)
  189. {
  190. return new byte[] { (byte)a, (byte)b };
  191. }
  192. private static byte[] BuildData(int point)
  193. {
  194. string str = point.ToString("X4");
  195. return new byte[4] { (byte)str[0], (byte)str[1], (byte)str[2], (byte)str[3] };
  196. }
  197. }
  198. //02 Setpoint
  199. public class LowFrequencyRFSetPointHandler : LowFrequencyRFHandler
  200. {
  201. public LowFrequencyRFSetPointHandler(LowFrequencyRF device, int point)
  202. : base(device, BuildPara('0', '2'), BuildPara('0', '0'), BuildData(point))
  203. {
  204. Name = "Set Point";
  205. }
  206. private static byte[] BuildPara(char a, char b)
  207. {
  208. return new byte[] { (byte)a, (byte)b };
  209. }
  210. private static byte[] BuildData(int point)
  211. {
  212. string str = point.ToString("X4");
  213. return new byte[4] { (byte)str[0], (byte)str[1], (byte)str[2], (byte)str[3] };
  214. }
  215. }
  216. public class LowFrequencyRFGetSetPointHandler : LowFrequencyRFHandler
  217. {
  218. public LowFrequencyRFGetSetPointHandler(LowFrequencyRF device)
  219. : base(device, BuildPara('0', '2'), BuildPara('0', '1'), null)
  220. {
  221. Name = "Get SetPoint";
  222. }
  223. private static byte[] BuildPara(char a, char b)
  224. {
  225. return new byte[] { (byte)a, (byte)b };
  226. }
  227. protected override void ParseData(LowFrequencyRFMessage response)
  228. {
  229. int value = GetData(response.Data[6], response.Data[7], response.Data[8], response.Data[9]);
  230. Device.NotePowerSetPoint(value);
  231. }
  232. private static int GetData(byte b1, byte b2, byte b3, byte b4)
  233. {
  234. byte[] byteData = new byte[4] { b1, b2, b3, b4 };
  235. string str = Encoding.ASCII.GetString(byteData);
  236. int value = Convert.ToInt32(str, 16);
  237. return value;
  238. }
  239. }
  240. //03 00 off, AA on
  241. public class LowFrequencyRFSwitchOnOffHandler : LowFrequencyRFHandler
  242. {
  243. public LowFrequencyRFSwitchOnOffHandler(LowFrequencyRF device, bool isOn)
  244. : base(device, BuildPara('0', '3'), BuildPara('0', '0'), BuildData(isOn))
  245. {
  246. Name = "Switch " + (isOn ? "On" : "Off");
  247. }
  248. private static byte[] BuildPara(char a, char b)
  249. {
  250. return new byte[] { (byte)a, (byte)b };
  251. }
  252. private static byte[] BuildData(bool isOn)
  253. {
  254. return isOn ? BuildPara('A', 'A') : BuildPara('0', '0');
  255. }
  256. }
  257. public class LowFrequencyRFGetSwitchOnOffHandler : LowFrequencyRFHandler
  258. {
  259. public LowFrequencyRFGetSwitchOnOffHandler(LowFrequencyRF device)
  260. : base(device, BuildPara('0', '3'), BuildPara('0', '1'), null)
  261. {
  262. Name = "Get Switch Status";
  263. }
  264. private static byte[] BuildPara(char a, char b)
  265. {
  266. return new byte[] { (byte)a, (byte)b };
  267. }
  268. protected override void ParseData(LowFrequencyRFMessage response)
  269. {
  270. bool isOn = false;
  271. if (response.Data[6] == (byte)'0' && response.Data[7] == (byte)'0')
  272. {
  273. isOn = false;
  274. }
  275. else if (response.Data[6] == (byte)'A' && response.Data[7] == (byte)'A')
  276. {
  277. isOn = true;
  278. }
  279. Device.NoteStatus(isOn);
  280. }
  281. }
  282. //80 AlarmReset
  283. public class LowFrequencyRFAlarmResetHandler : LowFrequencyRFHandler
  284. {
  285. public LowFrequencyRFAlarmResetHandler(LowFrequencyRF device)
  286. : base(device, BuildPara('8', '0'), BuildPara('0', '0'), BuildPara('A', 'A'))
  287. {
  288. Name = "reset alarm" ;
  289. }
  290. private static byte[] BuildPara(char a, char b)
  291. {
  292. return new byte[] { (byte)a, (byte)b };
  293. }
  294. }
  295. public class LowFrequencyRFGetAlarmStatusHandler : LowFrequencyRFHandler
  296. {
  297. public LowFrequencyRFGetAlarmStatusHandler(LowFrequencyRF device)
  298. : base(device, BuildPara('8', '0'), BuildPara('0', '1'), null)
  299. {
  300. Name = "get alarm status";
  301. }
  302. private static byte[] BuildPara(char a, char b)
  303. {
  304. return new byte[] { (byte)a, (byte)b };
  305. }
  306. protected override void ParseData(LowFrequencyRFMessage response)
  307. {
  308. byte[] byteData = new byte[8] {response.Data[6], response.Data[7], response.Data[8], response.Data[9],
  309. response.Data[10], response.Data[11], response.Data[12], response.Data[13] };
  310. string errorCode = Encoding.ASCII.GetString(byteData);
  311. bool isError = false;
  312. if (errorCode != "00000000")
  313. isError = true;
  314. Device.NoteErrorStatus(isError, errorCode);
  315. }
  316. }
  317. //81 Waring Status
  318. public class LowFrequencyRFWaringStatusHandler : LowFrequencyRFHandler
  319. {
  320. public LowFrequencyRFWaringStatusHandler(LowFrequencyRF device)
  321. : base(device, BuildPara('8', '1'), BuildPara('0', '1'), null)
  322. {
  323. Name = "Waring Status";
  324. }
  325. private static byte[] BuildPara(char a, char b)
  326. {
  327. return new byte[] { (byte)a, (byte)b };
  328. }
  329. }
  330. //90 Actual Forward and Refected
  331. public class LowFrequencyGetForwardAndRefectedfHandler : LowFrequencyRFHandler
  332. {
  333. public LowFrequencyGetForwardAndRefectedfHandler(LowFrequencyRF device)
  334. : base(device, BuildPara('9', '0'), BuildPara('0', '3'), null)
  335. {
  336. Name = "Get Forward And Refectedf";
  337. }
  338. private static byte[] BuildPara(char a, char b)
  339. {
  340. return new byte[] { (byte)a, (byte)b };
  341. }
  342. protected override void ParseData(LowFrequencyRFMessage response)
  343. {
  344. Device.NoteForwardPowerAndReflectPower(response.Data);
  345. }
  346. }
  347. }