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 VirgoCommon;
  24. using VirgoRT.Modules;
  25. namespace VirgoRT.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(), VirgoDevice.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. _scRFPhysicalMaxPower = SC.GetConfigItem($"{Module}.{Name}.RFPhysicalMaxPower");
  289. _scCurrentRFMaxPower = SC.GetConfigItem($"{Module}.{Name}.CurrentRFMaxPower");
  290. _diIntlk = IO.DI[$"{Module}.DI_Generator_Hardware_Interlock"];
  291. Initalized = false;
  292. }
  293. private void OnErrorHandler(ErrorEventArgsDevice args)
  294. {
  295. //SetPowerOnOff(false, out _);
  296. Status = GeneratorStatus.ERROR;
  297. StatusMatch = GeneratorStatus.ERROR;
  298. LOG.Error($"{Module} Comet RF Error {args.Reason}");
  299. }
  300. public override bool Initialize()
  301. {
  302. base.Initialize();
  303. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  304. Connect();
  305. _statRFOnTime = StatsDataManager.Instance.GetItemRFAndPump($"{Module}.BiasRfOnTime");
  306. _timerQueryStatus.Start(QUERY_INTERVAL);
  307. _checkerPower = new ToleranceChecker(_scPowerAlarmTime.DoubleValue);
  308. _checkerReflectPower = new ToleranceChecker(_scReflectPowerAlarmTime.DoubleValue);
  309. OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) =>
  310. {
  311. return Connect();
  312. });
  313. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetPowerOnOff}", (out string reason, int time, object[] param) =>
  314. {
  315. SetPowerOnOff(Convert.ToBoolean((string)param[0]), out reason);
  316. return true;
  317. });
  318. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetPower}", (out string reason, int time, object[] param) =>
  319. {
  320. reason = "";
  321. ushort val = Convert.ToUInt16(param[0]);
  322. SetPower(val);
  323. return true;
  324. });
  325. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetContinuousPower}", (out string reason, int time, object[] param) =>
  326. {
  327. reason = "";
  328. ushort val = Convert.ToUInt16(param[0]);
  329. SetPower(val);
  330. return true;
  331. });
  332. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
  333. {
  334. BiasRfMatchMode mode = (BiasRfMatchMode)Enum.Parse(typeof(BiasRfMatchMode), (string)param[0], true);
  335. reason = "";
  336. if (mode == BiasRfMatchMode.Hold)
  337. SetMatchingAutoMode(false, out reason);
  338. else
  339. SetMatchingAutoMode(true, out reason);
  340. return true;
  341. });
  342. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) =>
  343. {
  344. if (MatchMode == MatchingAutoMode.Hold)
  345. {
  346. reason = string.Empty;
  347. return true;
  348. }
  349. float c1 = (float)Convert.ToDouble((string)param[0]);
  350. reason = string.Format("Set RF match position c1 :{0}", c1);
  351. if (c1 <= 100 && c1 >= 0)
  352. {
  353. SendCmd(Mode.Write, CometRFCommand.CLoadRefPosition, (int)(c1 * 10));
  354. }
  355. return true;
  356. });
  357. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) =>
  358. {
  359. if (MatchMode == MatchingAutoMode.Hold)
  360. {
  361. reason = string.Empty;
  362. return true;
  363. }
  364. float c2 = (float)Convert.ToDouble((string)param[0]);
  365. reason = string.Format("Set RF match position c2 :{0}", c2);
  366. if (c2 <= 100 && c2 >= 0)
  367. {
  368. SendCmd(Mode.Write, CometRFCommand.CTuneRefPosition, (int)(c2 * 10));
  369. }
  370. return true;
  371. });
  372. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) =>
  373. {
  374. return SetMatchPosition(Convert.ToDouble((string)param[0]), Convert.ToDouble((string)param[1]), out reason);
  375. });
  376. DATA.Subscribe($"{Module}.{Name}.CommunicationStatus", () => _socket == null ? false : _socket.IsConnected);
  377. //EV.Subscribe(new EventItem("Event", AlarmRobotError, "Robot error", EventLevel.Alarm, Aitex.Core.RT.Event.EventType.HostNotification));
  378. DATA.Subscribe($"{Module}.BiasMatch.C1", () => CLoad);
  379. DATA.Subscribe($"{Module}.BiasMatch.C2", () => CTune);
  380. DATA.Subscribe($"{Module}.BiasMatch.VPP", () => VPP);
  381. DATA.Subscribe($"{Module}.BiasMatch.MatchProcessMode", () => (int)MatchMode);
  382. Reset();
  383. SendCmd(Mode.Write, CometRFCommand.ControlMode, 0);
  384. SendCmd(Mode.Write, CometRFCommand.MatchinMode, 2);
  385. Thread thread = new Thread(() =>
  386. {
  387. while (true)
  388. {
  389. SendCmd(Mode.Read, CometRFCommand.State, 1);
  390. SendCmd(Mode.Read, CometRFCommand.ForwardPower, 1);
  391. SendCmd(Mode.Read, CometRFCommand.ReflectedPower, 1);
  392. SendCmd(Mode.Read, CometRFCommand.MatchingState, 1);
  393. SendCmd(Mode.Read, CometRFCommand.MatchingActive, 1);
  394. SendCmd(Mode.Read, CometRFCommand.CLoadPosition, 1);
  395. SendCmd(Mode.Read, CometRFCommand.CTunePosition, 1);
  396. SendCmd(Mode.Read, CometRFCommand.ProcessControlMatching, 1);
  397. //if(IsError)
  398. //{
  399. // SendCmd(Mode.Read, CometRFCommand.NumberOfErrors, 1);
  400. // SendCmd(Mode.Read, CometRFCommand.NumberOfWarnings, 1);
  401. //}
  402. }
  403. });
  404. thread.Start();
  405. return true;
  406. }
  407. public virtual bool Connect()
  408. {
  409. _commErr = false;
  410. _socket?.Connect(this._addr);
  411. return true;
  412. }
  413. public void OnDataChanged(byte[] rawMessage)
  414. {
  415. try
  416. {
  417. int recTransactionNumber = BitConverter.ToInt32(new byte[] { rawMessage[1], rawMessage[0], 0x00, 0x00 }, 0);
  418. if (recTransactionNumber != TransactionNumber)
  419. {
  420. LOG.Error($"RF transaction number is different");
  421. return;
  422. }
  423. if (rawMessage[6] != _address)
  424. {
  425. LOG.Error($"RF invalid address byte");
  426. }
  427. if (rawMessage[7] == _read)
  428. {
  429. if (rawMessage[8] == 4)
  430. {
  431. int DataValue = BytesToInt(new byte[] { rawMessage[9], rawMessage[10], rawMessage[11], rawMessage[12] }, 0);
  432. byte[] abc = IntToBytes(DataValue);
  433. ParseReceiveData(_currentCommandNumber, DataValue);
  434. }
  435. else
  436. {
  437. string ReadData = Encoding.ASCII.GetString(rawMessage, 9, Convert.ToInt32(rawMessage[8]));
  438. ParseReceiveData(_currentCommandNumber, 0, ReadData);
  439. }
  440. }
  441. else if (rawMessage[7] == _write)
  442. {
  443. int CommandNumber = BitConverter.ToInt32(new byte[] { rawMessage[9], rawMessage[8], 0x00, 0x00 }, 0);
  444. int DataValue = BytesToInt(new byte[] { rawMessage[10], rawMessage[11], rawMessage[12], rawMessage[13] }, 0);
  445. ParseReceiveData(CommandNumber, DataValue);
  446. }
  447. else if (rawMessage[7] == _readInvalid)
  448. {
  449. int DataValue = BitConverter.ToInt32(new byte[] { rawMessage[8], 0x00, 0x00, 0x00 }, 0);
  450. LOG.Error($"{Module} [{Display} Read] invalid request, Command Number is {_currentCommandNumber.ToString()}, Exception Code is 0x{DataValue.ToString("X")}");
  451. }
  452. else if (rawMessage[7] == _writeInvalid)
  453. {
  454. int DataValue = BitConverter.ToInt32(new byte[] { rawMessage[8], 0x00, 0x00, 0x00 }, 0);
  455. LOG.Error($"{Module} [{Display} Write] invalid request, Command Number is {_currentCommandNumber.ToString()}, Exception Code is 0x{DataValue.ToString("X")}");
  456. }
  457. else
  458. {
  459. LOG.Error($"RF invalid function code");
  460. }
  461. }
  462. //catch (ExcuteFailedException e)
  463. //{
  464. // EV.PostMessage("RF", EventEnum.DefaultWarning, string.Format("executed failed. {0}", e.Message));
  465. // OnError();
  466. // _exceuteErr = false;
  467. //}
  468. //catch (InvalidPackageException e)
  469. //{
  470. // EV.PostMessage("RF", EventEnum.DefaultWarning, string.Format("receive invalid package. {0}", e.Message));
  471. // OnError();
  472. //}
  473. catch (System.Exception ex)
  474. {
  475. _commErr = true;
  476. LOG.Write("RF failed:" + ex.ToString());
  477. }
  478. }
  479. private void ParseReceiveData(int cometCommand, int value, string sValue = "")
  480. {
  481. switch (cometCommand)
  482. {
  483. case 8000:
  484. switch (value)
  485. {
  486. case 0:
  487. //LOG.Info($"{Module} [{Display} Read] Device is not ready");
  488. break;
  489. case 1:
  490. //LOG.Info($"{Module} [{Display} Read] Device is ready, RF is off");
  491. Status = GeneratorStatus.OFF;
  492. break;
  493. case 2:
  494. //LOG.Info($"{Module} [{Display} Read] Device is active, RF is on");
  495. Status = GeneratorStatus.ON;
  496. break;
  497. case 3:
  498. //LOG.Info($"{Module} [{Display} Read] Device is in error state");
  499. Status = GeneratorStatus.ERROR;
  500. break;
  501. case 4:
  502. //LOG.Info($"{Module} [{Display} Read] Device is in calibration state");
  503. break;
  504. case 5:
  505. //LOG.Info($"{Module} [{Display} Read] Device is waiting for firmware update");
  506. break;
  507. case 6:
  508. //LOG.Info($"{Module} [{Display} Read] Device is blocked for configuration update");
  509. break;
  510. default:
  511. break;
  512. }
  513. break;
  514. case 8021:
  515. //LOG.Info($"{Module} [{Display} Read] RF Forward power is {value / 1000} W");
  516. ForwardPower = value / 1000;
  517. break;
  518. case 8022:
  519. //LOG.Info($"{Module} [{Display} Read] RF Reflected power is {value / 1000} W");
  520. ReflectPower = value / 1000;
  521. break;
  522. case 8100:
  523. //LOG.Info($"{Module} [{Display} Read] RF Number of Errors is {value}");
  524. break;
  525. case 8101:
  526. LOG.Info($"{Module} [{Display} Read] RF Error 1 is {sValue}");
  527. break;
  528. case 8102:
  529. LOG.Info($"{Module} [{Display} Read] RF Error 1 state is {value}");
  530. break;
  531. case 8103:
  532. LOG.Info($"{Module} [{Display} Read] RF Error 2 is {sValue}");
  533. break;
  534. case 8104:
  535. LOG.Info($"{Module} [{Display} Read] RF Error 2 state is {value}");
  536. break;
  537. case 8105:
  538. LOG.Info($"{Module} [{Display} Read] RF Error 3 is {sValue}");
  539. break;
  540. case 8106:
  541. LOG.Info($"{Module} [{Display} Read] RF Error 3 state is {value}");
  542. break;
  543. case 8107:
  544. LOG.Info($"{Module} [{Display} Read] RF Error 4 is {sValue}");
  545. break;
  546. case 8108:
  547. LOG.Info($"{Module} [{Display} Read] RF Error 4 state is {value}");
  548. break;
  549. case 8109:
  550. LOG.Info($"{Module} [{Display} Read] RF Error 5 is {sValue}");
  551. break;
  552. case 8110:
  553. LOG.Info($"{Module} [{Display} Read] RF Error 5 state is {value}");
  554. break;
  555. case 8111:
  556. LOG.Info($"{Module} [{Display} Read] RF Error 6 is {sValue}");
  557. break;
  558. case 8112:
  559. LOG.Info($"{Module} [{Display} Read] RF Error 6 state is {value}");
  560. break;
  561. case 8113:
  562. LOG.Info($"{Module} [{Display} Read] RF Error 7 is {sValue}");
  563. break;
  564. case 8114:
  565. LOG.Info($"{Module} [{Display} Read] RF Error 7 state is {value}");
  566. break;
  567. case 8115:
  568. LOG.Info($"{Module} [{Display} Read] RF Error 8 is {sValue}");
  569. break;
  570. case 8116:
  571. LOG.Info($"{Module} [{Display} Read] RF Error 8 state is {value}");
  572. break;
  573. case 8150:
  574. LOG.Info($"{Module} [{Display} Read] RF Number of warnings is {value}");
  575. break;
  576. case 8151:
  577. LOG.Info($"{Module} [{Display} Read] RF Warning 1 is {sValue}");
  578. break;
  579. case 8152:
  580. LOG.Info($"{Module} [{Display} Read] RF Warning 2 is {sValue}");
  581. break;
  582. case 8153:
  583. LOG.Info($"{Module} [{Display} Read] RF Warning 3 is {sValue}");
  584. break;
  585. case 8154:
  586. LOG.Info($"{Module} [{Display} Read] RF Warning 4 is {sValue}");
  587. break;
  588. case 8155:
  589. LOG.Info($"{Module} [{Display} Read] RF Warning 5 is {sValue}");
  590. break;
  591. case 8156:
  592. LOG.Info($"{Module} [{Display} Read] RF Warning 6 is {sValue}");
  593. break;
  594. case 8157:
  595. LOG.Info($"{Module} [{Display} Read] RF Warning 7 is {sValue}");
  596. break;
  597. case 8158:
  598. LOG.Info($"{Module} [{Display} Read] RF Warning 8 is {sValue}");
  599. break;
  600. case 8159:
  601. LOG.Info($"{Module} [{Display} Read] RF Warning 9 is {sValue}");
  602. break;
  603. case 8160:
  604. LOG.Info($"{Module} [{Display} Read] RF Warning 10 is {sValue}");
  605. break;
  606. case 8161:
  607. LOG.Info($"{Module} [{Display} Read] RF Warning 11 is {sValue}");
  608. break;
  609. case 8162:
  610. LOG.Info($"{Module} [{Display} Read] RF Warning 12 is {sValue}");
  611. break;
  612. case 8163:
  613. LOG.Info($"{Module} [{Display} Read] RF Warning 13 is {sValue}");
  614. break;
  615. case 8164:
  616. LOG.Info($"{Module} [{Display} Read] RF Warning 14 is {sValue}");
  617. break;
  618. case 8165:
  619. LOG.Info($"{Module} [{Display} Read] RF Warning 15 is {sValue}");
  620. break;
  621. case 8166:
  622. LOG.Info($"{Module} [{Display} Read] RF Warning 16 is {sValue}");
  623. break;
  624. case 9201:
  625. switch (value)
  626. {
  627. case 0:
  628. //LOG.Info($"{Module} [BiasMatch Read] No connected");
  629. StatusMatch = GeneratorStatus.OFF;
  630. break;
  631. case 1:
  632. //LOG.Info($"{Module} [BiasMatch Read] Manual matching mode");
  633. StatusMatch = GeneratorStatus.ON;
  634. break;
  635. case 2:
  636. //LOG.Info($"{Module} [BiasMatch Read] Automatic matching mode");
  637. StatusMatch = GeneratorStatus.ON;
  638. break;
  639. case 11:
  640. //LOG.Info($"{Module} [BiasMatch Read] Manual matching mode (remote)");
  641. StatusMatch = GeneratorStatus.ON;
  642. break;
  643. case 12:
  644. //LOG.Info($"{Module} [BiasMatch Read] Automatic matching mode (remote)");
  645. StatusMatch = GeneratorStatus.ON;
  646. break;
  647. case 21:
  648. //LOG.Info($"{Module} [BiasMatch Read] Error matching mode");
  649. StatusMatch = GeneratorStatus.ERROR;
  650. break;
  651. default:
  652. break;
  653. }
  654. break;
  655. case 9202:
  656. switch (value)
  657. {
  658. case 0:
  659. //LOG.Info($"{Module} [BiasMatch Read] holding position");
  660. break;
  661. case 1:
  662. //LOG.Info($"{Module} [BiasMatch Read] moving");
  663. break;
  664. default:
  665. break;
  666. }
  667. break;
  668. case 9203:
  669. //LOG.Info($"{Module} [BiasMatch Read] C load position is {value} %");
  670. CLoad = value / 10f;
  671. break;
  672. case 9204:
  673. //LOG.Info($"{Module} [BiasMatch Read] C tune position is {value} %");
  674. CTune = value / 10f;
  675. break;
  676. case 9205:
  677. //LOG.Info($"{Module} [BiasMatch Read] C load is {value} pF");
  678. break;
  679. case 9206:
  680. //LOG.Info($"{Module} [BiasMatch Read] C tune is {value} pF");
  681. break;
  682. case 9210:
  683. //LOG.Info($"{Module} [BiasMatch Read] C load ref pos is {value} %");
  684. break;
  685. case 9212:
  686. //LOG.Info($"{Module} [BiasMatch Read] C tune ref pos {value} %");
  687. break;
  688. case 9251:
  689. //LOG.Info($"{Module} [BiasMatch Read] Process control Matching {value} V");
  690. VPP = value / 1000;
  691. break;
  692. case 1001:
  693. switch (value)
  694. {
  695. case 0:
  696. //LOG.Info($"{Module} [{Display} Write] Switch off RF");
  697. break;
  698. case 1:
  699. //LOG.Info($"{Module} [{Display} Write] Switch on RF");
  700. break;
  701. case 9:
  702. //LOG.Info($"{Module} [{Display} Write] RF Reset errors");
  703. break;
  704. default:
  705. break;
  706. }
  707. break;
  708. case 1201:
  709. switch (value)
  710. {
  711. case 0:
  712. //LOG.Info($"{Module} [{Display} Write] RF Control mode is Forward power");
  713. break;
  714. case 1:
  715. //LOG.Info($"{Module} [{Display} Write] RF Control mode is Load power");
  716. break;
  717. case 2:
  718. //LOG.Info($"{Module} [{Display} Write] RF Control mode is Process control");
  719. break;
  720. default:
  721. break;
  722. }
  723. break;
  724. case 1206:
  725. //LOG.Info($"{Module} [{Display} Write] RF Power Set point is {value / 1000} W");
  726. PowerSetPoint = (_scEnableCalibration.BoolValue ? value : CalibrationData(value, false)) / 1000;
  727. break;
  728. case 8201:
  729. switch (value)
  730. {
  731. case 1:
  732. //LOG.Info($"{Module} [BiasMatch Write] Manual matching mode");
  733. break;
  734. case 2:
  735. //LOG.Info($"{Module} [BiasMatch Write] Automatic matching mode");
  736. break;
  737. default:
  738. break;
  739. }
  740. break;
  741. case 8202:
  742. switch (value)
  743. {
  744. case 0:
  745. //LOG.Info($"{Module} [BiasMatch Write] BiasMatch is Automatic hold mode");
  746. MatchMode = MatchingAutoMode.Hold;
  747. break;
  748. case 1:
  749. //LOG.Info($"{Module} [BiasMatch Write] BiasMatch is Automatic preset mode");
  750. MatchMode = MatchingAutoMode.Preset;
  751. break;
  752. default:
  753. break;
  754. }
  755. break;
  756. case 8203:
  757. //LOG.Info($"{Module} [{Display} Write] Set C load ref position is {value / 10} %");
  758. CLoadSet = value / 10f;
  759. break;
  760. case 8204:
  761. //LOG.Info($"{Module} [{Display} Write] Set C tune ref position is {value / 10} %");
  762. CTuneSet = value / 10f;
  763. break;
  764. default:
  765. break;
  766. }
  767. }
  768. public override void Monitor()
  769. {
  770. // power on triggered
  771. _rfOnTrigger.CLK = PowerSetPoint > 0 && _isPowerOn;
  772. if (_rfOnTrigger.R)
  773. {
  774. _total = TotalDays;
  775. _fromLast = DaysFromLastPM;
  776. _timerTotal.Start(0);
  777. _timerFromLast.Start(0);
  778. _powerOnStartTime = DateTime.Now;
  779. _checkerPower.Reset(_scPowerAlarmTime.DoubleValue);
  780. _checkerReflectPower.Reset(_scReflectPowerAlarmTime.DoubleValue);
  781. }
  782. if (_rfOnTrigger.M)
  783. {
  784. TotalDays = _total + _timerTotal.GetElapseTime() / 1000 / 60 / 60;
  785. DaysFromLastPM = _fromLast + _timerFromLast.GetElapseTime() / 1000 / 60 / 60;
  786. _checkerPower.Monitor(ForwardPower, PowerSetPoint - _scPowerAlarmRange.DoubleValue, PowerSetPoint + _scPowerAlarmRange.DoubleValue, _scPowerAlarmTime.DoubleValue);
  787. if (_checkerPower.Trig)
  788. {
  789. EV.PostAlarmLog($"{Module}",
  790. $"{Display} Forward power {ForwardPower:0} out of range[{PowerSetPoint - _scPowerAlarmRange.DoubleValue:0},{PowerSetPoint + _scPowerAlarmRange.DoubleValue:0}] in {_scPowerAlarmTime.DoubleValue:0} seconds");
  791. SetPowerOnOff(false, out _);
  792. }
  793. _checkerReflectPower.Monitor(ReflectPower, double.MinValue, _scReflectPowerAlarmRange.DoubleValue, _scReflectPowerAlarmTime.DoubleValue);
  794. if (_checkerReflectPower.Trig)
  795. {
  796. EV.PostAlarmLog($"{Module}",
  797. $"{Display} Reflect power {ReflectPower:0} out of range[0,{_scReflectPowerAlarmRange.DoubleValue:0}] in {_scReflectPowerAlarmTime.DoubleValue:0} seconds");
  798. SetPowerOnOff(false, out _);
  799. }
  800. }
  801. if (PMIntervalDays > 0)
  802. {
  803. _trigPMNeeded.CLK = IsPMNeeded;
  804. if (_trigPMNeeded.Q)
  805. {
  806. if (EnableAlarm)
  807. {
  808. EV.PostAlarmLog($"{Module}", "rf on time value larger than setting interval days");
  809. }
  810. }
  811. }
  812. if(_rfOnTrigger.T)
  813. StatsDataManager.Instance.Increase($"{Module}.BiasRfOnTime", $"{Module} RfOnTime", DaysFromLastPM, TotalDays);
  814. if (!_rfOnTrigger.CLK)
  815. {
  816. ForwardPower = 0;
  817. ReflectPower = 0;
  818. }
  819. base.Monitor();
  820. }
  821. public override void Terminate()
  822. {
  823. _socket?.Dispose();
  824. }
  825. public override void SetPower(float val)
  826. {
  827. var power = !_scEnableCalibration.BoolValue ? val : CalibrationData(val, true);
  828. SendCmd(Mode.Write, CometRFCommand.PowerSetPoint, (int)power * 1000);
  829. }
  830. public override bool SetPowerOnOff(bool on, out string str)
  831. {
  832. str = "";
  833. _isPowerOn = on;
  834. SendCmd(Mode.Write, CometRFCommand.Command, on ? 1 : 0);
  835. return true;
  836. }
  837. public override bool SetMatchingAutoMode(bool on, out string str)
  838. {
  839. str = "";
  840. SendCmd(Mode.Write, CometRFCommand.MatchingAutoMode, on ? 1 : 0);
  841. return true;
  842. }
  843. public override bool SetMatchPosition(double c1, double c2, out string reason)
  844. {
  845. reason = string.Empty;
  846. if (c1 <= 100 && c1 >= 0)
  847. {
  848. double c1FilterValue = c1;
  849. SendCmd(Mode.Write, CometRFCommand.CLoadRefPosition, (int)(c1 * 10));
  850. reason = string.Format("Match position C1 set to {0} ", c1FilterValue);
  851. }
  852. if (c2 <= 100 && c2 >= 0)
  853. {
  854. double c2FilterValue = c2;
  855. SendCmd(Mode.Write, CometRFCommand.CTuneRefPosition, (int)(c2 * 10));
  856. reason += string.Format("Match position C2 set to {0}", c2FilterValue);
  857. }
  858. return true;
  859. }
  860. private bool SendCmd(Mode mode, int command, int argument)
  861. {
  862. if (command == CometRFCommand.Command && argument == 0)
  863. {
  864. EV.PostInfoLog(Module, $"Bias Generator send RF off");
  865. }
  866. else if (command == CometRFCommand.Command && argument == 1)
  867. {
  868. EV.PostInfoLog(Module, $"Bias Generator send RF on");
  869. }
  870. if (command == CometRFCommand.PowerSetPoint)
  871. {
  872. EV.PostInfoLog(Module, $"Bias Generator send [{argument / 1000}W]");
  873. }
  874. _currentCommandNumber = command;
  875. _sendData = BuildMessage(mode, command, argument);
  876. _socket?.Write(_sendData);
  877. Thread.Sleep(100);
  878. return true;
  879. }
  880. protected byte[] BuildMessage(Mode mode, int commandNumber, int argumentNumber)
  881. {
  882. List<byte> buffer = new List<byte>();
  883. TransactionNumber++;
  884. if (TransactionNumber > 65535) TransactionNumber = 0;
  885. buffer.Add(BitConverter.GetBytes(TransactionNumber)[1]);
  886. buffer.Add(BitConverter.GetBytes(TransactionNumber)[0]);
  887. buffer.Add(ProtocolIdentifierHighByte);
  888. buffer.Add(ProtocolIdentifierLowByte);
  889. if (mode == Mode.Read)
  890. {
  891. buffer.Add(0x00);
  892. buffer.Add(0x06);
  893. }
  894. else if (mode == Mode.Write)
  895. {
  896. buffer.Add(0x00);
  897. buffer.Add(0x08);
  898. }
  899. buffer.Add(_address);
  900. if (mode == Mode.Read)
  901. {
  902. buffer.Add(_read);
  903. }
  904. else if (mode == Mode.Write)
  905. {
  906. buffer.Add(_write);
  907. }
  908. buffer.Add(BitConverter.GetBytes(commandNumber)[1]);
  909. buffer.Add(BitConverter.GetBytes(commandNumber)[0]);
  910. if (mode == Mode.Read)
  911. {
  912. buffer.Add(0x00);
  913. buffer.Add(0x01);
  914. }
  915. else if (mode == Mode.Write)
  916. {
  917. buffer.Add(BitConverter.GetBytes(argumentNumber)[3]);
  918. buffer.Add(BitConverter.GetBytes(argumentNumber)[2]);
  919. buffer.Add(BitConverter.GetBytes(argumentNumber)[1]);
  920. buffer.Add(BitConverter.GetBytes(argumentNumber)[0]);
  921. }
  922. //byte checkSum = 0;
  923. //for (int i = 0; i < buffer.Count; i++)
  924. //{
  925. // checkSum += buffer[i];
  926. //}
  927. //buffer.Add(checkSum);
  928. return buffer.ToArray();
  929. }
  930. public int BytesToInt(byte[] src, int offset)
  931. {
  932. int value;
  933. value = (int)((src[offset + 3] & 0xFF)
  934. | ((src[offset + 2] & 0xFF) << 8)
  935. | ((src[offset + 1] & 0xFF) << 16)
  936. | ((src[offset] & 0xFF) << 24));
  937. return value;
  938. }
  939. public byte[] IntToBytes(int value)
  940. {
  941. byte[] src = new byte[4];
  942. src[0] = (byte)((value >> 24) & 0xFF);
  943. src[1] = (byte)((value >> 16) & 0xFF);
  944. src[2] = (byte)((value >> 8) & 0xFF);
  945. src[3] = (byte)(value & 0xFF);
  946. return src;
  947. }
  948. public override void Reset()
  949. {
  950. _rfOnTrigger.RST = true;
  951. _ErrTrigger.RST = true;
  952. _trigPMNeeded.RST = true;
  953. SendCmd(Mode.Write, CometRFCommand.Command, 9);
  954. Status = GeneratorStatus.OFF;
  955. StatusMatch = GeneratorStatus.OFF;
  956. _exceuteErr = false;
  957. if (_commErr)
  958. {
  959. Connect();
  960. }
  961. }
  962. ~CometRF()
  963. {
  964. _socket?.Dispose();
  965. }
  966. }
  967. }