STeamRFMatch.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.RT.Event;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.RT.OperationCenter;
  6. using Aitex.Core.RT.SCCore;
  7. using Aitex.Core.Util;
  8. using MECF.Framework.Common.Communications;
  9. using MECF.Framework.Common.Device.Bases;
  10. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFMatchs.MKSs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Runtime.Serialization;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFMatchs.STeam
  18. {
  19. public class STeamRFMatch : RfMatchBase, IConnection
  20. {
  21. public override EnumRfMatchTuneMode TuneMode1
  22. {
  23. get
  24. {
  25. return _statusData.Status2.Net1AutoMode ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual;
  26. }
  27. }
  28. public override EnumRfMatchTuneMode TuneMode2
  29. {
  30. get
  31. {
  32. return _statusData.Status2.Net2AutoMode ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual;
  33. }
  34. }
  35. public override float LoadPosition1
  36. {
  37. get { return _statusData.LoadPosi1; }
  38. }
  39. public override float LoadPosition2
  40. {
  41. get { return _statusData.LoadPosi2; }
  42. }
  43. public override float TunePosition1
  44. {
  45. get { return _statusData.TunePosi1; }
  46. }
  47. public override float TunePosition2
  48. {
  49. get { return _statusData.TunePosi2; }
  50. }
  51. public override float BiasPeak
  52. {
  53. get { return _statusData.BiasPeak; }
  54. }
  55. public override float DCBias
  56. {
  57. get { return _statusData.DCBias; }
  58. }
  59. public override AITRfMatchData DeviceData
  60. {
  61. get
  62. {
  63. AITRfMatchData data = new AITRfMatchData()
  64. {
  65. DeviceName = Name,
  66. DeviceSchematicId = DeviceID,
  67. DisplayName = Display,
  68. LoadPosition1 = LoadPosition1,
  69. LoadPosition2 = LoadPosition2,
  70. TunePosition1 = TunePosition1,
  71. TunePosition2 = TunePosition2,
  72. TuneMode1 = TuneMode1,
  73. TuneMode2 = TuneMode2,
  74. BiasPeak = BiasPeak,
  75. DCBias = DCBias,
  76. };
  77. return data;
  78. }
  79. }
  80. private MksStatusData _statusData;
  81. public string Address => Connection.Address;
  82. public new bool IsConnected => Connection.IsConnected && !_connection.IsCommunicationError;
  83. public bool Connect()
  84. {
  85. return _connection.Connect();
  86. }
  87. public bool Disconnect()
  88. {
  89. return _connection.Disconnect();
  90. }
  91. public byte SlaveAddress { get; private set; } = 0x01;
  92. public string PortStatus { get; set; } = "Closed";
  93. private STeamRFMatchConnection _connection;
  94. public STeamRFMatchConnection Connection
  95. {
  96. get { return _connection; }
  97. }
  98. private R_TRIG _trigError = new R_TRIG();
  99. private R_TRIG _trigCommunicationError = new R_TRIG();
  100. private R_TRIG _trigRetryConnect = new R_TRIG();
  101. private PeriodicJob _thread;
  102. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  103. private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
  104. private DeviceTimer _QueryTimer = new DeviceTimer();
  105. private readonly int _QueryInterval = 2000;
  106. private object _locker = new object();
  107. private bool _enableLog;
  108. private string _address;
  109. private string _scRoot;
  110. public STeamRFMatch(string module, string name, string scRoot) : base(module, name)
  111. {
  112. _scRoot = scRoot;
  113. }
  114. public override bool Initialize()
  115. {
  116. base.Initialize();
  117. _address = _scRoot;//SC.GetStringValue($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.Address");
  118. _enableLog = SC.GetValue<bool>($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.EnableLogMessage");
  119. _connection = new STeamRFMatchConnection(this, _address);
  120. _connection.EnableLog(_enableLog);
  121. if (_connection.Connect())
  122. {
  123. PortStatus = "Open";
  124. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  125. }
  126. //_lstMonitorHandler.AddLast(new STeamRFMatchOnOrOffFrequencyCounterHandler(this,1000,true));
  127. //_lstMonitorHandler.AddLast(new STeamRFMatchStartMesurementHandler(this));
  128. //_lstMonitorHandler.AddLast(new STeamRFMatchSetAutotuningParameterHandler(this,25,10,true,false,150));
  129. //_lstMonitorHandler.AddLast(new STeamRFMatchSetHysteresisParameterHandler(this,7));
  130. //_lstMonitorHandler.AddLast(new STeamRFMatchSetContinuousAutotuningHandler(this, AutotuningEnum.ON));
  131. //_lstMonitorHandler.AddLast(new STeamRFMatchSingleAutotuningStepHandler(this));
  132. //_lstMonitorHandler.AddLast(new STeamRFMatchSendTunePosOnorOffHandler(this,true));
  133. _lstMonitorHandler.AddLast(new STeamRFMatchSetAlarmParameterHandler(this, 60, 70, 3, false));
  134. _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  135. _QueryTimer.Start(_QueryInterval);
  136. //_lstMonitorHandler.AddLast(new KaimeiRFMatchGetStatusHandler(this));
  137. DATA.Subscribe($"{Module}.{Name}.IsConnected", () => IsConnected);
  138. DATA.Subscribe($"{Module}.{Name}.Address", () => Address);
  139. OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) =>
  140. {
  141. Disconnect();
  142. Connect();
  143. return true;
  144. });
  145. //for recipe
  146. return true;
  147. }
  148. internal void NoteStatus(MksStatusData data)
  149. {
  150. _statusData = data;
  151. }
  152. private bool OnTimer()
  153. {
  154. try
  155. {
  156. _connection.MonitorTimeout();
  157. if (!_connection.IsConnected || _connection.IsCommunicationError)
  158. {
  159. lock (_locker)
  160. {
  161. _lstHandler.Clear();
  162. }
  163. _trigRetryConnect.CLK = !_connection.IsConnected;
  164. if (_trigRetryConnect.Q)
  165. {
  166. _connection.SetPortAddress(SC.GetStringValue($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.Address"));
  167. if (!_connection.Connect())
  168. {
  169. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  170. }
  171. else
  172. {
  173. //_lstHandler.AddLast(new CommetRFMatchQueryPinHandler(this, _deviceAddress));
  174. //_lstHandler.AddLast(new CommetRFMatchSetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
  175. }
  176. }
  177. return true;
  178. }
  179. HandlerBase handler = null;
  180. if (!_connection.IsBusy)
  181. {
  182. lock (_locker)
  183. {
  184. if (_lstHandler.Count == 0)
  185. {
  186. if (_QueryTimer.IsTimeout())
  187. {
  188. foreach (var monitorHandler in _lstMonitorHandler)
  189. {
  190. _lstHandler.AddLast(monitorHandler);
  191. }
  192. _QueryTimer.Start(_QueryInterval);
  193. }
  194. }
  195. if (_lstHandler.Count > 0)
  196. {
  197. handler = _lstHandler.First.Value;
  198. _lstHandler.RemoveFirst();
  199. }
  200. }
  201. if (handler != null)
  202. {
  203. _connection.Execute(handler);
  204. }
  205. }
  206. }
  207. catch (Exception ex)
  208. {
  209. LOG.Write(ex);
  210. }
  211. return true;
  212. }
  213. public override void Monitor()
  214. {
  215. try
  216. {
  217. //_connection.EnableLog(_enableLog);
  218. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  219. if (_trigCommunicationError.Q)
  220. {
  221. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  222. }
  223. base.Monitor();
  224. }
  225. catch (Exception ex)
  226. {
  227. LOG.Write(ex);
  228. }
  229. }
  230. public override void Reset()
  231. {
  232. _trigError.RST = true;
  233. _connection.SetCommunicationError(false, "");
  234. _trigCommunicationError.RST = true;
  235. //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  236. _trigRetryConnect.RST = true;
  237. base.Reset();
  238. }
  239. #region Command Functions
  240. #endregion
  241. #region Properties
  242. public string Error { get; private set; }
  243. #endregion
  244. #region Note Functions
  245. private R_TRIG _trigWarningMessage = new R_TRIG();
  246. public void NoteError(string reason)
  247. {
  248. if (reason != null)
  249. {
  250. _trigWarningMessage.CLK = true;
  251. if (_trigWarningMessage.Q)
  252. {
  253. EV.PostAlarmLog(Module, $"{Module}.{Name} error, {reason}");
  254. }
  255. Error = reason;
  256. }
  257. else
  258. {
  259. Error = null;
  260. }
  261. }
  262. #endregion
  263. }
  264. }