From 1c0d7258867ceae1e2d06490d166f8f7660f154f Mon Sep 17 00:00:00 2001 From: "GukSang.Jin" Date: Wed, 31 Dec 2025 14:15:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WindowsFormsApp6/Form3.cs | 120 +++++++++++--------------------------- 1 file changed, 34 insertions(+), 86 deletions(-) diff --git a/WindowsFormsApp6/Form3.cs b/WindowsFormsApp6/Form3.cs index daf3b18..eea9681 100644 --- a/WindowsFormsApp6/Form3.cs +++ b/WindowsFormsApp6/Form3.cs @@ -372,16 +372,21 @@ namespace WindowsFormsApp6 } /// - /// 定时器事件 - 模拟从寄存器读取吸水时间数据 + /// 定时器事件 - 模拟从寄存器读取数据 /// private void DataTimer_Tick(object sender, EventArgs e) { - // 读取吸水时间(s) - 从寄存器读取 + // 第1行:读取吸水时间(s) - 从寄存器读取(每个试样读取3次) DataRow timeRow = sampleDataTable.Rows[0]; for (int i = 1; i <= currentSampleCount; i++) { - double registerValue = ReadRegisterData(i - 1); - timeRow[$"试样{i}_1"] = registerValue; + double time1 = ReadRegisterData((i - 1) * 3); // 第1次测试 + double time2 = ReadRegisterData((i - 1) * 3 + 1); // 第2次测试 + double time3 = ReadRegisterData((i - 1) * 3 + 2); // 第3次测试 + + timeRow[$"试样{i}_1"] = time1; + timeRow[$"试样{i}_2"] = time2; + timeRow[$"试样{i}_3"] = time3; } UpdateDisplay(); @@ -392,6 +397,7 @@ namespace WindowsFormsApp6 /// private double ReadRegisterData(int registerAddress) { + // 吸水时间寄存器:30-34秒 return 30 + random.NextDouble() * 4; } @@ -411,14 +417,19 @@ namespace WindowsFormsApp6 } } - // 第1行:吸水时间(s)- 试样次数1(系统读数) - // 每个试样只在第1次测试时记录吸水时间 + // 第1行:吸水时间(s)- 系统读数(每个试样3次) DataRow timeRow = sampleDataTable.Rows[0]; for (int i = 1; i <= currentSampleCount; i++) { - double timeValue = Math.Round(30 + random.NextDouble() * 4, 2); // 30-34秒 - timeRow[$"试样{i}_1"] = timeValue; - // 试样次数2和3不使用,保持为0 + // 生成3次测试的吸水时间数据(30-34秒,略有差异) + double baseTime = 31 + random.NextDouble() * 2; // 基准时间 31-33秒 + double time1 = Math.Round(baseTime + (random.NextDouble() - 0.5) * 2, 2); // ±1秒 + double time2 = Math.Round(baseTime + (random.NextDouble() - 0.5) * 2, 2); + double time3 = Math.Round(baseTime + (random.NextDouble() - 0.5) * 2, 2); + + timeRow[$"试样{i}_1"] = time1; + timeRow[$"试样{i}_2"] = time2; + timeRow[$"试样{i}_3"] = time3; } // 第2行:吸芯高度(mm)- 手动输入,不自动生成 @@ -431,63 +442,12 @@ namespace WindowsFormsApp6 RefreshDataGridView(); ShowMessage($"已生成 {currentSampleCount} 个试样的吸水时间数据\n" + - $"- 吸水时间:30-34秒(系统读数)\n" + + $"- 吸水时间:30-34秒(系统读数,每个试样3次)\n" + $"- 吸芯高度:请手动输入(每个试样3次测试)\n" + $"- 芯吸速率:输入吸芯高度后自动计算", "模拟数据生成"); } - /// - /// 生成完整的测试数据(包含吸芯高度)- 用于快速测试 - /// - public void GenerateFullMockData() - { - // 清空所有行的数据(保持表结构) - foreach (DataRow row in sampleDataTable.Rows) - { - for (int i = 1; i <= currentSampleCount; i++) - { - row[$"试样{i}_1"] = 0.0; - row[$"试样{i}_2"] = 0.0; - row[$"试样{i}_3"] = 0.0; - } - } - - // 第1行:吸水时间(s) - DataRow timeRow = sampleDataTable.Rows[0]; - for (int i = 1; i <= currentSampleCount; i++) - { - double timeValue = Math.Round(30 + random.NextDouble() * 4, 2); - timeRow[$"试样{i}_1"] = timeValue; - } - - // 第2行:吸芯高度(mm)- 生成测试数据 - DataRow heightRow = sampleDataTable.Rows[1]; - for (int i = 1; i <= currentSampleCount; i++) - { - double baseHeight = 55 + random.NextDouble() * 10; - double height1 = Math.Round(baseHeight + (random.NextDouble() - 0.5) * 4, 2); - double height2 = Math.Round(baseHeight + (random.NextDouble() - 0.5) * 4, 2); - double height3 = Math.Round(baseHeight + (random.NextDouble() - 0.5) * 4, 2); - - heightRow[$"试样{i}_1"] = height1; - heightRow[$"试样{i}_2"] = height2; - heightRow[$"试样{i}_3"] = height3; - } - - // 计算所有行 - CalculateAllRows(); - - // 更新显示 - RefreshDataGridView(); - - ShowMessage($"已生成完整测试数据(包含吸芯高度)\n" + - $"- 吸水时间:30-34秒\n" + - $"- 吸芯高度:50-70mm(自动生成)\n" + - $"- 芯吸速率:已自动计算", - "完整测试数据"); - } - /// /// 更新界面显示 /// @@ -535,37 +495,25 @@ namespace WindowsFormsApp6 for (int i = 1; i <= currentSampleCount; i++) { - // 获取吸水时间(试样次数1:系统读数) - double time = ConvertToDouble(timeRow[$"试样{i}_1"]); + // 获取3次吸水时间测试数据 + double time1 = ConvertToDouble(timeRow[$"试样{i}_1"]); + double time2 = ConvertToDouble(timeRow[$"试样{i}_2"]); + double time3 = ConvertToDouble(timeRow[$"试样{i}_3"]); - // 获取吸芯高度(优先使用对应列的数据) - // 试样次数1 → 使用吸芯高度_1 - // 试样次数2 → 使用吸芯高度_2 - // 试样次数3 → 使用吸芯高度_3 + // 获取3次吸芯高度测试数据 double height1 = ConvertToDouble(heightRow[$"试样{i}_1"]); double height2 = ConvertToDouble(heightRow[$"试样{i}_2"]); double height3 = ConvertToDouble(heightRow[$"试样{i}_3"]); - // 计算芯吸速率(使用对应的吸芯高度) - double rate = 0; - if (time > 0) - { - // 使用平均吸芯高度或选择非零值 - double avgHeight = 0; - int count = 0; - if (height1 > 0) { avgHeight += height1; count++; } - if (height2 > 0) { avgHeight += height2; count++; } - if (height3 > 0) { avgHeight += height3; count++; } - - if (count > 0) - { - avgHeight /= count; - rate = avgHeight / (time / 60.0); - } - } + // 计算3次芯吸速率(每次测试独立计算,使用对应的时间和高度) + double rate1 = (time1 > 0 && height1 > 0) ? height1 / (time1 / 60.0) : 0; + double rate2 = (time2 > 0 && height2 > 0) ? height2 / (time2 / 60.0) : 0; + double rate3 = (time3 > 0 && height3 > 0) ? height3 / (time3 / 60.0) : 0; - // 存储到试样次数3(系统计算) - rateRow[$"试样{i}_3"] = Math.Round(rate, 2); + // 存储到对应的列 + rateRow[$"试样{i}_1"] = Math.Round(rate1, 2); + rateRow[$"试样{i}_2"] = Math.Round(rate2, 2); + rateRow[$"试样{i}_3"] = Math.Round(rate3, 2); } }