BaecChillerHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using MECF.Framework.Common.Communications;
  2. using MECF.Framework.Common.Utilities;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Chillers.BaecChiller
  9. {
  10. //Modbus RTU
  11. public abstract class BaecChillerHandler : HandlerBase
  12. {
  13. public BaecChiller Device { get; }
  14. public string _command;
  15. protected string _parameter;
  16. protected BaecChillerHandler(BaecChiller device, string command)
  17. : base(BuildMessage(ToByteArray(command)))
  18. {
  19. Device = device;
  20. _command = command;
  21. _address = device.SlaveAddress;
  22. Name = command;
  23. }
  24. protected BaecChillerHandler(BaecChiller device, string command, string parameter1, short parameter2)
  25. : base(BuildMessage(ToByteArray(command), ToByteArray(parameter1), ShortToByteArray(parameter2)))
  26. {
  27. Device = device;
  28. _command = command;
  29. _parameter = $"{parameter1},{parameter2}";
  30. _address = device.SlaveAddress;
  31. Name = command;
  32. }
  33. protected BaecChillerHandler(BaecChiller device, string command, string parameter1, short parameter2, short parameter3)
  34. : base(BuildMessage(ToByteArray(command), ToByteArray(parameter1), ShortToByteArray(parameter2), ShortToByteArray(parameter3)))
  35. {
  36. Device = device;
  37. _command = command;
  38. _parameter = $"{parameter1},{parameter2},{parameter3}";
  39. _address = device.SlaveAddress;
  40. Name = command;
  41. }
  42. protected BaecChillerHandler(BaecChiller device, string command, string parameter)
  43. : base(BuildMessage(ToByteArray(command), ToByteArray(parameter)))
  44. {
  45. Device = device;
  46. _command = command;
  47. _parameter = parameter;
  48. _address = device.SlaveAddress;
  49. Name = command;
  50. }
  51. protected BaecChillerHandler(BaecChiller device, string command, byte[] parameter)
  52. : base(BuildMessage(ToByteArray(command), parameter))
  53. {
  54. Device = device;
  55. _command = command;
  56. //_parameter = parameter;
  57. Name = command;
  58. }
  59. private static byte _address = 0x01;
  60. protected static byte[] BuildMessage(byte[] commandArray, byte[] argumentArray1 = null, byte[] argumentArray2 = null, byte[] argumentArray3 = null)
  61. {
  62. List<byte> buffer = new List<byte>();
  63. buffer.Add(_address);
  64. if (commandArray != null && commandArray.Length > 0)
  65. {
  66. buffer.AddRange(commandArray);
  67. }
  68. if (argumentArray1 != null && argumentArray1.Length > 0)
  69. {
  70. buffer.AddRange(argumentArray1);
  71. }
  72. if (argumentArray2 != null && argumentArray2.Length > 0)
  73. {
  74. buffer.AddRange(argumentArray2.Reverse());
  75. }
  76. if (argumentArray3 != null && argumentArray3.Length > 0)
  77. {
  78. buffer.AddRange(argumentArray3.Reverse());
  79. }
  80. var checkSum = Crc16.CRC16_ModbusRTU(buffer.ToArray());
  81. var ret = BitConverter.GetBytes(checkSum);
  82. buffer.AddRange(ret);
  83. return buffer.ToArray();
  84. }
  85. protected bool HandleMessage(MessageBase msg)
  86. {
  87. RisshiChillerMessage response = msg as RisshiChillerMessage;
  88. if (!response.IsComplete)
  89. return false;
  90. return true;
  91. }
  92. protected static string Ushort2String(ushort num)
  93. {
  94. var bytes = UshortToByteArray(num);
  95. return string.Join(",", bytes, 0);
  96. }
  97. protected static byte[] ToByteArray(string parameter)
  98. {
  99. if (parameter == null)
  100. return new byte[] { };
  101. return parameter.Split(',').Select(para => Convert.ToByte(para, 16)).ToArray();
  102. }
  103. protected static byte[] IntToByteArray(int num)
  104. {
  105. byte[] bytes = BitConverter.GetBytes(num);
  106. return bytes;
  107. }
  108. protected static byte[] UshortToByteArray(ushort num)
  109. {
  110. byte[] bytes = BitConverter.GetBytes(num);
  111. return bytes;
  112. }
  113. protected static byte[] ShortToByteArray(short num)
  114. {
  115. byte[] bytes = BitConverter.GetBytes(num);
  116. return bytes;
  117. }
  118. protected int ByteArrayToInt(byte[] bytes)
  119. {
  120. if (bytes.Length == 4)
  121. {
  122. int temp = BitConverter.ToInt32(bytes, 0);
  123. return temp;
  124. }
  125. return 0;
  126. }
  127. protected short ByteArrayToShort(byte[] bytes)
  128. {
  129. if (bytes.Length == 2)
  130. {
  131. short temp = BitConverter.ToInt16(bytes, 0);
  132. return temp;
  133. }
  134. return 0;
  135. }
  136. protected ushort ByteArrayToUshort(byte[] bytes)
  137. {
  138. if (bytes.Length == 2)
  139. {
  140. ushort temp = BitConverter.ToUInt16(bytes, 0);
  141. return temp;
  142. }
  143. return 0;
  144. }
  145. }
  146. public class BaecChillerSetOnOffHandler : BaecChillerHandler
  147. {
  148. public BaecChillerSetOnOffHandler(BaecChiller device, bool isCH1, bool isOn)
  149. : base(device, "06", $"{(isCH1 ? "00,00" : "00,01")},{(isOn ? "00,01" : "00,00")}")
  150. {
  151. }
  152. public override bool HandleMessage(MessageBase msg, out bool handled)
  153. {
  154. if (HandleMessage(msg))
  155. {
  156. var result = msg as RisshiChillerMessage;
  157. handled = true;
  158. return true;
  159. }
  160. handled = false;
  161. return false;
  162. }
  163. }
  164. public class BaecChillerSetTemperatureHandler : BaecChillerHandler
  165. {
  166. public BaecChillerSetTemperatureHandler(BaecChiller device, bool isCH1, float temp)
  167. : base(device, "06", $"{(isCH1 ? "00,03" : "00,04")}", (short)(temp * 10))
  168. {
  169. }
  170. public override bool HandleMessage(MessageBase msg, out bool handled)
  171. {
  172. if (HandleMessage(msg))
  173. {
  174. var result = msg as RisshiChillerMessage;
  175. handled = true;
  176. return true;
  177. }
  178. handled = false;
  179. return false;
  180. }
  181. }
  182. public class BaecChillerSetTemperatureWarningRangeHandler : BaecChillerHandler
  183. {
  184. public BaecChillerSetTemperatureWarningRangeHandler(BaecChiller device, bool isCH1, float tempHighLimit, float tempLowLimit)
  185. : base(device, "10", $"{(isCH1 ? "00,06" : "00,08")}", (short)(tempHighLimit * 10), (short)(tempLowLimit * 10))
  186. {
  187. }
  188. public override bool HandleMessage(MessageBase msg, out bool handled)
  189. {
  190. if (HandleMessage(msg))
  191. {
  192. var result = msg as RisshiChillerMessage;
  193. handled = true;
  194. return true;
  195. }
  196. handled = false;
  197. return false;
  198. }
  199. }
  200. public class BaecChillerGetStatusHandler : BaecChillerHandler
  201. {
  202. public BaecChillerGetStatusHandler(BaecChiller device)
  203. : base(device, "03", "00,0C,00,0E")
  204. {
  205. }
  206. public override bool HandleMessage(MessageBase msg, out bool handled)
  207. {
  208. if (HandleMessage(msg))
  209. {
  210. var result = msg as RisshiChillerMessage;
  211. var datas = result.Data.ToList();
  212. if (datas.Count == 28)
  213. {
  214. var reason = string.Empty;
  215. var tempCH1 = datas.Skip(0).Take(2).Reverse().ToArray();
  216. var tempCH2 = datas.Skip(2).Take(2).Reverse().ToArray();
  217. var flowCH1 = datas.Skip(6).Take(2).Reverse().ToArray();
  218. var flowCH2 = datas.Skip(8).Take(2).Reverse().ToArray();
  219. var warningCH1 = datas.Skip(12).Take(2).Reverse().ToArray();
  220. var warningCH2 = datas.Skip(14).Take(2).Reverse().ToArray();
  221. var alarmCH1 = datas.Skip(18).Take(2).Reverse().ToArray();
  222. var alarmCH2 = datas.Skip(20).Take(2).Reverse().ToArray();
  223. var stateCH1 = datas.Skip(24).Take(2).Reverse().ToArray();
  224. var stateCH2 = datas.Skip(26).Take(2).Reverse().ToArray();
  225. Device.CH1TemperatureFeedback = BitConverter.ToInt16(tempCH1, 0) / 10.0f;
  226. Device.CH2TemperatureFeedback = BitConverter.ToInt16(tempCH2, 0) / 10.0f;
  227. Device.CH1WaterFlow = BitConverter.ToUInt16(flowCH1, 0) / 10.0f;
  228. Device.CH2WaterFlow = BitConverter.ToUInt16(flowCH2, 0) / 10.0f;
  229. var warningCH1Status = BitConverter.ToInt16(warningCH1, 0);
  230. var warningCH2Status = BitConverter.ToInt16(warningCH2, 0);
  231. Device.IsCH1Warning = (warningCH1Status & 0b_0000_0001) > 0;
  232. Device.IsCH2Warning = (warningCH2Status & 0b_0000_0001) > 0;
  233. if (Device.IsCH1Warning)
  234. {
  235. reason += (warningCH1Status & 0b_0000_0010) > 0 ? "CH1 water level low warning," : "";
  236. reason += (warningCH1Status & 0b_0000_0100) > 0 ? "CH1 water temperature high warning," : "";
  237. reason += (warningCH1Status & 0b_0000_1000) > 0 ? "CH1 water temperature low warning," : "";
  238. }
  239. if (Device.IsCH2Warning)
  240. {
  241. reason += (warningCH2Status & 0b_0000_0010) > 0 ? "CH2 water level low warning," : "";
  242. reason += (warningCH2Status & 0b_0000_0100) > 0 ? "CH2 water temperature high warning," : "";
  243. reason += (warningCH2Status & 0b_0000_1000) > 0 ? "CH2 water temperature low warning," : "";
  244. }
  245. var alarmCH1Status = BitConverter.ToInt16(alarmCH1, 0);
  246. var alarmCH2Status = BitConverter.ToInt16(alarmCH2, 0);
  247. Device.IsCH1Alarm = (alarmCH1Status & 0b_0000_0001) > 0;
  248. Device.IsCH2Alarm = (alarmCH2Status & 0b_0000_0001) > 0;
  249. if (Device.IsCH1Alarm)
  250. {
  251. reason += (alarmCH1Status & 0b_0000_0010) > 0 ? "CH1 water level low alarm," : "";
  252. reason += (alarmCH1Status & 0b_0000_0100) > 0 ? "CH1 water temperature high alarm," : "";
  253. reason += (alarmCH1Status & 0b_0000_1000) > 0 ? "CH1 water temperature low alarm," : "";
  254. reason += (alarmCH1Status & 0b_0001_0000) > 0 ? "CH1 water flow low alarm," : "";
  255. }
  256. if (Device.IsCH2Alarm)
  257. {
  258. reason += (alarmCH2Status & 0b_0000_0010) > 0 ? "CH2 water level low alarm," : "";
  259. reason += (alarmCH2Status & 0b_0000_0100) > 0 ? "CH2 water temperature high alarm," : "";
  260. reason += (alarmCH2Status & 0b_0000_1000) > 0 ? "CH2 water temperature low alarm," : "";
  261. reason += (alarmCH2Status & 0b_0001_0000) > 0 ? "CH2 water flow low alarm," : "";
  262. }
  263. Device.IsCH1On = (BitConverter.ToInt16(stateCH1, 0) & 0b_0000_0001) > 0;
  264. Device.IsCH2On = (BitConverter.ToInt16(stateCH2, 0) & 0b_0000_0001) > 0;
  265. Device.NoteError(reason);
  266. }
  267. handled = true;
  268. return true;
  269. }
  270. handled = false;
  271. return false;
  272. }
  273. }
  274. }