RevtechMatch.cs 7.1 KB

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