ModulationIndexAndMaxFreqOffset.cs 19 KB

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