diff --git a/CurveCaptureHelper.cs b/CurveCaptureHelper.cs index aa6a82e..294fc73 100644 --- a/CurveCaptureHelper.cs +++ b/CurveCaptureHelper.cs @@ -73,6 +73,11 @@ namespace PLCDataMonitor public float? D { get; set; } + + + // 【新增】用于存储净运行时间(秒),用于导出和绘图 + public double ElapsedSeconds { get; set; } + public string Id { get; set; } // 构造函数(用于创建数据实例) diff --git a/CurvePage.xaml.cs b/CurvePage.xaml.cs index d8ee604..989c9e2 100644 --- a/CurvePage.xaml.cs +++ b/CurvePage.xaml.cs @@ -666,6 +666,12 @@ namespace PLCDataMonitor private void ClearCurveButton_Click(object sender, RoutedEventArgs e) { ClearCurve(); + + + + // 新增:重置主窗口计时器 + var mainWindow = System.Windows.Window.GetWindow(this) as MainWindow; + if (mainWindow != null) { mainWindow.ResetTestTimer(); } } public void ClearCurve() diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 45bb0aa..2a3669f 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -55,6 +55,16 @@ namespace PLCDataMonitor public List<(DateTime start, DateTime end)> PausePeriods => _pausePeriods; + + + + // 【新增】用于记录净运行时间的秒表 + private System.Diagnostics.Stopwatch _testStopwatch = new System.Diagnostics.Stopwatch(); + // 【新增】用于检测新一轮测试开始 + private int _lastActualCount = 0; + private int _lastSetCount = 0; + + // 添加清空方法 public void ClearPausePeriods() { @@ -370,6 +380,61 @@ namespace PLCDataMonitor } + + + + + + + + + // 【核心修改】1. 控制秒表逻辑 (处理暂停和重新开始) + // 判断是否是新的一轮测试开始 (实际次数归1,或者从上一轮结束状态变为1) + bool isNewTestStart = (data.actualCount == 1 && _lastActualCount == 0) || + (data.actualCount == 1 && _lastActualCount == _lastSetCount && _lastSetCount > 0); + + if (isNewTestStart) + { + // 新一轮测试,重置并启动秒表 + _testStopwatch.Restart(); + } + else + { + // 非新一轮,根据暂停状态控制秒表 + if (!isPaused && data.setCount > 0 && data.actualCount > 0) + { + // 测试进行中且未暂停 -> 启动秒表 (如果之前是停的) + if (!_testStopwatch.IsRunning) _testStopwatch.Start(); + } + else + { + // 暂停中 或 测试未开始 -> 停止秒表 + if (_testStopwatch.IsRunning) _testStopwatch.Stop(); + } + } + + // 更新记录,供下一次循环判断 + _lastActualCount = data.actualCount; + _lastSetCount = data.setCount; + + // 【核心修改】2. 生成“净运行时间”戳 + // 使用 DateTime.MinValue 作为基准,加上秒表时间。 + // 这样计算 TotalSeconds 时,得到的就是纯粹的秒数,不包含暂停时间。 + DateTime effectiveTime = DateTime.MinValue.AddMilliseconds(_testStopwatch.ElapsedMilliseconds); + + + + + + + + + + + + + + // --- 结束记录暂停时间段 --- var pressure1 = data.pressure1; @@ -472,18 +537,32 @@ namespace PLCDataMonitor if (data.setCount > 0 && data.actualCount > 0 && data.setCount == data.actualCount && !_hasInsertedReport) // 一次测试执行完成,保存报表 { - // 创建报表记录(时间+压力+最大摩擦力) + //// 创建报表记录(时间+压力+最大摩擦力) + //var reportRecord = new ReportData( + // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), // 当前时间 + // pressure1.ToString("F1"), // 工位1压力 + // pressure2.ToString("F1"), // 工位2压力 + // maxFriction1.ToString("F1"), // 工位1最大摩擦力 + // maxFriction2.ToString("F1"), + // data.t1, + // data.t2, + // data.D + //); + // 获取当前的净运行秒数 + double currentElapsedSec = _testStopwatch.ElapsedMilliseconds / 1000.0; + var reportRecord = new ReportData( - DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), // 当前时间 - pressure1.ToString("F1"), // 工位1压力 - pressure2.ToString("F1"), // 工位2压力 - maxFriction1.ToString("F1"), // 工位1最大摩擦力 + currentElapsedSec.ToString(), // 绝对时间可以保留作为“发生时刻”,或者改为显示秒数 + pressure1.ToString("F1"), + pressure2.ToString("F1"), + maxFriction1.ToString("F1"), maxFriction2.ToString("F1"), data.t1, data.t2, data.D + // 注意:如果你的 ReportData 构造函数没有 ElapsedSeconds 参数,需要重载构造函数或直接赋值 ); - + reportRecord.ElapsedSeconds = currentElapsedSec; // 插入报表 _reportPage.AddReportRecord(reportRecord); // 标记为已插入(避免同一批次重复插入) @@ -531,6 +610,23 @@ namespace PLCDataMonitor } } + + + + + public void ResetTestTimer() + { + if (_testStopwatch != null) + { + _testStopwatch.Reset(); + } + // 同时也重置一下计数记录,防止逻辑判断错误 + _lastActualCount = 0; + _lastSetCount = 0; + } + + + private (float pressure1, float pressure2, float friction1, float friction2, int setCount, int actualCount, float maxFriction1, float maxFriction2, float t1, float t2, float D, bool status) ReadModbusData() { diff --git a/ReportPage.xaml.cs b/ReportPage.xaml.cs index 53b3103..e0d957c 100644 --- a/ReportPage.xaml.cs +++ b/ReportPage.xaml.cs @@ -95,6 +95,26 @@ namespace PLCDataMonitor reportSheet.Cells[4, col].Style.Border.Right.Style = ExcelBorderStyle.Thin; } reportSheet.Row(4).Height = 25; + + + + + + + + + + + + + + + + + + + + // ========== 处理曲线数据,过滤暂停段 ========== var mainWindow = (MainWindow)Application.Current.MainWindow; @@ -296,6 +316,24 @@ namespace PLCDataMonitor curveSheet.Row(1).Height = 25; // 写入过滤后的数据和重建的时间轴 + + + + + + + + + + + + + + + + + + int dataRow = 2; for (int i = 0; i < filteredData.Count; i++) {