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;
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) =>
{
try
{
modbusClient.WriteSingleRegister("0", 1);
Thread.Sleep(50);
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;
}
}
@@ -598,6 +610,8 @@ namespace 精工涂胶检测项目
}
}
private void MyJobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e)
{
try
{
///待会儿需要改通过ToolBlock是否报错来判断。
//结果处理代码
@@ -626,13 +640,47 @@ namespace 精工涂胶检测项目
ICogRecord BOT6;
ICogRecord BDKTJ;
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);
///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;
cog3DDisplayV2WF1.Add(rImgG);
}
catch (Exception ex)
{
AddLog($"获取图像失败:{ex.Message}", 1);
return;
}
string rootPath = @"D:\Date";
string currentDateFolder = Form1.CreateCurrentDateFolder(rootPath);
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"];
@@ -657,10 +705,19 @@ namespace 精工涂胶检测项目
Image img = cogRecordDisplay1.CreateContentBitmap(Cognex.VisionPro.Display.CogDisplayContentBitmapConstants.Display);
string imageName = s + DateTime.Now.ToString("ss") + ".Png";
imageSavePath = Path.Combine(currentDateFolder, imageName);
try
{
img.Save(imageSavePath, ImageFormat.Png);
}
catch (Exception ex)
{
AddLog($"保存图像失败:{ex.Message}", 1);
return;
}
cogRecordDisplay1.AutoFit = true;
}
try
{
tmpVolum1 = topRecord.SubRecords["Volum1"];
tmpArea1 = topRecord.SubRecords["Area1"];
tmpVolum2 = topRecord.SubRecords["Volum2"];
@@ -683,8 +740,12 @@ namespace 精工涂胶检测项目
BOT5 = topRecord.SubRecords["BOT2"];
BOT6 = topRecord.SubRecords["BOT3"];
TJ = topRecord.SubRecords["TJ"];
//label11.Text = tmpVolum.ToString();
//label12.Text = tmpArea.ToString();//
}
catch (Exception ex)
{
AddLog($"获取数据失败:{ex.Message}", 1);
return;
}
string volumStatus;
string areaStatus;
@@ -699,28 +760,52 @@ namespace 精工涂胶检测项目
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 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;
//算体积
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();
if (checkBox3.Checked)
{
@@ -731,6 +816,13 @@ namespace 精工涂胶检测项目
qx = (double)QX.Content;
}
dw = (double)DW.Content;
}
catch (Exception ex)
{
AddLog($"获取数据失败:{ex.Message}", 1);
return;
}
//定义12个阈值参数
string CSV1max;
string CSV1min;
@@ -749,7 +841,8 @@ namespace 精工涂胶检测项目
//使用阈值
try
{string used=label81.Text;
{
string used = label81.Text;
string usePath = Path.Combine(@"D:\", $"{used}.txt");
if (File.Exists(usePath))
{
@@ -1127,6 +1220,8 @@ namespace 精工涂胶检测项目
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");
@@ -1142,6 +1237,11 @@ namespace 精工涂胶检测项目
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)
{
@@ -1208,14 +1308,17 @@ namespace 精工涂胶检测项目
AddLog("数据写入成功", 1);
}
catch (Exception ex) { AddLog("数据写入成功", 1); ; }
catch (Exception ex) { AddLog($"数据写入失败:{ex.Message}", 1); ; }
finally
{
if (workbook != null) { workbook.Close();
if (workbook != null)
{
workbook.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
}
if (excelApp != null)
{ excelApp.Quit();
{
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
}
worksheet = null;
@@ -1226,7 +1329,8 @@ namespace 精工涂胶检测项目
}
}
bool isbdNewFile = !File.Exists(bdexcelPath);
if (checkBox5.Checked==true) {
if (checkBox5.Checked == true)
{
try
{
excelApp = new Excel.Application();
@@ -1312,6 +1416,12 @@ namespace 精工涂胶检测项目
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>