CometRF.cs 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using Aitex.Core.Common.DeviceData;
  9. using Aitex.Core.RT.ConfigCenter;
  10. using Aitex.Core.RT.DataCenter;
  11. using Aitex.Core.RT.Event;
  12. using Aitex.Core.RT.IOCore;
  13. using Aitex.Core.RT.Log;
  14. using Aitex.Core.RT.OperationCenter;
  15. using Aitex.Core.RT.SCCore;
  16. using Aitex.Core.RT.Tolerance;
  17. using Aitex.Core.Util;
  18. using Aitex.Core.RT.Device;
  19. using Aitex.Sorter.Common;
  20. using MECF.Framework.Common.Communications;
  21. using MECF.Framework.Common.DataCenter;
  22. using MECF.Framework.Common.Device.Bases;
  23. using MECF.Framework.Common.Equipment;
  24. using Venus_Core;
  25. using Venus_RT.Modules;
  26. namespace Venus_RT.Devices
  27. {
  28. public enum Mode { Read = 0, Write }
  29. public enum MatchingAutoMode { Hold = 0, Preset }
  30. static class CometRFCommand
  31. {
  32. public const int Model = 11;
  33. public const int Type = 12;
  34. public const int SerialNumber = 13;
  35. public const int NominalPower = 15;
  36. public const int NominalFrequency = 16;
  37. public const int Command = 1001;
  38. public const int FrequencyMode = 1101;
  39. public const int ControlMode = 1201;
  40. public const int PowerSetPoint = 1206;
  41. public const int TurnPulseMode = 1301;
  42. public const int PulsePeriod = 1302;
  43. public const int PulseDutyCycle = 1303;
  44. public const int RFOnTime = 1701;
  45. public const int ForwardPowerLimit = 1702;
  46. public const int ReflectedPowerLimit = 1703;
  47. public const int MatchingMode = 1703;
  48. public const int State = 8000;
  49. public const int NumberOfErrors = 8100;
  50. public const int NumberOfWarnings = 8150;
  51. public const int ForwardPower = 8021;
  52. public const int ReflectedPower = 8022;
  53. public const int MatchinMode = 8201;
  54. public const int MatchingAutoMode = 8202;
  55. public const int ActCLoadPosition = 8213;
  56. public const int ActCTunePosition = 8214;
  57. public const int CLoadRefPosition = 8203;
  58. public const int CTuneRefPosition = 8204;
  59. public const int MatchingState = 9201;
  60. public const int MatchingActive = 9202;
  61. public const int CLoadPosition = 9203;
  62. public const int CTunePosition = 9204;
  63. public const int CLoad = 9205;
  64. public const int CTune = 9206;
  65. public const int CLoadRefPos = 9210;
  66. public const int CTuneRefPos = 9212;
  67. public const int ProcessControlMatching = 9251;
  68. }
  69. class CometRF : RfPowerBase
  70. {
  71. private double _total;
  72. private double _fromLast;
  73. private readonly SCConfigItem _scPowerAlarmTime;
  74. private readonly SCConfigItem _scPowerAlarmRange;
  75. private readonly SCConfigItem _scReflectPowerAlarmTime;
  76. private readonly SCConfigItem _scReflectPowerAlarmRange;
  77. private readonly SCConfigItem _scPowerRange;
  78. private int TransactionNumber = 0;
  79. private static byte ProtocolIdentifierHighByte = 0x00;
  80. private static byte ProtocolIdentifierLowByte = 0x00;
  81. private static byte _address = 0x0A;
  82. private static byte _read = 0x41;
  83. private static byte _write = 0x42;
  84. private static byte _readInvalid = 0xC1;
  85. private static byte _writeInvalid = 0xC2;
  86. private static byte[] _sendData;
  87. private const int QUERY_INTERVAL = 500;
  88. protected bool _commErr = false;
  89. protected bool _exceuteErr = false;
  90. protected string _addr;
  91. protected static Object _locker = new Object();
  92. private int _currentCommandNumber = 0;
  93. public GeneratorStatus Status { get; set; }
  94. public GeneratorStatus StatusMatch { get; set; }
  95. public MatchingAutoMode MatchMode { get; set; }
  96. public int ErrorCode { get; set; }
  97. public bool Initalized { get; set; }
  98. public const string delimiter = "\r";
  99. //private string AlarmRobotError = "RF Error";
  100. protected AsyncSocketDevice _socket;
  101. private DateTime _powerOnStartTime;
  102. private TimeSpan _powerOnElapsedTime;
  103. private readonly DeviceTimer _timerQueryStatus = new DeviceTimer();
  104. private readonly DeviceTimer _timerTotal = new DeviceTimer();
  105. private readonly DeviceTimer _timerFromLast = new DeviceTimer();
  106. private readonly RD_TRIG _rfOnTrigger = new RD_TRIG();
  107. private readonly R_TRIG _ErrTrigger = new R_TRIG();
  108. private readonly R_TRIG _trigPMNeeded = new R_TRIG();
  109. private readonly RD_TRIG _trigOnOff = new RD_TRIG();
  110. private StatsDataItemRFAndPump _statRFOnTime;
  111. private ToleranceChecker _checkerPower;
  112. private ToleranceChecker _checkerReflectPower;
  113. private readonly DIAccessor _diIntlk;
  114. private bool _isPowerOn;
  115. public override float ScalePower => (float)_scPowerRange.DoubleValue;
  116. public string Address
  117. {
  118. get { return _addr; }
  119. }
  120. public bool IsConnected
  121. {
  122. get
  123. {
  124. if (_socket == null)
  125. return false;
  126. else
  127. return _socket.IsConnected;
  128. }
  129. }
  130. public bool Disconnect()
  131. {
  132. return true;
  133. }
  134. public bool Communication
  135. {
  136. get
  137. {
  138. return !_commErr || _commErr || _exceuteErr;
  139. }
  140. }
  141. public bool Error
  142. {
  143. get
  144. {
  145. return ErrorCode > 0;
  146. }
  147. }
  148. public int MatchProcessMode
  149. {
  150. get
  151. {
  152. return (int)MatchMode;
  153. }
  154. }
  155. public override bool IsPowerOn
  156. {
  157. get => Status == GeneratorStatus.ON;
  158. set { }
  159. }
  160. public override bool IsMatchOn
  161. {
  162. get => StatusMatch == GeneratorStatus.ON;
  163. set { }
  164. }
  165. public override bool IsError
  166. {
  167. get => Status == GeneratorStatus.ERROR;
  168. set { }
  169. }
  170. public override bool IsMatchError
  171. {
  172. get => StatusMatch == GeneratorStatus.ERROR;
  173. set { }
  174. }
  175. public string LastPMTime
  176. {
  177. get
  178. {
  179. return _statRFOnTime != null ? _statRFOnTime.LastPMTime.ToString() : "";
  180. }
  181. }
  182. public float DaysFromLastPM
  183. {
  184. get
  185. {
  186. return _statRFOnTime == null ? 0 : _statRFOnTime.fromLastPM;
  187. }
  188. set
  189. {
  190. if (_statRFOnTime != null)
  191. _statRFOnTime.fromLastPM = value;
  192. }
  193. }
  194. public float TotalDays
  195. {
  196. get
  197. {
  198. return _statRFOnTime != null ? _statRFOnTime.Total : 0;
  199. }
  200. set
  201. {
  202. if (_statRFOnTime != null)
  203. _statRFOnTime.Total = value;
  204. }
  205. }
  206. public double PMIntervalDays
  207. {
  208. get
  209. {
  210. return _statRFOnTime != null ? _statRFOnTime.PMInterval : 0;
  211. }
  212. }
  213. public bool IsPMNeeded
  214. {
  215. get
  216. {
  217. return DaysFromLastPM > PMIntervalDays;
  218. }
  219. }
  220. public bool EnableAlarm
  221. {
  222. get
  223. {
  224. return _statRFOnTime == null || _statRFOnTime.AlarmEnable;
  225. }
  226. }
  227. [Subscription("PowerOnTime")]
  228. public string PowerOnTime
  229. {
  230. get
  231. {
  232. if (IsPowerOn)
  233. _powerOnElapsedTime = DateTime.Now - _powerOnStartTime;
  234. return $"{(int)_powerOnElapsedTime.TotalHours:00}:{_powerOnElapsedTime.Minutes:00}:{(_powerOnElapsedTime.Seconds > 0 ? (_powerOnElapsedTime.Seconds + 1) : 0):00}";
  235. }
  236. }
  237. public bool RFInterlock => _diIntlk == null || _diIntlk.Value;
  238. private float _forwardPower;
  239. public override float ForwardPower
  240. {
  241. get
  242. {
  243. return _forwardPower;
  244. }
  245. set
  246. {
  247. _forwardPower = CalibrationData(value, false);
  248. }
  249. }
  250. public new AITRfData DeviceData =>
  251. new AITRfData
  252. {
  253. Module = Module,
  254. DeviceName = Name,
  255. ScalePower = ScalePower,
  256. ForwardPower = ForwardPower,
  257. ReflectPower = ReflectPower,
  258. IsRfOn = IsPowerOn,
  259. PowerSetPoint = PowerSetPoint,
  260. PowerOnElapsedTime = PowerOnTime,
  261. IsInterlockOk = RFInterlock,
  262. IsMatchOn = IsMatchOn,
  263. MatchPositionC1 = CLoad,
  264. MatchPositionC2 = CTune,
  265. MatchVPP = VPP,
  266. MatchPresetMode = MatchProcessMode,
  267. MatchPositionC1SetPoint = CLoadSet,
  268. MatchPositionC2SetPoint = CTuneSet,
  269. WorkMode = (int)RfMode.ContinuousWaveMode,
  270. DisplayName = "Bias RF",
  271. };
  272. public CometRF(ModuleName mod, string address) : base(mod.ToString(), VenusDevice.BiasRf.ToString())
  273. {
  274. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  275. {
  276. address = "127.0.0.1:502";
  277. }
  278. Status = GeneratorStatus.Unknown;
  279. StatusMatch = GeneratorStatus.Unknown;
  280. MatchMode = MatchingAutoMode.Preset;
  281. _addr = address;
  282. _socket = new AsyncSocketDevice(address);
  283. _socket.OnDataChanged += new AsyncSocketDevice.MessageHandler(OnDataChanged);
  284. _socket.OnErrorHappened += new AsyncSocketDevice.ErrorHandler(OnErrorHandler);
  285. _scPowerAlarmTime = SC.GetConfigItem($"{Module}.{Name}.PowerAlarmTime");
  286. _scPowerAlarmRange = SC.GetConfigItem($"{Module}.{Name}.PowerAlarmRange");
  287. _scReflectPowerAlarmTime = SC.GetConfigItem($"{Module}.{Name}.ReflectPowerAlarmTime");
  288. _scReflectPowerAlarmRange = SC.GetConfigItem($"{Module}.{Name}.ReflectPowerAlarmRange");
  289. _scPowerRange = SC.GetConfigItem($"{Module}.{Name}.PowerRange");
  290. _scEnableCalibration = SC.GetConfigItem($"{Module}.{Name}.EnableCalibration");
  291. _scCalibrationTable = SC.GetConfigItem($"{Module}.{Name}.CalibrationTable");
  292. _diIntlk = IO.DI[$"{Module}.DI_RF_Generator_Interlock"];
  293. Initalized = false;
  294. }
  295. ErrorEventArgsDevice oldargs;
  296. private void OnErrorHandler(ErrorEventArgsDevice args)
  297. {
  298. //SetPowerOnOff(false, out _);
  299. Status = GeneratorStatus.ERROR;
  300. StatusMatch = GeneratorStatus.ERROR;
  301. if (oldargs != null && oldargs.Code == args.Code) return;
  302. LOG.Write(eEvent.ERR_RF,Module, $"{Module} Comet RF Error {args.Reason}");
  303. oldargs = args;
  304. }
  305. public override bool Initialize()
  306. {
  307. base.Initialize();
  308. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  309. Connect();
  310. _statRFOnTime = StatsDataManager.Instance.GetItemRFAndPump($"{Module}.BiasRfOnTime");
  311. _timerQueryStatus.Start(QUERY_INTERVAL);
  312. _checkerPower = new ToleranceChecker(_scPowerAlarmTime.DoubleValue);
  313. _checkerReflectPower = new ToleranceChecker(_scReflectPowerAlarmTime.DoubleValue);
  314. OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) =>
  315. {
  316. return Connect();
  317. });
  318. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetPowerOnOff}", (out string reason, int time, object[] param) =>
  319. {
  320. SetPowerOnOff(Convert.ToBoolean((string)param[0]), out reason);
  321. return true;
  322. });
  323. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetPower}", (out string reason, int time, object[] param) =>
  324. {
  325. reason = "";
  326. ushort val = Convert.ToUInt16(param[0]);
  327. SetPower(val);
  328. return true;
  329. });
  330. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetContinuousPower}", (out string reason, int time, object[] param) =>
  331. {
  332. reason = "";
  333. ushort val = Convert.ToUInt16(param[0]);
  334. SetPower(val);
  335. return true;
  336. });
  337. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
  338. {
  339. BiasRfMatchMode mode = (BiasRfMatchMode)Enum.Parse(typeof(BiasRfMatchMode), (string)param[0], true);
  340. reason = "";
  341. if (mode == BiasRfMatchMode.Hold)
  342. SetMatchingAutoMode(false, out reason);
  343. else
  344. SetMatchingAutoMode(true, out reason);
  345. return true;
  346. });
  347. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) =>
  348. {
  349. if (MatchMode == MatchingAutoMode.Hold)
  350. {
  351. reason = string.Empty;
  352. return true;
  353. }
  354. float c1 = (float)Convert.ToDouble((string)param[0]);
  355. reason = string.Format("Set RF match position c1 :{0}", c1);
  356. if (c1 <= 100 && c1 >= 0)
  357. {
  358. SendCmd(Mode.Write, CometRFCommand.CLoadRefPosition, (int)(c1 * 10));
  359. }
  360. return true;
  361. });
  362. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) =>
  363. {
  364. if (MatchMode == MatchingAutoMode.Hold)
  365. {
  366. reason = string.Empty;
  367. return true;
  368. }
  369. float c2 = (float)Convert.ToDouble((string)param[0]);
  370. reason = string.Format("Set RF match position c2 :{0}", c2);
  371. if (c2 <= 100 && c2 >= 0)
  372. {
  373. SendCmd(Mode.Write, CometRFCommand.CTuneRefPosition, (int)(c2 * 10));
  374. }
  375. return true;
  376. });
  377. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) =>
  378. {
  379. return SetMatchPosition(Convert.ToDouble((string)param[0]), Convert.ToDouble((string)param[1]), out reason);
  380. });
  381. DATA.Subscribe($"{Module}.{Name}.CommunicationStatus", () => _socket == null ? false : _socket.IsConnected);
  382. //EV.Subscribe(new EventItem("Event", AlarmRobotError, "Robot error", EventLevel.Alarm, Aitex.Core.RT.Event.EventType.HostNotification));
  383. //DATA.Subscribe($"{Module}.BiasMatch.C1", () => CLoad);
  384. //DATA.Subscribe($"{Module}.BiasMatch.C2", () => CTune);
  385. ////DATA.Subscribe($"{Module}.BiasMatch.VPP", () => VPP);
  386. //DATA.Subscribe($"{Module}.BiasMatch.MatchProcessMode", () => (int)MatchMode);
  387. Reset();
  388. SendCmd(Mode.Write, CometRFCommand.ControlMode, 0);
  389. SendCmd(Mode.Write, CometRFCommand.MatchinMode, 2);
  390. Thread thread = new Thread(() =>
  391. {
  392. while (true)
  393. {
  394. SendCmd(Mode.Read, CometRFCommand.State, 1);
  395. SendCmd(Mode.Read, CometRFCommand.ForwardPower, 1);
  396. SendCmd(Mode.Read, CometRFCommand.ReflectedPower, 1);
  397. SendCmd(Mode.Read, CometRFCommand.MatchingState, 1);
  398. SendCmd(Mode.Read, CometRFCommand.MatchingActive, 1);
  399. SendCmd(Mode.Read, CometRFCommand.CLoadPosition, 1);
  400. SendCmd(Mode.Read, CometRFCommand.CTunePosition, 1);
  401. SendCmd(Mode.Read, CometRFCommand.ProcessControlMatching, 1);
  402. //if(IsError)
  403. //{
  404. // SendCmd(Mode.Read, CometRFCommand.NumberOfErrors, 1);
  405. // SendCmd(Mode.Read, CometRFCommand.NumberOfWarnings, 1);
  406. //}
  407. }
  408. });
  409. if (IsConnected)
  410. {
  411. thread.Start();
  412. }
  413. return true;
  414. }
  415. public virtual bool Connect()
  416. {
  417. _commErr = false;
  418. _socket?.Connect(this._addr);
  419. return true;
  420. }
  421. public void OnDataChanged(byte[] rawMessage)
  422. {
  423. try
  424. {
  425. int recTransactionNumber = BitConverter.ToInt32(new byte[] { rawMessage[1], rawMessage[0], 0x00, 0x00 }, 0);
  426. if (recTransactionNumber != TransactionNumber)
  427. {
  428. if (!SC.GetValue<bool>("System.IsSimulatorMode"))
  429. LOG.Write(eEvent.ERR_RF, Module, $"RF transaction number is different");
  430. return;
  431. }
  432. if (rawMessage[6] != _address)
  433. {
  434. LOG.Write(eEvent.ERR_RF, Module, $"RF invalid address byte");
  435. }
  436. if (rawMessage[7] == _read)
  437. {
  438. if (rawMessage[8] == 4)
  439. {
  440. int DataValue = BytesToInt(new byte[] { rawMessage[9], rawMessage[10], rawMessage[11], rawMessage[12] }, 0);
  441. byte[] abc = IntToBytes(DataValue);
  442. ParseReceiveData(_currentCommandNumber, DataValue);
  443. }
  444. else
  445. {
  446. string ReadData = Encoding.ASCII.GetString(rawMessage, 9, Convert.ToInt32(rawMessage[8]));
  447. ParseReceiveData(_currentCommandNumber, 0, ReadData);
  448. }
  449. }
  450. else if (rawMessage[7] == _write)
  451. {
  452. int CommandNumber = BitConverter.ToInt32(new byte[] { rawMessage[9], rawMessage[8], 0x00, 0x00 }, 0);
  453. int DataValue = BytesToInt(new byte[] { rawMessage[10], rawMessage[11], rawMessage[12], rawMessage[13] }, 0);
  454. ParseReceiveData(CommandNumber, DataValue);
  455. }
  456. else if (rawMessage[7] == _readInvalid)
  457. {
  458. int DataValue = BitConverter.ToInt32(new byte[] { rawMessage[8], 0x00, 0x00, 0x00 }, 0);
  459. LOG.Write(eEvent.ERR_RF, Module, $"{Module} [{Display} Read] invalid request, Command Number is {_currentCommandNumber.ToString()}, Exception Code is 0x{DataValue.ToString("X")}");
  460. }
  461. else if (rawMessage[7] == _writeInvalid)
  462. {
  463. int DataValue = BitConverter.ToInt32(new byte[] { rawMessage[8], 0x00, 0x00, 0x00 }, 0);
  464. LOG.Write(eEvent.ERR_RF, Module, $"{Module} [{Display} Write] invalid request, Command Number is {_currentCommandNumber.ToString()}, Exception Code is 0x{DataValue.ToString("X")}");
  465. }
  466. else
  467. {
  468. LOG.Write(eEvent.ERR_RF, Module, $"RF invalid function code");
  469. }
  470. }
  471. //catch (ExcuteFailedException e)
  472. //{
  473. // EV.PostMessage("RF", EventEnum.DefaultWarning, string.Format("executed failed. {0}", e.Message));
  474. // OnError();
  475. // _exceuteErr = false;
  476. //}
  477. //catch (InvalidPackageException e)
  478. //{
  479. // EV.PostMessage("RF", EventEnum.DefaultWarning, string.Format("receive invalid package. {0}", e.Message));
  480. // OnError();
  481. //}
  482. catch (System.Exception ex)
  483. {
  484. _commErr = true;
  485. LOG.WriteExeption("RF failed:" , ex);
  486. }
  487. }
  488. private void ParseReceiveData(int cometCommand, int value, string sValue = "")
  489. {
  490. switch (cometCommand)
  491. {
  492. case 8000:
  493. switch (value)
  494. {
  495. case 0:
  496. LOG.Write(eEvent.WARN_RF, Module, $"{Module} [{Display} Read] Device is not ready");
  497. break;
  498. case 1:
  499. LOG.Write(eEvent.INFO_RF, Module, $"{Module} [{Display} Read] Device is ready, RF is off");
  500. Status = GeneratorStatus.OFF;
  501. break;
  502. case 2:
  503. LOG.Write(eEvent.INFO_RF, Module, $"{Module} [{Display} Read] Device is active, RF is on");
  504. Status = GeneratorStatus.ON;
  505. break;
  506. case 3:
  507. LOG.Write(eEvent.ERR_RF, Module, $"{Module} [{Display} Read] Device is in error state");
  508. Status = GeneratorStatus.ERROR;
  509. break;
  510. case 4:
  511. LOG.Write(eEvent.INFO_RF, Module, $"{Module} [{Display} Read] Device is in calibration state");
  512. break;
  513. case 5:
  514. LOG.Write(eEvent.INFO_RF, Module, $"{Module} [{Display} Read] Device is waiting for firmware update");
  515. break;
  516. case 6:
  517. LOG.Write(eEvent.INFO_RF, Module, $"{Module} [{Display} Read] Device is blocked for configuration update");
  518. break;
  519. default:
  520. break;
  521. }
  522. break;
  523. case 8021:
  524. //LOG.Info($"{Module} [{Display} Read] RF Forward power is {value / 1000} W");
  525. ForwardPower = value / 1000;
  526. break;
  527. case 8022:
  528. //LOG.Info($"{Module} [{Display} Read] RF Reflected power is {value / 1000} W");
  529. ReflectPower = value / 1000;
  530. break;
  531. case 8100:
  532. //LOG.Info($"{Module} [{Display} Read] RF Number of Errors is {value}");
  533. break;
  534. case 8101:
  535. //LOG.Info($"{Module} [{Display} Read] RF Error 1 is {sValue}");
  536. break;
  537. case 8102:
  538. //LOG.Info($"{Module} [{Display} Read] RF Error 1 state is {value}");
  539. break;
  540. case 8103:
  541. //LOG.Info($"{Module} [{Display} Read] RF Error 2 is {sValue}");
  542. break;
  543. case 8104:
  544. //LOG.Info($"{Module} [{Display} Read] RF Error 2 state is {value}");
  545. break;
  546. case 8105:
  547. //LOG.Info($"{Module} [{Display} Read] RF Error 3 is {sValue}");
  548. break;
  549. case 8106:
  550. //LOG.Info($"{Module} [{Display} Read] RF Error 3 state is {value}");
  551. break;
  552. case 8107:
  553. //LOG.Info($"{Module} [{Display} Read] RF Error 4 is {sValue}");
  554. break;
  555. case 8108:
  556. //LOG.Info($"{Module} [{Display} Read] RF Error 4 state is {value}");
  557. break;
  558. case 8109:
  559. //LOG.Info($"{Module} [{Display} Read] RF Error 5 is {sValue}");
  560. break;
  561. case 8110:
  562. //LOG.Info($"{Module} [{Display} Read] RF Error 5 state is {value}");
  563. break;
  564. case 8111:
  565. //LOG.Info($"{Module} [{Display} Read] RF Error 6 is {sValue}");
  566. break;
  567. case 8112:
  568. //LOG.Info($"{Module} [{Display} Read] RF Error 6 state is {value}");
  569. break;
  570. case 8113:
  571. //LOG.Info($"{Module} [{Display} Read] RF Error 7 is {sValue}");
  572. break;
  573. case 8114:
  574. //LOG.Info($"{Module} [{Display} Read] RF Error 7 state is {value}");
  575. break;
  576. case 8115:
  577. //LOG.Info($"{Module} [{Display} Read] RF Error 8 is {sValue}");
  578. break;
  579. case 8116:
  580. //LOG.Info($"{Module} [{Display} Read] RF Error 8 state is {value}");
  581. break;
  582. case 8150:
  583. //LOG.Info($"{Module} [{Display} Read] RF Number of warnings is {value}");
  584. break;
  585. case 8151:
  586. //LOG.Info($"{Module} [{Display} Read] RF Warning 1 is {sValue}");
  587. break;
  588. case 8152:
  589. //LOG.Info($"{Module} [{Display} Read] RF Warning 2 is {sValue}");
  590. break;
  591. case 8153:
  592. //LOG.Info($"{Module} [{Display} Read] RF Warning 3 is {sValue}");
  593. break;
  594. case 8154:
  595. //LOG.Info($"{Module} [{Display} Read] RF Warning 4 is {sValue}");
  596. break;
  597. case 8155:
  598. //LOG.Info($"{Module} [{Display} Read] RF Warning 5 is {sValue}");
  599. break;
  600. case 8156:
  601. //LOG.Info($"{Module} [{Display} Read] RF Warning 6 is {sValue}");
  602. break;
  603. case 8157:
  604. //LOG.Info($"{Module} [{Display} Read] RF Warning 7 is {sValue}");
  605. break;
  606. case 8158:
  607. //LOG.Info($"{Module} [{Display} Read] RF Warning 8 is {sValue}");
  608. break;
  609. case 8159:
  610. //LOG.Info($"{Module} [{Display} Read] RF Warning 9 is {sValue}");
  611. break;
  612. case 8160:
  613. //LOG.Info($"{Module} [{Display} Read] RF Warning 10 is {sValue}");
  614. break;
  615. case 8161:
  616. //LOG.Info($"{Module} [{Display} Read] RF Warning 11 is {sValue}");
  617. break;
  618. case 8162:
  619. //LOG.Info($"{Module} [{Display} Read] RF Warning 12 is {sValue}");
  620. break;
  621. case 8163:
  622. //LOG.Info($"{Module} [{Display} Read] RF Warning 13 is {sValue}");
  623. break;
  624. case 8164:
  625. //LOG.Info($"{Module} [{Display} Read] RF Warning 14 is {sValue}");
  626. break;
  627. case 8165:
  628. //LOG.Info($"{Module} [{Display} Read] RF Warning 15 is {sValue}");
  629. break;
  630. case 8166:
  631. //LOG.Info($"{Module} [{Display} Read] RF Warning 16 is {sValue}");
  632. break;
  633. case 9201:
  634. switch (value)
  635. {
  636. case 0:
  637. //LOG.Info($"{Module} [BiasMatch Read] No connected");
  638. StatusMatch = GeneratorStatus.OFF;
  639. break;
  640. case 1:
  641. //LOG.Info($"{Module} [BiasMatch Read] Manual matching mode");
  642. StatusMatch = GeneratorStatus.ON;
  643. break;
  644. case 2:
  645. //LOG.Info($"{Module} [BiasMatch Read] Automatic matching mode");
  646. StatusMatch = GeneratorStatus.ON;
  647. break;
  648. case 11:
  649. //LOG.Info($"{Module} [BiasMatch Read] Manual matching mode (remote)");
  650. StatusMatch = GeneratorStatus.ON;
  651. break;
  652. case 12:
  653. //LOG.Info($"{Module} [BiasMatch Read] Automatic matching mode (remote)");
  654. StatusMatch = GeneratorStatus.ON;
  655. break;
  656. case 21:
  657. //LOG.Info($"{Module} [BiasMatch Read] Error matching mode");
  658. StatusMatch = GeneratorStatus.ERROR;
  659. break;
  660. default:
  661. break;
  662. }
  663. break;
  664. case 9202:
  665. switch (value)
  666. {
  667. case 0:
  668. //LOG.Info($"{Module} [BiasMatch Read] holding position");
  669. break;
  670. case 1:
  671. //LOG.Info($"{Module} [BiasMatch Read] moving");
  672. break;
  673. default:
  674. break;
  675. }
  676. break;
  677. case 9203:
  678. //LOG.Info($"{Module} [BiasMatch Read] C load position is {value} %");
  679. CLoad = value / 10f;
  680. break;
  681. case 9204:
  682. //LOG.Info($"{Module} [BiasMatch Read] C tune position is {value} %");
  683. CTune = value / 10f;
  684. break;
  685. case 9205:
  686. //LOG.Info($"{Module} [BiasMatch Read] C load is {value} pF");
  687. break;
  688. case 9206:
  689. //LOG.Info($"{Module} [BiasMatch Read] C tune is {value} pF");
  690. break;
  691. case 9210:
  692. //LOG.Info($"{Module} [BiasMatch Read] C load ref pos is {value} %");
  693. break;
  694. case 9212:
  695. //LOG.Info($"{Module} [BiasMatch Read] C tune ref pos {value} %");
  696. break;
  697. case 9251:
  698. //LOG.Info($"{Module} [BiasMatch Read] Process control Matching {value} V");
  699. VPP = value / 1000;
  700. break;
  701. case 1001:
  702. switch (value)
  703. {
  704. case 0:
  705. //LOG.Info($"{Module} [{Display} Write] Switch off RF");
  706. break;
  707. case 1:
  708. //LOG.Info($"{Module} [{Display} Write] Switch on RF");
  709. break;
  710. case 9:
  711. //LOG.Info($"{Module} [{Display} Write] RF Reset errors");
  712. break;
  713. default:
  714. break;
  715. }
  716. break;
  717. case 1201:
  718. switch (value)
  719. {
  720. case 0:
  721. //LOG.Info($"{Module} [{Display} Write] RF Control mode is Forward power");
  722. break;
  723. case 1:
  724. //LOG.Info($"{Module} [{Display} Write] RF Control mode is Load power");
  725. break;
  726. case 2:
  727. //LOG.Info($"{Module} [{Display} Write] RF Control mode is Process control");
  728. break;
  729. default:
  730. break;
  731. }
  732. break;
  733. case 1206:
  734. //LOG.Info($"{Module} [{Display} Write] RF Power Set point is {value / 1000} W");
  735. PowerSetPoint = (_scEnableCalibration.BoolValue ? value : CalibrationData(value, false)) / 1000;
  736. break;
  737. case 8201:
  738. switch (value)
  739. {
  740. case 1:
  741. //LOG.Info($"{Module} [BiasMatch Write] Manual matching mode");
  742. break;
  743. case 2:
  744. //LOG.Info($"{Module} [BiasMatch Write] Automatic matching mode");
  745. break;
  746. default:
  747. break;
  748. }
  749. break;
  750. case 8202:
  751. switch (value)
  752. {
  753. case 0:
  754. //LOG.Info($"{Module} [BiasMatch Write] BiasMatch is Automatic hold mode");
  755. MatchMode = MatchingAutoMode.Hold;
  756. break;
  757. case 1:
  758. //LOG.Info($"{Module} [BiasMatch Write] BiasMatch is Automatic preset mode");
  759. MatchMode = MatchingAutoMode.Preset;
  760. break;
  761. default:
  762. break;
  763. }
  764. break;
  765. case 8203:
  766. //LOG.Info($"{Module} [{Display} Write] Set C load ref position is {value / 10} %");
  767. CLoadSet = value / 10f;
  768. break;
  769. case 8204:
  770. //LOG.Info($"{Module} [{Display} Write] Set C tune ref position is {value / 10} %");
  771. CTuneSet = value / 10f;
  772. break;
  773. default:
  774. break;
  775. }
  776. }
  777. public override void Monitor()
  778. {
  779. // power on triggered
  780. _rfOnTrigger.CLK = PowerSetPoint > 0 && _isPowerOn;
  781. if (_rfOnTrigger.R)
  782. {
  783. _total = TotalDays;
  784. _fromLast = DaysFromLastPM;
  785. _timerTotal.Start(0);
  786. _timerFromLast.Start(0);
  787. _powerOnStartTime = DateTime.Now;
  788. _checkerPower.Reset(_scPowerAlarmTime.DoubleValue);
  789. _checkerReflectPower.Reset(_scReflectPowerAlarmTime.DoubleValue);
  790. }
  791. if (_rfOnTrigger.M)
  792. {
  793. TotalDays =Convert.ToSingle( _timerTotal.GetElapseTime() / 1000 / 60 / 60);
  794. DaysFromLastPM =Convert.ToSingle( _timerFromLast.GetElapseTime() / 1000 / 60 / 60);
  795. _checkerPower.Monitor(ForwardPower, PowerSetPoint - _scPowerAlarmRange.DoubleValue, PowerSetPoint + _scPowerAlarmRange.DoubleValue, _scPowerAlarmTime.DoubleValue);
  796. if (_checkerPower.Trig)
  797. {
  798. LOG.Write(eEvent.ERR_RF, Module,
  799. $"{Display} Forward power {ForwardPower:0} out of range[{PowerSetPoint - _scPowerAlarmRange.DoubleValue:0},{PowerSetPoint + _scPowerAlarmRange.DoubleValue:0}] in {_scPowerAlarmTime.DoubleValue:0} seconds");
  800. SetPowerOnOff(false, out _);
  801. }
  802. _checkerReflectPower.Monitor(ReflectPower, double.MinValue, _scReflectPowerAlarmRange.DoubleValue, _scReflectPowerAlarmTime.DoubleValue);
  803. if (_checkerReflectPower.Trig)
  804. {
  805. LOG.Write(eEvent.ERR_RF, Module,
  806. $"{Display} Reflect power {ReflectPower:0} out of range[0,{_scReflectPowerAlarmRange.DoubleValue:0}] in {_scReflectPowerAlarmTime.DoubleValue:0} seconds");
  807. SetPowerOnOff(false, out _);
  808. }
  809. }
  810. if (PMIntervalDays > 0)
  811. {
  812. _trigPMNeeded.CLK = IsPMNeeded;
  813. if (_trigPMNeeded.Q)
  814. {
  815. if (EnableAlarm)
  816. {
  817. LOG.Write(eEvent.ERR_RF, Module, "rf on time value larger than setting interval days");
  818. }
  819. }
  820. }
  821. if(_rfOnTrigger.T)
  822. //StatsDataManager.Instance.Increase($"{Module}.BiasRfOnTime", $"{Module} RfOnTime", DaysFromLastPM, TotalDays);
  823. if (!_rfOnTrigger.CLK)
  824. {
  825. ForwardPower = 0;
  826. ReflectPower = 0;
  827. }
  828. base.Monitor();
  829. }
  830. public override void Terminate()
  831. {
  832. _socket?.Dispose();
  833. }
  834. public override void SetPower(float val)
  835. {
  836. var power = !_scEnableCalibration.BoolValue ? val : CalibrationData(val, true);
  837. SendCmd(Mode.Write, CometRFCommand.PowerSetPoint, (int)power * 1000);
  838. }
  839. public override bool SetPowerOnOff(bool on, out string str)
  840. {
  841. str = "";
  842. var _chamber = DEVICE.GetDevice<JetPMBase>(Module);
  843. if (on && !_chamber.CheckGeneratorAndHVInterlock(VenusDevice.Rf))
  844. {
  845. return false;
  846. }
  847. _isPowerOn = on;
  848. SendCmd(Mode.Write, CometRFCommand.Command, on ? 1 : 0);
  849. return true;
  850. }
  851. public override bool SetMatchingAutoMode(bool on, out string str)
  852. {
  853. str = "";
  854. SendCmd(Mode.Write, CometRFCommand.MatchingAutoMode, on ? 1 : 0);
  855. return true;
  856. }
  857. public override bool SetMatchPosition(double c1, double c2, out string reason)
  858. {
  859. reason = string.Empty;
  860. if (c1 <= 100 && c1 >= 0)
  861. {
  862. double c1FilterValue = c1;
  863. SendCmd(Mode.Write, CometRFCommand.CLoadRefPosition, (int)(c1 * 10));
  864. reason = string.Format("Match position C1 set to {0} ", c1FilterValue);
  865. }
  866. if (c2 <= 100 && c2 >= 0)
  867. {
  868. double c2FilterValue = c2;
  869. SendCmd(Mode.Write, CometRFCommand.CTuneRefPosition, (int)(c2 * 10));
  870. reason += string.Format("Match position C2 set to {0}", c2FilterValue);
  871. }
  872. return true;
  873. }
  874. public override void SetPulseMode(bool on)
  875. {
  876. SendCmd(Mode.Write, CometRFCommand.TurnPulseMode, on ? 2 : 0);
  877. }
  878. public override void SetPulseRateFreq(int nFreq)
  879. {
  880. if(nFreq > 0)
  881. {
  882. int nPeriod = 10000000 / nFreq;
  883. SendCmd(Mode.Write, CometRFCommand.PulsePeriod, nPeriod);
  884. }
  885. else
  886. {
  887. LOG.Write(eEvent.ERR_RF, Module, $"{Name},SetPulseRateFreq() parameter error: {nFreq}");
  888. }
  889. }
  890. public override void SetPulseDutyCycle(int percentage)
  891. {
  892. if(percentage >= 10 && percentage <= 90)
  893. {
  894. SendCmd(Mode.Write, CometRFCommand.PulseDutyCycle, percentage * 10);
  895. }
  896. else
  897. {
  898. LOG.Write(eEvent.ERR_RF, Module, $"{Name},SetPulseDutyCycle() parameter error: {percentage}");
  899. }
  900. }
  901. private bool SendCmd(Mode mode, int command, int argument)
  902. {
  903. if (command == CometRFCommand.Command && argument == 0)
  904. {
  905. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Bias Generator send RF off");
  906. }
  907. else if (command == CometRFCommand.Command && argument == 1)
  908. {
  909. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Bias Generator send RF on");
  910. }
  911. if (command == CometRFCommand.PowerSetPoint)
  912. {
  913. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Bias Generator send [{argument / 1000}W]");
  914. }
  915. _currentCommandNumber = command;
  916. _sendData = BuildMessage(mode, command, argument);
  917. _socket?.Write(_sendData);
  918. Thread.Sleep(100);
  919. return true;
  920. }
  921. protected byte[] BuildMessage(Mode mode, int commandNumber, int argumentNumber)
  922. {
  923. List<byte> buffer = new List<byte>();
  924. TransactionNumber++;
  925. if (TransactionNumber > 65535) TransactionNumber = 0;
  926. buffer.Add(BitConverter.GetBytes(TransactionNumber)[1]);
  927. buffer.Add(BitConverter.GetBytes(TransactionNumber)[0]);
  928. buffer.Add(ProtocolIdentifierHighByte);
  929. buffer.Add(ProtocolIdentifierLowByte);
  930. if (mode == Mode.Read)
  931. {
  932. buffer.Add(0x00);
  933. buffer.Add(0x06);
  934. }
  935. else if (mode == Mode.Write)
  936. {
  937. buffer.Add(0x00);
  938. buffer.Add(0x08);
  939. }
  940. buffer.Add(_address);
  941. if (mode == Mode.Read)
  942. {
  943. buffer.Add(_read);
  944. }
  945. else if (mode == Mode.Write)
  946. {
  947. buffer.Add(_write);
  948. }
  949. buffer.Add(BitConverter.GetBytes(commandNumber)[1]);
  950. buffer.Add(BitConverter.GetBytes(commandNumber)[0]);
  951. if (mode == Mode.Read)
  952. {
  953. buffer.Add(0x00);
  954. buffer.Add(0x01);
  955. }
  956. else if (mode == Mode.Write)
  957. {
  958. buffer.Add(BitConverter.GetBytes(argumentNumber)[3]);
  959. buffer.Add(BitConverter.GetBytes(argumentNumber)[2]);
  960. buffer.Add(BitConverter.GetBytes(argumentNumber)[1]);
  961. buffer.Add(BitConverter.GetBytes(argumentNumber)[0]);
  962. }
  963. //byte checkSum = 0;
  964. //for (int i = 0; i < buffer.Count; i++)
  965. //{
  966. // checkSum += buffer[i];
  967. //}
  968. //buffer.Add(checkSum);
  969. return buffer.ToArray();
  970. }
  971. public int BytesToInt(byte[] src, int offset)
  972. {
  973. int value;
  974. value = (int)((src[offset + 3] & 0xFF)
  975. | ((src[offset + 2] & 0xFF) << 8)
  976. | ((src[offset + 1] & 0xFF) << 16)
  977. | ((src[offset] & 0xFF) << 24));
  978. return value;
  979. }
  980. public byte[] IntToBytes(int value)
  981. {
  982. byte[] src = new byte[4];
  983. src[0] = (byte)((value >> 24) & 0xFF);
  984. src[1] = (byte)((value >> 16) & 0xFF);
  985. src[2] = (byte)((value >> 8) & 0xFF);
  986. src[3] = (byte)(value & 0xFF);
  987. return src;
  988. }
  989. public override void Reset()
  990. {
  991. _rfOnTrigger.RST = true;
  992. _ErrTrigger.RST = true;
  993. _trigPMNeeded.RST = true;
  994. SendCmd(Mode.Write, CometRFCommand.Command, 9);
  995. Status = GeneratorStatus.OFF;
  996. StatusMatch = GeneratorStatus.OFF;
  997. _exceuteErr = false;
  998. if (_commErr)
  999. {
  1000. Connect();
  1001. }
  1002. }
  1003. ~CometRF()
  1004. {
  1005. _socket?.Dispose();
  1006. }
  1007. }
  1008. }