SpuriousSuppression.cs 21 KB

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