using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO.Ports; using System.Text; using System.Text.RegularExpressions; using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Event; using Aitex.Core.RT.IOCore; using Aitex.Core.RT.Log; using Aitex.Core.RT.OperationCenter; using Aitex.Core.RT.SCCore; using Aitex.Core.RT.Tolerance; using Aitex.Core.Util; using MECF.Framework.Common.Communications; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.Device.Bases; using MECF.Framework.Common.Equipment; using VirgoCommon; using VirgoRT.Modules; namespace VirgoRT.Devices { public class ComRevtechMatch : RevtechMatch { private readonly AsyncSerialPort _serial; private string _portNum; private bool _isException = false; public ComRevtechMatch(ModuleName mod, string name, string configName = "") : base(mod, name, configName) { _portNum = SC.GetStringValue($"{mod}.{(string.IsNullOrWhiteSpace(configName) ? name : configName)}.Port"); _serial = new AsyncSerialPort(_portNum, 115200, 8, Parity.None, StopBits.One, "\n", true); _isException = false; //_serial.EnableLog = true; } public override bool Initialize() { base.Initialize(); if (_serial.Open()) { //_serial.OnBinaryDataChanged += OnDataChanged; _serial.OnDataChanged += SerialPortDataReceived; _serial.OnErrorHappened += SerialPortErrorOccurred; LOG.Info($"{Module} {Name} Revtech match port:[{_portNum}] connect"); } else { EV.PostAlarmLog(this.Module, $"Revtech match port:[{_portNum}] open failed"); return false; } return true; } protected override void SendCmd(string str) { base.SendCmd(str); _serial.Write(Encoding.ASCII.GetBytes(str + "\n")); } private void SerialPortErrorOccurred(string str) { EV.PostAlarmLog(Module, $"{Module} {Name} Revtech Match error [{str}]"); } private void SerialPortDataReceived(string data) { try { string[] matchData = data.Split(new char[] { ',' }); if (matchData.Length > 13) { if (matchData[0].Contains("MANUAL") || matchData[0].Contains("AUTO")) { WorkMode = matchData[0] == "MANUAL" ? EnumRfMatchTuneMode.Manual : EnumRfMatchTuneMode.Auto; TunePosition1 = Convert.ToSingle(matchData[8]); TunePosition2 = Convert.ToSingle(matchData[7]); VPP = (ushort)Convert.ToSingle(matchData[12]); DCBias = Convert.ToSingle(matchData[13]); } } _isException = false; } catch (Exception ex) { if(!_isException) LOG.Info($"{Module} {Name} ex={ex}"); _isException = true; } } } }