RevtechMatch.cs 7.3 KB

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