ModulationIndexAndMaxFreqOffset.cs 20 KB

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