AdixenTurboPump.cs 14 KB

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