调试数据

This commit is contained in:
2026-04-22 16:02:37 +08:00
parent 09017dd253
commit a982685d4b
6 changed files with 70 additions and 165 deletions

View File

@@ -22,7 +22,8 @@ namespace 头罩视野.Views
{
public partial class PageTest : Page
{
/// 只加这一个变量
private CancellationTokenSource? _cts;
private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster;
DispatcherTimer _timer;
DataChange c = new DataChange();
@@ -157,12 +158,7 @@ namespace 头罩视野.Views
TestDataStore.AddNewRecord(data);
_lastRecord = data;
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
testTimer?.Stop();
_timer?.Stop();
}
//停止btn
private void Button_Click_Stop(object sender, RoutedEventArgs e)
{
@@ -249,7 +245,12 @@ namespace 头罩视野.Views
private async Task ReadAndUpdateFloatAsync(int address, int length, System.Windows.Controls.TextBox control, string format, string unit)
{
try
{
{ // 1. 页面销毁时直接退出,不执行
if (_cts?.IsCancellationRequested == true) return;
// 2. 关键:判断对象为空就直接返回,不执行
if (_modbusMaster == null) return;
ushort[] registers = await Task.Run(async () =>
await _modbusMaster!.ReadHoldingRegistersAsync(1, (ushort)address, (ushort)length)
);
@@ -409,6 +410,14 @@ namespace 头罩视野.Views
c = new DataChange();
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
testTimer?.Stop();
_timer?.Stop();
_cts?.Cancel();
_cts = null;
}
}
}