SMCChiller.cs 20 KB

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