TxHighFrequencyRF.cs 13 KB

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