AIRSYSChiller.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using Aitex.Core.Common.DeviceData;
  5. using Aitex.Core.RT.DataCenter;
  6. using Aitex.Core.RT.Device;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.IOCore;
  9. using Aitex.Core.RT.Log;
  10. using Aitex.Core.RT.OperationCenter;
  11. using Aitex.Core.RT.SCCore;
  12. using Aitex.Core.RT.Tolerance;
  13. using Aitex.Core.Util;
  14. using MECF.Framework.Common.Communications;
  15. using MECF.Framework.Common.Device.Bases;
  16. using MECF.Framework.Common.Equipment;
  17. namespace Venus_RT.Devices
  18. {
  19. class AIRSYSChiller : ChillerBase
  20. {
  21. private float _controlTcSetPoint;
  22. private float _controlTcFeedback;
  23. private readonly double _scSetPointLimitMax;
  24. private readonly double _scSetPointLimitMin;
  25. private readonly string _PortNum = "COM48";
  26. private readonly AsyncSerialPort _serial;
  27. private readonly DeviceTimer _timerQueryStatus = new DeviceTimer();
  28. private const ushort CHK_ST_INTERVAL = 300;
  29. public enum Operation
  30. {
  31. ReadTemperature,
  32. ReadStatus,
  33. ReadFlowRate,
  34. ReadResistivity,
  35. UnitStart,
  36. UnitStop,
  37. SelectThermoProbe,
  38. SetTemperature,
  39. SetResistivity,
  40. }
  41. private readonly Dictionary<Operation, string> _noneParaCommandOp = new Dictionary<Operation, string>
  42. {
  43. {Operation.ReadTemperature, "AA10\r" },
  44. {Operation.ReadStatus, "AA42\r" },
  45. {Operation.ReadFlowRate, "AAB1\r" },
  46. {Operation.ReadResistivity, "AAB2\r" },
  47. {Operation.UnitStart, "AA90\r" },
  48. {Operation.UnitStop, "AA91\r" },
  49. {Operation.SelectThermoProbe, "AAA1\r" },
  50. };
  51. private readonly Dictionary<Operation, string> _oneParaCommandOp = new Dictionary<Operation, string>
  52. {
  53. {Operation.SetTemperature, "AAB0{0:X4}\r" },
  54. {Operation.SetResistivity, "AAB3{0:X4}\r" },
  55. };
  56. public double SetPointLimitMax
  57. {
  58. get
  59. {
  60. return _scSetPointLimitMax; ;
  61. }
  62. }
  63. public double SetPointLimitMin
  64. {
  65. get
  66. {
  67. return _scSetPointLimitMin; ;
  68. }
  69. }
  70. [Subscription(AITChillerProperty.ControlTcSetPoint)]
  71. public float ControlTcSetPoint
  72. {
  73. get
  74. {
  75. return _controlTcSetPoint;
  76. }
  77. }
  78. [Subscription(AITChillerProperty.ControlTcFeedback)]
  79. public float ControlTcFeedback
  80. {
  81. get
  82. {
  83. return _controlTcFeedback;
  84. }
  85. }
  86. [Subscription(AITChillerProperty.CoolantInletTempFeedback)]
  87. public override float CoolantInletTcFeedback
  88. {
  89. get
  90. {
  91. return GetAiValue($"{Module}.AI_Coolant_Inlet_Temp");
  92. }
  93. }
  94. [Subscription(AITChillerProperty.CoolantOutletTempFeedback)]
  95. public override float CoolantOutletTcFeedback
  96. {
  97. get
  98. {
  99. return GetAiValue($"{Module}.AI_Coolant_Outlet_Temp");
  100. }
  101. }
  102. public override AITChillerData DeviceData
  103. {
  104. get
  105. {
  106. AITChillerData deviceData = new AITChillerData
  107. {
  108. Module = Module,
  109. DeviceName = Name,
  110. DeviceModule = Module,
  111. DeviceSchematicId = DeviceID,
  112. DisplayName = Display,
  113. IsError = false,
  114. IsWarning = false,
  115. IsPowerOn = IsRunning,
  116. ScaleMax = SetPointLimitMax,
  117. ScaleMin = SetPointLimitMin,
  118. SetPoint = ControlTcSetPoint,
  119. FeedBack = CoolantOutletTcFeedback,
  120. CoolantInletFeedBack = CoolantInletTcFeedback,
  121. CoolantOutletFeedBack = CoolantOutletTcFeedback,
  122. Unit = "℃",
  123. };
  124. return deviceData;
  125. }
  126. }
  127. private SCConfigItem _scAlarmRange;
  128. private SCConfigItem _scEnableAlarm;
  129. private SCConfigItem _scAlarmTime;
  130. private ToleranceChecker _toleranceChecker = new ToleranceChecker();
  131. private string _lastAlarmString;
  132. private int _operation_flag = 0;
  133. public AIRSYSChiller(ModuleName mod, string name) : base(mod.ToString(), name, name, "")
  134. {
  135. if (Module == "PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue<bool>($"PMB.Chiller.EnableChiller")) return;
  136. _PortNum = SC.GetStringValue($"{mod}.{Name}.Port");
  137. _scSetPointLimitMax = SC.GetValue<double>($"{Module}.{Name}.SetPointLimitMax");
  138. _scSetPointLimitMin = SC.GetValue<double>($"{Module}.{Name}.SetPointLimitMin");
  139. _scEnableAlarm = SC.GetConfigItem($"{Module}.{Name}.EnableToleranceAlarm");
  140. _scAlarmRange = SC.GetConfigItem($"{Module}.{Name}.ToleranceAlarmRange");
  141. _scAlarmTime = SC.GetConfigItem($"{Module}.{Name}.ToleranceAlarmTime");
  142. _serial = new AsyncSerialPort(_PortNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.Two, "\r", true);
  143. }
  144. public override bool Initialize()
  145. {
  146. base.Initialize();
  147. if (!_serial.Open())
  148. {
  149. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, "AIRSYS Chiller串口无法打开");
  150. return false;
  151. }
  152. _serial.OnDataChanged += OnPortDataChanged;
  153. _serial.OnErrorHappened += OnErrorOccurred;
  154. _timerQueryStatus.Start(CHK_ST_INTERVAL);
  155. return true;
  156. }
  157. public override void Monitor()
  158. {
  159. if (_timerQueryStatus.IsTimeout())
  160. {
  161. Operation queryOp = _operation_flag++ % 2 == 0 ? Operation.ReadStatus : Operation.ReadTemperature;
  162. _sendCommand(queryOp);
  163. _timerQueryStatus.Start(CHK_ST_INTERVAL);
  164. }
  165. if (IsRunning && _scEnableAlarm != null && _scEnableAlarm.BoolValue)
  166. {
  167. var range = _scAlarmRange.IntValue;
  168. var time = _scAlarmTime.IntValue;
  169. _toleranceChecker.Monitor(_controlTcFeedback, _controlTcSetPoint - Math.Abs(range), _controlTcSetPoint + Math.Abs(range), time);
  170. if (_toleranceChecker.Trig)
  171. {
  172. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, Display + $" temperature out of tolerance in {time:0} seconds");
  173. }
  174. }
  175. else
  176. {
  177. _toleranceChecker.RST = true;
  178. }
  179. }
  180. private void OnPortDataChanged(string obj)
  181. {
  182. if(obj.StartsWith("AA"))
  183. {
  184. string op = obj.Substring(2, 2);
  185. switch(op)
  186. {
  187. case "42":
  188. ushort significate_status = ushort.Parse(obj.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
  189. ushort low_significatate_status = ushort.Parse(obj.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
  190. _process_signicate_chiller_status(significate_status);
  191. _process_low_signicate_chiller_status(low_significatate_status);
  192. break;
  193. case "10":
  194. _controlTcFeedback = (float)Int32.Parse(obj.Substring(4, 4), System.Globalization.NumberStyles.HexNumber) / (float)10.0;
  195. break;
  196. }
  197. }
  198. else
  199. {
  200. _noRepeatAlarm($"AIRSYS chiller receive wrong message:{obj}");
  201. }
  202. }
  203. private void OnErrorOccurred(string obj)
  204. {
  205. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, $"[{Module}] AIRSYS chiller error: [{obj}]");
  206. }
  207. public override void SetChillerTemp(float value, float offset)
  208. {
  209. int isettemp = Convert.ToInt16((value - offset) * 10);
  210. _sendCommand(Operation.SetTemperature, isettemp);
  211. _controlTcSetPoint = value;
  212. }
  213. public override void SetChillerOnOff(bool on)
  214. {
  215. IsRunning = on;
  216. _sendCommand(on ? Operation.UnitStart : Operation.UnitStop);
  217. }
  218. private bool _sendCommand(Operation op)
  219. {
  220. if (_noneParaCommandOp.ContainsKey(op))
  221. {
  222. return _sendCmd(_noneParaCommandOp[op]);
  223. }
  224. _noRepeatAlarm($"Adixen Turbo Pump: The {op} command need parameters");
  225. return false;
  226. }
  227. private bool _sendCommand(Operation op, int data)
  228. {
  229. if (_oneParaCommandOp.ContainsKey(op))
  230. {
  231. var cmd = string.Format(_oneParaCommandOp[op], data);
  232. return _sendCmd(cmd);
  233. }
  234. _noRepeatAlarm($"Adixen Turbo Pump: The command {op} does not need one parameter");
  235. return false;
  236. }
  237. private bool _sendCmd(string cmd)
  238. {
  239. return _serial.Write(cmd);
  240. }
  241. private void _noRepeatAlarm(string alarm)
  242. {
  243. if (_lastAlarmString != alarm)
  244. {
  245. _lastAlarmString = alarm;
  246. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, alarm);
  247. }
  248. }
  249. private void _process_signicate_chiller_status(ushort status)
  250. {
  251. StringBuilder errorInfo = new StringBuilder();
  252. if((status & 0x80) != 0)
  253. {
  254. errorInfo.Append("Memory Data Error Fault; ");
  255. }
  256. if ((status & 0x40) != 0)
  257. {
  258. errorInfo.Append("Controller Error Fault; ");
  259. }
  260. if ((status & 0x20) != 0)
  261. {
  262. errorInfo.Append("Pump Breaker Trip Fault; ");
  263. }
  264. if ((status & 0x10) != 0)
  265. {
  266. errorInfo.Append("Heater Breaker Trip Fault; ");
  267. }
  268. if ((status & 0x08) != 0)
  269. {
  270. errorInfo.Append("Water Leak Fault; ");
  271. }
  272. if ((status & 0x04) != 0)
  273. {
  274. errorInfo.Append("Return Low Flow Fault; ");
  275. }
  276. if ((status & 0x02) != 0)
  277. {
  278. errorInfo.Append("Reservoir High Temperature Fault; ");
  279. }
  280. if ((status & 0x01) != 0)
  281. {
  282. errorInfo.Append("Reservoir Low Level Fault; ");
  283. }
  284. if(errorInfo.Length > 10)
  285. {
  286. IsError = true;
  287. _noRepeatAlarm($"AIRSYS chiller status error:{errorInfo}");
  288. }
  289. else
  290. {
  291. IsError = true;
  292. }
  293. }
  294. private void _process_low_signicate_chiller_status(ushort status)
  295. {
  296. StringBuilder errorInfo = new StringBuilder();
  297. if ((status & 0x80) != 0)
  298. {
  299. errorInfo.Append("Temperature Fuse Fault; ");
  300. }
  301. if ((status & 0x40) != 0)
  302. {
  303. errorInfo.Append("DC Power Fuse Fault; ");
  304. }
  305. if ((status & 0x02) != 0)
  306. {
  307. errorInfo.Append("Reservoir High Temperature Warning; ");
  308. }
  309. if ((status & 0x01) != 0)
  310. {
  311. errorInfo.Append("Reservoir Low Level Warning; ");
  312. }
  313. if (errorInfo.Length > 10)
  314. {
  315. _noRepeatAlarm($"AIRSYS chiller status error:{errorInfo}");
  316. }
  317. }
  318. }
  319. }