From 6d4de4176313737daa18d5d39f8861a0caf95846 Mon Sep 17 00:00:00 2001
From: xyy <544939200@qq.com>
Date: Tue, 24 Feb 2026 18:24:09 +0800
Subject: [PATCH]
---
MainWindow.xaml | 89 +++++++++++++++++++++++++++++++++++-----------
MainWindow.xaml.cs | 60 +++++++++++++++++++++++++++++--
2 files changed, 127 insertions(+), 22 deletions(-)
diff --git a/MainWindow.xaml b/MainWindow.xaml
index a1a6dad..6ebe43b 100644
--- a/MainWindow.xaml
+++ b/MainWindow.xaml
@@ -5,6 +5,48 @@
Width="1024" Height="768"
WindowStartupLocation="CenterScreen"
FontSize="13">
+
+
+
+
+
@@ -43,8 +85,8 @@
-
-
+
+
@@ -70,36 +112,43 @@
+
-
-
-
+
+
+
+
+
-
-
-
+
+
+
+
+
-
-
-
+
+
+
+
+
-
-
-
+
+
+
@@ -116,9 +165,9 @@
-
-
-
+
+
+
@@ -195,9 +244,9 @@
-
-
-
+
+
+
diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs
index d06fe01..9e8e2f3 100644
--- a/MainWindow.xaml.cs
+++ b/MainWindow.xaml.cs
@@ -8,6 +8,7 @@ using System.Linq;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
+using System.Windows.Threading;
namespace PetroleumViscosityTest
{
@@ -26,6 +27,12 @@ namespace PetroleumViscosityTest
private System.Diagnostics.Stopwatch stopwatch2 = new System.Diagnostics.Stopwatch();
private System.Diagnostics.Stopwatch stopwatch3 = new System.Diagnostics.Stopwatch();
private System.Diagnostics.Stopwatch stopwatch4 = new System.Diagnostics.Stopwatch();
+
+ // 新增:四个 DispatcherTimer,用于实时更新
+ private DispatcherTimer timer1 = new DispatcherTimer();
+ private DispatcherTimer timer2 = new DispatcherTimer();
+ private DispatcherTimer timer3 = new DispatcherTimer();
+ private DispatcherTimer timer4 = new DispatcherTimer();
// --------------------------------
public MainWindow()
@@ -37,14 +44,27 @@ namespace PetroleumViscosityTest
// --- 新增:初始化串口通信 ---
InitSerialPort();
+
+ // 初始化 DispatcherTimer
+ timer1.Interval = TimeSpan.FromMilliseconds(100);
+ timer1.Tick += (s, e) => { txtTime1.Text = (stopwatch1.Elapsed.TotalSeconds).ToString("F3"); };
+
+ timer2.Interval = TimeSpan.FromMilliseconds(100);
+ timer2.Tick += (s, e) => { txtTime2.Text = (stopwatch2.Elapsed.TotalSeconds).ToString("F3"); };
+
+ timer3.Interval = TimeSpan.FromMilliseconds(100);
+ timer3.Tick += (s, e) => { txtTime3.Text = (stopwatch3.Elapsed.TotalSeconds).ToString("F3"); };
+
+ timer4.Interval = TimeSpan.FromMilliseconds(100);
+ timer4.Tick += (s, e) => { txtTime4.Text = (stopwatch4.Elapsed.TotalSeconds).ToString("F3"); };
// --------------------------------
}
-
+
private void InitSerialPort()
{
try
{
- serialPort = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One); // 请根据实际COM口修改
+ serialPort = new SerialPort("COM7", 9600, Parity.None, 8, StopBits.One); // 请根据实际COM口修改
serialPort.DataReceived += SerialPort_DataReceived;
serialPort.Open();
@@ -315,6 +335,21 @@ namespace PetroleumViscosityTest
txtRepeatJudge.Clear();
lstRawTimes.ItemsSource = null;
rawTimes.Clear();
+
+ // 停止并重置所有计时器
+ timer1.Stop();
+ timer2.Stop();
+ timer3.Stop();
+ timer4.Stop();
+ stopwatch1.Reset();
+ stopwatch2.Reset();
+ stopwatch3.Reset();
+ stopwatch4.Reset();
+
+
+
+
+
txtStatus.Text = "已清空";
}
@@ -328,6 +363,15 @@ namespace PetroleumViscosityTest
txtTime5.Clear();
txtTime6.Clear();
rawTimes.Clear();
+
+ timer1.Stop();
+ timer2.Stop();
+ timer3.Stop();
+ timer4.Stop();
+ stopwatch1.Reset();
+ stopwatch2.Reset();
+ stopwatch3.Reset();
+ stopwatch4.Reset();
lstRawTimes.ItemsSource = null;
}
@@ -539,12 +583,15 @@ namespace PetroleumViscosityTest
// 第1次
private void BtnStart1_Click(object sender, RoutedEventArgs e)
{
+ timer1.Stop();
stopwatch1.Reset();
stopwatch1.Start();
+ timer1.Start();
}
private void BtnStop1_Click(object sender, RoutedEventArgs e)
{
+ timer1.Stop();
stopwatch1.Stop();
txtTime1.Text = (stopwatch1.Elapsed.TotalSeconds).ToString("F3");
}
@@ -552,12 +599,15 @@ namespace PetroleumViscosityTest
// 第2次
private void BtnStart2_Click(object sender, RoutedEventArgs e)
{
+ timer2.Stop();
stopwatch2.Reset();
stopwatch2.Start();
+ timer2.Start();
}
private void BtnStop2_Click(object sender, RoutedEventArgs e)
{
+ timer2.Stop();
stopwatch2.Stop();
txtTime2.Text = (stopwatch2.Elapsed.TotalSeconds).ToString("F3");
}
@@ -565,12 +615,15 @@ namespace PetroleumViscosityTest
// 第3次
private void BtnStart3_Click(object sender, RoutedEventArgs e)
{
+ timer3.Stop();
stopwatch3.Reset();
stopwatch3.Start();
+ timer3.Start();
}
private void BtnStop3_Click(object sender, RoutedEventArgs e)
{
+ timer3.Stop();
stopwatch3.Stop();
txtTime3.Text = (stopwatch3.Elapsed.TotalSeconds).ToString("F3");
}
@@ -578,12 +631,15 @@ namespace PetroleumViscosityTest
// 第4次
private void BtnStart4_Click(object sender, RoutedEventArgs e)
{
+ timer4.Stop();
stopwatch4.Reset();
stopwatch4.Start();
+ timer4.Start();
}
private void BtnStop4_Click(object sender, RoutedEventArgs e)
{
+ timer4.Stop();
stopwatch4.Stop();
txtTime4.Text = (stopwatch4.Elapsed.TotalSeconds).ToString("F3");
}