CometRF.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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 double 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 double 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. thread.Start();
  410. return true;
  411. }
  412. public virtual bool Connect()
  413. {
  414. _commErr = false;
  415. _socket?.Connect(this._addr);
  416. return true;
  417. }
  418. public void OnDataChanged(byte[] rawMessage)
  419. {
  420. try
  421. {
  422. int recTransactionNumber = BitConverter.ToInt32(new byte[] { rawMessage[1], rawMessage[0], 0x00, 0x00 }, 0);
  423. if (recTransactionNumber != TransactionNumber)
  424. {
  425. if (!SC.GetValue<bool>("System.IsSimulatorMode"))
  426. LOG.Write(eEvent.ERR_RF, Module, $"RF transaction number is different");
  427. return;
  428. }
  429. if (rawMessage[6] != _address)
  430. {
  431. LOG.Write(eEvent.ERR_RF, Module, $"RF invalid address byte");
  432. }
  433. if (rawMessage[7] == _read)
  434. {
  435. if (rawMessage[8] == 4)
  436. {
  437. int DataValue = BytesToInt(new byte[] { rawMessage[9], rawMessage[10], rawMessage[11], rawMessage[12] }, 0);
  438. byte[] abc = IntToBytes(DataValue);
  439. ParseReceiveData(_currentCommandNumber, DataValue);
  440. }
  441. else
  442. {
  443. string ReadData = Encoding.ASCII.GetString(rawMessage, 9, Convert.ToInt32(rawMessage[8]));
  444. ParseReceiveData(_currentCommandNumber, 0, ReadData);
  445. }
  446. }
  447. else if (rawMessage[7] == _write)
  448. {
  449. int CommandNumber = BitConverter.ToInt32(new byte[] { rawMessage[9], rawMessage[8], 0x00, 0x00 }, 0);
  450. int DataValue = BytesToInt(new byte[] { rawMessage[10], rawMessage[11], rawMessage[12], rawMessage[13] }, 0);
  451. ParseReceiveData(CommandNumber, DataValue);
  452. }
  453. else if (rawMessage[7] == _readInvalid)
  454. {
  455. int DataValue = BitConverter.ToInt32(new byte[] { rawMessage[8], 0x00, 0x00, 0x00 }, 0);
  456. LOG.Write(eEvent.ERR_RF, Module, $"{Module} [{Display} Read] invalid request, Command Number is {_currentCommandNumber.ToString()}, Exception Code is 0x{DataValue.ToString("X")}");
  457. }
  458. else if (rawMessage[7] == _writeInvalid)
  459. {
  460. int DataValue = BitConverter.ToInt32(new byte[] { rawMessage[8], 0x00, 0x00, 0x00 }, 0);
  461. LOG.Write(eEvent.ERR_RF, Module, $"{Module} [{Display} Write] invalid request, Command Number is {_currentCommandNumber.ToString()}, Exception Code is 0x{DataValue.ToString("X")}");
  462. }
  463. else
  464. {
  465. LOG.Write(eEvent.ERR_RF, Module, $"RF invalid function code");
  466. }
  467. }
  468. //catch (ExcuteFailedException e)
  469. //{
  470. // EV.PostMessage("RF", EventEnum.DefaultWarning, string.Format("executed failed. {0}", e.Message));
  471. // OnError();
  472. // _exceuteErr = false;
  473. //}
  474. //catch (InvalidPackageException e)
  475. //{
  476. // EV.PostMessage("RF", EventEnum.DefaultWarning, string.Format("receive invalid package. {0}", e.Message));
  477. // OnError();
  478. //}
  479. catch (System.Exception ex)
  480. {
  481. _commErr = true;
  482. LOG.WriteExeption("RF failed:" , ex);
  483. }
  484. }
  485. private void ParseReceiveData(int cometCommand, int value, string sValue = "")
  486. {
  487. switch (cometCommand)
  488. {
  489. case 8000:
  490. switch (value)
  491. {
  492. case 0:
  493. LOG.Write(eEvent.ERR_RF, Module, $"{Module} [{Display} Read] Device is not ready");
  494. break;
  495. case 1:
  496. //LOG.Info($"{Module} [{Display} Read] Device is ready, RF is off");
  497. Status = GeneratorStatus.OFF;
  498. break;
  499. case 2:
  500. //LOG.Info($"{Module} [{Display} Read] Device is active, RF is on");
  501. Status = GeneratorStatus.ON;
  502. break;
  503. case 3:
  504. //LOG.Info($"{Module} [{Display} Read] Device is in error state");
  505. Status = GeneratorStatus.ERROR;
  506. break;
  507. case 4:
  508. //LOG.Info($"{Module} [{Display} Read] Device is in calibration state");
  509. break;
  510. case 5:
  511. //LOG.Info($"{Module} [{Display} Read] Device is waiting for firmware update");
  512. break;
  513. case 6:
  514. //LOG.Info($"{Module} [{Display} Read] Device is blocked for configuration update");
  515. break;
  516. default:
  517. break;
  518. }
  519. break;
  520. case 8021:
  521. //LOG.Info($"{Module} [{Display} Read] RF Forward power is {value / 1000} W");
  522. ForwardPower = value / 1000;
  523. break;
  524. case 8022:
  525. //LOG.Info($"{Module} [{Display} Read] RF Reflected power is {value / 1000} W");
  526. ReflectPower = value / 1000;
  527. break;
  528. case 8100:
  529. //LOG.Info($"{Module} [{Display} Read] RF Number of Errors is {value}");
  530. break;
  531. case 8101:
  532. //LOG.Info($"{Module} [{Display} Read] RF Error 1 is {sValue}");
  533. break;
  534. case 8102:
  535. //LOG.Info($"{Module} [{Display} Read] RF Error 1 state is {value}");
  536. break;
  537. case 8103:
  538. //LOG.Info($"{Module} [{Display} Read] RF Error 2 is {sValue}");
  539. break;
  540. case 8104:
  541. //LOG.Info($"{Module} [{Display} Read] RF Error 2 state is {value}");
  542. break;
  543. case 8105:
  544. //LOG.Info($"{Module} [{Display} Read] RF Error 3 is {sValue}");
  545. break;
  546. case 8106:
  547. //LOG.Info($"{Module} [{Display} Read] RF Error 3 state is {value}");
  548. break;
  549. case 8107:
  550. //LOG.Info($"{Module} [{Display} Read] RF Error 4 is {sValue}");
  551. break;
  552. case 8108:
  553. //LOG.Info($"{Module} [{Display} Read] RF Error 4 state is {value}");
  554. break;
  555. case 8109:
  556. //LOG.Info($"{Module} [{Display} Read] RF Error 5 is {sValue}");
  557. break;
  558. case 8110:
  559. //LOG.Info($"{Module} [{Display} Read] RF Error 5 state is {value}");
  560. break;
  561. case 8111:
  562. //LOG.Info($"{Module} [{Display} Read] RF Error 6 is {sValue}");
  563. break;
  564. case 8112:
  565. //LOG.Info($"{Module} [{Display} Read] RF Error 6 state is {value}");
  566. break;
  567. case 8113:
  568. //LOG.Info($"{Module} [{Display} Read] RF Error 7 is {sValue}");
  569. break;
  570. case 8114:
  571. //LOG.Info($"{Module} [{Display} Read] RF Error 7 state is {value}");
  572. break;
  573. case 8115:
  574. //LOG.Info($"{Module} [{Display} Read] RF Error 8 is {sValue}");
  575. break;
  576. case 8116:
  577. //LOG.Info($"{Module} [{Display} Read] RF Error 8 state is {value}");
  578. break;
  579. case 8150:
  580. //LOG.Info($"{Module} [{Display} Read] RF Number of warnings is {value}");
  581. break;
  582. case 8151:
  583. //LOG.Info($"{Module} [{Display} Read] RF Warning 1 is {sValue}");
  584. break;
  585. case 8152:
  586. //LOG.Info($"{Module} [{Display} Read] RF Warning 2 is {sValue}");
  587. break;
  588. case 8153:
  589. //LOG.Info($"{Module} [{Display} Read] RF Warning 3 is {sValue}");
  590. break;
  591. case 8154:
  592. //LOG.Info($"{Module} [{Display} Read] RF Warning 4 is {sValue}");
  593. break;
  594. case 8155:
  595. //LOG.Info($"{Module} [{Display} Read] RF Warning 5 is {sValue}");
  596. break;
  597. case 8156:
  598. //LOG.Info($"{Module} [{Display} Read] RF Warning 6 is {sValue}");
  599. break;
  600. case 8157:
  601. //LOG.Info($"{Module} [{Display} Read] RF Warning 7 is {sValue}");
  602. break;
  603. case 8158:
  604. //LOG.Info($"{Module} [{Display} Read] RF Warning 8 is {sValue}");
  605. break;
  606. case 8159:
  607. //LOG.Info($"{Module} [{Display} Read] RF Warning 9 is {sValue}");
  608. break;
  609. case 8160:
  610. //LOG.Info($"{Module} [{Display} Read] RF Warning 10 is {sValue}");
  611. break;
  612. case 8161:
  613. //LOG.Info($"{Module} [{Display} Read] RF Warning 11 is {sValue}");
  614. break;
  615. case 8162:
  616. //LOG.Info($"{Module} [{Display} Read] RF Warning 12 is {sValue}");
  617. break;
  618. case 8163:
  619. //LOG.Info($"{Module} [{Display} Read] RF Warning 13 is {sValue}");
  620. break;
  621. case 8164:
  622. //LOG.Info($"{Module} [{Display} Read] RF Warning 14 is {sValue}");
  623. break;
  624. case 8165:
  625. //LOG.Info($"{Module} [{Display} Read] RF Warning 15 is {sValue}");
  626. break;
  627. case 8166:
  628. //LOG.Info($"{Module} [{Display} Read] RF Warning 16 is {sValue}");
  629. break;
  630. case 9201:
  631. switch (value)
  632. {
  633. case 0:
  634. //LOG.Info($"{Module} [BiasMatch Read] No connected");
  635. StatusMatch = GeneratorStatus.OFF;
  636. break;
  637. case 1:
  638. //LOG.Info($"{Module} [BiasMatch Read] Manual matching mode");
  639. StatusMatch = GeneratorStatus.ON;
  640. break;
  641. case 2:
  642. //LOG.Info($"{Module} [BiasMatch Read] Automatic matching mode");
  643. StatusMatch = GeneratorStatus.ON;
  644. break;
  645. case 11:
  646. //LOG.Info($"{Module} [BiasMatch Read] Manual matching mode (remote)");
  647. StatusMatch = GeneratorStatus.ON;
  648. break;
  649. case 12:
  650. //LOG.Info($"{Module} [BiasMatch Read] Automatic matching mode (remote)");
  651. StatusMatch = GeneratorStatus.ON;
  652. break;
  653. case 21:
  654. //LOG.Info($"{Module} [BiasMatch Read] Error matching mode");
  655. StatusMatch = GeneratorStatus.ERROR;
  656. break;
  657. default:
  658. break;
  659. }
  660. break;
  661. case 9202:
  662. switch (value)
  663. {
  664. case 0:
  665. //LOG.Info($"{Module} [BiasMatch Read] holding position");
  666. break;
  667. case 1:
  668. //LOG.Info($"{Module} [BiasMatch Read] moving");
  669. break;
  670. default:
  671. break;
  672. }
  673. break;
  674. case 9203:
  675. //LOG.Info($"{Module} [BiasMatch Read] C load position is {value} %");
  676. CLoad = value / 10f;
  677. break;
  678. case 9204:
  679. //LOG.Info($"{Module} [BiasMatch Read] C tune position is {value} %");
  680. CTune = value / 10f;
  681. break;
  682. case 9205:
  683. //LOG.Info($"{Module} [BiasMatch Read] C load is {value} pF");
  684. break;
  685. case 9206:
  686. //LOG.Info($"{Module} [BiasMatch Read] C tune is {value} pF");
  687. break;
  688. case 9210:
  689. //LOG.Info($"{Module} [BiasMatch Read] C load ref pos is {value} %");
  690. break;
  691. case 9212:
  692. //LOG.Info($"{Module} [BiasMatch Read] C tune ref pos {value} %");
  693. break;
  694. case 9251:
  695. //LOG.Info($"{Module} [BiasMatch Read] Process control Matching {value} V");
  696. VPP = value / 1000;
  697. break;
  698. case 1001:
  699. switch (value)
  700. {
  701. case 0:
  702. //LOG.Info($"{Module} [{Display} Write] Switch off RF");
  703. break;
  704. case 1:
  705. //LOG.Info($"{Module} [{Display} Write] Switch on RF");
  706. break;
  707. case 9:
  708. //LOG.Info($"{Module} [{Display} Write] RF Reset errors");
  709. break;
  710. default:
  711. break;
  712. }
  713. break;
  714. case 1201:
  715. switch (value)
  716. {
  717. case 0:
  718. //LOG.Info($"{Module} [{Display} Write] RF Control mode is Forward power");
  719. break;
  720. case 1:
  721. //LOG.Info($"{Module} [{Display} Write] RF Control mode is Load power");
  722. break;
  723. case 2:
  724. //LOG.Info($"{Module} [{Display} Write] RF Control mode is Process control");
  725. break;
  726. default:
  727. break;
  728. }
  729. break;
  730. case 1206:
  731. //LOG.Info($"{Module} [{Display} Write] RF Power Set point is {value / 1000} W");
  732. PowerSetPoint = (_scEnableCalibration.BoolValue ? value : CalibrationData(value, false)) / 1000;
  733. break;
  734. case 8201:
  735. switch (value)
  736. {
  737. case 1:
  738. //LOG.Info($"{Module} [BiasMatch Write] Manual matching mode");
  739. break;
  740. case 2:
  741. //LOG.Info($"{Module} [BiasMatch Write] Automatic matching mode");
  742. break;
  743. default:
  744. break;
  745. }
  746. break;
  747. case 8202:
  748. switch (value)
  749. {
  750. case 0:
  751. //LOG.Info($"{Module} [BiasMatch Write] BiasMatch is Automatic hold mode");
  752. MatchMode = MatchingAutoMode.Hold;
  753. break;
  754. case 1:
  755. //LOG.Info($"{Module} [BiasMatch Write] BiasMatch is Automatic preset mode");
  756. MatchMode = MatchingAutoMode.Preset;
  757. break;
  758. default:
  759. break;
  760. }
  761. break;
  762. case 8203:
  763. //LOG.Info($"{Module} [{Display} Write] Set C load ref position is {value / 10} %");
  764. CLoadSet = value / 10f;
  765. break;
  766. case 8204:
  767. //LOG.Info($"{Module} [{Display} Write] Set C tune ref position is {value / 10} %");
  768. CTuneSet = value / 10f;
  769. break;
  770. default:
  771. break;
  772. }
  773. }
  774. public override void Monitor()
  775. {
  776. // power on triggered
  777. _rfOnTrigger.CLK = PowerSetPoint > 0 && _isPowerOn;
  778. if (_rfOnTrigger.R)
  779. {
  780. _total = TotalDays;
  781. _fromLast = DaysFromLastPM;
  782. _timerTotal.Start(0);
  783. _timerFromLast.Start(0);
  784. _powerOnStartTime = DateTime.Now;
  785. _checkerPower.Reset(_scPowerAlarmTime.DoubleValue);
  786. _checkerReflectPower.Reset(_scReflectPowerAlarmTime.DoubleValue);
  787. }
  788. if (_rfOnTrigger.M)
  789. {
  790. TotalDays = _total + _timerTotal.GetElapseTime() / 1000 / 60 / 60;
  791. DaysFromLastPM = _fromLast + _timerFromLast.GetElapseTime() / 1000 / 60 / 60;
  792. _checkerPower.Monitor(ForwardPower, PowerSetPoint - _scPowerAlarmRange.DoubleValue, PowerSetPoint + _scPowerAlarmRange.DoubleValue, _scPowerAlarmTime.DoubleValue);
  793. if (_checkerPower.Trig)
  794. {
  795. LOG.Write(eEvent.ERR_RF, Module,
  796. $"{Display} Forward power {ForwardPower:0} out of range[{PowerSetPoint - _scPowerAlarmRange.DoubleValue:0},{PowerSetPoint + _scPowerAlarmRange.DoubleValue:0}] in {_scPowerAlarmTime.DoubleValue:0} seconds");
  797. SetPowerOnOff(false, out _);
  798. }
  799. _checkerReflectPower.Monitor(ReflectPower, double.MinValue, _scReflectPowerAlarmRange.DoubleValue, _scReflectPowerAlarmTime.DoubleValue);
  800. if (_checkerReflectPower.Trig)
  801. {
  802. LOG.Write(eEvent.ERR_RF, Module,
  803. $"{Display} Reflect power {ReflectPower:0} out of range[0,{_scReflectPowerAlarmRange.DoubleValue:0}] in {_scReflectPowerAlarmTime.DoubleValue:0} seconds");
  804. SetPowerOnOff(false, out _);
  805. }
  806. }
  807. if (PMIntervalDays > 0)
  808. {
  809. _trigPMNeeded.CLK = IsPMNeeded;
  810. if (_trigPMNeeded.Q)
  811. {
  812. if (EnableAlarm)
  813. {
  814. LOG.Write(eEvent.ERR_RF, Module, "rf on time value larger than setting interval days");
  815. }
  816. }
  817. }
  818. if(_rfOnTrigger.T)
  819. StatsDataManager.Instance.Increase($"{Module}.BiasRfOnTime", $"{Module} RfOnTime", DaysFromLastPM, TotalDays);
  820. if (!_rfOnTrigger.CLK)
  821. {
  822. ForwardPower = 0;
  823. ReflectPower = 0;
  824. }
  825. base.Monitor();
  826. }
  827. public override void Terminate()
  828. {
  829. _socket?.Dispose();
  830. }
  831. public override void SetPower(float val)
  832. {
  833. var power = !_scEnableCalibration.BoolValue ? val : CalibrationData(val, true);
  834. SendCmd(Mode.Write, CometRFCommand.PowerSetPoint, (int)power * 1000);
  835. }
  836. public override bool SetPowerOnOff(bool on, out string str)
  837. {
  838. str = "";
  839. var _chamber = DEVICE.GetDevice<JetPMBase>(Module);
  840. if (on && !_chamber.CheckGeneratorAndHVInterlock(VenusDevice.Rf))
  841. {
  842. return false;
  843. }
  844. _isPowerOn = on;
  845. SendCmd(Mode.Write, CometRFCommand.Command, on ? 1 : 0);
  846. return true;
  847. }
  848. public override bool SetMatchingAutoMode(bool on, out string str)
  849. {
  850. str = "";
  851. SendCmd(Mode.Write, CometRFCommand.MatchingAutoMode, on ? 1 : 0);
  852. return true;
  853. }
  854. public override bool SetMatchPosition(double c1, double c2, out string reason)
  855. {
  856. reason = string.Empty;
  857. if (c1 <= 100 && c1 >= 0)
  858. {
  859. double c1FilterValue = c1;
  860. SendCmd(Mode.Write, CometRFCommand.CLoadRefPosition, (int)(c1 * 10));
  861. reason = string.Format("Match position C1 set to {0} ", c1FilterValue);
  862. }
  863. if (c2 <= 100 && c2 >= 0)
  864. {
  865. double c2FilterValue = c2;
  866. SendCmd(Mode.Write, CometRFCommand.CTuneRefPosition, (int)(c2 * 10));
  867. reason += string.Format("Match position C2 set to {0}", c2FilterValue);
  868. }
  869. return true;
  870. }
  871. public override void SetPulseMode(bool on)
  872. {
  873. SendCmd(Mode.Write, CometRFCommand.TurnPulseMode, on ? 1 : 0);
  874. }
  875. public override void SetPulseRateFreq(int nFreq)
  876. {
  877. if(nFreq > 0)
  878. {
  879. int nPeriod = 10000000 / nFreq;
  880. SendCmd(Mode.Write, CometRFCommand.PulsePeriod, nPeriod);
  881. }
  882. else
  883. {
  884. LOG.Write(eEvent.ERR_RF, Module, $"{Name},SetPulseRateFreq() parameter error: {nFreq}");
  885. }
  886. }
  887. public override void SetPulseDutyCycle(int percentage)
  888. {
  889. if(percentage >= 10 && percentage <= 90)
  890. {
  891. SendCmd(Mode.Write, CometRFCommand.PulseDutyCycle, percentage * 10);
  892. }
  893. else
  894. {
  895. LOG.Write(eEvent.ERR_RF, Module, $"{Name},SetPulseDutyCycle() parameter error: {percentage}");
  896. }
  897. }
  898. private bool SendCmd(Mode mode, int command, int argument)
  899. {
  900. if (command == CometRFCommand.Command && argument == 0)
  901. {
  902. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Bias Generator send RF off");
  903. }
  904. else if (command == CometRFCommand.Command && argument == 1)
  905. {
  906. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Bias Generator send RF on");
  907. }
  908. if (command == CometRFCommand.PowerSetPoint)
  909. {
  910. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Bias Generator send [{argument / 1000}W]");
  911. }
  912. _currentCommandNumber = command;
  913. _sendData = BuildMessage(mode, command, argument);
  914. _socket?.Write(_sendData);
  915. Thread.Sleep(100);
  916. return true;
  917. }
  918. protected byte[] BuildMessage(Mode mode, int commandNumber, int argumentNumber)
  919. {
  920. List<byte> buffer = new List<byte>();
  921. TransactionNumber++;
  922. if (TransactionNumber > 65535) TransactionNumber = 0;
  923. buffer.Add(BitConverter.GetBytes(TransactionNumber)[1]);
  924. buffer.Add(BitConverter.GetBytes(TransactionNumber)[0]);
  925. buffer.Add(ProtocolIdentifierHighByte);
  926. buffer.Add(ProtocolIdentifierLowByte);
  927. if (mode == Mode.Read)
  928. {
  929. buffer.Add(0x00);
  930. buffer.Add(0x06);
  931. }
  932. else if (mode == Mode.Write)
  933. {
  934. buffer.Add(0x00);
  935. buffer.Add(0x08);
  936. }
  937. buffer.Add(_address);
  938. if (mode == Mode.Read)
  939. {
  940. buffer.Add(_read);
  941. }
  942. else if (mode == Mode.Write)
  943. {
  944. buffer.Add(_write);
  945. }
  946. buffer.Add(BitConverter.GetBytes(commandNumber)[1]);
  947. buffer.Add(BitConverter.GetBytes(commandNumber)[0]);
  948. if (mode == Mode.Read)
  949. {
  950. buffer.Add(0x00);
  951. buffer.Add(0x01);
  952. }
  953. else if (mode == Mode.Write)
  954. {
  955. buffer.Add(BitConverter.GetBytes(argumentNumber)[3]);
  956. buffer.Add(BitConverter.GetBytes(argumentNumber)[2]);
  957. buffer.Add(BitConverter.GetBytes(argumentNumber)[1]);
  958. buffer.Add(BitConverter.GetBytes(argumentNumber)[0]);
  959. }
  960. //byte checkSum = 0;
  961. //for (int i = 0; i < buffer.Count; i++)
  962. //{
  963. // checkSum += buffer[i];
  964. //}
  965. //buffer.Add(checkSum);
  966. return buffer.ToArray();
  967. }
  968. public int BytesToInt(byte[] src, int offset)
  969. {
  970. int value;
  971. value = (int)((src[offset + 3] & 0xFF)
  972. | ((src[offset + 2] & 0xFF) << 8)
  973. | ((src[offset + 1] & 0xFF) << 16)
  974. | ((src[offset] & 0xFF) << 24));
  975. return value;
  976. }
  977. public byte[] IntToBytes(int value)
  978. {
  979. byte[] src = new byte[4];
  980. src[0] = (byte)((value >> 24) & 0xFF);
  981. src[1] = (byte)((value >> 16) & 0xFF);
  982. src[2] = (byte)((value >> 8) & 0xFF);
  983. src[3] = (byte)(value & 0xFF);
  984. return src;
  985. }
  986. public override void Reset()
  987. {
  988. _rfOnTrigger.RST = true;
  989. _ErrTrigger.RST = true;
  990. _trigPMNeeded.RST = true;
  991. SendCmd(Mode.Write, CometRFCommand.Command, 9);
  992. Status = GeneratorStatus.OFF;
  993. StatusMatch = GeneratorStatus.OFF;
  994. _exceuteErr = false;
  995. if (_commErr)
  996. {
  997. Connect();
  998. }
  999. }
  1000. ~CometRF()
  1001. {
  1002. _socket?.Dispose();
  1003. }
  1004. }
  1005. }