KITZCTTThrottleValve.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.OperationCenter;
  5. using Aitex.Core.RT.SCCore;
  6. using Aitex.Core.Util;
  7. using MECF.Framework.Common.Communications;
  8. using MECF.Framework.Common.Device.Bases;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.ThrottleValves.KITZ
  15. {
  16. public class KITZThrottleValve : ThrottleValveBase
  17. {
  18. #region properties
  19. public int TVAlarm
  20. {
  21. get
  22. {
  23. //return _aiThrottleValveAlarm.Value;
  24. return 0;
  25. }
  26. }
  27. public override AITThrottleValveData DeviceData
  28. {
  29. get
  30. {
  31. AITThrottleValveData data = new AITThrottleValveData()
  32. {
  33. DeviceName = Name,
  34. DeviceSchematicId = DeviceID,
  35. DisplayName = Display,
  36. UnitPressure = "Torr",
  37. UnitPosition = "%",
  38. Module = Module,
  39. PositionFeedback = PositionFeedback,
  40. PressureFeedback = PressureFeedback,
  41. PressureSetPoint = PressureSetPoint,
  42. PositionSetPoint = PositionSetPoint,
  43. MaxValuePressure = PressureRange,
  44. MaxValuePosition = 100,
  45. Mode = (int)Mode,
  46. };
  47. return data;
  48. }
  49. }
  50. public double PressureRange
  51. {
  52. get
  53. {
  54. if (_scPressureRange == null)
  55. return 0;
  56. return _scPressureRange.DoubleValue;
  57. }
  58. }
  59. public bool IsError
  60. {
  61. get { return _errorCode != 0; }
  62. }
  63. public int DBNumber { get; private set; } = 6;
  64. #endregion
  65. private KITZConnection _connection;
  66. private const int VlaveClose = 1;
  67. private const int VlaveOpen = 2;
  68. private const int VlavePositionControl = 3;
  69. private const int VlavePressureControl = 4;
  70. private int _errorCode;
  71. private RD_TRIG _trigPumpOnOff = new RD_TRIG();
  72. private R_TRIG _trigError = new R_TRIG();
  73. private R_TRIG _trigOverTemp = new R_TRIG();
  74. private R_TRIG _trigWarningMessage = new R_TRIG();
  75. private string _lastError = string.Empty;
  76. private R_TRIG _trigCommunicationError = new R_TRIG();
  77. private R_TRIG _trigRetryConnect = new R_TRIG();
  78. private PeriodicJob _thread;
  79. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  80. private object _locker = new object();
  81. private bool _enableLog = true;
  82. private bool _isFirstTime = true;
  83. private SCConfigItem _scPressureRange;
  84. public KITZThrottleValve(string module, string name) : base(module, name)
  85. {
  86. }
  87. public override bool Initialize()
  88. {
  89. base.Initialize();
  90. _scPressureRange = SC.GetConfigItem($"{Module}.{Name}.PressureRange");
  91. //for recipe
  92. OP.Subscribe($"{Module}.{Name}.SetPressure", (out string reason, int time, object[] param) =>
  93. {
  94. reason = string.Empty;
  95. SetPressure(Convert.ToSingle(param[0]));
  96. return true;
  97. });
  98. string portName = SC.GetStringValue($"{Module}.{Name}.Address");
  99. _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
  100. _connection = new KITZConnection(portName);
  101. _connection.EnableLog(_enableLog);
  102. if (_connection.Connect())
  103. {
  104. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  105. }
  106. _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  107. return true;
  108. }
  109. private bool OnTimer()
  110. {
  111. try
  112. {
  113. _connection.MonitorTimeout();
  114. if (!_connection.IsConnected || _connection.IsCommunicationError)
  115. {
  116. lock (_locker)
  117. {
  118. _lstHandler.Clear();
  119. }
  120. _trigRetryConnect.CLK = !_connection.IsConnected;
  121. if (_trigRetryConnect.Q)
  122. {
  123. _connection.SetPortAddress(SC.GetStringValue($"{Module}.{Name}.Address"));
  124. if (!_connection.Connect())
  125. {
  126. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  127. }
  128. }
  129. return true;
  130. }
  131. HandlerBase handler = null;
  132. if (!_connection.IsBusy)
  133. {
  134. lock (_locker)
  135. {
  136. if (_lstHandler.Count == 0)
  137. {
  138. if(_isFirstTime)
  139. {
  140. _lstHandler.AddFirst(new RemoteHandler(this));
  141. _lstHandler.AddLast(new CalibrationHandler(this));
  142. Mode = PressureCtrlMode.TVCalib;
  143. _isFirstTime = false;
  144. }
  145. _lstHandler.AddLast(new QueryStateHandler(this));
  146. if(Mode != PressureCtrlMode.TVCalib)
  147. {
  148. _lstHandler.AddLast(new QueryPressureHandler(this));
  149. _lstHandler.AddLast(new QueryPositionHandler(this));
  150. }
  151. }
  152. if (_lstHandler.Count > 0)
  153. {
  154. handler = _lstHandler.First.Value;
  155. _lstHandler.RemoveFirst();
  156. }
  157. }
  158. if (handler != null)
  159. {
  160. _connection.Execute(handler);
  161. }
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. LOG.Write(ex);
  167. }
  168. return true;
  169. }
  170. public override void Monitor()
  171. {
  172. try
  173. {
  174. _connection.EnableLog(_enableLog);
  175. _trigError.CLK = IsError;
  176. if (_trigError.Q)
  177. {
  178. EV.PostAlarmLog(Module, $"{Module}.{Name} is error, error code {_errorCode:D3}");
  179. }
  180. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  181. if (_trigCommunicationError.Q)
  182. {
  183. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. LOG.Write(ex);
  189. }
  190. }
  191. public override void Reset()
  192. {
  193. _tolerancePressureAlarmChecker.Reset(PressureAlarmTime);
  194. _tolerancePressureWarningChecker.Reset(PressureWarningTime);
  195. _tolerancePositionAlarmChecker.Reset(PositionAlarmTime);
  196. _tolerancePositionWarningChecker.Reset(PositionWarningTime);
  197. if (_trigError.M || _trigWarningMessage.M)
  198. _isFirstTime = true;
  199. _trigError.RST = true;
  200. _trigOverTemp.RST = true;
  201. _trigWarningMessage.RST = true;
  202. _connection.SetCommunicationError(false, "");
  203. _trigCommunicationError.RST = true;
  204. _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
  205. _trigRetryConnect.RST = true;
  206. base.Reset();
  207. }
  208. public void SetErrorCode(int errorCode)
  209. {
  210. _errorCode = errorCode;
  211. }
  212. public void SetError(string reason)
  213. {
  214. _trigWarningMessage.CLK = true;
  215. if (_trigWarningMessage.Q)
  216. {
  217. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  218. }
  219. }
  220. public override void SetControlMode(PressureCtrlMode mode)
  221. {
  222. if(Mode == PressureCtrlMode.TVCalib)
  223. {
  224. EV.PostWarningLog(Module, $"Throttle valve is in calibration mode, pls wait");
  225. return;
  226. }
  227. if (mode == PressureCtrlMode.TVClose)
  228. {
  229. lock (_locker)
  230. {
  231. _lstHandler.AddLast(new CloseHandler(this));
  232. }
  233. }
  234. else if (mode == PressureCtrlMode.TVOpen)
  235. {
  236. lock (_locker)
  237. {
  238. _lstHandler.AddLast(new OpenHandler(this));
  239. }
  240. }
  241. else if (mode == PressureCtrlMode.TVPositionCtrl)
  242. {
  243. lock (_locker)
  244. {
  245. _lstHandler.AddLast(new SetPositionHandler(this, 0));
  246. }
  247. }
  248. else if (mode == PressureCtrlMode.TVPressureCtrl)
  249. {
  250. lock (_locker)
  251. {
  252. _lstHandler.AddLast(new SetPressureHandler(this, 0, DBNumber));
  253. }
  254. }
  255. }
  256. public override void SetPressure(float pressure)
  257. {
  258. if (Mode == PressureCtrlMode.TVCalib)
  259. {
  260. EV.PostWarningLog(Module, $"{Name} is in calibration mode, pls wait");
  261. return;
  262. }
  263. if (pressure < 0 || pressure > PressureRange)
  264. {
  265. EV.PostWarningLog(Module, $"{Name} invalid pressure {pressure}, not in the scope (0, {PressureRange})");
  266. return;
  267. }
  268. PressureSetPoint = pressure;
  269. lock (_locker)
  270. {
  271. _lstHandler.AddLast(new SetPressureHandler(this, pressure, DBNumber));
  272. }
  273. }
  274. public void SetPressure(float pressure, int dbNo)
  275. {
  276. if (Mode == PressureCtrlMode.TVCalib)
  277. {
  278. EV.PostWarningLog(Module, $"{Name} is in calibration mode, pls wait");
  279. return;
  280. }
  281. if (pressure < 0 || pressure > PressureRange)
  282. {
  283. EV.PostWarningLog(Module, $"{Name} invalid pressure {pressure}, not in the scope (0, {PressureRange})");
  284. return;
  285. }
  286. PressureSetPoint = pressure;
  287. DBNumber = dbNo;
  288. lock (_locker)
  289. {
  290. _lstHandler.AddLast(new SetPressureHandler(this, pressure, dbNo));
  291. }
  292. }
  293. public override void SetPosition(float position)
  294. {
  295. if (Mode == PressureCtrlMode.TVCalib)
  296. {
  297. EV.PostWarningLog(Module, $"{Name} is in calibration mode, pls wait");
  298. return;
  299. }
  300. if (position < 0 || position > 100)
  301. {
  302. EV.PostWarningLog(Module, $"{Name} invalid position {position}, not in the scope (0, 100)");
  303. return;
  304. }
  305. PositionSetPoint = position;
  306. lock (_locker)
  307. {
  308. _lstHandler.AddLast(new SetPositionHandler(this, position));
  309. }
  310. }
  311. }
  312. }