AIRSYSChiller.cs 13 KB

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