AdixenTurboPump.cs 18 KB

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