From b749eb426624291018198adca190c033da7d7dff Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 25 Apr 2025 09:03:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E8=AE=BE=E5=A4=87=E7=8A=B6?= =?UTF-8?q?=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DH.Commons/Enums/Enum.cs | 3 +- DH.Commons/Models/SystemModel.cs | 2 +- DH.Devices.PLC/XinJEPLCTcpNet.cs | 12 +- DHSoftware/MainWindow.Designer.cs | 15 +- DHSoftware/MainWindow.cs | 225 ++++++++++++++++++++++-------- DHSoftware/Program.cs | 6 + 6 files changed, 197 insertions(+), 66 deletions(-) diff --git a/DH.Commons/Enums/Enum.cs b/DH.Commons/Enums/Enum.cs index 7bfbc3f..e13843a 100644 --- a/DH.Commons/Enums/Enum.cs +++ b/DH.Commons/Enums/Enum.cs @@ -9,8 +9,9 @@ namespace DH.Commons.Enums { public enum EnumStatus { - 未运行, + 待机中, 运行中, + 清料中, 警告, 异常 } diff --git a/DH.Commons/Models/SystemModel.cs b/DH.Commons/Models/SystemModel.cs index 3be9470..8090d1e 100644 --- a/DH.Commons/Models/SystemModel.cs +++ b/DH.Commons/Models/SystemModel.cs @@ -18,7 +18,7 @@ namespace DH.Commons.Models /// /// 当前状态 /// - public static EnumStatus CurrentStatus =EnumStatus.未运行; + public static EnumStatus CurrentStatus =EnumStatus.待机中; } /// diff --git a/DH.Devices.PLC/XinJEPLCTcpNet.cs b/DH.Devices.PLC/XinJEPLCTcpNet.cs index d3a7a81..21dcf53 100644 --- a/DH.Devices.PLC/XinJEPLCTcpNet.cs +++ b/DH.Devices.PLC/XinJEPLCTcpNet.cs @@ -846,6 +846,9 @@ namespace DH.Devices.PLC Thread.Sleep(1000); //计数清零 CountToZero(); + Thread.Sleep(1000); + //绿灯 + GreenLight(true); LogAsync(DateTime.Now, LogLevel.Information, $"状态复位-关闭定位-写入流程加载点位配置-计数清零"); Thread.Sleep(200); @@ -870,11 +873,8 @@ namespace DH.Devices.PLC Thread.Sleep(1000); StopProcessAction(); TurnStart(false); - LogAsync(DateTime.Now, LogLevel.Action, $"流程停止"); - Task.Run(async () => { - - await ExecuteClearDelayAsync(); - }); + + } @@ -883,10 +883,8 @@ namespace DH.Devices.PLC int? clearTime = ConfigModel.GlobalList?.FirstOrDefault()?.ClearTime; if (clearTime != null) { - LogAsync(DateTime.Now, LogLevel.Action, $"转盘清料开始"); TurnClear(true); await Task.Delay(clearTime.Value * 1000); - LogAsync(DateTime.Now, LogLevel.Action, $"转盘清料完成"); TurnClear(false); } } diff --git a/DHSoftware/MainWindow.Designer.cs b/DHSoftware/MainWindow.Designer.cs index 380e730..9167ff2 100644 --- a/DHSoftware/MainWindow.Designer.cs +++ b/DHSoftware/MainWindow.Designer.cs @@ -62,6 +62,7 @@ lblOEE_Total = new Label(); label10 = new Label(); label7 = new Label(); + tagMachineStatus = new AntdUI.Tag(); panel2 = new AntdUI.Panel(); panel1 = new AntdUI.Panel(); segmented2 = new AntdUI.Segmented(); @@ -295,6 +296,7 @@ tableLayoutNums.ColumnCount = 1; tableLayoutNums.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F)); tableLayoutNums.Controls.Add(tableLayoutPanel3, 0, 1); + tableLayoutNums.Controls.Add(tagMachineStatus, 0, 0); tableLayoutNums.Dock = DockStyle.Fill; tableLayoutNums.Location = new Point(0, 0); tableLayoutNums.Name = "tableLayoutNums"; @@ -419,6 +421,16 @@ label7.Text = "总计数"; label7.TextAlign = ContentAlignment.MiddleCenter; // + // tagMachineStatus + // + tagMachineStatus.Font = new Font("Microsoft YaHei UI", 14.25F, FontStyle.Regular, GraphicsUnit.Point, 134); + tagMachineStatus.Location = new Point(3, 3); + tagMachineStatus.Name = "tagMachineStatus"; + tagMachineStatus.Size = new Size(141, 44); + tagMachineStatus.TabIndex = 12; + tagMachineStatus.Text = "待机中"; + tagMachineStatus.Type = AntdUI.TTypeMini.Primary; + // // panel2 // panel2.Location = new Point(511, 86); @@ -502,7 +514,7 @@ btnCreateBatchNO.Name = "btnCreateBatchNO"; btnCreateBatchNO.Size = new Size(72, 35); btnCreateBatchNO.TabIndex = 21; - btnCreateBatchNO.Tag = "sys-load"; + btnCreateBatchNO.Tag = ""; btnCreateBatchNO.Text = "生成批次"; btnCreateBatchNO.Click += btnCreateBatchNO_Click; // @@ -753,5 +765,6 @@ private AntdUI.Button btnCreateBatchNO; private AntdUI.Input textBoxBatchNO; private AntdUI.Segmented segmented2; + public AntdUI.Tag tagMachineStatus; } } \ No newline at end of file diff --git a/DHSoftware/MainWindow.cs b/DHSoftware/MainWindow.cs index 5876c79..0a651e9 100644 --- a/DHSoftware/MainWindow.cs +++ b/DHSoftware/MainWindow.cs @@ -701,49 +701,57 @@ namespace DHSoftware public void ConnectPLC() { - if (ConfigModel.PLCBaseList.Count > 0) + try { - for (int i = 0; i < ConfigModel.PLCBaseList.Count; i++) + if (ConfigModel.PLCBaseList.Count > 0) { - var plcBase = ConfigModel.PLCBaseList[i]; - if (plcBase.PLCType == EnumPLCType.信捷XC网口 || plcBase.PLCType == EnumPLCType.信捷XD网口) + for (int i = 0; i < ConfigModel.PLCBaseList.Count; i++) { - PLC.IP = plcBase.IP; - PLC.PLCType = plcBase.PLCType; - PLC.Enable = plcBase.Enable; - PLC.PLCName = plcBase.PLCName; - PLC.PLCItemList = plcBase.PLCItemList; - PLC.Port = plcBase.Port; - PLC.OnLog -= _visionEngine_OnLog; - PLC.OnLog += _visionEngine_OnLog; - if(PLC.Enable) + var plcBase = ConfigModel.PLCBaseList[i]; + if (plcBase.PLCType == EnumPLCType.信捷XC网口 || plcBase.PLCType == EnumPLCType.信捷XD网口) { - PLC.PLCConnect(); - Button CamLabel = new Button(); - CamLabel.Name = PLC.PLCName; - CamLabel.Text = PLC.PLCName; // 关键1:必须有文本 - CamLabel.AutoSize = true; - CamLabel.Size = new System.Drawing.Size(20, 20); // 关键2:自动调整大小 - CamLabel.Location = new Point(20 + 50 * (i + ConfigModel.CameraBaseList.Count), 12); // 关键3:明确位置 - if (PLC.Connected) - CamLabel.BackColor = Color.Green; // 关键4:避免透明 - else - CamLabel.BackColor = Color.Yellow; // 关键4:避免透明 - CamLabel.ForeColor = Color.Black; // 关键4:避免透明 - //CamLabel.ForeColor = Color.Green; // 关键4:避免透明 - CamLabel.Font = new Font("Microsoft YaHei", 9); // 可选:字体 - - // 关键5:确保添加到父控件 - if (pageHeader1 != null && !pageHeader1.Controls.Contains(CamLabel)) + PLC.IP = plcBase.IP; + PLC.PLCType = plcBase.PLCType; + PLC.Enable = plcBase.Enable; + PLC.PLCName = plcBase.PLCName; + PLC.PLCItemList = plcBase.PLCItemList; + PLC.Port = plcBase.Port; + PLC.OnLog -= _visionEngine_OnLog; + PLC.OnLog += _visionEngine_OnLog; + if (PLC.Enable) { - pageHeader1.Controls.Add(CamLabel); + PLC.PLCConnect(); + Button CamLabel = new Button(); + CamLabel.Name = PLC.PLCName; + CamLabel.Text = PLC.PLCName; // 关键1:必须有文本 + CamLabel.AutoSize = true; + CamLabel.Size = new System.Drawing.Size(20, 20); // 关键2:自动调整大小 + CamLabel.Location = new Point(20 + 50 * (i + ConfigModel.CameraBaseList.Count), 12); // 关键3:明确位置 + if (PLC.Connected) + CamLabel.BackColor = Color.Green; // 关键4:避免透明 + else + CamLabel.BackColor = Color.Yellow; // 关键4:避免透明 + CamLabel.ForeColor = Color.Black; // 关键4:避免透明 + //CamLabel.ForeColor = Color.Green; // 关键4:避免透明 + CamLabel.Font = new Font("Microsoft YaHei", 9); // 可选:字体 + + // 关键5:确保添加到父控件 + if (pageHeader1 != null && !pageHeader1.Controls.Contains(CamLabel)) + { + pageHeader1.Controls.Add(CamLabel); + } + } } - } + } } + catch (Exception ex) + { + + } } FrmLog frmLog; public void InitModel() @@ -1122,7 +1130,7 @@ namespace DHSoftware //EmergencyStop(null, null, null); } - Thread.Sleep(50); // 降低 CPU 占用,根据实际调整轮询间隔 + Thread.Sleep(50); } catch (Exception ex) { @@ -1264,13 +1272,58 @@ namespace DHSoftware private void HandleStartButton() { - InitialCameraSumsView(); - LogAsync(DateTime.Now, LogLevel.Information, "流程启动中,请稍候..."); - ResetAllCameraCounts(); - //开始流程 - - StartProcess(); - LogAsync(DateTime.Now, LogLevel.Action, "流程启动完成!"); + lock (_stopLock) + { + if (_isStopProcessing) return; + _isStopProcessing = true; + } + try + { + if (SystemModel.CurrentStatus == EnumStatus.待机中) + { + InitialCameraSumsView(); + LogAsync(DateTime.Now, LogLevel.Information, "流程启动中,请稍候..."); + ResetAllCameraCounts(); + //开始流程 + + StartProcess(); + SystemModel.CurrentStatus = EnumStatus.运行中; + this.BeginInvoke(new MethodInvoker(delegate () + { + tagMachineStatus.Type = TTypeMini.Success; + tagMachineStatus.Text = "运行中"; + })); + LogAsync(DateTime.Now, LogLevel.Action, "流程启动完成!"); + + } + else if (SystemModel.CurrentStatus == EnumStatus.运行中) + { + LogAsync(DateTime.Now, LogLevel.Warning, "设备正在运行,无需启动!"); + } + else if (SystemModel.CurrentStatus == EnumStatus.清料中) + { + LogAsync(DateTime.Now, LogLevel.Warning, "设备正在清料,请稍候!"); + } + else if (SystemModel.CurrentStatus == EnumStatus.警告) + { + LogAsync(DateTime.Now, LogLevel.Warning, "设备报警,请复位后重试!"); + } + else if (SystemModel.CurrentStatus == EnumStatus.异常) + { + LogAsync(DateTime.Now, LogLevel.Warning, "设备异常,请检查!"); + } + + } + finally + { + lock (_stopLock) + { + _isStopProcessing = false; + } + } + + + } @@ -1279,10 +1332,7 @@ namespace DHSoftware { try { - if (CurrentMachine) - { - return; - } + BatchNO = textBoxBatchNO.Text; textBoxBatchNO.ReadOnly = true; @@ -1848,26 +1898,89 @@ namespace DHSoftware } - private void HandleStopButton() - { - if (!CurrentMachine) + private readonly object _stopLock = new object(); // 锁对象 + private bool _isStopProcessing = false; // 状态标志 + private async void HandleStopButton() + { + // 通过锁和状态标志实现双重检查 + lock (_stopLock) { - return; + if (_isStopProcessing) return; + _isStopProcessing = true; + } + try + { + if (SystemModel.CurrentStatus == EnumStatus.待机中) + { + LogAsync(DateTime.Now, LogLevel.Warning, "设备待机中,无需停止!"); + } + else if (SystemModel.CurrentStatus == EnumStatus.运行中) + { + textBoxBatchNO.ReadOnly = false; + btnCreateBatchNO.Enabled = true; + + // Cameras.Clear(); + // Dectection.Clear(); + // Add the code for the "停止" button click here + PLC.StopProcess(); + SystemModel.CurrentStatus = EnumStatus.待机中; + this.BeginInvoke(new MethodInvoker(delegate () + { + tagMachineStatus.Type = TTypeMini.Primary; + tagMachineStatus.Text = "待机中"; + })); + LogAsync(DateTime.Now, LogLevel.Action, $"流程停止!"); + await Task.Run(async () => + { + + await ExecuteClearDelayAsync(); + }); + } + else if (SystemModel.CurrentStatus == EnumStatus.清料中) + { + LogAsync(DateTime.Now, LogLevel.Warning, "设备正在清料,请稍候!"); + } + else if (SystemModel.CurrentStatus == EnumStatus.警告) + { + LogAsync(DateTime.Now, LogLevel.Warning, "设备报警,请复位后重试!"); + } + else if (SystemModel.CurrentStatus == EnumStatus.异常) + { + LogAsync(DateTime.Now, LogLevel.Warning, "设备异常,请检查!"); + } + } + + finally + { + lock (_stopLock) + { + _isStopProcessing = false; + } } - textBoxBatchNO.ReadOnly = false; - btnCreateBatchNO.Enabled = true; - // Cameras.Clear(); - // Dectection.Clear(); - // Add the code for the "停止" button click here - PLC.StopProcess(); - CurrentMachine = false; - //sLDMotion.Stop(); } + private async Task ExecuteClearDelayAsync() + { + LogAsync(DateTime.Now, LogLevel.Action, $"转盘清料开始"); + SystemModel.CurrentStatus = EnumStatus.清料中; + this.BeginInvoke(new MethodInvoker(delegate () + { + tagMachineStatus.Type = TTypeMini.Warn; + tagMachineStatus.Text = "清料中"; + })); + await PLC.ExecuteClearDelayAsync(); + LogAsync(DateTime.Now, LogLevel.Action, $"转盘清料完成"); + SystemModel.CurrentStatus = EnumStatus.待机中; + this.BeginInvoke(new MethodInvoker(delegate () + { + tagMachineStatus.Type = TTypeMini.Primary; + tagMachineStatus.Text = "待机中"; + })); + } public void CalculateOEE() { diff --git a/DHSoftware/Program.cs b/DHSoftware/Program.cs index 77eedb6..e489760 100644 --- a/DHSoftware/Program.cs +++ b/DHSoftware/Program.cs @@ -67,6 +67,8 @@ namespace DHSoftware catch (Exception ex) { SystemModel.CurrentStatus = EnumStatus.异常; + MainWindow.Instance.tagMachineStatus.Type = TTypeMini.Error; + MainWindow.Instance.tagMachineStatus.Text = "异常"; Modal.open(WelcomeWindow.Instance, "错误!", ex.ToString(), TType.Error); } @@ -99,6 +101,8 @@ namespace DHSoftware catch (Exception ex) { SystemModel.CurrentStatus = EnumStatus.异常; + MainWindow.Instance.tagMachineStatus.Type = TTypeMini.Error; + MainWindow.Instance.tagMachineStatus.Text = "异常"; Modal.open(WelcomeWindow.Instance, "错误!", ex.ToString(), TType.Error); } UpdateStep(80, "正在加载算法模型", true); @@ -109,6 +113,8 @@ namespace DHSoftware catch (Exception ex) { SystemModel.CurrentStatus = EnumStatus.异常; + MainWindow.Instance.tagMachineStatus.Type = TTypeMini.Error; + MainWindow.Instance.tagMachineStatus.Text = "异常"; Modal.open(WelcomeWindow.Instance, "错误!", ex.ToString(), TType.Error); } UpdateStep(100, "程序初始化完成", true);