AdixenTurboPump.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 Stopwatch _responseWatch = new Stopwatch();
  158. private readonly int _readInterval = 1000;
  159. private ConcurrentQueue<Operation> _queCommands = new ConcurrentQueue<Operation>();
  160. private Operation _lastCommand = Operation.Invalid;
  161. private string _lastAlarmString = string.Empty;
  162. private Regex _rex_ok = new Regex(@"#\d{1,3},OK");
  163. private Regex _rex_err = new Regex(@"#\d{1,3},Err");
  164. private byte _pumpStatus = 0;
  165. private byte _valveStatus = 0;
  166. public AdixenTurboPump(ModuleName mod)
  167. {
  168. Name = VenusDevice.TurboPump.ToString();
  169. Module = mod.ToString();
  170. var _PortNum = SC.GetStringValue($"{mod}.TurboPump.Port");
  171. _serial = new AsyncSerialPort(_PortNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\r", false);
  172. }
  173. public override bool Initialize()
  174. {
  175. if (!_serial.Open())
  176. {
  177. LOG.Write(eEvent.ERR_TURBO_PUMP, Module, "Adixen Turbo Pump 串口无法打开");
  178. return false;
  179. }
  180. _serial.OnDataChanged += OnPortDataChanged;
  181. _serial.OnBinaryDataChanged += OnPortBinaryDataChanged;
  182. _serial.OnErrorHappened += OnErrorOccurred;
  183. _queryWatch.Start();
  184. return true;
  185. }
  186. public override void Monitor()
  187. {
  188. if(_queryWatch.ElapsedMilliseconds > _readInterval)
  189. {
  190. _queryWatch.Restart();
  191. if (_lastCommand == Operation.Invalid)
  192. {
  193. if (_queCommands.IsEmpty)
  194. {
  195. _sendCommand(Operation.GetIntervalParametersState);
  196. }
  197. else
  198. {
  199. Operation command;
  200. if (_queCommands.TryDequeue(out command))
  201. {
  202. _sendCommand(command);
  203. _lastCommand = command;
  204. }
  205. }
  206. }
  207. else if (_responseWatch.ElapsedMilliseconds > _timeout)
  208. {
  209. _serial.ClearPortBuffer();
  210. _lastCommand = Operation.Invalid;
  211. }
  212. }
  213. }
  214. public override void Reset()
  215. {
  216. Operation result;
  217. while (_queCommands.TryDequeue(out result)) ;
  218. //_serial.ClearPortBuffer();
  219. _lastCommand = Operation.Invalid;
  220. }
  221. public override void Terminate()
  222. {
  223. _serial?.Close();
  224. }
  225. private void OnErrorOccurred(string obj)
  226. {
  227. _noRepeatAlarm($"[{Module}] Adixen Turbo Pump serial port error: [{obj}]");
  228. }
  229. private void OnPortDataChanged(string obj)
  230. {
  231. try
  232. {
  233. _lastCommand = Operation.Invalid;
  234. if (obj.Substring(0, 2) == "OK")
  235. {
  236. return;
  237. }
  238. if(obj.Substring(0, 3) == "Err")
  239. {
  240. if (_comError.ContainsKey(obj.Substring(0, 4)))
  241. {
  242. _noRepeatAlarm($"Adixen Turbo Pump communication error: {_comError[obj.Substring(0, 4)]}");
  243. }
  244. else
  245. {
  246. _noRepeatAlarm($"Adixen Turbo Pump unknown communication error: {obj}");
  247. }
  248. return;
  249. }
  250. var result_data = obj.Trim().Split(',');
  251. if (result_data.Length >= 9)
  252. {
  253. int speed = 0;
  254. int temp = 0;
  255. if (int.TryParse(result_data[0], out speed))
  256. {
  257. Speed = speed;
  258. }
  259. if (int.TryParse(result_data[8], out temp))
  260. {
  261. Temperature = temp;
  262. }
  263. }
  264. }
  265. catch (Exception ex)
  266. {
  267. _noRepeatAlarm($"[{Module}] Adixen Turbo Pump error: [{ex.Message}], Data: {obj}");
  268. }
  269. }
  270. private void OnPortBinaryDataChanged(byte[] obj)
  271. {
  272. bool bValidate = false;
  273. for(int i=0; i< obj.Length; i++)
  274. {
  275. if(obj[i] == (byte)'#' && obj.Length > i + 5 && obj[i+4] == (byte)',')
  276. {
  277. int nOffset = 5;
  278. if(obj.Length - i >= _internal_parameters_length)
  279. {
  280. _pumpStatus = obj[i + 6];
  281. _valveStatus = obj[i + 7];
  282. nOffset = 9;
  283. }
  284. byte[] array = new byte[obj.Length - i - nOffset];
  285. Array.Copy(obj, i + nOffset, array, 0, obj.Length - i - nOffset);
  286. var strData = System.Text.Encoding.Default.GetString(array);
  287. OnPortDataChanged(strData);
  288. bValidate = true;
  289. break;
  290. }
  291. }
  292. if(!bValidate)
  293. {
  294. var strData = System.Text.Encoding.Default.GetString(obj);
  295. _noRepeatAlarm($"Receive invalidate data:{strData} ");
  296. }
  297. }
  298. public override void SetPumpOnOff(bool on)
  299. {
  300. var _chamber = DEVICE.GetDevice<JetPM>(Module);
  301. if (on && !_chamber.TurboPumpInterlock)
  302. {
  303. LOG.Write(eEvent.ERR_TURBO_PUMP, Module, "Cannot Run Turbo Pump for Turbo Pump Interlock(DI-9) is off.");
  304. return ;
  305. }
  306. _queCommands.Enqueue(on ? Operation.StartPumpRotation : Operation.StopPump);
  307. }
  308. private bool _sendCommand(Operation op)
  309. {
  310. if (_noneParaCommandOp.ContainsKey(op))
  311. {
  312. var cmd = string.Format(_noneParaCommandOp[op], _address);
  313. return _sendCmd(cmd);
  314. }
  315. else if (_readDataCommandOp.ContainsKey(op))
  316. {
  317. var cmd = string.Format(_readDataCommandOp[op], _address);
  318. return _sendCmd(cmd);
  319. }
  320. _noRepeatAlarm($"Adixen Turbo Pump: The {op} command need parameters");
  321. return false;
  322. }
  323. private bool _sendCommand(Operation op, int data)
  324. {
  325. if (_setDataCommandOp.ContainsKey(op))
  326. {
  327. var cmd = string.Format(_setDataCommandOp[op], _address, data);
  328. return _sendCmd(cmd);
  329. }
  330. _noRepeatAlarm($"Adixen Turbo Pump: The command {op} does not need one parameter");
  331. return false;
  332. }
  333. private bool _sendCmd(string cmd)
  334. {
  335. _responseWatch.Restart();
  336. return _serial.Write(cmd);
  337. }
  338. private void _noRepeatAlarm(string alarm)
  339. {
  340. if(_lastAlarmString != alarm)
  341. {
  342. _lastAlarmString = alarm;
  343. LOG.Write(eEvent.ERR_TURBO_PUMP, Module, alarm);
  344. }
  345. }
  346. }
  347. }