This commit is contained in:
GukSang.Jin
2026-02-04 12:51:01 +08:00
parent 343370cacc
commit 85d16f02f9
2 changed files with 207 additions and 55 deletions

View File

@@ -75,41 +75,86 @@ namespace 全自动水压检测仪
public void BtnClickFunctionForNew(ButtonType buttonType, ushort address)
{
try
int maxRetries = 3;
int retryCount = 0;
while (retryCount < maxRetries)
{
switch (buttonType)
try
{
case ButtonType.:
modbusMaster.WriteSingleCoil(1, address, true);
Thread.Sleep(100);
modbusMaster.WriteSingleCoil(1, address, false);
Thread.Sleep(100);
break;
case ButtonType.:
if (modbusMaster.ReadCoils(1, address, 1)[0])
{
modbusMaster.WriteSingleCoil(1, address, false); Thread.Sleep(100);
}
else
{ modbusMaster.WriteSingleCoil(1, address, true); Thread.Sleep(100); }
break;
case ButtonType.:
modbusMaster.WriteSingleCoil(1, address, true);
Thread.Sleep(100);
break;
case ButtonType.:
modbusMaster.WriteSingleCoil(1, address, false);
Thread.Sleep(100);
break;
default:
break;
switch (buttonType)
{
case ButtonType.:
modbusMaster.WriteSingleCoil(1, address, true);
Thread.Sleep(100);
modbusMaster.WriteSingleCoil(1, address, false);
Thread.Sleep(100);
break;
case ButtonType.:
bool currentState = false;
try
{
// 尝试读取当前状态
bool[] coils = modbusMaster.ReadCoils(1, address, 1);
if (coils != null && coils.Length > 0)
{
currentState = coils[0];
}
}
catch (Exception readEx)
{
// 如果读取失败默认写入true开启状态
System.Diagnostics.Debug.WriteLine($"[Function] 读取地址{address}失败,使用默认值: {readEx.Message}");
}
// 写入相反的状态
modbusMaster.WriteSingleCoil(1, address, !currentState);
Thread.Sleep(100);
break;
case ButtonType.:
modbusMaster.WriteSingleCoil(1, address, true);
Thread.Sleep(100);
break;
case ButtonType.:
modbusMaster.WriteSingleCoil(1, address, false);
Thread.Sleep(100);
break;
default:
break;
}
// 操作成功,退出重试循环
return;
}
catch (SlaveException slaveEx)
{
retryCount++;
System.Diagnostics.Debug.WriteLine($"[Function] Modbus从站异常 (尝试 {retryCount}/{maxRetries}): {slaveEx.Message}");
if (retryCount >= maxRetries)
{
MessageBox.Show($"操作失败!\n\nModbus从站错误: {slaveEx.SlaveExceptionCode}\n{slaveEx.Message}", "错误");
return;
}
// 等待后重试
Thread.Sleep(200);
}
catch (Exception ex)
{
retryCount++;
System.Diagnostics.Debug.WriteLine($"[Function] 操作失败 (尝试 {retryCount}/{maxRetries}): {ex.Message}");
if (retryCount >= maxRetries)
{
MessageBox.Show("操作失败!" + "\n" + "\n" + ex.Message, "错误");
return;
}
// 等待后重试
Thread.Sleep(200);
}
}
catch (Exception ex)
{
MessageBox.Show("操作失败!" + "\n" + "\n" + ex.Message, "错误");
}
}
public void WriteToPLC(string inPutValue, ushort address, DataType dataType)
{

View File

@@ -770,7 +770,14 @@ namespace 全自动水压检测仪
//高低温切换
if (modbusResults.ButtonStatus != null && modbusResults.ButtonStatus.Length > 0 && uiSwitch1 != null && !uiSwitch1.IsDisposed)
{
uiSwitch1.Active = modbusResults.ButtonStatus[0];
bool newSwitchState = modbusResults.ButtonStatus[0];
bool currentSwitchState = uiSwitch1.Active;
// 只有状态真正改变时才更新
if (newSwitchState != currentSwitchState)
{
uiSwitch1.Active = newSwitchState;
}
}
//低温指示
@@ -1271,23 +1278,59 @@ namespace 全自动水压检测仪
}
//切换实验模式
private void uiSwitch1_Click(object sender, EventArgs e)
private async void uiSwitch1_Click(object sender, EventArgs e)
{
ma?.BtnClickFunctionForNew(Function.ButtonType., 10030);
// 切换模式后更新控件可见性
// 延迟一小段时间等待PLC状态更新
System.Threading.Tasks.Task.Delay(500).ContinueWith(_ =>
try
{
SafeInvoke(() =>
// 暂停定时器避免并发访问Modbus
_readTimer?.Stop();
_readTimerTwo?.Stop();
_alarmMonitorTimer?.Stop();
// 在后台线程执行Modbus操作
await Task.Run(() =>
{
try
{
ma?.BtnClickFunctionForNew(Function.ButtonType., 10030);
}
catch (Exception ex)
{
Debug.WriteLine($"[uiSwitch1_Click] 切换模式失败: {ex.Message}");
SafeInvoke(() =>
{
MessageBox.Show($"切换模式失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
});
}
});
// 延迟等待PLC状态更新
await Task.Delay(800);
// 更新控件可见性
SafeInvoke(() =>
{
UpdateControlsVisibilityByMode();
// 更新图表模式
bool isHighTempMode = uiLight2 != null && uiLight2.State == UILightState.On;
_chartManager?.UpdateChartMode(isHighTempMode);
});
});
// 恢复定时器
_readTimer?.Start();
_readTimerTwo?.Start();
_alarmMonitorTimer?.Start();
}
catch (Exception ex)
{
Debug.WriteLine($"[uiSwitch1_Click] 异常: {ex.Message}");
// 确保定时器恢复
_readTimer?.Start();
_readTimerTwo?.Start();
_alarmMonitorTimer?.Start();
}
}
//出口温度设置
@@ -1599,54 +1642,118 @@ namespace 全自动水压检测仪
{
// 判断当前是否为高温模式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.Visible = isHighTempMode;
if (uiPanel31 != null && !uiPanel31.IsDisposed)
{
uiPanel31.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel31(高温液位) Visible = {isHighTempMode}");
}
// 箱体温度(°C) - uiPanel35包含uiLabel42, uiLabel43实时数据监控区域
if (uiPanel35 != null) uiPanel35.Visible = isHighTempMode;
if (uiPanel35 != null && !uiPanel35.IsDisposed)
{
uiPanel35.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel35(箱体温度) Visible = {isHighTempMode}");
}
// 出口温度(°C) - uiPanel33包含uiLabel38, uiLabel39实时数据监控区域
if (uiPanel33 != null) uiPanel33.Visible = isHighTempMode;
if (uiPanel33 != null && !uiPanel33.IsDisposed)
{
uiPanel33.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel33(出口温度) Visible = {isHighTempMode}");
}
// 高温加水按钮 - uiButton5
if (uiButton5 != null) uiButton5.Visible = isHighTempMode;
if (uiButton5 != null && !uiButton5.IsDisposed)
{
uiButton5.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiButton5(高温加水) Visible = {isHighTempMode}");
}
// 高温加水指示灯 - uiLight11
if (uiPanel43 != null) uiPanel43.Visible = isHighTempMode;
if (uiPanel43 != null && !uiPanel43.IsDisposed)
{
uiPanel43.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel43(高温加水指示灯) Visible = {isHighTempMode}");
}
// 水箱加热按钮 - uiButton4用户操作设定区域
if (uiButton4 != null) uiButton4.Visible = isHighTempMode;
if (uiButton4 != null && !uiButton4.IsDisposed)
{
uiButton4.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiButton4(水箱加热) Visible = {isHighTempMode}");
}
// 出口温度判定设置 - uiLabel48和uiTextBox9用户操作设定区域
if (uiLabel48 != null) uiLabel48.Visible = isHighTempMode;
if (uiTextBox9 != null) uiTextBox9.Visible = isHighTempMode;
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.Visible = isHighTempMode;
if (uiButton8 != null && !uiButton8.IsDisposed)
{
uiButton8.Visible = isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiButton8(写入按钮) Visible = {isHighTempMode}");
}
// 箱体温度设置 - uiLabel37和uiTextBox4用户操作设定区域
if (uiLabel37 != null) uiLabel37.Visible = isHighTempMode;
if (uiTextBox4 != null) uiTextBox4.Visible = isHighTempMode;
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.Visible = !isHighTempMode;
if (uiPanel23 != null && !uiPanel23.IsDisposed)
{
uiPanel23.Visible = !isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiPanel23(常温液位) Visible = {!isHighTempMode}");
}
// 常温加水按钮 - uiButton13
if (uiButton13 != null) uiButton13.Visible = !isHighTempMode;
if (uiButton13 != null && !uiButton13.IsDisposed)
{
uiButton13.Visible = !isHighTempMode;
Debug.WriteLine($"[UpdateControlsVisibilityByMode] uiButton13(常温加水) Visible = {!isHighTempMode}");
}
// 常温加水指示灯 - uiLight6
if (uiPanel38 != null) uiPanel38.Visible = !isHighTempMode;
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}");
}
}
}