using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Event; using Aitex.Core.RT.Log; using Aitex.Core.RT.OperationCenter; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; using MECF.Framework.Common.Communications; using MECF.Framework.Common.Device.Bases; using MECF.Framework.Common.Equipment; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; namespace JetVirgoPM.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 = 1, Ethernet = 2 } public class RevtechMatch : RfMatchBase { private AsyncSocketDevice _socket; private AsyncSerialPort _serial; private MatchCommunicationType _matchCommunicationType; private string _address; private string _port; [Subscription("MatchWorkMode")] public EnumRfMatchTuneMode WorkMode { get; set; } public List SerachCommandList; private BlockingCollection SetPointCommandQueue = new BlockingCollection(); [Subscription("VPP")] public float Vpp { get; set; } private float c1SetPoint; private float c2SetPoint; private readonly DeviceTimer _timerQueryStatus = new DeviceTimer(); private readonly DeviceTimer _timerQuery = new DeviceTimer(); private int QUERY_INTERVAL = 1000; public new AITRfData DeviceData => new AITRfData { Module = Module, DeviceName = Name, WorkMode = (int)WorkMode, MatchPositionC1 = TunePosition1, MatchPositionC2 = TunePosition2, //DCBias = DCBias.ToString(), //C1SetPoint = c1SetPoint, //C2SetPoint = c2SetPoint, }; public RevtechMatch(ModuleName mod, string name) : base(mod.ToString(), name) { _matchCommunicationType = (MatchCommunicationType)SC.GetValue($"{mod}.{name}.CommunicationType"); TunePosition1 = -1; TunePosition2 = -1; if (_matchCommunicationType == MatchCommunicationType.RS232) { var portNum = SC.GetStringValue($"{mod}.{name}.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}.{name}.IPAddress"); _socket = new AsyncSocketDevice(_address,"\n"); EV.PostInfoLog(mod.ToString(), "match初始化 建立连接"); _socket.OnDataChanged += new AsyncSocketDevice.MessageHandler(OnDataChanged); _socket.OnErrorHappened += _socket_OnErrorHappened; _socket.Connect(_address); } SerachCommandList = new List() { RevtechMatchMessage.QUERY_STATE_INFORMATION }; //intervalTime = 100; //sendDataChangedEvent += RevtechMatch_sendDataChangedEvent; //baseStopwatch.Start(); //baseTimer.Enabled = true; } ~RevtechMatch() { _serial?.Close(); } private void _socket_OnErrorHappened(ErrorEventArgsDevice args) { EV.PostAlarmLog(Module, $"{Module} {Name} Error {args.Reason}"); } public override bool Initialize() { base.Initialize(); if (_matchCommunicationType == MatchCommunicationType.RS232) { if (_serial != null && _serial.Open()) { _serial.OnBinaryDataChanged += OnDataChanged; EV.PostInfoLog(Module, $"{Name} 串口成功打开"); } else { EV.PostInfoLog(Module, $"{Name} 串口无法打开"); } } else if (_matchCommunicationType == MatchCommunicationType.Ethernet) { EV.PostInfoLog(Module, $"{Name} 网口准备打开"); _socket?.Connect(_address); if(_socket.IsConnected) EV.PostInfoLog(Module, $"{Name} 网口打开"); } 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}.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], out reason); return true; }); _timerQueryStatus.Start(QUERY_INTERVAL); _timerQuery.Start(QUERY_INTERVAL); 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")) { if (matchData[0].Contains("MANUAL")) WorkMode = EnumRfMatchTuneMode.Manual; else WorkMode = EnumRfMatchTuneMode.Auto; TunePosition1 = Convert.ToSingle(matchData[8]); TunePosition2 = Convert.ToSingle(matchData[7]); Vpp = Convert.ToSingle(matchData[12]); DCBias = Convert.ToSingle(matchData[13]); } } } catch (Exception ex) { LOG.Write(ex); } } private void RevtechMatch_sendDataChangedEvent(string obj) { if ((_matchCommunicationType == MatchCommunicationType.Ethernet) && _socket.IsConnected) { byte[] value = Encoding.ASCII.GetBytes(obj); _socket?.Write(value); } } public override void SetMatchPositionC1(float c1, out string reason) { base.SetMatchPositionC1(c1, out reason); //SetWorkMode(EnumRfMatchTuneMode.Manual); //c1SetPoint = c1; //SetPointCommandQueue.Add($"{RevtechMatchMessage.SET_C1_POS} {c1}\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(string mode, out string reason) { reason = string.Empty; //if (!Enum.TryParse(mode,out EnumRfMatchTuneMode enumRfMatchTuneMode)) //{ // return false; //} SetWorkMode(EnumRfMatchTuneMode.Auto); 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() { try { if (_timerQueryStatus.IsTimeout()) { if (SetPointCommandQueue.Count > 0) { string value; if (SetPointCommandQueue.TryTake(out value)) { value += "\n"; byte[] obj = Encoding.ASCII.GetBytes(value); _socket.Write(obj); EV.PostInfoLog(Module, $"{obj}"); } } _timerQueryStatus.Restart(QUERY_INTERVAL); } if (_timerQuery.IsTimeout()) { RevtechMatch_sendDataChangedEvent(RevtechMatchMessage.QUERY_STATE_INFORMATION); _timerQuery.Restart(QUERY_INTERVAL); } } catch (Exception ex) { LOG.Write(ex); } } //public override bool ReConnect() //{ // if (_matchCommunicationType == MatchCommunicationType.RS232) // { // return _serial.ReConnect(); // } // else if (_matchCommunicationType == MatchCommunicationType.Ethernet) // { // return _socket.ReConnect(_address); // } // return false; //} } }