using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Log; using Aitex.Core.RT.OperationCenter; using Aitex.Core.RT.SCCore; using MECF.Framework.Common.Communications; using MECF.Framework.Common.Device.Bases; using MECF.Framework.Common.Equipment; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using Venus_Core; namespace Venus_RT.Devices { static class RevtechMatchMessage { public const string QUERY_STATE_INFORMATION = "MATCH:FETCH?\n"; public const string SET_C1_POS = "MATCH:POS:C1"; public const string SET_C2_POS = "MATCH:POS:C2"; public const string SET_WORK_MODE = "MATCH:MODE"; public const string PRE_SET = "MATCH:PSET:SET 00\n"; } public enum MatchCommunicationType { RS232, Ethernet } public class RevtechMatch : RfMatchBase { private AsyncSocketDevice _socket; private AsyncSerialPort _serial; private MatchCommunicationType _matchCommunicationType; private string _address; private string _port; public string WorkMode { get; set; } = ""; public float Vpp { get; set; } private float c1SetPoint; private float c2SetPoint; private VenusDevice _device; public new AITMatchData DeviceData => new AITMatchData { Module = Module, DeviceName = Name, WorkMode = WorkMode, C1 = TunePosition1, C2 = TunePosition2, VPP = Vpp.ToString(), DCBias = DCBias.ToString(), C1SetPoint = c1SetPoint, C2SetPoint = c2SetPoint, }; public RevtechMatch(ModuleName mod, VenusDevice venusDevice, MatchCommunicationType matchCommunicationType) : base(mod.ToString(), venusDevice.ToString()) { _device = venusDevice; //var test = System.IO.Ports.SerialPort.GetPortNames(); _matchCommunicationType = matchCommunicationType; if (matchCommunicationType == MatchCommunicationType.RS232) { var portNum = SC.GetStringValue($"{mod}.{venusDevice}.Port"); _port = portNum; if (SC.GetValue("System.IsSimulatorMode")) { _serial = new AsyncSerialPort(portNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\n", true); } else { _serial = new AsyncSerialPort(portNum, 115200, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\n", true); } } else if (matchCommunicationType == MatchCommunicationType.Ethernet) { _address = SC.GetStringValue($"{mod}.{venusDevice}.IPAddress"); _socket = new AsyncSocketDevice(_address); _socket.OnDataChanged += new AsyncSocketDevice.MessageHandler(OnDataChanged); _socket.OnErrorHappened += _socket_OnErrorHappened; } SerachCommandList = new List() { RevtechMatchMessage.QUERY_STATE_INFORMATION }; intervalTime = 300; sendDataChangedEvent += RevtechMatch_sendDataChangedEvent; baseStopwatch.Start(); baseTimer.Enabled = true; } private void _socket_OnErrorHappened(ErrorEventArgsDevice args) { LOG.Write(eEvent.ERR_MATCH, Module, $"{Module} {Name} Error {args.Reason}"); } public override bool Initialize() { base.Initialize(); if (_matchCommunicationType == MatchCommunicationType.RS232) { if (_serial != null && _serial.Open()) { _serial.OnBinaryDataChanged += OnDataChanged; LOG.Write(eEvent.INFO_MATCH, Module, $"{Name} 串口成功打开"); } else { LOG.Write(eEvent.ERR_MATCH, Module, $"{Name} 串口无法打开"); } } else if (_matchCommunicationType == MatchCommunicationType.Ethernet) { _socket?.Connect(_address); } DATA.Subscribe($"{Module}.{Name}.C1", () => TunePosition1); DATA.Subscribe($"{Module}.{Name}.C2", () => TunePosition2); DATA.Subscribe($"{Module}.{Name}.WorkMode", () => WorkMode); DATA.Subscribe($"{Module}.{Name}.Vpp", () => Vpp); DATA.Subscribe($"{Module}.{Name}.DCBias", () => DCBias); DATA.Subscribe($"{Module}.{Name}.C1SetPoint", () => c1SetPoint, Aitex.Core.Util.SubscriptionAttribute.FLAG.IgnoreSaveDB); DATA.Subscribe($"{Module}.{Name}.C2SetPoint", () => c2SetPoint, Aitex.Core.Util.SubscriptionAttribute.FLAG.IgnoreSaveDB); DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData); OP.Subscribe($"{Module}.{Name}.SetC1", (func, args) => { return true; }); OP.Subscribe($"{Module}.{Name}.SetC2", (func, args) => { return true; }); OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) => { SetMatchPositionC1((float)Convert.ToDouble(param[0]), out reason); return true; }); OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) => { SetMatchPositionC2((float)Convert.ToDouble(param[0]), out reason); return true; }); OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) => { SetMatchPosition((float)Convert.ToDouble(param[0]), (float)Convert.ToDouble(param[1]), out reason); return true; }); OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) => { SetMatchMode((string)param[0] == "Auto" ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual, out reason); return true; }); return true; } private void OnDataChanged(byte[] obj) { try { string data = System.Text.Encoding.ASCII.GetString(obj); if (data.Length < 10 && data.Length > 20) { return; } string[] matchData = data.Split(new char[] { ',' }); if (matchData.Length > 13) { if (matchData[0].Contains("MANUAL") || matchData[0].Contains("AUTO")) { WorkMode = matchData[0]; TunePosition1 = Convert.ToSingle(matchData[8]); TunePosition2 = Convert.ToSingle(matchData[7]); Vpp = Convert.ToSingle(matchData[12]); DCBias = Convert.ToSingle(matchData[13]); } } } catch { } } private void RevtechMatch_sendDataChangedEvent(string obj) { if ((_matchCommunicationType == MatchCommunicationType.Ethernet) && _socket.IsConnected) { byte[] value = Encoding.ASCII.GetBytes(obj); _socket?.Write(value); } else if ((_matchCommunicationType == MatchCommunicationType.RS232 && _serial?.IsOpen() == true)) { _serial?.Write(obj); } } public override void SetMatchPositionC1(float c1, out string reason) { base.SetMatchPositionC1(c1, out reason); SetWorkMode(EnumRfMatchTuneMode.Manual); c1SetPoint = c1; double value; if (Name == "RFBox") { var aValue = SC.GetValue($"{Module}.RFBox.CalculationParameterA"); var kValue = SC.GetValue($"{Module}.RFBox.CalculationParameterK"); value = Math.Round((c1 + aValue) / kValue,1); if (value < 0) { value = 0; } if (value > 100) { value = 100; } } else { value = c1; } SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C1_POS} {value}\n"); } public override void SetMatchPosition(float c1, float c2, out string reason) { base.SetMatchPosition(c1, c2, out reason); executeMatchPostion(c1, c2); c1SetPoint = c1; c2SetPoint = c2; //reason = ""; } private void executeMatchPostion(float c1, float c2) { SetWorkMode(EnumRfMatchTuneMode.Manual); SetPosition(c1, c2); //SetWorkMode(EnumRfMatchTuneMode.Auto); } private void SetPosition(float c1val, float c2val) { SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C1_POS} {c1val}\n"); SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C2_POS} {c2val}\n"); } public override bool SetMatchMode(EnumRfMatchTuneMode enumRfMatchTuneMode, out string reason) { reason = string.Empty; SetWorkMode(enumRfMatchTuneMode); return true; } private void SetWorkMode(EnumRfMatchTuneMode mode) { if (mode == EnumRfMatchTuneMode.Auto) { SetPointCommandQueue.Add("MATCH:MODE HAUTO\n"); } else if (mode == EnumRfMatchTuneMode.Manual) { SetPointCommandQueue.Add("MATCH:MODE MANUAL\n"); } } public override void Monitor() { } public override bool ReConnect() { if (_matchCommunicationType == MatchCommunicationType.RS232) { return _serial.ReConnect(); } else if (_matchCommunicationType == MatchCommunicationType.Ethernet) { return _socket.ReConnect(_address); } return false; } } }