RenzYi Modified

This commit is contained in:
2026-05-08 13:05:33 +08:00
parent 760ff50baf
commit 8f6f7a561a

View File

@@ -113,7 +113,7 @@ namespace 精工涂胶检测项目
private MyMessageFilter messageFilter; private MyMessageFilter messageFilter;
public ModbusTcpClient modbusClient; // 新增Modbus客户端实例 public ModbusTcpClient modbusClient; // 新增Modbus客户端实例
private Timer modbusPollTimer; // 新增轮询定时器 private Timer modbusPollTimer; // 新增轮询定时器
private Timer modbusHeartbeat; private System.Timers.Timer modbusHeartbeat;
public Plc plc; public Plc plc;
@@ -221,20 +221,32 @@ namespace 精工涂胶检测项目
{ {
if (modbusHeartbeat == null) if (modbusHeartbeat == null)
{ {
modbusHeartbeat = new Timer { Interval = 500 }; modbusHeartbeat = new System.Timers.Timer { Interval = 500 };
modbusHeartbeat.Tick += async (s, e) => modbusHeartbeat.AutoReset = true;
modbusHeartbeat.SynchronizingObject = null;
modbusHeartbeat.Elapsed += async (s, e) =>
{
try
{ {
modbusClient.WriteSingleRegister("0", 1); modbusClient.WriteSingleRegister("0", 1);
Thread.Sleep(50); await Task.Delay(50);
}
catch (Exception ex)
{
AddLog($"Modbus心跳失败{ex.Message}", 2);
}
}; };
modbusHeartbeat.Start();
} }
} }
private void StopHeartBeat() private void StopHeartBeat()
{ {
if (modbusHeartbeat == null && modbusHeartbeat.Enabled) if (modbusHeartbeat != null)
{ {
modbusHeartbeat.Stop(); modbusHeartbeat.Stop();
modbusHeartbeat.Dispose();
modbusHeartbeat = null;
} }
} }
@@ -598,6 +610,8 @@ namespace 精工涂胶检测项目
} }
} }
private void MyJobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e) private void MyJobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e)
{
try
{ {
///待会儿需要改通过ToolBlock是否报错来判断。 ///待会儿需要改通过ToolBlock是否报错来判断。
//结果处理代码 //结果处理代码
@@ -626,13 +640,47 @@ namespace 精工涂胶检测项目
ICogRecord BOT6; ICogRecord BOT6;
ICogRecord BDKTJ; ICogRecord BDKTJ;
ICogRecord TJ; ICogRecord TJ;
ICogRecord topRecord = myJobManager.UserResult(); ICogRecord topRecord;
string currentDateFolder;
try
{
topRecord = myJobManager.UserResult();
}
catch (Exception ex)
{
AddLog($"获取用户结果失败:{ex.Message}", 1);
return;
}
try
{
string s = ProductSN.Substring(2); string s = ProductSN.Substring(2);
///create graphic wrapper for the range image with its grey overlay }
catch (Exception ex)
{
AddLog($"获取产品SN失败{ex.Message}", 1);
return;
}
try
{
Cog3DRangeImageGraphic rImgG = myToolBlock1.Outputs["CombinedImage"].Value as Cog3DRangeImageGraphic; Cog3DRangeImageGraphic rImgG = myToolBlock1.Outputs["CombinedImage"].Value as Cog3DRangeImageGraphic;
cog3DDisplayV2WF1.Add(rImgG); }
catch (Exception ex)
{
AddLog($"获取图像失败:{ex.Message}", 1);
return;
}
string rootPath = @"D:\Date"; string rootPath = @"D:\Date";
string currentDateFolder = Form1.CreateCurrentDateFolder(rootPath); try
{
currentDateFolder = Form1.CreateCurrentDateFolder(rootPath);
}
catch (Exception ex)
{
AddLog($"创建当前文件夹失败:{ex.Message}", 1);
return;
}
AddLog("当前文件夹已创建", 1); AddLog("当前文件夹已创建", 1);
//图像显示需要改 //图像显示需要改
// ICogRecord ShowImageRecord = topRecord.SubRecords["ShowLastRunRecordForUserQueue"].SubRecords["LastRun"].SubRecords["Image Source.OutputImage"]; // ICogRecord ShowImageRecord = topRecord.SubRecords["ShowLastRunRecordForUserQueue"].SubRecords["LastRun"].SubRecords["Image Source.OutputImage"];
@@ -655,12 +703,21 @@ namespace 精工涂胶检测项目
label2.Invoke(new Action(() => label2.Text = s)); label2.Invoke(new Action(() => label2.Text = s));
Image img = cogRecordDisplay1.CreateContentBitmap(Cognex.VisionPro.Display.CogDisplayContentBitmapConstants.Display); Image img = cogRecordDisplay1.CreateContentBitmap(Cognex.VisionPro.Display.CogDisplayContentBitmapConstants.Display);
string imageName = s +DateTime.Now.ToString("ss")+ ".Png"; string imageName = s + DateTime.Now.ToString("ss") + ".Png";
imageSavePath = Path.Combine(currentDateFolder,imageName); imageSavePath = Path.Combine(currentDateFolder, imageName);
try
{
img.Save(imageSavePath, ImageFormat.Png); img.Save(imageSavePath, ImageFormat.Png);
}
catch (Exception ex)
{
AddLog($"保存图像失败:{ex.Message}", 1);
return;
}
cogRecordDisplay1.AutoFit = true; cogRecordDisplay1.AutoFit = true;
} }
try
{
tmpVolum1 = topRecord.SubRecords["Volum1"]; tmpVolum1 = topRecord.SubRecords["Volum1"];
tmpArea1 = topRecord.SubRecords["Area1"]; tmpArea1 = topRecord.SubRecords["Area1"];
tmpVolum2 = topRecord.SubRecords["Volum2"]; tmpVolum2 = topRecord.SubRecords["Volum2"];
@@ -681,10 +738,14 @@ namespace 精工涂胶检测项目
BOT3 = topRecord.SubRecords["BOT3"]; BOT3 = topRecord.SubRecords["BOT3"];
BOT4 = topRecord.SubRecords["BOT1"]; BOT4 = topRecord.SubRecords["BOT1"];
BOT5 = topRecord.SubRecords["BOT2"]; BOT5 = topRecord.SubRecords["BOT2"];
BOT6= topRecord.SubRecords["BOT3"]; BOT6 = topRecord.SubRecords["BOT3"];
TJ = topRecord.SubRecords["TJ"]; TJ = topRecord.SubRecords["TJ"];
//label11.Text = tmpVolum.ToString(); }
//label12.Text = tmpArea.ToString();// catch (Exception ex)
{
AddLog($"获取数据失败:{ex.Message}", 1);
return;
}
string volumStatus; string volumStatus;
string areaStatus; string areaStatus;
@@ -699,28 +760,52 @@ namespace 精工涂胶检测项目
double dj = 1.0; double dj = 1.0;
double qx = 1.0; double qx = 1.0;
double dw = 1.0; double dw = 1.0;
double volum1 = (double)tmpVolum1.Content;
double area1 = (double)tmpArea1.Content; double volum1;
double volum2 = (double)tmpVolum2.Content; double area1;
double area2 = (double)tmpArea2.Content; double volum2;
double volum3 = (double)tmpVolum3.Content; double area2;
double area3 = (double)tmpArea3.Content; double volum3;
double top1 = (double)TOP1.Content; double area3;
double top2 = (double)TOP2.Content; double top1;
double top3 = (double)TOP3.Content; double top2;
double top4 = (double)TOP1.Content; double top3;
double top5 = (double)TOP2.Content; double top4;
double top6 = (double)TOP3.Content; double top5;
double topmj = (double)TOP2.Content*(double)TOP3.Content; double top6;
double bot1 = (double)BOT1.Content; double topmj;
double bot2 = (double)BOT2.Content; double bot1;
double bot3 = (double)BOT3.Content; double bot2;
double bot4 = (double)BOT4.Content; double bot3;
double bot5 = (double)BOT5.Content; double bot4;
double bot6 = (double)BOT6.Content; double bot5;
double botmj = (double)BOT1.Content * (double)BOT3.Content; 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;
//算体积 //算体积
double bdktj = (double)TJ.Content-3800;// 0.333333 *20*(topmj+botmj+3200); bdktj = (double)TJ.Content - 3800;// 0.333333 *20*(topmj+botmj+3200);
//string dj = DJCJ.ToString(); //string dj = DJCJ.ToString();
if (checkBox3.Checked) if (checkBox3.Checked)
{ {
@@ -731,6 +816,13 @@ namespace 精工涂胶检测项目
qx = (double)QX.Content; qx = (double)QX.Content;
} }
dw = (double)DW.Content; dw = (double)DW.Content;
}
catch (Exception ex)
{
AddLog($"获取数据失败:{ex.Message}", 1);
return;
}
//定义12个阈值参数 //定义12个阈值参数
string CSV1max; string CSV1max;
string CSV1min; string CSV1min;
@@ -749,8 +841,9 @@ namespace 精工涂胶检测项目
//使用阈值 //使用阈值
try try
{string used=label81.Text; {
string usePath= Path.Combine(@"D:\", $"{used}.txt"); string used = label81.Text;
string usePath = Path.Combine(@"D:\", $"{used}.txt");
if (File.Exists(usePath)) if (File.Exists(usePath))
{ {
string[] lines = File.ReadAllLines(usePath); // 读取所有行[^1] string[] lines = File.ReadAllLines(usePath); // 读取所有行[^1]
@@ -781,13 +874,13 @@ namespace 精工涂胶检测项目
csv3min = Convert.ToInt32(CSV3min); csv3min = Convert.ToInt32(CSV3min);
csa3max = Convert.ToInt32(CSA3max); csa3max = Convert.ToInt32(CSA3max);
csa3min = Convert.ToInt32(CSA3min); csa3min = Convert.ToInt32(CSA3min);
bdyz= Convert.ToInt32(BdYZ); bdyz = Convert.ToInt32(BdYZ);
TOPMJMin = 1600 * ((100 - bdyz) * 0.01); TOPMJMin = 1600 * ((100 - bdyz) * 0.01);
TOPMJMax = 1600 * ((100 + bdyz) * 0.01); TOPMJMax = 1600 * ((100 + bdyz) * 0.01);
BOTMJMin = 6400 * ((100 - bdyz) * 0.01); BOTMJMin = 6400 * ((100 - bdyz) * 0.01);
BOTMJMax = 6400 * ((100 + bdyz) * 0.01); BOTMJMax = 6400 * ((100 + bdyz) * 0.01);
TOP1Min = 40 * ((100 - bdyz) * 0.01); TOP1Min = 40 * ((100 - bdyz) * 0.01);
TOP1Max = 40 * ((100+ bdyz) * 0.01); TOP1Max = 40 * ((100 + bdyz) * 0.01);
TOP2Min = 40 * ((100 - bdyz) * 0.01); TOP2Min = 40 * ((100 - bdyz) * 0.01);
TOP2Max = 40 * ((100 + bdyz) * 0.01); TOP2Max = 40 * ((100 + bdyz) * 0.01);
TOP3Min = 40 * ((100 - bdyz) * 0.01); TOP3Min = 40 * ((100 - bdyz) * 0.01);
@@ -810,7 +903,7 @@ namespace 精工涂胶检测项目
BOT5Max = 80 * ((100 + bdyz) * 0.01); BOT5Max = 80 * ((100 + bdyz) * 0.01);
BOT6Min = 80 * ((100 - bdyz) * 0.01); BOT6Min = 80 * ((100 - bdyz) * 0.01);
BOT6Max = 80 * ((100 + bdyz) * 0.01); BOT6Max = 80 * ((100 + bdyz) * 0.01);
BDKTJMin =74000; BDKTJMin = 74000;
BDKTJMax = 75000; BDKTJMax = 75000;
} }
if (checkBox5.Checked) if (checkBox5.Checked)
@@ -818,7 +911,7 @@ namespace 精工涂胶检测项目
label64.Text = BDKTJMin.ToString() + "~" + BDKTJMax.ToString() + "mm³"; label64.Text = BDKTJMin.ToString() + "~" + BDKTJMax.ToString() + "mm³";
} }
if (checkBox2.Checked== false) if (checkBox2.Checked == false)
{ {
csv1max = 999999; csv1max = 999999;
csv1min = 0; //700 csv1min = 0; //700
@@ -845,7 +938,7 @@ namespace 精工涂胶检测项目
{ {
} }
if (volum1 > csv1min && volum1 < csv1max&& dw < 3&& dj < 2 && dw < 3) if (volum1 > csv1min && volum1 < csv1max && dw < 3 && dj < 2 && dw < 3)
{ {
volumStatus1 = "OK"; volumStatus1 = "OK";
} }
@@ -872,7 +965,7 @@ namespace 精工涂胶检测项目
volumStatus3 = "NG"; volumStatus3 = "NG";
} }
if (area1 > csa1min && area1 < csa1max&& dw < 2&& dj <2) if (area1 > csa1min && area1 < csa1max && dw < 2 && dj < 2)
{ {
areaStatus1 = "OK"; areaStatus1 = "OK";
} }
@@ -972,7 +1065,7 @@ namespace 精工涂胶检测项目
if (label54.InvokeRequired && myToolBlock2.RunStatus.Result == CogToolResultConstants.Accept) if (label54.InvokeRequired && myToolBlock2.RunStatus.Result == CogToolResultConstants.Accept)
{ {
label54.Invoke(new Action(() => label54.Text = bdktj.ToString("0.00" + "mm³"))); 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.Text = ("OK")));
label80.Invoke(new Action(() => label80.BackColor = Color.Green)); label80.Invoke(new Action(() => label80.BackColor = Color.Green));
@@ -1127,6 +1220,8 @@ namespace 精工涂胶检测项目
lbl_CheckNum.Invoke(new Action(() => lbl_OkRate.Text = (OKs / zs).ToString("0.00%"))); lbl_CheckNum.Invoke(new Action(() => lbl_OkRate.Text = (OKs / zs).ToString("0.00%")));
} }
try
{
modbusClient.WriteSingleRegister("4", (short)modbusResult);//ModbusResult modbusClient.WriteSingleRegister("4", (short)modbusResult);//ModbusResult
modbusClient.WriteSingleRegisterString("31", "11" + imageSavePath); //存图路径 modbusClient.WriteSingleRegisterString("31", "11" + imageSavePath); //存图路径
string Area1 = area1.ToString("0.00"); string Area1 = area1.ToString("0.00");
@@ -1142,6 +1237,11 @@ namespace 精工涂胶检测项目
modbusClient.WriteSingleRegisterString("90", "11" + Volume2);//区域2体积 modbusClient.WriteSingleRegisterString("90", "11" + Volume2);//区域2体积
modbusClient.WriteSingleRegisterString("96", "11" + Area3);//区域3面积 modbusClient.WriteSingleRegisterString("96", "11" + Area3);//区域3面积
modbusClient.WriteSingleRegisterString("102", "11" + Volume3);//区域3体积 modbusClient.WriteSingleRegisterString("102", "11" + Volume3);//区域3体积
}
catch (Exception ex)
{
AddLog($"发送数据失败:{ex.Message}", 1);
}
if (ProductSN == null) if (ProductSN == null)
{ {
@@ -1159,7 +1259,7 @@ namespace 精工涂胶检测项目
string bdexcelPath = Path.Combine(rootPath, bddateFileName); string bdexcelPath = Path.Combine(rootPath, bddateFileName);
//CaptureSelfForm(this,savePath); //CaptureSelfForm(this,savePath);
bool isNewFile = !File.Exists(excelPath); bool isNewFile = !File.Exists(excelPath);
if (checkBox5.Checked==false) if (checkBox5.Checked == false)
{ {
try try
{ {
@@ -1208,14 +1308,17 @@ namespace 精工涂胶检测项目
AddLog("数据写入成功", 1); AddLog("数据写入成功", 1);
} }
catch (Exception ex) { AddLog("数据写入成功", 1); ; } catch (Exception ex) { AddLog($"数据写入失败:{ex.Message}", 1); ; }
finally finally
{ {
if (workbook != null) { workbook.Close(); if (workbook != null)
{
workbook.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
} }
if (excelApp != null) if (excelApp != null)
{ excelApp.Quit(); {
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
} }
worksheet = null; worksheet = null;
@@ -1226,7 +1329,8 @@ namespace 精工涂胶检测项目
} }
} }
bool isbdNewFile = !File.Exists(bdexcelPath); bool isbdNewFile = !File.Exists(bdexcelPath);
if (checkBox5.Checked==true) { if (checkBox5.Checked == true)
{
try try
{ {
excelApp = new Excel.Application(); excelApp = new Excel.Application();
@@ -1312,6 +1416,12 @@ namespace 精工涂胶检测项目
} }
} }
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary> /// <summary>