This commit is contained in:
xyy
2026-02-24 18:24:09 +08:00
parent 35db366dfe
commit 6d4de41763
2 changed files with 127 additions and 22 deletions

View File

@@ -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");
}