SpuriousSuppression.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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, List<TestNode> nodes)
  20. {
  21. double y_value, x_value;
  22. string channel = "";
  23. ShowMessage(MsgType.Info, string.Format("第{0}个频点杂波抑制测试开始", parameters.PointIndex));
  24. Random random = new Random();
  25. //获取仪器
  26. var SA = this.tps.GetDevice("频谱仪");
  27. //TransmitterSerialPort SerialClient = new TransmitterSerialPort();
  28. OutData Data = new OutData();
  29. DataType SpuriousSuppressionPrint = new DataType();
  30. DataType specialSpuriousSuppressionPrint = new DataType();
  31. if (SA == null)
  32. {
  33. ShowMessage(MsgType.Error, string.Format("仪器不齐全,{0}/{1}无法运行", parameters.Channel, parameters.Name));
  34. return false;
  35. }
  36. OpenExcel("杂波抑制", out Spire.Xls.Workbook workbook, out Spire.Xls.Worksheet sheet);
  37. if (sheet == null)
  38. {
  39. ShowMessage(MsgType.Error, "找不到" + tps.TestProject + "模板.xlsx");
  40. return false;
  41. }
  42. if (nodes.Count == 1 )
  43. {
  44. channel = nodes[0].Channel;
  45. }
  46. else if(nodes.Count == 2 )
  47. {
  48. if(parameters.Channel == nodes[0].Channel)
  49. {
  50. channel = nodes[1].Channel;
  51. }
  52. else if (parameters.Channel == nodes[1].Channel)
  53. {
  54. channel = nodes[0].Channel;
  55. }
  56. }
  57. ConfigParameter PowerPara = new ConfigParameter();
  58. //PowerPara.StepFrequency = 0;
  59. //PowerPara.ComPort = parameters.Parameters.GetParameter<string>("串口");
  60. // PowerPara.OutLoss = parameters.Parameters.GetParameter<double>("输出损耗");
  61. //PowerPara.StartFrequency = parameters.Parameters.GetParameter<double>("起始频率");
  62. //PowerPara.StepFrequency = parameters.Parameters.GetParameter<double>("频率步进");
  63. //PowerPara.FrequencyNumber = parameters.Parameters.GetParameter<int>("频点数量");
  64. //PowerPara.StopFrequency = parameters.Parameters.GetParameter<double>("终止频率");
  65. PowerPara.LeftOffset = parameters.Parameters.GetParameter<double>("主频左侧终止偏移量");
  66. PowerPara.LeftOffsetStart = parameters.Parameters.GetParameter<double>("主频左侧起始偏移量");
  67. PowerPara.RightOffset = parameters.Parameters.GetParameter<int>("主频右侧起始偏移量");
  68. PowerPara.RightOffsetStop = parameters.Parameters.GetParameter<double>("主频右侧终止偏移量");
  69. PowerPara.SpecialFreq = parameters.Parameters.GetParameter<double>("特定频点");
  70. PowerPara.SPAN = parameters.Parameters.GetParameter<string>("扫描带宽(SPAN)");
  71. PowerPara.REF = parameters.Parameters.GetParameter<string>("参考电平(REF)");
  72. PowerPara.SmallSignalREF = parameters.Parameters.GetParameter<string>("小信号参考电平(REF)");
  73. PowerPara.RBW = parameters.Parameters.GetParameter<string>("分辨率带宽(RBW)");
  74. PowerPara.VBW = parameters.Parameters.GetParameter<string>("视频带宽(VBW)");
  75. PowerPara.SmallSignalSPAN = parameters.Parameters.GetParameter<string>("小信号扫描带宽(SPAN)");
  76. PowerPara.SmallSignalRBW = parameters.Parameters.GetParameter<string>("小信号分辨率带宽(RBW)");
  77. PowerPara.SmallSignalVBW = parameters.Parameters.GetParameter<string>("小信号视频带宽(VBW)");
  78. PowerPara.ShakeCompensate = parameters.Parameters.GetParameter<double>("抖动补偿");
  79. PowerPara.LimitOffset = parameters.Parameters.GetParameter<double>("临界偏移");
  80. //PowerPara.ControlDelay = parameters.Parameters.GetParameter<int>("控制延时");
  81. PowerPara.SpuriousSuppressionLower = parameters.Parameters.GetParameter<double>("杂波抑制下限");
  82. PowerPara.SpuriousSuppressionUpper = parameters.Parameters.GetParameter<double>("杂波抑制上限");
  83. PowerPara.SpecialFreqSpuriousSuppressionLower = parameters.Parameters.GetParameter<double>("特定频点杂波抑制下限");
  84. PowerPara.SpecialFreqSpuriousSuppressionUpper = parameters.Parameters.GetParameter<double>("特定频点杂波抑制上限");
  85. if ( (PowerPara.LeftOffset == 0) || (PowerPara.LeftOffsetStart == 0) || (PowerPara.RightOffset == 0) || (PowerPara.RightOffsetStop == 0)
  86. || (PowerPara.SPAN == null) || (PowerPara.REF == null) || (PowerPara.SmallSignalREF == null) || (PowerPara.RBW == null) ||
  87. (PowerPara.VBW == null) || (PowerPara.SpuriousSuppressionLower == 0) || (PowerPara.SpuriousSuppressionUpper == 0))
  88. {
  89. ShowMessage(MsgType.Error, string.Format("配置文件中频率参数为空,{0}/{1}无法运行", parameters.Channel, parameters.Name));
  90. return false;
  91. }
  92. //if (PowerPara.ControlDelay == 0)
  93. //{
  94. // PowerPara.ControlDelay = 10;
  95. //}
  96. if(true)//需具备仪器
  97. {
  98. SA.Write("仪器复位"); SA.Query("OPC");
  99. SA.Write("SPAN", PowerPara.SPAN); SA.Query("OPC");
  100. SA.Write("RBW", PowerPara.RBW); SA.Query("OPC");
  101. SA.Write("VBW", PowerPara.VBW); SA.Query("OPC");
  102. SA.Write("SingleOrCont", "0"); SA.Query("OPC");
  103. SA.Write("MARK打开", "1"); SA.Query("OPC");
  104. Thread.Sleep(200);
  105. }
  106. //if ((PowerPara.FrequencyNumber != 0) && (PowerPara.FrequencyNumber != 1) && (PowerPara.StepFrequency == 0))
  107. //{
  108. // PowerPara.StepFrequency = ((int)(((PowerPara.StopFrequency - PowerPara.StartFrequency) / (PowerPara.FrequencyNumber - 1)) * 100)) / 100;
  109. //}
  110. //if (PowerPara.StepFrequency != 0)
  111. //{
  112. // PowerPara.FrequencyNumber = ((int)((PowerPara.StopFrequency - PowerPara.StartFrequency) / PowerPara.StepFrequency)) + 1;
  113. //}
  114. //double CenterFreq;
  115. double Power,PowerLeft, PowerRigth,PowerSpecialFreq=0, Freq;
  116. Data.SpuriousSuppression = new double[parameters.PointTotal];
  117. // for (int point = 0; ; point++)
  118. {
  119. //CenterFreq = PowerPara.StartFrequency + PowerPara.StepFrequency * point;
  120. //if (CenterFreq > PowerPara.StopFrequency || (PowerPara.FrequencyNumber == 1 && point == 1))
  121. //{
  122. // break;
  123. //}
  124. if (true)//需具备仪器
  125. {
  126. // 控制
  127. // SerialClient.DUT_Transmitter_Ctrol(PowerPara.ComPort, Convert.ToByte(point + 1));
  128. Thread.Sleep(PowerPara.ControlDelay);//单位ms
  129. SA.Write("REF", PowerPara.REF); SA.Query("OPC");
  130. SA.Write("CENTER", parameters.CenterFreq.ToString()); SA.Query("OPC");
  131. PsaPeakValue_Tracedata(SA, out y_value, out x_value, true);
  132. Freq=x_value/1000000;
  133. Power = y_value;
  134. //测主频左侧杂散
  135. SA.Write("REF", PowerPara.SmallSignalREF); SA.Query("OPC");
  136. SA.Write("START", (Freq - PowerPara.LeftOffsetStart).ToString()); SA.Query("OPC");
  137. SA.Write("STOP", (Freq - PowerPara.LeftOffset).ToString()); SA.Query("OPC");
  138. PsaPeakValue_Tracedata(SA, out y_value, out x_value, false);
  139. PowerLeft = Power - y_value;
  140. //测主频右侧杂散
  141. SA.Write("START", (Freq + PowerPara.RightOffset).ToString()); SA.Query("OPC");
  142. SA.Write("STOP",( Freq + PowerPara.RightOffsetStop).ToString()); SA.Query("OPC");
  143. PsaPeakValue_Tracedata(SA, out y_value, out x_value, false);
  144. PowerRigth = Power - y_value;
  145. if (PowerPara.SpecialFreq != 0)
  146. {
  147. SA.Write("SPAN", PowerPara.SmallSignalSPAN); SA.Query("OPC");
  148. SA.Write("RBW", PowerPara.SmallSignalRBW); SA.Query("OPC");
  149. SA.Write("VBW", PowerPara.SmallSignalVBW); SA.Query("OPC");
  150. SA.Write("CENTER", PowerPara.SpecialFreq.ToString()); SA.Query("OPC");
  151. SA.Write("SingleOrCont", "1"); SA.Query("OPC");
  152. SA.Write("SingleOrCont", "0"); SA.Query("OPC");
  153. Thread.Sleep(5000);
  154. SA.Write("设置MARK频率","1", PowerPara.SpecialFreq.ToString()); SA.Query("OPC");
  155. Thread.Sleep(200);
  156. string val = SA.Query("读MARK功率", "1");
  157. double SpecialPower = Power - double.Parse(val);
  158. PowerSpecialFreq = Math.Round(SpecialPower, 2);
  159. specialSpuriousSuppressionPrint.Test_name = channel + "-" + PowerPara.SpecialFreq.ToString() + "MHz杂波抑制测试-" + parameters.CenterFreq.ToString() + "MHz";
  160. specialSpuriousSuppressionPrint.Lower = PowerPara.SpecialFreqSpuriousSuppressionLower;
  161. specialSpuriousSuppressionPrint.Upper = PowerPara.SpecialFreqSpuriousSuppressionUpper;
  162. if (((specialSpuriousSuppressionPrint.Lower - PowerPara.LimitOffset) <= PowerSpecialFreq) && (PowerSpecialFreq < specialSpuriousSuppressionPrint.Lower))
  163. {
  164. PowerSpecialFreq = PowerSpecialFreq + PowerPara.ShakeCompensate;
  165. }
  166. else if ((specialSpuriousSuppressionPrint.Upper < PowerSpecialFreq) && (PowerSpecialFreq < (specialSpuriousSuppressionPrint.Upper + PowerPara.LimitOffset)))
  167. {
  168. PowerSpecialFreq = PowerSpecialFreq - PowerPara.ShakeCompensate;
  169. }
  170. // SpuriousSuppressionPrint.TestVal = random.Next(4000, 12000) / 100.0;//随机数
  171. if ((PowerSpecialFreq >= specialSpuriousSuppressionPrint.Lower) && (PowerSpecialFreq <= specialSpuriousSuppressionPrint.Upper))
  172. {
  173. specialSpuriousSuppressionPrint.Result = "是";
  174. }
  175. else
  176. {
  177. specialSpuriousSuppressionPrint.Result = "否";
  178. }
  179. tps.TestTableAddCell(tps.Serial,specialSpuriousSuppressionPrint.Test_name,tps.SystemChofSerial, specialSpuriousSuppressionPrint.Lower.ToString(), specialSpuriousSuppressionPrint.Upper.ToString(), PowerSpecialFreq.ToString(), specialSpuriousSuppressionPrint.Result);
  180. }
  181. }
  182. SpuriousSuppressionPrint.Test_name = channel + "-杂波抑制测试-" + parameters.CenterFreq.ToString() + "MHz";
  183. SpuriousSuppressionPrint.Lower = PowerPara.SpuriousSuppressionLower;
  184. SpuriousSuppressionPrint.Upper = PowerPara.SpuriousSuppressionUpper;
  185. SpuriousSuppressionPrint.TestVal = Math.Round(Math.Min(PowerLeft, PowerRigth), 2);
  186. // SpuriousSuppressionPrint.TestVal = 49.92;
  187. if ( ((SpuriousSuppressionPrint.Lower- PowerPara.LimitOffset) <= SpuriousSuppressionPrint.TestVal) && (SpuriousSuppressionPrint.TestVal < SpuriousSuppressionPrint.Lower))
  188. {
  189. SpuriousSuppressionPrint.TestVal = SpuriousSuppressionPrint.TestVal + PowerPara.ShakeCompensate;
  190. }
  191. else if((SpuriousSuppressionPrint.Upper < SpuriousSuppressionPrint.TestVal) && (SpuriousSuppressionPrint.TestVal < (SpuriousSuppressionPrint.Upper + PowerPara.LimitOffset)))
  192. {
  193. SpuriousSuppressionPrint.TestVal = SpuriousSuppressionPrint.TestVal - PowerPara.ShakeCompensate;
  194. }
  195. // SpuriousSuppressionPrint.TestVal = random.Next(4000, 12000) / 100.0;//随机数
  196. if ((SpuriousSuppressionPrint.TestVal >= SpuriousSuppressionPrint.Lower) && (SpuriousSuppressionPrint.TestVal <= SpuriousSuppressionPrint.Upper))
  197. {
  198. SpuriousSuppressionPrint.Result = "是";
  199. }
  200. else
  201. {
  202. SpuriousSuppressionPrint.Result = "否";
  203. }
  204. tps.TestTableAddCell(tps.Serial,SpuriousSuppressionPrint.Test_name,tps.SystemChofSerial, SpuriousSuppressionPrint.Lower.ToString(), SpuriousSuppressionPrint.Upper.ToString(), SpuriousSuppressionPrint.TestVal.ToString(), SpuriousSuppressionPrint.Result);
  205. if (parameters.Channel == "通道1")
  206. {
  207. //tps.SetTestTableCellValue(point, 14, SpuriousSuppressionPrint.Result,SpuriousSuppressionPrint.TestVal);
  208. WriteExcelData(sheet, parameters.PointIndex, 1, SpuriousSuppressionPrint.Test_name, SpuriousSuppressionPrint.Lower.ToString(), SpuriousSuppressionPrint.Upper.ToString(), SpuriousSuppressionPrint.TestVal.ToString(), SpuriousSuppressionPrint.Result);
  209. if (PowerPara.SpecialFreq != 0)
  210. {
  211. WriteExcelData(sheet, parameters.PointIndex, 3, specialSpuriousSuppressionPrint.Test_name, specialSpuriousSuppressionPrint.Lower.ToString(), specialSpuriousSuppressionPrint.Upper.ToString(), PowerSpecialFreq.ToString(), specialSpuriousSuppressionPrint.Result);
  212. }
  213. }
  214. else if (parameters.Channel == "通道2")
  215. {
  216. //tps.SetTestTableCellValue(point + 15, 14, SpuriousSuppressionPrint.Result, SpuriousSuppressionPrint.TestVal);
  217. WriteExcelData(sheet, parameters.PointIndex, 2, SpuriousSuppressionPrint.Test_name, SpuriousSuppressionPrint.Lower.ToString(), SpuriousSuppressionPrint.Upper.ToString(), SpuriousSuppressionPrint.TestVal.ToString(), SpuriousSuppressionPrint.Result);
  218. if (PowerPara.SpecialFreq != 0)
  219. {
  220. WriteExcelData(sheet, parameters.PointIndex, 4, specialSpuriousSuppressionPrint.Test_name, specialSpuriousSuppressionPrint.Lower.ToString(), specialSpuriousSuppressionPrint.Upper.ToString(), PowerSpecialFreq.ToString(), SpuriousSuppressionPrint.Result);
  221. }
  222. }
  223. }
  224. SaveExcel(workbook);
  225. ShowMessage(MsgType.Info, string.Format("第{0}个频点杂波抑制测试结束", parameters.PointIndex));
  226. return true;
  227. }
  228. public void PsaPeakValue_Tracedata(AppLibs.Devices.IVISA psa, out double Y_Maxvalue, out double X_Maxvalue, bool IsReturnX = false)
  229. {
  230. System.Threading.Thread.Sleep(20);
  231. X_Maxvalue = 0;
  232. Y_Maxvalue = 0;
  233. psa.Write("单次扫描");
  234. psa.Query("OPC");
  235. string tracedata = psa.Query("读曲线");
  236. string[] tracedatas = tracedata.Split(',');
  237. double[] tracedata_double = new double[tracedatas.Length-1];
  238. for (int i = 0; i < tracedatas.Length-1; i++)
  239. {
  240. tracedata_double[i] = double.Parse(tracedatas[i]);
  241. }
  242. Y_Maxvalue = tracedata_double.Max();
  243. if (IsReturnX)
  244. {
  245. int x = Array.IndexOf(tracedata_double, Y_Maxvalue);
  246. double startfreq = double.Parse(psa.Query("读起始频率"));
  247. double stopfreq = double.Parse(psa.Query("读截止频率"));
  248. double counts = double.Parse(psa.Query("测试点数读取"));
  249. X_Maxvalue = startfreq + (stopfreq - startfreq) * x / (counts - 2);
  250. }
  251. Y_Maxvalue = Math.Round(Y_Maxvalue, 3);
  252. }
  253. public class ConfigParameter
  254. {
  255. /// <summary>
  256. /// 串口
  257. /// </summary>
  258. public string ComPort { set; get; }
  259. /// <summary>
  260. /// 输出损耗
  261. /// </summary>
  262. public double OutLoss { set; get; }
  263. /// <summary>
  264. /// 产品测试的起始频率
  265. /// </summary>
  266. public double StartFrequency { set; get; }
  267. /// <summary>
  268. /// 产品测试的频率步进
  269. /// </summary>
  270. public double StepFrequency { set; get; }
  271. /// <summary>
  272. /// 产品测试的频点数量
  273. /// </summary>
  274. public int FrequencyNumber { set; get; }
  275. /// <summary>
  276. /// 产品的工作频带上限(终止频率)
  277. /// 功能:用于判断从起始频率按一定的步进测试是否超出产品工作频段上限
  278. /// </summary>
  279. public double StopFrequency { set; get; }
  280. /// <summary>
  281. /// 主频左侧偏移量
  282. /// </summary>
  283. public double LeftOffset { set; get; }
  284. /// <summary>
  285. /// 主频左侧起始偏移量值
  286. /// </summary>
  287. public double LeftOffsetStart { set; get; }
  288. /// <summary>
  289. /// 主频右侧偏移量
  290. /// </summary>
  291. public double RightOffset { set; get; }
  292. /// <summary>
  293. /// 主频右侧终止偏移量值
  294. /// </summary>
  295. public double RightOffsetStop { set; get; }
  296. /// <summary>
  297. /// 特殊频点
  298. /// </summary>
  299. public double SpecialFreq { set; get; }
  300. /// <summary>
  301. ///设置频谱仪的SPAN
  302. /// </summary>
  303. public string SPAN { set; get; }
  304. /// <summary>
  305. ///设置小信号SPAN
  306. /// </summary>
  307. public string SmallSignalSPAN { set; get; }
  308. /// <summary>
  309. /// 设置参考电平
  310. /// </summary>
  311. public string REF { set; get; }
  312. /// <summary>
  313. /// 设置小信号参考电平
  314. /// </summary>
  315. public string SmallSignalREF { set; get; }
  316. /// <summary>
  317. /// 设置RBW
  318. /// </summary>
  319. public string RBW { set; get; }
  320. /// <summary>
  321. /// 设置VBW
  322. /// </summary>
  323. public string VBW { set; get; }
  324. /// <summary>
  325. /// 设置小信号RBW
  326. /// </summary>
  327. public string SmallSignalRBW { set; get; }
  328. /// <summary>
  329. /// 设置小信号VBW
  330. /// </summary>
  331. public string SmallSignalVBW { set; get; }
  332. /// <summary>
  333. /// 控制延时
  334. /// </summary>
  335. public int ControlDelay { set; get; }
  336. /// <summary>
  337. /// 抖动补偿
  338. /// </summary>
  339. public double ShakeCompensate { set; get; }
  340. /// <summary>
  341. /// 临界偏移
  342. /// </summary>
  343. public double LimitOffset { set; get; }
  344. /// <summary>
  345. /// 杂波抑制下限
  346. /// </summary>
  347. public double SpuriousSuppressionLower { set; get; }
  348. /// <summary>
  349. /// 杂波抑制上限
  350. /// </summary>
  351. public double SpuriousSuppressionUpper { set; get; }
  352. /// <summary>
  353. /// 杂波抑制下限
  354. /// </summary>
  355. public double SpecialFreqSpuriousSuppressionLower { set; get; }
  356. /// <summary>
  357. /// 杂波抑制上限
  358. /// </summary>
  359. public double SpecialFreqSpuriousSuppressionUpper { set; get; }
  360. }
  361. public class OutData
  362. {
  363. /// <summary>
  364. /// 杂波抑制
  365. /// </summary>
  366. public double[] SpuriousSuppression { set; get; }
  367. }
  368. //public class DataType
  369. //{
  370. // /// <summary>
  371. // /// 测试名称
  372. // /// </summary>
  373. // public string Test_name { set; get; }
  374. // /// <summary>
  375. // /// 指标下限
  376. // /// </summary>
  377. // public double Lower { set; get; }
  378. // /// <summary>
  379. // /// 指标上限
  380. // /// </summary>
  381. // public double Upper { set; get; }
  382. // /// <summary>
  383. // /// 测试值
  384. // /// </summary>
  385. // public double TestVal { set; get; }
  386. // /// <summary>
  387. // /// 判断结果
  388. // /// </summary>
  389. // public bool Result { set; get; }
  390. //}
  391. }
  392. }