HarmonicSuppression.cs 17 KB

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