This commit is contained in:
zhougf committed 2025-03-31 13:39:28 +08:00
1 parent 2b56f8e7d3
commit 60cafc4f8c
6 files changed
+384 -91

No files matched your search

+202 -52
View File
@@ -39,6 +39,10 @@ namespace WindowsFormsApp1
private Dictionary<int, System.Windows.Forms.Label> plcStatusLabels = new Dictionary<int, System.Windows.Forms.Label>();
private Dictionary<int, System.Windows.Forms.Panel> plcStatusIndicators = new Dictionary<int, System.Windows.Forms.Panel>();
// 在MainForm类的成员变量部分添加一个定时器
private System.Windows.Forms.Timer statusReportTimer = new System.Windows.Forms.Timer();
private ApiClient statusApiClient = new ApiClient(); // 专用于状态上报的API客户端
public MainForm()
{
InitializeComponent();
@@ -46,6 +50,10 @@ namespace WindowsFormsApp1
this.FormClosed += MainForm_FormClosed;
CheckForIllegalCrossThreadCalls = false;
// 初始化状态上报定时器
statusReportTimer.Interval = 15000; // 15秒
//statusReportTimer.Tick += StatusReportTimer_Tick;
}
private void Form1_Load(object sender, EventArgs e)
@@ -141,22 +149,32 @@ namespace WindowsFormsApp1
continue;
}
if(Plc.id == 1)
{
LOT号.Text = PlcData.Read_MaterialCode0;
Tray盘二维码.Text = PlcData.Read_MaterialCode1;
材料值.Text = PlcData.Read_MaterialCode2;
}
当前时间.Text = $"PLC状态:{plcstatus.GetName(typeof(plcstatus),PlcData.Read_Status)}";
DateTime Plc_Time = DateTime.Now;
//PLC链接状态.Text = "PLC链接状态:OK";
//PLC链接状态.BackColor = SystemColors.Control;
//PLC通讯时间.Text = "PLC通讯时间:" + Plc_Time.ToString("MM-dd HH:mm:ss");
// 更新对应PLC的状态指示器为绿色
//LogExecute.logNet.WriteError($"{Plc.plcname}");
UpdatePlcStatus(Plc.id, true);
#endregion
ApiClient apiClient = new ApiClient();
otherdata otherdata = new otherdata();
Init.SN = PlcData.Read_MaterialCode0;
//Init.SN = PlcData.Read_MaterialCode0;
#region 机台状态及报警
//机台状态
//机台状态.Text = "机台状态:" + Enum.GetName(typeof(plcstatus), Init.Plcstatus);
@@ -165,7 +183,7 @@ namespace WindowsFormsApp1
{
DateTime oldPlcstatus_LastTime = Init.Plcstatus_LastTime;
//状态+报警
if (oldstatus != PlcData.Read_Status || (Plc_Time - oldPlcstatus_LastTime).TotalSeconds >= 15)
if (oldstatus != PlcData.Read_Status)
{
//反馈状态
Init.Plcstatus = PlcData.Read_Status;
@@ -298,10 +316,44 @@ namespace WindowsFormsApp1
if (nowflow.dzscan)
{
//扫码
ScanForm_DZ scanForm = new ScanForm_DZ();
DialogResult scan_result = scanForm.ShowDialog();
ChangeBarcode();
var stationid = Init.t_Line.lineremark2;
// ccd扫码
if ("Assy1".Contains(stationid))
{
SQLiteHelper<t_keyvalue> sQLiteHelper_key = new db.SQLiteHelper<t_keyvalue>();
var key_sqlresult = sQLiteHelper_key.QueryAll();
var result1 = apiClient.CallFunctionByName("SN_FeedingCheck", PlcData, new otherdata() { JS_SN = PlcData.Read_MaterialCode2 });
if (result1.Success && result1.Mesresult == mesresult.PASS)
{
//继续调用接口
var result2 = apiClient.CallFunctionByName("Link_Comp", PlcData, new otherdata() { JS_SN = PlcData.Read_MaterialCode2 });
if (result2.Success && result2.Mesresult == mesresult.PASS)
{
// 写数据到 主页的barcode 数据库改,缓存改
var txt = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Barcode1");
txt.value = PlcData.Read_MaterialCode2;
var code = key_sqlresult.Data.First(x => x.key == txt.key);
code.value = PlcData.Read_MaterialCode2;
sQLiteHelper_key.Update(code);
}
else
{
LogExecute.logNet.WriteError($"{PlcData.Read_MaterialCode2}绑定NG");
}
}
else
{
LogExecute.logNet.WriteError($"{PlcData.Read_MaterialCode2}核对NG");
}
}
else
{
ScanForm_DZ scanForm = new ScanForm_DZ();
DialogResult scan_result = scanForm.ShowDialog();
}
ChangeBarcode();
var writeresult = siemensHelp.WriteAddress(plcaddress, PlcData);
if (!writeresult.Success)
{
@@ -521,7 +573,7 @@ namespace WindowsFormsApp1
#region 开始及结束处理按钮
private void 开始处理_Click(object sender, EventArgs e)
private async void 开始处理_Click(object sender, EventArgs e)
{
if (开始处理.Text == "开始处理")
{
@@ -690,31 +742,26 @@ namespace WindowsFormsApp1
}
else
{
foreach (var thread in Init.threadHelps)
// 禁用按钮,防止重复点击
开始处理.Enabled = false;
// 显示 Loading 窗体
LoadingForm loadingForm = new LoadingForm("正在停止处理...");
loadingForm.Show();
try
{
if (thread.Thread.IsAlive)
{
// 请求取消线程
thread.cancellationTokenSource1?.Cancel();
thread.Thread.Join(); // 等待线程完成
}
thread.isrunning1 = false;
}
// 异步清理资源
await Task.Run(() => CleanupResources());
if (Init.ScanPort != null && Init.ScanPort.IsOpen)
// 恢复界面
FormRecover();
}
finally
{
Init.ScanPort?.Close();
loadingForm.Close();
开始处理.Enabled = true;
}
if (Init.AirtightPort != null && Init.AirtightPort.IsOpen)
{
Init.AirtightPort?.Close();
}
//Thread.Sleep(500);
FormRecover();
LogExecute.logNet.WriteInfo($"用户{Init.t_User.username},停止处理!");
}
}
@@ -722,14 +769,95 @@ namespace WindowsFormsApp1
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
// 如果已经执行过清理,则直接返回
if (_isCleanupDone)
return;
//if (_isCleanupDone)
// return;
//try
//{
// _isCleanupDone = true; // 标记清理操作已执行
// // 给日志系统一点时间完成写入
// System.Threading.Thread.Sleep(100);
// // 停止所有线程
// if (Init.threadHelps != null)
// {
// foreach (var thread in Init.threadHelps)
// {
// try
// {
// if (thread.Thread != null && thread.Thread.IsAlive)
// {
// thread.cancellationTokenSource1?.Cancel();
// }
// }
// catch { /* 忽略异常 */ }
// }
// }
// // 安全关闭串口
// try
// {
// if (Init.ScanPort != null && Init.ScanPort.IsOpen)
// {
// Init.ScanPort.Close();
// Init.ScanPort = null;
// }
// }
// catch { /* 忽略异常 */ }
// try
// {
// if (Init.AirtightPort != null && Init.AirtightPort.IsOpen)
// {
// Init.AirtightPort.Close();
// Init.AirtightPort = null;
// }
// }
// catch { /* 忽略异常 */ }
//}
//catch { /* 忽略所有异常 */ }
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
// 设置窗体正在关闭标志
_isFormClosing = true;
// 如果当前正在处理中,则进行清理
if (开始处理.Text == "停止处理" && !_isCleanupDone)
{
e.Cancel = true; // 取消关闭
// 显示 Loading 窗体
LoadingForm loadingForm = new LoadingForm("正在关闭程序...");
loadingForm.Show();
Task.Run(() =>
{
try
{
_isCleanupDone = true;
CleanupResources();
}
finally
{
this.Invoke((Action)(() =>
{
loadingForm.Close();
this.Close(); // 重新触发关闭
}));
}
});
}
}
// 添加一个清理资源的私有方法
private void CleanupResources()
{
try
{
_isCleanupDone = true; // 标记清理操作已执行
// 给日志系统一点时间完成写入
System.Threading.Thread.Sleep(100);
// 停止状态上报定时器
statusReportTimer.Stop();
// 停止所有线程
if (Init.threadHelps != null)
{
@@ -740,7 +868,9 @@ namespace WindowsFormsApp1
if (thread.Thread != null && thread.Thread.IsAlive)
{
thread.cancellationTokenSource1?.Cancel();
thread.Thread.Join(); // 等待线程完成
}
thread.isrunning1 = false;
}
catch { /* 忽略异常 */ }
}
@@ -766,29 +896,15 @@ namespace WindowsFormsApp1
}
}
catch { /* 忽略异常 */ }
// 记录日志
LogExecute.logNet.WriteInfo($"用户{Init.t_User.username},停止处理!");
// 给日志系统一点时间完成写入
System.Threading.Thread.Sleep(100);
}
catch { /* 忽略所有异常 */ }
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
// 设置窗体正在关闭标志
_isFormClosing = true;
// 如果当前正在处理中,则记录日志
if (开始处理.Text == "停止处理")
{
try
{
LogExecute.logNet.WriteInfo($"用户{Init.t_User.username},程序关闭时自动停止处理!");
// 给日志系统一点时间完成写入
System.Threading.Thread.Sleep(100);
}
catch { /* 忽略异常 */ }
}
}
#endregion
#region 页面按钮-跳转
private void 扭力角度扭矩设置ToolStripMenuItem_Click(object sender, EventArgs e)
@@ -1117,7 +1233,7 @@ namespace WindowsFormsApp1
// 设置事件结果
resultLabel.Text = $"{(result == mesresult.PASS ? "PASS" : "FAIL")}";
resultLabel.ForeColor = result == mesresult.PASS ? Color.Green : Color.Red;
resultLabel.BackColor = result == mesresult.PASS ? Color.Green : Color.Red;
resultLabel.Visible = true;
// 设置事件时间
@@ -1285,6 +1401,40 @@ namespace WindowsFormsApp1
}
#endregion
// 添加状态上报定时器的事件处理方法
private void StatusReportTimer_Tick(object sender, EventArgs e)
{
try
{
// 如果没有PLC数据或者状态为0,则不上报
if (Init.Plcstatus == 0)
return;
otherdata statusOtherData = new otherdata();
statusOtherData.Plc_Status = Init.Plcstatus;
var statusResult = statusApiClient.CallFunctionByName("Status", null, statusOtherData);
if (statusResult.Success)
{
MES_MsgfORM("Status", statusResult, statusResult.Requesttime);
// 更新最后上报时间
Init.Plcstatus_LastTime = DateTime.Now;
LogExecute.logNet.WriteDebug($"定时上报机台状态成功: {Init.Plcstatus}");
}
else
{
//MES链接状态.Text = "MES链接状态:NG";
//MES链接状态.BackColor = Color.Red;
LogExecute.logNet.WriteError($"定时上报机台状态失败: MES通讯异常!");
}
}
catch (Exception ex)
{
LogExecute.logNet.WriteError($"定时上报机台状态异常: {ex.Message}");
}
}
}
}