RevtechMatch.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. intervalTime = 100;
  63. sendDataChangedEvent += RevtechMatch_sendDataChangedEvent;
  64. baseStopwatch.Start();
  65. baseTimer.Enabled = true;
  66. }
  67. private void _socket_OnErrorHappened(ErrorEventArgsDevice args)
  68. {
  69. LOG.Write(eEvent.ERR_MATCH, Module, $"{Module} Revtech Match Error {args.Reason}");
  70. }
  71. public override bool Initialize()
  72. {
  73. base.Initialize();
  74. if (_matchCommunicationType == MatchCommunicationType.RS232)
  75. {
  76. if (_serial != null && _serial.Open())
  77. {
  78. _serial.OnBinaryDataChanged += OnDataChanged;
  79. LOG.Write(eEvent.INFO_MATCH, Module, $"{Name} 串口成功打开");
  80. }
  81. else
  82. {
  83. LOG.Write(eEvent.ERR_MATCH, Module, $"{Name} 串口无法打开");
  84. }
  85. }
  86. else if (_matchCommunicationType == MatchCommunicationType.Ethernet)
  87. {
  88. _socket?.Connect(_address);
  89. }
  90. DATA.Subscribe($"{Module}.{Name}.C1", () => TunePosition1);
  91. DATA.Subscribe($"{Module}.{Name}.C2", () => TunePosition2);
  92. DATA.Subscribe($"{Module}.{Name}.WorkMode", () => WorkMode);
  93. DATA.Subscribe($"{Module}.{Name}.Vpp", () => Vpp);
  94. DATA.Subscribe($"{Module}.{Name}.DCBias", () => DCBias);
  95. OP.Subscribe($"{Module}.{Name}.SetC1", (func, args) =>
  96. {
  97. return true;
  98. });
  99. OP.Subscribe($"{Module}.{Name}.SetC2", (func, args) =>
  100. {
  101. return true;
  102. });
  103. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) =>
  104. {
  105. SetMatchPositionC1((float)Convert.ToDouble(param[0]), out reason);
  106. return true;
  107. });
  108. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) =>
  109. {
  110. SetMatchPositionC2((float)Convert.ToDouble(param[0]), out reason);
  111. return true;
  112. });
  113. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) =>
  114. {
  115. SetMatchPosition((float)Convert.ToDouble(param[0]), (float)Convert.ToDouble(param[1]), out reason);
  116. return true;
  117. });
  118. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
  119. {
  120. SetMatchMode((string)param[0] == "Auto" ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual, out reason);
  121. return true;
  122. });
  123. return true;
  124. }
  125. private void OnDataChanged(byte[] obj)
  126. {
  127. try
  128. {
  129. string data = System.Text.Encoding.ASCII.GetString(obj);
  130. if (data.Length < 10 && data.Length > 20)
  131. {
  132. return;
  133. }
  134. string[] matchData = data.Split(',');
  135. if (matchData.Length > 9)
  136. {
  137. if (matchData[0].Contains("MANUAL") || matchData[0].Contains("AUTO"))
  138. {
  139. WorkMode = matchData[0];
  140. TunePosition1 = Convert.ToSingle(matchData[8]);
  141. TunePosition2 = Convert.ToSingle(matchData[7]);
  142. Vpp = matchData[12];
  143. DCBias = Convert.ToSingle(matchData[13]);
  144. }
  145. }
  146. }
  147. catch
  148. {
  149. }
  150. }
  151. private void RevtechMatch_sendDataChangedEvent(string obj)
  152. {
  153. if ((_matchCommunicationType == MatchCommunicationType.Ethernet))
  154. {
  155. byte[] value = Encoding.ASCII.GetBytes(obj);
  156. _socket?.Write(value);
  157. }
  158. else if ((_matchCommunicationType == MatchCommunicationType.RS232 && _serial?.IsOpen() == true))
  159. {
  160. _serial?.Write(obj);
  161. }
  162. }
  163. public override void SetMatchPosition(float c1, float c2, out string reason)
  164. {
  165. base.SetMatchPosition(c1, c2, out reason);
  166. executeMatchPostion(c1, c2);
  167. reason = "";
  168. }
  169. private void executeMatchPostion(float c1, float c2)
  170. {
  171. SetWorkMode(EnumRfMatchTuneMode.Manual);
  172. SetPosition(c1, c2);
  173. //SetWorkMode(EnumRfMatchTuneMode.Auto);
  174. }
  175. private void SetPosition(float c1val, float c2val)
  176. {
  177. SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C1_POS} {c1val}\n");
  178. SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C2_POS} {c2val}\n");
  179. }
  180. public override bool SetMatchMode(EnumRfMatchTuneMode enumRfMatchTuneMode, out string reason)
  181. {
  182. reason = string.Empty;
  183. SetWorkMode(enumRfMatchTuneMode);
  184. return true;
  185. }
  186. private void SetWorkMode(EnumRfMatchTuneMode mode)
  187. {
  188. if (mode == EnumRfMatchTuneMode.Auto && WorkMode.Contains("MANUAL"))
  189. {
  190. SetPointCommandQueue.Add("MATCH:MODE HAUTO\n");
  191. }
  192. else if (mode == EnumRfMatchTuneMode.Manual && WorkMode.Contains("AUTO"))
  193. {
  194. SetPointCommandQueue.Add("MATCH:MODE MANUAL\n");
  195. }
  196. }
  197. }
  198. }