TxLowFrequencyRF.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Text;
  5. using Aitex.Core.Common.DeviceData;
  6. using Aitex.Core.RT.DataCenter;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.RT.OperationCenter;
  10. using Aitex.Core.RT.SCCore;
  11. using Aitex.Core.Util;
  12. using MECF.Framework.Common.Communications;
  13. using MECF.Framework.Common.Device.Bases;
  14. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.AdTecTxLow
  15. {
  16. public class LowFrequencyRF : RfPowerBase
  17. {
  18. public enum EnumLowRfPowerCommunicationMode
  19. {
  20. Manual = 1,
  21. RS232C = 2,
  22. }
  23. public override bool IsPowerOn
  24. {
  25. get { return _isOn; }
  26. }
  27. public override bool IsSetPowerOn
  28. {
  29. get { return _setOn; }
  30. }
  31. public override bool IsError
  32. {
  33. get { return _isError; }
  34. }
  35. public override EnumRfPowerRegulationMode RegulationMode
  36. {
  37. get { return _regulationMode; }
  38. }
  39. public override float ForwardPower
  40. {
  41. get { return _forwardPower; }
  42. }
  43. public override float ReflectPower
  44. {
  45. get { return _reflectPower; }
  46. }
  47. public override float PowerSetPoint
  48. {
  49. get { return _powerSetPoint; }
  50. }
  51. public override float Frequency { get; set; }
  52. public override float PulsingFrequency { get; set; }
  53. public override float PulsingDutyCycle { get; set; }
  54. public Func<bool, bool> FuncCheckInterLock;
  55. public Func<bool, bool> FuncForceAction;
  56. private R_TRIG _trigForceAction = new R_TRIG();
  57. public override AITRfPowerData DeviceData
  58. {
  59. get
  60. {
  61. AITRfPowerData data = new AITRfPowerData()
  62. {
  63. Module = Module,
  64. DeviceName = Name,
  65. DeviceSchematicId = DeviceID,
  66. DisplayName = Display,
  67. UnitPower = _unit,
  68. ForwardPower = ForwardPower,
  69. ReflectPower = ReflectPower,
  70. PowerSetPoint = PowerSetPoint,
  71. RegulationMode = RegulationMode,
  72. IsRfOn = IsPowerOn,
  73. IsRfAlarm = IsError,
  74. Frequency = Frequency,
  75. PulsingFrequency = PulsingFrequency,
  76. PulsingDutyCycle = PulsingDutyCycle,
  77. ScalePower = _scalePower
  78. };
  79. return data;
  80. }
  81. }
  82. public LowFrequencyRFConnection Connection
  83. {
  84. get { return _connection; }
  85. }
  86. private LowFrequencyRFConnection _connection;
  87. //private byte _deviceAddress;
  88. private float _powerSetPoint;
  89. private float _reflectPower;
  90. private float _scalePower;
  91. private bool _isOn;
  92. private bool _isError;
  93. private string _errorCode;
  94. private float _forwardPower;
  95. private string _unit;
  96. private bool _setOn;
  97. private EnumRfPowerRegulationMode _regulationMode;
  98. private EnumLowRfPowerCommunicationMode _commMode;
  99. private RD_TRIG _trigRfOnOff = new RD_TRIG();
  100. private R_TRIG _trigError = new R_TRIG();
  101. private R_TRIG _trigWarningMessage = new R_TRIG();
  102. private R_TRIG _trigCommunicationError = new R_TRIG();
  103. private R_TRIG _trigRetryConnect = new R_TRIG();
  104. private R_TRIG _trigConnect = new R_TRIG();
  105. private PeriodicJob _thread;
  106. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  107. private object _locker = new object();
  108. private bool _enableLog = true;
  109. private bool _isHaloInstalled;
  110. public LowFrequencyRF(string module, string name) : base(module, name)
  111. {
  112. }
  113. public override bool Initialize()
  114. {
  115. base.Initialize();
  116. string portName = SC.GetStringValue($"{Module}.{Name}.Address");
  117. //int bautRate = SC.GetValue<int>($"{ScBasePath}.{Name}.BaudRate");
  118. //int dataBits = SC.GetValue<int>($"{ScBasePath}.{Name}.DataBits");
  119. //Enum.TryParse(SC.GetStringValue($"{ScBasePath}.{Name}.Parity"), out Parity parity);
  120. //Enum.TryParse(SC.GetStringValue($"{ScBasePath}.{Name}.StopBits"), out StopBits stopBits);
  121. _unit = SC.GetStringValue($"{Module}.{Name}.Unit");
  122. _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
  123. _scalePower = (float)SC.GetValue<double>($"{Module}.{Name}.ScalePower");
  124. _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
  125. _connection = new LowFrequencyRFConnection(portName);
  126. _connection.IsEnableHandlerRetry = true;
  127. _connection.EnableLog(_enableLog);
  128. if (_connection.Connect())
  129. {
  130. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  131. }
  132. _thread = new PeriodicJob(150, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  133. OP.Subscribe($"{Module}.{Name}.SetPowerForRecipe", (function, args) =>
  134. {
  135. float value = Convert.ToSingle(args[0]);
  136. SetPower(value);
  137. bool isOn = value > 0;
  138. if (isOn ^ IsPowerOn)
  139. {
  140. SetPowerOnOff(isOn, out _);
  141. }
  142. return true;
  143. });
  144. return true;
  145. }
  146. protected override void SetRampPower(float power)
  147. {
  148. //_PowerSetPoint = power;
  149. //lock (_locker)
  150. //{
  151. // _lstHandler.AddLast(new TruPlasmaRF1001PreSetPiValueHandler(this, (int)PowerSetPoint.HalfAdjust()));
  152. //}
  153. }
  154. private bool OnTimer()
  155. {
  156. try
  157. {
  158. _connection.MonitorTimeout();
  159. _trigConnect.CLK = _connection.IsConnected;
  160. if (_trigConnect.Q)
  161. {
  162. _lstHandler.AddLast(new LowFrequencyRFSetControlModeHandler(this, (int)EnumLowRfPowerCommunicationMode.RS232C));
  163. }
  164. if (!_connection.IsConnected)
  165. {
  166. lock (_locker)
  167. {
  168. _lstHandler.Clear();
  169. }
  170. _trigRetryConnect.CLK = !_connection.IsConnected;
  171. if (_trigRetryConnect.Q)
  172. {
  173. _connection.SetPortAddress(SC.GetStringValue($"{Module}.{Name}.Address"));
  174. if (_connection.Connect() && _connection.IsConnected)
  175. {
  176. _connection.ForceClear();
  177. EV.PostInfoLog(Module, $"Reconnect with {_connection.Address}, {Module}.{Name}");
  178. }
  179. else
  180. {
  181. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  182. }
  183. }
  184. return true;
  185. }
  186. // 达到一定条件,强制关闭电源
  187. if (FuncForceAction != null)
  188. {
  189. _trigForceAction.CLK = FuncForceAction(IsPowerOn);
  190. if (_trigForceAction.Q)
  191. {
  192. SetPowerOnOff(false, out string reason);
  193. //EV.PostAlarmLog(Module, $"Force set {Name} off for interlock");
  194. }
  195. }
  196. HandlerBase handler = null;
  197. if (!_connection.IsBusy)
  198. {
  199. lock (_locker)
  200. {
  201. if (_lstHandler.Count == 0)
  202. {
  203. if (_commMode != EnumLowRfPowerCommunicationMode.RS232C)
  204. {
  205. _lstHandler.AddLast(new LowFrequencyRFSetControlModeHandler(this, (int)EnumLowRfPowerCommunicationMode.RS232C));
  206. }
  207. //if (_setOn || _isOn)
  208. {
  209. _lstHandler.AddLast(new LowFrequencyRFGetControlModeHandler(this));
  210. //_lstHandler.AddLast(new LowFrequencyRFGetSetPointHandler(this));
  211. _lstHandler.AddLast(new LowFrequencyRFGetSwitchOnOffHandler(this));
  212. _lstHandler.AddLast(new LowFrequencyGetForwardAndRefectedfHandler(this));
  213. //_lstHandler.AddLast(new LowFrequencyRFWaringStatusHandler(this));
  214. _lstHandler.AddLast(new LowFrequencyRFGetAlarmStatusHandler(this));
  215. }
  216. }
  217. if (_lstHandler.Count > 0)
  218. {
  219. handler = _lstHandler.First.Value;
  220. _lstHandler.RemoveFirst();
  221. }
  222. }
  223. if (handler != null)
  224. {
  225. _connection.Execute(handler);
  226. }
  227. }
  228. }
  229. catch (Exception ex)
  230. {
  231. LOG.Write(ex);
  232. }
  233. return true;
  234. }
  235. public override void Monitor()
  236. {
  237. try
  238. {
  239. _connection.EnableLog(_enableLog);
  240. _trigRfOnOff.CLK = _isOn;
  241. if (_trigRfOnOff.R)
  242. {
  243. EV.PostInfoLog(Module, $"{Module}.{Name} is on");
  244. }
  245. if (_trigRfOnOff.T)
  246. {
  247. EV.PostInfoLog(Module, $"{Module}.{Name} is off");
  248. }
  249. _trigError.CLK = IsError;
  250. if (_trigError.Q)
  251. {
  252. EV.PostAlarmLog(Module, $"{Module}.{Name} is error, error code {_errorCode:D3}");
  253. }
  254. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  255. if (_trigCommunicationError.Q)
  256. {
  257. EV.PostWarningLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  258. }
  259. //MonitorRampingPower();
  260. base.Monitor();
  261. }
  262. catch (Exception ex)
  263. {
  264. LOG.Write(ex);
  265. }
  266. }
  267. public override void Reset()
  268. {
  269. _trigError.RST = true;
  270. _trigWarningMessage.RST = true;
  271. _connection.SetCommunicationError(false, "");
  272. _trigCommunicationError.RST = true;
  273. _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
  274. _trigRetryConnect.RST = true;
  275. base.Reset();
  276. }
  277. public override void SetPower(float power)
  278. {
  279. _powerSetPoint = power;
  280. lock (_locker)
  281. {
  282. //if (power > 0)
  283. _lstHandler.AddLast(new LowFrequencyRFSetPointHandler(this, _isHaloInstalled ? (int)(power * 10) : (int)power));
  284. //else
  285. // _lstHandler.AddFirst(new LowFrequencyRFSetPointHandler(this, _isHaloInstalled ? (int)(power * 10) : (int)power));
  286. }
  287. }
  288. public override bool SetPowerOnOff(bool isOn, out string reason)
  289. {
  290. reason = "";
  291. if (FuncCheckInterLock != null)
  292. {
  293. if (!FuncCheckInterLock(isOn))
  294. {
  295. return false;
  296. }
  297. }
  298. _setOn = isOn;
  299. lock (_locker)
  300. {
  301. //if (isOn)
  302. _lstHandler.AddLast(new LowFrequencyRFSwitchOnOffHandler(this, isOn));
  303. //else
  304. // _lstHandler.AddFirst(new LowFrequencyRFSwitchOnOffHandler(this, isOn));
  305. }
  306. return true;
  307. }
  308. internal void NoteCommMode(EnumLowRfPowerCommunicationMode mode)
  309. {
  310. _commMode = mode;
  311. }
  312. internal void NoteStatus(bool isOn)
  313. {
  314. _isOn = isOn;
  315. }
  316. internal void NoteErrorStatus(bool isError, string errorCode)
  317. {
  318. _isError = isError;
  319. _errorCode = errorCode;
  320. }
  321. internal void NotePowerSetPoint(int power)
  322. {
  323. _powerSetPoint = power;
  324. }
  325. internal void NoteForwardPowerAndReflectPower(byte[] data)
  326. {
  327. _forwardPower = GetData(data[6], data[7], data[8], data[9]);
  328. _reflectPower = GetData(data[10], data[11], data[12], data[13]);
  329. }
  330. private static int GetData(byte b1, byte b2, byte b3, byte b4)
  331. {
  332. byte[] byteData = new byte[4] { b1, b2, b3, b4 };
  333. string str = Encoding.ASCII.GetString(byteData);
  334. int value = Convert.ToInt32(str, 16);
  335. return value;
  336. }
  337. }
  338. }