RevtechMatch.cs 7.9 KB

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