HarmonicSuppression.cs 16 KB

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