SMCChiller.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Concurrent;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. using Aitex.Core.Common.DeviceData;
  8. using Aitex.Core.RT.DataCenter;
  9. using Aitex.Core.RT.Device;
  10. using Aitex.Core.RT.Event;
  11. using Aitex.Core.RT.IOCore;
  12. using Aitex.Core.RT.Log;
  13. using Aitex.Core.RT.OperationCenter;
  14. using Aitex.Core.RT.SCCore;
  15. using Aitex.Core.RT.Tolerance;
  16. using Aitex.Core.Util;
  17. using MECF.Framework.Common.Communications;
  18. using MECF.Framework.Common.DataCenter;
  19. using MECF.Framework.Common.Device.Bases;
  20. using MECF.Framework.Common.Equipment;
  21. using MECF.Framework.Common.Utilities;
  22. using Venus_Core;
  23. namespace Venus_RT.Devices
  24. {
  25. public enum SMCChillerState { ON = 0, OFF, Connected, Disconnected, Unknown, ERROR }
  26. static class SMCChillerMessage
  27. {
  28. public const string Start = ":";
  29. public const string End = "\r\n";
  30. public const string SlaveAddress = "01";
  31. public const string Function_R_M = "03";
  32. public const string Function_W_S = "06";
  33. public const string Function_W_M = "10";
  34. public const string Function_RW_M = "17";
  35. public const string Data_Address_GetTemp = "0000";
  36. public const string Data_Address_GetPress = "0002";
  37. public const string Data_Address_GetElec = "0003";
  38. public const string Data_Address_Status1 = "0004";
  39. public const string Data_Address_Alarm1 = "0005";
  40. public const string Data_Address_Alarm2 = "0006";
  41. public const string Data_Address_Alarm3 = "0007";
  42. public const string Data_Address_Status2 = "0009";
  43. public const string Data_Address_SetTemp = "000B";
  44. public const string Data_Address_SetRun = "000C";
  45. public const string SET_ON = SlaveAddress + Function_W_S + Data_Address_SetRun + "0001";
  46. public const string SET_OFF = SlaveAddress + Function_W_S + Data_Address_SetRun + "0000";
  47. public const string SET_TEMP = SlaveAddress + Function_W_S + Data_Address_SetTemp;
  48. public const string GET_ALL = SlaveAddress + Function_R_M + Data_Address_GetTemp + "000B";
  49. }
  50. class SMCChiller : ChillerBase//, IoHeaterController
  51. {
  52. public SMCChillerState StatusSMC { get; set; }
  53. private float _controlTcSetPoint;
  54. private float _controlTcFeedback;
  55. private readonly double _scSetPointLimitMax;
  56. private readonly double _scSetPointLimitMin;
  57. private readonly string _PortNum = "COM92";
  58. private readonly AsyncSerialPort _serial;
  59. private readonly DeviceTimer _timerQueryStatus = new DeviceTimer();
  60. private const ushort CHK_ST_INTERVAL = 1000;
  61. bool[] Statusflag = new bool[32]; //状态On or Off
  62. bool[] LastStatusflag = new bool[32]; //上次循环 状态On or Off
  63. bool[] Alarmflag = new bool[48]; //报警On or Off
  64. bool[] LastAlarmflag = new bool[48]; //上次循环 报警On or Off
  65. string[] AlarmMsg = new string[48]; //报警消息
  66. bool[] NeedToStop = new bool[48]; //报警是否强制停机
  67. BlockingCollection<string> blockingCollection = new BlockingCollection<string>();
  68. private void SetAlarmMsg()
  69. {
  70. AlarmMsg[0] = "Low level in tank";
  71. AlarmMsg[1] = "High circulating fluid discharge temp";
  72. AlarmMsg[2] = "Circulating fluid discharge temp. rise";
  73. AlarmMsg[3] = "Circulating fluid discharge temp.";
  74. AlarmMsg[4] = "High circulating fluid return temp.";
  75. AlarmMsg[5] = "High circulating fluid discharge pressure";
  76. AlarmMsg[6] = "Abnormal pump operation";
  77. AlarmMsg[7] = "Circulating fluid discharge pressure rise";
  78. AlarmMsg[8] = "Circulating fluid discharge pressure drop";
  79. AlarmMsg[9] = "High compressor intake temp.";
  80. AlarmMsg[10] = "Low compressor intake temp.";
  81. AlarmMsg[11] = "Low super heat temperature";
  82. AlarmMsg[12] = "High compressor discharge pressure";
  83. //AlarmMsg[13] Unused
  84. AlarmMsg[14] = "Refrigerant circuit pressure (high pressure side) drop";
  85. AlarmMsg[15] = "Refrigerant circuit pressure (low pressure side) rise";
  86. AlarmMsg[16] = "Refrigerant circuit pressure (low pressure side) drop";
  87. AlarmMsg[17] = "Compressor overload";
  88. AlarmMsg[18] = "Communication error";
  89. AlarmMsg[19] = "Memory error";
  90. AlarmMsg[20] = "DC line fuse cut";
  91. AlarmMsg[21] = "Circulating fluid discharge temp. sensor failure";
  92. AlarmMsg[22] = "Circulating fluid return temp. sensor failure";
  93. AlarmMsg[23] = "Compressor intake temp. sensor failure";
  94. AlarmMsg[24] = "Circulating fluid discharge pressure sensor failure";
  95. AlarmMsg[25] = "Compressor discharge pressure sensor failure";
  96. AlarmMsg[26] = "Compressor intake pressure sensor failure";
  97. AlarmMsg[27] = "Maintenance of pump";
  98. AlarmMsg[28] = "Maintenance of fan motor";
  99. AlarmMsg[29] = "Maintenance of compressor";
  100. AlarmMsg[30] = "Contact input 1 signal detection alarm";
  101. AlarmMsg[31] = "Contact input 2 signal detection alarm";
  102. AlarmMsg[32] = "Water leakage";
  103. AlarmMsg[33] = "Electric resistivity/conductivity level rise";
  104. AlarmMsg[34] = "Electric resistivity/conductivity level drop";
  105. AlarmMsg[35] = "Electric resistivity/conductivity sensor error";
  106. //AlarmMsg[36] Unused
  107. NeedToStop[0] = true;
  108. NeedToStop[1] = true;
  109. NeedToStop[4] = true;
  110. NeedToStop[5] = true;
  111. NeedToStop[6] = true;
  112. NeedToStop[9] = true;
  113. NeedToStop[10] = true;
  114. NeedToStop[11] = true;
  115. NeedToStop[12] = true;
  116. NeedToStop[14] = true;
  117. NeedToStop[15] = true;
  118. NeedToStop[16] = true;
  119. NeedToStop[17] = true;
  120. NeedToStop[19] = true;
  121. NeedToStop[20] = true;
  122. NeedToStop[21] = true;
  123. NeedToStop[22] = true;
  124. NeedToStop[23] = true;
  125. NeedToStop[24] = true;
  126. NeedToStop[25] = true;
  127. NeedToStop[26] = true;
  128. NeedToStop[30] = true;
  129. NeedToStop[31] = true;
  130. NeedToStop[32] = true;
  131. }
  132. public double SetPointLimitMax
  133. {
  134. get
  135. {
  136. return _scSetPointLimitMax; ;
  137. }
  138. }
  139. public double SetPointLimitMin
  140. {
  141. get
  142. {
  143. return _scSetPointLimitMin; ;
  144. }
  145. }
  146. [Subscription(AITChillerProperty.IsRunning)]
  147. public override bool IsRunning
  148. {
  149. get
  150. {
  151. return StatusSMC == SMCChillerState.ON;
  152. }
  153. }
  154. [Subscription(AITChillerProperty.IsError)]
  155. public override bool IsError
  156. {
  157. get
  158. {
  159. return StatusSMC == SMCChillerState.ERROR || StatusSMC == SMCChillerState.Disconnected;
  160. }
  161. }
  162. [Subscription(AITChillerProperty.ControlTcSetPoint)]
  163. public float ControlTcSetPoint
  164. {
  165. get
  166. {
  167. return _controlTcSetPoint;
  168. }
  169. }
  170. [Subscription(AITChillerProperty.ControlTcFeedback)]
  171. public float ControlTcFeedback
  172. {
  173. get
  174. {
  175. return _controlTcFeedback;
  176. }
  177. }
  178. [Subscription(AITChillerProperty.CoolantInletTempFeedback)]
  179. public override float CoolantInletTcFeedback
  180. {
  181. get
  182. {
  183. return GetAiValue($"{Module}.AI_Coolant_Inlet_Temp");
  184. }
  185. }
  186. [Subscription(AITChillerProperty.CoolantOutletTempFeedback)]
  187. public override float CoolantOutletTcFeedback
  188. {
  189. get
  190. {
  191. return GetAiValue($"{Module}.AI_Coolant_Outlet_Temp");
  192. }
  193. }
  194. public override AITChillerData DeviceData
  195. {
  196. get
  197. {
  198. AITChillerData deviceData = new AITChillerData
  199. {
  200. Module = Module,
  201. DeviceName = Name,
  202. DeviceModule = Module,
  203. DeviceSchematicId = DeviceID,
  204. DisplayName = Display,
  205. IsError = false,
  206. IsWarning = false,
  207. IsPowerOn = IsRunning,
  208. ScaleMax = SetPointLimitMax,
  209. ScaleMin = SetPointLimitMin,
  210. SetPoint = ControlTcSetPoint,
  211. FeedBack = CoolantOutletTcFeedback,
  212. CoolantInletFeedBack = CoolantInletTcFeedback,
  213. CoolantOutletFeedBack = CoolantOutletTcFeedback,
  214. Unit = "℃",
  215. };
  216. return deviceData;
  217. }
  218. }
  219. private SCConfigItem _scAlarmRange;
  220. private SCConfigItem _scEnableAlarm;
  221. private SCConfigItem _scAlarmTime;
  222. private ToleranceChecker _toleranceChecker = new ToleranceChecker();
  223. // --------------------------Constructor-----------------------
  224. //
  225. public SMCChiller(ModuleName mod, string name) : base(mod.ToString(), name, name, "")
  226. {
  227. if (Module == "PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue<bool>($"PMB.Chiller.EnableChiller")) return;
  228. _PortNum = SC.GetStringValue($"{mod}.{Name}.Port");
  229. _scSetPointLimitMax = SC.GetValue<double>($"{Module}.{Name}.SetPointLimitMax");
  230. _scSetPointLimitMin = SC.GetValue<double>($"{Module}.{Name}.SetPointLimitMin");
  231. StatusSMC = SMCChillerState.Unknown;
  232. _scEnableAlarm = SC.GetConfigItem($"{Module}.{Name}.EnableToleranceAlarm");
  233. _scAlarmRange = SC.GetConfigItem($"{Module}.{Name}.ToleranceAlarmRange");
  234. _scAlarmTime = SC.GetConfigItem($"{Module}.{Name}.ToleranceAlarmTime");
  235. _serial = new AsyncSerialPort(_PortNum, 9600, 7, System.IO.Ports.Parity.Even, System.IO.Ports.StopBits.One, "\r\n", true);
  236. Task.Run(async () =>
  237. {
  238. while (true)
  239. {
  240. var data = blockingCollection.Take();
  241. _serial?.Write(data);
  242. await Task.Delay(500);
  243. }
  244. });
  245. }
  246. public override bool Initialize()
  247. {
  248. base.Initialize();
  249. if (Module == "PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue<bool>($"PMB.Chiller.EnableChiller"))
  250. {
  251. OP.Subscribe($"{Module}.{RtOperation.SetPMBChillerState}", (function, args) =>
  252. {
  253. StatusSMC = Convert.ToBoolean(args[0]) ? SMCChillerState.ON : SMCChillerState.OFF;
  254. return true;
  255. });
  256. return true;
  257. }
  258. SetAlarmMsg();
  259. if (!_serial.Open())
  260. {
  261. StatusSMC = SMCChillerState.Disconnected;
  262. LOG.Write( eEvent.ERR_DEVICE_CHILLER, Module, "SMC Chiller串口无法打开");
  263. return false;
  264. }
  265. StatusSMC = SMCChillerState.Connected;
  266. _serial.OnDataChanged += OnPortDataChanged;
  267. _serial.OnErrorHappened += OnErrorOccurred;
  268. _timerQueryStatus.Start(CHK_ST_INTERVAL);
  269. return true;
  270. }
  271. private void OnPortDataChanged(string obj)
  272. {
  273. try
  274. {
  275. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  276. {
  277. Statusflag[0] = true;
  278. if (_controlTcSetPoint <= SetPointLimitMax/2 && _controlTcSetPoint >= SetPointLimitMin)
  279. _controlTcFeedback = _controlTcSetPoint;
  280. return;
  281. }
  282. if (string.IsNullOrEmpty(obj))
  283. {
  284. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, $"[{Module}] smc chiller message IsNullOrEmpty");
  285. return;
  286. }
  287. string cmd = obj.Split(new char[] { '\r', '\n' })[0];
  288. if (ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(cmd.Substring(1, cmd.Length - 3))).ToString("X2") !=
  289. cmd.Substring(cmd.Length - 2)) //LRC check
  290. {
  291. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, $"[{Module}] smc chiller message LRC check error");
  292. return;
  293. }
  294. switch (cmd.Substring(3, 2)) //function code
  295. {
  296. case "03": //返回的寄存器内容
  297. if (cmd.Substring(5, 2) == "16") //返回了全部11个寄存器、22个Byte数据
  298. {
  299. for (int iRegisterNo = 0; iRegisterNo < 11; iRegisterNo++)
  300. {
  301. string sBuf = cmd.Substring(4 * iRegisterNo + 7, 4);
  302. int iBuf = 0;
  303. for (int i = 3; i > -1; i--)
  304. {
  305. iBuf += Convert.ToInt32(sBuf.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  306. }
  307. switch (iRegisterNo)
  308. {
  309. case 0: //Circulating fluid discharge temperature
  310. _controlTcFeedback = (float)iBuf / 10;
  311. break;
  312. case 4: //Status flag 1
  313. for (int i = 0; i < 16; i++)
  314. {
  315. Statusflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  316. }
  317. break;
  318. case 5: //Alarm flag 1
  319. for (int i = 0; i < 16; i++)
  320. {
  321. Alarmflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  322. }
  323. break;
  324. case 6: //Alarm flag 2
  325. for (int i = 0; i < 16; i++)
  326. {
  327. Alarmflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  328. }
  329. break;
  330. case 7: //Alarm flag 3
  331. for (int i = 0; i < 16; i++)
  332. {
  333. Alarmflag[i + 32] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  334. }
  335. break;
  336. case 9: //Status flag 2
  337. for (int i = 0; i < 16; i++)
  338. {
  339. Statusflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  340. }
  341. break;
  342. }
  343. }
  344. }
  345. break;
  346. case "06": //寄存器的写成功消息
  347. break;
  348. }
  349. }
  350. catch (Exception ex)
  351. {
  352. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, $"[{Module}] smc chiller error: [{ex.Message}]");
  353. }
  354. }
  355. private void OnErrorOccurred(string obj)
  356. {
  357. StatusSMC = SMCChillerState.ERROR;
  358. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, $"[{Module}] smc chiller error: [{obj}]");
  359. }
  360. public override void Monitor()
  361. {
  362. try
  363. {
  364. if (Module=="PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue<bool>($"PMB.Chiller.EnableChiller"))
  365. return;
  366. if (_timerQueryStatus.IsTimeout() && this.StatusSMC != SMCChillerState.ERROR)
  367. {
  368. this.SendCmd(SMCChillerMessage.GET_ALL);
  369. _timerQueryStatus.Start(CHK_ST_INTERVAL);
  370. }
  371. if (Statusflag[0])
  372. this.StatusSMC = SMCChillerState.ON;
  373. else
  374. this.StatusSMC = SMCChillerState.OFF;
  375. for (int i = 0; i < 48; i++)
  376. {
  377. if (Alarmflag[i])
  378. this.StatusSMC = SMCChillerState.ERROR;
  379. if (Alarmflag[i] && !LastAlarmflag[i])//取报警信号上升沿
  380. {
  381. //报警一次
  382. if (NeedToStop[i])
  383. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, AlarmMsg[i]);
  384. else
  385. LOG.Write(eEvent.WARN_DEVICE_CHILLER, Module, AlarmMsg[i]);
  386. }
  387. LastAlarmflag[i] = Alarmflag[i];
  388. }
  389. if (IsRunning && _scEnableAlarm!=null && _scEnableAlarm.BoolValue)
  390. {
  391. var range = _scAlarmRange.IntValue;
  392. var time = _scAlarmTime.IntValue;
  393. _toleranceChecker.Monitor(_controlTcFeedback, _controlTcSetPoint - Math.Abs(range), _controlTcSetPoint + Math.Abs(range), time);
  394. if (_toleranceChecker.Trig)
  395. {
  396. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, Display + $" temperature out of tolerance in {time:0} seconds");
  397. }
  398. }
  399. else
  400. {
  401. _toleranceChecker.RST = true;
  402. }
  403. }
  404. catch (Exception ex)
  405. {
  406. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, ex.ToString());
  407. }
  408. }
  409. private void SendCmd(string str)
  410. {
  411. if (Module == "PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue<bool>($"PMB.Chiller.EnableChiller")) return;
  412. //消息通用格式:开头 + 内容 + LRC + 结尾
  413. //_serial?.Write(":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
  414. blockingCollection.Add(":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
  415. }
  416. public override void Reset()
  417. {
  418. _toleranceChecker.RST = true;
  419. }
  420. public override void Terminate()
  421. {
  422. _serial?.Close();
  423. }
  424. public override void SetChillerTemp(float value, float offset)
  425. {
  426. int isettemp = Convert.ToInt16((value - offset) * 10);
  427. SendCmd(SMCChillerMessage.SET_TEMP + isettemp.ToString("X4"));
  428. _controlTcSetPoint = value;
  429. }
  430. public override void SetChillerOnOff(bool on)
  431. {
  432. SendCmd(on ? SMCChillerMessage.SET_ON : SMCChillerMessage.SET_OFF);
  433. }
  434. }
  435. }