WattsineRF.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.SCCore;
  4. using Aitex.Core.Util;
  5. using MECF.Framework.Common.Communications;
  6. using MECF.Framework.Common.Device.Bases;
  7. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Common;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.Wattsine
  14. {
  15. public class WattsineRF : RfPowerBase, IConnection
  16. {
  17. public string Address { get; }
  18. public override bool IsConnected { get; }
  19. public bool Connect()
  20. {
  21. return true;
  22. }
  23. public bool Disconnect()
  24. {
  25. return true;
  26. }
  27. public string PortStatus { get; set; } = "Closed";
  28. private WattsineRFConnection _connection;
  29. public WattsineRFConnection Connection
  30. {
  31. get { return _connection; }
  32. }
  33. public string PortName { get; set; }
  34. private R_TRIG _trigError = new R_TRIG();
  35. private R_TRIG _trigCommunicationError = new R_TRIG();
  36. private R_TRIG _trigRetryConnect = new R_TRIG();
  37. private PeriodicJob _thread;
  38. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  39. private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
  40. public List<IOResponse> IOResponseList { get; set; } = new List<IOResponse>();
  41. private object _locker = new object();
  42. //private bool _enableLog;
  43. private string _scRoot;
  44. public WattsineRF(string module, string name, string scRoot, string portName) : base(module, name)
  45. {
  46. _scRoot = scRoot;
  47. PortName = portName;
  48. }
  49. public override bool Initialize()
  50. {
  51. base.Initialize();
  52. //ResetPropertiesAndResponses();
  53. if (_connection != null && _connection.IsConnected )
  54. return true;
  55. if (_connection != null && _connection.IsConnected)
  56. _connection.Disconnect();
  57. PortName = PortName;
  58. //_enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
  59. _connection = new WattsineRFConnection(PortName);
  60. //_connection.EnableLog(_enableLog);
  61. //SetLocalAdress(1);
  62. _lstMonitorHandler.AddLast(new WattsineRFWriteRegisterHandler(this, "00,11", $"00,01"));
  63. if (_connection.Connect())
  64. {
  65. PortStatus = "Open";
  66. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  67. }
  68. _thread = new PeriodicJob(6000, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  69. return true;
  70. }
  71. private bool OnTimer()
  72. {
  73. try
  74. {
  75. //_connection.MonitorTimeout();
  76. if (!_connection.IsConnected || _connection.IsCommunicationError)
  77. {
  78. lock (_locker)
  79. {
  80. _lstHandler.Clear();
  81. }
  82. _trigRetryConnect.CLK = !_connection.IsConnected;
  83. if (_trigRetryConnect.Q)
  84. {
  85. _connection.SetPortAddress(SC.GetStringValue($"{ScBasePath}.{Name}.Address"));
  86. if (!_connection.Connect())
  87. {
  88. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  89. }
  90. else
  91. {
  92. }
  93. }
  94. return true;
  95. }
  96. HandlerBase handler = null;
  97. if (!_connection.IsBusy)
  98. {
  99. lock (_locker)
  100. {
  101. if (_lstHandler.Count == 0)
  102. {
  103. foreach (var monitorHandler in _lstMonitorHandler)
  104. {
  105. _lstHandler.AddLast(monitorHandler);
  106. }
  107. }
  108. if (_lstHandler.Count > 0)
  109. {
  110. handler = _lstHandler.First.Value;
  111. _lstHandler.RemoveFirst();
  112. }
  113. }
  114. if (handler != null)
  115. {
  116. _connection.Execute(handler);
  117. }
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. LOG.Write(ex);
  123. }
  124. return true;
  125. }
  126. public override void Monitor()
  127. {
  128. try
  129. {
  130. //_connection.EnableLog(_enableLog);
  131. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  132. if (_trigCommunicationError.Q)
  133. {
  134. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  135. }
  136. }
  137. catch (Exception ex)
  138. {
  139. LOG.Write(ex);
  140. }
  141. }
  142. public override void Reset()
  143. {
  144. _trigError.RST = true;
  145. _connection.SetCommunicationError(false, "");
  146. _trigCommunicationError.RST = true;
  147. //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  148. _trigRetryConnect.RST = true;
  149. base.Reset();
  150. }
  151. #region Command Functions
  152. /// <summary>
  153. /// 系统复位
  154. /// </summary>
  155. internal void SystemReset()
  156. {
  157. _lstHandler.AddLast(new WattsineRFWriteRegisterHandler(this, "00,10", "00,01"));
  158. }
  159. internal void SetLocalAdress(int adress)
  160. {
  161. string addres16 = string.Format("{0:X1}", adress);
  162. _lstHandler.AddLast(new WattsineRFWriteRegisterHandler(this, "00,11", $"00,{addres16}"));
  163. }
  164. internal void SetPowerONOFF(bool isOn)
  165. {
  166. _lstHandler.AddLast(new WattsineRFWriteRegisterHandler(this, "00,80", $"00,{(isOn ? "01" : "00")}"));
  167. }
  168. internal void SetRfONOFF(bool isOn)
  169. {
  170. _lstHandler.AddLast(new WattsineRFWriteRegisterHandler(this, "00,81", $"00,{(isOn ? "01" : "00")}"));
  171. }
  172. internal void ReadAlarm()
  173. {
  174. _lstHandler.AddLast(new WattsineRFReadRegisterHandler(this, "00,A0", $"00,00"));
  175. }
  176. /// <summary>
  177. /// 读取设备
  178. /// 整机输出功率
  179. /// 整机反射功率
  180. /// 整机驻波比值
  181. /// </summary>
  182. internal void ReadDeviceInfo()
  183. {
  184. _lstHandler.AddLast(new WattsineRFReadRegisterHandler(this, "00,A1", $"00,03"));
  185. }
  186. #endregion
  187. #region Properties
  188. public string Error { get; private set; }
  189. public int Alarm { get; private set; }
  190. public int OutputPower { get; set; }
  191. public int ReflectedPower { get; set; }
  192. public int IsolatorPower { get; set; }
  193. #endregion
  194. }
  195. }