ModulationIndexAndMaxFreqOffset.cs 18 KB

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