This commit is contained in:
xyy
2026-03-25 21:19:50 +08:00
parent aa4f3e1f4c
commit fe2332d56d
4 changed files with 151 additions and 6 deletions

View File

@@ -73,6 +73,11 @@ namespace PLCDataMonitor
public float? D { get; set; } public float? D { get; set; }
// 【新增】用于存储净运行时间(秒),用于导出和绘图
public double ElapsedSeconds { get; set; }
public string Id { get; set; } public string Id { get; set; }
// 构造函数(用于创建数据实例) // 构造函数(用于创建数据实例)

View File

@@ -666,6 +666,12 @@ namespace PLCDataMonitor
private void ClearCurveButton_Click(object sender, RoutedEventArgs e) private void ClearCurveButton_Click(object sender, RoutedEventArgs e)
{ {
ClearCurve(); ClearCurve();
// 新增:重置主窗口计时器
var mainWindow = System.Windows.Window.GetWindow(this) as MainWindow;
if (mainWindow != null) { mainWindow.ResetTestTimer(); }
} }
public void ClearCurve() public void ClearCurve()

View File

@@ -55,6 +55,16 @@ namespace PLCDataMonitor
public List<(DateTime start, DateTime end)> PausePeriods => _pausePeriods; 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() 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; var pressure1 = data.pressure1;
@@ -472,18 +537,32 @@ namespace PLCDataMonitor
if (data.setCount > 0 && data.actualCount > 0 && if (data.setCount > 0 && data.actualCount > 0 &&
data.setCount == data.actualCount && !_hasInsertedReport) // 一次测试执行完成,保存报表 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( var reportRecord = new ReportData(
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), // 当前时间 currentElapsedSec.ToString(), // 绝对时间可以保留作为“发生时刻”,或者改为显示秒数
pressure1.ToString("F1"), // 工位1压力 pressure1.ToString("F1"),
pressure2.ToString("F1"), // 工位2压力 pressure2.ToString("F1"),
maxFriction1.ToString("F1"), // 工位1最大摩擦力 maxFriction1.ToString("F1"),
maxFriction2.ToString("F1"), maxFriction2.ToString("F1"),
data.t1, data.t1,
data.t2, data.t2,
data.D data.D
// 注意:如果你的 ReportData 构造函数没有 ElapsedSeconds 参数,需要重载构造函数或直接赋值
); );
reportRecord.ElapsedSeconds = currentElapsedSec;
// 插入报表 // 插入报表
_reportPage.AddReportRecord(reportRecord); _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, 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() int setCount, int actualCount, float maxFriction1, float maxFriction2, float t1, float t2, float D, bool status) ReadModbusData()
{ {

View File

@@ -95,6 +95,26 @@ namespace PLCDataMonitor
reportSheet.Cells[4, col].Style.Border.Right.Style = ExcelBorderStyle.Thin; reportSheet.Cells[4, col].Style.Border.Right.Style = ExcelBorderStyle.Thin;
} }
reportSheet.Row(4).Height = 25; reportSheet.Row(4).Height = 25;
// ========== 处理曲线数据,过滤暂停段 ========== // ========== 处理曲线数据,过滤暂停段 ==========
var mainWindow = (MainWindow)Application.Current.MainWindow; var mainWindow = (MainWindow)Application.Current.MainWindow;
@@ -296,6 +316,24 @@ namespace PLCDataMonitor
curveSheet.Row(1).Height = 25; curveSheet.Row(1).Height = 25;
// 写入过滤后的数据和重建的时间轴 // 写入过滤后的数据和重建的时间轴
int dataRow = 2; int dataRow = 2;
for (int i = 0; i < filteredData.Count; i++) for (int i = 0; i < filteredData.Count; i++)
{ {