RevtechMatch.cs 10 KB

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