1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.ComponentModel;
- using Tps_LQ_Transmitter.com;
- using System.IO;
- using Tps_LQ_Transmitter.models;
- using System.Data;
- using AppLibs.Devices;
- using System.Windows.Forms;
- using CommonDevHostApp.Devices;
- using Tps_LQ_Transmitter.Common;
- using System.Diagnostics;
- using System.Threading;
- namespace Tps_LQ_Transmitter
- {
- public delegate void DltShowMsg(MsgType msgType, string msg);
- public delegate void DltCellValueChanged(int row, int cell, bool ok, object value);
- public delegate void DltCellValueAdd(string series, string name,string systemch, string lower, string upper, string testval, string result);
- public delegate void DltRemainTime(string channel, int point, int FrequencyNumber, double CenterFreq, double testMin);
- public delegate void DltCurrentSeries(string series);
- /// <summary>
- /// 一个测试文件对应的测试项目
- /// </summary>
- public class MainTps
- {
- /// <summary>
- /// 测试人员
- /// </summary>
- public string Tester { get; set; }
- //测试地点
- public string Place { get; set; }
- //环境温度
- public string Temperature { get; set; }
- //当前测试的产品序列号
- public string Serial { get; set; }
- //当前测试的产品序列号所用的系统通道号(总共8路)
- public string SystemChofSerial { get; set; }
- //环境湿度
- public string Humidity { get; set; }
- //待测产品
- public string Product { get; set; }
- //测试项目
- public string TestProject { get; set; }
- //配置文件路径
- string FilePath;
- /// <summary>
- /// 设备节点,表示当前测试程序包含的试验设备,绑定到界面的设备列表中
- /// </summary>
- public IList<DeviceNode> Devices;
- /// <summary>
- /// 测试节点,表示当前测试程序含有的测试指标,绑定到界面的指标列表中
- /// </summary>
- public IList<TestNode> TestNodes;
- /// <summary>
- /// 手工输入表
- /// </summary>
- public DataTable ManualTable;
- /// <summary>
- /// 自动测试表
- /// </summary>
- public DataTable TestTable;
- /// <summary>
- /// 测试值字典
- /// </summary>
- public Dictionary<string,double> TestValueDict;
- public Dictionary<string,double> GloabConfigDict;
- public string DCPowerAddress;//程控电源地址
- public string N9030A_SA_Address;//N9030A频谱仪地址
- public int TestFreqSum;//频点数
- public TransmitterSerialPort SerialClient = new TransmitterSerialPort();
- public byte ThridByte;//频点控制第3个字节
- public byte FourthByte;//频点控制第4个字节
- public int ControlDelay;//控制延时
- public event DltCellValueChanged ManualTableCellChanged;
- public event DltCellValueChanged TestTableCellChanged;
- public event DltCellValueAdd AddTestTableCell;
- public event DltRemainTime RemainTimeUpdate;
- public event DltCurrentSeries CurrentSeries;
- DataTable InitTestTable,InitManualTable; //原始数据的界面,用于初始化或者重置
- /// <summary>
- /// 测试指标的结构
- /// </summary>
- List<BaseModelStruct> baseModelStructs;
- public MainTps()
- {
- Devices = new BindingList<DeviceNode>();
- TestNodes = new BindingList<TestNode>();
- GloabConfigDict = new Dictionary<string, double>();
- DCPowerAddress = "";
- baseModelStructs = new List<BaseModelStruct>();
- LoadBaseModelStructs();
- }
- #region 模板基础方法
- /// <summary>
- /// 加载配置文件
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public bool LoadConfigFile(string filePath)
- {
- bool ok = true;
- if (File.Exists(filePath) == false)
- {
- ShowMessage(MsgType.Error, string.Format("找不到配置文件:{0}", filePath));
- return false;
- }
- this.FilePath = filePath;
- using (Spire.Xls.Workbook workbook = new Spire.Xls.Workbook())
- {
- workbook.LoadFromFile(filePath);
- //处理Excel数据,更多请参考官方Demo
- Spire.Xls.Worksheet sheet = workbook.Worksheets["自动测试项总表"];
- if(sheet == null)
- {
- ShowMessage(MsgType.Error, "文件中找不到‘自动测试项总表’");
- return false;
- }
- int blankRows = 0; //避免出错
- int num = 0;
- //默认最多两百行
- while (num++ < 200)
- {
- //索引从1开始
- string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString():"";
- col1 = col1.Trim();
- if (string.IsNullOrEmpty(col1))
- {
- blankRows++;
- if(blankRows > 5)
- {
- //连续5个空行以上则退出
- break;
- }
- }
- else
- {
- blankRows = 0;
- }
- if (col1.Equals("设备配置表"))
- {
- ok &= readDevices(sheet,num);
- //解析行数跳过,实际上还得再加上
- num = num + this.Devices.Count+2;
- }
- else if (col1.Equals("测试指标配置表"))
- {
- ok &= readTestNodes(workbook,sheet,num);
- var items = (from p in TestNodes select p.Name).Distinct().ToArray();
- //解析行数跳过,实际上还得再加上
- num = num + items.Count() + 3;
- }
- }
- //加载人工测试项总表
- sheet = workbook.Worksheets["人工测试项总表"];
- if (sheet != null)
- {
- blankRows = 0; //避免出错
- num = 0;
- //默认最多两百行
- while (num++ < 200)
- {
- //索引从1开始
- string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
- col1 = col1.Trim();
- if (string.IsNullOrEmpty(col1))
- {
- blankRows++;
- if (blankRows > 5)
- {
- //连续5个空行以上则退出
- break;
- }
- }
- else
- {
- blankRows = 0;
- }
- if (col1.Equals("人工测试项总表"))
- {
- this.ManualTable = readTable(sheet, num);
- if (this.ManualTable != null)
- {
- this.InitManualTable = this.ManualTable.Copy();
- }
- }
- }
- }
- //加载人工测试项总表
- sheet = workbook.Worksheets["自动测试记录表"];
- if (sheet != null)
- {
- blankRows = 0; //避免出错
- num = 0;
- //默认最多两百行
- while (num++ < 200)
- {
- //索引从1开始
- string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
- col1 = col1.Trim();
- if (string.IsNullOrEmpty(col1))
- {
- blankRows++;
- if (blankRows > 5)
- {
- //连续5个空行以上则退出
- break;
- }
- }
- else
- {
- blankRows = 0;
- }
- if (col1.Equals("自动测试记录表"))
- {
- this.TestTable = readTable(sheet, num);
- if(this.TestTable != null)
- {
- this.InitTestTable = this.TestTable.Copy();
- }
- }
- }
- }
- //加载人工测试项总表
- sheet = workbook.Worksheets["公共设置"];
- if (sheet != null)
- {
- blankRows = 0; //避免出错
- num = 0;
- //默认最多两百行
- while (num++ < 200)
- {
- //索引从1开始
- string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
- col1 = col1.Trim();
- if (string.IsNullOrEmpty(col1))
- {
- blankRows++;
- if (blankRows > 5)
- {
- //连续5个空行以上则退出
- break;
- }
- }
- else
- {
- blankRows = 0;
- }
-
- if (col1.Equals("参数表"))
- {
- //电压
- string WorkVolt = sheet.Range[3, 3].Value2 != null ? sheet.Range[3, 3].Value2.ToString() : "";
- GloabConfigDict.Add("电压", double.Parse(WorkVolt));
- //电流
- string WorkCurrent = sheet.Range[4, 3].Value2 != null ? sheet.Range[4, 3].Value2.ToString() : "";
- GloabConfigDict.Add("电流", double.Parse(WorkCurrent));
- DCPowerAddress = sheet.Range[5, 3].Value2 != null ? sheet.Range[5, 3].Value2.ToString() : "";
- TestFreqSum = int.Parse(sheet.Range[7, 3].Value2 != null ? sheet.Range[7, 3].Value2.ToString() : "");
- }
- }
- }
- }
- return ok;
- }
- /// <summary>
- /// 读设备配置表
- /// </summary>
- /// <param name="rowIndex">设备配置表的起始位置</param>
- bool readDevices(Spire.Xls.Worksheet sheet, int rowIndex)
- {
- //检查表的格式正确
- string col1 = sheet.Range[rowIndex + 1, 1].Value2 != null ? sheet.Range[rowIndex + 1, 1].Value2.ToString() : "";
- string col2 = sheet.Range[rowIndex + 1, 2].Value2 != null ? sheet.Range[rowIndex + 1, 2].Value2.ToString() : "";
- string col3 = sheet.Range[rowIndex + 1, 3].Value2 != null ? sheet.Range[rowIndex + 1, 3].Value2.ToString() : "";
- string col4 = sheet.Range[rowIndex + 1, 4].Value2 != null ? sheet.Range[rowIndex + 1, 4].Value2.ToString() : "";
- if (!(col1 == "设备名称" && col2 == "设备类型" && col3 == "设备型号" && col4 == "控制句柄"))
- {
- ShowMessage(MsgType.Error, "'设备配置表'行首标题位置和格式不正确");
- return false;
- }
- int num = rowIndex + 1;
- //默认最多30行
- while (num++ < rowIndex + 30)
- {
- col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
- col2 = sheet.Range[num, 2].Value2 != null ? sheet.Range[num, 2].Value2.ToString() : "";
- col3 = sheet.Range[num, 3].Value2 != null ? sheet.Range[num, 3].Value2.ToString() : "";
- col4 = sheet.Range[num, 4].Value2 != null ? sheet.Range[num, 4].Value2.ToString() : "";
- if ( string.IsNullOrEmpty(col2) || string.IsNullOrEmpty(col3))
- {
- break;
- }
- DeviceNode dn = new DeviceNode();
- dn.DeviceName = col1;
- dn.DeviceType = col2;
- dn.DeviceModel = col3;
- dn.DeviceHandle = col4;
- this.Devices.Add(dn);
- }
- return true;
- }
- /// <summary>
- /// 读测试指标配置表
- /// </summary>
- /// <param name="rowIndex">测试指标配置表的起始位置</param>
- bool readTestNodes(Spire.Xls.Workbook workbook,Spire.Xls.Worksheet sheet, int rowIndex)
- {
- List<TestNode> models = new List<TestNode>();
- //检查表的格式正确
- string col1 = sheet.Range[rowIndex + 2, 1].Value2 != null ? sheet.Range[rowIndex + 2, 1].Value2.ToString() : "";
- string col2 = sheet.Range[rowIndex + 2, 2].Value2 != null ? sheet.Range[rowIndex + 2, 2].Value2.ToString() : "";
- string col3 = sheet.Range[rowIndex + 2, 3].Value2 != null ? sheet.Range[rowIndex + 2, 3].Value2.ToString() : "";
- string col4 = sheet.Range[rowIndex + 2, 4].Value2 != null ? sheet.Range[rowIndex + 2, 4].Value2.ToString() : "";
- if (!(col1 == "测试指标" && col2 == "默认勾选" && col3 == "测试模板" && col4 == "测试顺序"))
- {
- ShowMessage(MsgType.Error, "'测试指标配置表'行首标题位置和格式不正确");
- return false;
- }
- int num = rowIndex + 2;
- //默认最多100行,先解析测试指标个数
- while (num++ < rowIndex + 100)
- {
- col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
- col2 = sheet.Range[num, 2].Value2 != null ? sheet.Range[num, 2].Value2.ToString() : "";
- col3 = sheet.Range[num, 3].Value2 != null ? sheet.Range[num, 3].Value2.ToString() : "";
- col4 = sheet.Range[num, 4].Value2 != null ? sheet.Range[num, 4].Value2.ToString() : "";
- if (string.IsNullOrEmpty(col1) || string.IsNullOrEmpty(col3))
- {
- break;
- }
- TestNode tn = new TestNode();
- tn.Name = col1;
- if(col2 == "是")
- {
- tn.IsSelected = true;
- }
- else
- {
- tn.IsSelected = false;
- }
- tn.Template = col3;
- int order = Int32.MaxValue;
- if (Int32.TryParse(col4, out order))
- tn.Order = order;
- models.Add(tn);
- }
- //再解析各通道的配置数据
- int colIndex = 5; //通道数据从第5列开始
- while(true)
- {
- col1 = sheet.Range[rowIndex + 3, colIndex].Value2 != null ? sheet.Range[rowIndex + 3, colIndex].Value2.ToString() : "";
- col2 = sheet.Range[rowIndex + 3, colIndex + 1].Value2 != null ? sheet.Range[rowIndex + 3, colIndex + 1].Value2.ToString() : "";
- //有新通道数据则继续,否则退出
- if(string.IsNullOrEmpty(col1) || string.IsNullOrEmpty(col2) || col2 != "启用")
- {
- break;
- }
- string channel = sheet.Range[rowIndex + 1, colIndex].Value2 != null ? sheet.Range[rowIndex + 1, colIndex].Value2.ToString() : "";
- //按指标个数来解析通道数据
- for (int i = 0; i < models.Count; i++)
- {
- col1 = sheet.Range[rowIndex + 3 + i, colIndex].Value2 != null ? sheet.Range[rowIndex + 3 + i, colIndex].Value2.ToString() : "";
- col2 = sheet.Range[rowIndex + 3 + i, colIndex + 1].Value2 != null ? sheet.Range[rowIndex + 3 + i, colIndex + 1].Value2.ToString() : "";
- if (col2 != "启用")
- continue;
- TestNode newNode = models[i].Copy();
- newNode.Channel = channel; //通道名
- newNode.ParaConfigTable = col1; //测试配置表
- //复制已有参数表,或者从文件解析参数表
- TestNode preNode = TestNodes.FirstOrDefault(x => x.ParaConfigTable == col1);
- if(preNode != null)
- {
- //存在则复制,避免读文件费时间
- newNode.Parameters = preNode.Parameters.Copy();
- }
- else
- {
- //否则找到文件中对应的配置表
- Spire.Xls.Worksheet paraSheet = workbook.Worksheets[col1];
- if (paraSheet == null)
- {
- ShowMessage(MsgType.Error, "文件中找不到参数配置表:"+ col1);
- continue;
- }
- //解析参数配置表
- newNode.Parameters = readTestParameter(paraSheet);
- }
- //如果已经创建了同名的处理对象,则直接使用,否则则新建,采用单例模式
- //如果注释了下面的if,就是多例模式,即同名处理模块是不同的软件对象
- TestNode tn = this.TestNodes.FirstOrDefault(x => x.Template == newNode.Template);
- if(tn != null && tn.TestModel != null)
- {
- newNode.TestModel = tn.TestModel;
- }
- else
- {
- //根据测试指标的名称,创建BaseModel指标处理对象
- newNode.TestModel = CreateModel(newNode.Template);
- }
-
- this.TestNodes.Add(newNode);
- }
- colIndex += 2; //跳过2列
- }
- return true;
- }
- BaseModel CreateModel(string template)
- {
- //如果没有这个名字的软件模块,则返回null
- BaseModelStruct bms = baseModelStructs.FirstOrDefault(x => x.ModelName == template);
- if (bms == null)
- {
- ShowMessage(MsgType.Error, "找不到名称为:" + template +" 的测试指标模块。");
- return null;
- }
-
- BaseModel model = (BaseModel)Activator.CreateInstance(bms.type);
- model.tps = this;
- model.MessageEvent += ShowMessage;
- return model;
- }
- void LoadBaseModelStructs()
- {
- try
- {
- Type[] types = this.GetType().Assembly.GetTypes();
- foreach (var item in types)
- {
- //排除接口本身
- if (item.Name != "BaseModel")
- {
- if (typeof(BaseModel).IsAssignableFrom(item))
- {
- BaseModel model = (BaseModel)Activator.CreateInstance(item);
- BaseModelStruct bms = new BaseModelStruct();
- bms.ModelName = model.TemplateName;
- bms.type = item;
- this.baseModelStructs.Add(bms);
- model = null;
- }
- }
- }
- }
- catch(Exception e)
- {
- ShowMessage(MsgType.Error, "实例化BaseModel测试指标模块时出现错误,某个测试指标模块创建时发生错误,请检查代码!详细错误见程序日志文件!");
- Bundle.log.WriteLog(AppLibs.Host.LogLevel.Error, "类:MainTps,方法:LoadBaseModelStructs", e.Message + e.StackTrace);
- }
-
- }
- /// <summary>
- /// 读参数配置表
- /// </summary>
- /// <returns></returns>
- TestParameters readTestParameter(Spire.Xls.Worksheet sheet)
- {
- TestParameters pars = new TestParameters();
- int blankRows = 0; //避免出错
- int num = 0;
- //默认最多两百行
- while (num++ < 200)
- {
- //索引从1开始
- string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
- col1 = col1.Trim();
- if (string.IsNullOrEmpty(col1))
- {
- blankRows++;
- if (blankRows > 5)
- {
- //连续5个空行以上则退出
- break;
- }
- }
- else
- {
- blankRows = 0;
- }
- if (col1.Equals("参数表"))
- {
- ///获取表中的参数
- List<ParameterNode> nodes = readParameters(sheet,num);
- foreach (var item in nodes)
- {
- if(pars.Parameters.ContainsKey(item.Name))
- {
- ShowMessage(MsgType.Error, string.Format("参数配置表:{0} 存在同名参数:{1},无法重复添加参数", sheet.Name, item.Name));
- continue;
- }
- pars.Parameters.Add(item.Name, item);
- }
- //解析行数跳过,实际上还得再加上
- num = num + nodes.Count +1;
- }
- else if (col1.Equals("测试显示表"))
- {
- //获取表中的原始显示界面
- pars.InitialTable = readTable( sheet, num);
- //解析行数跳过,实际上还得再加上
- num = num + pars.InitialTable.Rows.Count + 1;
- }
- }
- return pars;
- }
- /// <summary>
- /// 读参数表
- /// </summary>
- /// <param name="sheet"></param>
- /// <returns></returns>
- List<ParameterNode> readParameters(Spire.Xls.Worksheet sheet,int rowIndex)
- {
- List<ParameterNode> nodes = new List<ParameterNode>();
- string col1 = sheet.Range[rowIndex + 1, 1].Value2 != null ? sheet.Range[rowIndex + 1, 1].Value2.ToString() : "";
- string col2 = sheet.Range[rowIndex + 1, 2].Value2 != null ? sheet.Range[rowIndex + 1, 2].Value2.ToString() : "";
- string col3 = sheet.Range[rowIndex + 1, 3].Value2 != null ? sheet.Range[rowIndex + 1, 3].Value2.ToString() : "";
- string col4 = sheet.Range[rowIndex + 1, 4].Value2 != null ? sheet.Range[rowIndex + 1, 4].Value2.ToString() : "";
- if (!(col1 == "名称" && col2 == "类型" && col3 == "值"))
- {
- ShowMessage(MsgType.Error, "'参数表'行首标题位置和格式不正确");
- return nodes;
- }
- int num = rowIndex + 1;
- //默认最多30行
- while (num++ < rowIndex + 200)
- {
- col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
- col2 = sheet.Range[num, 2].Value2 != null ? sheet.Range[num, 2].Value2.ToString() : "";
- col3 = sheet.Range[num, 3].Value2 != null ? sheet.Range[num, 3].Value2.ToString() : "";
- col4 = sheet.Range[num, 4].Value2 != null ? sheet.Range[num, 4].Value2.ToString() : "";
- if (string.IsNullOrEmpty(col1) || string.IsNullOrEmpty(col2) || string.IsNullOrEmpty(col3))
- {
- break;
- }
- ParameterNode pn = new ParameterNode();
- pn.Name = col1;
- pn.Type = col2;
- pn.StrValue = col3;
- pn.Mark = col4;
- nodes.Add(pn);
- }
- return nodes;
- }
- /// <summary>
- /// 读表格,人工测试表和测试显示表
- /// </summary>
- /// <param name="sheet"></param>
- /// <returns></returns>
- DataTable readTable(Spire.Xls.Worksheet sheet, int rowIndex)
- {
- //先创建列,再创建行
- DataTable dt = new DataTable();
- int num = 0;
- while (num++ < 50)
- {
- string col = sheet.Range[rowIndex + 1, num].Value2 != null ? sheet.Range[rowIndex + 1, num].Value2.ToString() : "";
- if (string.IsNullOrEmpty(col))
- break;
- dt.Columns.Add(col);
- }
- //再创建行
- num = rowIndex + 1;
- //默认最多200行
- while (num++ < rowIndex + 200)
- {
- //首列无值就认为表格结束
- string val = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
- if (string.IsNullOrEmpty(val.Trim()))
- break;
- DataRow dr = dt.NewRow();
- dt.Rows.Add(dr);
- for (int i = 0; i < dt.Columns.Count; i++)
- {
- val = sheet.Range[num, i+1].Value2 != null ? sheet.Range[num, i+1].Value2.ToString() : "";
- dr[i] = val;
- }
-
- }
- return dt;
- }
- /// <summary>
- /// 检查仪器是否正常配置
- /// </summary>
- /// <returns></returns>
- public bool CheckDevices()
- {
- bool ok = true;
- AppLibs.Devices.IDeviceHost devHost = Bundle.host.GetFirstOrDefaultService<AppLibs.Devices.IDeviceHost>();
- foreach (DeviceNode device in this.Devices)
- {
- DeviceParameter dev = devHost.GetFirstOrDefaultDeviceID(device.DeviceHandle);
- if (dev == null || !(device.DeviceType == dev.DeviceType && device.DeviceModel == dev.DeviceModel))
- {
- device.State = false;
- ok = false;
- }
- else
- {
- device.State = true;
- continue;
- }
- }
- return ok;
- }
- public void StopClose(string[] ProductSeries,CommonVisaResource DCPower, RainwormPower rainwormPower, TransmitterSerialPort SerialClient)
- {
- if (Product.Contains("YZH16A"))//针对YZH16A的产品
- {
- SerialClient.DUT_Transmitter_Ctrol(0x00, 0x00, 0x55);//todo:掉电控制
- }
- else
- {
- SerialClient.DUT_Transmitter_Ctrol1(0x00, 0x00);//todo:掉电控制
- }
- SerialClient.SerialClose();
- if (ProductSeries.Length > 1)//如果测试台数大于1,则使用外部TDK电源
- {
- DCPower.Write("GLOBal:OUTPut:STATe 0\n"); DCPower.Query("*OPC?\n");//需要取消注释
- ShowMessage(MsgType.Info, string.Format("关闭6YYC信号源,关闭串口,关闭外部TDK电源."));
- }
- else
- {
- rainwormPower.powerOnoff(RainwormPower.State.OFF);//设置蚯蚓电源输出
- rainwormPower.portClose();
- ShowMessage(MsgType.Info, string.Format("关闭6YYC信号源,关闭串口,关闭内部蚯蚓电源."));
- }
-
- }
- /// <summary>
- /// 开始测试
- /// </summary>
- /// <param name="isOrderByChannel">按照指标分组还是按照通道分组</param>
- public void Start(bool isOrderByChannel,string[] ProductSeries, CommonVisaResource DCPower, RainwormPower rainwormPower, TransmitterSerialPort SerialClient, MatchComPara CfigComParas)
- {
-
- isStop = false;
- //清空测试数据
- // BaseModel.TestResults.Clear();
- FrmMain FrmPara = new FrmMain();
- byte FourthByte = 0x00;
- string ComPort = CfigComParas.GetComPort("1");
- byte ThridByte = Convert.ToByte(CfigComParas.GetThirdByte("1"), 16);
- int FrequencyNumber = 0;
- if (TestFreqSum >= CfigComParas.ComParameters.Count)//如果限定的测试频点数大于设置的频点数
- {
- FrequencyNumber = CfigComParas.ComParameters.Count;//取设置的频点数
- }
- else
- {
- FrequencyNumber = TestFreqSum;//取限定的测试频点数
- }
- int ControlDelay = this.TestNodes[0].Parameters.GetParameter<int>("控制延时");
- //SerialClient.SerialOpen(CfigComParas.GetComPort("1"));
- //ShowMessage(MsgType.Info, string.Format("打开串口."));
- //选择勾选项
- List<TestNode> nodes = this.TestNodes.Where(x => x.IsSelected == true).ToList();
- if (isOrderByChannel)
- {
- //按照通道分组
- Stopwatch TimesCounter = new Stopwatch();
- double testMin = 0;
- TimesCounter.Restart();
- int Channel = 1;
- foreach (IGrouping<string, TestNode> group in nodes.GroupBy(x => x.Channel))
- {
- if (nodes.Count == 2)
- {
- if (group.Key == nodes[0].Channel)
- {
- MessageBox.Show("将产品通道1输出电缆接【衰减】接口,产品通道2输出电缆接【陷波】接口。", "温馨提示");
- }
- else if (group.Key == nodes[1].Channel)
- {
- MessageBox.Show("将产品通道1输出电缆接【陷波】接口,产品通道2输出电缆接【衰减】接口。", "温馨提示");
- }
- }
- else if(nodes.Count == 1)
- {
- MessageBox.Show("将产品输出电缆接【衰减】接口。", "温馨提示");
- }
- //channel++;
- //排序后执行
- for (int point = 0; point < FrequencyNumber; point++)//频点总数
- {
- FourthByte = Convert.ToByte(CfigComParas.GetFourthByte((point + 1).ToString()), 16);
- double CenterFreq = double.Parse(CfigComParas.Getfreqpoint((point + 1).ToString()));
- for (int i = 0; i < this.TestNodes.Count; i++)
- {
- this.TestNodes[i].PointIndex = point;
- this.TestNodes[i].CenterFreq = CenterFreq;
- this.TestNodes[i].PointTotal = FrequencyNumber;
- }
- if (FrmPara.IsRuning == false)//中途点击停止测试按钮
- {
- //控制断电
- StopClose(ProductSeries, DCPower, rainwormPower, SerialClient);
- return;
- }
- //控制
- if (Product.Contains("YZH16A"))//针对YZH16A的产品
- {
- SerialClient.DUT_Transmitter_Ctrol(ThridByte, FourthByte, 0x55);//todo:切换频点
- }
- else
- {
- SerialClient.DUT_Transmitter_Ctrol1(ThridByte, FourthByte);//todo:切换频点
- }
- ShowMessage(MsgType.Info, string.Format("发送串口指令EB 90 {0} {1}.", ThridByte, FourthByte));
- testMin = TimesCounter.ElapsedMilliseconds / 1000f / 60f;
- Thread.Sleep(ControlDelay);//单位ms
- ShowMessage(MsgType.Info, string.Format("延时{0}ms.", ControlDelay));
- RemainTimeUpdate(group.Key, point, FrequencyNumber, CenterFreq, testMin);
- //Tps_LQ_Transmitter.FrmMain.BeginInvoke(new Action(() =>
- //{
- // //labRemainTime.Text = $"第{point + 1}个频点[{CenterFreq}]MHz测试,共{currFileNode.Tps.TestNodes.Count - 1}个频点,耗时{Math.Round(testMin, 2)}Min。";
- //}));
- //Tps_LQ_Transmitter.FrmMain.BeginInvoke(new FrmMain RemainTime, point, CenterFreq, testMin);
- // //labRemainTime.Text = $"第{point + 1}个频点[{CenterFreq}]MHz测试,共{currFileNode.Tps.TestNodes.Count - 1}个频点,耗时{Math.Round(testMin, 2)}Min。";
- foreach (TestNode testnode in group.OrderBy(a => a.Order))
- {
- if (FrmPara.IsRuning == false)//中途点击停止测试按钮
- {
- //控制断电
- StopClose(ProductSeries, DCPower, rainwormPower, SerialClient);
- return;
- }
- if (isStop == false)
- {
- if (nodes.Count == 1 && testnode.Name.Contains("杂波抑制"))
- {
- MessageBox.Show("将产品输出电缆接【陷波】接口。", "温馨提示");
- }
- if (nodes.Count == 2 && testnode.Name.Contains("杂波抑制"))
- {
- if (group.Key == nodes[0].Channel)
- {
- ShowMessage(MsgType.Info, string.Format("开始执行测试指标:{0}/{1}", nodes[1].Channel, testnode.Name));
- }
- else if (group.Key == nodes[1].Channel)
- {
- ShowMessage(MsgType.Info, string.Format("开始执行测试指标:{0}/{1}", nodes[0].Channel, testnode.Name));
- }
-
- }
- else
- {
- ShowMessage(MsgType.Info, string.Format("开始执行测试指标:{0}/{1}", testnode.Channel, testnode.Name));
- }
-
-
- if (testnode.TestModel != null)
- {
-
- if (testnode.IsSelected)
- {
- currModel = testnode.TestModel;
- for (int i =0;i< ProductSeries.Length;i++)
- {
- if (FrmPara.IsRuning == false)//中途点击停止测试按钮
- {
- //控制断电
- StopClose(ProductSeries, DCPower, rainwormPower, SerialClient);
- return;
- }
- this.Serial = ProductSeries[i];
- SystemChofSerial = (Array.IndexOf(ProductSeries, ProductSeries[i]) + 1).ToString();
- UpDateCurrentSeries(this.Serial);
- // 陷波支路用于测杂波抑制,衰减支路用于测试其他指标,
- //双通道测试时:当测通道1的衰减支路指标时,在不换线的情况下,则进行通道2的陷波支路指标,然后换线,测通道2的衰减支路指标时,在不换线的情况下,则进行通道1的陷波支路指标。
- //单通道测试时,当测通道1的衰减支路指标时,换线后,单独测试通道1的陷波支路指标
- if (testnode.Name.Contains("杂波抑制"))
- {
- //todo:切产品同时切到陷波支路(1切8射频开关,1切2射频开关)
- }
- else
- {
- //todo:切产品同时切到衰减支路(1切8射频开关,1切2射频开关)
- }
- testnode.TestModel.Start(testnode, nodes);
- }
- }
- }
- else
- {
- ShowMessage(MsgType.Error, string.Format("测试指标:{0}/{1} 没有实例化执行模块", testnode.Channel, testnode.Name));
- }
- }
- else
- {
- break;
- }
- }
- }
- }
- }
- else
- {
- //按照指标名称进行
- foreach (IGrouping<string, TestNode> group in nodes.GroupBy(x => x.Name))
- {
- //排序后执行
- foreach (TestNode testnode in group.OrderBy(a => a.Order))
- {
- if (isStop == false)
- {
- ShowMessage(MsgType.Info, string.Format("开始执行测试指标:{0}/{1}", testnode.Channel, testnode.Name));
- if (testnode.TestModel != null)
- {
- currModel = testnode.TestModel;
- // testnode.TestModel.Start(testnode);
- }
- else
- {
- ShowMessage(MsgType.Error, string.Format("测试指标:{0}/{1} 没有实例化执行模块", testnode.Channel, testnode.Name));
- }
- }
- else
- {
- break;
- }
- }
- }
- }
- //保存自动测试记录表
- SaveTestTable();
- ShowMessage(MsgType.Info, string.Format("序列号:{0} 测试完成", Serial));
- }
- bool isStop = false;
- BaseModel currModel = null;
- public void Stop()
- {
- isStop = true;
- if(currModel!= null)
- {
- currModel.Stop();
- }
- ShowMessage(MsgType.Info, string.Format("序列号:{0} 测试中止", Serial));
- }
- public event DltShowMsg MessageEvent;
- /// <summary>
- /// 本对象显示消息,以及测试指标显示消息都经过这个方法
- /// </summary>
- /// <param name="msgType"></param>
- /// <param name="msg"></param>
- void ShowMessage(MsgType msgType, string msg)
- {
- if(MessageEvent != null)
- {
- MessageEvent(msgType, msg);
- }
- //if (msgType == MsgType.Notice || msgType == MsgType.Error)
- if (msgType == MsgType.Error)
- {
- MessageBox.Show(msg);
- }
- }
- public void ResetTestTable()
- {
- if(this.InitTestTable != null)
- {
- TestTable = this.InitTestTable.Copy();
- }
- }
- public void ResetManualTable()
- {
- if (this.InitManualTable != null)
- {
- ManualTable = this.InitManualTable.Copy();
- }
- }
- public AppLibs.Devices.IVISA GetDevice(string name)
- {
- DeviceNode node = this.Devices.FirstOrDefault(x => x.DeviceName == name);
- //不存在或者状态不对,都返回空
- if(node == null || node.State == false)
- {
- return null;
- }
- else
- {
- AppLibs.Devices.IDeviceHost devHost = Bundle.host.GetFirstOrDefaultService<AppLibs.Devices.IDeviceHost>();
- if (devHost == null)
- return null;
- DeviceParameter dev = devHost.GetFirstOrDefaultDeviceID(node.DeviceHandle);
- //要地址,名称,型号一致
- if (dev == null || !(node.DeviceType == dev.DeviceType && node.DeviceModel == dev.DeviceModel))
- {
- return null;
- }
- else
- {
- return (IVISA)dev;
- }
- }
- }
- public AppLibs.Devices.IVISA GetDeviceModel(string DeviceHandle)
- {
- DeviceNode node = this.Devices.FirstOrDefault(x => x.DeviceHandle == DeviceHandle);
- //不存在或者状态不对,都返回空
- if (node == null && node.State == false)
- {
- return null;
- }
- else
- {
- AppLibs.Devices.IDeviceHost devHost = Bundle.host.GetFirstOrDefaultService<AppLibs.Devices.IDeviceHost>();
- if (devHost == null)
- return null;
- DeviceParameter dev = devHost.GetFirstOrDefaultDeviceID(node.DeviceHandle);
- //要地址,名称,型号一致
- //if (dev == null || !(node.DeviceType == dev.DeviceType && node.DeviceModel == dev.DeviceModel))
- //if (dev == null || !(node.DeviceType == dev.DeviceType ))
- if (dev == null)
- {
- return null;
- }
- else
- {
- return (IVISA)dev;
- }
- }
- }
- public void SetManualTableCellValue(int row, int col, bool ok, object value)
- {
- if (ManualTableCellChanged != null)
- ManualTableCellChanged(row, col,ok, value);
- }
- public void SetTestTableCellValue(int row, int col, bool ok, object value)
- {
- if (TestTableCellChanged != null)
- TestTableCellChanged(row, col, ok, value);
- }
- public void TestTableAddCell(string series, string name,string systemch, string lower, string upper, string testval,string result)
- {
- if (AddTestTableCell != null)
- AddTestTableCell(series,name,systemch,lower,upper,testval,result);
- }
- public void UpDateCurrentSeries(string CuurentSeries)
- {
- if (CurrentSeries != null )
- {
- CurrentSeries(CuurentSeries);
- }
- }
- void SaveTestTable()
- {
- //if(string.IsNullOrEmpty(Serial))
- //{
- // ShowMessage(MsgType.Info, "序列号为空,数据不保存");
- // return;
- //}
- //AppLibs.Function.IReportHost report = Bundle.host.GetFirstOrDefaultService<AppLibs.Function.IReportHost>();
- //if(report != null)
- //{
- // AppLibs.Models.TableInfo tableInfo = new AppLibs.Models.TableInfo();
- // tableInfo.data = TestTable;
- // tableInfo.table = new AppLibs.Models.Table();
- // tableInfo.table.Result = "合格";
- // tableInfo.table.Temperature = Temperature;
- // tableInfo.table.Tester = Tester;
- // tableInfo.table.Time = DateTime.Now;
- // tableInfo.table.Place = Place;
- // tableInfo.table.Serial = Serial;
- // tableInfo.table.Humidity = Humidity;
- // tableInfo.table.Type = TestProject;
- // tableInfo.table.ProductCode = Product;
- // tableInfo.table.ProductName = Product;
- // tableInfo.table.Name = TestProject + "#" + Product;
- // try
- // {
- // if (report.WriteAndReplaceTable(tableInfo))
- // {
- // ShowMessage(MsgType.Info, "自动测试记录表保存成功");
- // }
- // else
- // {
- // ShowMessage(MsgType.Info, "自动测试记录表保存失败");
- // }
- // }
- // catch (Exception ex)
- // {
- // ShowMessage(MsgType.Error, "自动测试记录表保存失败");
- // }
-
- //}
- //else
- //{
- // ShowMessage(MsgType.Info, "软件未包含数据存储模块");
- //}
-
- }
- //判断某个测试项是否勾选
- public bool IsTestItemSelected(string channel , string name)
- {
- var node = this.TestNodes.FirstOrDefault(x => x.Channel == channel && x.Name == name);
- if (node == null)
- return false;
- return node.IsSelected;
- }
- #endregion
- #region 二次修改的方法
- //test
- #endregion
- }
- }
|