HarmonicSuppression.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Tps_LQ_Transmitter.com;
  8. namespace Tps_LQ_Transmitter.models
  9. {
  10. class HarmonicSupperssion : BaseModel
  11. {
  12. public HarmonicSupperssion()
  13. {
  14. TemplateName = "谐波抑制测试";
  15. }
  16. /// <summary>
  17. /// 谐波抑制测试
  18. /// </summary>
  19. public override bool Run(TestNode parameters)
  20. {
  21. double y_value, x_value;
  22. Random random = new Random();
  23. //获取仪器
  24. var SA = this.tps.GetDevice("频谱仪");
  25. TransmitterSerialPort SerialClient = new TransmitterSerialPort();
  26. OutData Data = new OutData();
  27. DataType HarmonicSuppressionPrint = new DataType();
  28. if (SA == null)
  29. {
  30. ShowMessage(MsgType.Error, string.Format("仪器不齐全,{0}/{1}无法运行", parameters.Channel, parameters.Name));
  31. return false;
  32. }
  33. OpenExcel("谐波抑制", out Spire.Xls.Workbook workbook, out Spire.Xls.Worksheet sheet);
  34. if (sheet == null)
  35. {
  36. ShowMessage(MsgType.Error, "找不到" + tps.TestProject + "模板.xlsx");
  37. return false;
  38. }
  39. sheet.Range["L1"].Value2 = tps.Serial;
  40. ConfigParameter PowerPara = new ConfigParameter();
  41. PowerPara.ComPort = parameters.Parameters.GetParameter<string>("串口");
  42. PowerPara.OutLoss = parameters.Parameters.GetParameter<double>("输出损耗");
  43. PowerPara.StartFrequency = parameters.Parameters.GetParameter<double>("起始频率");
  44. PowerPara.StepFrequency = parameters.Parameters.GetParameter<double>("频率步进");
  45. PowerPara.FrequencyNumber = parameters.Parameters.GetParameter<int>("频点数量");
  46. PowerPara.StopFrequency = parameters.Parameters.GetParameter<double>("终止频率");
  47. PowerPara.SPAN = parameters.Parameters.GetParameter<string>("扫描带宽(SPAN)");
  48. PowerPara.REF = parameters.Parameters.GetParameter<string>("参考电平(REF)");
  49. PowerPara.SmallSignalREF = parameters.Parameters.GetParameter<string>("小信号参考电平(REF)");
  50. PowerPara.RBW = parameters.Parameters.GetParameter<string>("分辨率带宽(RBW)");
  51. PowerPara.VBW = parameters.Parameters.GetParameter<string>("视频带宽(VBW)");
  52. PowerPara.HarmonicTime = parameters.Parameters.GetParameterToArray<int>("谐波次数");
  53. PowerPara.ControlDelay = parameters.Parameters.GetParameter<int>("控制延时");
  54. PowerPara.HarmonicSuppressionLower = parameters.Parameters.GetParameter<double>("谐波抑制下限");
  55. if ((PowerPara.StartFrequency == 0) || ((PowerPara.StepFrequency == 0) && (PowerPara.FrequencyNumber == 0)) || (PowerPara.StopFrequency == 0)
  56. || (PowerPara.SPAN == null) || (PowerPara.REF == null) ||(PowerPara.SmallSignalREF==null)|| (PowerPara.RBW == null) || (PowerPara.VBW == null) || (PowerPara.HarmonicTime == null)
  57. ||(PowerPara.HarmonicSuppressionLower==0))
  58. {
  59. ShowMessage(MsgType.Error, string.Format("配置文件中频率参数为空,{0}/{1}无法运行", parameters.Channel, parameters.Name));
  60. return false;
  61. }
  62. if (PowerPara.ControlDelay == 0)
  63. {
  64. PowerPara.ControlDelay = 10;
  65. }
  66. if (true)//需具备仪器
  67. {
  68. SA.Write("仪器复位"); SA.Query("OPC");
  69. SA.Write("SPAN", PowerPara.SPAN); SA.Query("OPC");
  70. SA.Write("RBW", PowerPara.RBW); SA.Query("OPC");
  71. SA.Write("VBW", PowerPara.VBW); SA.Query("OPC");
  72. // SA.Write("SingleOrCont", "0"); 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. int harmonic_num;
  84. double Power, Freq;
  85. Data.HarmonicSuppression = new double[PowerPara.FrequencyNumber,PowerPara.HarmonicTime.Length];
  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(true)//需具备仪器
  94. {
  95. //控制
  96. // SerialClient.DUT_Transmitter_Ctrol(PowerPara.ComPort, Convert.ToByte(point + 1));
  97. Thread.Sleep(PowerPara.ControlDelay);//单位ms
  98. SA.Write("REF", PowerPara.REF); SA.Query("OPC");
  99. SA.Write("CENTER", CenterFreq.ToString()); SA.Query("OPC");
  100. SA.Write("SingleOrCont", "0"); SA.Query("OPC");
  101. PsaPeakValue_Tracedata(SA, out y_value, out x_value, true);
  102. Freq=x_value;
  103. Power = y_value;
  104. }
  105. double[] HarmonicTimeVal = new double[PowerPara.HarmonicTime.Length];
  106. double HarmonicMinTimeVal = 0;
  107. HarmonicSuppressionPrint.Lower = PowerPara.HarmonicSuppressionLower;
  108. if (true)//需具备仪器
  109. {
  110. SA.Write("REF", PowerPara.SmallSignalREF); SA.Query("OPC");
  111. for (harmonic_num = 0; harmonic_num < PowerPara.HarmonicTime.Length; harmonic_num++)
  112. {
  113. SA.Write("CENTER", (Freq/1000000 * PowerPara.HarmonicTime[harmonic_num]).ToString()); SA.Query("OPC");//N次谐波
  114. // SA.Write("SingleOrCont", "0"); SA.Query("OPC");
  115. SA.Write("单次扫描"); SA.Query("OPC");
  116. PsaPeakValue_Tracedata(SA, out y_value, out x_value, false);
  117. Data.HarmonicSuppression[point, harmonic_num] = Math.Round(Power - y_value, 2);
  118. HarmonicSuppressionPrint.Test_name = parameters.Channel + "-"+ PowerPara.HarmonicTime[harmonic_num].ToString()+ "次-谐波抑制测试-" + (Freq / 1000000).ToString() + "MHz";
  119. HarmonicTimeVal[harmonic_num] = Data.HarmonicSuppression[point, harmonic_num];
  120. if (HarmonicTimeVal[harmonic_num] >= HarmonicSuppressionPrint.Lower)
  121. {
  122. HarmonicSuppressionPrint.Result = "是";
  123. }
  124. else
  125. {
  126. HarmonicSuppressionPrint.Result = "否";
  127. }
  128. tps.TestTableAddCell(HarmonicSuppressionPrint.Test_name, HarmonicSuppressionPrint.Lower.ToString(), "/", HarmonicTimeVal[harmonic_num].ToString(), HarmonicSuppressionPrint.Result);
  129. }
  130. if (PowerPara.HarmonicTime.Length > 1)//计算几次谐波中的最小值
  131. {
  132. HarmonicMinTimeVal = HarmonicTimeVal.Min();
  133. }
  134. else
  135. {
  136. HarmonicMinTimeVal = HarmonicTimeVal[0];
  137. }
  138. }
  139. HarmonicSuppressionPrint.Test_name = parameters.Channel + "-谐波抑制测试-" + (Freq / 1000000).ToString() + "MHz";
  140. HarmonicSuppressionPrint.TestVal = HarmonicMinTimeVal;
  141. // HarmonicSuppressionPrint.TestVal = random.Next(4000, 12000) / 100.0;//随机数
  142. if (HarmonicSuppressionPrint.TestVal >= HarmonicSuppressionPrint.Lower)
  143. {
  144. HarmonicSuppressionPrint.Result = "是";
  145. }
  146. else
  147. {
  148. HarmonicSuppressionPrint.Result = "否";
  149. }
  150. tps.TestTableAddCell(HarmonicSuppressionPrint.Test_name, HarmonicSuppressionPrint.Lower.ToString(), "/", HarmonicSuppressionPrint.TestVal.ToString(), HarmonicSuppressionPrint.Result);
  151. if (parameters.Channel == "通道1")
  152. {
  153. if (point == 0)
  154. {
  155. WriteExcelData(sheet, point - 1, 1, "测试名称", "下限", "上限", "测试值", "通过");
  156. }
  157. WriteExcelData(sheet, point, 1, HarmonicSuppressionPrint.Test_name, HarmonicSuppressionPrint.Lower.ToString(),"/", HarmonicSuppressionPrint.TestVal.ToString(), HarmonicSuppressionPrint.Result);
  158. // tps.SetTestTableCellValue(point, 15, HarmonicSuppressionPrint.Result, HarmonicSuppressionPrint.TestVal);
  159. }
  160. else if (parameters.Channel == "通道2")
  161. {
  162. if (point == 0)
  163. {
  164. WriteExcelData(sheet, point-1, 2, "测试名称", "下限", "上限", "测试值", "通过");
  165. }
  166. WriteExcelData(sheet, point, 2, HarmonicSuppressionPrint.Test_name, HarmonicSuppressionPrint.Lower.ToString(), "/", HarmonicSuppressionPrint.TestVal.ToString(), HarmonicSuppressionPrint.Result);
  167. // tps.SetTestTableCellValue(point + 15, 15, HarmonicSuppressionPrint.Result, HarmonicSuppressionPrint.TestVal);
  168. }
  169. }
  170. SaveExcel(workbook);
  171. return true;
  172. }
  173. public void PsaPeakValue_Tracedata(AppLibs.Devices.IVISA psa, out double Y_Maxvalue, out double X_Maxvalue, bool IsReturnX = false)
  174. {
  175. System.Threading.Thread.Sleep(20);
  176. X_Maxvalue = 0;
  177. Y_Maxvalue = 0;
  178. psa.Write("单次扫描");
  179. psa.Query("OPC");
  180. string tracedata = psa.Query("读曲线");
  181. string[] tracedatas = tracedata.Split(',');
  182. double[] tracedata_double = new double[tracedatas.Length-1];
  183. for (int i = 0; i < tracedatas.Length-1; i++)
  184. {
  185. tracedata_double[i] = double.Parse(tracedatas[i]);
  186. }
  187. Y_Maxvalue = tracedata_double.Max();
  188. if (IsReturnX)
  189. {
  190. int x = Array.IndexOf(tracedata_double, Y_Maxvalue);
  191. double startfreq = double.Parse(psa.Query("读起始频率"));
  192. double stopfreq = double.Parse(psa.Query("读截止频率"));
  193. double counts = double.Parse(psa.Query("测试点数读取"));
  194. X_Maxvalue = startfreq + (stopfreq - startfreq) * x / (counts - 2);
  195. }
  196. Y_Maxvalue = Math.Round(Y_Maxvalue, 3);
  197. }
  198. public class ConfigParameter
  199. {
  200. /// <summary>
  201. /// 串口
  202. /// </summary>
  203. public string ComPort { set; get; }
  204. /// <summary>
  205. /// 输出损耗
  206. /// </summary>
  207. public double OutLoss { set; get; }
  208. /// <summary>
  209. /// 产品测试的起始频率
  210. /// </summary>
  211. public double StartFrequency { set; get; }
  212. /// <summary>
  213. /// 产品测试的频率步进
  214. /// </summary>
  215. public double StepFrequency { set; get; }
  216. /// <summary>
  217. /// 产品测试的频点数量
  218. /// </summary>
  219. public int FrequencyNumber { set; get; }
  220. /// <summary>
  221. /// 产品的工作频带上限(终止频率)
  222. /// 功能:用于判断从起始频率按一定的步进测试是否超出产品工作频段上限
  223. /// </summary>
  224. public double StopFrequency { set; get; }
  225. /// <summary>
  226. ///设置频谱仪的SPAN
  227. /// </summary>
  228. public string SPAN { set; get; }
  229. /// <summary>
  230. /// 设置参考电平
  231. /// </summary>
  232. public string REF { set; get; }
  233. /// <summary>
  234. /// 设置小信号参考电平
  235. /// </summary>
  236. public string SmallSignalREF { set; get; }
  237. /// <summary>
  238. /// 设置RBW
  239. /// </summary>
  240. public string RBW { set; get; }
  241. /// <summary>
  242. /// 设置VBW
  243. /// </summary>
  244. public string VBW { set; get; }
  245. /// <summary>
  246. /// 谐波次数
  247. /// </summary>
  248. public int[] HarmonicTime { set; get; }
  249. /// <summary>
  250. /// 控制延时
  251. /// </summary>
  252. public int ControlDelay { set; get; }
  253. /// <summary>
  254. /// 谐波抑制下限
  255. /// </summary>
  256. public double HarmonicSuppressionLower { set; get; }
  257. }
  258. public class OutData
  259. {
  260. /// <summary>
  261. //谐波抑制
  262. /// </summary>
  263. public double[,] HarmonicSuppression { set; get; }
  264. }
  265. //public class DataType
  266. //{
  267. // /// <summary>
  268. // /// 测试名称
  269. // /// </summary>
  270. // public string Test_name { set; get; }
  271. // /// <summary>
  272. // /// 指标下限
  273. // /// </summary>
  274. // public double Lower { set; get; }
  275. // /// <summary>
  276. // /// 指标上限
  277. // /// </summary>
  278. // public double Upper { set; get; }
  279. // /// <summary>
  280. // /// 测试值
  281. // /// </summary>
  282. // public double TestVal { set; get; }
  283. // /// <summary>
  284. // /// 判断结果
  285. // /// </summary>
  286. // public bool Result { set; get; }
  287. //}
  288. }
  289. }