AdixenTurboPump.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. }
  191. else
  192. {
  193. Operation command;
  194. if(_queCommands.TryDequeue(out command))
  195. {
  196. _sendCommand(command);
  197. _lastCommand = command;
  198. }
  199. }
  200. }
  201. else if(_queryWatch.ElapsedMilliseconds > _timeout)
  202. {
  203. _serial.ClearPortBuffer();
  204. _lastCommand = Operation.Invalid;
  205. }
  206. }
  207. public override void Reset()
  208. {
  209. Operation result;
  210. while (_queCommands.TryDequeue(out result)) ;
  211. //_serial.ClearPortBuffer();
  212. _lastCommand = Operation.Invalid;
  213. }
  214. public override void Terminate()
  215. {
  216. _serial?.Close();
  217. }
  218. private void OnErrorOccurred(string obj)
  219. {
  220. _noRepeatAlarm($"[{Module}] Adixen Turbo Pump serial port error: [{obj}]");
  221. }
  222. private void OnPortDataChanged(string obj)
  223. {
  224. try
  225. {
  226. _lastCommand = Operation.Invalid;
  227. if (obj.Substring(0, 2) == "OK")
  228. {
  229. return;
  230. }
  231. if(obj.Substring(0, 3) == "Err")
  232. {
  233. if (_comError.ContainsKey(obj.Substring(0, 4)))
  234. {
  235. _noRepeatAlarm($"Adixen Turbo Pump communication error: {_comError[obj.Substring(0, 4)]}");
  236. }
  237. else
  238. {
  239. _noRepeatAlarm($"Adixen Turbo Pump unknown communication error: {obj}");
  240. }
  241. return;
  242. }
  243. var result_data = obj.Trim().Split(',');
  244. if (result_data.Length >= 9)
  245. {
  246. int speed = 0;
  247. int temp = 0;
  248. if (int.TryParse(result_data[0], out speed))
  249. {
  250. Speed = speed;
  251. }
  252. if (int.TryParse(result_data[8], out temp))
  253. {
  254. Temperature = temp;
  255. }
  256. }
  257. }
  258. catch (Exception ex)
  259. {
  260. _noRepeatAlarm($"[{Module}] Adixen Turbo Pump error: [{ex.Message}], Data: {obj}");
  261. }
  262. }
  263. private void OnPortBinaryDataChanged(byte[] obj)
  264. {
  265. bool bValidate = false;
  266. for(int i=0; i< obj.Length; i++)
  267. {
  268. if(obj[i] == (byte)'#')
  269. {
  270. int nOffset = 5;
  271. if(obj.Length - i >= _internal_parameters_length)
  272. {
  273. _pumpStatus = obj[i + 6];
  274. _valveStatus = obj[i + 7];
  275. nOffset = 9;
  276. }
  277. byte[] array = new byte[obj.Length - i - nOffset];
  278. Array.Copy(obj, i + nOffset, array, 0, obj.Length - i - nOffset);
  279. var strData = System.Text.Encoding.Default.GetString(array);
  280. OnPortDataChanged(strData);
  281. bValidate = true;
  282. break;
  283. }
  284. }
  285. if(!bValidate)
  286. {
  287. var strData = System.Text.Encoding.Default.GetString(obj);
  288. _noRepeatAlarm($"Receive invalidate data:{strData} ");
  289. }
  290. }
  291. public override void SetPumpOnOff(bool on)
  292. {
  293. var _chamber = DEVICE.GetDevice<JetPM>(Module);
  294. if (on && !_chamber.TurboPumpInterlock)
  295. {
  296. LOG.Write(eEvent.ERR_TURBO_PUMP, Module, "Cannot Run Turbo Pump for Turbo Pump Interlock(DI-9) is off.");
  297. return ;
  298. }
  299. _queCommands.Enqueue(on ? Operation.StartPumpRotation : Operation.StopPump);
  300. }
  301. private bool _sendCommand(Operation op)
  302. {
  303. if (_noneParaCommandOp.ContainsKey(op))
  304. {
  305. var cmd = string.Format(_noneParaCommandOp[op], _address);
  306. return _sendCmd(cmd);
  307. }
  308. else if (_readDataCommandOp.ContainsKey(op))
  309. {
  310. var cmd = string.Format(_readDataCommandOp[op], _address);
  311. return _sendCmd(cmd);
  312. }
  313. _noRepeatAlarm($"Adixen Turbo Pump: The {op} command need parameters");
  314. return false;
  315. }
  316. private bool _sendCommand(Operation op, int data)
  317. {
  318. if (_setDataCommandOp.ContainsKey(op))
  319. {
  320. var cmd = string.Format(_setDataCommandOp[op], _address, data);
  321. return _sendCmd(cmd);
  322. }
  323. _noRepeatAlarm($"Adixen Turbo Pump: The command {op} does not need one parameter");
  324. return false;
  325. }
  326. private bool _sendCmd(string cmd)
  327. {
  328. _queryWatch.Restart();
  329. return _serial.Write(cmd);
  330. }
  331. private void _noRepeatAlarm(string alarm)
  332. {
  333. if(_lastAlarmString != alarm)
  334. {
  335. _lastAlarmString = alarm;
  336. LOG.Write(eEvent.ERR_TURBO_PUMP, Module, alarm);
  337. }
  338. }
  339. }
  340. }