SpuriousSuppression.cs 14 KB

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