HarmonicSuppression.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. if (true)//需具备仪器
  108. {
  109. SA.Write("REF", PowerPara.SmallSignalREF); SA.Query("OPC");
  110. for (harmonic_num = 0; harmonic_num < PowerPara.HarmonicTime.Length; harmonic_num++)
  111. {
  112. SA.Write("CENTER", (Freq/1000000 * PowerPara.HarmonicTime[harmonic_num]).ToString()); SA.Query("OPC");//N次谐波
  113. SA.Write("SingleOrCont", "0"); SA.Query("OPC");
  114. PsaPeakValue_Tracedata(SA, out y_value, out x_value, false);
  115. Data.HarmonicSuppression[point, harmonic_num] = Math.Round(Power - y_value, 2);
  116. HarmonicTimeVal[harmonic_num] = Data.HarmonicSuppression[point, harmonic_num];
  117. }
  118. if (PowerPara.HarmonicTime.Length > 1)//计算几次谐波中的最小值
  119. {
  120. HarmonicMinTimeVal = HarmonicTimeVal.Min();
  121. }
  122. else
  123. {
  124. HarmonicMinTimeVal = HarmonicTimeVal[0];
  125. }
  126. }
  127. HarmonicSuppressionPrint.Test_name = parameters.Channel + "-谐波抑制测试-" + (Freq / 1000000).ToString() + "MHz";
  128. HarmonicSuppressionPrint.Lower = PowerPara.HarmonicSuppressionLower;
  129. HarmonicSuppressionPrint.TestVal = HarmonicMinTimeVal;
  130. // HarmonicSuppressionPrint.TestVal = random.Next(4000, 12000) / 100.0;//随机数
  131. if (HarmonicSuppressionPrint.TestVal >= HarmonicSuppressionPrint.Lower)
  132. {
  133. HarmonicSuppressionPrint.Result = "是";
  134. }
  135. else
  136. {
  137. HarmonicSuppressionPrint.Result = "否";
  138. }
  139. tps.TestTableAddCell(HarmonicSuppressionPrint.Test_name, HarmonicSuppressionPrint.Lower.ToString(), "/", HarmonicSuppressionPrint.TestVal.ToString(), HarmonicSuppressionPrint.Result);
  140. if (parameters.Channel == "通道1")
  141. {
  142. if (point == 0)
  143. {
  144. WriteExcelData(sheet, point - 1, 1, "测试名称", "下限", "上限", "测试值", "通过");
  145. }
  146. WriteExcelData(sheet, point, 1, HarmonicSuppressionPrint.Test_name, HarmonicSuppressionPrint.Lower.ToString(),"/", HarmonicSuppressionPrint.TestVal.ToString(), HarmonicSuppressionPrint.Result);
  147. // tps.SetTestTableCellValue(point, 15, HarmonicSuppressionPrint.Result, HarmonicSuppressionPrint.TestVal);
  148. }
  149. else if (parameters.Channel == "通道2")
  150. {
  151. if (point == 0)
  152. {
  153. WriteExcelData(sheet, point-1, 2, "测试名称", "下限", "上限", "测试值", "通过");
  154. }
  155. WriteExcelData(sheet, point, 2, HarmonicSuppressionPrint.Test_name, HarmonicSuppressionPrint.Lower.ToString(), "/", HarmonicSuppressionPrint.TestVal.ToString(), HarmonicSuppressionPrint.Result);
  156. // tps.SetTestTableCellValue(point + 15, 15, HarmonicSuppressionPrint.Result, HarmonicSuppressionPrint.TestVal);
  157. }
  158. }
  159. SaveExcel(workbook);
  160. return true;
  161. }
  162. public void PsaPeakValue_Tracedata(AppLibs.Devices.IVISA psa, out double Y_Maxvalue, out double X_Maxvalue, bool IsReturnX = false)
  163. {
  164. System.Threading.Thread.Sleep(20);
  165. X_Maxvalue = 0;
  166. Y_Maxvalue = 0;
  167. psa.Write("单次扫描");
  168. psa.Query("OPC");
  169. string tracedata = psa.Query("读曲线");
  170. string[] tracedatas = tracedata.Split(',');
  171. double[] tracedata_double = new double[tracedatas.Length-1];
  172. for (int i = 0; i < tracedatas.Length-1; i++)
  173. {
  174. tracedata_double[i] = double.Parse(tracedatas[i]);
  175. }
  176. Y_Maxvalue = tracedata_double.Max();
  177. if (IsReturnX)
  178. {
  179. int x = Array.IndexOf(tracedata_double, Y_Maxvalue);
  180. double startfreq = double.Parse(psa.Query("读起始频率"));
  181. double stopfreq = double.Parse(psa.Query("读截止频率"));
  182. double counts = double.Parse(psa.Query("测试点数读取"));
  183. X_Maxvalue = startfreq + (stopfreq - startfreq) * x / (counts - 2);
  184. }
  185. Y_Maxvalue = Math.Round(Y_Maxvalue, 3);
  186. }
  187. public class ConfigParameter
  188. {
  189. /// <summary>
  190. /// 串口
  191. /// </summary>
  192. public string ComPort { set; get; }
  193. /// <summary>
  194. /// 输出损耗
  195. /// </summary>
  196. public double OutLoss { set; get; }
  197. /// <summary>
  198. /// 产品测试的起始频率
  199. /// </summary>
  200. public double StartFrequency { set; get; }
  201. /// <summary>
  202. /// 产品测试的频率步进
  203. /// </summary>
  204. public double StepFrequency { set; get; }
  205. /// <summary>
  206. /// 产品测试的频点数量
  207. /// </summary>
  208. public int FrequencyNumber { set; get; }
  209. /// <summary>
  210. /// 产品的工作频带上限(终止频率)
  211. /// 功能:用于判断从起始频率按一定的步进测试是否超出产品工作频段上限
  212. /// </summary>
  213. public double StopFrequency { set; get; }
  214. /// <summary>
  215. ///设置频谱仪的SPAN
  216. /// </summary>
  217. public string SPAN { set; get; }
  218. /// <summary>
  219. /// 设置参考电平
  220. /// </summary>
  221. public string REF { set; get; }
  222. /// <summary>
  223. /// 设置小信号参考电平
  224. /// </summary>
  225. public string SmallSignalREF { set; get; }
  226. /// <summary>
  227. /// 设置RBW
  228. /// </summary>
  229. public string RBW { set; get; }
  230. /// <summary>
  231. /// 设置VBW
  232. /// </summary>
  233. public string VBW { set; get; }
  234. /// <summary>
  235. /// 谐波次数
  236. /// </summary>
  237. public int[] HarmonicTime { set; get; }
  238. /// <summary>
  239. /// 控制延时
  240. /// </summary>
  241. public int ControlDelay { set; get; }
  242. /// <summary>
  243. /// 谐波抑制下限
  244. /// </summary>
  245. public double HarmonicSuppressionLower { set; get; }
  246. }
  247. public class OutData
  248. {
  249. /// <summary>
  250. //谐波抑制
  251. /// </summary>
  252. public double[,] HarmonicSuppression { set; get; }
  253. }
  254. //public class DataType
  255. //{
  256. // /// <summary>
  257. // /// 测试名称
  258. // /// </summary>
  259. // public string Test_name { set; get; }
  260. // /// <summary>
  261. // /// 指标下限
  262. // /// </summary>
  263. // public double Lower { set; get; }
  264. // /// <summary>
  265. // /// 指标上限
  266. // /// </summary>
  267. // public double Upper { set; get; }
  268. // /// <summary>
  269. // /// 测试值
  270. // /// </summary>
  271. // public double TestVal { set; get; }
  272. // /// <summary>
  273. // /// 判断结果
  274. // /// </summary>
  275. // public bool Result { set; get; }
  276. //}
  277. }
  278. }