CometRF.cs 45 KB

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