RevtechMatch.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.Common.Equipment;
  11. using System;
  12. using System.Collections.Concurrent;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Runtime.InteropServices;
  16. using System.Text;
  17. namespace JetVirgoPM.Devices
  18. {
  19. static class RevtechMatchMessage
  20. {
  21. public const string QUERY_STATE_INFORMATION = "MATCH:FETCH?\n";
  22. public const string SET_C1_POS = "MATCH:POS:C1";
  23. public const string SET_C2_POS = "MATCH:POS:C2";
  24. public const string SET_WORK_MODE = "MATCH:MODE";
  25. public const string PRE_SET = "MATCH:PSET:SET 00\n";
  26. }
  27. public enum MatchCommunicationType
  28. {
  29. RS232 = 1,
  30. Ethernet = 2
  31. }
  32. public class RevtechMatch : RfMatchBase
  33. {
  34. private AsyncSocketDevice _socket;
  35. private AsyncSerialPort _serial;
  36. private MatchCommunicationType _matchCommunicationType;
  37. private string _address;
  38. private string _port;
  39. [Subscription("MatchWorkMode")]
  40. public EnumRfMatchTuneMode WorkMode { get; set; }
  41. public List<string> SerachCommandList;
  42. private BlockingCollection<string> SetPointCommandQueue = new BlockingCollection<string>();
  43. [Subscription("VPP")]
  44. public float Vpp { get; set; }
  45. private float c1SetPoint;
  46. private float c2SetPoint;
  47. private readonly DeviceTimer _timerQueryStatus = new DeviceTimer();
  48. private readonly DeviceTimer _timerQuery = new DeviceTimer();
  49. private int QUERY_INTERVAL = 1000;
  50. public new AITRfData DeviceData =>
  51. new AITRfData
  52. {
  53. Module = Module,
  54. DeviceName = Name,
  55. WorkMode = (int)WorkMode,
  56. MatchPositionC1 = TunePosition1,
  57. MatchPositionC2 = TunePosition2,
  58. //DCBias = DCBias.ToString(),
  59. //C1SetPoint = c1SetPoint,
  60. //C2SetPoint = c2SetPoint,
  61. };
  62. public RevtechMatch(ModuleName mod, string name) : base(mod.ToString(), name)
  63. {
  64. _matchCommunicationType = (MatchCommunicationType)SC.GetValue<int>($"{mod}.{name}.CommunicationType");
  65. TunePosition1 = -1;
  66. TunePosition2 = -1;
  67. if (_matchCommunicationType == MatchCommunicationType.RS232)
  68. {
  69. var portNum = SC.GetStringValue($"{mod}.{name}.Port");
  70. _port = portNum;
  71. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  72. {
  73. _serial = new AsyncSerialPort(portNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\n", true);
  74. }
  75. else
  76. {
  77. _serial = new AsyncSerialPort(portNum, 115200, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\n", true);
  78. }
  79. }
  80. else if (_matchCommunicationType == MatchCommunicationType.Ethernet)
  81. {
  82. _address = SC.GetStringValue($"{mod}.{name}.IPAddress");
  83. _socket = new AsyncSocketDevice(_address,"\n");
  84. EV.PostInfoLog(mod.ToString(), "match初始化 建立连接");
  85. _socket.OnDataChanged += new AsyncSocketDevice.MessageHandler(OnDataChanged);
  86. _socket.OnErrorHappened += _socket_OnErrorHappened;
  87. _socket.Connect(_address);
  88. }
  89. SerachCommandList = new List<string>()
  90. {
  91. RevtechMatchMessage.QUERY_STATE_INFORMATION
  92. };
  93. //intervalTime = 100;
  94. //sendDataChangedEvent += RevtechMatch_sendDataChangedEvent;
  95. //baseStopwatch.Start();
  96. //baseTimer.Enabled = true;
  97. }
  98. ~RevtechMatch()
  99. {
  100. _serial?.Close();
  101. }
  102. private void _socket_OnErrorHappened(ErrorEventArgsDevice args)
  103. {
  104. EV.PostAlarmLog(Module, $"{Module} {Name} Error {args.Reason}");
  105. }
  106. public override bool Initialize()
  107. {
  108. base.Initialize();
  109. if (_matchCommunicationType == MatchCommunicationType.RS232)
  110. {
  111. if (_serial != null && _serial.Open())
  112. {
  113. _serial.OnBinaryDataChanged += OnDataChanged;
  114. EV.PostInfoLog(Module, $"{Name} 串口成功打开");
  115. }
  116. else
  117. {
  118. EV.PostInfoLog(Module, $"{Name} 串口无法打开");
  119. }
  120. }
  121. else if (_matchCommunicationType == MatchCommunicationType.Ethernet)
  122. {
  123. EV.PostInfoLog(Module, $"{Name} 网口准备打开");
  124. _socket?.Connect(_address);
  125. if(_socket.IsConnected)
  126. EV.PostInfoLog(Module, $"{Name} 网口打开");
  127. }
  128. DATA.Subscribe($"{Module}.{Name}.C1", () => TunePosition1);
  129. DATA.Subscribe($"{Module}.{Name}.C2", () => TunePosition2);
  130. DATA.Subscribe($"{Module}.{Name}.WorkMode", () => WorkMode);
  131. DATA.Subscribe($"{Module}.{Name}.Vpp", () => Vpp);
  132. DATA.Subscribe($"{Module}.{Name}.DCBias", () => DCBias);
  133. //DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  134. OP.Subscribe($"{Module}.{Name}.SetC1", (func, args) =>
  135. {
  136. return true;
  137. });
  138. OP.Subscribe($"{Module}.{Name}.SetC2", (func, args) =>
  139. {
  140. return true;
  141. });
  142. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) =>
  143. {
  144. SetMatchPositionC1((float)Convert.ToDouble(param[0]), out reason);
  145. return true;
  146. });
  147. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) =>
  148. {
  149. SetMatchPositionC2((float)Convert.ToDouble(param[0]), out reason);
  150. return true;
  151. });
  152. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) =>
  153. {
  154. SetMatchPosition((float)Convert.ToDouble(param[0]), (float)Convert.ToDouble(param[1]), out reason);
  155. return true;
  156. });
  157. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
  158. {
  159. SetMatchMode((string)param[0], out reason);
  160. return true;
  161. });
  162. _timerQueryStatus.Start(QUERY_INTERVAL);
  163. _timerQuery.Start(QUERY_INTERVAL);
  164. return true;
  165. }
  166. private void OnDataChanged(byte[] obj)
  167. {
  168. try
  169. {
  170. string data = System.Text.Encoding.ASCII.GetString(obj);
  171. if (data.Length < 10 && data.Length > 20)
  172. {
  173. return;
  174. }
  175. string[] matchData = data.Split(new char[] { ',' });
  176. if (matchData.Length > 13)
  177. {
  178. if (matchData[0].Contains("MANUAL") || matchData[0].Contains("AUTO"))
  179. {
  180. if (matchData[0].Contains("MANUAL"))
  181. WorkMode = EnumRfMatchTuneMode.Manual;
  182. else
  183. WorkMode = EnumRfMatchTuneMode.Auto;
  184. TunePosition1 = Convert.ToSingle(matchData[8]);
  185. TunePosition2 = Convert.ToSingle(matchData[7]);
  186. Vpp = Convert.ToSingle(matchData[12]);
  187. DCBias = Convert.ToSingle(matchData[13]);
  188. }
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. LOG.Write(ex);
  194. }
  195. }
  196. private void RevtechMatch_sendDataChangedEvent(string obj)
  197. {
  198. if ((_matchCommunicationType == MatchCommunicationType.Ethernet) && _socket.IsConnected)
  199. {
  200. byte[] value = Encoding.ASCII.GetBytes(obj);
  201. _socket?.Write(value);
  202. }
  203. }
  204. public override void SetMatchPositionC1(float c1, out string reason)
  205. {
  206. base.SetMatchPositionC1(c1, out reason);
  207. //SetWorkMode(EnumRfMatchTuneMode.Manual);
  208. //c1SetPoint = c1;
  209. //SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C1_POS} {c1}\n");
  210. }
  211. public override void SetMatchPosition(float c1, float c2, out string reason)
  212. {
  213. //base.SetMatchPosition(c1, c2, out reason);
  214. //executeMatchPostion(c1, c2);
  215. //c1SetPoint = c1;
  216. //c2SetPoint = c2;
  217. reason = "";
  218. }
  219. private void executeMatchPostion(float c1, float c2)
  220. {
  221. SetWorkMode(EnumRfMatchTuneMode.Manual);
  222. SetPosition(c1, c2);
  223. //SetWorkMode(EnumRfMatchTuneMode.Auto);
  224. }
  225. private void SetPosition(float c1val, float c2val)
  226. {
  227. SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C1_POS} {c1val}\n");
  228. SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C2_POS} {c2val}\n");
  229. }
  230. public override bool SetMatchMode(string mode, out string reason)
  231. {
  232. reason = string.Empty;
  233. //if (!Enum.TryParse<EnumRfMatchTuneMode>(mode,out EnumRfMatchTuneMode enumRfMatchTuneMode))
  234. //{
  235. // return false;
  236. //}
  237. SetWorkMode(EnumRfMatchTuneMode.Auto);
  238. return true;
  239. }
  240. private void SetWorkMode(EnumRfMatchTuneMode mode)
  241. {
  242. if (mode == EnumRfMatchTuneMode.Auto)
  243. {
  244. SetPointCommandQueue.Add("MATCH:MODE HAUTO\n");
  245. }
  246. else if (mode == EnumRfMatchTuneMode.Manual)
  247. {
  248. SetPointCommandQueue.Add("MATCH:MODE MANUAL\n");
  249. }
  250. }
  251. public override void Monitor()
  252. {
  253. try
  254. {
  255. if (_timerQueryStatus.IsTimeout())
  256. {
  257. if (SetPointCommandQueue.Count > 0)
  258. {
  259. string value;
  260. if (SetPointCommandQueue.TryTake(out value))
  261. {
  262. value += "\n";
  263. byte[] obj = Encoding.ASCII.GetBytes(value);
  264. _socket.Write(obj);
  265. EV.PostInfoLog(Module, $"{obj}");
  266. }
  267. }
  268. _timerQueryStatus.Restart(QUERY_INTERVAL);
  269. }
  270. if (_timerQuery.IsTimeout())
  271. {
  272. RevtechMatch_sendDataChangedEvent(RevtechMatchMessage.QUERY_STATE_INFORMATION);
  273. _timerQuery.Restart(QUERY_INTERVAL);
  274. }
  275. }
  276. catch (Exception ex)
  277. {
  278. LOG.Write(ex);
  279. }
  280. }
  281. //public override bool ReConnect()
  282. //{
  283. // if (_matchCommunicationType == MatchCommunicationType.RS232)
  284. // {
  285. // return _serial.ReConnect();
  286. // }
  287. // else if (_matchCommunicationType == MatchCommunicationType.Ethernet)
  288. // {
  289. // return _socket.ReConnect(_address);
  290. // }
  291. // return false;
  292. //}
  293. }
  294. }