csv 页面等

This commit is contained in:
zhougf committed 2025-03-29 11:46:48 +08:00
1 parent 57299c0b55
commit 2b56f8e7d3
17 files changed
+923 -794

No files matched your search

+420 -238
View File
@@ -23,14 +23,28 @@ using WindowsFormsApp1.mod.server;
using WindowsFormsApp1.server;
using static SQLite.SQLite3;
using static WindowsFormsApp1.server.ApiClient;
using Label = System.Windows.Forms.Label;
namespace WindowsFormsApp1
{
public partial class MainForm : Form
{
// 添加一个标志变量,用于标记是否已经执行了清理操作
private bool _isCleanupDone = false;
private bool _isFormClosing = false; // 添加标志,表示窗体正在关闭
// 添加一个静态变量来跟踪下一个要使用的槽位
private static int _nextEventSlot = 1;
// 在MainForm类中添加一个字典,用于存储PLC状态控件
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>();
public MainForm()
{
InitializeComponent();
// 添加FormClosed事件处理程序
this.FormClosed += MainForm_FormClosed;
CheckForIllegalCrossThreadCalls = false;
}
@@ -55,6 +69,14 @@ namespace WindowsFormsApp1
//lineremark7.Text = "";
操作人员ID.Text = Init.t_User.username;
// 添加窗体关闭事件处理
this.FormClosing += MainForm_FormClosing;
// 自动开始处理
开始处理_Click(this, EventArgs.Empty);
}
@@ -84,7 +106,10 @@ namespace WindowsFormsApp1
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
// 更新对应PLC的状态指示器为红色
UpdatePlcStatus(Plc.id, false);
LogExecute.logNet.WriteError($"{Plc.plcip}建立S7连接失败");
continue;
}
@@ -101,7 +126,6 @@ namespace WindowsFormsApp1
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}读取全部地址处理错误:处理停止");
continue;
}
@@ -113,12 +137,10 @@ namespace WindowsFormsApp1
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}写入心跳错误:处理停止");
continue;
}
LOT号.Text = PlcData.Read_MaterialCode0;
Tray盘二维码.Text = PlcData.Read_MaterialCode1;
@@ -127,7 +149,9 @@ namespace WindowsFormsApp1
//PLC链接状态.Text = "PLC链接状态:OK";
//PLC链接状态.BackColor = SystemColors.Control;
//PLC通讯时间.Text = "PLC通讯时间:" + Plc_Time.ToString("MM-dd HH:mm:ss");
PLC_Form(Plc, plcstate.OK, Plc_Time);
// 更新对应PLC的状态指示器为绿色
UpdatePlcStatus(Plc.id, true);
#endregion
ApiClient apiClient = new ApiClient();
@@ -150,10 +174,10 @@ namespace WindowsFormsApp1
var statusresult = apiClient.CallFunctionByName("Status", PlcData, otherdata);
if (statusresult.Success)
{
MES链接状态.Text = "MES链接状态:OK";
MES链接状态.BackColor = SystemColors.Control;
MES通讯时间.Text = "MES通讯时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss");
MES_MsgfORM("Status", statusresult.Mesresult, statusresult.Requesttime);
//MES链接状态.Text = "MES链接状态:OK";
//MES链接状态.BackColor = SystemColors.Control;
//MES通讯时间.Text = "MES通讯时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss");
MES_MsgfORM("Status", statusresult, statusresult.Requesttime);
if (statusresult.Mesresult != mesresult.PASS)
{
PlcData.Write_Status = 1001;
@@ -163,8 +187,8 @@ namespace WindowsFormsApp1
}
else
{
MES链接状态.Text = "MES链接状态:NG";
MES链接状态.BackColor = Color.Red;
//MES链接状态.Text = "MES链接状态:NG";
//MES链接状态.BackColor = Color.Red;
LogExecute.logNet.WriteError($"MES通讯异常!");
PlcData.Write_Status = 1002;
var errresult = siemensHelp.WriteAddress(plcaddress, PlcData);
@@ -180,7 +204,7 @@ namespace WindowsFormsApp1
{
//反馈报警
List<string> list = new List<string>();
///////////////////////////////测试用
///////////////////////////////测试用
list.Add("M03CVSO-01");
otherdata.Plc_ErrorMsgs = list;
Init.Plcstatus = PlcData.Read_Status;
@@ -188,10 +212,10 @@ namespace WindowsFormsApp1
var statusresult = apiClient.CallFunctionByName("Alarm", PlcData, otherdata);
if (statusresult.Success)
{
MES链接状态.Text = "MES链接状态:OK";
MES链接状态.BackColor = SystemColors.Control;
MES通讯时间.Text = "MES通讯时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss");
MES_MsgfORM("Alarm", statusresult.Mesresult, statusresult.Requesttime);
//MES链接状态.Text = "MES链接状态:OK";
//MES链接状态.BackColor = SystemColors.Control;
//MES通讯时间.Text = "MES通讯时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss");
MES_MsgfORM("Alarm", statusresult, statusresult.Requesttime);
if (statusresult.Mesresult != mesresult.PASS)
{
PlcData.Write_Status = 1001;
@@ -201,8 +225,8 @@ namespace WindowsFormsApp1
}
else
{
MES链接状态.Text = "MES链接状态:NG";
MES链接状态.BackColor = Color.Red;
//MES链接状态.Text = "MES链接状态:NG";
//MES链接状态.BackColor = Color.Red;
LogExecute.logNet.WriteError($"MES通讯异常!");
PlcData.Write_Status = 1002;
siemensHelp.WriteAddress(plcaddress, PlcData);
@@ -232,7 +256,6 @@ namespace WindowsFormsApp1
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}写入地址处理错误:处理停止");
}
continue;
@@ -251,7 +274,7 @@ namespace WindowsFormsApp1
LogExecute.logNet.WriteError($"未找到当前期望接口ID {nowflow.hopeinterfaceid},对应的接口!");
continue;
}
#endregion
#region 根据流程,判断是否需要扫码、气密、扭力等
if (nowflow.scan)
@@ -263,7 +286,8 @@ namespace WindowsFormsApp1
{
string returnValue = scanForm.ReturnValue;
otherdata.JS_SN = returnValue;
//Init.SN = returnValue;
LOT号.Text = returnValue;
Init.ScanString = returnValue;
}
else
@@ -283,15 +307,14 @@ namespace WindowsFormsApp1
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}写入地址处理错误:处理停止");
continue;
}
else
{
MES链接状态.Text = "MES链接状态:OK";
MES链接状态.BackColor = SystemColors.Control;
MES通讯时间.Text = "MES通讯时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss");
//MES链接状态.Text = "MES链接状态:OK";
//MES链接状态.BackColor = SystemColors.Control;
//MES通讯时间.Text = "MES通讯时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss");
}
continue;
@@ -315,7 +338,6 @@ namespace WindowsFormsApp1
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}压力曲线数据地址处理错误:处理停止");
continue;
}
@@ -333,7 +355,6 @@ namespace WindowsFormsApp1
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}螺丝枪扭力数据地址处理错误:处理停止");
continue;
}
@@ -352,7 +373,6 @@ namespace WindowsFormsApp1
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}螺丝枪角度数据地址处理错误:处理停止");
continue;
}
@@ -363,7 +383,8 @@ namespace WindowsFormsApp1
//文件上传
}
#endregion
// NG料进站不判断,进站MES返回FAIL,记录。该站其他事件不触发MES,直接跳过
//if()
#region 判断物料调用次数 固化站
if (Init.t_Line.lineremark2 == "Curing")
{
@@ -388,7 +409,7 @@ namespace WindowsFormsApp1
materialCall.checkin1 = DateTime.Now;
shouldCallApi = true;
}
else if (!materialCall.checkout3.HasValue
else if (!materialCall.checkout3.HasValue
&& materialCall.checkout2.HasValue
&& materialCall.checkout1.HasValue
&& nowinterface.code.Contains("Checkout"))
@@ -406,6 +427,7 @@ namespace WindowsFormsApp1
materialCall.checkin2 = DateTime.Now;
else if (!materialCall.checkin3.HasValue)
materialCall.checkin3 = DateTime.Now;
PlcData.Write_Status = PLC_Status.SN_CheckIN;
}
else if (nowinterface.code.Contains("SN_Checkout"))
{
@@ -413,6 +435,7 @@ namespace WindowsFormsApp1
materialCall.checkout1 = DateTime.Now;
else if (!materialCall.checkout2.HasValue)
materialCall.checkout2 = DateTime.Now;
PlcData.Write_Status = PLC_Status.SN_Checkout;
}
}
materialCallHelper.Update(materialCall);
@@ -427,7 +450,6 @@ namespace WindowsFormsApp1
var writeresult = siemensHelp.WriteAddress(plcaddress, PlcData);
if (!writeresult.Success)
{
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}写入地址处理错误:处理停止");
continue;
}
@@ -436,12 +458,17 @@ namespace WindowsFormsApp1
}
#endregion
//文件路径 列表
otherdata.FileUploadAddress = Init.t_Keyvalues.First(x => x.key == "UploadPath").value;
otherdata.ImageAddress = Init.t_Keyvalues.First(x => x.key == "ImageFilePath").value;
otherdata.VedioAddress = Init.t_Keyvalues.First(x => x.key == "VedioFilePath").value;
#region 调用后端接口
var result = apiClient.CallFunctionByName(nowinterface.code, PlcData, otherdata);
if (result.IsShow)
{
MES提示信息.Visible = true;
MES提示信息.Text = DateTime.Now.ToString("HH:mm:ss") + ":" + result.Msg;
MES_MsgfORM(nowinterface.code, result, result.Requesttime);
}
if (result.Success)
@@ -451,18 +478,17 @@ namespace WindowsFormsApp1
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}写入地址处理错误:处理停止");
continue;
}
else
{
ChangeBarcode();
MES_MsgfORM(nowinterface.code, result.Mesresult, result.Requesttime);
MES_MsgfORM(nowinterface.code, result, result.Requesttime);
MES链接状态.Text = "MES链接状态:OK";
MES链接状态.BackColor = SystemColors.Control;
MES通讯时间.Text = "MES通讯时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss");
//MES链接状态.Text = "MES链接状态:OK";
//MES链接状态.BackColor = SystemColors.Control;
//MES通讯时间.Text = "MES通讯时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss");
if (result.Mesresult != mesresult.PASS)
{
PlcData.Write_Status = 1001;
@@ -473,8 +499,8 @@ namespace WindowsFormsApp1
}
else
{
MES链接状态.Text = "MES链接状态:NG";
MES链接状态.BackColor = Color.Red;
//MES链接状态.Text = "MES链接状态:NG";
//MES链接状态.BackColor = Color.Red;
LogExecute.logNet.WriteError($"MES通讯异常!");
PlcData.Write_Status = 1002;
siemensHelp.WriteAddress(plcaddress, PlcData);
@@ -501,12 +527,13 @@ namespace WindowsFormsApp1
{
SQLiteHelper<t_plc> sQLiteHelper_plc = new db.SQLiteHelper<t_plc>();
var plc_sqlresult = sQLiteHelper_plc.QuerySingle(a=>a.id == Init.t_Line.plcid);
var plc_sqlresult = sQLiteHelper_plc.QueryAll();
if (!plc_sqlresult.Success)
{
MessageBox.Show("无法初始化PLC信息,无法开始处理!");
return;
}
var plc_result = plc_sqlresult.Data.FindAll(a => Init.t_Lineplcs.Select(p => p.plcid).Contains(a.id));
SQLiteHelper<t_keyvalue> sQLiteHelper_key = new db.SQLiteHelper<t_keyvalue>();
var key_sqlresult = sQLiteHelper_key.QueryAll();
@@ -583,6 +610,9 @@ namespace WindowsFormsApp1
Init.t_Pressurecurves = pressurecurve_result.Data;
Init.t_Interfaces = interface_result.Data;
Init.t_Keyvalues = key_sqlresult.Data;
// 初始化PLC状态面板
InitializePlcStatusPanel();
#endregion
#region 初始化COM口服务
try
@@ -640,17 +670,19 @@ namespace WindowsFormsApp1
#endregion
Init.threadHelps = new List<ThreadHelp>();
var plc = plc_sqlresult.Data;
plc.plchelp = new SiemensHelp(plc.plcip, plc.timeout);
ThreadHelp threadHelp = new ThreadHelp();
if (!threadHelp.isrunning1)
foreach (var plc in plc_result)
{
threadHelp.cancellationTokenSource1 = new CancellationTokenSource();
threadHelp.Thread = new Thread(() => build(plc, threadHelp.cancellationTokenSource1.Token));
threadHelp.Thread.Start();
threadHelp.isrunning1 = true;
plc.plchelp = new SiemensHelp(plc.plcip, plc.timeout);
ThreadHelp threadHelp = new ThreadHelp();
if (!threadHelp.isrunning1)
{
threadHelp.cancellationTokenSource1 = new CancellationTokenSource();
threadHelp.Thread = new Thread(() => build(plc, threadHelp.cancellationTokenSource1.Token));
threadHelp.Thread.Start();
threadHelp.isrunning1 = true;
}
Init.threadHelps.Add(threadHelp);
}
Init.threadHelps.Add(threadHelp);
开始处理.Text = "停止处理";
开始处理.BackColor = Color.Red;
@@ -685,6 +717,78 @@ namespace WindowsFormsApp1
LogExecute.logNet.WriteInfo($"用户{Init.t_User.username},停止处理!");
}
}
// 添加FormClosed事件处理程序
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
// 如果已经执行过清理,则直接返回
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 == "停止处理")
{
try
{
LogExecute.logNet.WriteInfo($"用户{Init.t_User.username},程序关闭时自动停止处理!");
// 给日志系统一点时间完成写入
System.Threading.Thread.Sleep(100);
}
catch { /* 忽略异常 */ }
}
}
#endregion
#region 页面按钮-跳转
private void 扭力角度扭矩设置ToolStripMenuItem_Click(object sender, EventArgs e)
@@ -771,46 +875,77 @@ namespace WindowsFormsApp1
#region 页面-布局、日志等
private void LogNet_BeforeSaveToFile(object sender, HslEventArgs e)
{
lock (this)
// 如果窗体正在关闭,则不再尝试更新UI
if (_isFormClosing)
{
DateTime time = e.HslMessage.Time;//获取时间
string text = e.HslMessage.Text;//日志文本
int textLength = richTextBox1.TextLength;
int lineCount = richTextBox1.GetLineFromCharIndex(textLength);
if (lineCount >= 200)
{
richTextBox1.Clear();
}
if (e.HslMessage.Degree == HslMessageDegree.ERROR)
{
richTextBox1.SelectionColor = Color.Red;
richTextBox1.AppendText("[错误] " + time.ToString() + " | " + text + "\n");
}
else if (e.HslMessage.Degree == HslMessageDegree.DEBUG)
{
richTextBox1.SelectionColor = Color.Green;
richTextBox1.AppendText("[日志] " + time.ToString() + " | " + text + "\n");
}
else if (e.HslMessage.Degree == HslMessageDegree.WARN)
{
richTextBox1.SelectionColor = Color.Orange;
richTextBox1.AppendText("[警告] " + time.ToString() + " | " + text + "\n");
}
else if (e.HslMessage.Degree == HslMessageDegree.INFO)
{
richTextBox1.SelectionColor = Color.Orange;
richTextBox1.AppendText("[提示] " + time.ToString() + " | " + text + "\n");
}
else
{
richTextBox1.SelectionColor = Color.Red;
richTextBox1.AppendText("[未知] " + time.ToString() + " | " + text + "\n");
}
// 仅写入文件日志,不更新UI
return;
}
}
try
{
// 使用 BeginInvoke 而不是直接在锁内操作 UI 控件,减少 UI 线程阻塞
this.BeginInvoke(new Action(() =>
{
DateTime time = e.HslMessage.Time;
string text = e.HslMessage.Text;
// 使用 StringBuilder 构建日志文本,提高性能
StringBuilder logBuilder = new StringBuilder();
string logPrefix;
Color logColor;
// 根据日志级别设置前缀和颜色
switch (e.HslMessage.Degree)
{
case HslMessageDegree.ERROR:
logPrefix = "[错误] ";
logColor = Color.Red;
break;
case HslMessageDegree.DEBUG:
logPrefix = "[日志] ";
logColor = Color.Green;
break;
case HslMessageDegree.WARN:
case HslMessageDegree.INFO:
logPrefix = e.HslMessage.Degree == HslMessageDegree.WARN ? "[警告] " : "[提示] ";
logColor = Color.Orange;
break;
default:
logPrefix = "[未知] ";
logColor = Color.Red;
break;
}
logBuilder.Append(logPrefix).Append(time.ToString()).Append(" | ").Append(text).Append("\n");
// 检查日志行数,如果超过阈值则清理
int lineCount = richTextBox1.GetLineFromCharIndex(richTextBox1.TextLength);
if (lineCount >= 200)
{
// 优化:只保留后 100 行,而不是全部清空再重新添加
int startIndex = richTextBox1.GetFirstCharIndexFromLine(lineCount - 100);
if (startIndex > 0)
{
richTextBox1.Select(0, startIndex);
richTextBox1.SelectedText = "";
}
}
// 添加新日志
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.SelectionLength = 0;
richTextBox1.SelectionColor = logColor;
richTextBox1.AppendText(logBuilder.ToString());
// 重置颜色并滚动到底部
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.SelectionLength = 0;
richTextBox1.SelectionColor = richTextBox1.ForeColor;
richTextBox1.ScrollToCaret();
}));
}
catch { /* 忽略异常 */ }
}
private void timer1_Tick(object sender, EventArgs e)
{
当前时间.Text = "当前时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss");
@@ -820,41 +955,19 @@ namespace WindowsFormsApp1
开始处理.Text = "开始处理";
开始处理.BackColor = Color.Green;
MES链接状态.Text = "MES链接状态:None";
MES链接状态.BackColor = SystemColors.Control;
//MES链接状态.Text = "MES链接状态:None";
//MES链接状态.BackColor = SystemColors.Control;
MES通讯时间.Text = "MES通讯时间:None";
MES通讯时间.BackColor = SystemColors.Control;
//MES通讯时间.Text = "MES通讯时间:None";
//MES通讯时间.BackColor = SystemColors.Control;
PLC1名称.Visible = false;
PLC1状态.Visible = false;
PLC1机台状态.Visible = false;
PLC1时间.Visible = false;
//胶水上下线.Enabled = false;
MES提示信息.Visible = false;
LOT号.Visible = false;
Tray盘二维码.Visible = false;
//MES提示信息.Visible = false;
//LOT号.Visible = false;
//Tray盘二维码.Visible = false;
事件1.Visible = false;
事件1结果.Visible = false;
事件1时间.Visible = false;
事件2.Visible = false;
事件2结果.Visible = false;
事件2时间.Visible = false;
事件3.Visible = false;
事件3结果.Visible = false;
事件3时间.Visible = false;
事件4.Visible = false;
事件4结果.Visible = false;
事件4时间.Visible = false;
底部注释1.Visible = false;
底部注释2.Visible = false;
@@ -864,6 +977,12 @@ namespace WindowsFormsApp1
剩余寿命.Visible = false;
// 初始化所有PLC状态指示器为灰色(未连接状态)
foreach (var indicator in plcStatusIndicators.Values)
{
indicator.BackColor = Color.Gray;
}
}
public void ChangeBarcode()
@@ -871,148 +990,211 @@ namespace WindowsFormsApp1
剩余寿命.Visible = true;
var power = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Power");
剩余寿命.Text = power == null ? "": power.value;
剩余寿命.Text = power == null ? "" : power.value;
}
public void PLC_Form(t_plc plc, plcstate state, DateTime time)
// 添加初始化PLC状态面板的方法
private void InitializePlcStatusPanel()
{
string plcname = plc.plcip;
string datetime = time.ToString("HH:mm:ss");
PLC1名称.Visible = true;
PLC1状态.Visible = true;
PLC1机台状态.Visible = true;
PLC1时间.Visible = true;
PLC1名称.Text = plcname;
PLC1状态.Text = state.ToString();
PLC1机台状态.Text = Enum.GetName(typeof(plcstatus), Init.Plcstatus);
PLC1时间.Text = datetime;
if (PLC1状态.Text == plcstate.NG.ToString())
// 假设我们已经有一个名为plcStatusGroupBox的GroupBox控件
// 和一个名为plcStatusTableLayout的TableLayoutPanel控件
// 清空现有控件
plcStatusTableLayout.Controls.Clear();
plcStatusLabels.Clear();
plcStatusIndicators.Clear();
// 设置TableLayoutPanel的行列数
int plcCount = Init.t_Lineplcs?.Count ?? 0;
// 修改为横向布局:每列显示一个PLC,每个PLC占用一行两列
plcStatusTableLayout.ColumnCount = plcCount * 2; // 每个PLC需要2列
plcStatusTableLayout.RowCount = 1; // 只需要1行
// 设置列样式
plcStatusTableLayout.ColumnStyles.Clear();
for (int i = 0; i < plcCount; i++)
{
PLC1状态.BackColor = Color.Red;
// 为每个PLC添加两列:一列放指示灯,一列放标签
plcStatusTableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 30F)); // 状态指示灯列
plcStatusTableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 150F)); // PLC信息列
}
else
// 设置行样式
plcStatusTableLayout.RowStyles.Clear();
plcStatusTableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 25F));
// 如果没有PLC,显示提示信息
if (plcCount == 0)
{
PLC1状态.BackColor = SystemColors.Control;
var noPlcLabel = new System.Windows.Forms.Label();
noPlcLabel.Text = "未配置PLC";
noPlcLabel.Dock = DockStyle.Fill;
noPlcLabel.TextAlign = ContentAlignment.MiddleCenter;
plcStatusTableLayout.Controls.Add(noPlcLabel, 0, 0);
plcStatusTableLayout.SetColumnSpan(noPlcLabel, 2);
return;
}
if (PLC1机台状态.Text == plcstatus.停机.ToString())
// 为每个PLC创建状态指示器
int colIndex = 0;
foreach (var linePlc in Init.t_Lineplcs)
{
PLC1机台状态.BackColor = Color.Red;
}
else
{
PLC1机台状态.BackColor = SystemColors.Control;
// 获取PLC信息
var plc = Init.t_Plcaddresses.FirstOrDefault(p => p.plcid == linePlc.plcid);
if (plc == null) continue;
// 创建状态指示灯(一个小Panel)
Panel statusIndicator = new Panel();
statusIndicator.Size = new Size(16, 16);
statusIndicator.BackColor = Color.Gray; // 初始为灰色
statusIndicator.Margin = new Padding(5);
// 创建PLC名称标签
Label plcLabel = new Label();
plcLabel.Text = $"PLC-{plc.plcid}:{plc.dbaddress}";
plcLabel.Dock = DockStyle.Fill;
plcLabel.TextAlign = ContentAlignment.MiddleLeft;
// 添加到TableLayoutPanel(使用列索引)
plcStatusTableLayout.Controls.Add(statusIndicator, colIndex, 0); // 状态指示灯放在第一列
plcStatusTableLayout.Controls.Add(plcLabel, colIndex + 1, 0); // 标签放在第二列
// 保存引用以便后续更新
plcStatusIndicators[linePlc.plcid] = statusIndicator;
plcStatusLabels[linePlc.plcid] = plcLabel;
colIndex += 2; // 每个PLC占用两列,所以索引加2
}
}
public void MES_MsgfORM(string name, mesresult result, DateTime dateTime)
// 添加更新PLC状态的方法
private void UpdatePlcStatus(int plcId, bool isConnected)
{
if (事件1.Text == name)
try
{
事件1.Visible = true;
事件1结果.Visible = true;
事件1时间.Visible = true;
事件1.Text = name;
事件1时间.Text = dateTime.ToString("MM-dd HH:mm:ss");
事件1结果.Text = result.ToString();
}
else if (事件2.Text == name)
{
事件2.Visible = true;
事件2结果.Visible = true;
事件2时间.Visible = true;
事件2.Text = name;
事件2时间.Text = dateTime.ToString("MM-dd HH:mm:ss");
事件2结果.Text = result.ToString();
}
else if (事件3.Text == name)
{
事件3.Visible = true;
事件3结果.Visible = true;
事件3时间.Visible = true;
事件3.Text = name;
事件3时间.Text = dateTime.ToString("MM-dd HH:mm:ss");
事件3结果.Text = result.ToString();
}
else if (事件4.Text == name)
{
事件4.Visible = true;
事件4结果.Visible = true;
事件4时间.Visible = true;
事件4.Text = name;
事件4时间.Text = dateTime.ToString("MM-dd HH:mm:ss");
事件4结果.Text = result.ToString();
}
else
{
if (事件1.Text.Contains("事件"))
if (plcStatusIndicators.ContainsKey(plcId))
{
事件1.Visible = true;
事件1结果.Visible = true;
事件1时间.Visible = true;
事件1.Text = name;
事件1时间.Text = dateTime.ToString("MM-dd HH:mm:ss");
事件1结果.Text = result.ToString();
// 使用Invoke确保在UI线程上更新控件
this.Invoke((MethodInvoker)delegate
{
plcStatusIndicators[plcId].BackColor = isConnected ? Color.Green : Color.Red;
});
}
else if (事件2.Text.Contains("事件"))
{
事件2.Visible = true;
事件2结果.Visible = true;
事件2时间.Visible = true;
事件2.Text = name;
事件2时间.Text = dateTime.ToString("MM-dd HH:mm:ss");
事件2结果.Text = result.ToString();
}
else if (事件3.Text.Contains("事件"))
{
事件3.Visible = true;
事件3结果.Visible = true;
事件3时间.Visible = true;
事件3.Text = name;
事件3时间.Text = dateTime.ToString("MM-dd HH:mm:ss");
事件3结果.Text = result.ToString();
}
else if (事件4.Text.Contains("事件"))
{
事件4.Visible = true;
事件4结果.Visible = true;
事件4时间.Visible = true;
事件4.Text = name;
事件4时间.Text = dateTime.ToString("MM-dd HH:mm:ss");
事件4结果.Text = result.ToString();
}
else
{
事件1.Visible = true;
事件1结果.Visible = true;
事件1时间.Visible = true;
事件1.Text = name;
事件1时间.Text = dateTime.ToString("MM-dd HH:mm:ss");
事件1结果.Text = result.ToString();
}
catch { /* 忽略异常 */ }
}
事件2.Visible = false;
事件2结果.Visible = false;
事件2时间.Visible = false;
事件2.Text = "事件2";
事件2时间.Text = "事件2时间";
事件2结果.Text = "OK";
/// <summary>
/// 更新事件显示区域
/// </summary>
private void UpdateEventDisplay(string name, mesresult result, DateTime dateTime, string message)
{
UpdateEventSlot(事件1, 事件1结果, 事件1时间, name, result, dateTime, message);
// 根据接口名称显示特定信息
//UpdateSpecificEventInfo(name, message);
}
事件3.Visible = false;
事件3结果.Visible = false;
事件3时间.Visible = false;
事件3.Text = "事件3";
事件3时间.Text = "事件3时间";
事件3结果.Text = "OK";
/// <summary>
/// 查找可用的事件显示槽位
/// </summary>
private int FindAvailableEventSlot()
{
// 首先查找不可见的事件槽位
if (!事件1.Visible) return 1;
return 1;
}
事件4.Visible = false;
事件4结果.Visible = false;
事件4时间.Visible = false;
事件4.Text = "事件4";
事件4时间.Text = "事件4时间";
事件4结果.Text = "OK";
}
/// <summary>
/// 更新指定事件槽位的显示内容
/// </summary>
private void UpdateEventSlot(Label eventLabel, Label resultLabel, Label timeLabel,
string name, mesresult result, DateTime dateTime, string message)
{
// 设置事件名称
eventLabel.Text = $"{name}";
eventLabel.Visible = true;
// 设置事件结果
resultLabel.Text = $"{(result == mesresult.PASS ? "PASS" : "FAIL")}";
resultLabel.ForeColor = result == mesresult.PASS ? Color.Green : Color.Red;
resultLabel.Visible = true;
// 设置事件时间
timeLabel.Text = $"{dateTime:MM-dd HH:mm:ss}";
timeLabel.Visible = true;
}
/// <summary>
/// 根据接口名称更新特定信息显示
/// </summary>
private void UpdateSpecificEventInfo(string name, string message)
{
// 根据不同的接口名称,显示特定的信息
switch (name)
{
case "SN_Release":
// 显示SN号
if (!string.IsNullOrEmpty(Init.ScanString))
{
LOT号.Text = $"SN号:{Init.ScanString}";
LOT号.Visible = true;
}
break;
case "Tray_Release":
// 显示Tray盘信息
if (!string.IsNullOrEmpty(message))
{
Tray盘二维码.Text = $"Tray盘:{message}";
Tray盘二维码.Visible = true;
}
break;
case "Status":
// 机台状态信息已在其他地方更新
break;
case "Alarm":
// 报警信息可以在底部注释中显示
if (!string.IsNullOrEmpty(message))
{
底部注释3.Text = $"报警信息:{message}";
底部注释3.Visible = true;
}
break;
// 可以根据需要添加更多的接口类型处理
default:
// 对于其他类型的接口,可以在底部注释中显示额外信息
if (!string.IsNullOrEmpty(message))
{
if (!底部注释4.Visible)
{
底部注释4.Text = $"{name}:{message}";
底部注释4.Visible = true;
}
else if (!底部注释5.Visible)
{
底部注释5.Text = $"{name}:{message}";
底部注释5.Visible = true;
}
}
break;
}
}
public void MES_MsgfORM(string name, ApiFormResult result, DateTime dateTime)
{
// 显示MES提示信息
MES提示信息.Visible = true;
MES提示信息.Text = $"{result.Msg}";
MES提示信息.ForeColor = result.Mesresult == mesresult.PASS ? Color.Green : Color.Red;
// 更新事件显示区域
UpdateEventDisplay(name, result.Mesresult, dateTime, result.Msg);
}
#endregion
#region 其他业务处理