SpuriousSuppression.cs 15 KB

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