RevtechMatch.cs 7.4 KB

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