CometRF.cs 41 KB

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