AIRSYSChiller.cs 12 KB

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