SpuriousSuppression.cs 20 KB

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