CometRF.cs 40 KB

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