HarmonicSuppression.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. // MatchComPara CfigComParas = new MatchComPara();
  42. //CfigComParas = LoadComWorkBook();
  43. //if (CfigComParas == null)
  44. //{
  45. // return false;
  46. //}
  47. //byte FourthByte = 0x00;
  48. //string ComPort = CfigComParas.GetComPort("1");
  49. //byte ThridByte = Convert.ToByte(CfigComParas.GetThirdByte("1"), 16);
  50. // PowerPara.OutLoss = parameters.Parameters.GetParameter<double>("输出损耗");
  51. PowerPara.SPAN = parameters.Parameters.GetParameter<string>("扫描带宽(SPAN)");
  52. PowerPara.REF = parameters.Parameters.GetParameter<string>("参考电平(REF)");
  53. PowerPara.SmallSignalREF = parameters.Parameters.GetParameter<string>("小信号参考电平(REF)");
  54. PowerPara.RBW = parameters.Parameters.GetParameter<string>("分辨率带宽(RBW)");
  55. PowerPara.VBW = parameters.Parameters.GetParameter<string>("视频带宽(VBW)");
  56. PowerPara.HarmonicTime = parameters.Parameters.GetParameterToArray<int>("谐波次数");
  57. //PowerPara.ControlDelay = parameters.Parameters.GetParameter<int>("控制延时");
  58. PowerPara.HarmonicSuppressionLower = parameters.Parameters.GetParameter<double>("谐波抑制下限");
  59. PowerPara.HarmonicSuppressionUpper = parameters.Parameters.GetParameter<double>("谐波抑制上限");
  60. if ( (PowerPara.SPAN == null) || (PowerPara.REF == null) ||(PowerPara.SmallSignalREF==null)|| (PowerPara.RBW == null) || (PowerPara.VBW == null) || (PowerPara.HarmonicTime == null)
  61. ||(PowerPara.HarmonicSuppressionLower==0))
  62. {
  63. ShowMessage(MsgType.Error, string.Format("配置文件中频率参数为空,{0}/{1}无法运行", parameters.Channel, parameters.Name));
  64. return false;
  65. }
  66. //if (PowerPara.ControlDelay == 0)
  67. //{
  68. // PowerPara.ControlDelay = 10;
  69. //}
  70. //SerialClient.SerialOpen(ComPort);
  71. if (true)//需具备仪器
  72. {
  73. SA.Write("仪器复位"); SA.Query("OPC");
  74. SA.Write("SPAN", PowerPara.SPAN); SA.Query("OPC");
  75. SA.Write("RBW", PowerPara.RBW); SA.Query("OPC");
  76. SA.Write("VBW", PowerPara.VBW); SA.Query("OPC");
  77. // SA.Write("SingleOrCont", "0"); SA.Query("OPC");
  78. }
  79. double CenterFreq;
  80. int harmonic_num;
  81. double Power, Freq;
  82. Data.HarmonicSuppression = new double[parameters.PointTotal, PowerPara.HarmonicTime.Length];
  83. //for (int point = 0; ; point++)
  84. {
  85. //CenterFreq = PowerPara.StartFrequency + PowerPara.StepFrequency * point;
  86. if(true)//需具备仪器
  87. {
  88. //控制
  89. // SerialClient.DUT_Transmitter_Ctrol(PowerPara.ComPort, Convert.ToByte(point + 1));
  90. //Thread.Sleep(PowerPara.ControlDelay);//单位ms
  91. SA.Write("REF", PowerPara.REF); SA.Query("OPC");
  92. SA.Write("CENTER", parameters.CenterFreq.ToString()); SA.Query("OPC");
  93. SA.Write("SingleOrCont", "0"); SA.Query("OPC");
  94. PsaPeakValue_Tracedata(SA, out y_value, out x_value, true);
  95. Freq=x_value;
  96. Power = y_value;
  97. }
  98. double[] HarmonicTimeVal = new double[PowerPara.HarmonicTime.Length];
  99. double HarmonicMinTimeVal = 0;
  100. HarmonicSuppressionPrint.Lower = PowerPara.HarmonicSuppressionLower;
  101. HarmonicSuppressionPrint.Upper = PowerPara.HarmonicSuppressionUpper;
  102. if (true)//需具备仪器
  103. {
  104. SA.Write("REF", PowerPara.SmallSignalREF); SA.Query("OPC");
  105. for (harmonic_num = 0; harmonic_num < PowerPara.HarmonicTime.Length; harmonic_num++)
  106. {
  107. SA.Write("CENTER", (Freq/1000000 * PowerPara.HarmonicTime[harmonic_num]).ToString()); SA.Query("OPC");//N次谐波
  108. // SA.Write("SingleOrCont", "0"); SA.Query("OPC");
  109. SA.Write("单次扫描"); SA.Query("OPC");
  110. PsaPeakValue_Tracedata(SA, out y_value, out x_value, false);
  111. Data.HarmonicSuppression[parameters.PointIndex, harmonic_num] = Math.Round(Power - y_value, 2);
  112. HarmonicSuppressionPrint.Test_name = parameters.Channel + "-"+ PowerPara.HarmonicTime[harmonic_num].ToString()+ "次-谐波抑制测试-" + parameters.CenterFreq.ToString() + "MHz";
  113. HarmonicTimeVal[harmonic_num] = Data.HarmonicSuppression[parameters.PointIndex, harmonic_num];
  114. if ((HarmonicTimeVal[harmonic_num] >= HarmonicSuppressionPrint.Lower) && (HarmonicTimeVal[harmonic_num] <= HarmonicSuppressionPrint.Upper))
  115. {
  116. HarmonicSuppressionPrint.Result = "是";
  117. }
  118. else
  119. {
  120. HarmonicSuppressionPrint.Result = "否";
  121. }
  122. tps.TestTableAddCell(HarmonicSuppressionPrint.Test_name, HarmonicSuppressionPrint.Lower.ToString(), HarmonicSuppressionPrint.Upper.ToString(), HarmonicTimeVal[harmonic_num].ToString(), HarmonicSuppressionPrint.Result);
  123. if (parameters.Channel == "通道1")
  124. {
  125. WriteExcelData(sheet, parameters.PointIndex, 1+2*(harmonic_num+1), HarmonicSuppressionPrint.Test_name, HarmonicSuppressionPrint.Lower.ToString(), HarmonicSuppressionPrint.Upper.ToString(), HarmonicSuppressionPrint.TestVal.ToString(), HarmonicSuppressionPrint.Result);
  126. // tps.SetTestTableCellValue(point, 15, HarmonicSuppressionPrint.Result, HarmonicSuppressionPrint.TestVal);
  127. }
  128. else if (parameters.Channel == "通道2")
  129. {
  130. WriteExcelData(sheet, parameters.PointIndex, 2 + 2 * (harmonic_num + 1), HarmonicSuppressionPrint.Test_name, HarmonicSuppressionPrint.Lower.ToString(), HarmonicSuppressionPrint.Upper.ToString(), HarmonicSuppressionPrint.TestVal.ToString(), HarmonicSuppressionPrint.Result);
  131. // tps.SetTestTableCellValue(point + 15, 15, HarmonicSuppressionPrint.Result, HarmonicSuppressionPrint.TestVal);
  132. }
  133. }
  134. if (PowerPara.HarmonicTime.Length > 1)//计算几次谐波中的最小值
  135. {
  136. HarmonicMinTimeVal = HarmonicTimeVal.Min();
  137. }
  138. else
  139. {
  140. HarmonicMinTimeVal = HarmonicTimeVal[0];
  141. }
  142. }
  143. HarmonicSuppressionPrint.Test_name = parameters.Channel + "-谐波抑制测试-" + parameters.CenterFreq.ToString() + "MHz";
  144. HarmonicSuppressionPrint.TestVal = HarmonicMinTimeVal;
  145. // HarmonicSuppressionPrint.TestVal = random.Next(4000, 12000) / 100.0;//随机数
  146. if ((HarmonicTimeVal[harmonic_num] >= HarmonicSuppressionPrint.Lower) && (HarmonicTimeVal[harmonic_num] <= HarmonicSuppressionPrint.Upper))
  147. {
  148. HarmonicSuppressionPrint.Result = "是";
  149. }
  150. else
  151. {
  152. HarmonicSuppressionPrint.Result = "否";
  153. }
  154. tps.TestTableAddCell(HarmonicSuppressionPrint.Test_name, HarmonicSuppressionPrint.Lower.ToString(), HarmonicSuppressionPrint.Upper.ToString(), HarmonicSuppressionPrint.TestVal.ToString(), HarmonicSuppressionPrint.Result);
  155. if (parameters.Channel == "通道1")
  156. {
  157. WriteExcelData(sheet, parameters.PointIndex, 1, HarmonicSuppressionPrint.Test_name, HarmonicSuppressionPrint.Lower.ToString(), HarmonicSuppressionPrint.Upper.ToString(), HarmonicSuppressionPrint.TestVal.ToString(), HarmonicSuppressionPrint.Result);
  158. // tps.SetTestTableCellValue(point, 15, HarmonicSuppressionPrint.Result, HarmonicSuppressionPrint.TestVal);
  159. }
  160. else if (parameters.Channel == "通道2")
  161. {
  162. WriteExcelData(sheet, parameters.PointIndex, 2, HarmonicSuppressionPrint.Test_name, HarmonicSuppressionPrint.Lower.ToString(), HarmonicSuppressionPrint.Upper.ToString(), HarmonicSuppressionPrint.TestVal.ToString(), HarmonicSuppressionPrint.Result);
  163. // tps.SetTestTableCellValue(point + 15, 15, HarmonicSuppressionPrint.Result, HarmonicSuppressionPrint.TestVal);
  164. }
  165. }
  166. SaveExcel(workbook);
  167. return true;
  168. }
  169. public void PsaPeakValue_Tracedata(AppLibs.Devices.IVISA psa, out double Y_Maxvalue, out double X_Maxvalue, bool IsReturnX = false)
  170. {
  171. System.Threading.Thread.Sleep(20);
  172. X_Maxvalue = 0;
  173. Y_Maxvalue = 0;
  174. psa.Write("单次扫描");
  175. psa.Query("OPC");
  176. string tracedata = psa.Query("读曲线");
  177. string[] tracedatas = tracedata.Split(',');
  178. double[] tracedata_double = new double[tracedatas.Length-1];
  179. for (int i = 0; i < tracedatas.Length-1; i++)
  180. {
  181. tracedata_double[i] = double.Parse(tracedatas[i]);
  182. }
  183. Y_Maxvalue = tracedata_double.Max();
  184. if (IsReturnX)
  185. {
  186. int x = Array.IndexOf(tracedata_double, Y_Maxvalue);
  187. double startfreq = double.Parse(psa.Query("读起始频率"));
  188. double stopfreq = double.Parse(psa.Query("读截止频率"));
  189. double counts = double.Parse(psa.Query("测试点数读取"));
  190. X_Maxvalue = startfreq + (stopfreq - startfreq) * x / (counts - 2);
  191. }
  192. Y_Maxvalue = Math.Round(Y_Maxvalue, 3);
  193. }
  194. public class ConfigParameter
  195. {
  196. /// <summary>
  197. /// 串口
  198. /// </summary>
  199. public string ComPort { set; get; }
  200. /// <summary>
  201. /// 输出损耗
  202. /// </summary>
  203. public double OutLoss { set; get; }
  204. /// <summary>
  205. /// 产品测试的起始频率
  206. /// </summary>
  207. public double StartFrequency { set; get; }
  208. /// <summary>
  209. /// 产品测试的频率步进
  210. /// </summary>
  211. public double StepFrequency { set; get; }
  212. /// <summary>
  213. /// 产品测试的频点数量
  214. /// </summary>
  215. public int FrequencyNumber { set; get; }
  216. /// <summary>
  217. /// 产品的工作频带上限(终止频率)
  218. /// 功能:用于判断从起始频率按一定的步进测试是否超出产品工作频段上限
  219. /// </summary>
  220. public double StopFrequency { set; get; }
  221. /// <summary>
  222. ///设置频谱仪的SPAN
  223. /// </summary>
  224. public string SPAN { set; get; }
  225. /// <summary>
  226. /// 设置参考电平
  227. /// </summary>
  228. public string REF { set; get; }
  229. /// <summary>
  230. /// 设置小信号参考电平
  231. /// </summary>
  232. public string SmallSignalREF { set; get; }
  233. /// <summary>
  234. /// 设置RBW
  235. /// </summary>
  236. public string RBW { set; get; }
  237. /// <summary>
  238. /// 设置VBW
  239. /// </summary>
  240. public string VBW { set; get; }
  241. /// <summary>
  242. /// 谐波次数
  243. /// </summary>
  244. public int[] HarmonicTime { set; get; }
  245. /// <summary>
  246. /// 控制延时
  247. /// </summary>
  248. public int ControlDelay { set; get; }
  249. /// <summary>
  250. /// 谐波抑制下限
  251. /// </summary>
  252. public double HarmonicSuppressionLower { set; get; }
  253. /// <summary>
  254. /// 谐波抑制上限
  255. /// </summary>
  256. public double HarmonicSuppressionUpper{ 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. }