HanbellPumpHandler.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using Aitex.Core.Common.DeviceData;
  6. using MECF.Framework.Common.Communications;
  7. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps.Hanbell
  8. {
  9. public abstract class HanbellPumpHandler : HandlerBase
  10. {
  11. public HanbellPump Device { get; }
  12. private byte _address;
  13. protected byte _command;
  14. protected HanbellPumpHandler(HanbellPump device, byte address, byte command, byte[] data)
  15. : base(BuildMessage(address, command, data))
  16. {
  17. Device = device;
  18. _address = address;
  19. _command = command;
  20. }
  21. private static byte[] BuildMessage(byte address, byte command, byte[] data)
  22. {
  23. List<byte> buffer = new List<byte>();
  24. //MODBUS TCP
  25. buffer.Add(0x00);
  26. buffer.Add(0x00);
  27. buffer.Add(0x00);
  28. buffer.Add(0x00);
  29. int length = 2 + (data != null ? data.Length:0);
  30. buffer.Add((byte)((length >> 8) & 0xFF));
  31. buffer.Add((byte)(length & 0xFF));
  32. buffer.Add(address);
  33. buffer.Add(command);
  34. if (data != null && data.Length > 0)
  35. {
  36. buffer.AddRange(data);
  37. }
  38. // CRC ??
  39. //buffer.Add(CalcSum(buffer, buffer.Count));
  40. return buffer.ToArray();
  41. }
  42. //host->unit, unit->host(ack), unit->host(csr), host->unit(ack)
  43. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  44. {
  45. HanbellPumpMessage response = msg as HanbellPumpMessage;
  46. ResponseMessage = msg;
  47. Device.NoteError(null);
  48. if (response.Address != _address || response.FunctionCode != _command)
  49. {
  50. transactionComplete = false;
  51. return false;
  52. }
  53. if (response.IsComplete)
  54. {
  55. SetState(EnumHandlerState.Completed);
  56. transactionComplete = true;
  57. return true;
  58. }
  59. transactionComplete = false;
  60. return false;
  61. }
  62. public void SendAck()
  63. {
  64. Device.Connection.SendMessage(new byte[] { 0x06 });
  65. }
  66. private static byte ModRTU_CRC(byte[] buffer)
  67. {
  68. //ushort crc = 0xFFFF;
  69. // var buf = System.Text.Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, buffer));
  70. var buf = buffer;
  71. var len = buffer.Length;
  72. byte temp = buffer[0];
  73. for (int i = 1; i < buffer.Length; i++)
  74. {
  75. temp = (byte)(temp ^ buffer[i]);
  76. }
  77. return (byte)~temp;
  78. }
  79. }
  80. public class HanbellPumpRequestRegistersHandler : HanbellPumpHandler
  81. {
  82. public HanbellPumpRequestRegistersHandler(HanbellPump device, byte address, int startAddress, int registerCount)
  83. : base(device, address, 0x03, BuildData(startAddress, registerCount))
  84. {
  85. Name = "RequestRegisters";
  86. }
  87. private static byte[] BuildData(int startAddress, int registerCount)
  88. {
  89. return new byte[] { (byte)(startAddress >> 8), (byte)startAddress, (byte)(registerCount >> 8), (byte)registerCount };
  90. }
  91. public override bool HandleMessage(MessageBase msg, out bool handled)
  92. {
  93. if (base.HandleMessage(msg, out handled))
  94. {
  95. var result = msg as HanbellPumpMessage;
  96. if (result.Data.Length < 91)//91=data length + registerCount * 2
  97. {
  98. Device.NoteError("invalid response for command RequestRegisters");
  99. }
  100. else
  101. {
  102. //2031
  103. var digitalOutputStatus1High = result.Data[61];
  104. var digitalOutputStatus1Low = result.Data[62];
  105. Device.IsOn = ((digitalOutputStatus1Low & 0x00000002) == 0) || ((digitalOutputStatus1Low & 0x00000001) == 0);
  106. Device.NoteDPPower(result.Data[1] * 256 + result.Data[2]);
  107. Device.NoteBPPower(result.Data[3] * 256 + result.Data[4]);
  108. Device.NoteDPSpeed(result.Data[5] * 256 + result.Data[6]);
  109. Device.NoteBPSpeed(result.Data[37] * 256 + result.Data[8]);
  110. int waring0 = result.Data[45] * 256 + result.Data[46];
  111. Device.NoteDPCurrentHigh(waring0 & (1 << 15));
  112. Device.NoteBPCurrentHigh(waring0 & (1 << 14));
  113. int waring1 = result.Data[47] * 256 + result.Data[48];
  114. int waring2 = result.Data[49] * 256 + result.Data[50];
  115. int alarm0 = result.Data[51] * 256 + result.Data[52];
  116. int alarm1 = result.Data[53] * 256 + result.Data[54];
  117. int alarm2 = result.Data[55] * 256 + result.Data[56];
  118. Device.IsError = ((digitalOutputStatus1Low & 0x0000004) == 0) || ((digitalOutputStatus1Low & 0x000008) == 0) ||
  119. waring0 + waring1 + waring2 != 0 || alarm0 + alarm1 + alarm2 != 0;
  120. //if (waring0 + waring1 + waring2 != 0)
  121. //{
  122. // Device.NoteError("pump is in warning status");
  123. //}
  124. //if (alarm0 + alarm1 + alarm2 != 0)
  125. //{
  126. // Device.NoteError("pump is in alarm status");
  127. //}
  128. }
  129. }
  130. return true;
  131. }
  132. }
  133. public class HanbellPumpStartVaccumPumpHandler : HanbellPumpHandler
  134. {
  135. public HanbellPumpStartVaccumPumpHandler(HanbellPump device, byte address)
  136. : base(device, address, 0x05, BuildData())
  137. {
  138. Name = "StartVaccumPump";
  139. }
  140. private static byte[] BuildData()
  141. {
  142. return new byte[] { 0x00,0x0A,0xFF,0x00 };
  143. }
  144. public override bool HandleMessage(MessageBase msg, out bool handled)
  145. {
  146. if (base.HandleMessage(msg, out handled))
  147. {
  148. Device.NoteVaccumPumpCompleted(true);
  149. }
  150. return true;
  151. }
  152. }
  153. public class HanbellPumpStopVaccumPumpHandler : HanbellPumpHandler
  154. {
  155. public HanbellPumpStopVaccumPumpHandler(HanbellPump device, byte address)
  156. : base(device, address, 0x05, BuildData())
  157. {
  158. Name = "StopVaccumPump";
  159. }
  160. private static byte[] BuildData()
  161. {
  162. return new byte[] { 0x00, 0x0A, 0x00, 0x00 };
  163. }
  164. public override bool HandleMessage(MessageBase msg, out bool handled)
  165. {
  166. if (base.HandleMessage(msg, out handled))
  167. {
  168. Device.NoteVaccumPumpCompleted(false);
  169. }
  170. return true;
  171. }
  172. }
  173. public class HanbellPumpStartBPPumpHandler : HanbellPumpHandler
  174. {
  175. public HanbellPumpStartBPPumpHandler(HanbellPump device, byte address)
  176. : base(device, address, 0x05, BuildData())
  177. {
  178. Name = "StartBPPump";
  179. }
  180. private static byte[] BuildData()
  181. {
  182. return new byte[] { 0x00, 0x0B, 0xFF, 0x00 };
  183. }
  184. public override bool HandleMessage(MessageBase msg, out bool handled)
  185. {
  186. if (base.HandleMessage(msg, out handled))
  187. {
  188. Device.NoteBPPumpOnOff(true);
  189. }
  190. return true;
  191. }
  192. }
  193. public class HanbellPumpStopBPPumpHandler : HanbellPumpHandler
  194. {
  195. public HanbellPumpStopBPPumpHandler(HanbellPump device, byte address)
  196. : base(device, address, 0x05, BuildData())
  197. {
  198. Name = "StopBPPump";
  199. }
  200. private static byte[] BuildData()
  201. {
  202. return new byte[] { 0x00, 0x0B, 0x00, 0x00 };
  203. }
  204. public override bool HandleMessage(MessageBase msg, out bool handled)
  205. {
  206. if (base.HandleMessage(msg, out handled))
  207. {
  208. Device.NoteBPPumpOnOff(false);
  209. }
  210. return true;
  211. }
  212. }
  213. public class HanbellPumpResetHandler : HanbellPumpHandler
  214. {
  215. public HanbellPumpResetHandler(HanbellPump device, byte address)
  216. : base(device, address, 0x05, BuildData())
  217. {
  218. Name = "Reset";
  219. }
  220. private static byte[] BuildData()
  221. {
  222. return new byte[] { 0x00, 0x0C, 0xFF, 0x00 };
  223. }
  224. }
  225. public class HanbellPumpRawCommandHandler : HanbellPumpHandler
  226. {
  227. public HanbellPumpRawCommandHandler(HanbellPump device, byte address, string command, string parameter = null)
  228. : base(device, address, byte.Parse(command), BuildData(parameter))
  229. {
  230. }
  231. private static byte[] BuildData(string parameter)
  232. {
  233. if (parameter == null)
  234. return new byte[] { };
  235. return parameter.Split(',').Select(para => Convert.ToByte(para,16)).ToArray();
  236. }
  237. public override bool HandleMessage(MessageBase msg, out bool handled)
  238. {
  239. base.HandleMessage(msg, out handled);
  240. var result = msg as HanbellPumpMessage;
  241. Device.NoteRawCommandInfo(_command.ToString("X2"), string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  242. return true;
  243. }
  244. }
  245. }