AIRSYSChiller.cs 13 KB

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