DxkdpDcPower.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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.DCPowers.AE
  14. {
  15. public class DxkdpDcPower : RfPowerBase
  16. {
  17. public override bool IsPowerOn
  18. {
  19. get { return _isOn; }
  20. }
  21. public override bool IsSetPowerOn
  22. {
  23. get { return _setOn; }
  24. }
  25. public override bool IsError
  26. {
  27. get { return _isError; }
  28. }
  29. public override EnumRfPowerRegulationMode RegulationMode
  30. {
  31. get { return _regulationMode; }
  32. }
  33. public override float ForwardPower
  34. {
  35. get { return _forwardPower; }
  36. }
  37. public override float ReflectPower
  38. {
  39. get { return _reflectPower; }
  40. }
  41. public override float PowerSetPoint
  42. {
  43. get { return _powerSetPoint; }
  44. }
  45. public float Voltage
  46. {
  47. get { return _voltage; }
  48. }
  49. public float Current
  50. {
  51. get { return _current; }
  52. }
  53. public override float Frequency { get; set; }
  54. public override float PulsingFrequency { get; set; }
  55. public override float PulsingDutyCycle { get; set; }
  56. public override AITRfPowerData DeviceData
  57. {
  58. get
  59. {
  60. AITRfPowerData data = new AITRfPowerData()
  61. {
  62. Module = Module,
  63. DeviceName = Name,
  64. DeviceSchematicId = DeviceID,
  65. DisplayName = Display,
  66. UnitPower = _unit,
  67. ForwardPower = ForwardPower,
  68. ReflectPower = ReflectPower,
  69. PowerSetPoint = PowerSetPoint,
  70. RegulationMode = RegulationMode,
  71. Voltage = Voltage,
  72. Current = Current,
  73. CurrentSetPoint = _setCurrent,
  74. VoltageSetPoint = _setVoltage,
  75. ScaleCurrent = _scaleCurrent,
  76. UnitCurrent = "A",
  77. UnitVoltage = "V",
  78. IsRfOn = IsPowerOn,
  79. IsRfAlarm = IsError,
  80. Frequency = Frequency,
  81. PulsingFrequency = PulsingFrequency,
  82. PulsingDutyCycle = PulsingDutyCycle,
  83. };
  84. return data;
  85. }
  86. }
  87. public DxkdpDcPowerConnection Connection
  88. {
  89. get { return _connection; }
  90. }
  91. private DxkdpDcPowerConnection _connection;
  92. private byte _deviceAddress;
  93. private float _powerSetPoint;
  94. private float _setVoltage;
  95. private float _setCurrent;
  96. private float _reflectPower;
  97. private float _scaleCurrent;
  98. private bool _isOn;
  99. private bool _isError;
  100. private string _errorCode;
  101. private float _forwardPower;
  102. private string _unit;
  103. private float _voltage;
  104. private float _current;
  105. private int _queryInterval;
  106. private bool _setOn;
  107. private int _vmss = 2;
  108. private int _cmss = 3;
  109. private float _sysVoltage;
  110. private float _sysCurrent;
  111. private int _polarity;
  112. private bool _remoteMode;
  113. private EnumRfPowerRegulationMode _regulationMode;
  114. private EnumRfPowerCommunicationMode _commMode;
  115. public Func<bool, bool> FuncCheckInterLock;
  116. public Func<bool, bool> FuncForceAction;
  117. private R_TRIG _trigForceAction = new R_TRIG();
  118. private RD_TRIG _trigRfOnOff = new RD_TRIG();
  119. private R_TRIG _trigError = new R_TRIG();
  120. private R_TRIG _trigWarningMessage = new R_TRIG();
  121. private R_TRIG _trigCommunicationError = new R_TRIG();
  122. private R_TRIG _trigRetryConnect = new R_TRIG();
  123. private R_TRIG _trigConnect = new R_TRIG();
  124. private PeriodicJob _thread;
  125. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  126. private object _locker = new object();
  127. private bool _enableLog = true;
  128. private bool _isHaloInstalled;
  129. public DxkdpDcPower(string module, string name) : base(module, name)
  130. {
  131. }
  132. string portName;
  133. public override bool Initialize()
  134. {
  135. base.Initialize();
  136. portName = SC.GetStringValue($"{Module}.{Name}.Address");
  137. //int bautRate = SC.GetValue<int>($"{ScBasePath}.{Name}.BaudRate");
  138. //int dataBits = SC.GetValue<int>($"{ScBasePath}.{Name}.DataBits");
  139. //Enum.TryParse(SC.GetStringValue($"{ScBasePath}.{Name}.Parity"), out Parity parity);
  140. //Enum.TryParse(SC.GetStringValue($"{ScBasePath}.{Name}.StopBits"), out StopBits stopBits);
  141. _unit = SC.GetStringValue($"{Module}.{Name}.Unit");
  142. _deviceAddress = (byte)SC.GetValue<int>($"{Module}.{Name}.DeviceAddress");
  143. _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
  144. _scaleCurrent = (float)SC.GetValue<double>($"{Module}.{Name}.ScaleCurrent");
  145. _queryInterval = SC.GetValue<int>($"{Module}.{Name}.QueryInterval");
  146. _connection = new DxkdpDcPowerConnection(portName);
  147. _connection.IsEnableHandlerRetry = true;
  148. _connection.EnableLog(_enableLog);
  149. if (_connection.Connect())
  150. {
  151. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  152. }
  153. _thread = new PeriodicJob(_queryInterval, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  154. DATA.Subscribe($"{Module}.{Name}.Current", () => Current);
  155. DATA.Subscribe($"{Module}.{Name}.Voltage", () => Voltage);
  156. OP.Subscribe($"{Module}.{Name}.SetVoltage", (out string reason, int time, object[] args) =>
  157. {
  158. reason = "";
  159. float value = Convert.ToSingle((string)args[0]);
  160. if (!SetVoltage(out reason, time, value))
  161. {
  162. EV.PostAlarmLog(Module, $"{Module}.{Name} Can not set Voltage, {reason}");
  163. return false;
  164. }
  165. EV.PostInfoLog(Module, $"{Module}.{Name} set Voltage to {args[0]}");
  166. return true;
  167. });
  168. OP.Subscribe($"{Module}.{Name}.SetCurrent", (out string reason, int time, object[] args) =>
  169. {
  170. reason = "";
  171. float value = Convert.ToSingle((string)args[0]);
  172. SetCurrent(value);
  173. EV.PostInfoLog(Module, $"{Module}.{Name} set Current to {value}");
  174. return true;
  175. });
  176. OP.Subscribe($"{Module}.{Name}.SetCurrentForRecipe", (out string reason, int time, object[] args) =>
  177. {
  178. float value = Convert.ToSingle(args[0]);
  179. bool isOn = value > 0;
  180. reason = string.Empty;
  181. SetCurrent(value);
  182. if (isOn ^ IsPowerOn)
  183. {
  184. SetPowerOnOff(isOn, out _);
  185. }
  186. EV.PostInfoLog(Module, $"{Module}.{Name} set Current to {value}");
  187. return true;
  188. });
  189. return true;
  190. }
  191. private bool OnTimer()
  192. {
  193. try
  194. {
  195. _connection.MonitorTimeout();
  196. _trigConnect.CLK = _connection.IsConnected;
  197. if (_trigConnect.Q)
  198. {
  199. _lstHandler.AddLast(new DxkdpRfPowerSetModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
  200. }
  201. if (!_connection.IsConnected)
  202. {
  203. lock (_locker)
  204. {
  205. _lstHandler.Clear();
  206. }
  207. _trigRetryConnect.CLK = !_connection.IsConnected;
  208. if (_trigRetryConnect.Q)
  209. {
  210. _connection.SetPortAddress(SC.GetStringValue($"{Module}.{Name}.Address"));
  211. if (_connection.Connect() && _connection.IsConnected)
  212. {
  213. _connection.ForceClear();
  214. EV.PostInfoLog(Module, $"Reconnect with {_connection.Address}, {Module}.{Name}");
  215. }
  216. else
  217. {
  218. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  219. }
  220. }
  221. return true;
  222. }
  223. // 达到一定条件,强制关闭电源
  224. if (FuncForceAction != null)
  225. {
  226. _trigForceAction.CLK = FuncForceAction(IsPowerOn);
  227. if (_trigForceAction.Q)
  228. {
  229. SetCurrent(0);
  230. SetPowerOnOff(false, out string reason);
  231. //EV.PostAlarmLog(Module, $"Force set {Name} off for interlock");
  232. }
  233. }
  234. HandlerBase handler = null;
  235. if (!_connection.IsBusy)
  236. {
  237. lock (_locker)
  238. {
  239. if (_lstHandler.Count == 0)
  240. {
  241. if (!_remoteMode)
  242. {
  243. _lstHandler.AddLast(new DxkdpRfPowerSetModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
  244. }
  245. if (_setOn || _isOn)
  246. {
  247. //_lstHandler.AddLast(new DxkdpRfPowerQuerySysInformationHandler(this, _deviceAddress));
  248. _lstHandler.AddLast(new DxkdpRfPowerQueryStateHandler(this, _deviceAddress));
  249. _lstHandler.AddLast(new DxkdpRfPowerQueryPracticalHandler(this, _deviceAddress));
  250. }
  251. }
  252. if (_lstHandler.Count > 0)
  253. {
  254. handler = _lstHandler.First.Value;
  255. _lstHandler.RemoveFirst();
  256. }
  257. }
  258. if (handler != null)
  259. {
  260. _connection.Execute(handler);
  261. }
  262. }
  263. }
  264. catch (Exception ex)
  265. {
  266. LOG.Write(ex);
  267. }
  268. return true;
  269. }
  270. public override void Monitor()
  271. {
  272. try
  273. {
  274. _connection.EnableLog(_enableLog);
  275. _trigRfOnOff.CLK = _isOn;
  276. if (_trigRfOnOff.R)
  277. {
  278. EV.PostInfoLog(Module, $"{Module}.{Name} is on");
  279. }
  280. if (_trigRfOnOff.T)
  281. {
  282. EV.PostInfoLog(Module, $"{Module}.{Name} is off");
  283. }
  284. _trigError.CLK = IsError;
  285. if (_trigError.Q)
  286. {
  287. EV.PostAlarmLog(Module, $"{Module}.{Name} is error, error code {_errorCode:D3}");
  288. }
  289. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  290. if (_trigCommunicationError.Q)
  291. {
  292. EV.PostWarningLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  293. }
  294. //MonitorRampingPower();
  295. base.Monitor();
  296. }
  297. catch (Exception ex)
  298. {
  299. LOG.Write(ex);
  300. }
  301. }
  302. public override void Reset()
  303. {
  304. _trigError.RST = true;
  305. _trigWarningMessage.RST = true;
  306. _connection.SetCommunicationError(false, "");
  307. _trigCommunicationError.RST = true;
  308. _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
  309. _trigRetryConnect.RST = true;
  310. _trigForceAction.RST = true;
  311. base.Reset();
  312. }
  313. public void SetCurrent(float current)
  314. {
  315. _setCurrent = current;
  316. lock (_locker)
  317. {
  318. UInt16 setCurrent = Convert.ToUInt16(current * Math.Pow(10, _cmss));
  319. if (current > 0)
  320. _lstHandler.AddLast(new DxkdpRfPowerSetElectricityHandler(this, _deviceAddress, setCurrent));
  321. else
  322. _lstHandler.AddFirst(new DxkdpRfPowerSetElectricityHandler(this, _deviceAddress, setCurrent));
  323. }
  324. }
  325. public bool SetVoltage(out string reason, int time, float voltage)
  326. {
  327. reason = string.Empty;
  328. lock (_locker)
  329. {
  330. UInt16 setVoltage = Convert.ToUInt16(voltage * Math.Pow(10, _vmss));
  331. _lstHandler.AddLast(new DxkdpRfPowerSetVoltageHandler(this, _deviceAddress, setVoltage));
  332. }
  333. return true;
  334. }
  335. public override bool SetPowerOnOff(bool isOn, out string reason)
  336. {
  337. reason = "";
  338. if (FuncCheckInterLock != null)
  339. {
  340. if (!FuncCheckInterLock(isOn))
  341. {
  342. return false;
  343. }
  344. }
  345. _setOn = isOn;
  346. lock (_locker)
  347. {
  348. if (isOn)
  349. _lstHandler.AddLast(new DxkdpRfPowerSwitchOnOffHandler(this, _deviceAddress, isOn));
  350. else
  351. _lstHandler.AddFirst(new DxkdpRfPowerSwitchOnOffHandler(this, _deviceAddress, isOn));
  352. }
  353. return true;
  354. }
  355. internal void NoteError(string reason)
  356. {
  357. _trigWarningMessage.CLK = true;
  358. if (_trigWarningMessage.Q)
  359. {
  360. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  361. }
  362. }
  363. internal void NoteStatus(byte data)
  364. {
  365. _isOn = data == 0x01;
  366. }
  367. internal void NoteMinVStepSize(int vmss)
  368. {
  369. _vmss = vmss;
  370. }
  371. internal void NoteMinCStepSize(int cmss)
  372. {
  373. _cmss = cmss;
  374. }
  375. internal void NoteSysCurrent(int sysCurrent)
  376. {
  377. _sysCurrent = (float)sysCurrent /(float)Math.Pow(10, _cmss);
  378. }
  379. internal void NoteSysVoltage(int sysVoltage)
  380. {
  381. _sysVoltage = (float)sysVoltage / (float)Math.Pow(10, _vmss);
  382. }
  383. internal void NoteVoltage(int voltage)
  384. {
  385. _voltage = (float)voltage / (float)Math.Pow(10, _vmss);
  386. }
  387. internal void NoteCurrent(int current)
  388. {
  389. _current = (float)current / (float)Math.Pow(10, _cmss);
  390. }
  391. internal void NoteMode(bool mode)
  392. {
  393. _remoteMode = mode;
  394. }
  395. }
  396. }