AdixenTurboPump.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. ClearDeviceValue();
  246. }
  247. public override void Reset()
  248. {
  249. Operation result;
  250. while (_queCommands.TryDequeue(out result)) ;
  251. //_serial.ClearPortBuffer();
  252. _lastCommand = Operation.Invalid;
  253. }
  254. public override void Terminate()
  255. {
  256. _serial?.Close();
  257. }
  258. private void OnErrorOccurred(string obj)
  259. {
  260. _noRepeatAlarm($"[{Module}] Adixen Turbo Pump serial port error: [{obj}]");
  261. }
  262. private void OnPortDataChanged(string obj)
  263. {
  264. try
  265. {
  266. _lastCommand = Operation.Invalid;
  267. if (obj.Substring(0, 2) == "OK")
  268. {
  269. return;
  270. }
  271. if (obj.Substring(0, 3) == "Err")
  272. {
  273. if (_comError.ContainsKey(obj.Substring(0, 4)))
  274. {
  275. _noRepeatAlarm($"Adixen Turbo Pump communication error: {_comError[obj.Substring(0, 4)]}");
  276. }
  277. else
  278. {
  279. _noRepeatAlarm($"Adixen Turbo Pump unknown communication error: {obj}");
  280. }
  281. return;
  282. }
  283. var result_data = obj.Trim().Split(',');
  284. if (result_data.Length >= 9)
  285. {
  286. int speed = 0;
  287. int temp = 0;
  288. if (int.TryParse(result_data[0], out speed))
  289. {
  290. Speed = speed;
  291. }
  292. if (int.TryParse(result_data[8], out temp))
  293. {
  294. Temperature = temp;
  295. }
  296. }
  297. }
  298. catch (Exception ex)
  299. {
  300. _noRepeatAlarm($"[{Module}] Adixen Turbo Pump error: [{ex.Message}], Data: {obj}");
  301. }
  302. }
  303. private void _OnHandleBinaryData(byte[] obj)
  304. {
  305. //System.Threading.Monitor.Enter(_Lock);
  306. byte start = System.Text.Encoding.Default.GetBytes("#")[0];
  307. _serial.HandlePorts(obj, 78, start, Module, eEvent.ERR_TURBO_PUMP, 9);
  308. if (_serial.bValidate == true)
  309. {
  310. _pumpStatus = _serial.GetData[6];
  311. _valveStatus = _serial.GetData[7];
  312. //int nOffset = 9;
  313. //_serial.str.Remove(0, nOffset);
  314. OnPortDataChanged(_serial.str.ToString());
  315. _serial.str.Clear();
  316. _serial.GetData = new byte[] { };
  317. _serial.bValidate = false;
  318. }
  319. }
  320. private byte[] GetData = new byte[] { };
  321. private void OnHandleBinaryData(byte[] obj)
  322. {
  323. bool bValidate = false;
  324. GetData = (byte[])GetData.Concat(obj).ToArray();
  325. var strData1 = System.Text.Encoding.Default.GetString(GetData);
  326. for (int i = 0; i < GetData.Length; i++)
  327. {
  328. if (GetData[i] == (byte)'#')
  329. {
  330. if ((GetData.Length - i) >= 78)
  331. {
  332. _pumpStatus = GetData[i + 6];
  333. _valveStatus = GetData[i + 7];
  334. int nOffset = 9;
  335. byte[] array = new byte[GetData.Length - i - nOffset];
  336. Array.Copy(GetData, i + nOffset, array, 0, GetData.Length - i - nOffset);
  337. int _start = i + 78;
  338. int _end = GetData.Length - 1;
  339. //GetData = (byte[])GetData.Skip(_start).Take(_end - _start + 1).ToArray();
  340. GetData = new byte[] { };
  341. var strData = System.Text.Encoding.Default.GetString(array);
  342. OnPortDataChanged(strData);
  343. bValidate = true;
  344. break;
  345. }
  346. }
  347. }
  348. if (GetData.Length > 156 && !bValidate)
  349. {
  350. var strData = System.Text.Encoding.Default.GetString(GetData);
  351. _noRepeatAlarm($"Receive invalidate data:{strData} ");
  352. }
  353. }
  354. private void OnPortBinaryDataChanged(byte[] obj)
  355. {
  356. bool bValidate = false;
  357. for (int i = 0; i < obj.Length; i++)
  358. {
  359. if (obj[i] == (byte)'#' && obj.Length > i + 5 && obj[i + 4] == (byte)',')
  360. {
  361. int nOffset = 5;
  362. if (obj.Length - i >= _internal_parameters_length)
  363. {
  364. _pumpStatus = obj[i + 6];
  365. _valveStatus = obj[i + 7];
  366. nOffset = 9;
  367. }
  368. byte[] array = new byte[obj.Length - i - nOffset];
  369. Array.Copy(obj, i + nOffset, array, 0, obj.Length - i - nOffset);
  370. var strData = System.Text.Encoding.Default.GetString(array);
  371. OnPortDataChanged(strData);
  372. bValidate = true;
  373. break;
  374. }
  375. }
  376. if (!bValidate)
  377. {
  378. var strData = System.Text.Encoding.Default.GetString(obj);
  379. _noRepeatAlarm($"Receive invalidate data:{strData} ");
  380. }
  381. }
  382. public override void SetPumpOnOff(bool on)
  383. {
  384. var _chamber = DEVICE.GetDevice<JetPMBase>(Module);
  385. if (on && !_chamber.TurboPumpInterlock)
  386. {
  387. LOG.Write(eEvent.ERR_TURBO_PUMP, Module, "Cannot Run Turbo Pump for Turbo Pump Interlock is off.");
  388. return;
  389. }
  390. _queCommands.Enqueue(on ? Operation.StartPumpRotation : Operation.StopPump);
  391. }
  392. private bool _sendCommand(Operation op)
  393. {
  394. if (_noneParaCommandOp.ContainsKey(op))
  395. {
  396. var cmd = string.Format(_noneParaCommandOp[op], _address);
  397. return _sendCmd(cmd);
  398. }
  399. else if (_readDataCommandOp.ContainsKey(op))
  400. {
  401. var cmd = string.Format(_readDataCommandOp[op], _address);
  402. return _sendCmd(cmd);
  403. }
  404. _noRepeatAlarm($"Adixen Turbo Pump: The {op} command need parameters");
  405. return false;
  406. }
  407. private bool _sendCommand(Operation op, int data)
  408. {
  409. if (_setDataCommandOp.ContainsKey(op))
  410. {
  411. var cmd = string.Format(_setDataCommandOp[op], _address, data);
  412. return _sendCmd(cmd);
  413. }
  414. _noRepeatAlarm($"Adixen Turbo Pump: The command {op} does not need one parameter");
  415. return false;
  416. }
  417. private bool _sendCmd(string cmd)
  418. {
  419. _responseWatch.Restart();
  420. return _serial.Write(cmd);
  421. }
  422. private void _noRepeatAlarm(string alarm)
  423. {
  424. if (_lastAlarmString != alarm)
  425. {
  426. _lastAlarmString = alarm;
  427. LOG.Write(eEvent.ERR_TURBO_PUMP, Module, alarm);
  428. }
  429. }
  430. public override bool ReConnect()
  431. {
  432. return _serial.ReConnect();
  433. }
  434. private void ClearDeviceValue()
  435. {
  436. if (IsConnected == false)
  437. {
  438. IsRunning = false;
  439. }
  440. }
  441. }
  442. }