HarmonicSuppression.cs 15 KB

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