123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.IO;
- using System.Threading.Tasks;
- using Tps_LQ_Transmitter.com;
- namespace Tps_LQ_Transmitter.models
- {
- class ModulationIndexAndMaxFreqOffset: BaseModel
- {
- public ModulationIndexAndMaxFreqOffset()
- {
- TemplateName = "调制指数及最大频偏指标测试";
- }
- /// <summary>
- /// 功率及频率稳定度测试
- /// </summary>
- public override bool Run(TestNode parameters)
- {
- double y_value, x_value;
- Random random = new Random();
- //获取仪器
- var SA = this.tps.GetDevice("频谱仪");
- TransmitterSerialPort SerialClient = new TransmitterSerialPort();
- OutData Data = new OutData();
- DataType ModulationIndexPrint = new DataType();
- DataType MaxFreqOffsetPrint = new DataType();
- //if (SA == null)
- //{
- // ShowMessage(MsgType.Error, string.Format("仪器不齐全,{0}/{1}无法运行", parameters.Channel, parameters.Name));
- // return false;
- //}
- MatchModulaPara CfigModuParas = new MatchModulaPara();
- CfigModuParas = LoadModulationWorkBook();
- ConfigParameter PowerPara = new ConfigParameter();
- PowerPara.ComPort = parameters.Parameters.GetParameter<string>("串口");
- PowerPara.OutLoss = parameters.Parameters.GetParameter<double>("输出损耗");
- PowerPara.StartFrequency = parameters.Parameters.GetParameter<double>("起始频率");
- PowerPara.StepFrequency = parameters.Parameters.GetParameter<double>("频率步进");
- PowerPara.FrequencyNumber = parameters.Parameters.GetParameter<int>("频点数量");
- PowerPara.StopFrequency = parameters.Parameters.GetParameter<double>("终止频率");
- PowerPara.SPAN = parameters.Parameters.GetParameter<string>("扫描带宽(SPAN)");
- PowerPara.REF = parameters.Parameters.GetParameter<string>("参考电平(REF)");
- PowerPara.RBW = parameters.Parameters.GetParameter<string>("分辨率带宽(RBW)");
- PowerPara.VBW = parameters.Parameters.GetParameter<string>("视频带宽(VBW)");
- PowerPara.ControlDelay = parameters.Parameters.GetParameter<int>("控制延时");
- PowerPara.ModulationIndexLower = parameters.Parameters.GetParameter<double>("调制系数下限");
- PowerPara.ModulationIndexUpper = parameters.Parameters.GetParameter<double>("调制系数上限");
- if ((PowerPara.StartFrequency == 0) || ((PowerPara.StepFrequency == 0) && (PowerPara.FrequencyNumber == 0)) || (PowerPara.StopFrequency == 0)
- || (PowerPara.SPAN == null) || (PowerPara.REF == null) || (PowerPara.RBW == null) || (PowerPara.VBW == null)||
- (PowerPara.ModulationIndexLower==0) || (PowerPara.ModulationIndexUpper==0))
- {
- ShowMessage(MsgType.Error, string.Format("配置文件中频率参数为空,{0}/{1}无法运行", parameters.Channel, parameters.Name));
- return false;
- }
- if (PowerPara.ControlDelay == 0)
- {
- PowerPara.ControlDelay = 10;
- }
- if (false)//需具备仪器
- {
- 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("REF", PowerPara.REF); SA.Query("OPC");
- SA.Write("SingleOrCont", "0"); SA.Query("OPC");
- SA.Write("MARK打开", "1"); SA.Query("OPC");
- SA.Write("MARK打开", "2"); SA.Query("OPC");
- SA.Write("DELT打开", "2"); SA.Query("OPC");
- }
- 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[] PowerDelt = new double[PowerPara.FrequencyNumber];
- Data.ModulationIndex = new string[PowerPara.FrequencyNumber];
- Data.MaxFreqOffset = new string[PowerPara.FrequencyNumber];
-
- for (int point = 0;; point++)
- {
- CenterFreq = PowerPara.StartFrequency + PowerPara.StepFrequency * point;
- if (CenterFreq > PowerPara.StopFrequency ||( PowerPara.FrequencyNumber == 1 && point == 1))
- {
- break;
- }
- if(false)//需具备仪器
- {
- // 控制
- SerialClient.DUT_Transmitter_Ctrol(PowerPara.ComPort, Convert.ToByte(point + 1));
- Thread.Sleep(PowerPara.ControlDelay);//单位ms
- SA.Write("CENTER", CenterFreq.ToString()); SA.Query("OPC");
- SA.Write("PEAK", "1"); SA.Query("OPC");
- SA.Write("NextPeak", "2"); SA.Query("OPC");
- if(SA.Query("IDN")=="N9030A")//N9030A型号频谱仪
- {
- PowerDelt[point] = double.Parse(SA.Query("读MARK功率", "2")); SA.Query("OPC");
- }
- else//其他型号频谱仪
- {
- PowerDelt[point] = double.Parse(SA.Query("读DELT功率", "2")); SA.Query("OPC");
- }
-
- }
- PowerDelt[point] = random.Next(500, 1200) / 100.0;//随机数
- double temp = 30;
- string RelativeVal= "0";
- foreach (KeyValuePair<string, ModulationMatchParameter> kvp in CfigModuParas.ModulaParameters)
- {
- if ((Math.Abs(PowerDelt[point] - double.Parse(kvp.Value.CfgRelative))) < temp)
- {
- temp = Math.Abs(PowerDelt[point] - double.Parse(kvp.Value.CfgRelative));
- RelativeVal = kvp.Value.CfgRelative;
- }
- }
- Data.ModulationIndex[point] = CfigModuParas.GetModulaParameter(RelativeVal);//调制系数
- // Data.MaxFreqOffset[point] = CfigModuParas.GetFreqOffsetParameter(RelativeVal);//最大频偏
- ModulationIndexPrint.Lower = PowerPara.ModulationIndexLower;
- ModulationIndexPrint.Upper = PowerPara.ModulationIndexUpper;
- ModulationIndexPrint.TestVal = double.Parse(Data.ModulationIndex[point]);
- if (ModulationIndexPrint.TestVal >= ModulationIndexPrint.Lower && ModulationIndexPrint.TestVal <= ModulationIndexPrint.Upper)
- {
- ModulationIndexPrint.Result = true;
- }
- else
- {
- ModulationIndexPrint.Result = false;
- }
- if (parameters.Channel == "通道1")
- {
- tps.SetTestTableCellValue(point, 9, ModulationIndexPrint.Result,Data.ModulationIndex[point]);
- // tps.SetTestTableCellValue(point, 6, Data.MaxFreqOffset[point]);
- }
- else if (parameters.Channel == "通道2")
- {
- tps.SetTestTableCellValue(point + 15, 9, ModulationIndexPrint.Result, Data.ModulationIndex[point]);
- //tps.SetTestTableCellValue(point + 15, 6, Data.MaxFreqOffset[point]);
- }
- }
- 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];
- for (int i = 0; i < tracedatas.Length; i++)
- {
- tracedata_double[i] = double.Parse(tracedatas[i]);
- }
- Y_Maxvalue = Math.Round(tracedata_double.Max(), 3);
- 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 - 1);
- }
- }
- public MatchModulaPara LoadModulationWorkBook()
- {
- MatchModulaPara pars = new MatchModulaPara();
- string FileName = Path.Combine(Bundle.bundle.BundleDir, "config\\ModulationMode.xlsx");
- if (!File.Exists(FileName))
- {
- ShowMessage(MsgType.Error, "找不到ModulationMode.xlsx");
- return null;
- }
- Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
- workbook.LoadFromFile(FileName);
-
- //获取第一个工作表sheet1
- Spire.Xls.Worksheet sheet = workbook.Worksheets[0];
- int num = 0;
- int blankRows = 0;
- while (num++ < 200)
- {
- //索引从1开始
- string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
- col1 = col1.Trim();
- if (string.IsNullOrEmpty(col1))
- {
- blankRows++;
- if (blankRows > 5)
- {
- //连续5个空行以上则退出
- break;
- }
- }
- else
- {
- blankRows = 0;
- }
- if (col1.Equals("功率变化(dB)"))
- {
- List<ModulationMatchParameter> ModulationTable = ReadParameters(sheet, num);
- foreach (var item in ModulationTable)
- {
- if (pars.ModulaParameters.ContainsKey(item.CfgRelative))
- {
- ShowMessage(MsgType.Error, string.Format("调制对应表:{0} 存在同名参数:{1},无法重复添加参数", sheet.Name, item.CfgRelative));
- continue;
- }
- pars.ModulaParameters.Add(item.CfgRelative, item);
- }
- }
- }
- return pars;
- }
- List<ModulationMatchParameter> ReadParameters(Spire.Xls.Worksheet sheet, int rowIndex)
- {
- List<ModulationMatchParameter> ModulationTable = new List<ModulationMatchParameter>();
- string col1 = sheet.Range[rowIndex , 1].Value2 != null ? sheet.Range[rowIndex , 1].Value2.ToString() : "";
- string col2 = sheet.Range[rowIndex , 2].Value2 != null ? sheet.Range[rowIndex , 2].Value2.ToString() : "";
- string col3 = sheet.Range[rowIndex , 3].Value2 != null ? sheet.Range[rowIndex , 3].Value2.ToString() : "";
-
- if (!(col1 == "功率变化(dB)" && col2 == "调制指数(mf)" && col3 == "最大频偏(KHz)"))
- {
- ShowMessage(MsgType.Error, "'调制对应表'行首标题位置和格式不正确");
- return ModulationTable;
- }
- int num = rowIndex ;
- //默认最多30行
- while (num++ < rowIndex + 100)
- {
- col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
- col2 = sheet.Range[num, 2].Value2 != null ? sheet.Range[num, 2].Value2.ToString() : "";
- col3 = sheet.Range[num, 3].Value2 != null ? sheet.Range[num, 3].Value2.ToString() : "";
- if (string.IsNullOrEmpty(col1) || string.IsNullOrEmpty(col2) || string.IsNullOrEmpty(col3))
- {
- break;
- }
- ModulationMatchParameter ObjModulation = new ModulationMatchParameter();
- ObjModulation.CfgRelative = col1;
- ObjModulation.CfgModulation = col2;
- ObjModulation.CfgFreqOffset = col3;
-
- ModulationTable.Add(ObjModulation);
- }
- return ModulationTable;
- }
- /// <summary>
- /// 一个调制指数的配置信息
- /// </summary>
- public class MatchModulaPara
- {
- public Dictionary<string, ModulationMatchParameter> ModulaParameters;
- public MatchModulaPara()
- {
- ModulaParameters = new Dictionary<string, ModulationMatchParameter>();
- }
- public string GetModulaParameter(string name)
- {
- if (string.IsNullOrEmpty(name))
- return null;
- if (this.ModulaParameters.ContainsKey(name) == false)
- return null;
- string val1 = this.ModulaParameters[name].CfgModulation.Trim();
- return val1;
- }
- public string GetFreqOffsetParameter(string name)
- {
- if (string.IsNullOrEmpty(name))
- return null;
- if (this.ModulaParameters.ContainsKey(name) == false)
- return null;
- string val2 = this.ModulaParameters[name].CfgFreqOffset.Trim();
- return val2;
- }
- }
-
- /// <summary>
- /// 调制指数3个对应项
- /// </summary>
- public class ModulationMatchParameter
- {
-
- /// <summary>
- /// 功率变化(dB)
- /// </summary>
- public string CfgRelative{ get; set; }
- /// <summary>
- /// 调制指数(mf)
- /// </summary>
- public string CfgModulation { get; set; }
- /// <summary>
- /// 最大频偏(KHz)
- /// </summary>
- public string CfgFreqOffset { get; set; }
-
- }
- public class ConfigParameter
- {
- /// <summary>
- /// 串口
- /// </summary>
- public string ComPort { set; get; }
- /// <summary>
- /// 输出损耗
- /// </summary>
- public double OutLoss { set; get; }
- /// <summary>
- /// 产品测试的起始频率
- /// </summary>
- public double StartFrequency { set; get; }
- /// <summary>
- /// 产品测试的频率步进
- /// </summary>
- public double StepFrequency { set; get; }
- /// <summary>
- /// 产品测试的频点数量
- /// </summary>
- public int FrequencyNumber { set; get; }
- /// <summary>
- /// 产品的工作频带上限(终止频率)
- /// 功能:用于判断从起始频率按一定的步进测试是否超出产品工作频段上限
- /// </summary>
- public double StopFrequency { set; get; }
- /// <summary>
- ///设置频谱仪的SPAN
- /// </summary>
- public string SPAN { set; get; }
- /// <summary>
- /// 设置参考电平
- /// </summary>
- public string REF { set; get; }
- /// <summary>
- /// 设置RBW
- /// </summary>
- public string RBW { set; get; }
- /// <summary>
- /// 设置VBW
- /// </summary>
- public string VBW { set; get; }
- /// <summary>
- /// 控制延时
- /// </summary>
- public int ControlDelay { set; get; }
- /// <summary>
- /// 调制系数下限
- /// </summary>
- public double ModulationIndexLower { get; set; }
- /// <summary>
- ///调制系数上限
- /// </summary>
- public double ModulationIndexUpper { get; set; }
- }
- public class OutData
- {
- /// <summary>
- ///调制系数
- /// </summary>
- public string[] ModulationIndex { set; get; }
- /// <summary>
- /// 最大频偏
- /// </summary>
- public string[] MaxFreqOffset { set; get; }
-
- }
- public class DataType
- {
- /// <summary>
- /// 测试名称
- /// </summary>
- public string Test_name { set; get; }
- /// <summary>
- /// 指标下限
- /// </summary>
- public double Lower { set; get; }
- /// <summary>
- /// 指标上限
- /// </summary>
- public double Upper { set; get; }
- /// <summary>
- /// 测试值
- /// </summary>
- public double TestVal { set; get; }
- /// <summary>
- /// 判断结果
- /// </summary>
- public bool Result { set; get; }
- }
- }
- }
|