CometRF.cs 40 KB

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