SerenRfPower.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using Aitex.Core.Common.DeviceData;
  5. using Aitex.Core.RT.DataCenter;
  6. using Aitex.Core.RT.Event;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.RT.OperationCenter;
  9. using Aitex.Core.RT.SCCore;
  10. using Aitex.Core.Util;
  11. using MECF.Framework.Common.Communications;
  12. using MECF.Framework.Common.Device.Bases;
  13. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.Serens
  14. {
  15. public class SerenRfPower : RfPowerBase
  16. {
  17. public override bool IsError { get; set; }
  18. public bool Connect()
  19. {
  20. return _connection.Connect();
  21. }
  22. public bool Disconnect()
  23. {
  24. return _connection.Disconnect();
  25. }
  26. public override bool IsConnected => Connection != null && Connection.IsConnected && !_connection.IsCommunicationError;
  27. public override bool IsPowerOn
  28. {
  29. get { return _isOn; }
  30. }
  31. public override EnumRfPowerRegulationMode RegulationMode
  32. {
  33. get { return _regulationMode; }
  34. }
  35. public override float ForwardPower
  36. {
  37. get { return _forwardPower; }
  38. }
  39. public override float ReflectPower
  40. {
  41. get { return _reflectPower; }
  42. }
  43. public override float Frequency { get; set; }
  44. public override float PulsingFrequency { get; set; }
  45. public override float PulsingDutyCycle { get; set; }
  46. public string ControlSource { get; set; }
  47. public string RegulationSource { get; set; }
  48. public string SetpointSource { get; set; }
  49. public override AITRfPowerData DeviceData
  50. {
  51. get
  52. {
  53. AITRfPowerData data = new AITRfPowerData()
  54. {
  55. DeviceName = Name,
  56. Module = Module,
  57. DeviceSchematicId = DeviceID,
  58. DisplayName = Display,
  59. UnitPower = "W",
  60. ForwardPower = _originalPowerSetPoint > 0 ? CalibrationData(ForwardPower, false) : 0,
  61. ReflectPower = _originalPowerSetPoint > 0 ? ReflectPower : 0,
  62. PowerSetPoint = _originalPowerSetPoint,
  63. RegulationMode = RegulationMode,
  64. ScalePower = PowerRange,
  65. IsRfOn = IsPowerOn,
  66. IsRfAlarm = IsError,
  67. IsInterlockOk = !_isInterlockNotOK,
  68. Frequency = Frequency,
  69. PulsingFrequency = PulsingFrequency,
  70. PulsingDutyCycle = PulsingDutyCycle,
  71. };
  72. return data;
  73. }
  74. }
  75. public SerenRfPowerConnection Connection
  76. {
  77. get { return _connection; }
  78. }
  79. private SerenRfPowerConnection _connection;
  80. private byte _deviceAddress;
  81. private float _powerSetPoint;
  82. private float _reflectPower;
  83. private bool _isOn;
  84. private float _forwardPower;
  85. private EnumRfPowerRegulationMode _regulationMode;
  86. private EnumRfPowerCommunicationMode _commMode;
  87. private bool _isInterlockNotOK;
  88. private RD_TRIG _trigRfOnOff = new RD_TRIG();
  89. private R_TRIG _trigError = new R_TRIG();
  90. private R_TRIG _trigWarningMessage = new R_TRIG();
  91. private R_TRIG _trigCommunicationError = new R_TRIG();
  92. private R_TRIG _trigRetryConnect = new R_TRIG();
  93. private PeriodicJob _thread;
  94. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  95. private object _locker = new object();
  96. private bool _enableLog = true;
  97. private bool _isHaloInstalled;
  98. private string _scRoot;
  99. public SerenRfPower(string module, string name, string scRoot) : base(module, name)
  100. {
  101. _scRoot = scRoot;
  102. }
  103. public bool Initialize(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  104. {
  105. base.Initialize();
  106. _enableLog = true;
  107. _connection = new SerenRfPowerConnection(portName, baudRate, dataBits, parity, stopBits);
  108. _connection.EnableLog(_enableLog);
  109. if (_connection.Connect())
  110. {
  111. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  112. }
  113. _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  114. return true;
  115. }
  116. public override bool Initialize()
  117. {
  118. base.Initialize();
  119. string portName = SC.GetStringValue($"{_scRoot}.Address");
  120. int bautRate = SC.GetValue<int>($"{_scRoot}.BaudRate");
  121. int dataBits = SC.GetValue<int>($"{_scRoot}.DataBits");
  122. Enum.TryParse(SC.GetStringValue($"{_scRoot}.Parity"), out Parity parity);
  123. Enum.TryParse(SC.GetStringValue($"{_scRoot}.StopBits"), out StopBits stopBits);
  124. _deviceAddress = (byte)SC.GetValue<int>($"{_scRoot}.Address");
  125. _enableLog = SC.GetValue<bool>($"{_scRoot}.EnableLogMessage");
  126. _scPowerScale = SC.GetConfigItem($"{_scRoot}.PowerScale");
  127. _connection = new SerenRfPowerConnection(portName, bautRate, dataBits, parity, stopBits);
  128. _connection.EnableLog(_enableLog);
  129. if (_connection.Connect())
  130. {
  131. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  132. }
  133. _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  134. OP.Subscribe($"{Module}.{Name}.WaveModulation", (out string reason, int time, object[] args) =>
  135. {
  136. reason = "";
  137. //if (!Enum.TryParse((string)args[0], out EnumRpsControlMode mode))
  138. //{
  139. // EV.PostAlarmLog(Module, $"{Module}.{Name} Can not mode, {args[0]} is not a valid mode value");
  140. // return false;
  141. //}
  142. if (!PerformWaveModulation(out reason, time, ""))
  143. {
  144. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  145. return false;
  146. }
  147. EV.PostInfoLog(Module, $"{Module}.{Name} set mode to {args[0]}");
  148. return true;
  149. });
  150. OP.Subscribe($"{Module}.{Name}.PowerMode", (out string reason, int time, object[] args) =>
  151. {
  152. reason = "";
  153. string mode = (string)args[0];
  154. if (!PerformPowerMode(out reason, time, mode))
  155. {
  156. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  157. return false;
  158. }
  159. EV.PostInfoLog(Module, $"{Module}.{Name} set mode to {mode}");
  160. return true;
  161. });
  162. OP.Subscribe($"{Module}.{Name}.SetHighPower", (out string reason, int time, object[] args) =>
  163. {
  164. reason = "";
  165. float value = Convert.ToSingle((string)args[0]);
  166. if (!PerformSetHighPower(out reason, time, value))
  167. {
  168. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  169. return false;
  170. }
  171. EV.PostInfoLog(Module, $"{Module}.{Name} set high power to {value}");
  172. return true;
  173. });
  174. OP.Subscribe($"{Module}.{Name}.SetLowPower", (out string reason, int time, object[] args) =>
  175. {
  176. reason = "";
  177. float value = Convert.ToSingle((string)args[0]);
  178. if (!PerformSetLowPower(out reason, time, value))
  179. {
  180. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  181. return false;
  182. }
  183. EV.PostInfoLog(Module, $"{Module}.{Name} set low power to {value}");
  184. return true;
  185. });
  186. OP.Subscribe($"{Module}.{Name}.PulseFreq", (out string reason, int time, object[] args) =>
  187. {
  188. reason = "";
  189. float value = Convert.ToSingle((string)args[0]);
  190. if (!PerformPulseFreq(out reason, time, value))
  191. {
  192. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  193. return false;
  194. }
  195. EV.PostInfoLog(Module, $"{Module}.{Name} set pulse frequency to {value}");
  196. return true;
  197. });
  198. OP.Subscribe($"{Module}.{Name}.PulseDutyCycle", (out string reason, int time, object[] args) =>
  199. {
  200. reason = "";
  201. float value = Convert.ToSingle((string)args[0]);
  202. if (!PerformPulseDutyCycle(out reason, time, value))
  203. {
  204. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  205. return false;
  206. }
  207. EV.PostInfoLog(Module, $"{Module}.{Name} set pulse duty cycle to {value}");
  208. return true;
  209. });
  210. OP.Subscribe($"{Module}.{Name}.RampMode", (out string reason, int time, object[] args) =>
  211. {
  212. reason = "";
  213. if (!PerformRampMode(out reason, time, ""))
  214. {
  215. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  216. return false;
  217. }
  218. EV.PostInfoLog(Module, $"{Module}.{Name} set ramp mode to ");
  219. return true;
  220. });
  221. OP.Subscribe($"{Module}.{Name}.RampUp", (out string reason, int time, object[] args) =>
  222. {
  223. reason = "";
  224. if (!PerformRampUp(out reason, time, ""))
  225. {
  226. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  227. return false;
  228. }
  229. EV.PostInfoLog(Module, $"{Module}.{Name} set ramp up");
  230. return true;
  231. });
  232. OP.Subscribe($"{Module}.{Name}.RampDown", (out string reason, int time, object[] args) =>
  233. {
  234. reason = "";
  235. if (!PerformRampDown(out reason, time, ""))
  236. {
  237. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  238. return false;
  239. }
  240. EV.PostInfoLog(Module, $"{Module}.{Name} set ramp down");
  241. return true;
  242. });
  243. OP.Subscribe($"{Module}.{Name}.WarningPower", (out string reason, int time, object[] args) =>
  244. {
  245. reason = "";
  246. float value = Convert.ToSingle((string)args[0]);
  247. if (!PerformWarningPower(out reason, time, value))
  248. {
  249. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  250. return false;
  251. }
  252. EV.PostInfoLog(Module, $"{Module}.{Name} set warning power to {value}");
  253. return true;
  254. });
  255. OP.Subscribe($"{Module}.{Name}.AlarmPower", (out string reason, int time, object[] args) =>
  256. {
  257. reason = "";
  258. float value = Convert.ToSingle((string)args[0]);
  259. if (!PerformAlarmPower(out reason, time, value))
  260. {
  261. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  262. return false;
  263. }
  264. EV.PostInfoLog(Module, $"{Module}.{Name} set alarm power to {value}");
  265. return true;
  266. });
  267. DATA.Subscribe($"{Module}.{Name}.IsConnected", () => IsConnected);
  268. DATA.Subscribe($"{Module}.{Name}.Address", () => SC.GetStringValue($"{_scRoot}.Address"));
  269. OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) =>
  270. {
  271. Disconnect();
  272. Reset();
  273. Connect();
  274. return true;
  275. });
  276. return true;
  277. }
  278. private bool PerformWaveModulation(out string reason, int time, string mode)
  279. {
  280. reason = string.Empty;
  281. return true;
  282. }
  283. private bool PerformAlarmPower(out string reason, int time, float value)
  284. {
  285. reason = string.Empty;
  286. return true;
  287. }
  288. private bool PerformWarningPower(out string reason, int time, float value)
  289. {
  290. reason = string.Empty;
  291. return true;
  292. }
  293. private bool PerformPowerMode(out string reason, int time, string mode)
  294. {
  295. reason = string.Empty;
  296. return true;
  297. }
  298. private bool PerformSetHighPower(out string reason, int time, float value)
  299. {
  300. reason = string.Empty;
  301. return true;
  302. }
  303. private bool PerformSetLowPower(out string reason, int time, float value)
  304. {
  305. reason = string.Empty;
  306. return true;
  307. }
  308. private bool PerformPulseFreq(out string reason, int time, float value)
  309. {
  310. reason = string.Empty;
  311. return true;
  312. }
  313. private bool PerformPulseDutyCycle(out string reason, int time, float value)
  314. {
  315. reason = string.Empty;
  316. return true;
  317. }
  318. private bool PerformRampMode(out string reason, int time, string v)
  319. {
  320. reason = string.Empty;
  321. return true;
  322. }
  323. private bool PerformRampUp(out string reason, int time, string mode)
  324. {
  325. reason = string.Empty;
  326. return true;
  327. }
  328. private bool PerformRampDown(out string reason, int time, string mode)
  329. {
  330. reason = string.Empty;
  331. return true;
  332. }
  333. private bool OnTimer()
  334. {
  335. try
  336. {
  337. _connection.MonitorTimeout();
  338. if (!_connection.IsConnected || _connection.IsCommunicationError)
  339. {
  340. lock (_locker)
  341. {
  342. _lstHandler.Clear();
  343. }
  344. _trigRetryConnect.CLK = !_connection.IsConnected;
  345. if (_trigRetryConnect.Q)
  346. {
  347. _connection.SetPortAddress(SC.GetStringValue($"{ScBasePath}.{Name}.Address"));
  348. if (!_connection.Connect())
  349. {
  350. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  351. }
  352. else
  353. {
  354. }
  355. }
  356. return true;
  357. }
  358. HandlerBase handler = null;
  359. if (!_connection.IsBusy)
  360. {
  361. lock (_locker)
  362. {
  363. if (_lstHandler.Count == 0)
  364. {
  365. _lstHandler.AddLast(new SerenRfPowerQueryStatusHandler(this));
  366. }
  367. if (_lstHandler.Count > 0)
  368. {
  369. handler = _lstHandler.First.Value;
  370. _lstHandler.RemoveFirst();
  371. }
  372. }
  373. if (handler != null)
  374. {
  375. _connection.Execute(handler);
  376. }
  377. }
  378. }
  379. catch (Exception ex)
  380. {
  381. LOG.Write(ex);
  382. }
  383. return true;
  384. }
  385. public override void Monitor()
  386. {
  387. try
  388. {
  389. _connection.EnableLog(_enableLog);
  390. _trigRfOnOff.CLK = _isOn;
  391. if (_trigRfOnOff.R)
  392. {
  393. EV.PostInfoLog(Module, $"{Module}.{Name} is on");
  394. }
  395. if (_trigRfOnOff.T)
  396. {
  397. EV.PostInfoLog(Module, $"{Module}.{Name} is off");
  398. }
  399. //_trigError.CLK = IsError;
  400. //if (_trigError.Q)
  401. //{
  402. // EV.PostAlarmLog(Module, $"{Module}.{Name} is error, error code {_errorCode:D3}");
  403. //}
  404. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  405. if (_trigCommunicationError.Q)
  406. {
  407. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  408. }
  409. base.Monitor();
  410. }
  411. catch (Exception ex)
  412. {
  413. LOG.Write(ex);
  414. }
  415. }
  416. public override void Reset()
  417. {
  418. _trigError.RST = true;
  419. _trigWarningMessage.RST = true;
  420. _connection.SetCommunicationError(false, "");
  421. _trigCommunicationError.RST = true;
  422. _enableLog = SC.GetValue<bool>($"{_scRoot}.EnableLogMessage");
  423. _trigRetryConnect.RST = true;
  424. base.Reset();
  425. }
  426. public override void CheckTolerance()
  427. {
  428. if (!EnableAlarm || PowerSetPoint == 0 || (RecipeIgnoreTime > 0 && _recipeIgnoreTimer.GetElapseTime() < RecipeIgnoreTime * 1000))
  429. return;
  430. _toleranceAlarmChecker.Monitor(ForwardPower, (PowerSetPoint * (1 - AlarmRange / 100)), (PowerSetPoint * (1 + AlarmRange / 100)), AlarmTime);
  431. _toleranceWarningChecker.Monitor(ForwardPower, (PowerSetPoint * (1 - WarningRange / 100)), (PowerSetPoint * (1 + WarningRange / 100)), WarningTime);
  432. if (_PrThreshold > 0)
  433. _prThresholdChecker.Monitor(ReflectPower, 0, _PrThreshold, PrThresholdMonitorTime);
  434. //if (_toleranceAlarmChecker.Trig)
  435. //{
  436. // EV.PostMessage(Module, EventEnum.ToleranceAlarm, Module, Display,
  437. // String.Format("Out of range in {0} seconds", AlarmTime.ToString("0")));
  438. //}
  439. //else if (_toleranceWarningChecker.Trig)
  440. //{
  441. // EV.PostMessage(Module, EventEnum.ToleranceWarning, Module, Display,
  442. // String.Format("Out of range in {0} seconds", WarningTime.ToString("0")));
  443. //}
  444. }
  445. public override void SetPower(float power)
  446. {
  447. PowerSetPoint = power;
  448. lock (_locker)
  449. {
  450. _lstHandler.AddLast(new SerenRfPowerSetPowerHandler(this, (int)PowerSetPoint));
  451. }
  452. }
  453. public bool SetEchoOnOff(bool isOn, out string reason)
  454. {
  455. reason = "";
  456. lock (_locker)
  457. {
  458. _lstHandler.AddLast(new SerenRfPowerSetEchoHandler(this, isOn));
  459. }
  460. return true;
  461. }
  462. public override bool SetPowerOnOff(bool isOn, out string reason)
  463. {
  464. reason = "";
  465. lock (_locker)
  466. {
  467. _lstHandler.AddLast(new SerenRfPowerSwitchOnOffHandler(this, isOn));
  468. }
  469. return true;
  470. }
  471. public override void SetRegulationMode(EnumRfPowerRegulationMode mode)
  472. {
  473. lock (_locker)
  474. {
  475. _lstHandler.AddLast(new SerenRfPowerSetRegulationModeHandler(this, _deviceAddress, mode));
  476. }
  477. }
  478. public void QueryStatus()
  479. {
  480. lock (_locker)
  481. {
  482. _lstHandler.AddLast(new SerenRfPowerQueryStatusHandler(this));
  483. }
  484. }
  485. public void SetSerialControl()
  486. {
  487. lock (_locker)
  488. {
  489. _lstHandler.AddLast(new SerenRfPowerSetSerialModeHandler(this));
  490. }
  491. }
  492. internal void NoteError(string reason)
  493. {
  494. _trigWarningMessage.CLK = true;
  495. if (_trigWarningMessage.Q)
  496. {
  497. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  498. }
  499. }
  500. internal void NoteReflectPower(float reflect)
  501. {
  502. _reflectPower = reflect;
  503. }
  504. internal void NoteCommMode(EnumRfPowerCommunicationMode mode)
  505. {
  506. _commMode = mode;
  507. }
  508. internal void NoteStatus(byte[] data)
  509. {
  510. _isOn = (data[0] & 0x40) == 0x40;
  511. }
  512. internal void NoteStatus(bool isOn, bool isAlarm, bool isInterlockNotOK)
  513. {
  514. _isOn = isOn;
  515. _isInterlockNotOK = isInterlockNotOK;
  516. IsError = isAlarm;
  517. }
  518. internal void NoteRegulationModeSetPoint(EnumRfPowerRegulationMode regMode)
  519. {
  520. _regulationMode = regMode;
  521. }
  522. internal void NotePowerSetPoint(int power)
  523. {
  524. _powerSetPoint = power;
  525. }
  526. internal void NoteForwardPower(int power)
  527. {
  528. _forwardPower = power;
  529. }
  530. internal void NoteHaloInstalled(bool isInstalled)
  531. {
  532. _isHaloInstalled = isInstalled;
  533. }
  534. }
  535. public enum EnumRfPowerCommunicationMode
  536. {
  537. Undefined,
  538. Host = 2,
  539. UserPort = 4, //Analog
  540. Diagnostic = 8,
  541. DeviceNet = 16,
  542. EtherCat32 = 32,
  543. }
  544. }