MksRfPlasmaGenerator.cs 13 KB

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