123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- 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<string> SerachCommandList;
- private BlockingCollection<string> SetPointCommandQueue = new BlockingCollection<string>();
- [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<int>($"{mod}.{name}.CommunicationType");
- TunePosition1 = -1;
- TunePosition2 = -1;
- if (_matchCommunicationType == MatchCommunicationType.RS232)
- {
- var portNum = SC.GetStringValue($"{mod}.{name}.Port");
- _port = portNum;
- if (SC.GetValue<bool>("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<string>()
- {
- 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<EnumRfMatchTuneMode>(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;
- //}
- }
- }
|