SMCChillerDrive.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 enum ChillerMsgType
  31. {
  32. Get,
  33. Set
  34. }
  35. public class SMCChillerDrive
  36. {
  37. public SMCChillerState StatusSMC { get; set; }
  38. private readonly string _PortNum = "COM92";
  39. private readonly AsyncSerialPort _serial;
  40. BlockingCollection<string> blockingCollection = new BlockingCollection<string>();
  41. public string PortName => _PortNum;
  42. private Dictionary<string,string> _address2Module;//不正确 应该以字典的形式 以头去打印
  43. private Dictionary<string, ChillerData> _addressQuery;
  44. private Dictionary<string, Queue<string>> _addressCmdQueue;
  45. private ModuleName Module;//不正确 应该以字典的形式 以头去打印
  46. // --------------------------Constructor-----------------------
  47. //
  48. public SMCChillerDrive(string Port)
  49. {
  50. _PortNum = Port;
  51. StatusSMC = SMCChillerState.Unknown;
  52. _serial = new AsyncSerialPort(_PortNum, 9600, 7, System.IO.Ports.Parity.Even, System.IO.Ports.StopBits.One, "\r\n", true);
  53. if (!_serial.Open())
  54. {
  55. StatusSMC = SMCChillerState.Disconnected;
  56. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"SMC Chiller串口:{_PortNum}无法打开");
  57. }
  58. StatusSMC = SMCChillerState.Connected;
  59. _serial.OnDataChanged += OnPortDataChanged;
  60. _serial.OnErrorHappened += OnErrorOccurred;
  61. _address2Module = new Dictionary<string, string>();
  62. _addressQuery = new Dictionary<string, ChillerData>();
  63. _addressCmdQueue = new Dictionary<string, Queue<string>>();
  64. Task.Run(() =>
  65. {
  66. foreach (var data in blockingCollection.GetConsumingEnumerable())
  67. {
  68. _serial?.Write(data);
  69. System.Threading.Thread.Sleep(500);
  70. }
  71. });
  72. }
  73. private void OnPortDataChanged(string obj)
  74. {
  75. try
  76. {
  77. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  78. {
  79. //Statusflag[0] = true;
  80. //if (_controlTcSetPoint <= SetPointLimitMax / 2 && _controlTcSetPoint >= SetPointLimitMin)
  81. // _controlTcFeedback = _controlTcSetPoint;
  82. return;
  83. }
  84. if (string.IsNullOrEmpty(obj))
  85. {
  86. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller message IsNullOrEmpty");
  87. return;
  88. }
  89. string cmd = obj.Split(new char[] { '\r', '\n' })[0];
  90. //若为system或null 说明dic中无value或value无效
  91. //Debug.Assert(chillerData != null && module != ModuleName.System);
  92. if (ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(cmd.Substring(1, cmd.Length - 3))).ToString("X2") !=
  93. cmd.Substring(cmd.Length - 2)) //LRC check
  94. {
  95. LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, $"[{_PortNum}] smc chiller message LRC check error");
  96. return;
  97. }
  98. //注意 数据开头有":"
  99. //地址位 决定了是哪个chamber的哪个type
  100. ModuleName module = ModuleName.System;
  101. //LOG.Write(eEvent.INFO_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller receive message {cmd}");
  102. string header = cmd.Substring(1, 2);
  103. if (_address2Module.ContainsKey(header) && _addressQuery.ContainsKey(header))
  104. {
  105. module = ModuleHelper.Converter(_address2Module[header].Split('.')[0]);
  106. //LOG.Write(eEvent.INFO_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller know module:{_address2Module[cmd.Substring(1, 2)]},{cmd.Substring(1, 2)}");
  107. }
  108. else
  109. {
  110. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}]:address => {header} not in Dictionary");
  111. return;
  112. }
  113. switch (cmd.Substring(3, 2)) //function code
  114. {
  115. case "03": //返回的寄存器内容
  116. if (cmd.Substring(5, 2) == "16") //返回了全部11个寄存器、22个Byte数据
  117. {
  118. ChillerData chillerdata = new ChillerData();
  119. for (int iRegisterNo = 0; iRegisterNo < 11; iRegisterNo++)
  120. {
  121. string sBuf = cmd.Substring(4 * iRegisterNo + 7, 4);
  122. int iBuf = 0;
  123. for (int i = 3; i > -1; i--)
  124. {
  125. iBuf += Convert.ToInt32(sBuf.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  126. }
  127. switch (iRegisterNo)
  128. {
  129. case 0: //Circulating fluid discharge temperature
  130. if (iBuf > 32767) // 16位数的最大正值
  131. {
  132. iBuf -= 65536; // 转换为负值
  133. }
  134. chillerdata.ControlTcFeedback = (float)iBuf / 10;
  135. break;
  136. case 4: //Status flag 1
  137. for (int i = 0; i < 16; i++)
  138. {
  139. chillerdata.Statusflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  140. }
  141. break;
  142. case 5: //Alarm flag 1
  143. for (int i = 0; i < 16; i++)
  144. {
  145. chillerdata.Alarmflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  146. }
  147. break;
  148. case 6: //Alarm flag 2
  149. for (int i = 0; i < 16; i++)
  150. {
  151. chillerdata.Alarmflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  152. }
  153. break;
  154. case 7: //Alarm flag 3
  155. for (int i = 0; i < 16; i++)
  156. {
  157. chillerdata.Alarmflag[i + 32] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  158. }
  159. break;
  160. case 9: //Status flag 2
  161. for (int i = 0; i < 16; i++)
  162. {
  163. chillerdata.Statusflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
  164. }
  165. break;
  166. }
  167. }
  168. _addressCmdQueue[header].Dequeue();
  169. _addressQuery[header] = chillerdata;
  170. }
  171. break;
  172. case "06": //寄存器的写成功消息
  173. break;
  174. }
  175. }
  176. catch (Exception ex)
  177. {
  178. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller error: [{ex.Message}]");
  179. }
  180. }
  181. private void OnErrorOccurred(string obj)
  182. {
  183. StatusSMC = SMCChillerState.ERROR;
  184. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller error: [{obj}]");
  185. }
  186. public void SendCmd(ChillerMsgType type,string str)
  187. {
  188. //消息通用格式:开头 + 内容 + LRC + 结尾
  189. //_serial?.Write(":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
  190. //如果是设置的直接发出无需等待
  191. if (type == ChillerMsgType.Set)
  192. {
  193. blockingCollection.Add(":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
  194. }
  195. //如果是获取的可以慢一些 在数据回复后继续
  196. if (type == ChillerMsgType.Get)
  197. {
  198. //每interval获取温度 超过一秒时效性失去意义 500 * 2积累太多数据 同时注意其是与monitor紧密联系的 实际上是每interval就会进来可能导致数组变大
  199. //因此每次进来要控制规模 > 2 时 实时性已经失去此时应该等待回复并把头前的清除掉 而且要注意 chiller本身升温很慢 界面上ramp不明显
  200. _addressCmdQueue[str.Substring(0, 2)].Enqueue(str);
  201. if (_addressCmdQueue[str.Substring(0, 2)].Count > 1)
  202. _addressCmdQueue[str.Substring(0, 2)].Dequeue();
  203. else
  204. blockingCollection.Add(":" + _addressCmdQueue[str.Substring(0, 2)].Peek() + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
  205. }
  206. }
  207. public void AddAddress(string address, string chillertype)
  208. {
  209. //address => "02" chillertype => PMA.OutChiller
  210. _address2Module.Add(address,chillertype);
  211. _addressQuery.Add(address, new ChillerData());
  212. _addressCmdQueue.Add(address, new Queue<string>());
  213. }
  214. public ChillerData QueryData(string address)
  215. {
  216. if (_addressQuery.ContainsKey(address))
  217. {
  218. return _addressQuery[address];
  219. }
  220. else
  221. {
  222. LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] => address:{address} not exist");
  223. return null;
  224. }
  225. }
  226. public void Terminate()
  227. {
  228. _serial?.Close();
  229. }
  230. }
  231. public class ChillerData
  232. {
  233. public bool[] Statusflag {get; set;}
  234. public bool[] Alarmflag { get; set; }
  235. public float ControlTcFeedback { get; set; }
  236. public ChillerData()
  237. {
  238. Statusflag = new bool[32];
  239. Alarmflag = new bool[48];
  240. ControlTcFeedback = 0.0f;
  241. }
  242. }
  243. }