RevtechMatch.cs 9.5 KB

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