diff --git a/WindowsFormsApp6/MainForm.cs b/WindowsFormsApp6/MainForm.cs index 202ded1..c31adac 100644 --- a/WindowsFormsApp6/MainForm.cs +++ b/WindowsFormsApp6/MainForm.cs @@ -77,7 +77,7 @@ namespace WindowsFormsApp6 _readTimer.Tick += ReadTimer_Tick; // 尝试打开默认串口 - if (!TryOpenSerialPort("COM2")) + if (!TryOpenSerialPort("COM14")) { // 如果默认串口失败,弹出端口选择对话框 ShowPortSelectionDialog(); @@ -348,7 +348,7 @@ namespace WindowsFormsApp6 } // 转换寄存器值为实际时间(秒) - double timeValue = registers[0] / 10.0; + double timeValue = registers[0]; // 检查NaN if (double.IsNaN(timeValue) || double.IsInfinity(timeValue)) @@ -1053,6 +1053,7 @@ namespace WindowsFormsApp6 /// /// 读取Form3数据(液体芯吸速率) /// PLC地址:D200 - 吸水时间(s),D454 - 吸芯高度(mm),每次测试读取1个试样(各2个字节) + /// 一组数据包含次试样1,包含 3次测试,二组数据包含次试样2,包含 3次测试, 依次类推 /// 信号量:M310 /// 每次测试添加一个试样的一次测试数据,试样数量动态增长 /// @@ -1741,34 +1742,48 @@ namespace WindowsFormsApp6 nameCell.SetCellValue(rowName); nameCell.CellStyle = styles.dataStyle; - // 如果是平均时间行,只显示一个合并的单元格 + // 如果是平均时间行,按每5个试样一组显示 if (rowName.Contains("平均")) { - ICell avgCell = row.CreateCell(1); - if (dataTable.Columns.Contains("试样1") && dataRow["试样1"] != DBNull.Value) + // 计算有多少组(每5个试样一组) + int groupCount = (int)Math.Ceiling((double)sampleCount / 5.0); + + for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) { - if (double.TryParse(dataRow["试样1"].ToString(), out double value)) + int startSample = groupIndex * 5 + 1; + int endSample = Math.Min(startSample + 4, sampleCount); + + // 查找该组的平均值(存储在该组第一个试样的列中) + ICell avgCell = row.CreateCell(startSample); + if (dataTable.Columns.Contains($"试样{startSample}") && dataRow[$"试样{startSample}"] != DBNull.Value) { - avgCell.SetCellValue(value); + if (double.TryParse(dataRow[$"试样{startSample}"].ToString(), out double value)) + { + avgCell.SetCellValue(value); + } + else + { + avgCell.SetCellValue(""); + } } else { avgCell.SetCellValue(""); } + avgCell.CellStyle = styles.yellowStyle; + + // 为该组的其他单元格设置样式 + for (int i = startSample + 1; i <= endSample; i++) + { + row.CreateCell(i).CellStyle = styles.yellowStyle; + } + + // 合并该组的单元格 + if (endSample > startSample) + { + sheet.AddMergedRegion(new CellRangeAddress(currentRow - 1, currentRow - 1, startSample, endSample)); + } } - else - { - avgCell.SetCellValue(""); - } - avgCell.CellStyle = styles.yellowStyle; - - // 为合并区域的其他单元格设置样式 - for (int i = 2; i <= sampleCount; i++) - { - row.CreateCell(i).CellStyle = styles.yellowStyle; - } - // 合并平均时间单元格 - sheet.AddMergedRegion(new CellRangeAddress(currentRow - 1, currentRow - 1, 1, sampleCount)); } else {