using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Tps_LQ_Transmitter.com; namespace Tps_LQ_Transmitter.models { class SpuriousSuppression : BaseModel { public SpuriousSuppression() { TemplateName = "杂波抑制测试"; } /// /// 杂波抑制测试 /// public override bool Run(TestNode parameters) { double y_value, x_value; ShowMessage(MsgType.Info, string.Format("第{0}个频点杂波抑制测试开始", parameters.PointIndex)); Random random = new Random(); //获取仪器 var SA = this.tps.GetDevice("频谱仪"); //TransmitterSerialPort SerialClient = new TransmitterSerialPort(); OutData Data = new OutData(); DataType SpuriousSuppressionPrint = new DataType(); DataType specialSpuriousSuppressionPrint = new DataType(); if (SA == null) { ShowMessage(MsgType.Error, string.Format("仪器不齐全,{0}/{1}无法运行", parameters.Channel, parameters.Name)); return false; } OpenExcel("杂波抑制", out Spire.Xls.Workbook workbook, out Spire.Xls.Worksheet sheet); if (sheet == null) { ShowMessage(MsgType.Error, "找不到" + tps.TestProject + "模板.xlsx"); return false; } ConfigParameter PowerPara = new ConfigParameter(); //PowerPara.StepFrequency = 0; //PowerPara.ComPort = parameters.Parameters.GetParameter("串口"); // PowerPara.OutLoss = parameters.Parameters.GetParameter("输出损耗"); //PowerPara.StartFrequency = parameters.Parameters.GetParameter("起始频率"); //PowerPara.StepFrequency = parameters.Parameters.GetParameter("频率步进"); //PowerPara.FrequencyNumber = parameters.Parameters.GetParameter("频点数量"); //PowerPara.StopFrequency = parameters.Parameters.GetParameter("终止频率"); PowerPara.LeftOffset = parameters.Parameters.GetParameter("主频左侧终止偏移量"); PowerPara.LeftOffsetStart = parameters.Parameters.GetParameter("主频左侧起始偏移量"); PowerPara.RightOffset = parameters.Parameters.GetParameter("主频右侧起始偏移量"); PowerPara.RightOffsetStop = parameters.Parameters.GetParameter("主频右侧终止偏移量"); PowerPara.SpecialFreq = parameters.Parameters.GetParameter("特定频点"); PowerPara.SPAN = parameters.Parameters.GetParameter("扫描带宽(SPAN)"); PowerPara.REF = parameters.Parameters.GetParameter("参考电平(REF)"); PowerPara.SmallSignalREF = parameters.Parameters.GetParameter("小信号参考电平(REF)"); PowerPara.RBW = parameters.Parameters.GetParameter("分辨率带宽(RBW)"); PowerPara.VBW = parameters.Parameters.GetParameter("视频带宽(VBW)"); PowerPara.SmallSignalSPAN = parameters.Parameters.GetParameter("小信号扫描带宽(SPAN)"); PowerPara.SmallSignalRBW = parameters.Parameters.GetParameter("小信号分辨率带宽(RBW)"); PowerPara.SmallSignalVBW = parameters.Parameters.GetParameter("小信号视频带宽(VBW)"); PowerPara.ShakeCompensate = parameters.Parameters.GetParameter("抖动补偿"); PowerPara.LimitOffset = parameters.Parameters.GetParameter("临界偏移"); //PowerPara.ControlDelay = parameters.Parameters.GetParameter("控制延时"); PowerPara.SpuriousSuppressionLower = parameters.Parameters.GetParameter("杂波抑制下限"); PowerPara.SpuriousSuppressionUpper = parameters.Parameters.GetParameter("杂波抑制上限"); PowerPara.SpecialFreqSpuriousSuppressionLower = parameters.Parameters.GetParameter("特定频点杂波抑制下限"); PowerPara.SpecialFreqSpuriousSuppressionUpper = parameters.Parameters.GetParameter("特定频点杂波抑制上限"); if ( (PowerPara.LeftOffset == 0) || (PowerPara.LeftOffsetStart == 0) || (PowerPara.RightOffset == 0) || (PowerPara.RightOffsetStop == 0) || (PowerPara.SPAN == null) || (PowerPara.REF == null) || (PowerPara.SmallSignalREF == null) || (PowerPara.RBW == null) || (PowerPara.VBW == null) || (PowerPara.SpuriousSuppressionLower == 0) || (PowerPara.SpuriousSuppressionUpper == 0)) { ShowMessage(MsgType.Error, string.Format("配置文件中频率参数为空,{0}/{1}无法运行", parameters.Channel, parameters.Name)); return false; } //if (PowerPara.ControlDelay == 0) //{ // PowerPara.ControlDelay = 10; //} if(true)//需具备仪器 { SA.Write("仪器复位"); SA.Query("OPC"); SA.Write("SPAN", PowerPara.SPAN); SA.Query("OPC"); SA.Write("RBW", PowerPara.RBW); SA.Query("OPC"); SA.Write("VBW", PowerPara.VBW); SA.Query("OPC"); SA.Write("SingleOrCont", "0"); SA.Query("OPC"); SA.Write("MARK打开", "1"); SA.Query("OPC"); Thread.Sleep(200); } //if ((PowerPara.FrequencyNumber != 0) && (PowerPara.FrequencyNumber != 1) && (PowerPara.StepFrequency == 0)) //{ // PowerPara.StepFrequency = ((int)(((PowerPara.StopFrequency - PowerPara.StartFrequency) / (PowerPara.FrequencyNumber - 1)) * 100)) / 100; //} //if (PowerPara.StepFrequency != 0) //{ // PowerPara.FrequencyNumber = ((int)((PowerPara.StopFrequency - PowerPara.StartFrequency) / PowerPara.StepFrequency)) + 1; //} //double CenterFreq; double Power,PowerLeft, PowerRigth,PowerSpecialFreq=0, Freq; Data.SpuriousSuppression = new double[parameters.PointTotal]; // for (int point = 0; ; point++) { //CenterFreq = PowerPara.StartFrequency + PowerPara.StepFrequency * point; //if (CenterFreq > PowerPara.StopFrequency || (PowerPara.FrequencyNumber == 1 && point == 1)) //{ // break; //} if (true)//需具备仪器 { // 控制 // SerialClient.DUT_Transmitter_Ctrol(PowerPara.ComPort, Convert.ToByte(point + 1)); Thread.Sleep(PowerPara.ControlDelay);//单位ms SA.Write("REF", PowerPara.REF); SA.Query("OPC"); SA.Write("CENTER", parameters.CenterFreq.ToString()); SA.Query("OPC"); PsaPeakValue_Tracedata(SA, out y_value, out x_value, true); Freq=x_value/1000000; Power = y_value; //测主频左侧杂散 SA.Write("REF", PowerPara.SmallSignalREF); SA.Query("OPC"); SA.Write("START", (Freq - PowerPara.LeftOffsetStart).ToString()); SA.Query("OPC"); SA.Write("STOP", (Freq - PowerPara.LeftOffset).ToString()); SA.Query("OPC"); PsaPeakValue_Tracedata(SA, out y_value, out x_value, false); PowerLeft = Power - y_value; //测主频右侧杂散 SA.Write("START", (Freq + PowerPara.RightOffset).ToString()); SA.Query("OPC"); SA.Write("STOP",( Freq + PowerPara.RightOffsetStop).ToString()); SA.Query("OPC"); PsaPeakValue_Tracedata(SA, out y_value, out x_value, false); PowerRigth = Power - y_value; if (PowerPara.SpecialFreq != 0) { SA.Write("SPAN", PowerPara.SmallSignalSPAN); SA.Query("OPC"); SA.Write("RBW", PowerPara.SmallSignalRBW); SA.Query("OPC"); SA.Write("VBW", PowerPara.SmallSignalVBW); SA.Query("OPC"); SA.Write("CENTER", PowerPara.SpecialFreq.ToString()); SA.Query("OPC"); SA.Write("SingleOrCont", "1"); SA.Query("OPC"); Thread.Sleep(500); SA.Write("设置MARK频率","1", PowerPara.SpecialFreq.ToString()); SA.Query("OPC"); Thread.Sleep(200); string val = SA.Query("读MARK功率", "1"); double SpecialPower = Power - double.Parse(val); PowerSpecialFreq = Math.Round(SpecialPower, 2); specialSpuriousSuppressionPrint.Test_name = parameters.Channel +"-" +PowerPara.SpecialFreq.ToString() + "MHz杂波抑制测试-" + parameters.CenterFreq.ToString() + "MHz"; specialSpuriousSuppressionPrint.Lower = PowerPara.SpecialFreqSpuriousSuppressionLower; specialSpuriousSuppressionPrint.Upper = PowerPara.SpecialFreqSpuriousSuppressionUpper; if (((specialSpuriousSuppressionPrint.Lower - PowerPara.LimitOffset) <= PowerSpecialFreq) && (PowerSpecialFreq < specialSpuriousSuppressionPrint.Lower)) { PowerSpecialFreq = PowerSpecialFreq + PowerPara.ShakeCompensate; } else if ((specialSpuriousSuppressionPrint.Upper < PowerSpecialFreq) && (PowerSpecialFreq < (specialSpuriousSuppressionPrint.Upper + PowerPara.LimitOffset))) { PowerSpecialFreq = PowerSpecialFreq - PowerPara.ShakeCompensate; } // SpuriousSuppressionPrint.TestVal = random.Next(4000, 12000) / 100.0;//随机数 if ((PowerSpecialFreq >= specialSpuriousSuppressionPrint.Lower) && (PowerSpecialFreq <= specialSpuriousSuppressionPrint.Upper)) { specialSpuriousSuppressionPrint.Result = "是"; } else { specialSpuriousSuppressionPrint.Result = "否"; } tps.TestTableAddCell(specialSpuriousSuppressionPrint.Test_name, specialSpuriousSuppressionPrint.Lower.ToString(), specialSpuriousSuppressionPrint.Upper.ToString(), PowerSpecialFreq.ToString(), specialSpuriousSuppressionPrint.Result); } } SpuriousSuppressionPrint.Test_name = parameters.Channel + "-杂波抑制测试-" + parameters.CenterFreq.ToString() + "MHz"; SpuriousSuppressionPrint.Lower = PowerPara.SpuriousSuppressionLower; SpuriousSuppressionPrint.Upper = PowerPara.SpuriousSuppressionUpper; SpuriousSuppressionPrint.TestVal = Math.Round(Math.Min(PowerLeft, PowerRigth), 2); // SpuriousSuppressionPrint.TestVal = 49.92; if ( ((SpuriousSuppressionPrint.Lower- PowerPara.LimitOffset) <= SpuriousSuppressionPrint.TestVal) && (SpuriousSuppressionPrint.TestVal < SpuriousSuppressionPrint.Lower)) { SpuriousSuppressionPrint.TestVal = SpuriousSuppressionPrint.TestVal + PowerPara.ShakeCompensate; } else if((SpuriousSuppressionPrint.Upper < SpuriousSuppressionPrint.TestVal) && (SpuriousSuppressionPrint.TestVal < (SpuriousSuppressionPrint.Upper + PowerPara.LimitOffset))) { SpuriousSuppressionPrint.TestVal = SpuriousSuppressionPrint.TestVal - PowerPara.ShakeCompensate; } // SpuriousSuppressionPrint.TestVal = random.Next(4000, 12000) / 100.0;//随机数 if ((SpuriousSuppressionPrint.TestVal >= SpuriousSuppressionPrint.Lower) && (SpuriousSuppressionPrint.TestVal <= SpuriousSuppressionPrint.Upper)) { SpuriousSuppressionPrint.Result = "是"; } else { SpuriousSuppressionPrint.Result = "否"; } tps.TestTableAddCell(SpuriousSuppressionPrint.Test_name, SpuriousSuppressionPrint.Lower.ToString(), SpuriousSuppressionPrint.Upper.ToString(), SpuriousSuppressionPrint.TestVal.ToString(), SpuriousSuppressionPrint.Result); if (parameters.Channel == "通道1") { //tps.SetTestTableCellValue(point, 14, SpuriousSuppressionPrint.Result,SpuriousSuppressionPrint.TestVal); WriteExcelData(sheet, parameters.PointIndex, 1, SpuriousSuppressionPrint.Test_name, SpuriousSuppressionPrint.Lower.ToString(), SpuriousSuppressionPrint.Upper.ToString(), SpuriousSuppressionPrint.TestVal.ToString(), SpuriousSuppressionPrint.Result); if (PowerPara.SpecialFreq != 0) { WriteExcelData(sheet, parameters.PointIndex, 3, specialSpuriousSuppressionPrint.Test_name, specialSpuriousSuppressionPrint.Lower.ToString(), specialSpuriousSuppressionPrint.Upper.ToString(), PowerSpecialFreq.ToString(), specialSpuriousSuppressionPrint.Result); } } else if (parameters.Channel == "通道2") { //tps.SetTestTableCellValue(point + 15, 14, SpuriousSuppressionPrint.Result, SpuriousSuppressionPrint.TestVal); WriteExcelData(sheet, parameters.PointIndex, 2, SpuriousSuppressionPrint.Test_name, SpuriousSuppressionPrint.Lower.ToString(), SpuriousSuppressionPrint.Upper.ToString(), SpuriousSuppressionPrint.TestVal.ToString(), SpuriousSuppressionPrint.Result); if (PowerPara.SpecialFreq != 0) { WriteExcelData(sheet, parameters.PointIndex, 4, specialSpuriousSuppressionPrint.Test_name, specialSpuriousSuppressionPrint.Lower.ToString(), specialSpuriousSuppressionPrint.Upper.ToString(), PowerSpecialFreq.ToString(), SpuriousSuppressionPrint.Result); } } } SaveExcel(workbook); ShowMessage(MsgType.Info, string.Format("第{0}个频点杂波抑制测试结束", parameters.PointIndex)); return true; } public void PsaPeakValue_Tracedata(AppLibs.Devices.IVISA psa, out double Y_Maxvalue, out double X_Maxvalue, bool IsReturnX = false) { System.Threading.Thread.Sleep(20); X_Maxvalue = 0; Y_Maxvalue = 0; psa.Write("单次扫描"); psa.Query("OPC"); string tracedata = psa.Query("读曲线"); string[] tracedatas = tracedata.Split(','); double[] tracedata_double = new double[tracedatas.Length-1]; for (int i = 0; i < tracedatas.Length-1; i++) { tracedata_double[i] = double.Parse(tracedatas[i]); } Y_Maxvalue = tracedata_double.Max(); if (IsReturnX) { int x = Array.IndexOf(tracedata_double, Y_Maxvalue); double startfreq = double.Parse(psa.Query("读起始频率")); double stopfreq = double.Parse(psa.Query("读截止频率")); double counts = double.Parse(psa.Query("测试点数读取")); X_Maxvalue = startfreq + (stopfreq - startfreq) * x / (counts - 2); } Y_Maxvalue = Math.Round(Y_Maxvalue, 3); } public class ConfigParameter { /// /// 串口 /// public string ComPort { set; get; } /// /// 输出损耗 /// public double OutLoss { set; get; } /// /// 产品测试的起始频率 /// public double StartFrequency { set; get; } /// /// 产品测试的频率步进 /// public double StepFrequency { set; get; } /// /// 产品测试的频点数量 /// public int FrequencyNumber { set; get; } /// /// 产品的工作频带上限(终止频率) /// 功能:用于判断从起始频率按一定的步进测试是否超出产品工作频段上限 /// public double StopFrequency { set; get; } /// /// 主频左侧偏移量 /// public double LeftOffset { set; get; } /// /// 主频左侧起始偏移量值 /// public double LeftOffsetStart { set; get; } /// /// 主频右侧偏移量 /// public double RightOffset { set; get; } /// /// 主频右侧终止偏移量值 /// public double RightOffsetStop { set; get; } /// /// 特殊频点 /// public double SpecialFreq { set; get; } /// ///设置频谱仪的SPAN /// public string SPAN { set; get; } /// ///设置小信号SPAN /// public string SmallSignalSPAN { set; get; } /// /// 设置参考电平 /// public string REF { set; get; } /// /// 设置小信号参考电平 /// public string SmallSignalREF { set; get; } /// /// 设置RBW /// public string RBW { set; get; } /// /// 设置VBW /// public string VBW { set; get; } /// /// 设置小信号RBW /// public string SmallSignalRBW { set; get; } /// /// 设置小信号VBW /// public string SmallSignalVBW { set; get; } /// /// 控制延时 /// public int ControlDelay { set; get; } /// /// 抖动补偿 /// public double ShakeCompensate { set; get; } /// /// 临界偏移 /// public double LimitOffset { set; get; } /// /// 杂波抑制下限 /// public double SpuriousSuppressionLower { set; get; } /// /// 杂波抑制上限 /// public double SpuriousSuppressionUpper { set; get; } /// /// 杂波抑制下限 /// public double SpecialFreqSpuriousSuppressionLower { set; get; } /// /// 杂波抑制上限 /// public double SpecialFreqSpuriousSuppressionUpper { set; get; } } public class OutData { /// /// 杂波抑制 /// public double[] SpuriousSuppression { set; get; } } //public class DataType //{ // /// // /// 测试名称 // /// // public string Test_name { set; get; } // /// // /// 指标下限 // /// // public double Lower { set; get; } // /// // /// 指标上限 // /// // public double Upper { set; get; } // /// // /// 测试值 // /// // public double TestVal { set; get; } // /// // /// 判断结果 // /// // public bool Result { set; get; } //} } }