SmcHRZChillerHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using MECF.Framework.Common.Communications;
  2. using MECF.Framework.Common.Utilities;
  3. using System;
  4. using System.Collections.Generic;
  5. using Aitex.Core.RT.Device;
  6. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Chillers.SmcHRZChillers
  7. {
  8. static class SMCHRZChillerConstant
  9. {
  10. public const string Start = ":";
  11. public const string End = "\r\n";
  12. public const string SlaveAddress = "01";
  13. public const string Function_R_M = "17";
  14. public const string Function_W_S = "17";
  15. public const string Function_W_M = "17";
  16. public const string Function_RW_M = "17";
  17. public const string Data_Address_GetAll = "0000";
  18. public const string Data_Address_GetTemp = "0000";
  19. public const string Data_Address_GetFlow = "0001";
  20. public const string Data_Address_GetPress = "0002";
  21. public const string Data_Address_GetElec = "0003";
  22. public const string Data_Address_Status1 = "0004";
  23. public const string Data_Address_Alarm1 = "0005";
  24. public const string Data_Address_Alarm2 = "0006";
  25. public const string Data_Address_SetTemp = "000B";
  26. public const string Data_Address_SetRun = "000C";
  27. public const string Data_Address_SetFlow = "000D";
  28. public const string SET_ON = SlaveAddress + Function_W_S + "0000" + "0000" + Data_Address_SetRun + "0001" + "02" + "0001";
  29. public const string SET_OFF = SlaveAddress + Function_W_S + "0000" + "0000" + Data_Address_SetRun + "0001" + "02" + "0000";
  30. public const string SET_TEMP = SlaveAddress + Function_W_S + "0000" + "0000" + Data_Address_SetTemp + "0001" + "02";
  31. //全部13个寄存器
  32. public const string GET_ALL = SlaveAddress + Function_R_M + Data_Address_GetAll + "000D" + "0000"+"0000"+"00";
  33. }
  34. //Modbus RTU
  35. public abstract class SmcHRZChillerHandler : HandlerBase
  36. {
  37. public SmcHRZChiller Device { get; }
  38. public string _command;
  39. protected string _parameter;
  40. private static byte _address = 0x01;
  41. protected SmcHRZChillerHandler(SmcHRZChiller device, string command)
  42. : base(BuildMessage(command))
  43. {
  44. Device = device;
  45. _command = command;
  46. _address = device.SlaveAddress;
  47. Name = command;
  48. }
  49. protected static string BuildMessage(string command)
  50. {
  51. return ":" + command + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(command)).ToString("X2") + "\r\n";
  52. }
  53. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  54. {
  55. transactionComplete = true;
  56. AsciiMessage response = msg as AsciiMessage;
  57. if (!response.IsComplete)
  58. return false;
  59. return true;
  60. }
  61. }
  62. public class SmcHRZChillerSetOnOffHandler : SmcHRZChillerHandler
  63. {
  64. public SmcHRZChillerSetOnOffHandler(SmcHRZChiller device, bool isOn)
  65. : base(device, isOn ? SMCHRZChillerConstant.SET_ON : SMCHRZChillerConstant.SET_OFF)
  66. {
  67. }
  68. }
  69. public class SmcHRZChillerSetTemperatureHandler : SmcHRZChillerHandler
  70. {
  71. public SmcHRZChillerSetTemperatureHandler(SmcHRZChiller device, float temp)
  72. : base(device, BuildCommand(temp))
  73. {
  74. }
  75. private static string BuildCommand(float temp)
  76. {
  77. return SMCHRZChillerConstant.SET_TEMP + Convert.ToInt32(temp * 10).ToString("X4");
  78. }
  79. }
  80. //get all status register data
  81. public class SmcHRZChillerGetStatusHandler : SmcHRZChillerHandler
  82. {
  83. public SmcHRZChillerGetStatusHandler(SmcHRZChiller device)
  84. : base(device, SMCHRZChillerConstant.GET_ALL)
  85. {
  86. }
  87. string[] _statusFlag = new string[]
  88. {
  89. "Run",//(1) Run flag 0 = Stop 1 = Run
  90. //(2) Fault flag 0 = Not occurred 1 = Fault alarm given off
  91. //(3) Warning flag 0 = Not occurred 1 = Warning alarm given off
  92. //(4) Flow Unit flag 0 = LPM 1 = GPM
  93. //(5) Press Unit flag 0 = MPa 1 = PSI
  94. //(6) Remote flag 0 = Local 1 = SER REMOTE
  95. //(7) AUTO PURGE ready flag 0 = Condition isn’t formed 1 = Condition is formed
  96. //(8) AUTO PURGE running flag 0 = Stop 1 = running
  97. //(9) Time out 0 = Not occurred 1 = Occurred
  98. //(10)TEMP READY flag 0 = Condition isn’t formed 1 = Condition is formed
  99. //(11) to (16) reservation Fixed at 0
  100. };
  101. string[] _alarm1Flag = new string[]
  102. {
  103. "01 Water Leak Detect",// FLT 0=Not occurred, 1=occurred
  104. "02 Incorrect Phase Error",// FLT 0=Not occurred, 1=occurred
  105. "03 RFGT High Press FLT",// 0=Not occurred, 1=occurred
  106. "04 CPRSR Overheat FLT",// 0=Not occurred, 1=occurred
  107. "05 Reservoir Low Level FLT",// 0=Not occurred, 1=occurred
  108. "06 Reservoir Low Level WRN",// 0=Not occurred, 1=occurred
  109. "07 Reservoir High Level WRN",// 0=Not occurred, 1=occurred
  110. "08 Temp. Fuse Cutout FLT",// 0=Not occurred, 1=occurred
  111. "09 Reservoir High Temp.",// FLT 0=Not occurred, 1=occurred
  112. "10 Reservation",// 0=Fixed
  113. "11 Reservoir High Temp.WRN",// 0=Not occurred, 1=occurred
  114. "12 Return Low Flow FLT",// 0=Not occurred, 1=occurred
  115. "13 Return Low Flow WRN",// 0=Not occurred, 1=occurred
  116. "14 Heater Breaker Trip FLT",// 0=Not occurred, 1=occurred
  117. "15 Pump Breaker Trip FLT",// 0=Not occurred, 1=occurred
  118. "16 CPRSR Breaker Trip FLT",// 0=Not occurred, 1=occurred
  119. };
  120. string[] _alarm2Flag = new string[]
  121. {
  122. "17 Interlock Fuse Cutout FLT",// 0=Not occurred, 1=occurred
  123. "18 DC Power Fuse Cutout WRN",// 0=Not occurred, 1=occurred
  124. "19 FAN Motor Stop WRN",// 0=Not occurred, 1=occurred
  125. "20 Internal Pump Time Out WRN",// 0=Not occurred, 1=occurred
  126. "21 Controller Error FLT",// 0=Not occurred, 1=occurred
  127. "22 Memory Data Error FLT",// 0=Not occurred, 1=occurred
  128. "23 Communication Error WRN",// 0=Not occurred, 1=occurred
  129. "24* DI Low Level WRN",// 0=Not occurred, 1=occurred
  130. "25* Pump Inverter Error FLT",// 0=Not occurred, 1=occurred
  131. "26* DNET Comm. Error WRN",// 0=Not occurred, 1=occurred
  132. "27* DNET Comm. Error FLT",// 0=Not occurred, 1=occurred
  133. "28* CPRSR INV Error FLT",// 0=Not occurred, 1=occurred
  134. "",//29~32 Reservation Fixed at 0
  135. "",//29~32 Reservation Fixed at 0
  136. "",//29~32 Reservation Fixed at 0
  137. "",//29~32 Reservation Fixed at 0
  138. };
  139. //: 01 17 1A 00F9 0000 0000 FFFF 0066 0030 0040 0000 0000 0000 0000 00C8 0000 39
  140. public override bool HandleMessage(MessageBase msg, out bool handled)
  141. {
  142. AsciiMessage response = msg as AsciiMessage;
  143. string cmd = response.RawMessage;
  144. if (cmd.Length < 61)
  145. {
  146. handled = true;
  147. Device.NoteError("Chiller device response error format message, should at least 60 return data length");
  148. return false;
  149. }
  150. if (cmd.Substring(5, 2) != "1A")
  151. {
  152. handled = true;
  153. Device.NoteError("Chiller device response error format message, register number should be 1A (26)");
  154. return false;
  155. }
  156. if (cmd.Substring(3, 2) != "17")
  157. {
  158. handled = true;
  159. Device.NoteError("Chiller device response error function code");
  160. return false;
  161. }
  162. string temp = cmd.Substring(7, 4);
  163. string flow = cmd.Substring(11, 4);
  164. string pressure = cmd.Substring(15, 4);
  165. string resistanceRate = cmd.Substring(19, 4);
  166. string status = cmd.Substring(23, 4);
  167. string alarm1 = cmd.Substring(27, 4);
  168. string alarm2 = cmd.Substring(31, 4);
  169. string tempSet = cmd.Substring(51, 4);
  170. string cmdSet = cmd.Substring(55, 4);
  171. int value = 0;
  172. for (int i = 3; i > -1; i--)
  173. {
  174. value += Convert.ToInt32(temp.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  175. }
  176. Device.NoteTemp((float)value/10);
  177. value = 0;
  178. for (int i = 3; i > -1; i--)
  179. {
  180. value += Convert.ToInt32(status.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  181. }
  182. bool IsRun = Convert.ToBoolean((int)Math.Pow(2, 0) & value);
  183. bool IsFault = Convert.ToBoolean((int)Math.Pow(2, 1) & value);
  184. bool IsWarning = Convert.ToBoolean((int)Math.Pow(2, 2) & value);
  185. bool FlowUnit = Convert.ToBoolean((int)Math.Pow(2, 3) & value);
  186. bool PressUnit = Convert.ToBoolean((int)Math.Pow(2, 4) & value);
  187. bool IsRemote = Convert.ToBoolean((int)Math.Pow(2, 5) & value);
  188. bool IsTimeout = Convert.ToBoolean((int)Math.Pow(2, 8) & value);
  189. bool IsTempReady = Convert.ToBoolean((int)Math.Pow(2, 9) & value);
  190. Device.NoteIsRun(IsRun);
  191. Device.NoteIsFault(IsFault);
  192. Device.NoteIsWarning(IsWarning);
  193. Device.NoteFlowUnit(FlowUnit);
  194. Device.NotePressUnit(PressUnit);
  195. Device.NoteIsRemote(IsRemote);
  196. Device.NoteIsTimeout(IsTimeout);
  197. Device.NoteIsTempReady(IsTempReady);
  198. value = 0;
  199. for (int i = 3; i > -1; i--)
  200. {
  201. value += Convert.ToInt32(alarm1.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  202. }
  203. for (int i = 0; i < 16; i++)
  204. {
  205. if (Convert.ToBoolean((int) Math.Pow(2, i) & value))
  206. {
  207. Device.NoteError($"Alarm {_alarm1Flag[i]}");
  208. }
  209. }
  210. value = 0;
  211. for (int i = 3; i > -1; i--)
  212. {
  213. value += Convert.ToInt32(alarm2.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  214. }
  215. for (int i = 0; i < 16; i++)
  216. {
  217. if (Convert.ToBoolean((int)Math.Pow(2, i) & value))
  218. {
  219. Device.NoteError($"Alarm {16 + i}: {_alarm2Flag[i]}");
  220. }
  221. }
  222. handled = true;
  223. return true;
  224. }
  225. }
  226. }