SMCChiller.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. private double _scCoolantInletTcFeedbackOffset;
  60. private double _scCoolantOutletTcFeedbackOffset;
  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. private void SetAlarmMsg()
  68. {
  69. AlarmMsg[0] = "Low level in tank";
  70. AlarmMsg[1] = "High circulating fluid discharge temp";
  71. AlarmMsg[2] = "Circulating fluid discharge temp. rise";
  72. AlarmMsg[3] = "Circulating fluid discharge temp.";
  73. AlarmMsg[4] = "High circulating fluid return temp.";
  74. AlarmMsg[5] = "High circulating fluid discharge pressure";
  75. AlarmMsg[6] = "Abnormal pump operation";
  76. AlarmMsg[7] = "Circulating fluid discharge pressure rise";
  77. AlarmMsg[8] = "Circulating fluid discharge pressure drop";
  78. AlarmMsg[9] = "High compressor intake temp.";
  79. AlarmMsg[10] = "Low compressor intake temp.";
  80. AlarmMsg[11] = "Low super heat temperature";
  81. AlarmMsg[12] = "High compressor discharge pressure";
  82. //AlarmMsg[13] Unused
  83. AlarmMsg[14] = "Refrigerant circuit pressure (high pressure side) drop";
  84. AlarmMsg[15] = "Refrigerant circuit pressure (low pressure side) rise";
  85. AlarmMsg[16] = "Refrigerant circuit pressure (low pressure side) drop";
  86. AlarmMsg[17] = "Compressor overload";
  87. AlarmMsg[18] = "Communication error";
  88. AlarmMsg[19] = "Memory error";
  89. AlarmMsg[20] = "DC line fuse cut";
  90. AlarmMsg[21] = "Circulating fluid discharge temp. sensor failure";
  91. AlarmMsg[22] = "Circulating fluid return temp. sensor failure";
  92. AlarmMsg[23] = "Compressor intake temp. sensor failure";
  93. AlarmMsg[24] = "Circulating fluid discharge pressure sensor failure";
  94. AlarmMsg[25] = "Compressor discharge pressure sensor failure";
  95. AlarmMsg[26] = "Compressor intake pressure sensor failure";
  96. AlarmMsg[27] = "Maintenance of pump";
  97. AlarmMsg[28] = "Maintenance of fan motor";
  98. AlarmMsg[29] = "Maintenance of compressor";
  99. AlarmMsg[30] = "Contact input 1 signal detection alarm";
  100. AlarmMsg[31] = "Contact input 2 signal detection alarm";
  101. AlarmMsg[32] = "Water leakage";
  102. AlarmMsg[33] = "Electric resistivity/conductivity level rise";
  103. AlarmMsg[34] = "Electric resistivity/conductivity level drop";
  104. AlarmMsg[35] = "Electric resistivity/conductivity sensor error";
  105. //AlarmMsg[36] Unused
  106. NeedToStop[0] = true;
  107. NeedToStop[1] = true;
  108. NeedToStop[4] = true;
  109. NeedToStop[5] = true;
  110. NeedToStop[6] = true;
  111. NeedToStop[9] = true;
  112. NeedToStop[10] = true;
  113. NeedToStop[11] = true;
  114. NeedToStop[12] = true;
  115. NeedToStop[14] = true;
  116. NeedToStop[15] = true;
  117. NeedToStop[16] = true;
  118. NeedToStop[17] = true;
  119. NeedToStop[19] = true;
  120. NeedToStop[20] = true;
  121. NeedToStop[21] = true;
  122. NeedToStop[22] = true;
  123. NeedToStop[23] = true;
  124. NeedToStop[24] = true;
  125. NeedToStop[25] = true;
  126. NeedToStop[26] = true;
  127. NeedToStop[30] = true;
  128. NeedToStop[31] = true;
  129. NeedToStop[32] = true;
  130. }
  131. public double SetPointLimitMax
  132. {
  133. get
  134. {
  135. return _scSetPointLimitMax; ;
  136. }
  137. }
  138. public double SetPointLimitMin
  139. {
  140. get
  141. {
  142. return _scSetPointLimitMin; ;
  143. }
  144. }
  145. [Subscription(AITChillerProperty.IsRunning)]
  146. public override bool IsRunning
  147. {
  148. get
  149. {
  150. return StatusSMC == SMCChillerState.ON;
  151. }
  152. }
  153. [Subscription(AITChillerProperty.IsError)]
  154. public override bool IsError
  155. {
  156. get
  157. {
  158. return StatusSMC == SMCChillerState.ERROR || StatusSMC == SMCChillerState.Disconnected;
  159. }
  160. }
  161. [Subscription(AITChillerProperty.ControlTcSetPoint)]
  162. public float ControlTcSetPoint
  163. {
  164. get
  165. {
  166. return _controlTcSetPoint;
  167. }
  168. }
  169. [Subscription(AITChillerProperty.ControlTcFeedback)]
  170. public float ControlTcFeedback
  171. {
  172. get
  173. {
  174. return _controlTcFeedback;
  175. }
  176. }
  177. [Subscription(AITChillerProperty.CoolantInletTempFeedback)]
  178. public float CoolantInletTcFeedback
  179. {
  180. get
  181. {
  182. return GetAiValue($"{Module}.AI_Coolant_Inlet_Temp") + (float) _scCoolantInletTcFeedbackOffset;
  183. }
  184. }
  185. [Subscription(AITChillerProperty.CoolantOutletTempFeedback)]
  186. public float CoolantOutletTcFeedback
  187. {
  188. get
  189. {
  190. return GetAiValue($"{Module}.AI_Coolant_Outlet_Temp") + (float)_scCoolantOutletTcFeedbackOffset;
  191. }
  192. }
  193. public override AITChillerData DeviceData
  194. {
  195. get
  196. {
  197. AITChillerData deviceData = new AITChillerData
  198. {
  199. Module = Module,
  200. DeviceName = Name,
  201. DeviceModule = Module,
  202. DeviceSchematicId = DeviceID,
  203. DisplayName = Display,
  204. IsError = false,
  205. IsWarning = false,
  206. IsPowerOn = IsRunning,
  207. ScaleMax = SetPointLimitMax,
  208. ScaleMin = SetPointLimitMin,
  209. SetPoint = ControlTcSetPoint,
  210. FeedBack = CoolantOutletTcFeedback,
  211. CoolantInletFeedBack = CoolantInletTcFeedback,
  212. CoolantOutletFeedBack = CoolantOutletTcFeedback,
  213. Unit = "℃",
  214. };
  215. return deviceData;
  216. }
  217. }
  218. // --------------------------Constructor-----------------------
  219. //
  220. public SMCChiller(ModuleName mod, string name) : base(mod.ToString(), name, name, "")
  221. {
  222. if (Module == "PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue<bool>($"PMB.Chiller.EnableChiller")) return;
  223. _PortNum = SC.GetStringValue($"{mod}.{Name}.Port");
  224. _scSetPointLimitMax = SC.GetValue<double>($"{Module}.{Name}.SetPointLimitMax");
  225. _scSetPointLimitMin = SC.GetValue<double>($"{Module}.{Name}.SetPointLimitMin");
  226. _scCoolantInletTcFeedbackOffset = SC.GetValue<double>($"{Module}.{Name}.CoolantInletTcFeedbackOffset");
  227. _scCoolantOutletTcFeedbackOffset = SC.GetValue<double>($"{Module}.{Name}.CoolantOutletTcFeedbackOffset");
  228. StatusSMC = SMCChillerState.Unknown;
  229. _serial = new AsyncSerialPort(_PortNum, 9600, 7, System.IO.Ports.Parity.Even, System.IO.Ports.StopBits.One, "\r\n", true);
  230. }
  231. public override bool Initialize()
  232. {
  233. base.Initialize();
  234. if (Module == "PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue<bool>($"PMB.Chiller.EnableChiller"))
  235. {
  236. OP.Subscribe($"{Module}.{RtOperation.SetPMBChillerState}", (function, args) =>
  237. {
  238. StatusSMC = Convert.ToBoolean(args[0]) ? SMCChillerState.ON : SMCChillerState.OFF;
  239. return true;
  240. });
  241. return true;
  242. }
  243. SetAlarmMsg();
  244. if (!_serial.Open())
  245. {
  246. StatusSMC = SMCChillerState.Disconnected;
  247. EV.PostAlarmLog(this.Module, "SMC Chiller serial port open failed");
  248. return false;
  249. }
  250. StatusSMC = SMCChillerState.Connected;
  251. _serial.OnDataChanged += OnPortDataChanged;
  252. _serial.OnErrorHappened += OnErrorOccurred;
  253. _timerQueryStatus.Start(CHK_ST_INTERVAL);
  254. return true;
  255. }
  256. private void OnPortDataChanged(string obj)
  257. {
  258. try
  259. {
  260. if (string.IsNullOrEmpty(obj))
  261. {
  262. LOG.Error($"[{Module}] smc chiller message IsNullOrEmpty");
  263. return;
  264. }
  265. string cmd = obj.Split(new char[] { '\r', '\n' })[0];
  266. if (ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(cmd.Substring(1, cmd.Length - 3))).ToString("X2") !=
  267. cmd.Substring(cmd.Length - 2)) //LRC check
  268. {
  269. LOG.Error($"[{Module}] smc chiller message LRC check error");
  270. return;
  271. }
  272. switch (cmd.Substring(3, 2)) //function code
  273. {
  274. case "03": //返回的寄存器内容
  275. if (cmd.Substring(5, 2) == "16") //返回了全部11个寄存器、22个Byte数据
  276. {
  277. for (int iRegisterNo = 0; iRegisterNo < 11; iRegisterNo++)
  278. {
  279. string sBuf = cmd.Substring(4 * iRegisterNo + 7, 4);
  280. int iBuf = 0;
  281. for (int i = 3; i > -1; i--)
  282. {
  283. iBuf += Convert.ToInt32(sBuf.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  284. }
  285. switch (iRegisterNo)
  286. {
  287. case 0: //Circulating fluid discharge temperature
  288. _controlTcFeedback = (float)iBuf / 10;
  289. break;
  290. case 4: //Status flag 1
  291. for (int i = 0; i < 16; i++)
  292. {
  293. Statusflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  294. }
  295. break;
  296. case 5: //Alarm flag 1
  297. for (int i = 0; i < 16; i++)
  298. {
  299. Alarmflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  300. }
  301. break;
  302. case 6: //Alarm flag 2
  303. for (int i = 0; i < 16; i++)
  304. {
  305. Alarmflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  306. }
  307. break;
  308. case 7: //Alarm flag 3
  309. for (int i = 0; i < 16; i++)
  310. {
  311. Alarmflag[i + 32] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  312. }
  313. break;
  314. case 9: //Status flag 2
  315. for (int i = 0; i < 16; i++)
  316. {
  317. Statusflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  318. }
  319. break;
  320. }
  321. }
  322. }
  323. break;
  324. case "06": //寄存器的写成功消息
  325. break;
  326. }
  327. }
  328. catch (Exception ex)
  329. {
  330. LOG.Error($"[{Module}] smc chiller error: [{ex.Message}]");
  331. }
  332. }
  333. private void OnErrorOccurred(string obj)
  334. {
  335. StatusSMC = SMCChillerState.ERROR;
  336. LOG.Error($"[{Module}] smc chiller error: [{obj}]");
  337. }
  338. public override void Monitor()
  339. {
  340. try
  341. {
  342. if (Module=="PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue<bool>($"PMB.Chiller.EnableChiller")) return;
  343. if (_timerQueryStatus.IsTimeout() && this.StatusSMC != SMCChillerState.ERROR)
  344. {
  345. this.SendCmd(SMCChillerMessage.GET_ALL);
  346. _timerQueryStatus.Start(CHK_ST_INTERVAL);
  347. }
  348. if (Statusflag[0])
  349. this.StatusSMC = SMCChillerState.ON;
  350. else
  351. this.StatusSMC = SMCChillerState.OFF;
  352. for (int i = 0; i < 48; i++)
  353. {
  354. if (Alarmflag[i])
  355. this.StatusSMC = SMCChillerState.ERROR;
  356. if (Alarmflag[i] && !LastAlarmflag[i])//取报警信号上升沿
  357. {
  358. //报警一次
  359. if (NeedToStop[i])
  360. EV.PostAlarmLog(this.Module, AlarmMsg[i]);
  361. else
  362. EV.PostWarningLog(this.Module, AlarmMsg[i]);
  363. }
  364. LastAlarmflag[i] = Alarmflag[i];
  365. }
  366. }
  367. catch (Exception ex)
  368. {
  369. LOG.Write(ex);
  370. }
  371. }
  372. private void SendCmd(string str)
  373. {
  374. if (Module == "PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue<bool>($"PMB.Chiller.EnableChiller")) return;
  375. //消息通用格式:开头 + 内容 + LRC + 结尾
  376. _serial?.Write(":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
  377. }
  378. public override void Reset()
  379. {
  380. }
  381. public override void Terminate()
  382. {
  383. _serial?.Close();
  384. }
  385. public override void SetChillerTemp(float value, float offset)
  386. {
  387. int isettemp = Convert.ToInt16((value - offset) * 10);
  388. SendCmd(SMCChillerMessage.SET_TEMP + isettemp.ToString("X4"));
  389. _controlTcSetPoint = value;
  390. }
  391. public override void SetChillerOnOff(bool on)
  392. {
  393. SendCmd(on ? SMCChillerMessage.SET_ON : SMCChillerMessage.SET_OFF);
  394. }
  395. public bool SetPowerOnOff(bool isOn)
  396. {
  397. SetChillerOnOff(isOn);
  398. return true;
  399. }
  400. public bool Ramp(float temp)
  401. {
  402. double offset = SC.GetValue<double>($"{Module}.{Name}.ChillerTemperatureOffset");
  403. int isettemp = Convert.ToInt16((temp - offset) * 10);
  404. SendCmd(SMCChillerMessage.SET_TEMP + isettemp.ToString("X4"));
  405. _controlTcSetPoint = temp;
  406. LOG.Write($"{Module} {Name} set temperature to {isettemp}");
  407. return true;
  408. }
  409. public float GetFeedback()
  410. {
  411. return ControlTcFeedback;
  412. }
  413. public float GetSetPoint()
  414. {
  415. return ControlTcSetPoint;
  416. }
  417. public bool GetIsOn()
  418. {
  419. return IsRunning;
  420. }
  421. }
  422. }