This commit is contained in:
wxt
2026-01-16 16:59:07 +08:00
2 changed files with 117 additions and 187 deletions

View File

@@ -38,7 +38,8 @@ namespace 全自动水压检测仪
{ {
InitializeComponent(); InitializeComponent();
_cts = new CancellationTokenSource(); _cts = new CancellationTokenSource();
InitTimer(); // 初始化定时器 // 只创建定时器,不在构造器中启动,避免在 Load 前访问未初始化的资源(如 c / _modbusMaster
_readTimer = InitTimer(); // 初始化定时器并保存引用
} }
private System.Windows.Forms.Timer InitTimer() private System.Windows.Forms.Timer InitTimer()
@@ -58,7 +59,7 @@ namespace 全自动水压检测仪
catch { } catch { }
} }
}; };
timer.Start(); // 不在此处 Start等待 Load 完成后再 Start()
return timer; return timer;
} }
@@ -74,7 +75,10 @@ namespace 全自动水压检测仪
{ {
// 前置校验:连接状态检查 // 前置校验:连接状态检查
if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null) return; if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null) return;
// 确保 UI 已可用
if (!this.IsHandleCreated || this.IsDisposed) return;
try try
{ {
var registerConfigs = new List<RegisterConfig> var registerConfigs = new List<RegisterConfig>
@@ -114,8 +118,8 @@ namespace 全自动水压检测仪
} }
catch (Exception ex) catch (Exception ex)
{ {
_readTimer.Stop(); _readTimer?.Stop();
UIMessageBox.ShowError($"读取系数失败:{ex.Message}"); UIMessageBox.ShowError($"读取系数失败:{ex.Message}");
} }
} }
@@ -187,7 +191,7 @@ namespace 全自动水压检测仪
} }
})); }));
} }
catch(Exception ex) catch (Exception ex)
{ {
Debug.WriteLine($"[错误] 更新按钮状态失败: {ex.Message}"); Debug.WriteLine($"[错误] 更新按钮状态失败: {ex.Message}");
} }
@@ -215,6 +219,9 @@ namespace 全自动水压检测仪
} }
ma = new Function(_modbusMaster); ma = new Function(_modbusMaster);
c = new DataChange(); c = new DataChange();
// 在 Load 完成 Modbus 初始化与 DataChange 初始化后再启动读定时器
_readTimer?.Start();
} }
@@ -406,7 +413,7 @@ namespace 全自动水压检测仪
//水循环 //水循环
private void uiButton2_Click(object sender, EventArgs e) private void uiButton2_Click(object sender, EventArgs e)
{ {
ma?.BtnClickFunctionForNew(Function.ButtonType., 10000); ma?.BtnClickFunctionForNew(Function.ButtonType., 10000);
} }
@@ -424,4 +431,4 @@ namespace 全自动水压检测仪
ma?.WriteToPLCForNew(uiTextBox2.Text.Trim(), 2414, Function.DataType.); ma?.WriteToPLCForNew(uiTextBox2.Text.Trim(), 2414, Function.DataType.);
} }
} }
} }

View File

@@ -38,6 +38,7 @@ namespace 全自动水压检测仪
DataChange c; DataChange c;
private System.Windows.Forms.Timer _readTimer; private System.Windows.Forms.Timer _readTimer;
private System.Windows.Forms.Timer _readTimerTwo; // 第二个定时器引用
private bool _isManualInput = false; // 手动输入标记 private bool _isManualInput = false; // 手动输入标记
private bool _isSwitchingWindow = false; // 窗口切换标记,避免并发 private bool _isSwitchingWindow = false; // 窗口切换标记,避免并发
@@ -47,8 +48,9 @@ namespace 全自动水压检测仪
{ {
InitializeComponent(); InitializeComponent();
pressStopwatch = new Stopwatch(); pressStopwatch = new Stopwatch();
InitTimer(); // 初始化定时器 // 只创建定时器,不在构造器中启动,避免在 Load 前访问未初始化的资源
InitTimerTwo(); // 初始化定时器 _readTimer = InitTimer(); // 保存引用
_readTimerTwo = InitTimerTwo(); // 保存第二个定时器引用
_repository = new ConductivityRepository(); _repository = new ConductivityRepository();
CurrentReport = new List<ConductivityTestData>(); CurrentReport = new List<ConductivityTestData>();
} }
@@ -70,7 +72,7 @@ namespace 全自动水压检测仪
catch { } catch { }
} }
}; };
timer.Start(); // 不在此处 Start
return timer; return timer;
} }
private System.Windows.Forms.Timer InitTimerTwo() private System.Windows.Forms.Timer InitTimerTwo()
@@ -90,98 +92,10 @@ namespace 全自动水压检测仪
catch { } catch { }
} }
}; };
timer.Start(); // 不在此处 Start
return timer; return timer;
} }
//private async System.Threading.Tasks.Task ReadLeakTestParametersAsync()
//{
// if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null)
// {
// MessageBox.Show("TCP未连接或ModbusMaster未初始化", "提示");
// return;
// }
// try
// {
// var modbusData = await Task.Run(() =>
// {
// return new
// {
// //读取所有需要的寄存器(同步操作,但是在后台线程)
// Real1 = _modbusMaster.ReadHoldingRegisters(1, 3130, 2),// 实时压力
// Real2 = _modbusMaster.ReadHoldingRegisters(1, 3080, 2),// 常温实时液位
// Real3 = _modbusMaster.ReadHoldingRegisters(1, 2100, 2),// 初始压力
// Real4 = _modbusMaster.ReadHoldingRegisters(1, 2102, 2),// 结束压力
// Real5 = _modbusMaster.ReadHoldingRegisters(1, 2104, 2),// 压差
// Real6 = _modbusMaster.ReadHoldingRegisters(1, 2082, 2),// 计时s
// Real7 = _modbusMaster.ReadHoldingRegisters(1, 2084, 2),// 计时min
// Real8 = _modbusMaster.ReadHoldingRegisters(1, 2086, 2),// 计时h
// Real9 = _modbusMaster.ReadHoldingRegisters(1, 3030, 2),// 高温实时液位
// Real10 = _modbusMaster.ReadHoldingRegisters(1, 3480, 2),// 箱体温度
// Real11 = _modbusMaster.ReadHoldingRegisters(1, 3180, 2),// 出口温度
// CurrentTime = DateTime.Now // 后台线程获取时间不影响
// };
// });
// // 数据读取完成后回到UI线程更新控件await会自动捕获上下文此处已是UI线程
// // 时间显示
// uiLabel5.Text = modbusData.CurrentTime.ToString("yyyy-MM-dd HH:mm:ss");
// // 实时压力
// var value0 = c.UshortToFloat(modbusData.Real1[1], modbusData.Real1[0]);
// uiLabel28.Text = value0.ToString("F2");
// // 常温实时液位
// var value1 = c.UshortToFloat(modbusData.Real2[1], modbusData.Real2[0]);
// uiLabel12.Text = value1.ToString("F2");
// // 初始压力
// var value2 = c.UshortToFloat(modbusData.Real3[1], modbusData.Real3[0]);
// uiLabel14.Text = value2.ToString("F2");
// // 结束压力
// var value3 = c.UshortToFloat(modbusData.Real4[1], modbusData.Real4[0]);
// uiLabel19.Text = value3.ToString("F2");
// // 压差
// var value4 = c.UshortToFloat(modbusData.Real5[1], modbusData.Real5[0]);
// uiLabel22.Text = value4.ToString("F2");
// // 计时s
// int value5 = modbusData.Real6[0];
// uiLabel25.Text = value5.ToString();
// // 计时min
// int value6 = modbusData.Real7[0];
// uiLabel9.Text = value6.ToString();
// // 计时h
// int value7 = modbusData.Real8[0];
// uiLabel31.Text = value7.ToString();
// // 高温实时液位
// var value8 = c.UshortToFloat(modbusData.Real9[1], modbusData.Real9[0]);
// uiLabel7.Text = value8.ToString("F2");
// // 箱体温度
// var value9 = c.UshortToFloat(modbusData.Real10[1], modbusData.Real10[0]);
// uiLabel42.Text = value9.ToString("F1");
// // 出口温度
// var value10 = c.UshortToFloat(modbusData.Real11[1], modbusData.Real11[0]);
// uiLabel38.Text = value10.ToString("F1");
// }
// catch (Exception ex)
// {
// _readTimer.Stop();
// MessageBox.Show($"读取调试参数失败:{ex.Message}", "错误",
// MessageBoxButtons.OK, MessageBoxIcon.Error);
// }
//}
private async System.Threading.Tasks.Task ReadLeakTestParametersAsync() private async System.Threading.Tasks.Task ReadLeakTestParametersAsync()
{ {
// 检查连接状态 // 检查连接状态
@@ -191,7 +105,7 @@ namespace 全自动水压检测仪
return; return;
} }
// 检查UI控件是否可用如果方法可能在窗体加载前调用) // 检查UI控件是否可用避免在窗体释放前调用)
if (!this.IsHandleCreated || this.IsDisposed) if (!this.IsHandleCreated || this.IsDisposed)
{ {
return; return;
@@ -205,28 +119,29 @@ namespace 全自动水压检测仪
{ {
return new return new
{ {
//读取所需要的寄存器(同步操作,但在后台线程) //读取所需要的寄存器(同步操作,但在后台线程)
Real1 = SafelyReadRegisters(1, 3130, 2), // 实时压力 Real1 = SafelyReadRegisters(1, 3130, 2),// 实时压力
Real2 = SafelyReadRegisters(1, 3080, 2), // 常温实时液位 Real2 = SafelyReadRegisters(1, 3080, 2),// 常温实时液位
Real3 = SafelyReadRegisters(1, 2100, 2), // 初始压力 Real3 = SafelyReadRegisters(1, 2100, 2),// 初始压力
Real4 = SafelyReadRegisters(1, 2102, 2), // 结束压力 Real4 = SafelyReadRegisters(1, 2102, 2),// 结束压力
Real5 = SafelyReadRegisters(1, 2104, 2), // 压差 Real5 = SafelyReadRegisters(1, 2104, 2),// 压差
Real6 = SafelyReadRegisters(1, 2082, 2), // 计时s Real6 = SafelyReadRegisters(1, 2082, 2),// 计时s
Real7 = SafelyReadRegisters(1, 2084, 2), // 计时min Real7 = SafelyReadRegisters(1, 2084, 2),// 计时min
Real8 = SafelyReadRegisters(1, 2086, 2), // 计时h Real8 = SafelyReadRegisters(1, 2086, 2),// 计时h
Real9 = SafelyReadRegisters(1, 3030, 2), // 高温实时液位 Real9 = SafelyReadRegisters(1, 3030, 2),// 高温实时液位
Real10 = SafelyReadRegisters(1, 3480, 2), // 箱体温度 Real10 = SafelyReadRegisters(1, 3480, 2),// 箱体温度
Real11 = SafelyReadRegisters(1, 3180, 2), // 出口温度 Real11 = SafelyReadRegisters(1, 3180, 2),// 出口温度
CurrentTime = DateTime.Now CurrentTime = DateTime.Now
}; };
} }
catch catch
{ {
return null; // 读取失败返回null return null; // 读取失败返回 null
} }
}); });
// 检查是否成功读取数据 // 检查是否成功读取
if (modbusData == null) if (modbusData == null)
{ {
throw new InvalidOperationException("Modbus数据读取失败"); throw new InvalidOperationException("Modbus数据读取失败");
@@ -238,14 +153,13 @@ namespace 全自动水压检测仪
throw new InvalidOperationException("数据转换工具未初始化"); throw new InvalidOperationException("数据转换工具未初始化");
} }
// 数据读取完成后回到UI线程更新控件 // 检查控件是否存在
// 检查控件是否存在且窗体未被释放
if (this.IsDisposed || !this.IsHandleCreated) if (this.IsDisposed || !this.IsHandleCreated)
{ {
return; return;
} }
// 使用BeginInvoke确保在UI线程执行 // 使用 BeginInvoke 确保在 UI 线程执行
this.BeginInvoke(new Action(() => this.BeginInvoke(new Action(() =>
{ {
try try
@@ -384,38 +298,38 @@ namespace 全自动水压检测仪
private async System.Threading.Tasks.Task ReadLeakTestParametersAsyncTwo() private async System.Threading.Tasks.Task ReadLeakTestParametersAsyncTwo()
{ {
//是否连接 //是否连接
if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null) return; if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null) return;
//测试步 //测试步
try try
{ {
ushort[] testslip = _modbusMaster?.ReadHoldingRegisters(1, 2080, 2); ushort[] testslip = _modbusMaster?.ReadHoldingRegisters(1, 2080, 2);
if (testslip == null || testslip.Length == 0) return; if (testslip == null || testslip.Length == 0) return;
int testvalue = testslip[0]; int testvalue = testslip[0];
uiLabel3.Text = testvalue.ToString(); uiLabel3.Text = testvalue.ToString();
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine($"[错误] 读取测试步(地址80)失败: {ex.Message}"); Debug.WriteLine($"[错误] 读取测试步(地址80)失败: {ex.Message}");
} }
//压力设定 //压力设定
try try
{ {
ushort[] registers = await Task.Run(() => ushort[] registers = await Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 2400, 2) _modbusMaster?.ReadHoldingRegisters(1, 2400, 2)
); );
if (registers == null || registers.Length == 0) return; if (registers == null || registers.Length == 0) return;
float pressureValue = c.UshortToFloat(registers[1], registers[0]);
this.Invoke(new Action(() => float pressureValue = c.UshortToFloat(registers[1], registers[0]);
{
uiTextBox1.Text = pressureValue.ToString("F2"); this.Invoke(new Action(() =>
})); {
uiTextBox1.Text = pressureValue.ToString("F2");
}));
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -424,18 +338,18 @@ namespace 全自动水压检测仪
//箱体温度 //箱体温度
try try
{ {
ushort[] registers = await Task.Run(() => ushort[] registers = await Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 2402, 1) _modbusMaster?.ReadHoldingRegisters(1, 2402, 1)
); );
if (registers == null || registers.Length == 0) return; if (registers == null || registers.Length == 0) return;
float registerValue = registers[0]; float registerValue = registers[0];
this.Invoke(new Action(() => this.Invoke(new Action(() =>
{ {
uiTextBox4.Text = registerValue.ToString("F2"); uiTextBox4.Text = registerValue.ToString("F2");
})); }));
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -444,19 +358,19 @@ namespace 全自动水压检测仪
//保压时间 //保压时间
try try
{ {
ushort[] registers = await Task.Run(() => ushort[] registers = await Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 2404, 2) _modbusMaster?.ReadHoldingRegisters(1, 2404, 2)
); );
if (registers == null || registers.Length == 0) return; if (registers == null || registers.Length == 0) return;
float pressureValue = c.UshortToFloat(registers[1], registers[0]); float pressureValue = c.UshortToFloat(registers[1], registers[0]);
this.Invoke(new Action(() => this.Invoke(new Action(() =>
{ {
uiTextBox5.Text = pressureValue.ToString("F2"); uiTextBox5.Text = pressureValue.ToString("F2");
})); }));
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -465,18 +379,18 @@ namespace 全自动水压检测仪
//出口温度设置 //出口温度设置
try try
{ {
ushort[] registers = await Task.Run(() => ushort[] registers = await Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 2412, 2) _modbusMaster?.ReadHoldingRegisters(1, 2412, 2)
); );
if (registers == null || registers.Length == 0) return; if (registers == null || registers.Length == 0) return;
float temperatureValue = c.UshortToFloat(registers[1], registers[0]); float temperatureValue = c.UshortToFloat(registers[1], registers[0]);
this.Invoke(new Action(() => this.Invoke(new Action(() =>
{ {
uiTextBox9.Text = temperatureValue.ToString("F2"); uiTextBox9.Text = temperatureValue.ToString("F2");
})); }));
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -486,9 +400,9 @@ namespace 全自动水压检测仪
try try
{ {
//测试按 //测试按
bool[] testStatus = _modbusMaster?.ReadCoils(1, 10081, 1); bool[] testStatus = _modbusMaster?.ReadCoils(1, 10081, 1);
if (testStatus[0]) if (testStatus != null && testStatus.Length > 0 && testStatus[0])
{ {
uiButton2.Text = "测试中"; uiButton2.Text = "测试中";
uiButton2.ForeColor = System.Drawing.Color.Red; uiButton2.ForeColor = System.Drawing.Color.Red;
@@ -505,7 +419,7 @@ namespace 全自动水压检测仪
} }
//高低温切换 //高低温切换
bool[] buttonStatus = _modbusMaster?.ReadCoils(1, 10030, 1); bool[] buttonStatus = _modbusMaster?.ReadCoils(1, 10030, 1);
if (buttonStatus[0]) if (buttonStatus != null && buttonStatus.Length > 0 && buttonStatus[0])
{ {
uiSwitch1.Active = true; uiSwitch1.Active = true;
} }
@@ -515,7 +429,7 @@ namespace 全自动水压检测仪
} }
//低温指示 //低温指示
bool[] lowStatus = _modbusMaster?.ReadCoils(1, 10031, 1); bool[] lowStatus = _modbusMaster?.ReadCoils(1, 10031, 1);
if (lowStatus[0]) if (lowStatus != null && lowStatus.Length > 0 && lowStatus[0])
{ {
uiLight1.State = UILightState.On; uiLight1.State = UILightState.On;
} }
@@ -525,7 +439,7 @@ namespace 全自动水压检测仪
} }
//高温指示 //高温指示
bool[] highStatus = _modbusMaster?.ReadCoils(1, 10032, 1); bool[] highStatus = _modbusMaster?.ReadCoils(1, 10032, 1);
if (highStatus[0]) if (highStatus != null && highStatus.Length > 0 && highStatus[0])
{ {
uiLight2.State = UILightState.On; uiLight2.State = UILightState.On;
} }
@@ -535,7 +449,7 @@ namespace 全自动水压检测仪
} }
//进气阀指示 //进气阀指示
bool[] valve1 = _modbusMaster?.ReadCoils(1, 10014, 1); bool[] valve1 = _modbusMaster?.ReadCoils(1, 10014, 1);
if (valve1[0]) if (valve1 != null && valve1.Length > 0 && valve1[0])
{ {
uiLight3.State = UILightState.On; uiLight3.State = UILightState.On;
} }
@@ -545,7 +459,7 @@ namespace 全自动水压检测仪
} }
//测试高压阀指示 //测试高压阀指示
bool[] valve2 = _modbusMaster?.ReadCoils(1, 10008, 1); bool[] valve2 = _modbusMaster?.ReadCoils(1, 10008, 1);
if (valve2[0]) if (valve2 != null && valve2.Length > 0 && valve2[0])
{ {
uiLight4.State = UILightState.On; uiLight4.State = UILightState.On;
} }
@@ -555,7 +469,7 @@ namespace 全自动水压检测仪
} }
//常温抽水阀指示 //常温抽水阀指示
bool[] valve3 = _modbusMaster?.ReadCoils(1, 10006, 1); bool[] valve3 = _modbusMaster?.ReadCoils(1, 10006, 1);
if (valve3[0]) if (valve3 != null && valve3.Length > 0 && valve3[0])
{ {
uiLight5.State = UILightState.On; uiLight5.State = UILightState.On;
} }
@@ -563,9 +477,9 @@ namespace 全自动水压检测仪
{ {
uiLight5.State = UILightState.Off; uiLight5.State = UILightState.Off;
} }
//常温水箱加水指示 //常温水箱加水指示
bool[] valve4 = _modbusMaster?.ReadCoils(1, 10013, 1); bool[] valve4 = _modbusMaster?.ReadCoils(1, 10013, 1);
if (valve4[0]) if (valve4 != null && valve4.Length > 0 && valve4[0])
{ {
uiLight6.State = UILightState.On; uiLight6.State = UILightState.On;
uiButton13.ForeColor = Color.Red; uiButton13.ForeColor = Color.Red;
@@ -579,7 +493,7 @@ namespace 全自动水压检测仪
} }
//高温抽水阀指示 //高温抽水阀指示
bool[] valve5 = _modbusMaster?.ReadCoils(1, 10005, 1); bool[] valve5 = _modbusMaster?.ReadCoils(1, 10005, 1);
if (valve5[0]) if (valve5 != null && valve5.Length > 0 && valve5[0])
{ {
uiLight7.State = UILightState.On; uiLight7.State = UILightState.On;
} }
@@ -587,9 +501,9 @@ namespace 全自动水压检测仪
{ {
uiLight7.State = UILightState.Off; uiLight7.State = UILightState.Off;
} }
//空气抽气阀指示 //真空泵指示
bool[] valve6 = _modbusMaster?.ReadCoils(1, 10007, 1); bool[] valve6 = _modbusMaster?.ReadCoils(1, 10007, 1);
if (valve6[0]) if (valve6 != null && valve6.Length > 0 && valve6[0])
{ {
uiLight8.State = UILightState.On; uiLight8.State = UILightState.On;
} }
@@ -597,9 +511,9 @@ namespace 全自动水压检测仪
{ {
uiLight8.State = UILightState.Off; uiLight8.State = UILightState.Off;
} }
//升温 //升温指示
bool[] valve7 = _modbusMaster?.ReadCoils(1, 10004, 1); bool[] valve7 = _modbusMaster?.ReadCoils(1, 10004, 1);
if (valve7[0]) if (valve7 != null && valve7.Length > 0 && valve7[0])
{ {
uiLight9.State = UILightState.On; uiLight9.State = UILightState.On;
} }
@@ -609,7 +523,7 @@ namespace 全自动水压检测仪
} }
//常温回水阀指示 //常温回水阀指示
bool[] valve8 = _modbusMaster?.ReadCoils(1, 10010, 1); bool[] valve8 = _modbusMaster?.ReadCoils(1, 10010, 1);
if (valve8[0]) if (valve8 != null && valve8.Length > 0 && valve8[0])
{ {
uiLight10.State = UILightState.On; uiLight10.State = UILightState.On;
} }
@@ -617,9 +531,9 @@ namespace 全自动水压检测仪
{ {
uiLight10.State = UILightState.Off; uiLight10.State = UILightState.Off;
} }
//高温水箱加水指示 //高温水箱加水指示
bool[] valve9 = _modbusMaster?.ReadCoils(1, 10012, 1); bool[] valve9 = _modbusMaster?.ReadCoils(1, 10012, 1);
if (valve9[0]) if (valve9 != null && valve9.Length > 0 && valve9[0])
{ {
uiLight11.State = UILightState.On; uiLight11.State = UILightState.On;
uiButton5.ForeColor = Color.Red; uiButton5.ForeColor = Color.Red;
@@ -633,7 +547,7 @@ namespace 全自动水压检测仪
} }
//高温回水阀指示 //高温回水阀指示
bool[] valve10 = _modbusMaster?.ReadCoils(1, 10009, 1); bool[] valve10 = _modbusMaster?.ReadCoils(1, 10009, 1);
if (valve10[0]) if (valve10 != null && valve10.Length > 0 && valve10[0])
{ {
uiLight12.State = UILightState.On; uiLight12.State = UILightState.On;
} }
@@ -643,7 +557,7 @@ namespace 全自动水压检测仪
} }
//系统排水指示 //系统排水指示
bool[] valve11 = _modbusMaster?.ReadCoils(1, 10070, 1); bool[] valve11 = _modbusMaster?.ReadCoils(1, 10070, 1);
if (valve11[0]) if (valve11 != null && valve11.Length > 0 && valve11[0])
{ {
uiButton11.ForeColor = Color.Red; uiButton11.ForeColor = Color.Red;
uiButton11.Text = "系统排气中"; uiButton11.Text = "系统排气中";
@@ -654,14 +568,14 @@ namespace 全自动水压检测仪
uiButton11.Text = "系统排气"; uiButton11.Text = "系统排气";
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine($"[错误] 读取阀门状态失败: {ex.Message}"); Debug.WriteLine($"[错误] 读取阀门状态失败: {ex.Message}");
} }
} }
//切数页 //切数页
private void uiButton14_Click(object sender, EventArgs e) private void uiButton14_Click(object sender, EventArgs e)
{ {
SwitchWindow(ref _coeffiicientsetting, () => new Coeffiicientsetting()); SwitchWindow(ref _coeffiicientsetting, () => new Coeffiicientsetting());
@@ -692,6 +606,10 @@ namespace 全自动水压检测仪
c = new DataChange(); c = new DataChange();
_modbusMaster?.WriteSingleCoil(1, 30, true); _modbusMaster?.WriteSingleCoil(1, 30, true);
boolSignal1.OnRisingEdge += BoolSignal1_OnRisingEdge; boolSignal1.OnRisingEdge += BoolSignal1_OnRisingEdge;
// 在 Load 完成初始化后再启动定时器,避免定时器在 c / ma 未就绪时触发访问导致异常
_readTimer?.Start();
_readTimerTwo?.Start();
} }
void BoolSignal1_OnRisingEdge() void BoolSignal1_OnRisingEdge()
@@ -715,17 +633,18 @@ namespace 全自动水压检测仪
}); });
_repository.InsertReportItems(new ConductivityTestData _repository.InsertReportItems(new ConductivityTestData
{ {
barcode = uiTextBox2.Text, barcode = uiTextBox2.Text,
CreateTime = DateTime.Now, CreateTime = DateTime.Now,
diffpressure = uiTextBox6.Text.ToDouble(), diffpressure = uiTextBox6.Text.ToDouble(),
dwelltime = uiTextBox8.Text.ToDouble(), dwelltime = uiTextBox8.Text.ToDouble(),
temperature = uiTextBox4.Text.ToDouble(), temperature = uiTextBox4.Text.ToDouble(),
endpressure = uiTextBox7.Text.ToDouble(), endpressure = uiTextBox7.Text.ToDouble(),
startpressure = uiTextBox3.Text.ToDouble(), startpressure = uiTextBox3.Text.ToDouble(),
Type = uiLight1.State == UILightState.On ? 1 : 0 Type = uiLight1.State == UILightState.On ? 1 : 0
}); });
} }
private bool TryReconnect() private bool TryReconnect()
{ {
@@ -749,6 +668,7 @@ namespace 全自动水压检测仪
{ {
_isSwitchingWindow = true; _isSwitchingWindow = true;
_readTimer?.Stop(); _readTimer?.Stop();
_readTimerTwo?.Stop(); // 停止第二个定时器
// 2. 检查资源是否可用(添加重连机制) // 2. 检查资源是否可用(添加重连机制)
if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null) if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null)
@@ -772,6 +692,7 @@ namespace 全自动水压检测仪
{ {
_isSwitchingWindow = false; _isSwitchingWindow = false;
_readTimer?.Start(); _readTimer?.Start();
_readTimerTwo?.Start(); // 恢复第二个定时器
this.Show(); this.Show();
this.Activate(); this.Activate();
})); }));
@@ -792,6 +713,8 @@ namespace 全自动水压检测仪
// 停止定时器 // 停止定时器
_readTimer?.Stop(); _readTimer?.Stop();
_readTimer?.Dispose(); _readTimer?.Dispose();
_readTimerTwo?.Stop();
_readTimerTwo?.Dispose();
// 仅用户主动关闭时退出应用 // 仅用户主动关闭时退出应用
if (e.CloseReason == CloseReason.UserClosing) if (e.CloseReason == CloseReason.UserClosing)
@@ -827,10 +750,10 @@ namespace 全自动水压检测仪
//常温加水 //常温加水
private void uiButton13_Click(object sender, EventArgs e) private void uiButton13_Click(object sender, EventArgs e)
{ {
ma?.BtnClickFunctionForNew(Function.ButtonType., 10013); ma?.BtnClickFunctionForNew(Function.ButtonType., 10013);
} }
//隐藏 //隐藏
private void uiButton14_MouseDown(object sender, MouseEventArgs e) private void uiButton14_MouseDown(object sender, MouseEventArgs e)
{ {
@@ -905,4 +828,4 @@ namespace 全自动水压检测仪
} }
#endregion #endregion
} }
} }