From 8f6f7a561aa3aff4a18af00d73fea5cc879b09ad Mon Sep 17 00:00:00 2001 From: "wilmin.zheng" Date: Fri, 8 May 2026 13:05:33 +0800 Subject: [PATCH] RenzYi Modified --- .../精工涂胶检测项目/Form/FormMain.cs | 1398 +++++++++-------- 1 file changed, 754 insertions(+), 644 deletions(-) diff --git a/精工涂胶检测项目/精工涂胶检测项目/Form/FormMain.cs b/精工涂胶检测项目/精工涂胶检测项目/Form/FormMain.cs index a8d53fa..63dea28 100644 --- a/精工涂胶检测项目/精工涂胶检测项目/Form/FormMain.cs +++ b/精工涂胶检测项目/精工涂胶检测项目/Form/FormMain.cs @@ -113,7 +113,7 @@ namespace 精工涂胶检测项目 private MyMessageFilter messageFilter; public ModbusTcpClient modbusClient; // 新增Modbus客户端实例 private Timer modbusPollTimer; // 新增轮询定时器 - private Timer modbusHeartbeat; + private System.Timers.Timer modbusHeartbeat; public Plc plc; @@ -221,20 +221,32 @@ namespace 精工涂胶检测项目 { if (modbusHeartbeat == null) { - modbusHeartbeat = new Timer { Interval = 500 }; - modbusHeartbeat.Tick += async (s, e) => + modbusHeartbeat = new System.Timers.Timer { Interval = 500 }; + modbusHeartbeat.AutoReset = true; + modbusHeartbeat.SynchronizingObject = null; + modbusHeartbeat.Elapsed += async (s, e) => { - modbusClient.WriteSingleRegister("0", 1); - Thread.Sleep(50); + try + { + modbusClient.WriteSingleRegister("0", 1); + await Task.Delay(50); + } + catch (Exception ex) + { + AddLog($"Modbus心跳失败:{ex.Message}", 2); + } }; + modbusHeartbeat.Start(); } } private void StopHeartBeat() { - if (modbusHeartbeat == null && modbusHeartbeat.Enabled) + if (modbusHeartbeat != null) { modbusHeartbeat.Stop(); + modbusHeartbeat.Dispose(); + modbusHeartbeat = null; } } @@ -599,380 +611,461 @@ namespace 精工涂胶检测项目 } private void MyJobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e) { - ///待会儿需要改,通过ToolBlock是否报错来判断。 - //结果处理代码 - ICogRecord tmpVolum1; - ICogRecord tmpArea1; - ICogRecord tmpVolum2; - ICogRecord tmpArea2; - ICogRecord tmpVolum3; - ICogRecord tmpArea3; - ICogRecord DJJC; - ICogRecord QX; - ICogRecord DW; - ICogRecord TOP1; - ICogRecord TOP2; - ICogRecord TOP3; - ICogRecord TOP4; - ICogRecord TOP5; - ICogRecord TOP6; - ICogRecord TOPMJ; - ICogRecord BOTMJ; - ICogRecord BOT1; - ICogRecord BOT2; - ICogRecord BOT3; - ICogRecord BOT4; - ICogRecord BOT5; - ICogRecord BOT6; - ICogRecord BDKTJ; - ICogRecord TJ; - ICogRecord topRecord = myJobManager.UserResult(); - string s = ProductSN.Substring(2); - ///create graphic wrapper for the range image with its grey overlay - Cog3DRangeImageGraphic rImgG = myToolBlock1.Outputs["CombinedImage"].Value as Cog3DRangeImageGraphic; - cog3DDisplayV2WF1.Add(rImgG); - string rootPath = @"D:\Date"; - string currentDateFolder = Form1.CreateCurrentDateFolder(rootPath); - AddLog("当前文件夹已创建", 1); - //图像显示需要改 - // ICogRecord ShowImageRecord = topRecord.SubRecords["ShowLastRunRecordForUserQueue"].SubRecords["LastRun"].SubRecords["Image Source.OutputImage"]; - if (TXZH == "1") - { - ICogRecord ShowImageRecord = topRecord.SubRecords["ShowLastRunRecordForUserQueue"].SubRecords["LastRun"].SubRecords["Image Source.OutputImage"]; - cogRecordDisplay1.Record = ShowImageRecord; - cogRecordDisplay1.AutoFit = true; - } - if (button1State) - { - ICogRecord ShowImageRecord = topRecord.SubRecords["ShowLastRunRecordForUserQueue"].SubRecords["LastRun"].SubRecords["Image Source.OutputImage"]; - cogRecordDisplay1.Record = ShowImageRecord; - cogRecordDisplay1.AutoFit = true; - } - if (TXZH == "0") - { - ICogRecord ShowImageRecord = topRecord.SubRecords["ShowLastRunRecordForUserQueue"].SubRecords["LastRun"].SubRecords["CogToolBlock1.CogFixtureTool1.OutputImage"]; - cogRecordDisplay1.Record = ShowImageRecord; - - label2.Invoke(new Action(() => label2.Text = s)); - Image img = cogRecordDisplay1.CreateContentBitmap(Cognex.VisionPro.Display.CogDisplayContentBitmapConstants.Display); - string imageName = s +DateTime.Now.ToString("ss")+ ".Png"; - imageSavePath = Path.Combine(currentDateFolder,imageName); - img.Save(imageSavePath, ImageFormat.Png); - cogRecordDisplay1.AutoFit = true; - } - - tmpVolum1 = topRecord.SubRecords["Volum1"]; - tmpArea1 = topRecord.SubRecords["Area1"]; - tmpVolum2 = topRecord.SubRecords["Volum2"]; - tmpArea2 = topRecord.SubRecords["Area2"]; - tmpVolum3 = topRecord.SubRecords["Volum3"]; - tmpArea3 = topRecord.SubRecords["Area3"]; - DJJC = topRecord.SubRecords["DJ_result"]; - QX = topRecord.SubRecords["QX_result"]; - DW = topRecord.SubRecords["DW_PM"]; - TOP1 = topRecord.SubRecords["TOP1"]; - TOP2 = topRecord.SubRecords["TOP2"]; - TOP3 = topRecord.SubRecords["TOP3"]; - TOP4 = topRecord.SubRecords["TOP4"]; - TOP5 = topRecord.SubRecords["TOP5"]; - TOP6 = topRecord.SubRecords["TOP6"]; - BOT1 = topRecord.SubRecords["BOT1"]; - BOT2 = topRecord.SubRecords["BOT2"]; - BOT3 = topRecord.SubRecords["BOT3"]; - BOT4 = topRecord.SubRecords["BOT1"]; - BOT5 = topRecord.SubRecords["BOT2"]; - BOT6= topRecord.SubRecords["BOT3"]; - TJ = topRecord.SubRecords["TJ"]; - //label11.Text = tmpVolum.ToString(); - //label12.Text = tmpArea.ToString();// - - string volumStatus; - string areaStatus; - string volumStatus1; - string areaStatus1; - string volumStatus2; - string areaStatus2; - string volumStatus3; - string areaStatus3; - string resultStatus; - int modbusResult; - double dj = 1.0; - double qx = 1.0; - double dw = 1.0; - double volum1 = (double)tmpVolum1.Content; - double area1 = (double)tmpArea1.Content; - double volum2 = (double)tmpVolum2.Content; - double area2 = (double)tmpArea2.Content; - double volum3 = (double)tmpVolum3.Content; - double area3 = (double)tmpArea3.Content; - double top1 = (double)TOP1.Content; - double top2 = (double)TOP2.Content; - double top3 = (double)TOP3.Content; - double top4 = (double)TOP1.Content; - double top5 = (double)TOP2.Content; - double top6 = (double)TOP3.Content; - double topmj = (double)TOP2.Content*(double)TOP3.Content; - double bot1 = (double)BOT1.Content; - double bot2 = (double)BOT2.Content; - double bot3 = (double)BOT3.Content; - double bot4 = (double)BOT4.Content; - double bot5 = (double)BOT5.Content; - double bot6 = (double)BOT6.Content; - double botmj = (double)BOT1.Content * (double)BOT3.Content; - //算体积 - double bdktj = (double)TJ.Content-3800;// 0.333333 *20*(topmj+botmj+3200); - //string dj = DJCJ.ToString(); - if (checkBox3.Checked) - { - dj = (double)DJJC.Content; - } - if (checkBox4.Checked) - { - qx = (double)QX.Content; - } - dw = (double)DW.Content; - //定义12个阈值参数 - string CSV1max; - string CSV1min; - string CSA1max; - string CSA1min; - string CSV2max; - string CSV2min; - string CSA2max; - string CSA2min; - string CSV3max; - string CSV3min; - string CSA3max; - string CSA3min; - string BdYZ; - //这里要分别判断,程序不报错时 - //使用阈值 - try - {string used=label81.Text; - string usePath= Path.Combine(@"D:\", $"{used}.txt"); - if (File.Exists(usePath)) + { + ///待会儿需要改,通过ToolBlock是否报错来判断。 + //结果处理代码 + ICogRecord tmpVolum1; + ICogRecord tmpArea1; + ICogRecord tmpVolum2; + ICogRecord tmpArea2; + ICogRecord tmpVolum3; + ICogRecord tmpArea3; + ICogRecord DJJC; + ICogRecord QX; + ICogRecord DW; + ICogRecord TOP1; + ICogRecord TOP2; + ICogRecord TOP3; + ICogRecord TOP4; + ICogRecord TOP5; + ICogRecord TOP6; + ICogRecord TOPMJ; + ICogRecord BOTMJ; + ICogRecord BOT1; + ICogRecord BOT2; + ICogRecord BOT3; + ICogRecord BOT4; + ICogRecord BOT5; + ICogRecord BOT6; + ICogRecord BDKTJ; + ICogRecord TJ; + ICogRecord topRecord; + + string currentDateFolder; + try { - string[] lines = File.ReadAllLines(usePath); // 读取所有行[^1] - CSV1max = lines[0]; // 第一行数据 - CSV1min = lines[1]; // 第二行数据 - CSA1max = lines[2]; // 第三行数据 - CSA1min = lines[3]; // - CSV2max = lines[4]; // - CSV2min = lines[5]; // - CSA2max = lines[6]; // - CSA2min = lines[7]; // - CSV3max = lines[8]; - CSV3min = lines[9]; - CSA3max = lines[10]; - CSA3min = lines[11]; - BdYZ = lines[12]; - if (checkBox2.Checked) + topRecord = myJobManager.UserResult(); + + } + catch (Exception ex) + { + AddLog($"获取用户结果失败:{ex.Message}", 1); + return; + } + try + { + string s = ProductSN.Substring(2); + } + catch (Exception ex) + { + AddLog($"获取产品SN失败:{ex.Message}", 1); + return; + } + try + { + Cog3DRangeImageGraphic rImgG = myToolBlock1.Outputs["CombinedImage"].Value as Cog3DRangeImageGraphic; + } + catch (Exception ex) + { + AddLog($"获取图像失败:{ex.Message}", 1); + return; + } + string rootPath = @"D:\Date"; + try + { + currentDateFolder = Form1.CreateCurrentDateFolder(rootPath); + } + catch (Exception ex) + { + AddLog($"创建当前文件夹失败:{ex.Message}", 1); + return; + } + AddLog("当前文件夹已创建", 1); + //图像显示需要改 + // ICogRecord ShowImageRecord = topRecord.SubRecords["ShowLastRunRecordForUserQueue"].SubRecords["LastRun"].SubRecords["Image Source.OutputImage"]; + if (TXZH == "1") + { + ICogRecord ShowImageRecord = topRecord.SubRecords["ShowLastRunRecordForUserQueue"].SubRecords["LastRun"].SubRecords["Image Source.OutputImage"]; + cogRecordDisplay1.Record = ShowImageRecord; + cogRecordDisplay1.AutoFit = true; + } + if (button1State) + { + ICogRecord ShowImageRecord = topRecord.SubRecords["ShowLastRunRecordForUserQueue"].SubRecords["LastRun"].SubRecords["Image Source.OutputImage"]; + cogRecordDisplay1.Record = ShowImageRecord; + cogRecordDisplay1.AutoFit = true; + } + if (TXZH == "0") + { + ICogRecord ShowImageRecord = topRecord.SubRecords["ShowLastRunRecordForUserQueue"].SubRecords["LastRun"].SubRecords["CogToolBlock1.CogFixtureTool1.OutputImage"]; + cogRecordDisplay1.Record = ShowImageRecord; + + label2.Invoke(new Action(() => label2.Text = s)); + Image img = cogRecordDisplay1.CreateContentBitmap(Cognex.VisionPro.Display.CogDisplayContentBitmapConstants.Display); + string imageName = s + DateTime.Now.ToString("ss") + ".Png"; + imageSavePath = Path.Combine(currentDateFolder, imageName); + try { - csv1max = Convert.ToInt32(CSV1max); - csv1min = Convert.ToInt32(CSV1min); - csa1max = Convert.ToInt32(CSA1max); - csa1min = Convert.ToInt32(CSA1min); - csv2max = Convert.ToInt32(CSV2max); - csv2min = Convert.ToInt32(CSV2min); - csa2max = Convert.ToInt32(CSA2max); - csa2min = Convert.ToInt32(CSA2min); - csv3max = Convert.ToInt32(CSV3max); - csv3min = Convert.ToInt32(CSV3min); - csa3max = Convert.ToInt32(CSA3max); - csa3min = Convert.ToInt32(CSA3min); - bdyz= Convert.ToInt32(BdYZ); - TOPMJMin = 1600 * ((100 - bdyz) * 0.01); - TOPMJMax = 1600 * ((100 + bdyz) * 0.01); - BOTMJMin = 6400 * ((100 - bdyz) * 0.01); - BOTMJMax = 6400 * ((100 + bdyz) * 0.01); - TOP1Min = 40 * ((100 - bdyz) * 0.01); - TOP1Max = 40 * ((100+ bdyz) * 0.01); - TOP2Min = 40 * ((100 - bdyz) * 0.01); - TOP2Max = 40 * ((100 + bdyz) * 0.01); - TOP3Min = 40 * ((100 - bdyz) * 0.01); - TOP3Max = 40 * ((100 + bdyz) * 0.01); - TOP4Min = 40 * ((100 - bdyz) * 0.01); - TOP4Max = 40 * ((100 + bdyz) * 0.01); - TOP5Min = 40 * ((100 - bdyz) * 0.01); - TOP5Max = 40 * ((100 + bdyz) * 0.01); - TOP6Min = 40 * ((100 - bdyz) * 0.01); - TOP6Max = 40 * ((100 + bdyz) * 0.01); - BOT1Min = 80 * ((100 - bdyz) * 0.01); - BOT1Max = 80 * ((100 + bdyz) * 0.01); - BOT2Min = 80 * ((100 - bdyz) * 0.01); - BOT2Max = 80 * ((100 + bdyz) * 0.01); - BOT3Min = 80 * ((100 - bdyz) * 0.01); - BOT3Max = 80 * ((100 + bdyz) * 0.01); - BOT4Min = 80 * ((100 - bdyz) * 0.01); - BOT4Max = 80 * ((100 + bdyz) * 0.01); - BOT5Min = 80 * ((100 - bdyz) * 0.01); - BOT5Max = 80 * ((100 + bdyz) * 0.01); - BOT6Min = 80 * ((100 - bdyz) * 0.01); - BOT6Max = 80 * ((100 + bdyz) * 0.01); - BDKTJMin =74000; - BDKTJMax = 75000; + img.Save(imageSavePath, ImageFormat.Png); } - if (checkBox5.Checked) + catch (Exception ex) { - - label64.Text = BDKTJMin.ToString() + "~" + BDKTJMax.ToString() + "mm³"; + AddLog($"保存图像失败:{ex.Message}", 1); + return; } - if (checkBox2.Checked== false) + cogRecordDisplay1.AutoFit = true; + } + try + { + tmpVolum1 = topRecord.SubRecords["Volum1"]; + tmpArea1 = topRecord.SubRecords["Area1"]; + tmpVolum2 = topRecord.SubRecords["Volum2"]; + tmpArea2 = topRecord.SubRecords["Area2"]; + tmpVolum3 = topRecord.SubRecords["Volum3"]; + tmpArea3 = topRecord.SubRecords["Area3"]; + DJJC = topRecord.SubRecords["DJ_result"]; + QX = topRecord.SubRecords["QX_result"]; + DW = topRecord.SubRecords["DW_PM"]; + TOP1 = topRecord.SubRecords["TOP1"]; + TOP2 = topRecord.SubRecords["TOP2"]; + TOP3 = topRecord.SubRecords["TOP3"]; + TOP4 = topRecord.SubRecords["TOP4"]; + TOP5 = topRecord.SubRecords["TOP5"]; + TOP6 = topRecord.SubRecords["TOP6"]; + BOT1 = topRecord.SubRecords["BOT1"]; + BOT2 = topRecord.SubRecords["BOT2"]; + BOT3 = topRecord.SubRecords["BOT3"]; + BOT4 = topRecord.SubRecords["BOT1"]; + BOT5 = topRecord.SubRecords["BOT2"]; + BOT6 = topRecord.SubRecords["BOT3"]; + TJ = topRecord.SubRecords["TJ"]; + } + catch (Exception ex) + { + AddLog($"获取数据失败:{ex.Message}", 1); + return; + } + + string volumStatus; + string areaStatus; + string volumStatus1; + string areaStatus1; + string volumStatus2; + string areaStatus2; + string volumStatus3; + string areaStatus3; + string resultStatus; + int modbusResult; + double dj = 1.0; + double qx = 1.0; + double dw = 1.0; + + double volum1; + double area1; + double volum2; + double area2; + double volum3; + double area3; + double top1; + double top2; + double top3; + double top4; + double top5; + double top6; + double topmj; + double bot1; + double bot2; + double bot3; + double bot4; + double bot5; + double bot6; + double botmj; + double bdktj; + try + { + volum1 = (double)tmpVolum1.Content; + area1 = (double)tmpArea1.Content; + volum2 = (double)tmpVolum2.Content; + area2 = (double)tmpArea2.Content; + volum3 = (double)tmpVolum3.Content; + area3 = (double)tmpArea3.Content; + top1 = (double)TOP1.Content; + top2 = (double)TOP2.Content; + top3 = (double)TOP3.Content; + top4 = (double)TOP1.Content; + top5 = (double)TOP2.Content; + top6 = (double)TOP3.Content; + topmj = (double)TOP2.Content * (double)TOP3.Content; + bot1 = (double)BOT1.Content; + bot2 = (double)BOT2.Content; + bot3 = (double)BOT3.Content; + bot4 = (double)BOT4.Content; + bot5 = (double)BOT5.Content; + bot6 = (double)BOT6.Content; + botmj = (double)BOT1.Content * (double)BOT3.Content; + //算体积 + bdktj = (double)TJ.Content - 3800;// 0.333333 *20*(topmj+botmj+3200); + //string dj = DJCJ.ToString(); + if (checkBox3.Checked) { - csv1max = 999999; - csv1min = 0; //700 - csa1max = 999999; - csa1min = 0; //200 - csv2max = 999999; - csv2min = 0; //100 - csa2max = 999999; - csa2min = 0; //80 - csv3max = 999999; - csv3min = 0; //50 - csa3max = 999999; - csa3min = 0; //20 - bdyz = Convert.ToInt32(BdYZ); - label64.Text = "74000~75000mm³"; + dj = (double)DJJC.Content; } + if (checkBox4.Checked) + { + qx = (double)QX.Content; + } + dw = (double)DW.Content; + } + catch (Exception ex) + { + AddLog($"获取数据失败:{ex.Message}", 1); + return; + } + + //定义12个阈值参数 + string CSV1max; + string CSV1min; + string CSA1max; + string CSA1min; + string CSV2max; + string CSV2min; + string CSA2max; + string CSA2min; + string CSV3max; + string CSV3min; + string CSA3max; + string CSA3min; + string BdYZ; + //这里要分别判断,程序不报错时 + //使用阈值 + + try + { + string used = label81.Text; + string usePath = Path.Combine(@"D:\", $"{used}.txt"); + if (File.Exists(usePath)) + { + string[] lines = File.ReadAllLines(usePath); // 读取所有行[^1] + CSV1max = lines[0]; // 第一行数据 + CSV1min = lines[1]; // 第二行数据 + CSA1max = lines[2]; // 第三行数据 + CSA1min = lines[3]; // + CSV2max = lines[4]; // + CSV2min = lines[5]; // + CSA2max = lines[6]; // + CSA2min = lines[7]; // + CSV3max = lines[8]; + CSV3min = lines[9]; + CSA3max = lines[10]; + CSA3min = lines[11]; + BdYZ = lines[12]; + if (checkBox2.Checked) + { + csv1max = Convert.ToInt32(CSV1max); + csv1min = Convert.ToInt32(CSV1min); + csa1max = Convert.ToInt32(CSA1max); + csa1min = Convert.ToInt32(CSA1min); + csv2max = Convert.ToInt32(CSV2max); + csv2min = Convert.ToInt32(CSV2min); + csa2max = Convert.ToInt32(CSA2max); + csa2min = Convert.ToInt32(CSA2min); + csv3max = Convert.ToInt32(CSV3max); + csv3min = Convert.ToInt32(CSV3min); + csa3max = Convert.ToInt32(CSA3max); + csa3min = Convert.ToInt32(CSA3min); + bdyz = Convert.ToInt32(BdYZ); + TOPMJMin = 1600 * ((100 - bdyz) * 0.01); + TOPMJMax = 1600 * ((100 + bdyz) * 0.01); + BOTMJMin = 6400 * ((100 - bdyz) * 0.01); + BOTMJMax = 6400 * ((100 + bdyz) * 0.01); + TOP1Min = 40 * ((100 - bdyz) * 0.01); + TOP1Max = 40 * ((100 + bdyz) * 0.01); + TOP2Min = 40 * ((100 - bdyz) * 0.01); + TOP2Max = 40 * ((100 + bdyz) * 0.01); + TOP3Min = 40 * ((100 - bdyz) * 0.01); + TOP3Max = 40 * ((100 + bdyz) * 0.01); + TOP4Min = 40 * ((100 - bdyz) * 0.01); + TOP4Max = 40 * ((100 + bdyz) * 0.01); + TOP5Min = 40 * ((100 - bdyz) * 0.01); + TOP5Max = 40 * ((100 + bdyz) * 0.01); + TOP6Min = 40 * ((100 - bdyz) * 0.01); + TOP6Max = 40 * ((100 + bdyz) * 0.01); + BOT1Min = 80 * ((100 - bdyz) * 0.01); + BOT1Max = 80 * ((100 + bdyz) * 0.01); + BOT2Min = 80 * ((100 - bdyz) * 0.01); + BOT2Max = 80 * ((100 + bdyz) * 0.01); + BOT3Min = 80 * ((100 - bdyz) * 0.01); + BOT3Max = 80 * ((100 + bdyz) * 0.01); + BOT4Min = 80 * ((100 - bdyz) * 0.01); + BOT4Max = 80 * ((100 + bdyz) * 0.01); + BOT5Min = 80 * ((100 - bdyz) * 0.01); + BOT5Max = 80 * ((100 + bdyz) * 0.01); + BOT6Min = 80 * ((100 - bdyz) * 0.01); + BOT6Max = 80 * ((100 + bdyz) * 0.01); + BDKTJMin = 74000; + BDKTJMax = 75000; + } + if (checkBox5.Checked) + { + + label64.Text = BDKTJMin.ToString() + "~" + BDKTJMax.ToString() + "mm³"; + } + if (checkBox2.Checked == false) + { + csv1max = 999999; + csv1min = 0; //700 + csa1max = 999999; + csa1min = 0; //200 + csv2max = 999999; + csv2min = 0; //100 + csa2max = 999999; + csa2min = 0; //80 + csv3max = 999999; + csv3min = 0; //50 + csa3max = 999999; + csa3min = 0; //20 + bdyz = Convert.ToInt32(BdYZ); + label64.Text = "74000~75000mm³"; + } + } + else + { + MessageBox.Show("文件不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + catch (Exception ex) + { + + } + if (volum1 > csv1min && volum1 < csv1max && dw < 3 && dj < 2 && dw < 3) + { + volumStatus1 = "OK"; } else { - MessageBox.Show("文件不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + volumStatus1 = "NG"; + } - } - catch (Exception ex) - { - - } - if (volum1 > csv1min && volum1 < csv1max&& dw < 3&& dj < 2 && dw < 3) - { - volumStatus1 = "OK"; - } - else - { - volumStatus1 = "NG"; - - } - if (volum2 > csv2min && volum2 < csv2max) - { - volumStatus2 = "OK"; - } - else - { - volumStatus2 = "NG"; - - } - if (volum3 > csv3min && volum3 < csv3max) - { - volumStatus3 = "OK"; - } - else - { - volumStatus3 = "NG"; - - } - if (area1 > csa1min && area1 < csa1max&& dw < 2&& dj <2) - { - areaStatus1 = "OK"; - } - else - { - areaStatus1 = "NG"; - } - if (area2 > csa2min && area2 < csa2max) - { - areaStatus2 = "OK"; - } - else - { - areaStatus2 = "NG"; - } - if (area3 > csa3min && area3 < csa3max) - { - areaStatus3 = "OK"; - } - else - { - areaStatus3 = "NG"; - } - - if (dw > 1) - { - if (label34.InvokeRequired) + if (volum2 > csv2min && volum2 < csv2max) { - label34.Invoke(new Action(() => label34.Text = "断胶了,请注意检查!!!!")); + volumStatus2 = "OK"; } - resultStatus = "NG"; - modbusResult = 2; - } - if (volumStatus1 == "OK" && areaStatus1 == "OK" && volumStatus2 == "OK" && areaStatus2 == "OK" && volumStatus3 == "OK" && areaStatus3 == "OK" && dj < 2 && qx < 2) - { - resultStatus = "OK"; - modbusResult = 1; - okNum++; - } - else if (dj > 1) - { - if (label34.InvokeRequired) + else { - label34.Invoke(new Action(() => label34.Text = "断胶了,请注意检查!!!!")); - label34.BackColor = Color.Red; - } - resultStatus = "NG"; - modbusResult = 2; + volumStatus2 = "NG"; - } - else if (qx > 1) - { - if (label34.InvokeRequired) + } + if (volum3 > csv3min && volum3 < csv3max) { - label34.Invoke(new Action(() => label34.Text = "断胶了,请注意检查!!!!")); - label34.BackColor = Color.Red; + volumStatus3 = "OK"; } - resultStatus = "NG"; - modbusResult = 2; + else + { + volumStatus3 = "NG"; - } - else - { - resultStatus = "NG"; - modbusResult = 2; //先观察数据,别忘记改2ng - ngNum++; - } - CogImage16Range mImage = cogRecordDisplay1.Record.Content as CogImage16Range; - string result = $"{volum1:F2},{area1:F2},{resultStatus}"; - ; + } + if (area1 > csa1min && area1 < csa1max && dw < 2 && dj < 2) + { + areaStatus1 = "OK"; + } + else + { + areaStatus1 = "NG"; + } + if (area2 > csa2min && area2 < csa2max) + { + areaStatus2 = "OK"; + } + else + { + areaStatus2 = "NG"; + } + if (area3 > csa3min && area3 < csa3max) + { + areaStatus3 = "OK"; + } + else + { + areaStatus3 = "NG"; + } - if (lbl_Volume1.InvokeRequired) - { - lbl_Volume1.Invoke(new Action(() => lbl_Volume1.Text = volum1.ToString("0.00" + "mm³"))); - } - if (lbl_Area1.InvokeRequired) - { - lbl_Area1.Invoke(new Action(() => lbl_Area1.Text = area1.ToString("0.00" + "mm²"))); - } - if (lbl_Volume2.InvokeRequired) - { - lbl_Volume2.Invoke(new Action(() => lbl_Volume2.Text = volum2.ToString("0.00" + "mm³"))); - } - if (lbl_Area2.InvokeRequired) - { - lbl_Area2.Invoke(new Action(() => lbl_Area2.Text = area2.ToString("0.00" + "mm²"))); - } - if (lbl_Volume3.InvokeRequired) - { - lbl_Volume3.Invoke(new Action(() => lbl_Volume3.Text = volum3.ToString("0.00" + "mm³"))); - } - if (lbl_Area3.InvokeRequired) - { - lbl_Area3.Invoke(new Action(() => lbl_Area3.Text = area3.ToString("0.00" + "mm²"))); - } - - if (label54.InvokeRequired && myToolBlock2.RunStatus.Result == CogToolResultConstants.Accept) + if (dw > 1) + { + if (label34.InvokeRequired) + { + label34.Invoke(new Action(() => label34.Text = "断胶了,请注意检查!!!!")); + } + resultStatus = "NG"; + modbusResult = 2; + } + if (volumStatus1 == "OK" && areaStatus1 == "OK" && volumStatus2 == "OK" && areaStatus2 == "OK" && volumStatus3 == "OK" && areaStatus3 == "OK" && dj < 2 && qx < 2) + { + resultStatus = "OK"; + modbusResult = 1; + okNum++; + } + else if (dj > 1) + { + if (label34.InvokeRequired) + { + label34.Invoke(new Action(() => label34.Text = "断胶了,请注意检查!!!!")); + label34.BackColor = Color.Red; + } + resultStatus = "NG"; + modbusResult = 2; + + } + else if (qx > 1) + { + if (label34.InvokeRequired) + { + label34.Invoke(new Action(() => label34.Text = "断胶了,请注意检查!!!!")); + label34.BackColor = Color.Red; + } + resultStatus = "NG"; + modbusResult = 2; + + } + else + { + resultStatus = "NG"; + modbusResult = 2; //先观察数据,别忘记改2ng + ngNum++; + } + CogImage16Range mImage = cogRecordDisplay1.Record.Content as CogImage16Range; + string result = $"{volum1:F2},{area1:F2},{resultStatus}"; + ; + + if (lbl_Volume1.InvokeRequired) + { + lbl_Volume1.Invoke(new Action(() => lbl_Volume1.Text = volum1.ToString("0.00" + "mm³"))); + } + if (lbl_Area1.InvokeRequired) + { + lbl_Area1.Invoke(new Action(() => lbl_Area1.Text = area1.ToString("0.00" + "mm²"))); + } + if (lbl_Volume2.InvokeRequired) + { + lbl_Volume2.Invoke(new Action(() => lbl_Volume2.Text = volum2.ToString("0.00" + "mm³"))); + } + if (lbl_Area2.InvokeRequired) + { + lbl_Area2.Invoke(new Action(() => lbl_Area2.Text = area2.ToString("0.00" + "mm²"))); + } + if (lbl_Volume3.InvokeRequired) + { + lbl_Volume3.Invoke(new Action(() => lbl_Volume3.Text = volum3.ToString("0.00" + "mm³"))); + } + if (lbl_Area3.InvokeRequired) + { + lbl_Area3.Invoke(new Action(() => lbl_Area3.Text = area3.ToString("0.00" + "mm²"))); + } + + if (label54.InvokeRequired && myToolBlock2.RunStatus.Result == CogToolResultConstants.Accept) { label54.Invoke(new Action(() => label54.Text = bdktj.ToString("0.00" + "mm³"))); - if (bdktj > 74770 && bdktj < 75008 ) + if (bdktj > 74770 && bdktj < 75008) { label80.Invoke(new Action(() => label80.Text = ("OK"))); label80.Invoke(new Action(() => label80.BackColor = Color.Green)); @@ -983,173 +1076,180 @@ namespace 精工涂胶检测项目 label80.Invoke(new Action(() => label80.BackColor = Color.Red)); } } - else - { - label54.Invoke(new Action(() => label54.Text = "0.00" + "mm²")); - label80.Invoke(new Action(() => label80.Text = ("NG"))); - label80.Invoke(new Action(() => label80.BackColor = Color.Red)); - } - - if (volumStatus1 == "OK") - { - if (lbl_Volume1Status.InvokeRequired) + else { - lbl_Volume1Status.Invoke(new Action(() => lbl_Volume1Status.Text = "合格")); - lbl_Volume1Status.BackColor = Color.Green; + label54.Invoke(new Action(() => label54.Text = "0.00" + "mm²")); + label80.Invoke(new Action(() => label80.Text = ("NG"))); + label80.Invoke(new Action(() => label80.BackColor = Color.Red)); } - } - else - { - if (lbl_Volume1Status.InvokeRequired) + + if (volumStatus1 == "OK") { - lbl_Volume1Status.Invoke(new Action(() => lbl_Volume1Status.Text = "不合格")); - lbl_Volume1Status.BackColor = Color.Red; - label34.BackColor = Color.Red; - if (label34.InvokeRequired) + if (lbl_Volume1Status.InvokeRequired) { - label34.Invoke(new Action(() => label34.Text = "位置1体积检测超限!!!!")); + lbl_Volume1Status.Invoke(new Action(() => lbl_Volume1Status.Text = "合格")); + lbl_Volume1Status.BackColor = Color.Green; } } - } - if (volumStatus2 == "OK") - { - if (lbl_Volume2Status.InvokeRequired) + else { - lbl_Volume2Status.Invoke(new Action(() => lbl_Volume2Status.Text = "合格")); - lbl_Volume2Status.BackColor = Color.Green; + if (lbl_Volume1Status.InvokeRequired) + { + lbl_Volume1Status.Invoke(new Action(() => lbl_Volume1Status.Text = "不合格")); + lbl_Volume1Status.BackColor = Color.Red; + label34.BackColor = Color.Red; + if (label34.InvokeRequired) + { + label34.Invoke(new Action(() => label34.Text = "位置1体积检测超限!!!!")); + } + } } - } - else - { - if (lbl_Volume2Status.InvokeRequired) + if (volumStatus2 == "OK") { - lbl_Volume2Status.Invoke(new Action(() => lbl_Volume2Status.Text = "不合格")); - lbl_Volume2Status.BackColor = Color.Red; - label34.BackColor = Color.Red; + if (lbl_Volume2Status.InvokeRequired) + { + lbl_Volume2Status.Invoke(new Action(() => lbl_Volume2Status.Text = "合格")); + lbl_Volume2Status.BackColor = Color.Green; + } } - if (label34.InvokeRequired) + else { - label34.Invoke(new Action(() => label34.Text = "位置2体积检测超限!!!!")); + if (lbl_Volume2Status.InvokeRequired) + { + lbl_Volume2Status.Invoke(new Action(() => lbl_Volume2Status.Text = "不合格")); + lbl_Volume2Status.BackColor = Color.Red; + label34.BackColor = Color.Red; + } + if (label34.InvokeRequired) + { + label34.Invoke(new Action(() => label34.Text = "位置2体积检测超限!!!!")); + } } - } - if (volumStatus3 == "OK") - { - if (lbl_Volume3Status.InvokeRequired) + if (volumStatus3 == "OK") { - lbl_Volume3Status.Invoke(new Action(() => lbl_Volume3Status.Text = "合格")); - lbl_Volume3Status.BackColor = Color.Green; - GC.Collect(); + if (lbl_Volume3Status.InvokeRequired) + { + lbl_Volume3Status.Invoke(new Action(() => lbl_Volume3Status.Text = "合格")); + lbl_Volume3Status.BackColor = Color.Green; + GC.Collect(); + } } - } - else - { - if (lbl_Volume3Status.InvokeRequired) + else { - lbl_Volume3Status.Invoke(new Action(() => lbl_Volume3Status.Text = "不合格")); - lbl_Volume3Status.BackColor = Color.Red; - label34.BackColor = Color.Red; + if (lbl_Volume3Status.InvokeRequired) + { + lbl_Volume3Status.Invoke(new Action(() => lbl_Volume3Status.Text = "不合格")); + lbl_Volume3Status.BackColor = Color.Red; + label34.BackColor = Color.Red; + } + if (label34.InvokeRequired) + { + label34.Invoke(new Action(() => label34.Text = "位置3体积检测超限!!!!")); + } } - if (label34.InvokeRequired) + if (areaStatus1 == "OK") { - label34.Invoke(new Action(() => label34.Text = "位置3体积检测超限!!!!")); + if (lbl_Area1Status.InvokeRequired) + { + lbl_Area1Status.Invoke(new Action(() => lbl_Area1Status.Text = "合格")); + lbl_Area1Status.BackColor = Color.Green; + } } - } - if (areaStatus1 == "OK") - { - if (lbl_Area1Status.InvokeRequired) + else { - lbl_Area1Status.Invoke(new Action(() => lbl_Area1Status.Text = "合格")); - lbl_Area1Status.BackColor = Color.Green; + if (lbl_Area1Status.InvokeRequired) + { + lbl_Area1Status.Invoke(new Action(() => lbl_Area1Status.Text = "不合格")); + lbl_Area1Status.BackColor = Color.Red; + label34.BackColor = Color.Red; + } + if (label34.InvokeRequired) + { + label34.Invoke(new Action(() => label34.Text = "位置1面积检测超限!!!!")); + } } - } - else - { - if (lbl_Area1Status.InvokeRequired) + if (areaStatus2 == "OK") { - lbl_Area1Status.Invoke(new Action(() => lbl_Area1Status.Text = "不合格")); - lbl_Area1Status.BackColor = Color.Red; - label34.BackColor = Color.Red; + if (lbl_Area2Status.InvokeRequired) + { + lbl_Area2Status.Invoke(new Action(() => lbl_Area2Status.Text = "合格")); + lbl_Area2Status.BackColor = Color.Green; + } } - if (label34.InvokeRequired) + else { - label34.Invoke(new Action(() => label34.Text = "位置1面积检测超限!!!!")); + if (lbl_Area2Status.InvokeRequired) + { + lbl_Area2Status.Invoke(new Action(() => lbl_Area2Status.Text = "不合格")); + lbl_Area2Status.BackColor = Color.Red; + label34.BackColor = Color.Red; + } + if (label34.InvokeRequired) + { + label34.Invoke(new Action(() => label34.Text = "位置2面积检测超限!!!!")); + } } - } - if (areaStatus2 == "OK") - { - if (lbl_Area2Status.InvokeRequired) + if (areaStatus3 == "OK") { - lbl_Area2Status.Invoke(new Action(() => lbl_Area2Status.Text = "合格")); - lbl_Area2Status.BackColor = Color.Green; + if (lbl_Area3Status.InvokeRequired) + { + lbl_Area3Status.Invoke(new Action(() => lbl_Area3Status.Text = "合格")); + lbl_Area3Status.BackColor = Color.Green; + } } - } - else - { - if (lbl_Area2Status.InvokeRequired) + else { - lbl_Area2Status.Invoke(new Action(() => lbl_Area2Status.Text = "不合格")); - lbl_Area2Status.BackColor = Color.Red; - label34.BackColor = Color.Red; + if (lbl_Area3Status.InvokeRequired) + { + lbl_Area3Status.Invoke(new Action(() => lbl_Area3Status.Text = "不合格")); + lbl_Area3Status.BackColor = Color.Red; + label34.BackColor = Color.Red; + } + if (label34.InvokeRequired) + { + label34.Invoke(new Action(() => label34.Text = "位置3面积检测超限!!!!")); + } } - if (label34.InvokeRequired) + if (lbl_CheckNum.InvokeRequired) { - label34.Invoke(new Action(() => label34.Text = "位置2面积检测超限!!!!")); - } - } - if (areaStatus3 == "OK") - { - if (lbl_Area3Status.InvokeRequired) - { - lbl_Area3Status.Invoke(new Action(() => lbl_Area3Status.Text = "合格")); - lbl_Area3Status.BackColor = Color.Green; - } - } - else - { - if (lbl_Area3Status.InvokeRequired) - { - lbl_Area3Status.Invoke(new Action(() => lbl_Area3Status.Text = "不合格")); - lbl_Area3Status.BackColor = Color.Red; - label34.BackColor = Color.Red; - } - if (label34.InvokeRequired) - { - label34.Invoke(new Action(() => label34.Text = "位置3面积检测超限!!!!")); - } - } - if (lbl_CheckNum.InvokeRequired) - { - lbl_CheckNum.Invoke(new Action(() => lbl_CheckNum.Text = (okNum + ngNum).ToString())); - lbl_CheckNum.Invoke(new Action(() => label11.Text = okNum.ToString())); - lbl_CheckNum.Invoke(new Action(() => label14.Text = ngNum.ToString())); - double zs = Double.Parse((okNum + ngNum).ToString()); - double OKs = Double.Parse(okNum.ToString()); - lbl_CheckNum.Invoke(new Action(() => lbl_OkRate.Text = (OKs / zs).ToString("0.00%"))); + lbl_CheckNum.Invoke(new Action(() => lbl_CheckNum.Text = (okNum + ngNum).ToString())); + lbl_CheckNum.Invoke(new Action(() => label11.Text = okNum.ToString())); + lbl_CheckNum.Invoke(new Action(() => label14.Text = ngNum.ToString())); + double zs = Double.Parse((okNum + ngNum).ToString()); + double OKs = Double.Parse(okNum.ToString()); + lbl_CheckNum.Invoke(new Action(() => lbl_OkRate.Text = (OKs / zs).ToString("0.00%"))); + + } + try + { + modbusClient.WriteSingleRegister("4", (short)modbusResult);//ModbusResult + modbusClient.WriteSingleRegisterString("31", "11" + imageSavePath); //存图路径 + string Area1 = area1.ToString("0.00"); + string Volume1 = volum1.ToString("0.00"); + string Area2 = area2.ToString("0.00"); + string Volume2 = volum2.ToString("0.00"); + string Area3 = area3.ToString("0.00"); + string Volume3 = volum3.ToString("0.00"); + //要分组发送三次结果 + modbusClient.WriteSingleRegisterString("72", "11" + Area1);//区域1面积 + modbusClient.WriteSingleRegisterString("78", "11" + Volume1);//区域1体积 + modbusClient.WriteSingleRegisterString("84", "11" + Area2);//区域2面积 + modbusClient.WriteSingleRegisterString("90", "11" + Volume2);//区域2体积 + modbusClient.WriteSingleRegisterString("96", "11" + Area3);//区域3面积 + modbusClient.WriteSingleRegisterString("102", "11" + Volume3);//区域3体积 + } + catch (Exception ex) + { + AddLog($"发送数据失败:{ex.Message}", 1); + } + + if (ProductSN == null) + { + s = "123"; + } + else { s = ProductSN.Substring(2); } + //string s = ProductSN.Substring(2); - } - modbusClient.WriteSingleRegister("4", (short)modbusResult);//ModbusResult - modbusClient.WriteSingleRegisterString("31", "11" + imageSavePath); //存图路径 - string Area1 = area1.ToString("0.00"); - string Volume1 = volum1.ToString("0.00"); - string Area2 = area2.ToString("0.00"); - string Volume2 = volum2.ToString("0.00"); - string Area3 = area3.ToString("0.00"); - string Volume3 = volum3.ToString("0.00"); - //要分组发送三次结果 - modbusClient.WriteSingleRegisterString("72", "11" + Area1);//区域1面积 - modbusClient.WriteSingleRegisterString("78", "11" + Volume1);//区域1体积 - modbusClient.WriteSingleRegisterString("84", "11" + Area2);//区域2面积 - modbusClient.WriteSingleRegisterString("90", "11" + Volume2);//区域2体积 - modbusClient.WriteSingleRegisterString("96", "11" + Area3);//区域3面积 - modbusClient.WriteSingleRegisterString("102", "11" + Volume3);//区域3体积 - - if (ProductSN == null) - { - s = "123"; - } - else { s = ProductSN.Substring(2); } - //string s = ProductSN.Substring(2); - Excel.Application excelApp = null; Excel.Workbook workbook = null; Excel.Worksheet worksheet = null; @@ -1159,158 +1259,168 @@ namespace 精工涂胶检测项目 string bdexcelPath = Path.Combine(rootPath, bddateFileName); //CaptureSelfForm(this,savePath); bool isNewFile = !File.Exists(excelPath); - if (checkBox5.Checked==false) + if (checkBox5.Checked == false) + { + try + { + excelApp = new Excel.Application(); + excelApp.Visible = false; + excelApp.DisplayAlerts = false; + if (isNewFile) + { + workbook = excelApp.Workbooks.Add(); + worksheet = (Excel.Worksheet)workbook.Worksheets[1]; + worksheet.Name = "每日数据"; + worksheet.Cells[1, 1] = "记录时间"; + worksheet.Cells[1, 2] = "产品SN"; + worksheet.Cells[1, 3] = "位置1体积"; + worksheet.Cells[1, 4] = "位置1面积"; + worksheet.Cells[1, 5] = "位置2体积"; + worksheet.Cells[1, 6] = "位置2面积"; + worksheet.Cells[1, 7] = "位置3体积"; + worksheet.Cells[1, 8] = "位置3面积"; + worksheet.Range["A1:H1"].Font.Bold = true; + } + else + { + workbook = excelApp.Workbooks.Open(excelPath); + worksheet = (Excel.Worksheet)workbook.Worksheets["每日数据"]; + } + int lastRow = worksheet.UsedRange.Rows.Count + 1; + if (isNewFile) lastRow = 2; + worksheet.Cells[lastRow, 1] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + worksheet.Cells[lastRow, 2] = s; + worksheet.Cells[lastRow, 3] = volum1.ToString(); + worksheet.Cells[lastRow, 4] = area1.ToString(); + worksheet.Cells[lastRow, 5] = volum2.ToString(); + worksheet.Cells[lastRow, 6] = area2.ToString(); + worksheet.Cells[lastRow, 7] = volum3.ToString(); + worksheet.Cells[lastRow, 8] = area3.ToString(); + worksheet.Range["A:H"].EntireColumn.AutoFit(); + if (isNewFile) + { + workbook.SaveAs(excelPath); + } + else + { + workbook.Save(); + } + AddLog("数据写入成功", 1); + } + + catch (Exception ex) { AddLog($"数据写入失败:{ex.Message}", 1); ; } + finally + { + if (workbook != null) + { + workbook.Close(); + System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); + } + if (excelApp != null) + { + excelApp.Quit(); + System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); + } + worksheet = null; + workbook = null; + excelApp = null; + GC.Collect(); + GC.WaitForPendingFinalizers(); + } + } + bool isbdNewFile = !File.Exists(bdexcelPath); + if (checkBox5.Checked == true) + { + try + { + excelApp = new Excel.Application(); + excelApp.Visible = false; + excelApp.DisplayAlerts = false; + if (isbdNewFile) + { + workbook = excelApp.Workbooks.Add(); + worksheet = (Excel.Worksheet)workbook.Worksheets[1]; + worksheet.Name = "标定数据"; + worksheet.Cells[1, 1] = "记录时间"; + worksheet.Cells[1, 2] = "标定块顶部面积"; + worksheet.Cells[1, 3] = "标定块底部面积"; + worksheet.Cells[1, 4] = "标定块顶部位置1宽度"; + worksheet.Cells[1, 5] = "标定块顶部位置2宽度"; + worksheet.Cells[1, 6] = "标定块顶部位置3宽度"; + worksheet.Cells[1, 7] = "标定块顶部位置4宽度"; + worksheet.Cells[1, 8] = "标定块顶部位置5宽度"; + worksheet.Cells[1, 9] = "标定块顶部位置6宽度"; + worksheet.Cells[1, 10] = "标定块底部位置1宽度"; + worksheet.Cells[1, 11] = "标定块底部位置2宽度"; + worksheet.Cells[1, 12] = "标定块底部位置3宽度"; + worksheet.Cells[1, 13] = "标定块底部位置4宽度"; + worksheet.Cells[1, 14] = "标定块底部位置5宽度"; + worksheet.Cells[1, 15] = "标定块底部位置6宽度"; + worksheet.Cells[1, 16] = "标定块整体体积"; + worksheet.Range["A1:P1"].Font.Bold = true; + } + else + { + workbook = excelApp.Workbooks.Open(bdexcelPath); + worksheet = (Excel.Worksheet)workbook.Worksheets["标定数据"]; + } + int lastRow = worksheet.UsedRange.Rows.Count + 1; + if (isNewFile) lastRow = 2; + worksheet.Cells[lastRow, 1] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + worksheet.Cells[lastRow, 2] = topmj.ToString(); + worksheet.Cells[lastRow, 3] = botmj.ToString(); + worksheet.Cells[lastRow, 4] = top1.ToString(); + worksheet.Cells[lastRow, 5] = top2.ToString(); + worksheet.Cells[lastRow, 6] = top3.ToString(); + worksheet.Cells[lastRow, 7] = top4.ToString(); + worksheet.Cells[lastRow, 8] = top5.ToString(); + worksheet.Cells[lastRow, 9] = top6.ToString(); + worksheet.Cells[lastRow, 10] = bot1.ToString(); + worksheet.Cells[lastRow, 11] = bot2.ToString(); + worksheet.Cells[lastRow, 12] = bot3.ToString(); + worksheet.Cells[lastRow, 13] = bot4.ToString(); + worksheet.Cells[lastRow, 14] = bot5.ToString(); + worksheet.Cells[lastRow, 15] = bot6.ToString(); + worksheet.Cells[lastRow, 16] = bdktj.ToString(); + worksheet.Range["A:P"].EntireColumn.AutoFit(); + if (isbdNewFile) + { + workbook.SaveAs(bdexcelPath); + } + else + { + workbook.Save(); + } + AddLog("数据写入成功", 1); + } + + catch (Exception ex) { AddLog("数据写入成功", 1); ; } + finally + { + if (workbook != null) + { + workbook.Close(); + System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); + } + if (excelApp != null) + { + excelApp.Quit(); + System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); + } + worksheet = null; + workbook = null; + excelApp = null; + GC.Collect(); + GC.WaitForPendingFinalizers(); + } + } + + } + catch (Exception ex) { - try - { - excelApp = new Excel.Application(); - excelApp.Visible = false; - excelApp.DisplayAlerts = false; - if (isNewFile) - { - workbook = excelApp.Workbooks.Add(); - worksheet = (Excel.Worksheet)workbook.Worksheets[1]; - worksheet.Name = "每日数据"; - worksheet.Cells[1, 1] = "记录时间"; - worksheet.Cells[1, 2] = "产品SN"; - worksheet.Cells[1, 3] = "位置1体积"; - worksheet.Cells[1, 4] = "位置1面积"; - worksheet.Cells[1, 5] = "位置2体积"; - worksheet.Cells[1, 6] = "位置2面积"; - worksheet.Cells[1, 7] = "位置3体积"; - worksheet.Cells[1, 8] = "位置3面积"; - worksheet.Range["A1:H1"].Font.Bold = true; - } - else - { - workbook = excelApp.Workbooks.Open(excelPath); - worksheet = (Excel.Worksheet)workbook.Worksheets["每日数据"]; - } - int lastRow = worksheet.UsedRange.Rows.Count + 1; - if (isNewFile) lastRow = 2; - worksheet.Cells[lastRow, 1] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); - worksheet.Cells[lastRow, 2] = s; - worksheet.Cells[lastRow, 3] = volum1.ToString(); - worksheet.Cells[lastRow, 4] = area1.ToString(); - worksheet.Cells[lastRow, 5] = volum2.ToString(); - worksheet.Cells[lastRow, 6] = area2.ToString(); - worksheet.Cells[lastRow, 7] = volum3.ToString(); - worksheet.Cells[lastRow, 8] = area3.ToString(); - worksheet.Range["A:H"].EntireColumn.AutoFit(); - if (isNewFile) - { - workbook.SaveAs(excelPath); - } - else - { - workbook.Save(); - } - AddLog("数据写入成功", 1); - } - - catch (Exception ex) { AddLog("数据写入成功", 1); ; } - finally - { - if (workbook != null) { workbook.Close(); - System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); - } - if (excelApp != null) - { excelApp.Quit(); - System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); - } - worksheet = null; - workbook = null; - excelApp = null; - GC.Collect(); - GC.WaitForPendingFinalizers(); - } + MessageBox.Show(ex.ToString()); } - bool isbdNewFile = !File.Exists(bdexcelPath); - if (checkBox5.Checked==true) { - try - { - excelApp = new Excel.Application(); - excelApp.Visible = false; - excelApp.DisplayAlerts = false; - if (isbdNewFile) - { - workbook = excelApp.Workbooks.Add(); - worksheet = (Excel.Worksheet)workbook.Worksheets[1]; - worksheet.Name = "标定数据"; - worksheet.Cells[1, 1] = "记录时间"; - worksheet.Cells[1, 2] = "标定块顶部面积"; - worksheet.Cells[1, 3] = "标定块底部面积"; - worksheet.Cells[1, 4] = "标定块顶部位置1宽度"; - worksheet.Cells[1, 5] = "标定块顶部位置2宽度"; - worksheet.Cells[1, 6] = "标定块顶部位置3宽度"; - worksheet.Cells[1, 7] = "标定块顶部位置4宽度"; - worksheet.Cells[1, 8] = "标定块顶部位置5宽度"; - worksheet.Cells[1, 9] = "标定块顶部位置6宽度"; - worksheet.Cells[1, 10] = "标定块底部位置1宽度"; - worksheet.Cells[1, 11] = "标定块底部位置2宽度"; - worksheet.Cells[1, 12] = "标定块底部位置3宽度"; - worksheet.Cells[1, 13] = "标定块底部位置4宽度"; - worksheet.Cells[1, 14] = "标定块底部位置5宽度"; - worksheet.Cells[1, 15] = "标定块底部位置6宽度"; - worksheet.Cells[1, 16] = "标定块整体体积"; - worksheet.Range["A1:P1"].Font.Bold = true; - } - else - { - workbook = excelApp.Workbooks.Open(bdexcelPath); - worksheet = (Excel.Worksheet)workbook.Worksheets["标定数据"]; - } - int lastRow = worksheet.UsedRange.Rows.Count + 1; - if (isNewFile) lastRow = 2; - worksheet.Cells[lastRow, 1] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); - worksheet.Cells[lastRow, 2] = topmj.ToString(); - worksheet.Cells[lastRow, 3] = botmj.ToString(); - worksheet.Cells[lastRow, 4] = top1.ToString(); - worksheet.Cells[lastRow, 5] = top2.ToString(); - worksheet.Cells[lastRow, 6] = top3.ToString(); - worksheet.Cells[lastRow, 7] = top4.ToString(); - worksheet.Cells[lastRow, 8] = top5.ToString(); - worksheet.Cells[lastRow, 9] = top6.ToString(); - worksheet.Cells[lastRow, 10] = bot1.ToString(); - worksheet.Cells[lastRow, 11] = bot2.ToString(); - worksheet.Cells[lastRow, 12] = bot3.ToString(); - worksheet.Cells[lastRow, 13] = bot4.ToString(); - worksheet.Cells[lastRow, 14] = bot5.ToString(); - worksheet.Cells[lastRow, 15] = bot6.ToString(); - worksheet.Cells[lastRow, 16] = bdktj.ToString(); - worksheet.Range["A:P"].EntireColumn.AutoFit(); - if (isbdNewFile) - { - workbook.SaveAs(bdexcelPath); - } - else - { - workbook.Save(); - } - AddLog("数据写入成功", 1); - } - catch (Exception ex) { AddLog("数据写入成功", 1); ; } - finally - { - if (workbook != null) - { - workbook.Close(); - System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); - } - if (excelApp != null) - { - excelApp.Quit(); - System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); - } - worksheet = null; - workbook = null; - excelApp = null; - GC.Collect(); - GC.WaitForPendingFinalizers(); - } - } - }