SMCChillerDrive.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.SCCore;
  5. using Aitex.Core.RT.Tolerance;
  6. using Aitex.Core.Util;
  7. using MECF.Framework.Common.Communications;
  8. using MECF.Framework.Common.Equipment;
  9. using MECF.Framework.Common.Utilities;
  10. using System;
  11. using System.Collections.Concurrent;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Reflection;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using static Mono.Security.X509.X520;
  18. using Venus_Core;
  19. using System.Diagnostics;
  20. namespace Venus_RT.Devices
  21. {
  22. /// <summary>
  23. /// 描述:该类用于对单Chiller Controller下多设备共享 的驱动 只单一的用于串口的连接
  24. /// 需要存储的变量及说明
  25. /// Statusflag:与chiller的开关有关
  26. /// Alarmflag: 报警
  27. /// chillerIsOn:正在进行
  28. /// ControlTcFeedback:返回值
  29. /// </summary>
  30. public class SMCChillerDrive
  31. {
  32. public SMCChillerState StatusSMC { get; set; }
  33. private readonly string _PortNum = "COM92";
  34. private readonly AsyncSerialPort _serial;
  35. BlockingCollection<string> blockingCollection = new BlockingCollection<string>();
  36. public string PortName => _PortNum;
  37. private Dictionary<string,string> _address2Module;//不正确 应该以字典的形式 以头去打印
  38. private Dictionary<string, ChillerData> _addressQuery;
  39. private ModuleName Module;//不正确 应该以字典的形式 以头去打印
  40. // --------------------------Constructor-----------------------
  41. //
  42. public SMCChillerDrive(string Port)
  43. {
  44. _PortNum = Port;
  45. StatusSMC = SMCChillerState.Unknown;
  46. _serial = new AsyncSerialPort(_PortNum, 9600, 7, System.IO.Ports.Parity.Even, System.IO.Ports.StopBits.One, "\r\n", true);
  47. if (!_serial.Open())
  48. {
  49. StatusSMC = SMCChillerState.Disconnected;
  50. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"SMC Chiller串口:{_PortNum}无法打开");
  51. }
  52. StatusSMC = SMCChillerState.Connected;
  53. _serial.OnDataChanged += OnPortDataChanged;
  54. _serial.OnErrorHappened += OnErrorOccurred;
  55. _address2Module = new Dictionary<string, string>();
  56. _addressQuery = new Dictionary<string, ChillerData>();
  57. Task.Run(() =>
  58. {
  59. foreach (var data in blockingCollection.GetConsumingEnumerable())
  60. {
  61. _serial?.Write(data);
  62. System.Threading.Thread.Sleep(500);
  63. }
  64. });
  65. }
  66. private void OnPortDataChanged(string obj)
  67. {
  68. try
  69. {
  70. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  71. {
  72. //Statusflag[0] = true;
  73. //if (_controlTcSetPoint <= SetPointLimitMax / 2 && _controlTcSetPoint >= SetPointLimitMin)
  74. // _controlTcFeedback = _controlTcSetPoint;
  75. return;
  76. }
  77. if (string.IsNullOrEmpty(obj))
  78. {
  79. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller message IsNullOrEmpty");
  80. return;
  81. }
  82. string cmd = obj.Split(new char[] { '\r', '\n' })[0];
  83. //注意 数据开头有":"
  84. //地址位 决定了是哪个chamber的哪个type
  85. ChillerData chillerData = null;
  86. ModuleName module = ModuleName.System;
  87. if (_address2Module.ContainsKey(cmd.Substring(1, 2)) && _addressQuery.ContainsKey(cmd.Substring(1, 2)))
  88. {
  89. _addressQuery[cmd.Substring(1, 2)] = chillerData;
  90. module = ModuleHelper.Converter(_address2Module[cmd.Substring(1, 2)].Split('.')[0]);
  91. }
  92. else
  93. {
  94. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}]:address => {cmd.Substring(1, 2)} not in Dictionary");
  95. return;
  96. }
  97. //若为system或null 说明dic中无value或value无效
  98. Debug.Assert(chillerData != null && module != ModuleName.System);
  99. if (ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(cmd.Substring(1, cmd.Length - 3))).ToString("X2") !=
  100. cmd.Substring(cmd.Length - 2)) //LRC check
  101. {
  102. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, $"[{_PortNum}] smc chiller message LRC check error");
  103. return;
  104. }
  105. switch (cmd.Substring(3, 2)) //function code
  106. {
  107. case "03": //返回的寄存器内容
  108. if (cmd.Substring(5, 2) == "16") //返回了全部11个寄存器、22个Byte数据
  109. {
  110. for (int iRegisterNo = 0; iRegisterNo < 11; iRegisterNo++)
  111. {
  112. string sBuf = cmd.Substring(4 * iRegisterNo + 7, 4);
  113. int iBuf = 0;
  114. for (int i = 3; i > -1; i--)
  115. {
  116. iBuf += Convert.ToInt32(sBuf.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  117. }
  118. switch (iRegisterNo)
  119. {
  120. case 0: //Circulating fluid discharge temperature
  121. chillerData.ControlTcFeedback = (float)iBuf / 10;
  122. break;
  123. case 4: //Status flag 1
  124. for (int i = 0; i < 16; i++)
  125. {
  126. chillerData.Statusflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  127. }
  128. break;
  129. case 5: //Alarm flag 1
  130. for (int i = 0; i < 16; i++)
  131. {
  132. chillerData.Alarmflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  133. }
  134. break;
  135. case 6: //Alarm flag 2
  136. for (int i = 0; i < 16; i++)
  137. {
  138. chillerData.Alarmflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  139. }
  140. break;
  141. case 7: //Alarm flag 3
  142. for (int i = 0; i < 16; i++)
  143. {
  144. chillerData.Alarmflag[i + 32] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  145. }
  146. break;
  147. case 9: //Status flag 2
  148. for (int i = 0; i < 16; i++)
  149. {
  150. chillerData.Statusflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  151. }
  152. break;
  153. }
  154. }
  155. }
  156. _addressQuery[cmd.Substring(1, 2)] = chillerData;
  157. break;
  158. case "06": //寄存器的写成功消息
  159. break;
  160. }
  161. }
  162. catch (Exception ex)
  163. {
  164. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller error: [{ex.Message}]");
  165. }
  166. }
  167. private void OnErrorOccurred(string obj)
  168. {
  169. StatusSMC = SMCChillerState.ERROR;
  170. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller error: [{obj}]");
  171. }
  172. public void SendCmd(string str)
  173. {
  174. //if (Module == "PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue<bool>($"PMB.Chiller.EnableChiller")) return;
  175. //消息通用格式:开头 + 内容 + LRC + 结尾
  176. //_serial?.Write(":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
  177. blockingCollection.Add(":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
  178. }
  179. public void AddAddress(string address, string chillertype)
  180. {
  181. //address => "02" chillertype => PMA.OutChiller
  182. _address2Module.Add(address,chillertype);
  183. _addressQuery.Add(address, new ChillerData());
  184. }
  185. public ChillerData QueryData(string address)
  186. {
  187. if (_addressQuery.ContainsKey(address))
  188. {
  189. return _addressQuery[address];
  190. }
  191. else
  192. {
  193. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] => address:{address} not exist");
  194. return new ChillerData();
  195. }
  196. }
  197. public void Terminate()
  198. {
  199. _serial?.Close();
  200. }
  201. }
  202. public class ChillerData
  203. {
  204. public bool[] Statusflag {get; set;}
  205. public bool[] Alarmflag { get; set; }
  206. public float ControlTcFeedback { get; set; }
  207. public ChillerData()
  208. {
  209. Statusflag = new bool[32];
  210. Alarmflag = new bool[48];
  211. ControlTcFeedback = 0.0f;
  212. }
  213. }
  214. }