RevtechMatch.cs 7.2 KB

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