AeRfPower.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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.AE
  14. {
  15. public class AeRfPower : 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 override float ForwardPower
  27. {
  28. get { return _forwardPower; }
  29. }
  30. public override float ReflectPower
  31. {
  32. get { return _reflectPower; }
  33. }
  34. public override float PowerSetPoint
  35. {
  36. get { return _powerSetPoint; }
  37. }
  38. public override float Frequency { get; set; }
  39. public override float PulsingFrequency { get; set; }
  40. public override float PulsingDutyCycle { get; set; }
  41. public override AITRfPowerData DeviceData
  42. {
  43. get
  44. {
  45. AITRfPowerData data = new AITRfPowerData()
  46. {
  47. DeviceName = Name,
  48. DeviceSchematicId = DeviceID,
  49. DisplayName = Display,
  50. ForwardPower = ForwardPower,
  51. ReflectPower = ReflectPower,
  52. PowerSetPoint = PowerSetPoint,
  53. RegulationMode = RegulationMode,
  54. IsRfOn = IsPowerOn,
  55. Frequency = Frequency,
  56. PulsingFrequency = PulsingFrequency,
  57. PulsingDutyCycle = PulsingDutyCycle,
  58. };
  59. return data;
  60. }
  61. }
  62. public AeRfPowerConnection Connection
  63. {
  64. get { return _connection; }
  65. }
  66. private AeRfPowerConnection _connection;
  67. private byte _deviceAddress ;
  68. private float _powerSetPoint;
  69. private float _reflectPower;
  70. private bool _isOn;
  71. private float _forwardPower;
  72. private EnumRfPowerRegulationMode _regulationMode;
  73. private EnumRfPowerCommunicationMode _commMode;
  74. private RD_TRIG _trigRfOnOff = new RD_TRIG();
  75. private R_TRIG _trigError = new R_TRIG();
  76. private R_TRIG _trigWarningMessage = new R_TRIG();
  77. private R_TRIG _trigCommunicationError = new R_TRIG();
  78. private R_TRIG _trigRetryConnect = new R_TRIG();
  79. private PeriodicJob _thread;
  80. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  81. private object _locker = new object();
  82. private bool _enableLog = true;
  83. private bool _isHaloInstalled;
  84. public AeRfPower(string module, string name) : base(module, name)
  85. {
  86. }
  87. public override bool Initialize()
  88. {
  89. base.Initialize();
  90. string portName = SC.GetStringValue($"{ScBasePath}.{Name}.Address");
  91. int bautRate = SC.GetValue<int>($"{ScBasePath}.{Name}.BaudRate");
  92. int dataBits = SC.GetValue<int>($"{ScBasePath}.{Name}.DataBits");
  93. Enum.TryParse(SC.GetStringValue($"{ScBasePath}.{Name}.Parity"), out Parity parity);
  94. Enum.TryParse(SC.GetStringValue($"{ScBasePath}.{Name}.StopBits"), out StopBits stopBits);
  95. _deviceAddress = (byte)SC.GetValue<int>($"{ScBasePath}.{Name}.DeviceAddress");
  96. _enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  97. _connection = new AeRfPowerConnection(portName, bautRate, dataBits, parity, stopBits);
  98. _connection.EnableLog(_enableLog);
  99. if (_connection.Connect())
  100. {
  101. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  102. }
  103. _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  104. OP.Subscribe($"{Module}.{Name}.WaveModulation", (out string reason, int time, object[] args) =>
  105. {
  106. reason = "";
  107. //if (!Enum.TryParse((string)args[0], out EnumRpsControlMode mode))
  108. //{
  109. // EV.PostAlarmLog(Module, $"{Module}.{Name} Can not mode, {args[0]} is not a valid mode value");
  110. // return false;
  111. //}
  112. if (!PerformWaveModulation(out reason, time, ""))
  113. {
  114. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  115. return false;
  116. }
  117. EV.PostInfoLog(Module, $"{Module}.{Name} set mode to {args[0]}");
  118. return true;
  119. });
  120. OP.Subscribe($"{Module}.{Name}.PowerMode", (out string reason, int time, object[] args) =>
  121. {
  122. reason = "";
  123. string mode = (string)args[0];
  124. if (!PerformPowerMode(out reason, time, mode))
  125. {
  126. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  127. return false;
  128. }
  129. EV.PostInfoLog(Module, $"{Module}.{Name} set mode to {mode}");
  130. return true;
  131. });
  132. OP.Subscribe($"{Module}.{Name}.SetHighPower", (out string reason, int time, object[] args) =>
  133. {
  134. reason = "";
  135. float value = Convert.ToSingle((string)args[0]);
  136. if (!PerformSetHighPower(out reason, time, value))
  137. {
  138. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  139. return false;
  140. }
  141. EV.PostInfoLog(Module, $"{Module}.{Name} set high power to {value}");
  142. return true;
  143. });
  144. OP.Subscribe($"{Module}.{Name}.SetLowPower", (out string reason, int time, object[] args) =>
  145. {
  146. reason = "";
  147. float value = Convert.ToSingle((string)args[0]);
  148. if (!PerformSetLowPower(out reason, time, value))
  149. {
  150. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  151. return false;
  152. }
  153. EV.PostInfoLog(Module, $"{Module}.{Name} set low power to {value}");
  154. return true;
  155. });
  156. OP.Subscribe($"{Module}.{Name}.PulseFreq", (out string reason, int time, object[] args) =>
  157. {
  158. reason = "";
  159. float value = Convert.ToSingle((string)args[0]);
  160. if (!PerformPulseFreq(out reason, time, value))
  161. {
  162. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  163. return false;
  164. }
  165. EV.PostInfoLog(Module, $"{Module}.{Name} set pulse frequency to {value}");
  166. return true;
  167. });
  168. OP.Subscribe($"{Module}.{Name}.PulseDutyCycle", (out string reason, int time, object[] args) =>
  169. {
  170. reason = "";
  171. float value = Convert.ToSingle((string)args[0]);
  172. if (!PerformPulseDutyCycle(out reason, time, value))
  173. {
  174. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  175. return false;
  176. }
  177. EV.PostInfoLog(Module, $"{Module}.{Name} set pulse duty cycle to {value}");
  178. return true;
  179. });
  180. OP.Subscribe($"{Module}.{Name}.RampMode", (out string reason, int time, object[] args) =>
  181. {
  182. reason = "";
  183. if (!PerformRampMode(out reason, time, ""))
  184. {
  185. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  186. return false;
  187. }
  188. EV.PostInfoLog(Module, $"{Module}.{Name} set ramp mode to ");
  189. return true;
  190. });
  191. OP.Subscribe($"{Module}.{Name}.RampUp", (out string reason, int time, object[] args) =>
  192. {
  193. reason = "";
  194. if (!PerformRampUp(out reason, time, ""))
  195. {
  196. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  197. return false;
  198. }
  199. EV.PostInfoLog(Module, $"{Module}.{Name} set ramp up");
  200. return true;
  201. });
  202. OP.Subscribe($"{Module}.{Name}.RampDown", (out string reason, int time, object[] args) =>
  203. {
  204. reason = "";
  205. if (!PerformRampDown(out reason, time, ""))
  206. {
  207. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  208. return false;
  209. }
  210. EV.PostInfoLog(Module, $"{Module}.{Name} set ramp down");
  211. return true;
  212. });
  213. OP.Subscribe($"{Module}.{Name}.WarningPower", (out string reason, int time, object[] args) =>
  214. {
  215. reason = "";
  216. float value = Convert.ToSingle((string)args[0]);
  217. if (!PerformWarningPower(out reason, time, value))
  218. {
  219. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  220. return false;
  221. }
  222. EV.PostInfoLog(Module, $"{Module}.{Name} set warning power to {value}");
  223. return true;
  224. });
  225. OP.Subscribe($"{Module}.{Name}.AlarmPower", (out string reason, int time, object[] args) =>
  226. {
  227. reason = "";
  228. float value = Convert.ToSingle((string)args[0]);
  229. if (!PerformAlarmPower(out reason, time, value))
  230. {
  231. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set mode, {reason}");
  232. return false;
  233. }
  234. EV.PostInfoLog(Module, $"{Module}.{Name} set alarm power to {value}");
  235. return true;
  236. });
  237. return true;
  238. }
  239. private bool PerformWaveModulation(out string reason, int time, string mode)
  240. {
  241. reason = string.Empty;
  242. return true;
  243. }
  244. private bool PerformAlarmPower(out string reason, int time, float value)
  245. {
  246. reason = string.Empty;
  247. return true;
  248. }
  249. private bool PerformWarningPower(out string reason, int time, float value)
  250. {
  251. reason = string.Empty;
  252. return true;
  253. }
  254. private bool PerformPowerMode(out string reason, int time, string mode)
  255. {
  256. reason = string.Empty;
  257. return true;
  258. }
  259. private bool PerformSetHighPower(out string reason, int time, float value)
  260. {
  261. reason = string.Empty;
  262. return true;
  263. }
  264. private bool PerformSetLowPower(out string reason, int time, float value)
  265. {
  266. reason = string.Empty;
  267. return true;
  268. }
  269. private bool PerformPulseFreq(out string reason, int time, float value)
  270. {
  271. reason = string.Empty;
  272. return true;
  273. }
  274. private bool PerformPulseDutyCycle(out string reason, int time, float value)
  275. {
  276. reason = string.Empty;
  277. return true;
  278. }
  279. private bool PerformRampMode(out string reason, int time, string v)
  280. {
  281. reason = string.Empty;
  282. return true;
  283. }
  284. private bool PerformRampUp(out string reason, int time, string mode)
  285. {
  286. reason = string.Empty;
  287. return true;
  288. }
  289. private bool PerformRampDown(out string reason, int time, string mode)
  290. {
  291. reason = string.Empty;
  292. return true;
  293. }
  294. private bool OnTimer()
  295. {
  296. try
  297. {
  298. _connection.MonitorTimeout();
  299. if (!_connection.IsConnected || _connection.IsCommunicationError)
  300. {
  301. lock (_locker)
  302. {
  303. _lstHandler.Clear();
  304. }
  305. _trigRetryConnect.CLK = !_connection.IsConnected;
  306. if (_trigRetryConnect.Q)
  307. {
  308. _connection.SetPortAddress(SC.GetStringValue($"{ScBasePath}.{Name}.Address"));
  309. if (!_connection.Connect())
  310. {
  311. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  312. }
  313. else
  314. {
  315. _lstHandler.AddLast(new AeRfPowerQueryPinHandler(this, _deviceAddress));
  316. _lstHandler.AddLast(new AeRfPowerSetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
  317. }
  318. }
  319. return true;
  320. }
  321. HandlerBase handler = null;
  322. if (!_connection.IsBusy)
  323. {
  324. lock (_locker)
  325. {
  326. if (_lstHandler.Count == 0)
  327. {
  328. _lstHandler.AddLast(new AeRfPowerQueryCommModeHandler(this, _deviceAddress));
  329. _lstHandler.AddLast(new AeRfPowerQueryForwardPowerHandler(this, _deviceAddress));
  330. _lstHandler.AddLast(new AeRfPowerQueryReflectPowerHandler(this, _deviceAddress));
  331. _lstHandler.AddLast(new AeRfPowerQueryStatusHandler(this, _deviceAddress));
  332. _lstHandler.AddLast(new AeRfPowerQuerySetPointHandler(this, _deviceAddress));
  333. }
  334. if (_lstHandler.Count > 0)
  335. {
  336. handler = _lstHandler.First.Value;
  337. _lstHandler.RemoveFirst();
  338. }
  339. }
  340. if (handler != null)
  341. {
  342. _connection.Execute(handler);
  343. }
  344. }
  345. }
  346. catch (Exception ex)
  347. {
  348. LOG.Write(ex);
  349. }
  350. return true;
  351. }
  352. public override void Monitor()
  353. {
  354. try
  355. {
  356. _connection.EnableLog(_enableLog);
  357. _trigRfOnOff.CLK = _isOn;
  358. if (_trigRfOnOff.R)
  359. {
  360. EV.PostInfoLog(Module, $"{Module}.{Name} is on");
  361. }
  362. if (_trigRfOnOff.T)
  363. {
  364. EV.PostInfoLog(Module, $"{Module}.{Name} is off");
  365. }
  366. //_trigError.CLK = IsError;
  367. //if (_trigError.Q)
  368. //{
  369. // EV.PostAlarmLog(Module, $"{Module}.{Name} is error, error code {_errorCode:D3}");
  370. //}
  371. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  372. if (_trigCommunicationError.Q)
  373. {
  374. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  375. }
  376. }
  377. catch (Exception ex)
  378. {
  379. LOG.Write(ex);
  380. }
  381. }
  382. public override void Reset()
  383. {
  384. _trigError.RST = true;
  385. _trigWarningMessage.RST = true;
  386. _connection.SetCommunicationError(false, "");
  387. _trigCommunicationError.RST = true;
  388. _enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  389. _trigRetryConnect.RST = true;
  390. base.Reset();
  391. }
  392. public override void SetPower(float power)
  393. {
  394. lock (_locker)
  395. {
  396. _lstHandler.AddLast(new AeRfPowerSetPowerHandler(this, _deviceAddress, _isHaloInstalled ? (int)(power*10) : (int)power));
  397. }
  398. }
  399. public override bool SetPowerOnOff(bool isOn, out string reason)
  400. {
  401. reason = "";
  402. lock (_locker)
  403. {
  404. _lstHandler.AddLast(new AeRfPowerSwitchOnOffHandler(this, _deviceAddress, isOn));
  405. }
  406. return true;
  407. }
  408. public override void SetRegulationMode(EnumRfPowerRegulationMode mode)
  409. {
  410. lock (_locker)
  411. {
  412. _lstHandler.AddLast(new AeRfPowerSetRegulationModeHandler(this, _deviceAddress, mode));
  413. }
  414. }
  415. internal void NoteError(string reason)
  416. {
  417. _trigWarningMessage.CLK = true;
  418. if (_trigWarningMessage.Q)
  419. {
  420. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  421. }
  422. }
  423. internal void NoteReflectPower(float reflect)
  424. {
  425. _reflectPower = reflect;
  426. }
  427. internal void NoteCommMode(EnumRfPowerCommunicationMode mode)
  428. {
  429. _commMode = mode;
  430. }
  431. internal void NoteStatus(byte[] data)
  432. {
  433. _isOn = (data[0] & 0x40) == 0x40;
  434. }
  435. internal void NoteRegulationModeSetPoint(EnumRfPowerRegulationMode regMode)
  436. {
  437. _regulationMode = regMode;
  438. }
  439. internal void NotePowerSetPoint(int power)
  440. {
  441. _powerSetPoint = power;
  442. }
  443. internal void NoteForwardPower(int power)
  444. {
  445. _forwardPower = power;
  446. }
  447. internal void NoteHaloInstalled(bool isInstalled)
  448. {
  449. _isHaloInstalled = isInstalled;
  450. }
  451. }
  452. public enum EnumRfPowerCommunicationMode
  453. {
  454. Undefined,
  455. Host = 2,
  456. UserPort = 4, //Analog
  457. Diagnostic = 8,
  458. DeviceNet = 16,
  459. EtherCat32 = 32,
  460. }
  461. }