RevtechMatch.cs 10 KB

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