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();
_cts = new CancellationTokenSource();
InitTimer(); // 初始化定时器
// 只创建定时器,不在构造器中启动,避免在 Load 前访问未初始化的资源(如 c / _modbusMaster
_readTimer = InitTimer(); // 初始化定时器并保存引用
}
private System.Windows.Forms.Timer InitTimer()
@@ -58,7 +59,7 @@ namespace 全自动水压检测仪
catch { }
}
};
timer.Start();
// 不在此处 Start等待 Load 完成后再 Start()
return timer;
}
@@ -75,6 +76,9 @@ namespace 全自动水压检测仪
// 前置校验:连接状态检查
if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null) return;
// 确保 UI 已可用
if (!this.IsHandleCreated || this.IsDisposed) return;
try
{
var registerConfigs = new List<RegisterConfig>
@@ -114,7 +118,7 @@ namespace 全自动水压检测仪
}
catch (Exception ex)
{
_readTimer.Stop();
_readTimer?.Stop();
UIMessageBox.ShowError($"读取系数失败:{ex.Message}");
}
}
@@ -215,6 +219,9 @@ namespace 全自动水压检测仪
}
ma = new Function(_modbusMaster);
c = new DataChange();
// 在 Load 完成 Modbus 初始化与 DataChange 初始化后再启动读定时器
_readTimer?.Start();
}

View File

@@ -38,6 +38,7 @@ namespace 全自动水压检测仪
DataChange c;
private System.Windows.Forms.Timer _readTimer;
private System.Windows.Forms.Timer _readTimerTwo; // 第二个定时器引用
private bool _isManualInput = false; // 手动输入标记
private bool _isSwitchingWindow = false; // 窗口切换标记,避免并发
@@ -47,8 +48,9 @@ namespace 全自动水压检测仪
{
InitializeComponent();
pressStopwatch = new Stopwatch();
InitTimer(); // 初始化定时器
InitTimerTwo(); // 初始化定时器
// 只创建定时器,不在构造器中启动,避免在 Load 前访问未初始化的资源
_readTimer = InitTimer(); // 保存引用
_readTimerTwo = InitTimerTwo(); // 保存第二个定时器引用
_repository = new ConductivityRepository();
CurrentReport = new List<ConductivityTestData>();
}
@@ -70,7 +72,7 @@ namespace 全自动水压检测仪
catch { }
}
};
timer.Start();
// 不在此处 Start
return timer;
}
private System.Windows.Forms.Timer InitTimerTwo()
@@ -90,98 +92,10 @@ namespace 全自动水压检测仪
catch { }
}
};
timer.Start();
// 不在此处 Start
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()
{
// 检查连接状态
@@ -191,7 +105,7 @@ namespace 全自动水压检测仪
return;
}
// 检查UI控件是否可用如果方法可能在窗体加载前调用)
// 检查UI控件是否可用避免在窗体释放前调用)
if (!this.IsHandleCreated || this.IsDisposed)
{
return;
@@ -205,10 +119,10 @@ namespace 全自动水压检测仪
{
return new
{
//读取所需要的寄存器(同步操作,但在后台线程)
//读取所需要的寄存器(同步操作,但在后台线程)
Real1 = SafelyReadRegisters(1, 3130, 2),// 实时压力
Real2 = SafelyReadRegisters(1, 3080, 2),// 常温实时液位
Real3 = SafelyReadRegisters(1, 2100, 2), // 初始压力
Real3 = SafelyReadRegisters(1, 2100, 2),// 初始压力
Real4 = SafelyReadRegisters(1, 2102, 2),// 结束压力
Real5 = SafelyReadRegisters(1, 2104, 2),// 压差
Real6 = SafelyReadRegisters(1, 2082, 2),// 计时s
@@ -217,6 +131,7 @@ namespace 全自动水压检测仪
Real9 = SafelyReadRegisters(1, 3030, 2),// 高温实时液位
Real10 = SafelyReadRegisters(1, 3480, 2),// 箱体温度
Real11 = SafelyReadRegisters(1, 3180, 2),// 出口温度
CurrentTime = DateTime.Now
};
}
@@ -226,7 +141,7 @@ namespace 全自动水压检测仪
}
});
// 检查是否成功读取数据
// 检查是否成功读取
if (modbusData == null)
{
throw new InvalidOperationException("Modbus数据读取失败");
@@ -238,8 +153,7 @@ namespace 全自动水压检测仪
throw new InvalidOperationException("数据转换工具未初始化");
}
// 数据读取完成后回到UI线程更新控件
// 检查控件是否存在且窗体未被释放
// 检查控件是否存在
if (this.IsDisposed || !this.IsHandleCreated)
{
return;
@@ -386,7 +300,7 @@ namespace 全自动水压检测仪
//是否连接
if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null) return;
//测试步
//测试步
try
{
ushort[] testslip = _modbusMaster?.ReadHoldingRegisters(1, 2080, 2);
@@ -401,7 +315,7 @@ namespace 全自动水压检测仪
Debug.WriteLine($"[错误] 读取测试步(地址80)失败: {ex.Message}");
}
//压力设定
//压力设定
try
{
ushort[] registers = await Task.Run(() =>
@@ -486,9 +400,9 @@ namespace 全自动水压检测仪
try
{
//测试按
//测试按
bool[] testStatus = _modbusMaster?.ReadCoils(1, 10081, 1);
if (testStatus[0])
if (testStatus != null && testStatus.Length > 0 && testStatus[0])
{
uiButton2.Text = "测试中";
uiButton2.ForeColor = System.Drawing.Color.Red;
@@ -505,7 +419,7 @@ namespace 全自动水压检测仪
}
//高低温切换
bool[] buttonStatus = _modbusMaster?.ReadCoils(1, 10030, 1);
if (buttonStatus[0])
if (buttonStatus != null && buttonStatus.Length > 0 && buttonStatus[0])
{
uiSwitch1.Active = true;
}
@@ -515,7 +429,7 @@ namespace 全自动水压检测仪
}
//低温指示
bool[] lowStatus = _modbusMaster?.ReadCoils(1, 10031, 1);
if (lowStatus[0])
if (lowStatus != null && lowStatus.Length > 0 && lowStatus[0])
{
uiLight1.State = UILightState.On;
}
@@ -525,7 +439,7 @@ namespace 全自动水压检测仪
}
//高温指示
bool[] highStatus = _modbusMaster?.ReadCoils(1, 10032, 1);
if (highStatus[0])
if (highStatus != null && highStatus.Length > 0 && highStatus[0])
{
uiLight2.State = UILightState.On;
}
@@ -535,7 +449,7 @@ namespace 全自动水压检测仪
}
//进气阀指示
bool[] valve1 = _modbusMaster?.ReadCoils(1, 10014, 1);
if (valve1[0])
if (valve1 != null && valve1.Length > 0 && valve1[0])
{
uiLight3.State = UILightState.On;
}
@@ -545,7 +459,7 @@ namespace 全自动水压检测仪
}
//测试高压阀指示
bool[] valve2 = _modbusMaster?.ReadCoils(1, 10008, 1);
if (valve2[0])
if (valve2 != null && valve2.Length > 0 && valve2[0])
{
uiLight4.State = UILightState.On;
}
@@ -555,7 +469,7 @@ namespace 全自动水压检测仪
}
//常温抽水阀指示
bool[] valve3 = _modbusMaster?.ReadCoils(1, 10006, 1);
if (valve3[0])
if (valve3 != null && valve3.Length > 0 && valve3[0])
{
uiLight5.State = UILightState.On;
}
@@ -563,9 +477,9 @@ namespace 全自动水压检测仪
{
uiLight5.State = UILightState.Off;
}
//常温水箱加水指示
//常温水箱加水指示
bool[] valve4 = _modbusMaster?.ReadCoils(1, 10013, 1);
if (valve4[0])
if (valve4 != null && valve4.Length > 0 && valve4[0])
{
uiLight6.State = UILightState.On;
uiButton13.ForeColor = Color.Red;
@@ -579,7 +493,7 @@ namespace 全自动水压检测仪
}
//高温抽水阀指示
bool[] valve5 = _modbusMaster?.ReadCoils(1, 10005, 1);
if (valve5[0])
if (valve5 != null && valve5.Length > 0 && valve5[0])
{
uiLight7.State = UILightState.On;
}
@@ -587,9 +501,9 @@ namespace 全自动水压检测仪
{
uiLight7.State = UILightState.Off;
}
//空气抽气阀指示
//真空泵指示
bool[] valve6 = _modbusMaster?.ReadCoils(1, 10007, 1);
if (valve6[0])
if (valve6 != null && valve6.Length > 0 && valve6[0])
{
uiLight8.State = UILightState.On;
}
@@ -597,9 +511,9 @@ namespace 全自动水压检测仪
{
uiLight8.State = UILightState.Off;
}
//升温
//升温指示
bool[] valve7 = _modbusMaster?.ReadCoils(1, 10004, 1);
if (valve7[0])
if (valve7 != null && valve7.Length > 0 && valve7[0])
{
uiLight9.State = UILightState.On;
}
@@ -609,7 +523,7 @@ namespace 全自动水压检测仪
}
//常温回水阀指示
bool[] valve8 = _modbusMaster?.ReadCoils(1, 10010, 1);
if (valve8[0])
if (valve8 != null && valve8.Length > 0 && valve8[0])
{
uiLight10.State = UILightState.On;
}
@@ -617,9 +531,9 @@ namespace 全自动水压检测仪
{
uiLight10.State = UILightState.Off;
}
//高温水箱加水指示
//高温水箱加水指示
bool[] valve9 = _modbusMaster?.ReadCoils(1, 10012, 1);
if (valve9[0])
if (valve9 != null && valve9.Length > 0 && valve9[0])
{
uiLight11.State = UILightState.On;
uiButton5.ForeColor = Color.Red;
@@ -633,7 +547,7 @@ namespace 全自动水压检测仪
}
//高温回水阀指示
bool[] valve10 = _modbusMaster?.ReadCoils(1, 10009, 1);
if (valve10[0])
if (valve10 != null && valve10.Length > 0 && valve10[0])
{
uiLight12.State = UILightState.On;
}
@@ -643,7 +557,7 @@ namespace 全自动水压检测仪
}
//系统排水指示
bool[] valve11 = _modbusMaster?.ReadCoils(1, 10070, 1);
if (valve11[0])
if (valve11 != null && valve11.Length > 0 && valve11[0])
{
uiButton11.ForeColor = Color.Red;
uiButton11.Text = "系统排气中";
@@ -661,7 +575,7 @@ namespace 全自动水压检测仪
}
//切数页
//切数页
private void uiButton14_Click(object sender, EventArgs e)
{
SwitchWindow(ref _coeffiicientsetting, () => new Coeffiicientsetting());
@@ -692,6 +606,10 @@ namespace 全自动水压检测仪
c = new DataChange();
_modbusMaster?.WriteSingleCoil(1, 30, true);
boolSignal1.OnRisingEdge += BoolSignal1_OnRisingEdge;
// 在 Load 完成初始化后再启动定时器,避免定时器在 c / ma 未就绪时触发访问导致异常
_readTimer?.Start();
_readTimerTwo?.Start();
}
void BoolSignal1_OnRisingEdge()
@@ -715,6 +633,7 @@ namespace 全自动水压检测仪
});
_repository.InsertReportItems(new ConductivityTestData
{
barcode = uiTextBox2.Text,
@@ -749,6 +668,7 @@ namespace 全自动水压检测仪
{
_isSwitchingWindow = true;
_readTimer?.Stop();
_readTimerTwo?.Stop(); // 停止第二个定时器
// 2. 检查资源是否可用(添加重连机制)
if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null)
@@ -772,6 +692,7 @@ namespace 全自动水压检测仪
{
_isSwitchingWindow = false;
_readTimer?.Start();
_readTimerTwo?.Start(); // 恢复第二个定时器
this.Show();
this.Activate();
}));
@@ -792,6 +713,8 @@ namespace 全自动水压检测仪
// 停止定时器
_readTimer?.Stop();
_readTimer?.Dispose();
_readTimerTwo?.Stop();
_readTimerTwo?.Dispose();
// 仅用户主动关闭时退出应用
if (e.CloseReason == CloseReason.UserClosing)