RevtechMatch.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 string 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,
  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);
  67. }
  68. else
  69. {
  70. _serial = new AsyncSerialPort(portNum, 115200, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\n", false);
  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 = 100;
  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}.DeviceData", () => DeviceData);
  118. OP.Subscribe($"{Module}.{Name}.SetC1", (func, args) =>
  119. {
  120. return true;
  121. });
  122. OP.Subscribe($"{Module}.{Name}.SetC2", (func, args) =>
  123. {
  124. return true;
  125. });
  126. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) =>
  127. {
  128. SetMatchPositionC1((float)Convert.ToDouble(param[0]), out reason);
  129. return true;
  130. });
  131. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) =>
  132. {
  133. SetMatchPositionC2((float)Convert.ToDouble(param[0]), out reason);
  134. return true;
  135. });
  136. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) =>
  137. {
  138. SetMatchPosition((float)Convert.ToDouble(param[0]), (float)Convert.ToDouble(param[1]), out reason);
  139. return true;
  140. });
  141. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
  142. {
  143. SetMatchMode((string)param[0] == "Auto" ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual, out reason);
  144. return true;
  145. });
  146. return true;
  147. }
  148. private void OnDataChanged(byte[] obj)
  149. {
  150. try
  151. {
  152. string data = System.Text.Encoding.ASCII.GetString(obj);
  153. if (data.Length < 10 && data.Length > 20)
  154. {
  155. return;
  156. }
  157. string[] matchData = data.Split(',');
  158. if (matchData.Length > 9)
  159. {
  160. if (matchData[0].Contains("MANUAL") || matchData[0].Contains("AUTO"))
  161. {
  162. WorkMode = matchData[0];
  163. TunePosition1 = Convert.ToSingle(matchData[8]);
  164. TunePosition2 = Convert.ToSingle(matchData[7]);
  165. Vpp = matchData[12];
  166. DCBias = Convert.ToSingle(matchData[13]);
  167. }
  168. }
  169. }
  170. catch
  171. {
  172. }
  173. }
  174. private void RevtechMatch_sendDataChangedEvent(string obj)
  175. {
  176. if ((_matchCommunicationType == MatchCommunicationType.Ethernet) && _socket.IsConnected)
  177. {
  178. byte[] value = Encoding.ASCII.GetBytes(obj);
  179. _socket?.Write(value);
  180. }
  181. else if ((_matchCommunicationType == MatchCommunicationType.RS232 && _serial?.IsOpen() == true))
  182. {
  183. _serial?.Write(obj);
  184. }
  185. }
  186. public override void SetMatchPositionC1(float c1, out string reason)
  187. {
  188. base.SetMatchPositionC1(c1, out reason);
  189. SetWorkMode(EnumRfMatchTuneMode.Manual);
  190. c1SetPoint = c1;
  191. SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C1_POS} {c1}\n");
  192. }
  193. public override void SetMatchPosition(float c1, float c2, out string reason)
  194. {
  195. base.SetMatchPosition(c1, c2, out reason);
  196. executeMatchPostion(c1, c2);
  197. c1SetPoint = c1;
  198. c2SetPoint = c2;
  199. reason = "";
  200. }
  201. private void executeMatchPostion(float c1, float c2)
  202. {
  203. SetWorkMode(EnumRfMatchTuneMode.Manual);
  204. SetPosition(c1, c2);
  205. //SetWorkMode(EnumRfMatchTuneMode.Auto);
  206. }
  207. private void SetPosition(float c1val, float c2val)
  208. {
  209. SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C1_POS} {c1val}\n");
  210. SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C2_POS} {c2val}\n");
  211. }
  212. public override bool SetMatchMode(EnumRfMatchTuneMode enumRfMatchTuneMode, out string reason)
  213. {
  214. reason = string.Empty;
  215. SetWorkMode(enumRfMatchTuneMode);
  216. return true;
  217. }
  218. private void SetWorkMode(EnumRfMatchTuneMode mode)
  219. {
  220. if (mode == EnumRfMatchTuneMode.Auto)
  221. {
  222. SetPointCommandQueue.Add("MATCH:MODE HAUTO\n");
  223. }
  224. else if (mode == EnumRfMatchTuneMode.Manual)
  225. {
  226. SetPointCommandQueue.Add("MATCH:MODE MANUAL\n");
  227. }
  228. }
  229. public override void Monitor()
  230. {
  231. }
  232. public override bool ReConnect()
  233. {
  234. if (_matchCommunicationType == MatchCommunicationType.RS232)
  235. {
  236. return _serial.ReConnect();
  237. }
  238. else if (_matchCommunicationType == MatchCommunicationType.Ethernet)
  239. {
  240. //var ip = SC.GetStringValue($"{Module}.{_device}.IPAddress");
  241. return _socket.ReConnect();
  242. }
  243. return false;
  244. }
  245. }
  246. }