AdixenTurboPump.cs 16 KB

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