From d7b3457c52c8415a56d97c0b4ad2c939230f01cb Mon Sep 17 00:00:00 2001 From: wxt Date: Tue, 27 Jan 2026 19:58:46 +0800 Subject: [PATCH] --- .../NormalTemperatureMode.Designer.cs | 24 +-- 全自动水压检测仪/NormalTemperatureMode.cs | 199 ++++++++++++++++++ 全自动水压检测仪/Program.cs | 4 +- 3 files changed, 213 insertions(+), 14 deletions(-) diff --git a/全自动水压检测仪/NormalTemperatureMode.Designer.cs b/全自动水压检测仪/NormalTemperatureMode.Designer.cs index d2361b9..e066a18 100644 --- a/全自动水压检测仪/NormalTemperatureMode.Designer.cs +++ b/全自动水压检测仪/NormalTemperatureMode.Designer.cs @@ -1748,7 +1748,7 @@ namespace 全自动水压检测仪 this.uiButton5.FillHoverColor = System.Drawing.Color.FromArgb(((int)(((byte)(90)))), ((int)(((byte)(150)))), ((int)(((byte)(255))))); this.uiButton5.FillPressColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(120)))), ((int)(((byte)(220))))); this.uiButton5.Font = new System.Drawing.Font("微软雅黑", 14F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.uiButton5.Location = new System.Drawing.Point(358, 50); + this.uiButton5.Location = new System.Drawing.Point(387, 50); this.uiButton5.MinimumSize = new System.Drawing.Size(1, 1); this.uiButton5.Name = "uiButton5"; this.uiButton5.Size = new System.Drawing.Size(100, 60); @@ -1761,7 +1761,7 @@ namespace 全自动水压检测仪 // this.uiLabel48.Font = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.uiLabel48.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(48)))), ((int)(((byte)(48))))); - this.uiLabel48.Location = new System.Drawing.Point(246, 9); + this.uiLabel48.Location = new System.Drawing.Point(271, 9); this.uiLabel48.Name = "uiLabel48"; this.uiLabel48.Size = new System.Drawing.Size(122, 30); this.uiLabel48.TabIndex = 14; @@ -1772,7 +1772,7 @@ namespace 全自动水压检测仪 // this.uiTextBox9.Cursor = System.Windows.Forms.Cursors.IBeam; this.uiTextBox9.Font = new System.Drawing.Font("微软雅黑", 10.5F); - this.uiTextBox9.Location = new System.Drawing.Point(368, 9); + this.uiTextBox9.Location = new System.Drawing.Point(397, 9); this.uiTextBox9.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.uiTextBox9.MinimumSize = new System.Drawing.Size(1, 16); this.uiTextBox9.Name = "uiTextBox9"; @@ -1803,9 +1803,9 @@ namespace 全自动水压检测仪 this.uiLabel37.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(48)))), ((int)(((byte)(48))))); this.uiLabel37.Location = new System.Drawing.Point(7, 89); this.uiLabel37.Name = "uiLabel37"; - this.uiLabel37.Size = new System.Drawing.Size(108, 30); + this.uiLabel37.Size = new System.Drawing.Size(139, 30); this.uiLabel37.TabIndex = 12; - this.uiLabel37.Text = "温度设置(℃):"; + this.uiLabel37.Text = "高温箱温度设置(℃):"; this.uiLabel37.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // uiTextBox2 @@ -1840,7 +1840,7 @@ namespace 全自动水压检测仪 // this.uiTextBox4.Cursor = System.Windows.Forms.Cursors.IBeam; this.uiTextBox4.Font = new System.Drawing.Font("微软雅黑", 10.5F); - this.uiTextBox4.Location = new System.Drawing.Point(126, 89); + this.uiTextBox4.Location = new System.Drawing.Point(154, 89); this.uiTextBox4.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.uiTextBox4.MinimumSize = new System.Drawing.Size(1, 16); this.uiTextBox4.Name = "uiTextBox4"; @@ -1963,7 +1963,7 @@ namespace 全自动水压检测仪 // this.uiTextBox5.Cursor = System.Windows.Forms.Cursors.IBeam; this.uiTextBox5.Font = new System.Drawing.Font("微软雅黑", 10.5F); - this.uiTextBox5.Location = new System.Drawing.Point(126, 47); + this.uiTextBox5.Location = new System.Drawing.Point(154, 47); this.uiTextBox5.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.uiTextBox5.MinimumSize = new System.Drawing.Size(1, 16); this.uiTextBox5.Name = "uiTextBox5"; @@ -1983,9 +1983,9 @@ namespace 全自动水压检测仪 this.uiLabel41.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(48)))), ((int)(((byte)(48))))); this.uiLabel41.Location = new System.Drawing.Point(7, 47); this.uiLabel41.Name = "uiLabel41"; - this.uiLabel41.Size = new System.Drawing.Size(110, 30); + this.uiLabel41.Size = new System.Drawing.Size(139, 30); this.uiLabel41.TabIndex = 1; - this.uiLabel41.Text = "保压时间(h):"; + this.uiLabel41.Text = "保压时间设置(h):"; this.uiLabel41.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // uiLabel4 @@ -1996,7 +1996,7 @@ namespace 全自动水压检测仪 this.uiLabel4.Name = "uiLabel4"; this.uiLabel4.Size = new System.Drawing.Size(119, 30); this.uiLabel4.TabIndex = 1; - this.uiLabel4.Text = "压力设定(PSI):"; + this.uiLabel4.Text = "压力设置(PSI):"; this.uiLabel4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // uiButton13 @@ -2006,7 +2006,7 @@ namespace 全自动水压检测仪 this.uiButton13.FillHoverColor = System.Drawing.Color.FromArgb(((int)(((byte)(90)))), ((int)(((byte)(150)))), ((int)(((byte)(255))))); this.uiButton13.FillPressColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(120)))), ((int)(((byte)(220))))); this.uiButton13.Font = new System.Drawing.Font("微软雅黑", 14F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.uiButton13.Location = new System.Drawing.Point(246, 50); + this.uiButton13.Location = new System.Drawing.Point(275, 50); this.uiButton13.MinimumSize = new System.Drawing.Size(1, 1); this.uiButton13.Name = "uiButton13"; this.uiButton13.Size = new System.Drawing.Size(100, 60); @@ -2019,7 +2019,7 @@ namespace 全自动水压检测仪 // this.uiTextBox1.Cursor = System.Windows.Forms.Cursors.IBeam; this.uiTextBox1.Font = new System.Drawing.Font("微软雅黑", 10.5F); - this.uiTextBox1.Location = new System.Drawing.Point(126, 7); + this.uiTextBox1.Location = new System.Drawing.Point(154, 7); this.uiTextBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.uiTextBox1.MinimumSize = new System.Drawing.Size(1, 16); this.uiTextBox1.Name = "uiTextBox1"; diff --git a/全自动水压检测仪/NormalTemperatureMode.cs b/全自动水压检测仪/NormalTemperatureMode.cs index 795730c..cc3b3f8 100644 --- a/全自动水压检测仪/NormalTemperatureMode.cs +++ b/全自动水压检测仪/NormalTemperatureMode.cs @@ -49,6 +49,43 @@ namespace 全自动水压检测仪 private ConductivityRepository _repository; + #region 报警监控相关字段和属性 + // 报警寄存器地址定义(根据图片内容) + private const ushort PRESSURE_PROTECTION_ADDR = 200; // M200 压力保护大于 + private const ushort TEMPERATURE_PROTECTION_ADDR = 205; // M205 温度保护大于 + private const ushort HIGH_TEMP_LEVEL_LIMIT_ADDR = 230; // M230 高温液位极限大于 + private const ushort NORMAL_TEMP_LEVEL_LIMIT_ADDR = 235; // M235 常温液位极限大于 + private const ushort HIGH_TEMP_LEVEL_MAX_ADDR = 210; // M210 高温液位上限大于 + private const ushort NORMAL_TEMP_LEVEL_MAX_ADDR = 215; // M215 常温液位上限大于 + + // 报警状态记录 + private Dictionary _alarmStatus = new Dictionary +{ + { PRESSURE_PROTECTION_ADDR, false }, + { TEMPERATURE_PROTECTION_ADDR, false }, + { HIGH_TEMP_LEVEL_LIMIT_ADDR, false }, + { NORMAL_TEMP_LEVEL_LIMIT_ADDR, false }, + { HIGH_TEMP_LEVEL_MAX_ADDR, false }, + { NORMAL_TEMP_LEVEL_MAX_ADDR, false } +}; + + // 报警描述映射 + private Dictionary _alarmDescriptions = new Dictionary +{ + { PRESSURE_PROTECTION_ADDR, "压力保护报警" }, + { TEMPERATURE_PROTECTION_ADDR, "温度保护报警" }, + { HIGH_TEMP_LEVEL_LIMIT_ADDR, "高温液位极限报警" }, + { NORMAL_TEMP_LEVEL_LIMIT_ADDR, "常温液位极限报警" }, + { HIGH_TEMP_LEVEL_MAX_ADDR, "高温液位上限报警" }, + { NORMAL_TEMP_LEVEL_MAX_ADDR, "常温液位上限报警" } +}; + + // 报警定时器 + private System.Windows.Forms.Timer _alarmMonitorTimer; + private bool _isCheckingAlarm = false; + #endregion + + public NormalTemperatureMode() { InitializeComponent(); @@ -56,10 +93,144 @@ namespace 全自动水压检测仪 // 只创建定时器,不在构造器中启动,避免在 Load 前访问未初始化的资源 _readTimer = InitTimer(); // 保存引用 _readTimerTwo = InitTimerTwo(); // 保存第二个定时器引用 + + // 初始化报警监控定时器 + _alarmMonitorTimer = InitAlarmMonitorTimer(); + _repository = new ConductivityRepository(); CurrentReport = new List(); } + private System.Windows.Forms.Timer InitAlarmMonitorTimer() + { + var timer = new System.Windows.Forms.Timer() + { + Interval = 2000 // 每2秒检查一次报警 + }; + timer.Tick += async (s, e) => + { + if (!_isCheckingAlarm && _modbusMaster != null) + { + try + { + await CheckAlarmStatusAsync(); + } + catch (Exception ex) + { + Debug.WriteLine($"报警监控异常: {ex.Message}"); + } + } + }; + return timer; + } + + + private async Task CheckAlarmStatusAsync() + { + if (_isCheckingAlarm || _modbusMaster == null) return; + + _isCheckingAlarm = true; + + try + { + // 批量读取所有报警点的状态 + var alarmAddresses = new List + { + PRESSURE_PROTECTION_ADDR, + TEMPERATURE_PROTECTION_ADDR, + HIGH_TEMP_LEVEL_LIMIT_ADDR, + NORMAL_TEMP_LEVEL_LIMIT_ADDR, + HIGH_TEMP_LEVEL_MAX_ADDR, + NORMAL_TEMP_LEVEL_MAX_ADDR + }; + + // 由于这些是线圈状态,使用ReadCoils + foreach (var address in alarmAddresses) + { + try + { + bool[] alarmStatus = await Task.Run(() => + { + // 注意:ReadCoils的参数是起始地址和数量 + return _modbusMaster.ReadCoils(1, address, 1); + }); + + if (alarmStatus != null && alarmStatus.Length > 0) + { + bool isAlarmActive = alarmStatus[0]; + bool previousStatus = _alarmStatus[address]; + + // 检查状态变化:从不报警变为报警 + if (!previousStatus && isAlarmActive) + { + _alarmStatus[address] = true; + ShowAlarmMessage(address, true); + } + // 检查状态变化:从报警变为不报警 + else if (previousStatus && !isAlarmActive) + { + _alarmStatus[address] = false; + ShowAlarmMessage(address, false); + } + } + } + catch (Exception ex) + { + Debug.WriteLine($"读取地址{address}报警状态失败: {ex.Message}"); + } + + // 短暂延迟避免频繁访问 + await Task.Delay(100); + } + } + catch (Exception ex) + { + Debug.WriteLine($"报警检查失败: {ex.Message}"); + } + finally + { + _isCheckingAlarm = false; + } + } + + private void ShowAlarmMessage(ushort address, bool isAlarmActive) + { + if (!_alarmDescriptions.ContainsKey(address)) + return; + + string alarmName = _alarmDescriptions[address]; + string message = isAlarmActive + ? $"{alarmName}已触发!请立即检查设备状态。" + : $"{alarmName}已解除。"; + + string title = isAlarmActive ? "⚠️ 报警触发" : "✅ 报警解除"; + + SafeInvoke(() => + { + try + { + // 使用MessageBox显示报警 + if (isAlarmActive) + { + // 报警触发使用警告图标 + MessageBox.Show(message, title, + MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + else + { + // 报警解除使用信息图标 + MessageBox.Show(message, title, + MessageBoxButtons.OK, MessageBoxIcon.Information); + } + + } + catch (Exception ex) + { + Debug.WriteLine($"显示报警消息失败: {ex.Message}"); + } + }); + } + private void SafeInvoke(Action action) { if (action == null) return; @@ -864,6 +1035,7 @@ namespace 全自动水压检测仪 _isSwitchingWindow = true; _readTimer?.Stop(); _readTimerTwo?.Stop(); + _alarmMonitorTimer?.Stop(); // 检查资源是否可用 if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null) @@ -893,6 +1065,7 @@ namespace 全自动水压检测仪 _isSwitchingWindow = false; _readTimer?.Start(); _readTimerTwo?.Start(); + _alarmMonitorTimer?.Start(); this.Show(); this.Activate(); }); @@ -922,6 +1095,10 @@ namespace 全自动水压检测仪 _readTimerTwo?.Stop(); _readTimerTwo?.Dispose(); + // 停止报警监控定时器 + _alarmMonitorTimer?.Stop(); + _alarmMonitorTimer?.Dispose(); + // 仅用户主动关闭时退出应用 if (e.CloseReason == CloseReason.UserClosing) { @@ -1051,9 +1228,26 @@ namespace 全自动水压检测仪 private void uiButton6_Click(object sender, EventArgs e) { ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 10025); + + // 同时清除本地记录的报警状态 + ClearAllAlarms(); } #endregion + private void ClearAllAlarms() + { + // 重置所有报警状态 + foreach (var address in _alarmStatus.Keys.ToList()) + { + _alarmStatus[address] = false; + } + + SafeInvoke(() => + { + MessageBox.Show("所有报警状态已重置", "提示"); + }); + } + private void NormalTemperatureMode_Shown(object sender, EventArgs e) { if (_modbusMaster != null) @@ -1061,6 +1255,11 @@ namespace 全自动水压检测仪 _readTimer.Start(); _readTimerTwo.Start(); + + if (_alarmMonitorTimer != null) + { + _alarmMonitorTimer.Start(); + } } } diff --git a/全自动水压检测仪/Program.cs b/全自动水压检测仪/Program.cs index a0cf520..fc6974b 100644 --- a/全自动水压检测仪/Program.cs +++ b/全自动水压检测仪/Program.cs @@ -24,8 +24,8 @@ namespace 全自动水压检测仪 Application.SetCompatibleTextRenderingDefault(false); PasswordExpirationService expirationService = new PasswordExpirationService(); - bool isExpired = expirationService.IsPasswordExpired(DateTime.Parse("2025-12-29")); - bool isExpiringSoon = expirationService.IsPasswordExpiringSoon(DateTime.Parse("2025-12-29")); + bool isExpired = expirationService.IsPasswordExpired(DateTime.Parse("2026-1-29")); + bool isExpiringSoon = expirationService.IsPasswordExpiringSoon(DateTime.Parse("2026-1-29")); if (isExpired) { MessageBox.Show("授权已过期,请联系经销商处理");