HarmonicSuppression.cs 14 KB

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