RevtechMatch.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using Aitex.Core.Common.DeviceData;
  8. using Aitex.Core.RT.DataCenter;
  9. using Aitex.Core.RT.Event;
  10. using Aitex.Core.RT.IOCore;
  11. using Aitex.Core.RT.Log;
  12. using Aitex.Core.RT.OperationCenter;
  13. using Aitex.Core.RT.SCCore;
  14. using Aitex.Core.RT.Tolerance;
  15. using Aitex.Core.Util;
  16. using MECF.Framework.Common.Communications;
  17. using MECF.Framework.Common.DataCenter;
  18. using MECF.Framework.Common.Device.Bases;
  19. using MECF.Framework.Common.Equipment;
  20. using VirgoCommon;
  21. using VirgoRT.Modules;
  22. namespace VirgoRT.Devices
  23. {
  24. static class RevtechMatchMessage
  25. {
  26. public const string QUERY_STATE_INFORMATION = "MATCH:FETCH?";
  27. public const string SET_C1_POS = "MATCH:POS:C1";
  28. public const string SET_C2_POS = "MATCH:POS:C2";
  29. public const string SET_WORK_MODE = "MATCH:MODE";
  30. public const string PRE_SET = "MATCH:PSET:SET 00";
  31. }
  32. public enum MatchCommunicationType
  33. {
  34. RS232,
  35. Ethernet
  36. }
  37. class RevtechMatch : RfMatchBase
  38. {
  39. private AsyncSocketDevice _socket;
  40. private MatchCommunicationType _matchCommunicationType;
  41. private string _address;
  42. private readonly DeviceTimer _timerQueryStatus = new DeviceTimer();
  43. private int QUERY_INTERVAL = 1000;
  44. private Dictionary<string, string> _nameMap = new Dictionary<string, string>() {
  45. {VirgoDevice.Match.ToString(), "match" },
  46. {VirgoDevice.BiasMatch.ToString(),VirgoDevice.BiasRf.ToString() }
  47. };
  48. private Dictionary<string, string> _modeMap = new Dictionary<string, string>() {
  49. {"Preset", EnumRfMatchTuneMode.Auto.ToString() },
  50. {"Hold",EnumRfMatchTuneMode.Manual.ToString() }
  51. };
  52. [Subscription("MatchWorkMode")]
  53. public EnumRfMatchTuneMode WorkMode { get; set; }
  54. public float C1 { get; set; }
  55. public float C2 { get; set; }
  56. [Subscription("VPP")]
  57. public ushort VPP { get; set; }
  58. public override AITRfMatchData DeviceData
  59. {
  60. get
  61. {
  62. return new AITRfMatchData
  63. {
  64. };
  65. }
  66. }
  67. public RevtechMatch(ModuleName mod, string name, string configName = "") : base(mod.ToString(), name)
  68. {
  69. _address = SC.GetStringValue($"{mod}.{(string.IsNullOrWhiteSpace(configName) ? name : configName)}.IPAddress");
  70. _socket = new AsyncSocketDevice(_address);
  71. _socket.OnDataChanged += new AsyncSocketDevice.MessageHandler(OnDataChanged);
  72. _socket.OnErrorHappened += _socket_OnErrorHappened;
  73. }
  74. private void _socket_OnErrorHappened(ErrorEventArgsDevice args)
  75. {
  76. LOG.Error($"{Module} {Name} Error {args.Reason}");
  77. }
  78. private void OnDataChanged(byte[] obj)
  79. {
  80. try
  81. {
  82. string data = System.Text.Encoding.ASCII.GetString(obj);
  83. if (data.Length < 10 && data.Length > 20)
  84. {
  85. return;
  86. }
  87. string[] matchData = data.Split(',');
  88. if (matchData.Length > 13)
  89. {
  90. if (matchData[0].Contains("MANUAL") || matchData[0].Contains("AUTO"))
  91. {
  92. WorkMode = matchData[0] == "MANUAL" ? EnumRfMatchTuneMode.Manual : EnumRfMatchTuneMode.Auto;
  93. TunePosition1 = Convert.ToSingle(matchData[8]);
  94. TunePosition2 = Convert.ToSingle(matchData[7]);
  95. VPP = Convert.ToUInt16(matchData[12], 16);
  96. DCBias = Convert.ToSingle(matchData[13]);
  97. }
  98. }
  99. }
  100. catch
  101. {
  102. }
  103. }
  104. public override bool Initialize()
  105. {
  106. base.Initialize();
  107. LOG.Info($"{Module} {Name} Revtech match address:[{_address}] connect");
  108. _socket?.Connect(_address);
  109. DATA.Subscribe($"{Module}.{Name}.C1", () => TunePosition1);
  110. DATA.Subscribe($"{Module}.{Name}.C2", () => TunePosition2);
  111. DATA.Subscribe($"{Module}.{Name}.MatchProcessMode", () => (int)WorkMode);
  112. OP.Subscribe($"{Module}.{Name}.SetC1", (func, args) =>
  113. {
  114. return true;
  115. });
  116. OP.Subscribe($"{Module}.{Name}.SetC2", (func, args) =>
  117. {
  118. return true;
  119. });
  120. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) =>
  121. {
  122. SetMatchPositionC1((float)Convert.ToDouble(param[0]), out reason);
  123. return true;
  124. });
  125. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) =>
  126. {
  127. SetMatchPositionC2((float)Convert.ToDouble(param[0]), out reason);
  128. return true;
  129. });
  130. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) =>
  131. {
  132. SetMatchPosition((float)Convert.ToDouble(param[0]), (float)Convert.ToDouble(param[1]), out reason);
  133. return true;
  134. });
  135. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
  136. {
  137. var mode = EnumRfMatchTuneMode.Auto.ToString();
  138. if (_modeMap.ContainsKey((string)param[0]))
  139. mode = _modeMap[(string)param[0]];
  140. SetMatchMode(mode, out reason);
  141. return true;
  142. });
  143. OP.Subscribe($"{Module}.{_nameMap[Name]}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
  144. {
  145. var mode = EnumRfMatchTuneMode.Auto.ToString();
  146. if (_modeMap.ContainsKey((string)param[0]))
  147. mode = _modeMap[(string)param[0]];
  148. SetMatchMode(mode, out reason);
  149. return true;
  150. });
  151. OP.Subscribe($"{Module}.{_nameMap[Name]}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) =>
  152. {
  153. SetMatchPositionC1((float)Convert.ToDouble(param[0]), out reason);
  154. return true;
  155. });
  156. OP.Subscribe($"{Module}.{_nameMap[Name]}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) =>
  157. {
  158. SetMatchPositionC2((float)Convert.ToDouble(param[0]), out reason);
  159. return true;
  160. });
  161. _timerQueryStatus.Start(QUERY_INTERVAL);
  162. return true;
  163. }
  164. public override void Monitor()
  165. {
  166. try
  167. {
  168. if (_timerQueryStatus.IsTimeout())
  169. {
  170. this.SendCmd(RevtechMatchMessage.QUERY_STATE_INFORMATION);
  171. _timerQueryStatus.Start(QUERY_INTERVAL);
  172. }
  173. }
  174. catch (Exception ex)
  175. {
  176. LOG.Write(ex);
  177. }
  178. }
  179. public override void Terminate()
  180. {
  181. }
  182. public override void Reset()
  183. {
  184. }
  185. public override void SetMatchPosition(float c1, float c2, out string reason)
  186. {
  187. reason = "";
  188. base.SetMatchPosition(c1, c2, out reason);
  189. ExecuteMatchPostion(c1, c2);
  190. }
  191. public override bool SetMatchMode(string mode, out string reason)
  192. {
  193. reason = string.Empty;
  194. if (mode == EnumRfMatchTuneMode.Manual.ToString())
  195. SetWorkMode(EnumRfMatchTuneMode.Manual);
  196. else
  197. SetWorkMode(EnumRfMatchTuneMode.Auto);
  198. return true;
  199. }
  200. public override void SetMatchPositionC1(float c1, out string reason)
  201. {
  202. reason = string.Empty;
  203. LoadPosition1 = c1;
  204. base.SetMatchPositionC1(c1, out reason);
  205. ushort val1 = (ushort)(c1);
  206. SendCmd(RevtechMatchMessage.SET_C1_POS + $" {val1.ToString("X3")}");
  207. }
  208. public override void SetMatchPositionC2(float c2, out string reason)
  209. {
  210. reason = string.Empty;
  211. LoadPosition2 = c2;
  212. base.SetMatchPositionC2(c2,out reason);
  213. ushort val2 = (ushort)(c2);
  214. SendCmd(RevtechMatchMessage.SET_C2_POS + $" {val2.ToString("X3")}");
  215. }
  216. private void ExecuteMatchPostion(float c1, float c2)
  217. {
  218. SetPosition(c1, c2);
  219. }
  220. private void SetPosition(float c1val, float c2val)
  221. {
  222. LoadPosition1 = c1val;
  223. LoadPosition2 = c2val;
  224. ushort val1 = (ushort)(c1val);
  225. ushort val2 = (ushort)(c2val);
  226. SendCmd(RevtechMatchMessage.SET_C1_POS + $" {val1.ToString("X3")}");
  227. SendCmd(RevtechMatchMessage.SET_C2_POS + $" {val2.ToString("X3")}");
  228. }
  229. private void SetWorkMode(EnumRfMatchTuneMode mode)
  230. {
  231. if (mode == EnumRfMatchTuneMode.Auto)
  232. {
  233. SendCmd("MATCH:MODE HAUTO");
  234. }
  235. else if (mode == EnumRfMatchTuneMode.Manual)
  236. {
  237. SendCmd("MATCH:MODE MANUAL");
  238. }
  239. }
  240. private void SendCmd(string str)
  241. {
  242. if(!str.Contains(RevtechMatchMessage.QUERY_STATE_INFORMATION))
  243. {
  244. LOG.Write($"{Module} {Name} Revtech match send {str}");
  245. }
  246. _socket?.Write(Encoding.ASCII.GetBytes(str + "\n"));
  247. //EV.PostInfoLog(Module.ToString(), $"Revtech match send [{str}]");
  248. }
  249. }
  250. }