MksRfPower.cs 20 KB

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