SMCChillerDrive.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. //若为system或null 说明dic中无value或value无效
  84. //Debug.Assert(chillerData != null && module != ModuleName.System);
  85. if (ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(cmd.Substring(1, cmd.Length - 3))).ToString("X2") !=
  86. cmd.Substring(cmd.Length - 2)) //LRC check
  87. {
  88. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, $"[{_PortNum}] smc chiller message LRC check error");
  89. return;
  90. }
  91. //注意 数据开头有":"
  92. //地址位 决定了是哪个chamber的哪个type
  93. ModuleName module = ModuleName.System;
  94. LOG.Write(eEvent.INFO_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller receive message {cmd}");
  95. string header = cmd.Substring(1, 2);
  96. if (_address2Module.ContainsKey(header) && _addressQuery.ContainsKey(header))
  97. {
  98. module = ModuleHelper.Converter(_address2Module[header].Split('.')[0]);
  99. //LOG.Write(eEvent.INFO_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller know module:{_address2Module[cmd.Substring(1, 2)]},{cmd.Substring(1, 2)}");
  100. }
  101. else
  102. {
  103. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}]:address => {header} not in Dictionary");
  104. return;
  105. }
  106. switch (cmd.Substring(3, 2)) //function code
  107. {
  108. case "03": //返回的寄存器内容
  109. if (cmd.Substring(5, 2) == "16") //返回了全部11个寄存器、22个Byte数据
  110. {
  111. ChillerData chillerdata = new ChillerData();
  112. for (int iRegisterNo = 0; iRegisterNo < 11; iRegisterNo++)
  113. {
  114. string sBuf = cmd.Substring(4 * iRegisterNo + 7, 4);
  115. int iBuf = 0;
  116. for (int i = 3; i > -1; i--)
  117. {
  118. iBuf += Convert.ToInt32(sBuf.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  119. }
  120. switch (iRegisterNo)
  121. {
  122. case 0: //Circulating fluid discharge temperature
  123. chillerdata.ControlTcFeedback = (float)iBuf / 10;
  124. break;
  125. case 4: //Status flag 1
  126. for (int i = 0; i < 16; i++)
  127. {
  128. chillerdata.Statusflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  129. }
  130. break;
  131. case 5: //Alarm flag 1
  132. for (int i = 0; i < 16; i++)
  133. {
  134. chillerdata.Alarmflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  135. }
  136. break;
  137. case 6: //Alarm flag 2
  138. for (int i = 0; i < 16; i++)
  139. {
  140. chillerdata.Alarmflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  141. }
  142. break;
  143. case 7: //Alarm flag 3
  144. for (int i = 0; i < 16; i++)
  145. {
  146. chillerdata.Alarmflag[i + 32] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  147. }
  148. break;
  149. case 9: //Status flag 2
  150. for (int i = 0; i < 16; i++)
  151. {
  152. chillerdata.Statusflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  153. }
  154. break;
  155. }
  156. }
  157. _addressQuery[header] = chillerdata;
  158. }
  159. break;
  160. case "06": //寄存器的写成功消息
  161. break;
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller error: [{ex.Message}]");
  167. }
  168. }
  169. private void OnErrorOccurred(string obj)
  170. {
  171. StatusSMC = SMCChillerState.ERROR;
  172. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller error: [{obj}]");
  173. }
  174. public void SendCmd(string str)
  175. {
  176. //消息通用格式:开头 + 内容 + LRC + 结尾
  177. //_serial?.Write(":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
  178. LOG.Write(eEvent.INFO_DEVICE_CHILLER,ModuleName.System, "Send Cmd => :" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2"));
  179. blockingCollection.Add(":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
  180. }
  181. public void AddAddress(string address, string chillertype)
  182. {
  183. //address => "02" chillertype => PMA.OutChiller
  184. _address2Module.Add(address,chillertype);
  185. _addressQuery.Add(address, new ChillerData());
  186. }
  187. public ChillerData QueryData(string address)
  188. {
  189. if (_addressQuery.ContainsKey(address))
  190. {
  191. return _addressQuery[address];
  192. }
  193. else
  194. {
  195. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] => address:{address} not exist");
  196. return null;
  197. }
  198. }
  199. public void Terminate()
  200. {
  201. _serial?.Close();
  202. }
  203. }
  204. public class ChillerData
  205. {
  206. public bool[] Statusflag {get; set;}
  207. public bool[] Alarmflag { get; set; }
  208. public float ControlTcFeedback { get; set; }
  209. public ChillerData()
  210. {
  211. Statusflag = new bool[32];
  212. Alarmflag = new bool[48];
  213. ControlTcFeedback = 0.0f;
  214. }
  215. }
  216. }