SMCChiller.cs 19 KB

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