FrmMain.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. using CommonDevHostApp.Devices;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using Tps_LQ_Transmitter.com;
  15. using Tps_LQ_Transmitter.Common;
  16. using Tps_LQ_Transmitter.views;
  17. namespace Tps_LQ_Transmitter
  18. {
  19. public partial class FrmMain : Form
  20. {
  21. /// <summary>
  22. /// config目录下存在的测试配置文件信息
  23. /// </summary>
  24. IList<FileNode> FileNodes;
  25. bool comFlag = false;
  26. /// <summary>
  27. /// 当前选择的测试配置文件信息
  28. /// </summary>
  29. FileNode currFileNode;
  30. /// <summary>
  31. /// 停止测试标志
  32. /// </summary>
  33. public bool IsRuning = true;
  34. CommonVisaResource DCPower = null;
  35. /// <summary>
  36. /// 内部蚯蚓电源控制
  37. /// </summary>
  38. public static RainwormPower rainwormPower = null;
  39. /// <summary>
  40. /// 供电、射频开关与数采模块
  41. /// </summary>
  42. public static DioControlClass dio = null;
  43. /// <summary>
  44. /// 陷波器/衰减器 切换
  45. /// </summary>
  46. public static PcGpio gpio = null;
  47. /// <summary>
  48. /// PCM通信
  49. /// </summary>
  50. public static PcmControl pcm = null;
  51. /// <summary>
  52. /// 产品序列号数组
  53. /// </summary>
  54. public static string[] ProductSeries = null;
  55. /// <summary>
  56. /// 启用的产品路数组
  57. /// </summary>
  58. public static bool[] ProdEnabCh = { false, false, false, false, false, false, false, false };
  59. // 1 2 3 4 5 6 7 8
  60. FrmMsg frmMsg;
  61. FrmDevice frmDevice;
  62. public FrmMain()
  63. {
  64. InitializeComponent();
  65. frmMsg = new FrmMsg();
  66. frmDevice = new FrmDevice();
  67. }
  68. private void FrmMain_Load(object sender, EventArgs e)
  69. {
  70. //this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
  71. FileNodes = new BindingList<FileNode>();
  72. cbbProduct.ValueMember = "TestProject";
  73. cbbProduct.DisplayMember = "DisplayName";
  74. cbbProduct.DataSource = FileNodes;
  75. //加载主界面时就将内部硬件(蚯蚓电源、数采、射频开关)控制端口new出
  76. dio = new DioControlClass();
  77. gpio = new PcGpio();
  78. rainwormPower = new RainwormPower();
  79. pcm = new PcmControl();
  80. gpio.InitSetOut();
  81. for (int i = 0; i < 8; i++)
  82. {
  83. dio.SupplySwitch(i+1,DioControlClass.State.OFF);//加载页面时关闭供电开关
  84. }
  85. gpio.AllSwitchAttenuator();//加载页面时8路全切换至衰减器
  86. //加载配置文件信息
  87. string folder = Path.Combine(Bundle.bundle.BundleDir, "config");
  88. if(Directory.Exists(folder))
  89. {
  90. //文件名示例:测试#常温测试#035变频模块.xlsx
  91. string[] files = Directory.GetFiles(folder, "测试#*.xlsx");
  92. foreach (string file in files)
  93. {
  94. FileNode node = new FileNode();
  95. node.FilePath = file;
  96. string fname = Path.GetFileNameWithoutExtension(file);
  97. fname = fname.Substring(fname.IndexOf('#') + 1);
  98. node.DisplayName = fname;
  99. node.TestProject = fname.Substring(0, fname.IndexOf('#'));
  100. node.ProductName = fname.Substring(fname.IndexOf('#') + 1);
  101. FileNodes.Add(node);
  102. }
  103. }
  104. cbbProduct.Refresh();
  105. comFlag = true;
  106. superTabControl1.SelectedTabIndex = 0;
  107. }
  108. private void BtnLoadTpsConfig_Click(object sender, EventArgs e)
  109. {
  110. if (cbbProduct.SelectedIndex >= 0 && cbbProduct.SelectedItem != null)
  111. {
  112. //把试验项目显示到界面
  113. FileNode fn = (FileNode)cbbProduct.SelectedItem;
  114. //加载内容
  115. if (fn.Tps == null)
  116. {
  117. MainTps tps = new MainTps();
  118. try
  119. {
  120. if (tps.LoadConfigFile(fn.FilePath))
  121. {
  122. fn.Tps = tps;
  123. }
  124. else
  125. {
  126. ShowMessage(MsgType.Error, string.Format("加载配置文件失败,文件路径:{0}", fn.FilePath));
  127. return;
  128. }
  129. }
  130. catch(Exception ee)
  131. {
  132. ShowMessage(MsgType.Error, string.Format("加载配置文件出现异常:{0},详细请查看日志文件。", ee.Message));
  133. Bundle.log.WriteLog(AppLibs.Host.LogLevel.Error, "类:FrmMain,方法:BtnLoadTpsConfig_Click", ee.Message + ee.StackTrace);
  134. }
  135. }
  136. //初始化界面相关
  137. ShowTps(fn);
  138. }
  139. }
  140. List<TestMessage> tMsgs = new List<TestMessage>();
  141. void ShowMessage(MsgType msgType , string msg)
  142. {
  143. if (msgType == MsgType.Error)
  144. {
  145. errorCount++;
  146. this.Invoke(new Action(() => {
  147. BtnMessage.Text = "消息/错误:" + errorCount.ToString();
  148. BtnMessage.ForeColor = Color.Red;
  149. }));
  150. }
  151. tMsgs.Add(new TestMessage() { type = msgType, message = msg });
  152. frmMsg.ShowMsg(tMsgs);
  153. }
  154. /// <summary>
  155. /// 清空消息
  156. /// </summary>
  157. void ClearMessage()
  158. {
  159. tMsgs.Clear();
  160. frmMsg.ClearMsg();
  161. }
  162. void ShowTps(FileNode fn)
  163. {
  164. if (currFileNode != null && currFileNode.Tps!= null )
  165. {
  166. currFileNode.Tps.MessageEvent -= Tps_MessageEvent;
  167. currFileNode.Tps.ManualTableCellChanged -= Tps_ManualTableCellChanged;
  168. currFileNode.Tps.TestTableCellChanged -= Tps_TestTableCellChanged;
  169. currFileNode.Tps.AddTestTableCell -= Tps_TestTableAddCell;
  170. currFileNode.Tps.CurrentSeries -= tps_UpdateCurrentSeries;
  171. }
  172. currFileNode = fn;
  173. if(currFileNode.Tps != null)
  174. {
  175. currFileNode.Tps.MessageEvent += Tps_MessageEvent;
  176. currFileNode.Tps.ManualTableCellChanged += Tps_ManualTableCellChanged;
  177. currFileNode.Tps.TestTableCellChanged += Tps_TestTableCellChanged;
  178. currFileNode.Tps.AddTestTableCell += Tps_TestTableAddCell;
  179. currFileNode.Tps.CurrentSeries += tps_UpdateCurrentSeries;
  180. }
  181. currFileNode.Tps.RemainTimeUpdate += RemainTime;
  182. tbTestProject.Text = fn.TestProject;
  183. displayTree(true);
  184. //显示主界面
  185. dgvTestData.DataSource = currFileNode.Tps.TestTable;
  186. //显示人工配置界面
  187. dgvManualData.DataSource = currFileNode.Tps.ManualTable;
  188. //显示仪器列表,把当前TPS使用的仪器和仪器检查界面绑定
  189. frmDevice.ShowDevices(currFileNode.Tps.Devices);
  190. }
  191. private void Tps_TestTableAddCell(string series,string name, string systemch,string lower, string upper, string testval, string result)
  192. {
  193. this.Invoke(new Action(() => {
  194. currFileNode.Tps.TestTable.Rows.Add(series, name, systemch,lower,upper,testval, result);
  195. if (result=="是")
  196. {
  197. dgvTestData.Rows[currFileNode.Tps.TestTable.Rows.Count-1].Cells[5].Style.BackColor = Color.Green;
  198. }
  199. else
  200. {
  201. dgvTestData.Rows[currFileNode.Tps.TestTable.Rows.Count-1].Cells[5].Style.BackColor = Color.Red;
  202. }
  203. }));
  204. }
  205. private void Tps_TestTableCellChanged(int row, int cell,bool ok, object value)
  206. {
  207. this.Invoke(new Action(() => {
  208. currFileNode.Tps.TestTable.Rows[row][cell] = value;
  209. if (ok)
  210. {
  211. dgvTestData.Rows[row].Cells[cell].Style.BackColor = Color.White;
  212. }
  213. else
  214. {
  215. dgvTestData.Rows[row].Cells[cell].Style.BackColor = Color.Red;
  216. }
  217. }));
  218. }
  219. private void Tps_ManualTableCellChanged(int row, int cell, bool ok, object value)
  220. {
  221. this.Invoke(new Action(() => {
  222. currFileNode.Tps.ManualTable.Rows[row][cell] = value;
  223. if (ok)
  224. {
  225. dgvTestData.Rows[row].Cells[cell].Style.BackColor = Color.White;
  226. }
  227. else
  228. {
  229. dgvTestData.Rows[row].Cells[cell].Style.BackColor = Color.Red;
  230. }
  231. }));
  232. }
  233. private void RemainTime(string channel, int point, int FrequencyNumber, double CenterFreq, double testMin)
  234. {
  235. this.BeginInvoke(new Action(() =>
  236. {
  237. labRemainTime.Text = $"{channel},第{point + 1}/{FrequencyNumber}个频点:{CenterFreq}MHz测试,耗时{Math.Round(testMin, 2)}Min。";
  238. }));
  239. }
  240. private void tps_UpdateCurrentSeries(string series)
  241. {
  242. this.BeginInvoke(new Action(() =>
  243. {
  244. tbSerials.Text = series;
  245. }));
  246. }
  247. int errorCount = 0;
  248. private void Tps_MessageEvent(MsgType msgType, string msg)
  249. {
  250. ShowMessage(msgType, msg);
  251. }
  252. bool isOrderByChannel = true;
  253. Task task;
  254. private void BtnStart_Click(object sender, EventArgs e)
  255. {
  256. if (currFileNode == null)
  257. return;
  258. if (tbSerials.Items.Count == 0)
  259. {
  260. MessageBox.Show("温馨提示:请输入产品编号后再进行测试!");
  261. return;
  262. }
  263. labRemainTime.Text = $"开始测试";
  264. IsRuning = true;
  265. BtnLoadTpsConfig.Enabled = false;
  266. BtnStart.Enabled = false;
  267. tbSerials.Enabled = false;
  268. currFileNode.Tps.Tester = tbTester.Text;
  269. currFileNode.Tps.Place = tbPlace.Text;
  270. currFileNode.Tps.Product = currFileNode.ProductName;
  271. currFileNode.Tps.Serial = tbSerial.Text;
  272. currFileNode.Tps.Temperature = tbTemp.Text;
  273. currFileNode.Tps.Humidity = tbRH.Text;
  274. currFileNode.Tps.TestProject = tbTestProject.Text;
  275. ClearMessage();
  276. //检查仪器,如果OK则开始测试
  277. if(currFileNode.Tps.CheckDevices())
  278. {
  279. if(BtnDevice.ForeColor == Color.Red)
  280. {
  281. BtnDevice.ForeColor = Color.Black;
  282. }
  283. StartTest();
  284. }
  285. else
  286. {
  287. //设备检查不通过,需要提醒用户确认
  288. //BtnDevice.ForeColor = Color.Red;
  289. //显示仪器设置界面
  290. //if(MessageBox.Show("设备参数异常,是否继续测试?","警告",MessageBoxButtons.YesNo) == DialogResult.Yes)
  291. //{
  292. StartTest();
  293. //}
  294. //else
  295. //{
  296. // BtnLoadTpsConfig.Enabled = true;
  297. // BtnStart.Enabled = true;
  298. //}
  299. }
  300. }
  301. Thread SerialHandleThread1;
  302. private void WorkThretdHandleFunction(object num)
  303. {
  304. }
  305. void StartTest()
  306. {
  307. Stopwatch TimesCounter = new Stopwatch();
  308. double testMin = 0;
  309. TimesCounter.Restart();
  310. //开始测试的时候重置测试表格
  311. currFileNode.Tps.ResetTestTable();
  312. SerialHandleThread1 = new Thread(new ParameterizedThreadStart(WorkThretdHandleFunction));
  313. SerialHandleThread1.Priority = ThreadPriority.Highest;
  314. MainTps tps = new MainTps();
  315. SerialConfig scfg = new SerialConfig();
  316. MatchComPara CfigComParas = scfg.LoadComWorkBook();
  317. if (CfigComParas == null)
  318. {
  319. return;
  320. }
  321. byte FourthByte = 0x00;
  322. string ComPort = CfigComParas.GetComPort("1");
  323. byte ThridByte = Convert.ToByte(CfigComParas.GetThirdByte("1"), 16);
  324. currFileNode.Tps.ThridByte = ThridByte;
  325. int FrequencyNumber;
  326. if (currFileNode.Tps.TestFreqSum >= CfigComParas.ComParameters.Count)//如果限定的测试频点数大于设置的频点数
  327. {
  328. FrequencyNumber = CfigComParas.ComParameters.Count;//取设置的频点数
  329. }
  330. else
  331. {
  332. FrequencyNumber = currFileNode.Tps.TestFreqSum;//取限定的测试频点数
  333. }
  334. dgvTestData.DataSource = null;
  335. dgvTestData.DataSource = currFileNode.Tps.TestTable;
  336. double volt = currFileNode.Tps.GloabConfigDict["电压"];
  337. double Current = currFileNode.Tps.GloabConfigDict["电流"];
  338. // ConfigParameter SetVoltPara = new ConfigParameter();
  339. int countx = 0;//记录启用了几路(共8路)
  340. for (int i = 0; i < 8; i++)
  341. {
  342. if (ProdEnabCh[i]==true)
  343. {
  344. countx++;
  345. ShowMessage(MsgType.Info, string.Format("已启用第{0}路!",i+1));
  346. }
  347. }
  348. //if (tbSerials.Items.Count > 1)//如果测试台数大于1,则使用外部TDK电源
  349. if (countx > 1) //启用的路数大于1台,则使用外部TDK电源
  350. {
  351. DCPower = new CommonVisaResource();
  352. try
  353. {
  354. DCPower.Open(currFileNode.Tps.DCPowerAddress);
  355. }
  356. catch (Exception ex)
  357. {
  358. MessageBox.Show("打开TDK电源失败,请检查电源是否上电!");
  359. return;
  360. }
  361. DCPower.Write("INSTrument:NSELect 6\n"); DCPower.Query("*OPC?\n");
  362. DCPower.Write("VOLTage:PROTection:LEVel 34 V\n"); DCPower.Query("*OPC?\n");//设置过压保护
  363. DCPower.Write("VOLT:PROT:LOW 20 V\n"); DCPower.Query("*OPC?\n");//设置欠压保护
  364. DCPower.Write("VOLT:PROT:LOW:STAT UVP\n"); DCPower.Query("*OPC?\n");//启动欠压保护
  365. DCPower.Write($"VOLTage {volt} V\n"); DCPower.Query("*OPC?\n");//设置电压
  366. DCPower.Write($"CURRent {Current} A\n"); DCPower.Query("*OPC?\n");//设置电流
  367. DCPower.Write("GLOBal:OUTPut:STATe 1\n"); DCPower.Query("*OPC?\n");
  368. ShowMessage(MsgType.Info, string.Format("打开外部TDK电源."));
  369. }
  370. else //如何测试产品为1台,则使用内部蚯蚓电源
  371. {
  372. rainwormPower.powerSetting(volt, Current);//设置输出电压和限制电流
  373. rainwormPower.powerOnoff(RainwormPower.State.ON);//设置蚯蚓电源输出
  374. Thread.Sleep(100);
  375. if (rainwormPower.UnlockKey())
  376. {
  377. ShowMessage(MsgType.Info, string.Format("打开内部蚯蚓电源."));
  378. }
  379. else
  380. {
  381. MessageBox.Show("打开内部蚯蚓电源失败,请检查电源是否上电!");
  382. ShowMessage(MsgType.Error, string.Format("连接内部蚯蚓电源失败!"));
  383. }
  384. }
  385. TransmitterSerialPort SerialClient = new TransmitterSerialPort();
  386. int ControlDelay = currFileNode.Tps.TestNodes[0].Parameters.GetParameter<int>("控制延时");
  387. currFileNode.Tps.ControlDelay = ControlDelay;
  388. task = new Task(new Action(() => {
  389. //执行测试过程
  390. //currFileNode.Tps.Start(isOrderByChannel);
  391. SerialClient.SerialOpen(CfigComParas.GetComPort("1"));//获取config下的串口控制中的串口号,用于切频点的串口
  392. ShowMessage(MsgType.Info, string.Format("打开串口."));
  393. currFileNode.Tps.SerialClient = SerialClient;
  394. #region 根据频点来测指标
  395. //for (int point = 0; point < FrequencyNumber; point++)//频点总数
  396. //{
  397. //FourthByte = Convert.ToByte(CfigComParas.GetFourthByte((point + 1).ToString()), 16);
  398. //currFileNode.Tps.FourthByte = FourthByte;
  399. //double CenterFreq = double.Parse(CfigComParas.Getfreqpoint((point + 1).ToString()));
  400. //for (int i = 0; i < currFileNode.Tps.TestNodes.Count; i++)
  401. //{
  402. // currFileNode.Tps.TestNodes[i].PointIndex = point;
  403. // currFileNode.Tps.TestNodes[i].CenterFreq = CenterFreq;
  404. // //currFileNode.Tps.TestNodes[i].PointTotal = currFileNode.Tps.TestNodes.Count;
  405. // currFileNode.Tps.TestNodes[i].PointTotal = FrequencyNumber;
  406. //}
  407. //if (IsRuning == false)
  408. //{
  409. // //控制断电
  410. // if (currFileNode.Tps.Product.Contains("YZH16A"))//针对YZH16A的产品
  411. // {
  412. // SerialClient.DUT_Transmitter_Ctrol(0x00, 0x00, 0x55);
  413. // }
  414. // else
  415. // {
  416. // SerialClient.DUT_Transmitter_Ctrol1(0x00, 0x00);
  417. // }
  418. // SerialClient.SerialClose();
  419. // DCPower.Write("GLOBal:OUTPut:STATe 0\n"); DCPower.Query("*OPC?\n");
  420. // ShowMessage(MsgType.Info, string.Format("关闭6YYC信号源,关闭串口,关闭TDK电源."));
  421. // return;
  422. //}
  423. ////控制
  424. //if (currFileNode.Tps.Product.Contains("YZH16A"))//针对YZH16A的产品
  425. //{
  426. // SerialClient.DUT_Transmitter_Ctrol(ThridByte, FourthByte, 0x55);
  427. //}
  428. //else
  429. //{
  430. // SerialClient.DUT_Transmitter_Ctrol1(ThridByte, FourthByte);
  431. //}
  432. //ShowMessage(MsgType.Info, string.Format("发送串口指令EB 90 {0} {1}.", ThridByte, FourthByte));
  433. //testMin = TimesCounter.ElapsedMilliseconds/1000f / 60f;
  434. //Thread.Sleep(ControlDelay);//单位ms
  435. //ShowMessage(MsgType.Info, string.Format("延时{0}ms.", ControlDelay));
  436. //this.BeginInvoke(new Action(() =>
  437. //{
  438. // labRemainTime.Text = $"第{point + 1}个频点[{CenterFreq}]MHz测试,共{FrequencyNumber}个频点,耗时{Math.Round(testMin, 2)}Min。";
  439. //}));
  440. //}
  441. tbSerials.Items.CopyTo(ProductSeries, 0);
  442. currFileNode.Tps.Start(isOrderByChannel, ProductSeries, DCPower, rainwormPower, SerialClient, CfigComParas);
  443. TimesCounter.Stop();
  444. testMin = TimesCounter.ElapsedMilliseconds / 1000f / 60f;
  445. this.BeginInvoke(new Action(() => {
  446. labRemainTime.Text = $"测试结束,共计耗时{Math.Round(testMin,2)}分钟";
  447. }));
  448. #endregion
  449. //控制断电
  450. if (currFileNode.Tps.Product.Contains("YZH16A"))//针对YZH16A的产品
  451. {
  452. SerialClient.DUT_Transmitter_Ctrol(0x00, 0x00, 0x55);//todo:掉电控制
  453. }
  454. else
  455. {
  456. SerialClient.DUT_Transmitter_Ctrol1(0x00, 0x00);//todo:掉电控制
  457. }
  458. SerialClient.SerialClose();//需要取消注释
  459. //if (tbSerials.Items.Count > 1)//如果测试台数大于1,则使用外部TDK电源
  460. if (countx > 1)//如果启用路数大于1,则使用外部TDK电源
  461. {
  462. DCPower.Write("GLOBal:OUTPut:STATe 0\n"); DCPower.Query("*OPC?\n");//需要取消注释
  463. ShowMessage(MsgType.Info, string.Format("关闭6YYC信号源,关闭串口,关闭外部TDK电源."));
  464. }
  465. else
  466. {
  467. rainwormPower.powerOnoff(RainwormPower.State.OFF);//设置蚯蚓电源输出
  468. ShowMessage(MsgType.Info, string.Format("关闭6YYC信号源,关闭串口,关闭内部蚯蚓电源."));
  469. }
  470. this.Invoke(new Action(() => {
  471. BtnLoadTpsConfig.Enabled = true;
  472. BtnStart.Enabled = true;
  473. tbSerials.Enabled = true;
  474. }));
  475. }));
  476. task.Start();
  477. }
  478. public void DataTableFlush(DataTable data)
  479. {
  480. this.Invoke(new Action(() => {
  481. dgvTestData.DataSource = null;
  482. dgvTestData.DataSource = data;
  483. }));
  484. }
  485. private void BtnStop_Click(object sender, EventArgs e)
  486. {
  487. MainTps tps = new MainTps();
  488. CommonVisaResource DCPower = new CommonVisaResource();
  489. //获取仪器
  490. //var DC = tps.GetDevice("程控电源");
  491. currFileNode.Tps.Stop();
  492. IsRuning = false;
  493. //if(task != null && task.IsCompleted == false)
  494. //{
  495. // //等待任务退出
  496. // task.Wait();
  497. //}
  498. BtnStart.Enabled = true;
  499. BtnLoadTpsConfig.Enabled = true;
  500. labRemainTime.Text = $"已停止测试。";
  501. //DC.Write("关闭电源");
  502. for (int i = 0; i < 8; i++)
  503. {
  504. dio.SupplySwitch(i+1,DioControlClass.State.OFF);//关闭8路供电开关
  505. }
  506. }
  507. private void BtnDevice_Click(object sender, EventArgs e)
  508. {
  509. if (currFileNode != null)
  510. {
  511. //点开按钮前检查仪器状态,并根据状态来显示不同背景颜色
  512. currFileNode.Tps.CheckDevices();
  513. }
  514. frmDevice.ShowDialog();
  515. }
  516. private void BtnMessage_Click(object sender, EventArgs e)
  517. {
  518. frmMsg.ShowDialog();
  519. errorCount = 0;
  520. BtnMessage.ForeColor = Color.Black;
  521. BtnMessage.Text = "消息";
  522. }
  523. private void advTree1_NodeDoubleClick(object sender, DevComponents.AdvTree.TreeNodeMouseEventArgs e)
  524. {
  525. //打开注释,则启用各测试指标的各自界面,否则只用公共界面
  526. //if(e.Node.Tag != null)
  527. //{
  528. // TestNode node = (TestNode)e.Node.Tag;
  529. // //没有界面的就只清除显示
  530. // if (node.TestModel.UC == null)
  531. // panel1.Controls.Clear();
  532. // //相同的界面不做处理
  533. // if (panel1.Controls.Count > 0 && panel1.Controls[0] == node.TestModel.UC)
  534. // return;
  535. // panel1.Controls.Clear();
  536. // panel1.Controls.Add(node.TestModel.UC);
  537. // node.TestModel.UC.Dock = DockStyle.Fill;
  538. // node.TestModel.UC.Visible = true;
  539. //}
  540. }
  541. //树形控件按通道分类
  542. private void tsmChannel_Click(object sender, EventArgs e)
  543. {
  544. displayTree(true);
  545. }
  546. //树形控件按指标分类
  547. private void tmsTestNode_Click(object sender, EventArgs e)
  548. {
  549. displayTree(false);
  550. }
  551. bool treeCanSelect = true;
  552. private void tsmSelectAll_Click(object sender, EventArgs e)
  553. {
  554. checkAllNode(true);
  555. }
  556. private void tsmUnSelecteAll_Click(object sender, EventArgs e)
  557. {
  558. checkAllNode(false);
  559. }
  560. //选或者反选所有节点
  561. void checkAllNode(bool isChecked)
  562. {
  563. treeCanSelect = false;
  564. foreach (DevComponents.AdvTree.Node node in advTree1.Nodes)
  565. {
  566. node.Checked = isChecked;
  567. if (node.Nodes.Count > 0)
  568. {
  569. foreach (DevComponents.AdvTree.Node child in node.Nodes)
  570. {
  571. child.Checked = isChecked;
  572. }
  573. }
  574. }
  575. treeCanSelect = true;
  576. }
  577. /// <summary>
  578. /// 显示测试指标的树结构
  579. /// </summary>
  580. /// <param name="isOrderByChannel">是否按通道来显示</param>
  581. void displayTree(bool isOrderByChannel)
  582. {
  583. this.isOrderByChannel = isOrderByChannel;
  584. advTree1.BeginUpdate();
  585. treeCanSelect = false;
  586. advTree1.Nodes.Clear();
  587. IEnumerable<string> parents = null;
  588. if(isOrderByChannel)
  589. {
  590. parents = (from x in currFileNode.Tps.TestNodes select x.Channel).ToList().Distinct();
  591. }
  592. else
  593. {
  594. parents = (from x in currFileNode.Tps.TestNodes select x.Name).ToList().Distinct();
  595. }
  596. if (parents == null)
  597. return;
  598. List<TestNode> childs = null;
  599. foreach (string chName in parents)
  600. {
  601. DevComponents.AdvTree.Node parent = new DevComponents.AdvTree.Node();
  602. parent.Text = chName;
  603. parent.CheckBoxVisible = true;
  604. parent.Tag = null;
  605. if (isOrderByChannel)
  606. {
  607. childs = currFileNode.Tps.TestNodes.Where(x => x.Channel == chName).ToList();
  608. }
  609. else
  610. {
  611. childs = currFileNode.Tps.TestNodes.Where(x => x.Name == chName).ToList();
  612. }
  613. foreach (TestNode node in childs)
  614. {
  615. DevComponents.AdvTree.Node treeNode = new DevComponents.AdvTree.Node();
  616. if (isOrderByChannel)
  617. {
  618. treeNode.Text = node.Name;
  619. }
  620. else
  621. {
  622. treeNode.Text = node.Channel;
  623. }
  624. treeNode.CheckBoxVisible = true;
  625. if (node.IsSelected)
  626. {
  627. treeNode.Checked = true;
  628. parent.Checked = true;
  629. }
  630. treeNode.Tag = node;
  631. parent.Nodes.Add(treeNode);
  632. }
  633. advTree1.Nodes.Add(parent);
  634. }
  635. //如果父节点只有一个,即只有一个通道,则不显示通道
  636. if(advTree1.Nodes.Count == 1)
  637. {
  638. DevComponents.AdvTree.Node root = advTree1.Nodes[0];
  639. advTree1.Nodes.Clear();
  640. if (root.Nodes.Count == 1)
  641. {
  642. advTree1.Nodes.Add(root.Nodes[0]);
  643. }
  644. else
  645. {
  646. foreach (DevComponents.AdvTree.Node item in root.Nodes)
  647. {
  648. advTree1.Nodes.Add(item);
  649. }
  650. }
  651. }
  652. treeCanSelect = true;
  653. advTree1.EndUpdate();
  654. advTree1.Refresh();
  655. advTree1.ExpandAll();
  656. }
  657. /// <summary>
  658. /// 父节点全选或者反选,子节点采用一致的动作
  659. /// </summary>
  660. /// <param name="sender"></param>
  661. /// <param name="e"></param>
  662. private void advTree1_AfterCheck(object sender, DevComponents.AdvTree.AdvTreeCellEventArgs e)
  663. {
  664. if(treeCanSelect && e.Cell.Parent.Tag == null)
  665. {
  666. //表明选到了顶层,把子节点的选中状态和当前一致
  667. foreach (DevComponents.AdvTree.Node node in e.Cell.Parent.Nodes)
  668. {
  669. node.Checked = e.Cell.Checked;
  670. }
  671. }
  672. else if(e.Cell.Parent.Tag != null)
  673. {
  674. TestNode node = (TestNode)e.Cell.Parent.Tag;
  675. node.IsSelected = e.Cell.Checked;
  676. }
  677. }
  678. public class ConfigParameter
  679. {
  680. /// <summary>
  681. /// 设置过压保护
  682. /// </summary>
  683. public string SetOverVoltProtect { set; get; }
  684. /// <summary>
  685. /// 设置电压
  686. /// </summary>
  687. public double SetVolt { set; get; }
  688. /// <summary>
  689. /// 设置电流
  690. /// </summary>
  691. public double SetCurrent { set; get; }
  692. }
  693. private void dgvTestData_CellContentClick(object sender, DataGridViewCellEventArgs e)
  694. {
  695. }
  696. private void tbTester_TextChanged(object sender, EventArgs e)
  697. {
  698. }
  699. /// <summary>
  700. /// 打开调试界面事件处理
  701. /// </summary>
  702. /// <param name="sender"></param>
  703. /// <param name="e"></param>
  704. private void btnDebugging_Click(object sender, EventArgs e)
  705. {
  706. DebuggingForm debuggingForm = new DebuggingForm();
  707. debuggingForm.Show();
  708. }
  709. private void btnProductForm_Click(object sender, EventArgs e)
  710. {
  711. ProductForm productForm = new ProductForm();
  712. productForm.Show();
  713. }
  714. /// <summary>
  715. /// 产品编号框添加、插入、删除事件处理
  716. /// </summary>
  717. /// <param name="sender"></param>
  718. /// <param name="e"></param>
  719. private void tbSerials_KeyDown(object sender, KeyEventArgs e)
  720. {
  721. if (e.KeyCode == Keys.Enter)
  722. {
  723. if (tbSerials.Text != "")
  724. {
  725. string newItem = tbSerials.Text.Trim();
  726. bool flag = false;
  727. for (int i = 0; i < tbSerials.Items.Count; i++)
  728. {
  729. if (string.Compare(newItem, tbSerials.Items[i].ToString()) == 0)
  730. {
  731. flag = true;
  732. this.BackColor = Color.Blue;
  733. MessageBox.Show("已经有相同项,添加无效");
  734. }
  735. }
  736. if (flag == false)
  737. {
  738. if (tbSerials.Items.Count < 8)
  739. {
  740. tbSerials.Items.Add(newItem);
  741. tbSerials.Text = "";
  742. }
  743. else
  744. {
  745. MessageBox.Show("已有8套产品的编号,添加无效");
  746. }
  747. }
  748. }
  749. else
  750. {
  751. MessageBox.Show("未输入产品编号,添加无效");
  752. }
  753. }
  754. if(e.KeyCode == Keys.Insert)
  755. {
  756. if (tbSerials.Items.Count != 0)
  757. {
  758. if (tbSerials.SelectedItem != null)
  759. {
  760. string strItem = tbSerials.SelectedItem.ToString();
  761. if (tbSerials.Text != "")
  762. {
  763. string newItem = tbSerials.Text.Trim();
  764. bool flag = false;
  765. for (int i = 0; i < tbSerials.Items.Count; i++)
  766. {
  767. if (string.Compare(newItem, tbSerials.Items[i].ToString()) == 0)
  768. {
  769. flag = true;
  770. this.BackColor = Color.Blue;
  771. MessageBox.Show("已经有相同项,插入无效");
  772. }
  773. }
  774. if (flag == false)
  775. {
  776. if (tbSerials.Items.Count < 8)
  777. {
  778. tbSerials.Items.Insert(tbSerials.Items.IndexOf(strItem), newItem); ;
  779. tbSerials.Text = "";
  780. }
  781. else
  782. {
  783. MessageBox.Show("已有8套产品的编号,插入无效");
  784. }
  785. }
  786. }
  787. else
  788. {
  789. MessageBox.Show("未输入产品编号,插入无效");
  790. }
  791. }
  792. else
  793. {
  794. MessageBox.Show("未选中需要插入的位置,插入无效");
  795. }
  796. }
  797. else
  798. {
  799. MessageBox.Show("产品编号为空,插入无效");
  800. }
  801. }
  802. if(e.KeyCode == Keys.Delete)
  803. {
  804. if (tbSerials.Items.Count != 0)
  805. {
  806. if (tbSerials.SelectedItem != null)
  807. {
  808. string strItem = tbSerials.SelectedItem.ToString();
  809. DialogResult selsecltresult;
  810. selsecltresult = MessageBox.Show("是否删除当前编号?", "提示", MessageBoxButtons.OKCancel);
  811. if (selsecltresult == DialogResult.OK)
  812. {
  813. tbSerials.Items.Remove(strItem);
  814. if (tbSerials.Items.Count != 0 )
  815. {
  816. tbSerials.SelectedItem = tbSerials.Items[0];
  817. }
  818. }
  819. }
  820. else
  821. {
  822. MessageBox.Show("未选中需要删除的产品编号,删除无效");
  823. }
  824. }
  825. else
  826. {
  827. MessageBox.Show("产品编号为空,删除无效");
  828. }
  829. }
  830. if(e.KeyCode == Keys.Escape)
  831. {
  832. if (tbSerials.Items.Count != 0)
  833. {
  834. DialogResult selsecltresult;
  835. selsecltresult = MessageBox.Show("是否删除全部编号?", "提示", MessageBoxButtons.OKCancel);
  836. if (selsecltresult == DialogResult.OK)
  837. {
  838. tbSerials.Items.Clear();
  839. }
  840. }
  841. else
  842. {
  843. MessageBox.Show("产品编号为空,删除无效");
  844. }
  845. }
  846. ProductSeries = new string[tbSerials.Items.Count];//将产品编号载入ProductSeries字符串数组
  847. if (ProductSeries.Length>0)
  848. {
  849. tbSerials.Items.CopyTo(ProductSeries, 0);
  850. for (int i = 0; i < ProductSeries.Length; i++)
  851. {
  852. ProdEnabCh[i]= true;
  853. }
  854. }
  855. }
  856. }
  857. }