AIRSYSChiller.cs 12 KB

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