HarmonicSuppression.cs 11 KB

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