This commit is contained in:
GukSang.Jin
2026-02-04 18:14:40 +08:00
parent 78e346bdec
commit 2ba1f3dae7
2 changed files with 84 additions and 246 deletions

View File

@@ -30,13 +30,12 @@ namespace 全自动水压检测仪
/// </summary>
public void InitializeChart(Control targetPanel)
{
targetPanel.Invoke(new Action(() =>
{
if (targetPanel == null)
throw new ArgumentNullException(nameof(targetPanel));
// 清除占位文字
targetPanel.Text = null;
if (targetPanel == null)
throw new ArgumentNullException(nameof(targetPanel));
// 清除占位文字
targetPanel.Text = null;
// 初始化数据集合
_pressureData = new List<DataPoint>();
@@ -49,7 +48,7 @@ namespace 全自动水压检测仪
Background = OxyColors.White,
PlotAreaBorderColor = OxyColors.Gray
};
// 配置图例
_plotModel.Legends.Add(new OxyPlot.Legends.Legend
{
@@ -180,7 +179,7 @@ namespace 全自动水压检测仪
// 更新系列数据
_pressureSeries.Points.Clear();
_pressureSeries.Points.AddRange(_pressureData);
_pressureSetSeries.Points.Clear();
_pressureSetSeries.Points.AddRange(_pressureSetData);

View File

@@ -48,7 +48,7 @@ namespace 全自动水压检测仪
private System.Windows.Forms.Timer _readTimerTwo; // 第二个定时器引用
private bool _isManualInput = false; // 手动输入标记
private bool _isSwitchingWindow = false; // 窗口切换标记,避免并发
// 温度模式状态跟踪(用于防抖优化)
private bool _lastHighTempMode = false;
private bool _lastLowTempMode = false;
@@ -105,7 +105,7 @@ namespace 全自动水压检测仪
_repository = new ConductivityRepository();
CurrentReport = new List<ConductivityTestData>();
// 初始化图表管理器
_chartManager = new ChartManager();
}
@@ -519,13 +519,13 @@ namespace 全自动水压检测仪
{
var pressure = c.UshortToFloat(modbusData.Real1[1], modbusData.Real1[0]);
var pressureSetValue = c.UshortToFloat(modbusData.Real12[1], modbusData.Real12[0]);
// 获取测试时间从PLC读取的时分秒并计算总秒数
int seconds = (modbusData.Real6 != null && modbusData.Real6.Length >= 1) ? modbusData.Real6[0] : 0;
int minutes = (modbusData.Real7 != null && modbusData.Real7.Length >= 1) ? modbusData.Real7[0] : 0;
int hours = (modbusData.Real8 != null && modbusData.Real8.Length >= 1) ? modbusData.Real8[0] : 0;
int totalSeconds = hours * 3600 + minutes * 60 + seconds;
_chartManager?.AddDataPoint(pressure, pressureSetValue, totalSeconds);
}
catch (Exception chartEx)
@@ -776,7 +776,7 @@ namespace 全自动水压检测仪
{
bool newSwitchState = modbusResults.ButtonStatus[0];
bool currentSwitchState = uiSwitch1.Active;
// 只有状态真正改变时才更新
if (newSwitchState != currentSwitchState)
{
@@ -789,10 +789,11 @@ namespace 全自动水压检测仪
{
bool currentLowTempState = modbusResults.LowStatus[0];
uiLight1.State = currentLowTempState ? UILightState.On : UILightState.Off;
// 只有状态真正改变时才更新控件可见性
if (_lastLowTempMode != currentLowTempState)
{
_lastLowTempMode = currentLowTempState;
UpdateControlsVisibilityByMode();
}
}
@@ -802,10 +803,11 @@ namespace 全自动水压检测仪
{
bool currentHighTempState = modbusResults.HighStatus[0];
uiLight2.State = currentHighTempState ? UILightState.On : UILightState.Off;
// 如果温度模式状态发生变化,更新控件可见性
if (previousHighTempState != currentHighTempState)
// 只有状态真正改变时才更新控件可见性
if (_lastHighTempMode != currentHighTempState)
{
_lastHighTempMode = currentHighTempState;
UpdateControlsVisibilityByMode();
}
}
@@ -926,7 +928,6 @@ namespace 全自动水压检测仪
}
}
private void NormalTemperatureMode_Load(object sender, EventArgs e)
{
@@ -935,7 +936,7 @@ namespace 全自动水压检测仪
{
// 普通用户隐藏"录入系统"按钮
uiButton7.Visible = false;
// 调试输出
System.Diagnostics.Debug.WriteLine($"[NormalTemperatureMode] 普通用户登录,隐藏录入系统按钮");
}
@@ -943,11 +944,11 @@ namespace 全自动水压检测仪
{
// 管理员显示"录入系统"按钮
uiButton7.Visible = true;
// 调试输出
System.Diagnostics.Debug.WriteLine($"[NormalTemperatureMode] 管理员登录,显示录入系统按钮");
}
string plcIp = "192.168.1.10";
// string plcIp = "127.0.0.1";
bool initSuccess = Data.ModbusResourceManager.Instance.Init(plcIp, 502);
@@ -970,15 +971,13 @@ namespace 全自动水压检测仪
ma = new Function(_modbusMaster);
c = new DataChange();
// 暂时注释
//_modbusMaster?.WriteSingleCoil(1, 10030, false);
_modbusMaster?.WriteSingleCoil(1, 10030, true);
boolSignal1.OnRisingEdge += BoolSignal1_OnRisingEdge;
// 初始化图表
try
{
//_chartManager.InitializeChart(uiPanel_ChartPlaceholder);
_chartManager.InitializeChart(uiPanel_ChartPlaceholder);
Debug.WriteLine("[NormalTemperatureMode] 图表初始化成功");
}
catch (Exception ex)
@@ -1268,7 +1267,7 @@ namespace 全自动水压检测仪
{
ma?.BtnClickFunctionForNew(Function.ButtonType., 10080);
isAddTag = false;
// 清除图表数据,开始新的测试
_chartManager?.ClearData();
Debug.WriteLine("[NormalTemperatureMode] 启动测试,清除图表数据");
@@ -1286,9 +1285,9 @@ namespace 全自动水压检测仪
try
{
// 暂停定时器避免并发访问Modbus
//_readTimer?.Stop();
//_readTimerTwo?.Stop();
//_alarmMonitorTimer?.Stop();
_readTimer?.Stop();
_readTimerTwo?.Stop();
_alarmMonitorTimer?.Stop();
// 在后台线程执行Modbus操作
await Task.Run(() =>
@@ -1299,9 +1298,10 @@ namespace 全自动水压检测仪
}
catch (Exception ex)
{
Debug.WriteLine($"[uiSwitch1_Click] 切换模式失败: {ex.Message}");
SafeInvoke(() =>
{
Debug.WriteLine($"切换模式失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show($"切换模式失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
});
}
});
@@ -1309,29 +1309,29 @@ namespace 全自动水压检测仪
// 缩短延迟时间从800ms减少到300ms
await Task.Delay(300);
// 更新控件可见性
// 更新控件可见性(已优化为批量更新)
SafeInvoke(() =>
{
UpdateControlsVisibilityByMode();
// // 更新图表模式
// bool isHighTempMode = uiLight2 != null && uiLight2.State == UILightState.On;
// _chartManager?.UpdateChartMode(isHighTempMode);
//});
// 更新图表模式
bool isHighTempMode = uiLight2 != null && uiLight2.State == UILightState.On;
_chartManager?.UpdateChartMode(isHighTempMode);
});
//// 恢复定时器
//_readTimer?.Start();
//_readTimerTwo?.Start();
//_alarmMonitorTimer?.Start();
// 恢复定时器
_readTimer?.Start();
_readTimerTwo?.Start();
_alarmMonitorTimer?.Start();
}
catch (Exception ex)
{
Debug.WriteLine($"[uiSwitch1_Click] 异常: {ex.Message}");
//// 确保定时器恢复
//_readTimer?.Start();
//_readTimerTwo?.Start();
//_alarmMonitorTimer?.Start();
// 确保定时器恢复
_readTimer?.Start();
_readTimerTwo?.Start();
_alarmMonitorTimer?.Start();
}
}
@@ -1384,7 +1384,7 @@ namespace 全自动水压检测仪
{
if (_modbusMaster != null)
{
_readTimer.Start();
_readTimerTwo.Start();
@@ -1395,7 +1395,7 @@ namespace 全自动水压检测仪
}
// 初始化时根据当前模式更新控件可见性
//UpdateControlsVisibilityByMode();
UpdateControlsVisibilityByMode();
}
//返回录入系统
@@ -1527,7 +1527,7 @@ namespace 全自动水压检测仪
ushort[] exitTempRegisters = ConvertFloatToRegisters(exitTemperature);
_modbusMaster.WriteMultipleRegisters(1, 2412, exitTempRegisters);
}
// 将浮点数转换为寄存器数组2个寄存器
@@ -1566,7 +1566,7 @@ namespace 全自动水压检测仪
var light1State = uiLight1 != null ? uiLight1.State : UILightState.Off;
var light2State = uiLight2 != null ? uiLight2.State : UILightState.Off;
var modeText = _currentTemperatureMode ?? "";
form.UpdateTempModeStatus(light1State, light2State, modeText);
// 更新阀门状态
@@ -1638,250 +1638,89 @@ namespace 全自动水压检测仪
/// 常温模式:显示常温相关参数(常温实时液位、常温加水)
/// 高温模式:显示高温相关参数(高温实时液位、箱体温度、出口温度、高温加水、水箱加热)
/// </summary>
//private void UpdateControlsVisibilityByMode()
//{
// try
// {
// // 判断当前是否为高温模式uiLight2亮起表示高温模式
// bool isHighTempMode = uiLight2 != null && uiLight2.State == UILightState.On;
// bool isLowTempMode = uiLight1 != null && uiLight1.State == UILightState.On;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] 开始更新 - 高温模式: {isHighTempMode}, 常温模式: {isLowTempMode}");
// // ========== 高温模式专属控件(高温模式显示,常温模式隐藏) ==========
// // 高温实时液位(mm) - uiPanel31包含uiLabel7, uiLabel8, uiLabel34, uiProcessBar2
// if (uiPanel31 != null && !uiPanel31.IsDisposed)
// {
// uiPanel31.Visible = isHighTempMode;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel31(高温液位) Visible = {isHighTempMode}");
// }
// // 箱体温度(°C) - uiPanel35包含uiLabel42, uiLabel43实时数据监控区域
// if (uiPanel35 != null && !uiPanel35.IsDisposed)
// {
// uiPanel35.Visible = isHighTempMode;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel35(箱体温度) Visible = {isHighTempMode}");
// }
// // 出口温度(°C) - uiPanel33包含uiLabel38, uiLabel39实时数据监控区域
// if (uiPanel33 != null && !uiPanel33.IsDisposed)
// {
// uiPanel33.Visible = isHighTempMode;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel33(出口温度) Visible = {isHighTempMode}");
// }
// // 高温加水按钮 - uiButton5
// if (uiButton5 != null && !uiButton5.IsDisposed)
// {
// uiButton5.Visible = isHighTempMode;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiButton5(高温加水) Visible = {isHighTempMode}");
// }
// // 高温加水指示灯 - uiLight11
// if (uiPanel43 != null && !uiPanel43.IsDisposed)
// {
// uiPanel43.Visible = isHighTempMode;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel43(高温加水指示灯) Visible = {isHighTempMode}");
// }
// // 水箱加热按钮 - uiButton4用户操作设定区域
// if (uiButton4 != null && !uiButton4.IsDisposed)
// {
// uiButton4.Visible = isHighTempMode;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiButton4(水箱加热) Visible = {isHighTempMode}");
// }
// // 出口温度判定设置 - uiLabel48和uiTextBox9用户操作设定区域
// if (uiLabel48 != null && !uiLabel48.IsDisposed)
// {
// uiLabel48.Visible = isHighTempMode;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiLabel48(出口温度标签) Visible = {isHighTempMode}");
// }
// if (uiTextBox9 != null && !uiTextBox9.IsDisposed)
// {
// uiTextBox9.Visible = isHighTempMode;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiTextBox9(出口温度输入) Visible = {isHighTempMode}");
// }
// // 写入按钮 - uiButton8用于写入联络单号和件号数据到PLC常温和高温模式都需要
// // 注意:写入按钮应该始终显示,因为联络单号和件号在两种模式下都需要
// if (uiButton8 != null && !uiButton8.IsDisposed)
// {
// uiButton8.Visible = true; // 始终显示写入按钮
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiButton8(写入按钮) Visible = true (始终显示)");
// }
// // 箱体温度设置 - uiLabel37和uiTextBox4用户操作设定区域
// if (uiLabel37 != null && !uiLabel37.IsDisposed)
// {
// uiLabel37.Visible = isHighTempMode;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiLabel37(箱体温度标签) Visible = {isHighTempMode}");
// }
// if (uiTextBox4 != null && !uiTextBox4.IsDisposed)
// {
// uiTextBox4.Visible = isHighTempMode;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiTextBox4(箱体温度输入) Visible = {isHighTempMode}");
// }
// // ========== 常温模式专属控件(常温模式显示,高温模式隐藏) ==========
// // 常温实时液位(mm) - uiPanel23包含uiLabel12, uiLabel16, uiLabel17, uiProcessBar1
// if (uiPanel23 != null && !uiPanel23.IsDisposed)
// {
// uiPanel23.Visible = !isHighTempMode;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel23(常温液位) Visible = {!isHighTempMode}");
// }
// // 常温加水按钮 - uiButton13
// if (uiButton13 != null && !uiButton13.IsDisposed)
// {
// uiButton13.Visible = !isHighTempMode;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiButton13(常温加水) Visible = {!isHighTempMode}");
// }
// // 常温加水指示灯 - uiLight6
// if (uiPanel38 != null && !uiPanel38.IsDisposed)
// {
// uiPanel38.Visible = !isHighTempMode;
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel38(常温加水指示灯) Visible = {!isHighTempMode}");
// }
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] 模式切换完成 - 高温模式: {isHighTempMode}");
// }
// catch (ObjectDisposedException ex)
// {
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] 控件已释放: {ex.Message}");
// }
// catch (Exception ex)
// {
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] 更新控件可见性失败: {ex.Message}");
// Debug.WriteLine($"[UpdateControlsVisibilityByMode] 堆栈跟踪: {ex.StackTrace}");
// }
//}
private void UpdateControlsVisibilityByModeOptimized()
private void UpdateControlsVisibilityByMode()
{
// 在更新前暂停布局计算
this.SuspendLayout();
try
{
// 判断当前是否为高温模式uiLight2亮起表示高温模式
bool isHighTempMode = uiLight2 != null && uiLight2.State == UILightState.On;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] 开始更新 - 高温模式: {isHighTempMode}, 常温模式: {isLowTempMode}");
// 暂停布局更新,批量修改控件属性,减少重绘次数
this.SuspendLayout();
// ========== 高温模式专属控件(高温模式显示,常温模式隐藏) ==========
// 高温实时液位(mm) - uiPanel31包含uiLabel7, uiLabel8, uiLabel34, uiProcessBar2
// 高温实时液位(mm)
if (uiPanel31 != null && !uiPanel31.IsDisposed)
{
uiPanel31.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel31(高温液位) Visible = {isHighTempMode}");
}
// 箱体温度(°C) - uiPanel35包含uiLabel42, uiLabel43实时数据监控区域
// 箱体温度(°C)
if (uiPanel35 != null && !uiPanel35.IsDisposed)
{
uiPanel35.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel35(箱体温度) Visible = {isHighTempMode}");
}
// 出口温度(°C) - uiPanel33包含uiLabel38, uiLabel39实时数据监控区域
// 出口温度(°C)
if (uiPanel33 != null && !uiPanel33.IsDisposed)
{
uiPanel33.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel33(出口温度) Visible = {isHighTempMode}");
}
// 高温加水按钮 - uiButton5
// 高温加水按钮
if (uiButton5 != null && !uiButton5.IsDisposed)
{
uiButton5.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiButton5(高温加水) Visible = {isHighTempMode}");
}
// 高温加水指示灯 - uiLight11
// 高温加水指示灯
if (uiPanel43 != null && !uiPanel43.IsDisposed)
{
uiPanel43.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel43(高温加水指示灯) Visible = {isHighTempMode}");
}
// 水箱加热按钮 - uiButton4用户操作设定区域
// 水箱加热按钮
if (uiButton4 != null && !uiButton4.IsDisposed)
{
uiButton4.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiButton4(水箱加热) Visible = {isHighTempMode}");
}
// 出口温度判定设置 - uiLabel48和uiTextBox9用户操作设定区域
// 出口温度判定设置
if (uiLabel48 != null && !uiLabel48.IsDisposed)
{
uiLabel48.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiLabel48(出口温度标签) Visible = {isHighTempMode}");
}
if (uiTextBox9 != null && !uiTextBox9.IsDisposed)
{
uiTextBox9.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiTextBox9(出口温度输入) Visible = {isHighTempMode}");
}
// 写入按钮 - uiButton8用于写入联络单号和件号数据到PLC常温和高温模式都需要
// 注意:写入按钮应该始终显示,因为联络单号和件号在两种模式下都需要
if (uiButton8 != null && !uiButton8.IsDisposed)
{
uiButton8.Visible = true; // 始终显示写入按钮
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiButton8(写入按钮) Visible = true (始终显示)");
}
// 箱体温度设置 - uiLabel37和uiTextBox4用户操作设定区域
// 箱体温度设置
if (uiLabel37 != null && !uiLabel37.IsDisposed)
{
uiLabel37.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiLabel37(箱体温度标签) Visible = {isHighTempMode}");
}
if (uiTextBox4 != null && !uiTextBox4.IsDisposed)
{
uiTextBox4.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiTextBox4(箱体温度输入) Visible = {isHighTempMode}");
}
// ========== 常温模式专属控件(常温模式显示,高温模式隐藏) ==========
// 常温实时液位(mm) - uiPanel23包含uiLabel12, uiLabel16, uiLabel17, uiProcessBar1
// 常温实时液位(mm)
if (uiPanel23 != null && !uiPanel23.IsDisposed)
{
uiPanel23.Visible = !isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel23(常温液位) Visible = {!isHighTempMode}");
}
// 常温加水按钮 - uiButton13
// 常温加水按钮
if (uiButton13 != null && !uiButton13.IsDisposed)
{
uiButton13.Visible = !isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiButton13(常温加水) Visible = {!isHighTempMode}");
}
// 常温加水指示灯 - uiLight6
// 常温加水指示灯
if (uiPanel38 != null && !uiPanel38.IsDisposed)
{
uiPanel38.Visible = !isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel38(常温加水指示灯) Visible = {!isHighTempMode}");
}
// 写入按钮始终显示
if (uiButton8 != null && !uiButton8.IsDisposed)
uiButton8.Visible = true;
// 恢复布局更新,一次性重绘所有变更
this.ResumeLayout(true);
Debug.WriteLine($"[UpdateControlsVisibilityByMode] 模式切换完成 - 高温模式: {isHighTempMode}");
}
catch (ObjectDisposedException ex)
{
Debug.WriteLine($"[UpdateControlsVisibilityByMode] 控件已释放: {ex.Message}");
// 确保恢复布局
try { this.ResumeLayout(true); } catch { }
}
catch (Exception ex)
{
Debug.WriteLine($"[UpdateControlsVisibilityByMode] 更新控件可见性失败: {ex.Message}");
Debug.WriteLine($"[UpdateControlsVisibilityByMode] 堆栈跟踪: {ex.StackTrace}");
// 确保恢复布局
try { this.ResumeLayout(true); } catch { }
}
}
}
}