123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- 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<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}.{venusDevice}.IPAddress");
- _socket = new AsyncSocketDevice(_address);
- _socket.OnDataChanged += new AsyncSocketDevice.MessageHandler(OnDataChanged);
- _socket.OnErrorHappened += _socket_OnErrorHappened;
- }
- SerachCommandList = new List<string>()
- {
- 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<int>($"{Module}.RFBox.CalculationParameterA");
- var kValue = SC.GetValue<double>($"{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;
- }
- }
- }
|