| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 | 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.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 enum MatchCommunicationType    {        RS232,        Ethernet    }    public class RevtechMatch : RfMatchBase    {        private AsyncSocketDevice _socket;        private AsyncSerialPort _serial;        private MatchCommunicationType _matchCommunicationType;        private string _address;        public string WorkMode { get; set; } = "";        public RevtechMatch(ModuleName mod, VenusDevice venusDevice, MatchCommunicationType matchCommunicationType) : base(mod.ToString(), venusDevice.ToString())        {            _matchCommunicationType = matchCommunicationType;            if (matchCommunicationType == MatchCommunicationType.RS232)            {                var portNum = SC.GetStringValue($"{mod}.{venusDevice}.Port");                if (SC.GetValue<bool>("System.IsSimulatorMode"))                {                    _serial = new AsyncSerialPort(portNum, 9600, 8);                }                else                {                    _serial = new AsyncSerialPort(portNum, 115200, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\n", false);                }            }            else if (matchCommunicationType == MatchCommunicationType.Ethernet)            {                var address = SC.GetStringValue($"{mod}.{venusDevice}.IPAddress");                _address = address;                _socket = new AsyncSocketDevice(address);                _socket.OnDataChanged +=new AsyncSocketDevice.MessageHandler(OnDataChanged);                _socket.OnErrorHappened += _socket_OnErrorHappened;            }            SerachCommandList = new List<string>()            {            RevtechMatchMessage.QUERY_STATE_INFORMATION            };            sendDataChangedEvent += RevtechMatch_sendDataChangedEvent;            baseStopwatch.Start();            baseTimer.Enabled = true;        }        private void _socket_OnErrorHappened(ErrorEventArgsDevice args)        {            LOG.Write(eEvent.ERR_MATCH, Module, $"{Module} Revtech Match 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);            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)        {            string data = System.Text.Encoding.ASCII.GetString(obj);            if (data.Length < 10)            {                return;            }            string[] matchData = data.Split(',');                    if (matchData.Length > 9)            {                WorkMode = matchData[0];                TunePosition1 = Convert.ToSingle(matchData[7]);                TunePosition2 = Convert.ToSingle(matchData[8]);            }        }        private void RevtechMatch_sendDataChangedEvent(string obj)        {            if ((_matchCommunicationType == MatchCommunicationType.Ethernet))            {                byte[] value = Encoding.ASCII.GetBytes(obj);                _socket?.Write(value);            }            else if ((_matchCommunicationType == MatchCommunicationType.RS232 && _serial?.IsOpen() == true))            {                _serial?.Write(obj);            }        }        public override void SetMatchPosition(float c1, float c2, out string reason)        {            base.SetMatchPosition(c1, c2, out reason);            executeMatchPostion(c1, 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");            }        }    }}
 |