SmcHRSChillerHandler.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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.SmcHRSChillers
  9. {
  10. static class SMCHRSChillerConstant
  11. {
  12. public const string Start = ":";
  13. public const string End = "\r\n";
  14. public const string SlaveAddress = "01";
  15. public const string Function_R_M = "03";
  16. public const string Function_W_S = "06";
  17. public const string Function_W_M = "10";
  18. public const string Function_RW_M = "17";
  19. public const string Data_Address_GetAll = "0000";
  20. public const string Data_Address_GetTemp = "0000";
  21. public const string Data_Address_GetFlow = "0001";
  22. public const string Data_Address_GetPress = "0002";
  23. public const string Data_Address_GetElec = "0003";
  24. public const string Data_Address_Status1 = "0004";
  25. public const string Data_Address_Status2 = "0009";
  26. public const string Data_Address_Alarm1 = "0005";
  27. public const string Data_Address_Alarm2 = "0006";
  28. public const string Data_Address_Alarm3 = "0007";
  29. public const string Data_Address_SetTemp = "000B";
  30. public const string Data_Address_SetRun = "000C";
  31. public const string Data_Address_SetFlow = "000D";
  32. public const string SET_ON = SlaveAddress + Function_W_M + Data_Address_SetRun + "0001" + "02" + "0001";
  33. public const string SET_OFF = SlaveAddress + Function_W_M + Data_Address_SetRun + "0001" + "02" + "0000";
  34. public const string SET_TEMP = SlaveAddress + Function_W_M + Data_Address_SetTemp + "0001" + "02";
  35. //全部13个寄存器
  36. public const string GET_ALL = SlaveAddress + Function_R_M + Data_Address_GetAll + "000D";
  37. }
  38. //Modbus RTU
  39. public abstract class SmcHRSChillerHandler : HandlerBase
  40. {
  41. public SmcHRSChiller Device { get; }
  42. public string _command;
  43. protected string _parameter;
  44. private static byte _address = 0x01;
  45. protected SmcHRSChillerHandler(SmcHRSChiller device, string command)
  46. : base(BuildMessage(command))
  47. {
  48. Device = device;
  49. _command = command;
  50. _address = device.SlaveAddress;
  51. Name = command;
  52. }
  53. protected static string BuildMessage(string command)
  54. {
  55. var temp = ":" + command + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(command)).ToString("X2") + "\r\n";
  56. return ":" + command + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(command)).ToString("X2") + "\r\n";
  57. }
  58. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  59. {
  60. transactionComplete = true;
  61. AsciiMessage response = msg as AsciiMessage;
  62. if (!response.IsComplete)
  63. return false;
  64. return true;
  65. }
  66. }
  67. public class SmcHRSChillerSetOnOffHandler : SmcHRSChillerHandler
  68. {
  69. public SmcHRSChillerSetOnOffHandler(SmcHRSChiller device, bool isOn)
  70. : base(device, isOn ? SMCHRSChillerConstant.SET_ON : SMCHRSChillerConstant.SET_OFF)
  71. {
  72. }
  73. }
  74. public class SmcHRSChillerSetTemperatureHandler : SmcHRSChillerHandler
  75. {
  76. public SmcHRSChillerSetTemperatureHandler(SmcHRSChiller device, float temp)
  77. : base(device, BuildCommand(temp))
  78. {
  79. }
  80. private static string BuildCommand(float temp)
  81. {
  82. return SMCHRSChillerConstant.SET_TEMP + Convert.ToInt32(temp * 10).ToString("X4");
  83. }
  84. }
  85. //get all status register data
  86. public class SmcHRSChillerGetStatusHandler : SmcHRSChillerHandler
  87. {
  88. public SmcHRSChillerGetStatusHandler(SmcHRSChiller device)
  89. : base(device, SMCHRSChillerConstant.GET_ALL)
  90. {
  91. }
  92. string[] _status1Flag = new string[]
  93. {
  94. "Run",//(1) Run flag 0 = Stop 1 = Run
  95. //(2) Fault flag 0 = Not occurred 1 = Fault alarm given off
  96. //(3) Warning flag 0 = Not occurred 1 = Warning alarm given off
  97. //(4) Unused
  98. //(5) Press Unit flag 0 = MPa 1 = PSI
  99. //(6) Remote flag 0 = Local 1 = SER REMOTE
  100. //(7) Unused
  101. //(8) Unused
  102. //(9) Unused
  103. //(10)TEMP READY flag 0 = Condition isn’t formed 1 = Condition is formed
  104. //(11) Temp Unit flag 0 = °C 1 = °F
  105. //(12) Run timer set status 0= Not set 1= Set
  106. //(13) Stop timer set status 0= Not set 1= Set
  107. //(14) Reset after power failure set status 0= Not set 1= Set
  108. //(15) Anti-freezing set status 0= Not set 1= Set
  109. //(16) Automatic fluid filling condition 0= Not set 1= Set
  110. };
  111. string[] _alarm1Flag = new string[]
  112. {
  113. "Low level in tank",// 0=Not occurred, 1=occurred
  114. "High circulating fluid discharge temp",// 0=Not occurred, 1=occurred
  115. "Circulating fluid discharge temp. rise",// 0=Not occurred, 1=occurred
  116. "Circulating fluid discharge temp.",// 0=Not occurred, 1=occurred
  117. "High circulating fluid return temp.",// 0=Not occurred, 1=occurred
  118. "High circulating fluid discharge pressure",// 0=Not occurred, 1=occurred
  119. "Abnormal pump operation",// 0=Not occurred, 1=occurred
  120. "Circulating fluid discharge pressure rise",// 0=Not occurred, 1=occurred
  121. "Circulating fluid discharge pressure drop",// 0=Not occurred, 1=occurred
  122. "High compressor intake temp.",// 0=Fixed
  123. "Low compressor intake temp.",// 0=Not occurred, 1=occurred
  124. "Low super heat temperature",// 0=Not occurred, 1=occurred
  125. "High compressor discharge pressure",// 0=Not occurred, 1=occurred
  126. "Unused",// 0=Not occurred, 1=occurred
  127. "Refrigerant circuit pressure (high pressure side) drop",// 0=Not occurred, 1=occurred
  128. "Refrigerant circuit pressure (low pressure side) rise",// 0=Not occurred, 1=occurred
  129. };
  130. string[] _alarm2Flag = new string[]
  131. {
  132. "Refrigerant circuit pressure (low pressure side) drop",// 0=Not occurred, 1=occurred
  133. "Compressor overload",// 0=Not occurred, 1=occurred
  134. "Communication error",// 0=Not occurred, 1=occurred
  135. "Memory error",// 0=Not occurred, 1=occurred
  136. "DC line fuse cut",// 0=Not occurred, 1=occurred
  137. "Circulating fluid discharge temp. sensor failure",// 0=Not occurred, 1=occurred
  138. "Circulating fluid return temp. sensor failure",// 0=Not occurred, 1=occurred
  139. "Compressor intake temp. sensor failure",// 0=Not occurred, 1=occurred
  140. "Circulating fluid discharge pressure sensor failure",// 0=Not occurred, 1=occurred
  141. "Compressor discharge pressure sensor failure",// 0=Not occurred, 1=occurred
  142. "Compressor intake pressure sensor failure",// 0=Not occurred, 1=occurred
  143. "Maintenance of pump",// 0=Not occurred, 1=occurred
  144. "Maintenance of fan motor",// 0=Not occurred, 1=occurred
  145. "Maintenance of compressor",// 0=Not occurred, 1=occurred
  146. "Contact input 1 signal detection alarm",// 0=Not occurred, 1=occurred
  147. "Contact input 2 signal detection alarm",// 0=Not occurred, 1=occurred
  148. };
  149. string[] _alarm3Flag = new string[]
  150. {
  151. "Water leakage",// 0=Not occurred, 1=occurred
  152. "Electric resistivity/conductivity level rise",// 0=Not occurred, 1=occurred
  153. "Electric resistivity/conductivity level drop",// 0=Not occurred, 1=occurred
  154. "Electric resistivity/conductivity sensor error",// 0=Not occurred, 1=occurred
  155. "",//Reservation Fixed at 0
  156. "",//Reservation Fixed at 0
  157. "",//Reservation Fixed at 0
  158. "",//Reservation Fixed at 0
  159. "",//Reservation Fixed at 0
  160. "",//Reservation Fixed at 0
  161. "",//Reservation Fixed at 0
  162. "",//Reservation Fixed at 0
  163. "",//Reservation Fixed at 0
  164. "",//Reservation Fixed at 0
  165. "",//Reservation Fixed at 0
  166. "",//Reservation Fixed at 0
  167. };
  168. //: 01 17 1A 00F9 0000 0000 FFFF 0066 0030 0040 0000 0000 0000 0000 00C8 0000 39
  169. public override bool HandleMessage(MessageBase msg, out bool handled)
  170. {
  171. AsciiMessage response = msg as AsciiMessage;
  172. string cmd = response.RawMessage;
  173. if (cmd.Length < 61)
  174. {
  175. handled = true;
  176. Device.NoteError("Chiller device response error format message, should at least 60 return data length");
  177. return false;
  178. }
  179. if (cmd.Substring(5, 2) != "1A")
  180. {
  181. handled = true;
  182. Device.NoteError("Chiller device response error format message, register number should be 1A (26)");
  183. return false;
  184. }
  185. if (cmd.Substring(3, 2) != "03")
  186. {
  187. handled = true;
  188. Device.NoteError("Chiller device response error function code");
  189. return false;
  190. }
  191. string temp = cmd.Substring(7, 4);
  192. //string flow = cmd.Substring(11, 4);
  193. string pressure = cmd.Substring(15, 4);
  194. string resistanceRate = cmd.Substring(19, 4);
  195. string status1 = cmd.Substring(23, 4);
  196. string alarm1 = cmd.Substring(27, 4);
  197. string alarm2 = cmd.Substring(31, 4);
  198. string alarm3 = cmd.Substring(35, 4);
  199. string status2 = cmd.Substring(43, 4);
  200. string tempSet = cmd.Substring(51, 4);
  201. string cmdSet = cmd.Substring(55, 4);
  202. int value = 0;
  203. for (int i = 3; i > -1; i--)
  204. {
  205. value += Convert.ToInt32(temp.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  206. }
  207. Device.NoteTemp((float)value / 10);
  208. value = 0;
  209. for (int i = 3; i > -1; i--)
  210. {
  211. value += Convert.ToInt32(tempSet.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  212. }
  213. Device.NoteTempSetpoint((float)value / 10);
  214. value = 0;
  215. for (int i = 3; i > -1; i--)
  216. {
  217. value += Convert.ToInt32(status1.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  218. }
  219. bool IsRun = Convert.ToBoolean((int)Math.Pow(2, 0) & value);
  220. bool IsFault = Convert.ToBoolean((int)Math.Pow(2, 1) & value);
  221. bool IsWarning = Convert.ToBoolean((int)Math.Pow(2, 2) & value);
  222. bool FlowUnit = Convert.ToBoolean((int)Math.Pow(2, 3) & value);
  223. bool PressUnit = Convert.ToBoolean((int)Math.Pow(2, 4) & value);
  224. bool IsRemote = Convert.ToBoolean((int)Math.Pow(2, 5) & value);
  225. bool IsTimeout = Convert.ToBoolean((int)Math.Pow(2, 8) & value);
  226. bool IsTempReady = Convert.ToBoolean((int)Math.Pow(2, 9) & value);
  227. Device.NoteIsRun(IsRun);
  228. Device.NoteIsFault(IsFault);
  229. Device.NoteIsWarning(IsWarning);
  230. Device.NoteFlowUnit(FlowUnit);
  231. Device.NotePressUnit(PressUnit);
  232. Device.NoteIsRemote(IsRemote);
  233. Device.NoteIsTimeout(IsTimeout);
  234. Device.NoteIsTempReady(IsTempReady);
  235. value = 0;
  236. for (int i = 3; i > -1; i--)
  237. {
  238. value += Convert.ToInt32(alarm1.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  239. }
  240. for (int i = 0; i < 16; i++)
  241. {
  242. if (Convert.ToBoolean((int)Math.Pow(2, i) & value))
  243. {
  244. Device.IsCH1Alarm = true;
  245. Device.NoteError($"Alarm {_alarm1Flag[i]}");
  246. }
  247. }
  248. value = 0;
  249. for (int i = 3; i > -1; i--)
  250. {
  251. value += Convert.ToInt32(alarm2.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  252. }
  253. for (int i = 0; i < 16; i++)
  254. {
  255. if (Convert.ToBoolean((int)Math.Pow(2, i) & value))
  256. {
  257. Device.IsCH1Alarm = true;
  258. Device.NoteError($"Alarm {16 + i}: {_alarm2Flag[i]}");
  259. }
  260. }
  261. value = 0;
  262. for (int i = 3; i > -1; i--)
  263. {
  264. value += Convert.ToInt32(alarm3.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  265. }
  266. for (int i = 0; i < 16; i++)
  267. {
  268. if (Convert.ToBoolean((int)Math.Pow(2, i) & value))
  269. {
  270. Device.IsCH1Alarm = true;
  271. Device.NoteError($"Alarm {16 + i}: {_alarm3Flag[i]}");
  272. }
  273. }
  274. handled = true;
  275. return true;
  276. }
  277. }
  278. }