AdixenTurboPump.cs 17 KB

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