From bd9be5a7bc006f73b2170f6ebff8c303572e400b Mon Sep 17 00:00:00 2001 From: wxt Date: Wed, 14 Jan 2026 09:40:00 +0800 Subject: [PATCH] --- .../PenetrationForm.Designer.cs | 11 +-- .../外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.cs | 93 ++++++++++++++++--- .../外科辅料和患者防护罩激光抗性测试仪/PrimaryIgnitionForm.cs | 16 +++- 3 files changed, 101 insertions(+), 19 deletions(-) diff --git a/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.Designer.cs b/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.Designer.cs index ee957c3..82e0f31 100644 --- a/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.Designer.cs +++ b/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.Designer.cs @@ -859,7 +859,7 @@ uiLabel25.ForeColor = Color.FromArgb(64, 158, 255); uiLabel25.Location = new Point(750, 220); uiLabel25.Name = "uiLabel25"; - uiLabel25.Size = new Size(60, 34); + uiLabel25.Size = new Size(80, 34); uiLabel25.Style = Sunny.UI.UIStyle.Custom; uiLabel25.StyleCustomMode = true; uiLabel25.TabIndex = 21; @@ -885,7 +885,7 @@ uiLabel2.ForeColor = Color.FromArgb(64, 158, 255); uiLabel2.Location = new Point(750, 148); uiLabel2.Name = "uiLabel2"; - uiLabel2.Size = new Size(60, 34); + uiLabel2.Size = new Size(80, 34); uiLabel2.Style = Sunny.UI.UIStyle.Custom; uiLabel2.StyleCustomMode = true; uiLabel2.TabIndex = 19; @@ -1052,7 +1052,7 @@ uiLabel12.ForeColor = Color.FromArgb(64, 158, 255); uiLabel12.Location = new Point(460, 218); uiLabel12.Name = "uiLabel12"; - uiLabel12.Size = new Size(60, 34); + uiLabel12.Size = new Size(75, 34); uiLabel12.Style = Sunny.UI.UIStyle.Custom; uiLabel12.StyleCustomMode = true; uiLabel12.TabIndex = 9; @@ -1129,13 +1129,12 @@ uiLabel8.ForeColor = Color.FromArgb(64, 158, 255); uiLabel8.Location = new Point(460, 146); uiLabel8.Name = "uiLabel8"; - uiLabel8.Size = new Size(60, 34); + uiLabel8.Size = new Size(75, 34); uiLabel8.Style = Sunny.UI.UIStyle.Custom; uiLabel8.StyleCustomMode = true; uiLabel8.TabIndex = 3; uiLabel8.Text = "0.0"; uiLabel8.TextAlign = ContentAlignment.MiddleLeft; - // // uiLabel9 // @@ -1156,7 +1155,7 @@ uiLabel7.ForeColor = Color.FromArgb(64, 158, 255); uiLabel7.Location = new Point(460, 76); uiLabel7.Name = "uiLabel7"; - uiLabel7.Size = new Size(60, 34); + uiLabel7.Size = new Size(75, 34); uiLabel7.Style = Sunny.UI.UIStyle.Custom; uiLabel7.StyleCustomMode = true; uiLabel7.TabIndex = 1; diff --git a/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.cs b/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.cs index 009ef83..abc237b 100644 --- a/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.cs +++ b/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.cs @@ -333,6 +333,59 @@ namespace 外科辅料和患者防护罩激光抗性测试仪 private bool _lastRunStatus = false; private bool _isReported = false; + //private async Task ReadStartStatusAsync() + //{ + // try + // { + // bool[] registers3 = await _modbusMaster?.ReadCoilsAsync(1, 81, 1); + + // if (registers3 != null && registers3.Length >= 1) + // { + // bool currentRunStatus = registers3[0]; + // this.Invoke(new Action(() => + // { + // uiLabel31.Text = currentRunStatus ? "运行中" : "空闲中"; + // })); + // // ========== 关键修改1:移除重复的插入逻辑,只保留一段 ========== + // // 仅在「运行→停止」且未上报过的情况下执行一次插入 + // if (_lastRunStatus && !currentRunStatus && !_isReported) + // { + // // 添加延迟确保数据稳定 + // //await Task.Delay(500); + // //await Task.Delay(500); + // //加锁防止异步并发重复执行 + // await _asyncLock.WaitAsync(); + // try + // { + // // 二次校验,防止锁等待期间状态已变更 + // if (!_isReported) + // { + // var model = await InsertReportAsync(); + // if (model != null && !string.IsNullOrEmpty(model.Id2)) + // { + // conductivityRepository.InsertReportItems(model); + // _isReported = true; // 标记已上报 + // Debug.WriteLine($"报表插入成功:样品{model.sampleType2}, 分类{model.result2}"); + // } + // } + // } + // finally + // { + // _asyncLock.Release(); // 释放锁 + // } + // } + + // _lastRunStatus = currentRunStatus; + // } + // } + // catch (Exception ex) + // { + // MessageBox.Show(ex.Message); + // _readtimer?.Stop(); + // Debug.WriteLine($"读取状态失败: {ex.Message}"); + // } + // //finally { _asyncLock.Release(); } + //} private async Task ReadStartStatusAsync() { try @@ -342,24 +395,28 @@ namespace 外科辅料和患者防护罩激光抗性测试仪 if (registers3 != null && registers3.Length >= 1) { bool currentRunStatus = registers3[0]; + this.Invoke(new Action(() => { uiLabel31.Text = currentRunStatus ? "运行中" : "空闲中"; })); - // ========== 关键修改1:移除重复的插入逻辑,只保留一段 ========== - // 仅在「运行→停止」且未上报过的情况下执行一次插入 - if (_lastRunStatus && !currentRunStatus && !_isReported) + + // ========== 关键修改:修复状态机逻辑 ========== + // 检测到「运行→停止」的下降沿 + if (_lastRunStatus && !currentRunStatus) { - // 添加延迟确保数据稳定 - //await Task.Delay(500); - //await Task.Delay(500); - //加锁防止异步并发重复执行 - await _asyncLock.WaitAsync(); + Debug.WriteLine($"检测到运行停止:上次状态={_lastRunStatus},当前状态={currentRunStatus},已上报={_isReported}"); + + // 延迟等待数据稳定 + await Task.Delay(300); + + await _asyncLock.WaitAsync(); try { - // 二次校验,防止锁等待期间状态已变更 + // 只有在没有上报过时才执行插入 if (!_isReported) { + Debug.WriteLine("开始执行InsertReportAsync..."); var model = await InsertReportAsync(); if (model != null && !string.IsNullOrEmpty(model.Id2)) { @@ -367,13 +424,28 @@ namespace 外科辅料和患者防护罩激光抗性测试仪 _isReported = true; // 标记已上报 Debug.WriteLine($"报表插入成功:样品{model.sampleType2}, 分类{model.result2}"); } + else + { + Debug.WriteLine("InsertReportAsync返回空模型或无效ID"); + } + } + else + { + Debug.WriteLine("已上报过,跳过插入"); } } finally { - _asyncLock.Release(); // 释放锁 + _asyncLock.Release(); } } + // 检测到「停止→运行」的上升沿,重置上报标记 + else if (!_lastRunStatus && currentRunStatus) + { + _isReported = false; + _isCurrentRecordAdded = false; + Debug.WriteLine("检测到开始运行,重置上报标记"); + } _lastRunStatus = currentRunStatus; } @@ -384,7 +456,6 @@ namespace 外科辅料和患者防护罩激光抗性测试仪 _readtimer?.Stop(); Debug.WriteLine($"读取状态失败: {ex.Message}"); } - //finally { _asyncLock.Release(); } } diff --git a/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PrimaryIgnitionForm.cs b/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PrimaryIgnitionForm.cs index 161a470..72013b7 100644 --- a/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PrimaryIgnitionForm.cs +++ b/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PrimaryIgnitionForm.cs @@ -1239,10 +1239,22 @@ namespace 外科辅料和患者防护罩激光抗性测试仪 } } + + private void uiButton8_Click(object sender, EventArgs e) { - AutoCameraForm cameraForm = new AutoCameraForm(() => RunStatus); - this.SwitchToForm(() => cameraForm); + //AutoCameraForm cameraForm = new AutoCameraForm(() => RunStatus); + //this.SwitchToForm(() => cameraForm); + + var cameraForm = new AutoCameraForm(() => RunStatus); + + // 设置窗体关闭时释放资源 + cameraForm.FormClosed += (s, args) => + { + cameraForm.Dispose(); + }; + + cameraForm.Show(); } private void uiButton11_Click(object sender, EventArgs e)