MainTps.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.ComponentModel;
  7. using Tps_LQ_Transmitter.com;
  8. using System.IO;
  9. using Tps_LQ_Transmitter.models;
  10. using System.Data;
  11. using AppLibs.Devices;
  12. using System.Windows.Forms;
  13. namespace Tps_LQ_Transmitter
  14. {
  15. public delegate void DltShowMsg(MsgType msgType, string msg);
  16. public delegate void DltCellValueChanged(int row, int cell, bool ok, object value);
  17. public delegate void DltCellValueAdd(string name, string lower, string upper, string testval, string result);
  18. /// <summary>
  19. /// 一个测试文件对应的测试项目
  20. /// </summary>
  21. public class MainTps
  22. {
  23. /// <summary>
  24. /// 测试人员
  25. /// </summary>
  26. public string Tester { get; set; }
  27. //测试地点
  28. public string Place { get; set; }
  29. //环境温度
  30. public string Temperature { get; set; }
  31. //当前测试的产品序列号
  32. public string Serial { get; set; }
  33. //环境湿度
  34. public string Humidity { get; set; }
  35. //待测产品
  36. public string Product { get; set; }
  37. //测试项目
  38. public string TestProject { get; set; }
  39. //配置文件路径
  40. string FilePath;
  41. /// <summary>
  42. /// 设备节点,表示当前测试程序包含的试验设备,绑定到界面的设备列表中
  43. /// </summary>
  44. public IList<DeviceNode> Devices;
  45. /// <summary>
  46. /// 测试节点,表示当前测试程序含有的测试指标,绑定到界面的指标列表中
  47. /// </summary>
  48. public IList<TestNode> TestNodes;
  49. /// <summary>
  50. /// 手工输入表
  51. /// </summary>
  52. public DataTable ManualTable;
  53. /// <summary>
  54. /// 自动测试表
  55. /// </summary>
  56. public DataTable TestTable;
  57. /// <summary>
  58. /// 测试值字典
  59. /// </summary>
  60. public Dictionary<string,double> TestValueDict;
  61. public Dictionary<string,double> GloabConfigDict;
  62. public string DCPowerAddress;//程控电源地址
  63. public string N9030A_SA_Address;//N9030A频谱仪地址
  64. public int TestFreqSum;//频点数
  65. public TransmitterSerialPort SerialClient = new TransmitterSerialPort();
  66. public byte ThridByte;//频点控制第3个字节
  67. public byte FourthByte;//频点控制第4个字节
  68. public int ControlDelay;//控制延时
  69. public event DltCellValueChanged ManualTableCellChanged;
  70. public event DltCellValueChanged TestTableCellChanged;
  71. public event DltCellValueAdd AddTestTableCell;
  72. DataTable InitTestTable,InitManualTable; //原始数据的界面,用于初始化或者重置
  73. /// <summary>
  74. /// 测试指标的结构
  75. /// </summary>
  76. List<BaseModelStruct> baseModelStructs;
  77. public MainTps()
  78. {
  79. Devices = new BindingList<DeviceNode>();
  80. TestNodes = new BindingList<TestNode>();
  81. GloabConfigDict = new Dictionary<string, double>();
  82. DCPowerAddress = "";
  83. baseModelStructs = new List<BaseModelStruct>();
  84. LoadBaseModelStructs();
  85. }
  86. #region 模板基础方法
  87. /// <summary>
  88. /// 加载配置文件
  89. /// </summary>
  90. /// <param name="filePath"></param>
  91. /// <returns></returns>
  92. public bool LoadConfigFile(string filePath)
  93. {
  94. bool ok = true;
  95. if (File.Exists(filePath) == false)
  96. {
  97. ShowMessage(MsgType.Error, string.Format("找不到配置文件:{0}", filePath));
  98. return false;
  99. }
  100. this.FilePath = filePath;
  101. using (Spire.Xls.Workbook workbook = new Spire.Xls.Workbook())
  102. {
  103. workbook.LoadFromFile(filePath);
  104. //处理Excel数据,更多请参考官方Demo
  105. Spire.Xls.Worksheet sheet = workbook.Worksheets["自动测试项总表"];
  106. if(sheet == null)
  107. {
  108. ShowMessage(MsgType.Error, "文件中找不到‘自动测试项总表’");
  109. return false;
  110. }
  111. int blankRows = 0; //避免出错
  112. int num = 0;
  113. //默认最多两百行
  114. while (num++ < 200)
  115. {
  116. //索引从1开始
  117. string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString():"";
  118. col1 = col1.Trim();
  119. if (string.IsNullOrEmpty(col1))
  120. {
  121. blankRows++;
  122. if(blankRows > 5)
  123. {
  124. //连续5个空行以上则退出
  125. break;
  126. }
  127. }
  128. else
  129. {
  130. blankRows = 0;
  131. }
  132. if (col1.Equals("设备配置表"))
  133. {
  134. ok &= readDevices(sheet,num);
  135. //解析行数跳过,实际上还得再加上
  136. num = num + this.Devices.Count+2;
  137. }
  138. else if (col1.Equals("测试指标配置表"))
  139. {
  140. ok &= readTestNodes(workbook,sheet,num);
  141. var items = (from p in TestNodes select p.Name).Distinct().ToArray();
  142. //解析行数跳过,实际上还得再加上
  143. num = num + items.Count() + 3;
  144. }
  145. }
  146. //加载人工测试项总表
  147. sheet = workbook.Worksheets["人工测试项总表"];
  148. if (sheet != null)
  149. {
  150. blankRows = 0; //避免出错
  151. num = 0;
  152. //默认最多两百行
  153. while (num++ < 200)
  154. {
  155. //索引从1开始
  156. string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  157. col1 = col1.Trim();
  158. if (string.IsNullOrEmpty(col1))
  159. {
  160. blankRows++;
  161. if (blankRows > 5)
  162. {
  163. //连续5个空行以上则退出
  164. break;
  165. }
  166. }
  167. else
  168. {
  169. blankRows = 0;
  170. }
  171. if (col1.Equals("人工测试项总表"))
  172. {
  173. this.ManualTable = readTable(sheet, num);
  174. if (this.ManualTable != null)
  175. {
  176. this.InitManualTable = this.ManualTable.Copy();
  177. }
  178. }
  179. }
  180. }
  181. //加载人工测试项总表
  182. sheet = workbook.Worksheets["自动测试记录表"];
  183. if (sheet != null)
  184. {
  185. blankRows = 0; //避免出错
  186. num = 0;
  187. //默认最多两百行
  188. while (num++ < 200)
  189. {
  190. //索引从1开始
  191. string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  192. col1 = col1.Trim();
  193. if (string.IsNullOrEmpty(col1))
  194. {
  195. blankRows++;
  196. if (blankRows > 5)
  197. {
  198. //连续5个空行以上则退出
  199. break;
  200. }
  201. }
  202. else
  203. {
  204. blankRows = 0;
  205. }
  206. if (col1.Equals("自动测试记录表"))
  207. {
  208. this.TestTable = readTable(sheet, num);
  209. if(this.TestTable != null)
  210. {
  211. this.InitTestTable = this.TestTable.Copy();
  212. }
  213. }
  214. }
  215. }
  216. //加载人工测试项总表
  217. sheet = workbook.Worksheets["公共设置"];
  218. if (sheet != null)
  219. {
  220. blankRows = 0; //避免出错
  221. num = 0;
  222. //默认最多两百行
  223. while (num++ < 200)
  224. {
  225. //索引从1开始
  226. string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  227. col1 = col1.Trim();
  228. if (string.IsNullOrEmpty(col1))
  229. {
  230. blankRows++;
  231. if (blankRows > 5)
  232. {
  233. //连续5个空行以上则退出
  234. break;
  235. }
  236. }
  237. else
  238. {
  239. blankRows = 0;
  240. }
  241. if (col1.Equals("参数表"))
  242. {
  243. //电压
  244. string WorkVolt = sheet.Range[3, 3].Value2 != null ? sheet.Range[3, 3].Value2.ToString() : "";
  245. GloabConfigDict.Add("电压", double.Parse(WorkVolt));
  246. //电流
  247. string WorkCurrent = sheet.Range[4, 3].Value2 != null ? sheet.Range[4, 3].Value2.ToString() : "";
  248. GloabConfigDict.Add("电流", double.Parse(WorkCurrent));
  249. DCPowerAddress = sheet.Range[5, 3].Value2 != null ? sheet.Range[5, 3].Value2.ToString() : "";
  250. TestFreqSum = int.Parse(sheet.Range[7, 3].Value2 != null ? sheet.Range[7, 3].Value2.ToString() : "");
  251. }
  252. }
  253. }
  254. }
  255. return ok;
  256. }
  257. /// <summary>
  258. /// 读设备配置表
  259. /// </summary>
  260. /// <param name="rowIndex">设备配置表的起始位置</param>
  261. bool readDevices(Spire.Xls.Worksheet sheet, int rowIndex)
  262. {
  263. //检查表的格式正确
  264. string col1 = sheet.Range[rowIndex + 1, 1].Value2 != null ? sheet.Range[rowIndex + 1, 1].Value2.ToString() : "";
  265. string col2 = sheet.Range[rowIndex + 1, 2].Value2 != null ? sheet.Range[rowIndex + 1, 2].Value2.ToString() : "";
  266. string col3 = sheet.Range[rowIndex + 1, 3].Value2 != null ? sheet.Range[rowIndex + 1, 3].Value2.ToString() : "";
  267. string col4 = sheet.Range[rowIndex + 1, 4].Value2 != null ? sheet.Range[rowIndex + 1, 4].Value2.ToString() : "";
  268. if (!(col1 == "设备名称" && col2 == "设备类型" && col3 == "设备型号" && col4 == "控制句柄"))
  269. {
  270. ShowMessage(MsgType.Error, "'设备配置表'行首标题位置和格式不正确");
  271. return false;
  272. }
  273. int num = rowIndex + 1;
  274. //默认最多30行
  275. while (num++ < rowIndex + 30)
  276. {
  277. col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  278. col2 = sheet.Range[num, 2].Value2 != null ? sheet.Range[num, 2].Value2.ToString() : "";
  279. col3 = sheet.Range[num, 3].Value2 != null ? sheet.Range[num, 3].Value2.ToString() : "";
  280. col4 = sheet.Range[num, 4].Value2 != null ? sheet.Range[num, 4].Value2.ToString() : "";
  281. if ( string.IsNullOrEmpty(col2) || string.IsNullOrEmpty(col3))
  282. {
  283. break;
  284. }
  285. DeviceNode dn = new DeviceNode();
  286. dn.DeviceName = col1;
  287. dn.DeviceType = col2;
  288. dn.DeviceModel = col3;
  289. dn.DeviceHandle = col4;
  290. this.Devices.Add(dn);
  291. }
  292. return true;
  293. }
  294. /// <summary>
  295. /// 读测试指标配置表
  296. /// </summary>
  297. /// <param name="rowIndex">测试指标配置表的起始位置</param>
  298. bool readTestNodes(Spire.Xls.Workbook workbook,Spire.Xls.Worksheet sheet, int rowIndex)
  299. {
  300. List<TestNode> models = new List<TestNode>();
  301. //检查表的格式正确
  302. string col1 = sheet.Range[rowIndex + 2, 1].Value2 != null ? sheet.Range[rowIndex + 2, 1].Value2.ToString() : "";
  303. string col2 = sheet.Range[rowIndex + 2, 2].Value2 != null ? sheet.Range[rowIndex + 2, 2].Value2.ToString() : "";
  304. string col3 = sheet.Range[rowIndex + 2, 3].Value2 != null ? sheet.Range[rowIndex + 2, 3].Value2.ToString() : "";
  305. string col4 = sheet.Range[rowIndex + 2, 4].Value2 != null ? sheet.Range[rowIndex + 2, 4].Value2.ToString() : "";
  306. if (!(col1 == "测试指标" && col2 == "默认勾选" && col3 == "测试模板" && col4 == "测试顺序"))
  307. {
  308. ShowMessage(MsgType.Error, "'测试指标配置表'行首标题位置和格式不正确");
  309. return false;
  310. }
  311. int num = rowIndex + 2;
  312. //默认最多100行,先解析测试指标个数
  313. while (num++ < rowIndex + 100)
  314. {
  315. col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  316. col2 = sheet.Range[num, 2].Value2 != null ? sheet.Range[num, 2].Value2.ToString() : "";
  317. col3 = sheet.Range[num, 3].Value2 != null ? sheet.Range[num, 3].Value2.ToString() : "";
  318. col4 = sheet.Range[num, 4].Value2 != null ? sheet.Range[num, 4].Value2.ToString() : "";
  319. if (string.IsNullOrEmpty(col1) || string.IsNullOrEmpty(col3))
  320. {
  321. break;
  322. }
  323. TestNode tn = new TestNode();
  324. tn.Name = col1;
  325. if(col2 == "是")
  326. {
  327. tn.IsSelected = true;
  328. }
  329. else
  330. {
  331. tn.IsSelected = false;
  332. }
  333. tn.Template = col3;
  334. int order = Int32.MaxValue;
  335. if (Int32.TryParse(col4, out order))
  336. tn.Order = order;
  337. models.Add(tn);
  338. }
  339. //再解析各通道的配置数据
  340. int colIndex = 5; //通道数据从第5列开始
  341. while(true)
  342. {
  343. col1 = sheet.Range[rowIndex + 3, colIndex].Value2 != null ? sheet.Range[rowIndex + 3, colIndex].Value2.ToString() : "";
  344. col2 = sheet.Range[rowIndex + 3, colIndex + 1].Value2 != null ? sheet.Range[rowIndex + 3, colIndex + 1].Value2.ToString() : "";
  345. //有新通道数据则继续,否则退出
  346. if(string.IsNullOrEmpty(col1) || string.IsNullOrEmpty(col2) || col2 != "启用")
  347. {
  348. break;
  349. }
  350. string channel = sheet.Range[rowIndex + 1, colIndex].Value2 != null ? sheet.Range[rowIndex + 1, colIndex].Value2.ToString() : "";
  351. //按指标个数来解析通道数据
  352. for (int i = 0; i < models.Count; i++)
  353. {
  354. col1 = sheet.Range[rowIndex + 3 + i, colIndex].Value2 != null ? sheet.Range[rowIndex + 3 + i, colIndex].Value2.ToString() : "";
  355. col2 = sheet.Range[rowIndex + 3 + i, colIndex + 1].Value2 != null ? sheet.Range[rowIndex + 3 + i, colIndex + 1].Value2.ToString() : "";
  356. if (col2 != "启用")
  357. continue;
  358. TestNode newNode = models[i].Copy();
  359. newNode.Channel = channel; //通道名
  360. newNode.ParaConfigTable = col1; //测试配置表
  361. //复制已有参数表,或者从文件解析参数表
  362. TestNode preNode = TestNodes.FirstOrDefault(x => x.ParaConfigTable == col1);
  363. if(preNode != null)
  364. {
  365. //存在则复制,避免读文件费时间
  366. newNode.Parameters = preNode.Parameters.Copy();
  367. }
  368. else
  369. {
  370. //否则找到文件中对应的配置表
  371. Spire.Xls.Worksheet paraSheet = workbook.Worksheets[col1];
  372. if (paraSheet == null)
  373. {
  374. ShowMessage(MsgType.Error, "文件中找不到参数配置表:"+ col1);
  375. continue;
  376. }
  377. //解析参数配置表
  378. newNode.Parameters = readTestParameter(paraSheet);
  379. }
  380. //如果已经创建了同名的处理对象,则直接使用,否则则新建,采用单例模式
  381. //如果注释了下面的if,就是多例模式,即同名处理模块是不同的软件对象
  382. TestNode tn = this.TestNodes.FirstOrDefault(x => x.Template == newNode.Template);
  383. if(tn != null && tn.TestModel != null)
  384. {
  385. newNode.TestModel = tn.TestModel;
  386. }
  387. else
  388. {
  389. //根据测试指标的名称,创建BaseModel指标处理对象
  390. newNode.TestModel = CreateModel(newNode.Template);
  391. }
  392. this.TestNodes.Add(newNode);
  393. }
  394. colIndex += 2; //跳过2列
  395. }
  396. return true;
  397. }
  398. BaseModel CreateModel(string template)
  399. {
  400. //如果没有这个名字的软件模块,则返回null
  401. BaseModelStruct bms = baseModelStructs.FirstOrDefault(x => x.ModelName == template);
  402. if (bms == null)
  403. {
  404. ShowMessage(MsgType.Error, "找不到名称为:" + template +" 的测试指标模块。");
  405. return null;
  406. }
  407. BaseModel model = (BaseModel)Activator.CreateInstance(bms.type);
  408. model.tps = this;
  409. model.MessageEvent += ShowMessage;
  410. return model;
  411. }
  412. void LoadBaseModelStructs()
  413. {
  414. try
  415. {
  416. Type[] types = this.GetType().Assembly.GetTypes();
  417. foreach (var item in types)
  418. {
  419. //排除接口本身
  420. if (item.Name != "BaseModel")
  421. {
  422. if (typeof(BaseModel).IsAssignableFrom(item))
  423. {
  424. BaseModel model = (BaseModel)Activator.CreateInstance(item);
  425. BaseModelStruct bms = new BaseModelStruct();
  426. bms.ModelName = model.TemplateName;
  427. bms.type = item;
  428. this.baseModelStructs.Add(bms);
  429. model = null;
  430. }
  431. }
  432. }
  433. }
  434. catch(Exception e)
  435. {
  436. ShowMessage(MsgType.Error, "实例化BaseModel测试指标模块时出现错误,某个测试指标模块创建时发生错误,请检查代码!详细错误见程序日志文件!");
  437. Bundle.log.WriteLog(AppLibs.Host.LogLevel.Error, "类:MainTps,方法:LoadBaseModelStructs", e.Message + e.StackTrace);
  438. }
  439. }
  440. /// <summary>
  441. /// 读参数配置表
  442. /// </summary>
  443. /// <returns></returns>
  444. TestParameters readTestParameter(Spire.Xls.Worksheet sheet)
  445. {
  446. TestParameters pars = new TestParameters();
  447. int blankRows = 0; //避免出错
  448. int num = 0;
  449. //默认最多两百行
  450. while (num++ < 200)
  451. {
  452. //索引从1开始
  453. string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  454. col1 = col1.Trim();
  455. if (string.IsNullOrEmpty(col1))
  456. {
  457. blankRows++;
  458. if (blankRows > 5)
  459. {
  460. //连续5个空行以上则退出
  461. break;
  462. }
  463. }
  464. else
  465. {
  466. blankRows = 0;
  467. }
  468. if (col1.Equals("参数表"))
  469. {
  470. ///获取表中的参数
  471. List<ParameterNode> nodes = readParameters(sheet,num);
  472. foreach (var item in nodes)
  473. {
  474. if(pars.Parameters.ContainsKey(item.Name))
  475. {
  476. ShowMessage(MsgType.Error, string.Format("参数配置表:{0} 存在同名参数:{1},无法重复添加参数", sheet.Name, item.Name));
  477. continue;
  478. }
  479. pars.Parameters.Add(item.Name, item);
  480. }
  481. //解析行数跳过,实际上还得再加上
  482. num = num + nodes.Count +1;
  483. }
  484. else if (col1.Equals("测试显示表"))
  485. {
  486. //获取表中的原始显示界面
  487. pars.InitialTable = readTable( sheet, num);
  488. //解析行数跳过,实际上还得再加上
  489. num = num + pars.InitialTable.Rows.Count + 1;
  490. }
  491. }
  492. return pars;
  493. }
  494. /// <summary>
  495. /// 读参数表
  496. /// </summary>
  497. /// <param name="sheet"></param>
  498. /// <returns></returns>
  499. List<ParameterNode> readParameters(Spire.Xls.Worksheet sheet,int rowIndex)
  500. {
  501. List<ParameterNode> nodes = new List<ParameterNode>();
  502. string col1 = sheet.Range[rowIndex + 1, 1].Value2 != null ? sheet.Range[rowIndex + 1, 1].Value2.ToString() : "";
  503. string col2 = sheet.Range[rowIndex + 1, 2].Value2 != null ? sheet.Range[rowIndex + 1, 2].Value2.ToString() : "";
  504. string col3 = sheet.Range[rowIndex + 1, 3].Value2 != null ? sheet.Range[rowIndex + 1, 3].Value2.ToString() : "";
  505. string col4 = sheet.Range[rowIndex + 1, 4].Value2 != null ? sheet.Range[rowIndex + 1, 4].Value2.ToString() : "";
  506. if (!(col1 == "名称" && col2 == "类型" && col3 == "值"))
  507. {
  508. ShowMessage(MsgType.Error, "'参数表'行首标题位置和格式不正确");
  509. return nodes;
  510. }
  511. int num = rowIndex + 1;
  512. //默认最多30行
  513. while (num++ < rowIndex + 200)
  514. {
  515. col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  516. col2 = sheet.Range[num, 2].Value2 != null ? sheet.Range[num, 2].Value2.ToString() : "";
  517. col3 = sheet.Range[num, 3].Value2 != null ? sheet.Range[num, 3].Value2.ToString() : "";
  518. col4 = sheet.Range[num, 4].Value2 != null ? sheet.Range[num, 4].Value2.ToString() : "";
  519. if (string.IsNullOrEmpty(col1) || string.IsNullOrEmpty(col2) || string.IsNullOrEmpty(col3))
  520. {
  521. break;
  522. }
  523. ParameterNode pn = new ParameterNode();
  524. pn.Name = col1;
  525. pn.Type = col2;
  526. pn.StrValue = col3;
  527. pn.Mark = col4;
  528. nodes.Add(pn);
  529. }
  530. return nodes;
  531. }
  532. /// <summary>
  533. /// 读表格,人工测试表和测试显示表
  534. /// </summary>
  535. /// <param name="sheet"></param>
  536. /// <returns></returns>
  537. DataTable readTable(Spire.Xls.Worksheet sheet, int rowIndex)
  538. {
  539. //先创建列,再创建行
  540. DataTable dt = new DataTable();
  541. int num = 0;
  542. while (num++ < 50)
  543. {
  544. string col = sheet.Range[rowIndex + 1, num].Value2 != null ? sheet.Range[rowIndex + 1, num].Value2.ToString() : "";
  545. if (string.IsNullOrEmpty(col))
  546. break;
  547. dt.Columns.Add(col);
  548. }
  549. //再创建行
  550. num = rowIndex + 1;
  551. //默认最多200行
  552. while (num++ < rowIndex + 200)
  553. {
  554. //首列无值就认为表格结束
  555. string val = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  556. if (string.IsNullOrEmpty(val.Trim()))
  557. break;
  558. DataRow dr = dt.NewRow();
  559. dt.Rows.Add(dr);
  560. for (int i = 0; i < dt.Columns.Count; i++)
  561. {
  562. val = sheet.Range[num, i+1].Value2 != null ? sheet.Range[num, i+1].Value2.ToString() : "";
  563. dr[i] = val;
  564. }
  565. }
  566. return dt;
  567. }
  568. /// <summary>
  569. /// 检查仪器是否正常配置
  570. /// </summary>
  571. /// <returns></returns>
  572. public bool CheckDevices()
  573. {
  574. bool ok = true;
  575. AppLibs.Devices.IDeviceHost devHost = Bundle.host.GetFirstOrDefaultService<AppLibs.Devices.IDeviceHost>();
  576. foreach (DeviceNode device in this.Devices)
  577. {
  578. DeviceParameter dev = devHost.GetFirstOrDefaultDeviceID(device.DeviceHandle);
  579. if (dev == null || !(device.DeviceType == dev.DeviceType && device.DeviceModel == dev.DeviceModel))
  580. {
  581. device.State = false;
  582. ok = false;
  583. }
  584. else
  585. {
  586. device.State = true;
  587. continue;
  588. }
  589. }
  590. return ok;
  591. }
  592. /// <summary>
  593. ///
  594. /// </summary>
  595. /// <param name="isOrderByChannel">按照指标分组还是按照通道分组</param>
  596. public void Start(bool isOrderByChannel)
  597. {
  598. ShowMessage(MsgType.Info, string.Format("序列号:{0} 测试开始",Serial));
  599. isStop = false;
  600. //清空测试数据
  601. // BaseModel.TestResults.Clear();
  602. //选择勾选项
  603. List<TestNode> nodes = this.TestNodes.Where(x => x.IsSelected == true).ToList();
  604. if(isOrderByChannel)
  605. {
  606. //按照通道分组
  607. int Channel = 1;
  608. foreach (IGrouping<string, TestNode> group in nodes.GroupBy(x => x.Channel))
  609. {
  610. //ShowMessage(MsgType.Notice,$"温馨提示:即将测试第{Channel}通道,请将线缆换到{Channel}通道!");
  611. //Channel++;
  612. //排序后执行
  613. foreach (TestNode testnode in group.OrderBy(a => a.Order))
  614. {
  615. if(isStop == false)
  616. {
  617. ShowMessage(MsgType.Info, string.Format("开始执行测试指标:{0}/{1}", testnode.Channel, testnode.Name));
  618. if(testnode.TestModel != null)
  619. {
  620. //测试中途取消了勾选项
  621. if(testnode.IsSelected)
  622. {
  623. currModel = testnode.TestModel;
  624. testnode.TestModel.Start(testnode);
  625. }
  626. }
  627. else
  628. {
  629. ShowMessage(MsgType.Error, string.Format("测试指标:{0}/{1} 没有实例化执行模块", testnode.Channel, testnode.Name));
  630. }
  631. }
  632. else
  633. {
  634. break;
  635. }
  636. }
  637. }
  638. }
  639. else
  640. {
  641. //按照指标名称进行
  642. foreach (IGrouping<string, TestNode> group in nodes.GroupBy(x => x.Name))
  643. {
  644. //排序后执行
  645. foreach (TestNode testnode in group.OrderBy(a => a.Order))
  646. {
  647. if (isStop == false)
  648. {
  649. ShowMessage(MsgType.Info, string.Format("开始执行测试指标:{0}/{1}", testnode.Channel, testnode.Name));
  650. if (testnode.TestModel != null)
  651. {
  652. currModel = testnode.TestModel;
  653. testnode.TestModel.Start(testnode);
  654. }
  655. else
  656. {
  657. ShowMessage(MsgType.Error, string.Format("测试指标:{0}/{1} 没有实例化执行模块", testnode.Channel, testnode.Name));
  658. }
  659. }
  660. else
  661. {
  662. break;
  663. }
  664. }
  665. }
  666. }
  667. //保存自动测试记录表
  668. SaveTestTable();
  669. ShowMessage(MsgType.Info, string.Format("序列号:{0} 测试完成", Serial));
  670. }
  671. bool isStop = false;
  672. BaseModel currModel = null;
  673. public void Stop()
  674. {
  675. isStop = true;
  676. if(currModel!= null)
  677. {
  678. currModel.Stop();
  679. }
  680. ShowMessage(MsgType.Info, string.Format("序列号:{0} 测试中止", Serial));
  681. }
  682. public event DltShowMsg MessageEvent;
  683. /// <summary>
  684. /// 本对象显示消息,以及测试指标显示消息都经过这个方法
  685. /// </summary>
  686. /// <param name="msgType"></param>
  687. /// <param name="msg"></param>
  688. void ShowMessage(MsgType msgType, string msg)
  689. {
  690. if(MessageEvent != null)
  691. {
  692. MessageEvent(msgType, msg);
  693. }
  694. //if (msgType == MsgType.Notice || msgType == MsgType.Error)
  695. if (msgType == MsgType.Error)
  696. {
  697. MessageBox.Show(msg);
  698. }
  699. }
  700. public void ResetTestTable()
  701. {
  702. if(this.InitTestTable != null)
  703. {
  704. TestTable = this.InitTestTable.Copy();
  705. }
  706. }
  707. public void ResetManualTable()
  708. {
  709. if (this.InitManualTable != null)
  710. {
  711. ManualTable = this.InitManualTable.Copy();
  712. }
  713. }
  714. public AppLibs.Devices.IVISA GetDevice(string name)
  715. {
  716. DeviceNode node = this.Devices.FirstOrDefault(x => x.DeviceName == name);
  717. //不存在或者状态不对,都返回空
  718. if(node == null || node.State == false)
  719. {
  720. return null;
  721. }
  722. else
  723. {
  724. AppLibs.Devices.IDeviceHost devHost = Bundle.host.GetFirstOrDefaultService<AppLibs.Devices.IDeviceHost>();
  725. if (devHost == null)
  726. return null;
  727. DeviceParameter dev = devHost.GetFirstOrDefaultDeviceID(node.DeviceHandle);
  728. //要地址,名称,型号一致
  729. if (dev == null || !(node.DeviceType == dev.DeviceType && node.DeviceModel == dev.DeviceModel))
  730. {
  731. return null;
  732. }
  733. else
  734. {
  735. return (IVISA)dev;
  736. }
  737. }
  738. }
  739. public AppLibs.Devices.IVISA GetDeviceModel(string DeviceHandle)
  740. {
  741. DeviceNode node = this.Devices.FirstOrDefault(x => x.DeviceHandle == DeviceHandle);
  742. //不存在或者状态不对,都返回空
  743. if (node == null && node.State == false)
  744. {
  745. return null;
  746. }
  747. else
  748. {
  749. AppLibs.Devices.IDeviceHost devHost = Bundle.host.GetFirstOrDefaultService<AppLibs.Devices.IDeviceHost>();
  750. if (devHost == null)
  751. return null;
  752. DeviceParameter dev = devHost.GetFirstOrDefaultDeviceID(node.DeviceHandle);
  753. //要地址,名称,型号一致
  754. //if (dev == null || !(node.DeviceType == dev.DeviceType && node.DeviceModel == dev.DeviceModel))
  755. //if (dev == null || !(node.DeviceType == dev.DeviceType ))
  756. if (dev == null)
  757. {
  758. return null;
  759. }
  760. else
  761. {
  762. return (IVISA)dev;
  763. }
  764. }
  765. }
  766. public void SetManualTableCellValue(int row, int col, bool ok, object value)
  767. {
  768. if (ManualTableCellChanged != null)
  769. ManualTableCellChanged(row, col,ok, value);
  770. }
  771. public void SetTestTableCellValue(int row, int col, bool ok, object value)
  772. {
  773. if (TestTableCellChanged != null)
  774. TestTableCellChanged(row, col, ok, value);
  775. }
  776. public void TestTableAddCell(string name, string lower, string upper, string testval,string result)
  777. {
  778. if (AddTestTableCell != null)
  779. AddTestTableCell(name,lower,upper,testval,result);
  780. }
  781. void SaveTestTable()
  782. {
  783. //if(string.IsNullOrEmpty(Serial))
  784. //{
  785. // ShowMessage(MsgType.Info, "序列号为空,数据不保存");
  786. // return;
  787. //}
  788. //AppLibs.Function.IReportHost report = Bundle.host.GetFirstOrDefaultService<AppLibs.Function.IReportHost>();
  789. //if(report != null)
  790. //{
  791. // AppLibs.Models.TableInfo tableInfo = new AppLibs.Models.TableInfo();
  792. // tableInfo.data = TestTable;
  793. // tableInfo.table = new AppLibs.Models.Table();
  794. // tableInfo.table.Result = "合格";
  795. // tableInfo.table.Temperature = Temperature;
  796. // tableInfo.table.Tester = Tester;
  797. // tableInfo.table.Time = DateTime.Now;
  798. // tableInfo.table.Place = Place;
  799. // tableInfo.table.Serial = Serial;
  800. // tableInfo.table.Humidity = Humidity;
  801. // tableInfo.table.Type = TestProject;
  802. // tableInfo.table.ProductCode = Product;
  803. // tableInfo.table.ProductName = Product;
  804. // tableInfo.table.Name = TestProject + "#" + Product;
  805. // try
  806. // {
  807. // if (report.WriteAndReplaceTable(tableInfo))
  808. // {
  809. // ShowMessage(MsgType.Info, "自动测试记录表保存成功");
  810. // }
  811. // else
  812. // {
  813. // ShowMessage(MsgType.Info, "自动测试记录表保存失败");
  814. // }
  815. // }
  816. // catch (Exception ex)
  817. // {
  818. // ShowMessage(MsgType.Error, "自动测试记录表保存失败");
  819. // }
  820. //}
  821. //else
  822. //{
  823. // ShowMessage(MsgType.Info, "软件未包含数据存储模块");
  824. //}
  825. }
  826. //判断某个测试项是否勾选
  827. public bool IsTestItemSelected(string channel , string name)
  828. {
  829. var node = this.TestNodes.FirstOrDefault(x => x.Channel == channel && x.Name == name);
  830. if (node == null)
  831. return false;
  832. return node.IsSelected;
  833. }
  834. #endregion
  835. #region 二次修改的方法
  836. //test
  837. #endregion
  838. }
  839. }