CometRF.cs 41 KB

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