AdixenTurboPump.cs 17 KB

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