FrmMain.cs 35 KB

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