RevtechMatch.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.OperationCenter;
  5. using Aitex.Core.RT.SCCore;
  6. using MECF.Framework.Common.Communications;
  7. using MECF.Framework.Common.Device.Bases;
  8. using MECF.Framework.Common.Equipment;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Runtime.InteropServices;
  13. using System.Text;
  14. using Venus_Core;
  15. namespace Venus_RT.Devices
  16. {
  17. static class RevtechMatchMessage
  18. {
  19. public const string QUERY_STATE_INFORMATION = "MATCH:FETCH?\n";
  20. public const string SET_C1_POS = "MATCH:POS:C1";
  21. public const string SET_C2_POS = "MATCH:POS:C2";
  22. public const string SET_WORK_MODE = "MATCH:MODE";
  23. public const string PRE_SET = "MATCH:PSET:SET 00\n";
  24. }
  25. public enum MatchCommunicationType
  26. {
  27. RS232,
  28. Ethernet
  29. }
  30. public class RevtechMatch : RfMatchBase
  31. {
  32. private AsyncSocketDevice _socket;
  33. private AsyncSerialPort _serial;
  34. private MatchCommunicationType _matchCommunicationType;
  35. private string _address;
  36. private string _port;
  37. public string WorkMode { get; set; } = "";
  38. public float Vpp { get; set; }
  39. private float c1SetPoint;
  40. private float c2SetPoint;
  41. private VenusDevice _device;
  42. public new AITMatchData DeviceData =>
  43. new AITMatchData
  44. {
  45. Module = Module,
  46. DeviceName = Name,
  47. WorkMode = WorkMode,
  48. C1 = TunePosition1,
  49. C2 = TunePosition2,
  50. VPP = Vpp.ToString(),
  51. DCBias = DCBias.ToString(),
  52. C1SetPoint = c1SetPoint,
  53. C2SetPoint = c2SetPoint,
  54. };
  55. public RevtechMatch(ModuleName mod, VenusDevice venusDevice, MatchCommunicationType matchCommunicationType) : base(mod.ToString(), venusDevice.ToString())
  56. {
  57. _device = venusDevice;
  58. //var test = System.IO.Ports.SerialPort.GetPortNames();
  59. _matchCommunicationType = matchCommunicationType;
  60. if (matchCommunicationType == MatchCommunicationType.RS232)
  61. {
  62. var portNum = SC.GetStringValue($"{mod}.{venusDevice}.Port");
  63. _port = portNum;
  64. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  65. {
  66. _serial = new AsyncSerialPort(portNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\n", true);
  67. }
  68. else
  69. {
  70. _serial = new AsyncSerialPort(portNum, 115200, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\n", true);
  71. }
  72. }
  73. else if (matchCommunicationType == MatchCommunicationType.Ethernet)
  74. {
  75. _address = SC.GetStringValue($"{mod}.{venusDevice}.IPAddress");
  76. _socket = new AsyncSocketDevice(_address);
  77. _socket.OnDataChanged += new AsyncSocketDevice.MessageHandler(OnDataChanged);
  78. _socket.OnErrorHappened += _socket_OnErrorHappened;
  79. }
  80. SerachCommandList = new List<string>()
  81. {
  82. RevtechMatchMessage.QUERY_STATE_INFORMATION
  83. };
  84. intervalTime = 300;
  85. sendDataChangedEvent += RevtechMatch_sendDataChangedEvent;
  86. baseStopwatch.Start();
  87. baseTimer.Enabled = true;
  88. }
  89. private void _socket_OnErrorHappened(ErrorEventArgsDevice args)
  90. {
  91. LOG.Write(eEvent.ERR_MATCH, Module, $"{Module} {Name} Error {args.Reason}");
  92. }
  93. public override bool Initialize()
  94. {
  95. base.Initialize();
  96. if (_matchCommunicationType == MatchCommunicationType.RS232)
  97. {
  98. if (_serial != null && _serial.Open())
  99. {
  100. _serial.OnBinaryDataChanged += OnDataChanged;
  101. LOG.Write(eEvent.INFO_MATCH, Module, $"{Name} 串口成功打开");
  102. }
  103. else
  104. {
  105. LOG.Write(eEvent.ERR_MATCH, Module, $"{Name} 串口无法打开");
  106. }
  107. }
  108. else if (_matchCommunicationType == MatchCommunicationType.Ethernet)
  109. {
  110. _socket?.Connect(_address);
  111. }
  112. DATA.Subscribe($"{Module}.{Name}.C1", () => TunePosition1);
  113. DATA.Subscribe($"{Module}.{Name}.C2", () => TunePosition2);
  114. DATA.Subscribe($"{Module}.{Name}.WorkMode", () => WorkMode);
  115. DATA.Subscribe($"{Module}.{Name}.Vpp", () => Vpp);
  116. DATA.Subscribe($"{Module}.{Name}.DCBias", () => DCBias);
  117. DATA.Subscribe($"{Module}.{Name}.C1SetPoint", () => c1SetPoint, Aitex.Core.Util.SubscriptionAttribute.FLAG.IgnoreSaveDB);
  118. DATA.Subscribe($"{Module}.{Name}.C2SetPoint", () => c2SetPoint, Aitex.Core.Util.SubscriptionAttribute.FLAG.IgnoreSaveDB);
  119. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  120. OP.Subscribe($"{Module}.{Name}.SetC1", (func, args) =>
  121. {
  122. return true;
  123. });
  124. OP.Subscribe($"{Module}.{Name}.SetC2", (func, args) =>
  125. {
  126. return true;
  127. });
  128. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) =>
  129. {
  130. SetMatchPositionC1((float)Convert.ToDouble(param[0]), out reason);
  131. return true;
  132. });
  133. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) =>
  134. {
  135. SetMatchPositionC2((float)Convert.ToDouble(param[0]), out reason);
  136. return true;
  137. });
  138. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) =>
  139. {
  140. SetMatchPosition((float)Convert.ToDouble(param[0]), (float)Convert.ToDouble(param[1]), out reason);
  141. return true;
  142. });
  143. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
  144. {
  145. SetMatchMode((string)param[0] == "Auto" ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual, out reason);
  146. return true;
  147. });
  148. return true;
  149. }
  150. private void OnDataChanged(byte[] obj)
  151. {
  152. try
  153. {
  154. string data = System.Text.Encoding.ASCII.GetString(obj);
  155. if (data.Length < 10 && data.Length > 20)
  156. {
  157. return;
  158. }
  159. string[] matchData = data.Split(new char[] { ',' });
  160. if (matchData.Length > 13)
  161. {
  162. if (matchData[0].Contains("MANUAL") || matchData[0].Contains("AUTO"))
  163. {
  164. WorkMode = matchData[0];
  165. TunePosition1 = Convert.ToSingle(matchData[8]);
  166. TunePosition2 = Convert.ToSingle(matchData[7]);
  167. Vpp = Convert.ToSingle(matchData[12]);
  168. DCBias = Convert.ToSingle(matchData[13]);
  169. }
  170. }
  171. }
  172. catch
  173. {
  174. }
  175. }
  176. private void RevtechMatch_sendDataChangedEvent(string obj)
  177. {
  178. if ((_matchCommunicationType == MatchCommunicationType.Ethernet) && _socket.IsConnected)
  179. {
  180. byte[] value = Encoding.ASCII.GetBytes(obj);
  181. _socket?.Write(value);
  182. }
  183. else if ((_matchCommunicationType == MatchCommunicationType.RS232 && _serial?.IsOpen() == true))
  184. {
  185. _serial?.Write(obj);
  186. }
  187. }
  188. public override void SetMatchPositionC1(float c1, out string reason)
  189. {
  190. base.SetMatchPositionC1(c1, out reason);
  191. SetWorkMode(EnumRfMatchTuneMode.Manual);
  192. c1SetPoint = c1;
  193. double value;
  194. if (Name == "RFBox")
  195. {
  196. var aValue = SC.GetValue<int>($"{Module}.RFBox.CalculationParameterA");
  197. var kValue = SC.GetValue<double>($"{Module}.RFBox.CalculationParameterK");
  198. value = Math.Round((c1 + aValue) / kValue,1);
  199. if (value < 0)
  200. {
  201. value = 0;
  202. }
  203. if (value > 100)
  204. {
  205. value = 100;
  206. }
  207. }
  208. else
  209. {
  210. value = c1;
  211. }
  212. SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C1_POS} {value}\n");
  213. }
  214. public override void SetMatchPosition(float c1, float c2, out string reason)
  215. {
  216. base.SetMatchPosition(c1, c2, out reason);
  217. executeMatchPostion(c1, c2);
  218. c1SetPoint = c1;
  219. c2SetPoint = c2;
  220. //reason = "";
  221. }
  222. private void executeMatchPostion(float c1, float c2)
  223. {
  224. SetWorkMode(EnumRfMatchTuneMode.Manual);
  225. SetPosition(c1, c2);
  226. //SetWorkMode(EnumRfMatchTuneMode.Auto);
  227. }
  228. private void SetPosition(float c1val, float c2val)
  229. {
  230. SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C1_POS} {c1val}\n");
  231. SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C2_POS} {c2val}\n");
  232. }
  233. public override bool SetMatchMode(EnumRfMatchTuneMode enumRfMatchTuneMode, out string reason)
  234. {
  235. reason = string.Empty;
  236. SetWorkMode(enumRfMatchTuneMode);
  237. return true;
  238. }
  239. private void SetWorkMode(EnumRfMatchTuneMode mode)
  240. {
  241. if (mode == EnumRfMatchTuneMode.Auto)
  242. {
  243. SetPointCommandQueue.Add("MATCH:MODE HAUTO\n");
  244. }
  245. else if (mode == EnumRfMatchTuneMode.Manual)
  246. {
  247. SetPointCommandQueue.Add("MATCH:MODE MANUAL\n");
  248. }
  249. }
  250. public override void Monitor()
  251. {
  252. }
  253. public override bool ReConnect()
  254. {
  255. if (_matchCommunicationType == MatchCommunicationType.RS232)
  256. {
  257. return _serial.ReConnect();
  258. }
  259. else if (_matchCommunicationType == MatchCommunicationType.Ethernet)
  260. {
  261. return _socket.ReConnect(_address);
  262. }
  263. return false;
  264. }
  265. }
  266. }