ModulationIndexAndMaxFreqOffset.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.IO;
  7. using System.Threading.Tasks;
  8. using Tps_LQ_Transmitter.com;
  9. namespace Tps_LQ_Transmitter.models
  10. {
  11. class ModulationIndexAndMaxFreqOffset: BaseModel
  12. {
  13. public ModulationIndexAndMaxFreqOffset()
  14. {
  15. TemplateName = "调制指数及最大频偏指标测试";
  16. }
  17. /// <summary>
  18. /// 功率及频率稳定度测试
  19. /// </summary>
  20. public override bool Run(TestNode parameters)
  21. {
  22. double y_value, x_value;
  23. Random random = new Random();
  24. //获取仪器
  25. var SA = this.tps.GetDevice("频谱仪");
  26. TransmitterSerialPort SerialClient = new TransmitterSerialPort();
  27. OutData Data = new OutData();
  28. DataType ModulationIndexPrint = new DataType();
  29. DataType MaxFreqOffsetPrint = new DataType();
  30. if (SA == null)
  31. {
  32. ShowMessage(MsgType.Error, string.Format("仪器不齐全,{0}/{1}无法运行", parameters.Channel, parameters.Name));
  33. return false;
  34. }
  35. OpenExcel("调制系数", out Spire.Xls.Workbook workbook, out Spire.Xls.Worksheet sheet);
  36. if (sheet == null)
  37. {
  38. ShowMessage(MsgType.Error, "找不到" + tps.TestProject + "模板.xlsx");
  39. return false;
  40. }
  41. MatchModulaPara CfigModuParas = new MatchModulaPara();
  42. CfigModuParas = LoadModulationWorkBook();
  43. if (CfigModuParas == null)
  44. {
  45. return false;
  46. }
  47. ConfigParameter PowerPara = new ConfigParameter();
  48. PowerPara.ComPort = parameters.Parameters.GetParameter<string>("串口");
  49. PowerPara.OutLoss = parameters.Parameters.GetParameter<double>("输出损耗");
  50. PowerPara.StartFrequency = parameters.Parameters.GetParameter<double>("起始频率");
  51. PowerPara.StepFrequency = parameters.Parameters.GetParameter<double>("频率步进");
  52. PowerPara.FrequencyNumber = parameters.Parameters.GetParameter<int>("频点数量");
  53. PowerPara.StopFrequency = parameters.Parameters.GetParameter<double>("终止频率");
  54. PowerPara.SPAN = parameters.Parameters.GetParameter<string>("扫描带宽(SPAN)");
  55. PowerPara.REF = parameters.Parameters.GetParameter<string>("参考电平(REF)");
  56. PowerPara.RBW = parameters.Parameters.GetParameter<string>("分辨率带宽(RBW)");
  57. PowerPara.VBW = parameters.Parameters.GetParameter<string>("视频带宽(VBW)");
  58. PowerPara.ControlDelay = parameters.Parameters.GetParameter<int>("控制延时");
  59. PowerPara.ModulationIndexLower = parameters.Parameters.GetParameter<double>("调制系数下限");
  60. PowerPara.ModulationIndexUpper = parameters.Parameters.GetParameter<double>("调制系数上限");
  61. if ((PowerPara.StartFrequency == 0) || ((PowerPara.StepFrequency == 0) && (PowerPara.FrequencyNumber == 0)) || (PowerPara.StopFrequency == 0)
  62. || (PowerPara.SPAN == null) || (PowerPara.REF == null) || (PowerPara.RBW == null) || (PowerPara.VBW == null)||
  63. (PowerPara.ModulationIndexLower==0) || (PowerPara.ModulationIndexUpper==0))
  64. {
  65. ShowMessage(MsgType.Error, string.Format("配置文件中频率参数为空,{0}/{1}无法运行", parameters.Channel, parameters.Name));
  66. return false;
  67. }
  68. if (PowerPara.ControlDelay == 0)
  69. {
  70. PowerPara.ControlDelay = 10;
  71. }
  72. if (true)//需具备仪器
  73. {
  74. SA.Write("仪器复位"); SA.Query("OPC");
  75. SA.Write("SPAN", PowerPara.SPAN); SA.Query("OPC");
  76. SA.Write("RBW", PowerPara.RBW); SA.Query("OPC");
  77. SA.Write("VBW", PowerPara.VBW); SA.Query("OPC");
  78. SA.Write("REF", PowerPara.REF); SA.Query("OPC");
  79. SA.Write("MARK打开", "1"); SA.Query("OPC");
  80. SA.Write("MARK打开", "2"); SA.Query("OPC");
  81. SA.Write("DELT打开", "2"); SA.Query("OPC");
  82. }
  83. if ((PowerPara.FrequencyNumber != 0) && (PowerPara.FrequencyNumber != 1) && (PowerPara.StepFrequency == 0))
  84. {
  85. PowerPara.StepFrequency = ((int)(((PowerPara.StopFrequency - PowerPara.StartFrequency) / (PowerPara.FrequencyNumber - 1)) * 100)) / 100;
  86. }
  87. if(PowerPara.StepFrequency != 0)
  88. {
  89. PowerPara.FrequencyNumber = ((int)((PowerPara.StopFrequency - PowerPara.StartFrequency) / PowerPara.StepFrequency)) + 1;
  90. }
  91. double CenterFreq;
  92. double[] PowerDelt = new double[PowerPara.FrequencyNumber];
  93. Data.ModulationIndex = new string[PowerPara.FrequencyNumber];
  94. Data.MaxFreqOffset = new string[PowerPara.FrequencyNumber];
  95. for (int point = 0;; point++)
  96. {
  97. CenterFreq = PowerPara.StartFrequency + PowerPara.StepFrequency * point;
  98. if (CenterFreq > PowerPara.StopFrequency ||( PowerPara.FrequencyNumber == 1 && point == 1))
  99. {
  100. break;
  101. }
  102. if(true)//需具备仪器
  103. {
  104. // 控制
  105. // SerialClient.DUT_Transmitter_Ctrol(PowerPara.ComPort, Convert.ToByte(point + 1));
  106. Thread.Sleep(PowerPara.ControlDelay);//单位ms
  107. SA.Write("CENTER", CenterFreq.ToString()); SA.Query("OPC");
  108. SA.Write("SingleOrCont", "0"); SA.Query("OPC");
  109. SA.Write("单次扫描"); SA.Query("OPC");
  110. SA.Write("PEAK", "1"); SA.Query("OPC");
  111. SA.Write("PEAK", "2"); SA.Query("OPC");
  112. if (SA.Query("IDN").Contains("N9030"))//N9030A型号频谱仪
  113. {
  114. SA.Write("DELT打开", "2"); SA.Query("OPC");
  115. SA.Write("NextPeak", "2"); SA.Query("OPC");
  116. PowerDelt[point] = double.Parse(SA.Query("读MARK功率", "2")); SA.Query("OPC");
  117. }
  118. else//其他型号频谱仪
  119. {
  120. SA.Write("NextPeak", "2"); SA.Query("OPC");
  121. PowerDelt[point] = double.Parse(SA.Query("读DELT功率", "2")); SA.Query("OPC");
  122. }
  123. }
  124. // PowerDelt[point] = random.Next(500, 1200) / 100.0;//随机数
  125. double temp = 30;
  126. string RelativeVal= "0";
  127. foreach (KeyValuePair<string, ModulationMatchParameter> kvp in CfigModuParas.ModulaParameters)
  128. {
  129. if ((Math.Abs(Math.Abs(PowerDelt[point]) - double.Parse(kvp.Value.CfgRelative))) < temp)
  130. {
  131. temp = Math.Abs(Math.Abs(PowerDelt[point]) - double.Parse(kvp.Value.CfgRelative));
  132. RelativeVal = kvp.Value.CfgRelative;
  133. }
  134. }
  135. Data.ModulationIndex[point] = CfigModuParas.GetModulaParameter(RelativeVal);//调制系数
  136. Data.MaxFreqOffset[point] = CfigModuParas.GetFreqOffsetParameter(RelativeVal);//最大频偏
  137. ModulationIndexPrint.Test_name = parameters.Channel + "-调制系数测试-" + CenterFreq.ToString() + "MHz";
  138. MaxFreqOffsetPrint.Test_name = parameters.Channel + "-最大频偏(KHz)-" + CenterFreq.ToString() + "MHz";
  139. ModulationIndexPrint.Lower = PowerPara.ModulationIndexLower;
  140. ModulationIndexPrint.Upper = PowerPara.ModulationIndexUpper;
  141. ModulationIndexPrint.TestVal = double.Parse(Data.ModulationIndex[point]);
  142. MaxFreqOffsetPrint.TestVal = double.Parse(Data.MaxFreqOffset[point]);
  143. if (ModulationIndexPrint.TestVal >= ModulationIndexPrint.Lower && ModulationIndexPrint.TestVal <= ModulationIndexPrint.Upper)
  144. {
  145. ModulationIndexPrint.Result = "是";
  146. MaxFreqOffsetPrint.Result = "是";
  147. }
  148. else
  149. {
  150. ModulationIndexPrint.Result = "否";
  151. MaxFreqOffsetPrint.Result = "否";
  152. }
  153. tps.TestTableAddCell(MaxFreqOffsetPrint.Test_name, "/","/", MaxFreqOffsetPrint.TestVal.ToString(), MaxFreqOffsetPrint.Result);
  154. tps.TestTableAddCell(ModulationIndexPrint.Test_name, ModulationIndexPrint.Lower.ToString(), ModulationIndexPrint.Upper.ToString(), ModulationIndexPrint.TestVal.ToString(), ModulationIndexPrint.Result);
  155. if (parameters.Channel == "通道1")
  156. {
  157. if (point == 0)
  158. {
  159. WriteExcelData(sheet, point - 1, 1, "测试名称", "下限", "上限", "测试值", "通过");
  160. }
  161. //tps.SetTestTableCellValue(point, 9, ModulationIndexPrint.Result,Data.ModulationIndex[point]);
  162. WriteExcelData(sheet, point, 1, ModulationIndexPrint.Test_name, ModulationIndexPrint.Lower.ToString(), ModulationIndexPrint.Upper.ToString(), ModulationIndexPrint.TestVal.ToString(), ModulationIndexPrint.Result);
  163. // tps.SetTestTableCellValue(point, 6, Data.MaxFreqOffset[point]);
  164. }
  165. else if (parameters.Channel == "通道2")
  166. {
  167. if (point == 0)
  168. {
  169. WriteExcelData(sheet, point - 1, 2, "测试名称", "下限", "上限", "测试值", "通过");
  170. }
  171. WriteExcelData(sheet, point, 2, ModulationIndexPrint.Test_name, ModulationIndexPrint.Lower.ToString(), ModulationIndexPrint.Upper.ToString(), ModulationIndexPrint.TestVal.ToString(), ModulationIndexPrint.Result);
  172. //tps.SetTestTableCellValue(point + 15, 9, ModulationIndexPrint.Result, Data.ModulationIndex[point]);
  173. //tps.SetTestTableCellValue(point + 15, 6, Data.MaxFreqOffset[point]);
  174. }
  175. }
  176. SaveExcel(workbook);
  177. return true;
  178. }
  179. public void PsaPeakValue_Tracedata(AppLibs.Devices.IVISA psa, out double Y_Maxvalue, out double X_Maxvalue, bool IsReturnX = false)
  180. {
  181. System.Threading.Thread.Sleep(20);
  182. X_Maxvalue = 0;
  183. Y_Maxvalue = 0;
  184. psa.Write("单次扫描");
  185. psa.Query("OPC");
  186. string tracedata = psa.Query("读曲线");
  187. string[] tracedatas = tracedata.Split(',');
  188. double[] tracedata_double = new double[tracedatas.Length];
  189. for (int i = 0; i < tracedatas.Length; i++)
  190. {
  191. tracedata_double[i] = double.Parse(tracedatas[i]);
  192. }
  193. Y_Maxvalue = tracedata_double.Max();
  194. if (IsReturnX)
  195. {
  196. int x = Array.IndexOf(tracedata_double, Y_Maxvalue);
  197. double startfreq = double.Parse(psa.Query("读起始频率"));
  198. double stopfreq = double.Parse(psa.Query("读截止频率"));
  199. double counts = double.Parse(psa.Query("测试点数读取"));
  200. X_Maxvalue = startfreq + (stopfreq - startfreq) * x / (counts - 1);
  201. }
  202. Y_Maxvalue = Math.Round(Y_Maxvalue, 3);
  203. }
  204. public MatchModulaPara LoadModulationWorkBook()
  205. {
  206. MatchModulaPara pars = new MatchModulaPara();
  207. string FileName = Path.Combine(Bundle.bundle.BundleDir, "config\\ModulationMode.xlsx");
  208. if (!File.Exists(FileName))
  209. {
  210. ShowMessage(MsgType.Error, "找不到ModulationMode.xlsx");
  211. return null;
  212. }
  213. Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
  214. workbook.LoadFromFile(FileName);
  215. //获取第一个工作表sheet1
  216. Spire.Xls.Worksheet sheet = workbook.Worksheets[0];
  217. int num = 0;
  218. int blankRows = 0;
  219. while (num++ < 200)
  220. {
  221. //索引从1开始
  222. string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  223. col1 = col1.Trim();
  224. if (string.IsNullOrEmpty(col1))
  225. {
  226. blankRows++;
  227. if (blankRows > 5)
  228. {
  229. //连续5个空行以上则退出
  230. break;
  231. }
  232. }
  233. else
  234. {
  235. blankRows = 0;
  236. }
  237. if (col1.Equals("功率变化(dB)"))
  238. {
  239. List<ModulationMatchParameter> ModulationTable = ReadParameters(sheet, num);
  240. foreach (var item in ModulationTable)
  241. {
  242. if (pars.ModulaParameters.ContainsKey(item.CfgRelative))
  243. {
  244. ShowMessage(MsgType.Error, string.Format("调制对应表:{0} 存在同名参数:{1},无法重复添加参数", sheet.Name, item.CfgRelative));
  245. continue;
  246. }
  247. pars.ModulaParameters.Add(item.CfgRelative, item);
  248. }
  249. }
  250. }
  251. return pars;
  252. }
  253. List<ModulationMatchParameter> ReadParameters(Spire.Xls.Worksheet sheet, int rowIndex)
  254. {
  255. List<ModulationMatchParameter> ModulationTable = new List<ModulationMatchParameter>();
  256. string col1 = sheet.Range[rowIndex , 1].Value2 != null ? sheet.Range[rowIndex , 1].Value2.ToString() : "";
  257. string col2 = sheet.Range[rowIndex , 2].Value2 != null ? sheet.Range[rowIndex , 2].Value2.ToString() : "";
  258. string col3 = sheet.Range[rowIndex , 3].Value2 != null ? sheet.Range[rowIndex , 3].Value2.ToString() : "";
  259. if (!(col1 == "功率变化(dB)" && col2 == "调制指数(mf)" && col3 == "最大频偏(KHz)"))
  260. {
  261. ShowMessage(MsgType.Error, "'调制对应表'行首标题位置和格式不正确");
  262. return ModulationTable;
  263. }
  264. int num = rowIndex ;
  265. //默认最多30行
  266. while (num++ < rowIndex + 100)
  267. {
  268. col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  269. col2 = sheet.Range[num, 2].Value2 != null ? sheet.Range[num, 2].Value2.ToString() : "";
  270. col3 = sheet.Range[num, 3].Value2 != null ? sheet.Range[num, 3].Value2.ToString() : "";
  271. if (string.IsNullOrEmpty(col1) || string.IsNullOrEmpty(col2) || string.IsNullOrEmpty(col3))
  272. {
  273. break;
  274. }
  275. ModulationMatchParameter ObjModulation = new ModulationMatchParameter();
  276. ObjModulation.CfgRelative = col1;
  277. ObjModulation.CfgModulation = col2;
  278. ObjModulation.CfgFreqOffset = col3;
  279. ModulationTable.Add(ObjModulation);
  280. }
  281. return ModulationTable;
  282. }
  283. /// <summary>
  284. /// 一个调制指数的配置信息
  285. /// </summary>
  286. public class MatchModulaPara
  287. {
  288. public Dictionary<string, ModulationMatchParameter> ModulaParameters;
  289. public MatchModulaPara()
  290. {
  291. ModulaParameters = new Dictionary<string, ModulationMatchParameter>();
  292. }
  293. public string GetModulaParameter(string name)
  294. {
  295. if (string.IsNullOrEmpty(name))
  296. return null;
  297. if (this.ModulaParameters.ContainsKey(name) == false)
  298. return null;
  299. string val1 = this.ModulaParameters[name].CfgModulation.Trim();
  300. return val1;
  301. }
  302. public string GetFreqOffsetParameter(string name)
  303. {
  304. if (string.IsNullOrEmpty(name))
  305. return null;
  306. if (this.ModulaParameters.ContainsKey(name) == false)
  307. return null;
  308. string val2 = this.ModulaParameters[name].CfgFreqOffset.Trim();
  309. return val2;
  310. }
  311. }
  312. /// <summary>
  313. /// 调制指数3个对应项
  314. /// </summary>
  315. public class ModulationMatchParameter
  316. {
  317. /// <summary>
  318. /// 功率变化(dB)
  319. /// </summary>
  320. public string CfgRelative{ get; set; }
  321. /// <summary>
  322. /// 调制指数(mf)
  323. /// </summary>
  324. public string CfgModulation { get; set; }
  325. /// <summary>
  326. /// 最大频偏(KHz)
  327. /// </summary>
  328. public string CfgFreqOffset { get; set; }
  329. }
  330. public class ConfigParameter
  331. {
  332. /// <summary>
  333. /// 串口
  334. /// </summary>
  335. public string ComPort { set; get; }
  336. /// <summary>
  337. /// 输出损耗
  338. /// </summary>
  339. public double OutLoss { set; get; }
  340. /// <summary>
  341. /// 产品测试的起始频率
  342. /// </summary>
  343. public double StartFrequency { set; get; }
  344. /// <summary>
  345. /// 产品测试的频率步进
  346. /// </summary>
  347. public double StepFrequency { set; get; }
  348. /// <summary>
  349. /// 产品测试的频点数量
  350. /// </summary>
  351. public int FrequencyNumber { set; get; }
  352. /// <summary>
  353. /// 产品的工作频带上限(终止频率)
  354. /// 功能:用于判断从起始频率按一定的步进测试是否超出产品工作频段上限
  355. /// </summary>
  356. public double StopFrequency { set; get; }
  357. /// <summary>
  358. ///设置频谱仪的SPAN
  359. /// </summary>
  360. public string SPAN { set; get; }
  361. /// <summary>
  362. /// 设置参考电平
  363. /// </summary>
  364. public string REF { set; get; }
  365. /// <summary>
  366. /// 设置RBW
  367. /// </summary>
  368. public string RBW { set; get; }
  369. /// <summary>
  370. /// 设置VBW
  371. /// </summary>
  372. public string VBW { set; get; }
  373. /// <summary>
  374. /// 控制延时
  375. /// </summary>
  376. public int ControlDelay { set; get; }
  377. /// <summary>
  378. /// 调制系数下限
  379. /// </summary>
  380. public double ModulationIndexLower { get; set; }
  381. /// <summary>
  382. ///调制系数上限
  383. /// </summary>
  384. public double ModulationIndexUpper { get; set; }
  385. }
  386. public class OutData
  387. {
  388. /// <summary>
  389. ///调制系数
  390. /// </summary>
  391. public string[] ModulationIndex { set; get; }
  392. /// <summary>
  393. /// 最大频偏
  394. /// </summary>
  395. public string[] MaxFreqOffset { set; get; }
  396. }
  397. //public class DataType
  398. //{
  399. // /// <summary>
  400. // /// 测试名称
  401. // /// </summary>
  402. // public string Test_name { set; get; }
  403. // /// <summary>
  404. // /// 指标下限
  405. // /// </summary>
  406. // public double Lower { set; get; }
  407. // /// <summary>
  408. // /// 指标上限
  409. // /// </summary>
  410. // public double Upper { set; get; }
  411. // /// <summary>
  412. // /// 测试值
  413. // /// </summary>
  414. // public double TestVal { set; get; }
  415. // /// <summary>
  416. // /// 判断结果
  417. // /// </summary>
  418. // public bool Result { set; get; }
  419. //}
  420. }
  421. }