Compare commits

...

2 Commits

Author SHA1 Message Date
2c0e5217bc 完全替换掉System.Windows.Forms.Timer 2026-05-08 11:48:53 +08:00
f7cb1c10a5 RenzYi Modified 2026-05-08 10:56:48 +08:00

View File

@@ -29,7 +29,9 @@ using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement; using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Excel = Microsoft.Office.Interop.Excel; using Excel = Microsoft.Office.Interop.Excel;
using Timer = System.Windows.Forms.Timer; //≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
//using Timer = System.Windows.Forms.Timer;
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【END】
using System.Timers; using System.Timers;
namespace namespace
{ {
@@ -108,13 +110,25 @@ namespace 精工涂胶检测项目
CogPMAlignTool mPM; CogPMAlignTool mPM;
CogFixtureTool mFIX; CogFixtureTool mFIX;
int currentStatus; int currentStatus;
private Timer inactivityTimer; #region inactivityTimer System.Windows.Forms.Timer System.Timers.Timer 0BEGIN
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
//private Timer inactivityTimer;
private System.Timers.Timer mInactivityTimer;
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【END】
#endregion inactivityTimer System.Windows.Forms.Timer System.Timers.Timer 0END
FormLogin formLogin; FormLogin formLogin;
private MyMessageFilter messageFilter; private MyMessageFilter messageFilter;
public ModbusTcpClient modbusClient; // 新增Modbus客户端实例 public ModbusTcpClient modbusClient; // 新增Modbus客户端实例
private Timer modbusPollTimer; // 新增轮询定时器
private Timer modbusHeartbeat;
#region modbusPollTimer System.Windows.Forms.Timer System.Timers.Timer 0BEGIN
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
//private Timer modbusPollTimer; // 新增轮询定时器
private System.Timers.Timer mModbusPollTimer;
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【END】
#endregion modbusPollTimer System.Windows.Forms.Timer System.Timers.Timer 0END
private System.Timers.Timer modbusHeartbeat;
public Plc plc; public Plc plc;
public bool _autoModbusReconnect = true; public bool _autoModbusReconnect = true;
@@ -221,20 +235,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;
} }
} }
@@ -302,6 +328,8 @@ namespace 精工涂胶检测项目
// 再次回收(清理等待终结后的对象) // 再次回收(清理等待终结后的对象)
GC.Collect(); GC.Collect();
} }
#region modbusPollTimer System.Windows.Forms.Timer System.Timers.Timer 1BEGIN
private void StartModbusPolling() private void StartModbusPolling()
{ // 创建定时器3000 毫秒 = 3 秒 { // 创建定时器3000 毫秒 = 3 秒
_gcTimer = new System.Timers.Timer(3000); _gcTimer = new System.Timers.Timer(3000);
@@ -314,11 +342,19 @@ namespace 精工涂胶检测项目
// 启动定时器 // 启动定时器
_gcTimer.Start(); _gcTimer.Start();
if (modbusPollTimer == null)
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
//if (modbusPollTimer == null)
if (mModbusPollTimer == null)
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【END】
{ {
Vpro初始化(); Vpro初始化();
modbusPollTimer = new Timer { Interval = 1000 }; //≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
modbusPollTimer.Tick += async (s, e) => //modbusPollTimer = new Timer { Interval = 1000 };
//modbusPollTimer.Tick += async (s, e) =>
mModbusPollTimer = new System.Timers.Timer(1000);
mModbusPollTimer.Elapsed += async (s, e) =>
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【END】
{ {
if (!modbusClient.IsConnected) return; if (!modbusClient.IsConnected) return;
@@ -357,7 +393,6 @@ namespace 精工涂胶检测项目
} }
} }
if (trigger == "\u0000" || trigger == "") if (trigger == "\u0000" || trigger == "")
{ {
AddLog("相机收到PLC结束触发信号", 0); AddLog("相机收到PLC结束触发信号", 0);
@@ -365,7 +400,6 @@ namespace 精工涂胶检测项目
AddLog("给PLC发送ResultAcq置零信号", 0); AddLog("给PLC发送ResultAcq置零信号", 0);
} }
//触发标定程序 //触发标定程序
if (trigger == "\u0002") if (trigger == "\u0002")
{ {
@@ -381,8 +415,6 @@ namespace 精工涂胶检测项目
cog3DDisplayV2WF1.Clear(); cog3DDisplayV2WF1.Clear();
} }
} }
} }
if (resultHeartBeat.IsSuccess) if (resultHeartBeat.IsSuccess)
@@ -424,12 +456,23 @@ namespace 精工涂胶检测项目
}; };
} }
if (!modbusPollTimer.Enabled) //≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
//if (!modbusPollTimer.Enabled)
//{
// modbusPollTimer.Start();
// AddLog("Modbus实时读取已启动", 0);
//}
if (!mModbusPollTimer.Enabled)
{ {
modbusPollTimer.Start(); mModbusPollTimer.Start();
AddLog("Modbus实时读取已启动", 0); AddLog("Modbus实时读取已启动", 0);
} }
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【END】
} }
#endregion modbusPollTimer System.Windows.Forms.Timer System.Timers.Timer 1END
public string ConvertShortsToAsciiString(short[] shorts, bool isLittleEndian = false) public string ConvertShortsToAsciiString(short[] shorts, bool isLittleEndian = false)
{ {
var bytes = new List<byte>(); var bytes = new List<byte>();
@@ -468,18 +511,30 @@ namespace 精工涂胶检测项目
{ {
} }
#region inactivityTimer System.Windows.Forms.Timer System.Timers.Timer 1BEGIN
private void () private void ()
{ {
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
// 初始化不活动计时器 // 初始化不活动计时器
// 初始化不活动计时器 //inactivityTimer = new Timer();
inactivityTimer = new Timer(); //inactivityTimer.Interval = 300000; // 5秒【这是5秒为啥写300秒到底是多少】
inactivityTimer.Interval = 300000; // 5秒 //inactivityTimer.Tick += InactivityTimer_Tick;
inactivityTimer.Tick += InactivityTimer_Tick;
mInactivityTimer = new System.Timers.Timer(5000); //5秒
mInactivityTimer.Elapsed += InactivityTimer_Elapsed;
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【END】
// 初始化消息过滤器 // 初始化消息过滤器
messageFilter = new MyMessageFilter(); messageFilter = new MyMessageFilter();
Application.AddMessageFilter(messageFilter); Application.AddMessageFilter(messageFilter);
messageFilter.ActivityDetected += MessageFilter_ActivityDetected; messageFilter.ActivityDetected += MessageFilter_ActivityDetected;
} }
#endregion inactivityTimer System.Windows.Forms.Timer System.Timers.Timer 1END
private void Tcp事件注册() private void Tcp事件注册()
{ {
//事件注册 //事件注册
@@ -526,36 +581,68 @@ namespace 精工涂胶检测项目
myJobManager.UserResultAvailable += MyJobManager_UserResultAvailable; myJobManager.UserResultAvailable += MyJobManager_UserResultAvailable;
} }
#region modbusPollTimer System.Windows.Forms.Timer System.Timers.Timer 2BEGIN
private void StopModbusPolling() private void StopModbusPolling()
{ {
if (modbusPollTimer != null && modbusPollTimer.Enabled) //≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
//if (modbusPollTimer != null && modbusPollTimer.Enabled)
//{
// modbusPollTimer.Stop();
// AddLog("Modbus断线已停止实时读取", 1);
//}
if (mModbusPollTimer != null && mModbusPollTimer.Enabled)
{ {
modbusPollTimer.Stop(); mModbusPollTimer.Stop();
AddLog("Modbus断线已停止实时读取", 1); AddLog("Modbus断线已停止实时读取", 1);
} }
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【END】
} }
#endregion modbusPollTimer System.Windows.Forms.Timer System.Timers.Timer 2END
private void _modbusClient_Disconnected(object sender, string e) private void _modbusClient_Disconnected(object sender, string e)
{ {
AddLog($"PLC连接断开{e}", 1); AddLog($"PLC连接断开{e}", 1);
} }
private void InactivityTimer_Tick(object sender, EventArgs e)
#region inactivityTimer System.Windows.Forms.Timer System.Timers.Timer 2BEGIN
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
//private void InactivityTimer_Tick(object sender, EventArgs e)
//{
// if (currentStatus == 3)
// {
// SetMainEnabled(0, "登录");
// inactivityTimer.Stop();
// }
//}
private void InactivityTimer_Elapsed(object sender, ElapsedEventArgs e)
{ {
if (currentStatus == 3) if (currentStatus == 3)
{ {
SetMainEnabled(0, "登录"); SetMainEnabled(0, "登录");
inactivityTimer.Stop(); mInactivityTimer.Stop();
} }
} }
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【END】
private void MessageFilter_ActivityDetected(object sender, EventArgs e) private void MessageFilter_ActivityDetected(object sender, EventArgs e)
{ {
if (currentStatus == 3) // 仅admin状态重置计时器 if (currentStatus == 3) // 仅admin状态重置计时器
{ {
inactivityTimer.Stop(); //≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
inactivityTimer.Start(); //inactivityTimer.Stop();
//inactivityTimer.Start();
mInactivityTimer.Stop();
mInactivityTimer.Start();
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【END】
} }
} }
#endregion inactivityTimer System.Windows.Forms.Timer System.Timers.Timer 2END
/// <summary> /// <summary>
/// 程序关闭方法 /// 程序关闭方法
@@ -598,6 +685,8 @@ namespace 精工涂胶检测项目
} }
} }
private void MyJobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e) private void MyJobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e)
{
try
{ {
///待会儿需要改通过ToolBlock是否报错来判断。 ///待会儿需要改通过ToolBlock是否报错来判断。
//结果处理代码 //结果处理代码
@@ -626,13 +715,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 +778,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 +813,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 +835,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 +891,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 +916,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 +949,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 +978,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 +986,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 +1013,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 +1040,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 +1140,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 +1295,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 +1312,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 +1334,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 +1383,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 +1404,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 +1491,12 @@ namespace 精工涂胶检测项目
} }
} }
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary> /// <summary>
@@ -1570,6 +1755,8 @@ namespace 精工涂胶检测项目
public void SetMainEnabled(int status, string account) public void SetMainEnabled(int status, string account)
{ {
currentStatus = status; // 更新当前状态 currentStatus = status; // 更新当前状态
#region inactivityTimer System.Windows.Forms.Timer System.Timers.Timer 3BEGIN
switch (status) switch (status)
{ {
case 0: case 0:
@@ -1582,7 +1769,10 @@ namespace 精工涂胶检测项目
this.ToolStripMenuItem1.Text = "登录"; this.ToolStripMenuItem1.Text = "登录";
this.退ToolStripMenuItem.Enabled = false; this.退ToolStripMenuItem.Enabled = false;
this.btn_Auto.Enabled = false; this.btn_Auto.Enabled = false;
inactivityTimer.Stop(); //≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
//inactivityTimer.Stop();
mInactivityTimer.Stop();
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【END】
break; break;
case 1: case 1:
this.ToolStripMenuItem.Enabled = true; this.ToolStripMenuItem.Enabled = true;
@@ -1618,9 +1808,13 @@ namespace 精工涂胶检测项目
this.ToolStripMenuItem1.Enabled = false; this.ToolStripMenuItem1.Enabled = false;
this.退ToolStripMenuItem.Enabled = true; this.退ToolStripMenuItem.Enabled = true;
this.btn_Auto.Enabled = true; this.btn_Auto.Enabled = true;
inactivityTimer.Start(); //≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
//inactivityTimer.Start();
mInactivityTimer.Start();
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【END】
break; break;
} }
#endregion inactivityTimer System.Windows.Forms.Timer System.Timers.Timer 3END
} }
private void ToolStripMenuItem_Click(object sender, EventArgs e) private void ToolStripMenuItem_Click(object sender, EventArgs e)