AdixenTurboPump.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections.Generic;
  4. using System.Collections.Concurrent;
  5. using System.Text.RegularExpressions;
  6. using MECF.Framework.Common.Communications;
  7. using MECF.Framework.Common.Equipment;
  8. using Aitex.Core.RT.SCCore;
  9. using Aitex.Core.RT.Event;
  10. using Aitex.Core.RT.Device;
  11. using Aitex.Core.RT.Log;
  12. using Venus_Core;
  13. using MECF.Framework.Common.Device.Bases;
  14. namespace Venus_RT.Devices
  15. {
  16. class AdixenTurboPump : TurboPump
  17. {
  18. public enum Operation
  19. {
  20. SetAddress,
  21. GetChecksum,
  22. GetFaultList,
  23. SetDatalogInterval,
  24. EnableDatalog,
  25. EnableEcho,
  26. DisableEcho,
  27. IdentifyDevice,
  28. IdentifyProduct,
  29. GetCurrentValues,
  30. SwitchSpeedToNormal,
  31. SwitchSpeedToStandbyValue,
  32. SetStandbySpeed,
  33. GetParametersState,
  34. GetIntervalParametersState,
  35. SetPumpWorkingTime,
  36. SetElectronicWorkingTime,
  37. SetStartDelay,
  38. SetTimeToVent,
  39. SetVentingTime,
  40. SetSpeedThreshold,
  41. SetControlTemperatrue,
  42. StopPumpThermostage,
  43. SetBearingThreshold,
  44. GetCurrentSpeed,
  45. StartPumpRotation,
  46. StopPump,
  47. SetVersions,
  48. SetAnalogOutput,
  49. SetTemperatureUnit,
  50. SetBuzzerOpt,
  51. SetCommandMode,
  52. SetBraking,
  53. Invalid,
  54. }
  55. private readonly Dictionary<Operation, string> _noneParaCommandOp = new Dictionary<Operation, string>
  56. {
  57. {Operation.EnableDatalog, "#{0:D3}DLR\r" },
  58. {Operation.EnableEcho, "#{0:D3}ECHON\r" },
  59. {Operation.DisableEcho, "#{0:D3}ECHOFF\r" },
  60. {Operation.SwitchSpeedToNormal, "#{0:D3}NSP\r" },
  61. {Operation.SwitchSpeedToStandbyValue, "#{0:D3}SBY\r" },
  62. {Operation.StopPumpThermostage, "#{0:D3}SET31,30\r" },
  63. {Operation.StartPumpRotation, "#{0:D3}TMPON\r" },
  64. {Operation.StopPump, "#{0:D3}TMPOFF\r" },
  65. };
  66. private readonly Dictionary<Operation, string> _readDataCommandOp = new Dictionary<Operation, string>
  67. {
  68. {Operation.GetChecksum, "#{0:D3}CHKVS\r" },
  69. {Operation.GetFaultList, "#{0:D3}DEF\r" },
  70. {Operation.IdentifyDevice, "#{0:D3}IDN\r" },
  71. {Operation.IdentifyProduct, "#{0:D3}IDP\r" },
  72. {Operation.GetCurrentValues, "#{0:D3}LEV10\r" },
  73. {Operation.GetParametersState, "#{0:D3}SEL10\r" },
  74. {Operation.GetCurrentSpeed, "#{0:D3}SPD\r" },
  75. {Operation.GetIntervalParametersState, "#{0:D3}STA\r" },
  76. };
  77. private readonly Dictionary<Operation, string> _setDataCommandOp = new Dictionary<Operation, string>
  78. {
  79. {Operation.SetAddress, "#{0:D3}ADR {1:D3}\r" },
  80. {Operation.SetDatalogInterval, "#{0:D3}DLI {1:D3}\r" },
  81. {Operation.SetStandbySpeed, "#{0:D3}RPM,{1:D4}\r" },
  82. {Operation.SetPumpWorkingTime, "#{0:D3}SET10,{1:D5}\r" },
  83. {Operation.SetElectronicWorkingTime, "#{0:D3}SET11,{1:D5}\r" },
  84. {Operation.SetStartDelay, "#{0:D3}SET13,{1:D5}\r" },
  85. {Operation.SetTimeToVent, "#{0:D3}SET14,{1:D5}\r" },
  86. {Operation.SetVentingTime, "#{0:D3}SET15,{1:D4}\r" },
  87. {Operation.SetSpeedThreshold, "#{0:D3}SET30,{1:D2}\r" },
  88. {Operation.SetControlTemperatrue, "#{0:D3}SET31,{1:D2}\r" },
  89. {Operation.SetBearingThreshold, "#{0:D3}SET32,{1:D3}\r" },
  90. {Operation.SetAnalogOutput, "#{0:D3}OPT01,{1:D1}\r" },
  91. {Operation.SetTemperatureUnit, "#{0:D3}OPT02,{1:D1}\r" },
  92. {Operation.SetBuzzerOpt, "#{0:D3}OPT11,{1:D1}\r" },
  93. {Operation.SetCommandMode, "#{0:D3}OPT14,{1:D1}\r" },
  94. {Operation.SetBraking, "#{0:D3}OPT25,{1:D1}\r" },
  95. };
  96. private readonly Dictionary<string, string> _comError = new Dictionary<string, string>
  97. {
  98. {"Err0", "adjustment error(out of bounds)" },
  99. {"Err1", "command error (syntax) " },
  100. {"Err2", "parameter error (e.g. non hexadecimal character)" },
  101. {"Err3", "context error" },
  102. {"Err4", "checksum error" },
  103. };
  104. private readonly ushort PUMP_ON_FLAG = 0x02;
  105. private readonly ushort PUMP_ERROR_FLAG = 0x40;
  106. private readonly ushort ISO_VALVE_OPEN_FLAG = 0x01;
  107. private readonly ushort WATER_VALVE_OEPN_FLAG = 0x08;
  108. private readonly ushort VALVE_AT_SPEED_FLAG = 0x40;
  109. public override bool IsRunning
  110. {
  111. get
  112. {
  113. return (_pumpStatus & PUMP_ON_FLAG) == PUMP_ON_FLAG;
  114. }
  115. }
  116. public override bool IsError
  117. {
  118. get
  119. {
  120. return (_pumpStatus & PUMP_ERROR_FLAG) == PUMP_ERROR_FLAG;
  121. }
  122. }
  123. public bool IsISOValveOpen
  124. {
  125. get
  126. {
  127. return (_valveStatus & ISO_VALVE_OPEN_FLAG) == ISO_VALVE_OPEN_FLAG;
  128. }
  129. }
  130. public bool IsWaterValveOpen
  131. {
  132. get
  133. {
  134. return (_valveStatus & WATER_VALVE_OEPN_FLAG) == WATER_VALVE_OEPN_FLAG;
  135. }
  136. }
  137. public bool AtSpeed
  138. {
  139. get
  140. {
  141. return (_valveStatus & VALVE_AT_SPEED_FLAG) == VALVE_AT_SPEED_FLAG;
  142. }
  143. }
  144. public string sTemperature
  145. {
  146. get { return Temperature.ToString(); }
  147. }
  148. public string RotationalSpeed
  149. {
  150. get { return Speed.ToString(); }
  151. }
  152. private readonly int _timeout = 2000;
  153. private readonly int _address = 0;
  154. private readonly int _internal_parameters_length = 78;
  155. private readonly AsyncSerialPort _serial;
  156. private Stopwatch _queryWatch = new Stopwatch();
  157. private ConcurrentQueue<Operation> _queCommands = new ConcurrentQueue<Operation>();
  158. private Operation _lastCommand = Operation.Invalid;
  159. private string _lastAlarmString = string.Empty;
  160. private Regex _rex_ok = new Regex(@"#\d{1,3},OK");
  161. private Regex _rex_err = new Regex(@"#\d{1,3},Err");
  162. private byte _pumpStatus = 0;
  163. private byte _valveStatus = 0;
  164. public AdixenTurboPump(ModuleName mod)
  165. {
  166. Name = VenusDevice.TurboPump.ToString();
  167. Module = mod.ToString();
  168. var _PortNum = SC.GetStringValue($"{mod}.TurboPump.Port");
  169. _serial = new AsyncSerialPort(_PortNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\r", false);
  170. }
  171. public override bool Initialize()
  172. {
  173. if (!_serial.Open())
  174. {
  175. LOG.Write(eEvent.ERR_TURBO_PUMP, Module, "Adixen Turbo Pump 串口无法打开");
  176. return false;
  177. }
  178. _serial.OnDataChanged += OnPortDataChanged;
  179. _serial.OnBinaryDataChanged += OnPortBinaryDataChanged;
  180. _serial.OnErrorHappened += OnErrorOccurred;
  181. return true;
  182. }
  183. public override void Monitor()
  184. {
  185. if(_lastCommand == Operation.Invalid)
  186. {
  187. if(_queCommands.IsEmpty)
  188. {
  189. _sendCommand(Operation.GetIntervalParametersState);
  190. _lastCommand = Operation.GetIntervalParametersState;
  191. }
  192. else
  193. {
  194. Operation command;
  195. if(_queCommands.TryDequeue(out command))
  196. {
  197. _sendCommand(command);
  198. _lastCommand = command;
  199. }
  200. }
  201. }
  202. else if(_queryWatch.ElapsedMilliseconds >= _timeout)
  203. {
  204. _noRepeatAlarm($"last command: {_lastCommand} timeout");
  205. Reset();
  206. }
  207. }
  208. public override void Reset()
  209. {
  210. Operation result;
  211. while (_queCommands.TryDequeue(out result)) ;
  212. //_serial.ClearPortBuffer();
  213. _lastCommand = Operation.Invalid;
  214. }
  215. public override void Terminate()
  216. {
  217. _serial?.Close();
  218. }
  219. private void OnErrorOccurred(string obj)
  220. {
  221. _noRepeatAlarm($"[{Module}] Adixen Turbo Pump serial port error: [{obj}]");
  222. }
  223. private void OnPortDataChanged(string obj)
  224. {
  225. try
  226. {
  227. if (obj.Substring(0, 2) == "OK")
  228. {
  229. _lastCommand = Operation.Invalid;
  230. return;
  231. }
  232. if(obj.Substring(0, 3) == "Err")
  233. {
  234. if (_comError.ContainsKey(obj.Substring(0, 4)))
  235. {
  236. _noRepeatAlarm($"Adixen Turbo Pump communication error: {_comError[obj.Substring(0, 4)]}");
  237. }
  238. else
  239. {
  240. _noRepeatAlarm($"Adixen Turbo Pump unknown communication error: {obj}");
  241. }
  242. _lastCommand = Operation.Invalid;
  243. return;
  244. }
  245. if(_lastCommand == Operation.Invalid)
  246. {
  247. LOG.Write(eEvent.ERR_TURBO_PUMP, Module, $"Adixen Turbo Pump unexpected communication data: {obj}");
  248. return;
  249. }
  250. var result_data = obj.Trim().Split(',');
  251. switch(_lastCommand)
  252. {
  253. case Operation.GetCurrentSpeed:
  254. if(result_data.Length >= 2)
  255. {
  256. var str_speed = result_data[1].Split();
  257. int speed = 0;
  258. if (int.TryParse(str_speed[0], out speed))
  259. {
  260. Speed = speed;
  261. }
  262. }
  263. break;
  264. case Operation.GetIntervalParametersState:
  265. if(result_data.Length >= 9)
  266. {
  267. int speed = 0;
  268. int temp = 0;
  269. if(int.TryParse(result_data[0], out speed))
  270. {
  271. Speed = speed;
  272. }
  273. if(int.TryParse(result_data[8], out temp))
  274. {
  275. Temperature = temp;
  276. }
  277. }
  278. break;
  279. }
  280. _lastCommand = Operation.Invalid;
  281. }
  282. catch (Exception ex)
  283. {
  284. _noRepeatAlarm($"[{Module}] Adixen Turbo Pump error: [{ex.Message}], Data: {obj}");
  285. }
  286. }
  287. private void OnPortBinaryDataChanged(byte[] obj)
  288. {
  289. bool bValidate = false;
  290. for(int i=0; i< obj.Length; i++)
  291. {
  292. if(obj[i] == (byte)'#')
  293. {
  294. int nOffset = 5;
  295. if(_lastCommand == Operation.GetIntervalParametersState && obj.Length - i >= _internal_parameters_length)
  296. {
  297. _pumpStatus = obj[i + 6];
  298. _valveStatus = obj[i + 7];
  299. nOffset = 9;
  300. }
  301. byte[] array = new byte[obj.Length - i - nOffset];
  302. Array.Copy(obj, i + nOffset, array, 0, obj.Length - i - nOffset);
  303. var strData = System.Text.Encoding.Default.GetString(array);
  304. OnPortDataChanged(strData);
  305. bValidate = true;
  306. break;
  307. }
  308. }
  309. if(!bValidate)
  310. {
  311. var strData = System.Text.Encoding.Default.GetString(obj);
  312. _noRepeatAlarm($"Receive invalidate data:{strData} ");
  313. }
  314. }
  315. public override void SetPumpOnOff(bool on)
  316. {
  317. _queCommands.Enqueue(on ? Operation.StartPumpRotation : Operation.StopPump);
  318. }
  319. private bool _sendCommand(Operation op)
  320. {
  321. if (_noneParaCommandOp.ContainsKey(op))
  322. {
  323. var cmd = string.Format(_noneParaCommandOp[op], _address);
  324. return _sendCmd(cmd);
  325. }
  326. else if (_readDataCommandOp.ContainsKey(op))
  327. {
  328. var cmd = string.Format(_readDataCommandOp[op], _address);
  329. return _sendCmd(cmd);
  330. }
  331. _noRepeatAlarm($"Adixen Turbo Pump: The {op} command need parameters");
  332. return false;
  333. }
  334. private bool _sendCommand(Operation op, int data)
  335. {
  336. if (_setDataCommandOp.ContainsKey(op))
  337. {
  338. var cmd = string.Format(_setDataCommandOp[op], _address, data);
  339. return _sendCmd(cmd);
  340. }
  341. _noRepeatAlarm($"Adixen Turbo Pump: The command {op} does not need one parameter");
  342. return false;
  343. }
  344. private bool _sendCmd(string cmd)
  345. {
  346. _queryWatch.Restart();
  347. return _serial.Write(cmd);
  348. }
  349. private void _noRepeatAlarm(string alarm)
  350. {
  351. if(_lastAlarmString != alarm)
  352. {
  353. _lastAlarmString = alarm;
  354. LOG.Write(eEvent.ERR_TURBO_PUMP, Module, alarm);
  355. }
  356. }
  357. }
  358. }