Compare commits
7 Commits
fbab7e3c17
...
xyy
| Author | SHA1 | Date | |
|---|---|---|---|
| 8419aaaccd | |||
| 4764ca789b | |||
| 9eb7c54ac8 | |||
| abc3e186e2 | |||
| 8573f5c4e5 | |||
| d3fdb6a288 | |||
| 2c20eaf323 |
@@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net8.0-windows7.0</TargetFramework>
|
||||
<TargetFramework>net10.0-windows7.0</TargetFramework>
|
||||
<RootNamespace>ASTM_D7896_19瞬态热线法</RootNamespace>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
@@ -16,11 +16,11 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.2" />
|
||||
<PackageReference Include="EPPlus" Version="7.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.16" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.8" />
|
||||
<PackageReference Include="NModbus4.NetCore" Version="3.0.0" />
|
||||
<PackageReference Include="NModbus4.NetCore" Version="4.0.0" />
|
||||
<PackageReference Include="OxyPlot.Wpf" Version="2.2.0" />
|
||||
<PackageReference Include="PdfSharpCore" Version="1.3.67" />
|
||||
</ItemGroup>
|
||||
|
||||
2
App.xaml
2
App.xaml
@@ -1,7 +1,7 @@
|
||||
<Application x:Class="ASTM_D7896_Tester.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:converters="clr-namespace:ASTM_D7896_Tester.Converters" StartupUri="Window1.xaml"
|
||||
xmlns:converters="clr-namespace:ASTM_D7896_Tester.Converters"
|
||||
>
|
||||
<Application.Resources>
|
||||
<converters:InverseBooleanConverter x:Key="InverseBooleanConverter"/>
|
||||
|
||||
BIN
CSI-L028瞬态热线法导热系数测试仪器(1).gxw
Normal file
BIN
CSI-L028瞬态热线法导热系数测试仪器(1).gxw
Normal file
Binary file not shown.
BIN
CSI-L028瞬态热线法导热系数测试仪器(1).pdf
Normal file
BIN
CSI-L028瞬态热线法导热系数测试仪器(1).pdf
Normal file
Binary file not shown.
@@ -46,7 +46,7 @@ public class TestParameters
|
||||
public int MeasurementCount { get; set; } = 10;
|
||||
[Range(5, 300)]
|
||||
public int IntervalSeconds { get; set; } = 30;
|
||||
public double PlatinumWireLength { get; set; } = 0.040; // 默认 5.6 cm
|
||||
public double PlatinumWireLength { get; set; } = 0.04; // 默认 5.6 cm
|
||||
public double PlatinumWireDiameter { get; set; } = 0.00006;
|
||||
public string ReportOutputPath { get; set; } = "Reports\\";
|
||||
|
||||
@@ -91,6 +91,6 @@ public class CalibrationCoefficients
|
||||
public ushort PressureProtection { get; set; }
|
||||
public ushort TemperatureCoefficient { get; set; }
|
||||
public ushort ResistanceCoefficient { get; set; }
|
||||
public double ThermalConductivityCorrection { get; set; } = 47.305349f;//蒸馏水 比热率修正
|
||||
public double ThermalDiffusivityCorrection { get; set; } = 0.312912;//导热率修正
|
||||
public double ThermalConductivityCorrection { get; set; } = 37;//蒸馏水 比热率修正
|
||||
public double ThermalDiffusivityCorrection { get; set; } = 0.45;//导热率修正
|
||||
}
|
||||
@@ -52,7 +52,7 @@ public partial class D7896ViewModel : ObservableObject
|
||||
[ObservableProperty] private PlotModel _temperatureCurveModel;
|
||||
|
||||
// UI 绑定属性 (与之前一致)
|
||||
public ObservableCollection<string> ReferenceLiquids { get; } = new() { "蒸馏水", "甲苯", "乙二醇" };
|
||||
public ObservableCollection<string> ReferenceLiquids { get; } = new() { "蒸馏水" };
|
||||
[ObservableProperty] private string _sampleId = "未命名样品";
|
||||
[ObservableProperty] private double _testTemperature = 25.0;
|
||||
[ObservableProperty] private string _testDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
@@ -143,6 +143,7 @@ public partial class D7896ViewModel : ObservableObject
|
||||
Application.Current?.Dispatcher.Invoke(() => ChamberPressure = rawPressure);
|
||||
|
||||
float rawTemp = await _plcService.ReadFloatAsync(_config.PlcRegisterAddresses.Temperature);
|
||||
rawTemp *= 1.0435f;
|
||||
Application.Current?.Dispatcher.Invoke(() => CurrentTestTemperature = rawTemp);
|
||||
}
|
||||
catch { }
|
||||
@@ -158,8 +159,7 @@ public partial class D7896ViewModel : ObservableObject
|
||||
// }
|
||||
// catch { return 0; }
|
||||
//}
|
||||
// 在类成员变量区域添加
|
||||
private int currentSettleMs = 200; // 电流稳定等待时间(毫秒)
|
||||
|
||||
[RelayCommand]
|
||||
private async Task StartTestAsync()
|
||||
{
|
||||
@@ -304,7 +304,7 @@ public partial class D7896ViewModel : ObservableObject
|
||||
double[] ustdBase = await _th1963Ustd.FetchBatchAsync();
|
||||
double[] uptBase = await _th1953Ustd.FetchBatchAsync();
|
||||
|
||||
double dynamicR0 = 1.49; // 默认值
|
||||
double dynamicR0 = 1.476979; // 默认值
|
||||
if (ustdBase != null && ustdBase.Length > 0 && uptBase != null && uptBase.Length > 0)
|
||||
{
|
||||
double sumR0 = 0; int cnt = 0;
|
||||
@@ -335,25 +335,21 @@ public partial class D7896ViewModel : ObservableObject
|
||||
await _th1963Ustd.PrepareBatchAsync(samples);
|
||||
await _th1953Ustd.PrepareBatchAsync(samples);
|
||||
|
||||
|
||||
|
||||
await _plcService.WriteCoilAsync(_config.PlcRegisterAddresses.StartCommand, true);
|
||||
// 等待电流稳定(不触发采集)
|
||||
try { await Task.Delay(currentSettleMs, _testCts.Token); } catch (OperationCanceledException) { break; }
|
||||
// 电流稳定后,触发采集
|
||||
try { await Task.Delay(5, _testCts.Token); } catch (OperationCanceledException) { break; }
|
||||
|
||||
await Task.WhenAll(_th1963Ustd.TriggerAsync(), _th1953Ustd.TriggerAsync());
|
||||
// 继续加热剩余时间(加热总时间 = 稳定等待时间 + 有效加热时间)
|
||||
|
||||
try { await Task.Delay((int)(heatingDuration * 1000), _testCts.Token); } catch (OperationCanceledException) { break; }
|
||||
await _plcService.WriteCoilAsync(_config.PlcRegisterAddresses.StartCommand, false);
|
||||
|
||||
|
||||
int remainingMs = (int)((totalDuration - heatingDuration) * 1000) + 100;
|
||||
try { await Task.Delay(remainingMs, _testCts.Token); } catch (OperationCanceledException) { break; }
|
||||
|
||||
double[] ustd = await _th1963Ustd.FetchBatchAsync();
|
||||
double[] upt = await _th1953Ustd.FetchBatchAsync();
|
||||
|
||||
if (dynamicR0 == 1.49) // 基线无效
|
||||
if (dynamicR0 == 1.476979) // 基线无效
|
||||
{
|
||||
double sumR0 = 0; int cnt = 0;
|
||||
for (int j = 2; j < Math.Min(6, ustd.Length); j++)
|
||||
@@ -395,7 +391,7 @@ public partial class D7896ViewModel : ObservableObject
|
||||
|
||||
// ---- 异常值检测 ----
|
||||
bool isOutlier = false;
|
||||
double deviationThreshold = 0.1; // 10% 偏差阈值
|
||||
double deviationThreshold = 0.20; // 20% 偏差阈值
|
||||
|
||||
if (validCount >= 2) // 至少有两个有效数据后才开始剔除
|
||||
{
|
||||
@@ -511,13 +507,12 @@ public partial class D7896ViewModel : ObservableObject
|
||||
}
|
||||
}
|
||||
|
||||
// 滑动平均平滑(窗口大小改为 11)
|
||||
int windowSize = 40; // 原为 5
|
||||
// 滑动平均平滑(窗口5)
|
||||
double[] smoothDeltaT = new double[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
int start = Math.Max(0, i - windowSize / 2);
|
||||
int end = Math.Min(n - 1, i + windowSize / 2);
|
||||
int start = Math.Max(0, i - 2);
|
||||
int end = Math.Min(n - 1, i + 2);
|
||||
double sum = 0; int cnt = 0;
|
||||
for (int j = start; j <= end; j++)
|
||||
if (!double.IsNaN(deltaT[j])) { sum += deltaT[j]; cnt++; }
|
||||
@@ -673,6 +668,34 @@ public partial class D7896ViewModel : ObservableObject
|
||||
return timeArray.Length - 1;
|
||||
}
|
||||
|
||||
private void ExportMeasurementCsv(string path, double[] time, double[] ustd, double[] upt, double[] deltaT, int fitStart, int fitEnd)
|
||||
{
|
||||
using var sw = new StreamWriter(path, false, Encoding.UTF8);
|
||||
sw.WriteLine("index,time,U_std,U_pt,deltaT,fitWindow");
|
||||
int n = Math.Min(Math.Min(time.Length, ustd.Length), deltaT.Length);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
bool inFit = (i >= fitStart && i <= fitEnd);
|
||||
sw.WriteLine($"{i},{time[i]:F6},{ustd[i]:F6},{upt[i]:F6},{(double.IsNaN(deltaT[i]) ? 0 : deltaT[i]):F6},{(inFit ? 1 : 0)}");
|
||||
}
|
||||
}
|
||||
|
||||
private void ExportCandidateWindowsCsv(string path, double[] time, double[] deltaT, int startIdx, int endIdx)
|
||||
{
|
||||
using var sw = new StreamWriter(path, false, Encoding.UTF8);
|
||||
sw.WriteLine("s,e,t_start,t_end,meanDelta");
|
||||
int n = time.Length;
|
||||
int minPts = 10;
|
||||
for (int s = startIdx; s <= endIdx - minPts; s++)
|
||||
{
|
||||
for (int e = s + minPts - 1; e <= endIdx; e++)
|
||||
{
|
||||
double mean = deltaT.Skip(s).Take(e - s + 1).Where(x => !double.IsNaN(x)).DefaultIfEmpty(0).Average();
|
||||
sw.WriteLine($"{s},{e},{time[s]:F6},{time[e]:F6},{mean:F6}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// 最小二乘法拟合斜率 (X轴为横坐标,Y轴为纵坐标) — 用于加热段 ln(t) vs ΔT
|
||||
///// </summary>
|
||||
@@ -787,6 +810,40 @@ public partial class D7896ViewModel : ObservableObject
|
||||
TemperatureCurveModel = null;
|
||||
}
|
||||
|
||||
//[RelayCommand]
|
||||
//private async Task GenerateReportAsync()
|
||||
//{
|
||||
// if (Measurements.Count == 0)
|
||||
// {
|
||||
// MessageBox.Show("没有测试数据", "提示");
|
||||
// return;
|
||||
// }
|
||||
// try
|
||||
// {
|
||||
// var extraParams = new Dictionary<string, object>
|
||||
// {
|
||||
// ["SampleVolume"] = SampleVolume,
|
||||
// ["BubbleRemoved"] = BubbleRemoved,
|
||||
// ["UsePressure"] = UsePressure,
|
||||
// ["PressureValue"] = PressureValue,
|
||||
// ["IsCleanConfirmed"] = IsCleanConfirmed,
|
||||
// ["CleanerName"] = CleanerName,
|
||||
// ["AmbientTemperature"] = AmbientTemperature,
|
||||
// ["AmbientCalibrated"] = AmbientCalibrated,
|
||||
// ["PlatinumCompatible"] = PlatinumCompatible,
|
||||
// ["LiquidReactivityNote"] = LiquidReactivityNote,
|
||||
// ["InitialResistance"] = PlatinumResistance
|
||||
// };
|
||||
// string reportPath = await _reportService.GenerateReportAsync(SampleId, TestTemperature, Measurements.ToList(),
|
||||
// AverageThermalConductivity, AverageThermalDiffusivity, AverageVolumetricHeatCapacity,
|
||||
// _config.TestParameters, extraParams);
|
||||
// MessageBox.Show($"报告已生成: {reportPath}", "成功");
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// MessageBox.Show($"生成报告失败: {ex.Message}", "错误");
|
||||
// }
|
||||
//}
|
||||
[RelayCommand]
|
||||
private async Task GenerateReportAsync()
|
||||
{
|
||||
@@ -810,23 +867,8 @@ public partial class D7896ViewModel : ObservableObject
|
||||
|
||||
try
|
||||
{
|
||||
// 先在 UI 线程导出曲线图为字节数组
|
||||
byte[] chartImageBytes = null;
|
||||
await Application.Current.Dispatcher.InvokeAsync(() =>
|
||||
{
|
||||
if (TemperatureCurveModel != null && TemperatureCurveModel.Series.Count > 0)
|
||||
{
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
var exporter = new PngExporter { Width = 600, Height = 400 };
|
||||
exporter.Export(TemperatureCurveModel, stream);
|
||||
chartImageBytes = stream.ToArray();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 然后在后台线程生成 PDF(避免阻塞 UI)
|
||||
await Task.Run(() => GeneratePdfReport(pdfPath, chartImageBytes));
|
||||
// 生成 PDF
|
||||
await Task.Run(() => GeneratePdfReport(pdfPath));
|
||||
MessageBox.Show($"报表已生成: {pdfPath}", "成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -838,134 +880,103 @@ public partial class D7896ViewModel : ObservableObject
|
||||
|
||||
|
||||
|
||||
private void GeneratePdfReport(string filePath, byte[] chartImageBytes)
|
||||
private void GeneratePdfReport(string filePath)
|
||||
{
|
||||
// 1. 创建文档
|
||||
using (var document = new PdfDocument())
|
||||
{
|
||||
// 创建第一个页面
|
||||
// 2. 添加页面
|
||||
var page = document.AddPage();
|
||||
page.Width = XUnit.FromMillimeter(210);
|
||||
page.Height = XUnit.FromMillimeter(297);
|
||||
var gfx = XGraphics.FromPdfPage(page);
|
||||
|
||||
var titleFont = new XFont("SimHei", 16, XFontStyle.Bold);
|
||||
var headerFont = new XFont("SimHei", 12, XFontStyle.Bold);
|
||||
var normalFont = new XFont("SimHei", 10, XFontStyle.Regular);
|
||||
double yPosition = 30;
|
||||
|
||||
// 标题
|
||||
gfx.DrawString("ASTM D7896-19 瞬态热线法测试报告", titleFont, XBrushes.Black,
|
||||
new XRect(0, yPosition, page.Width, 30), XStringFormats.TopCenter);
|
||||
yPosition += 40;
|
||||
|
||||
// 基础信息(只显示一次)
|
||||
gfx.DrawString($"样品名称: {SampleId}", normalFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 22;
|
||||
gfx.DrawString($"测试温度: {TestTemperature:F1} °C", normalFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 22;
|
||||
gfx.DrawString($"测试时间: {TestDateTime}", normalFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 22;
|
||||
gfx.DrawString($"样品体积: {SampleVolume:F1} mL", normalFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 22;
|
||||
gfx.DrawString($"样品密度: {SampleDensity:F0} kg/m^3", normalFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 22;
|
||||
gfx.DrawString($"环境温度: {AmbientTemperature:F1} °C", normalFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 22;
|
||||
gfx.DrawString($"铂丝电阻温度系数: {AlphaPt:F4} /°C", normalFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 22;
|
||||
gfx.DrawString($"铂丝长度: {_config.TestParameters.PlatinumWireLength:F3} m", normalFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 22;
|
||||
gfx.DrawString($"铂丝直径: {_config.TestParameters.PlatinumWireDiameter * 1000:F2} mm", normalFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 35;
|
||||
|
||||
// 曲线图(如果有)
|
||||
if (chartImageBytes != null)
|
||||
// 3. 开始绘制
|
||||
using (var gfx = XGraphics.FromPdfPage(page))
|
||||
{
|
||||
using (var imgStream = new MemoryStream(chartImageBytes))
|
||||
// 创建字体
|
||||
var titleFont = new XFont("Verdana", 16, XFontStyle.Bold);
|
||||
var headerFont = new XFont("Verdana", 12, XFontStyle.Bold);
|
||||
var normalFont = new XFont("Verdana", 10, XFontStyle.Regular);
|
||||
double yPosition = 30;
|
||||
|
||||
// ---------- 4. 添加标题 ----------
|
||||
gfx.DrawString("ASTM D7896-19 瞬态热线法测试报告", titleFont, XBrushes.Black,
|
||||
new XRect(0, yPosition, page.Width, 30), XStringFormats.TopCenter);
|
||||
yPosition += 40;
|
||||
|
||||
// ---------- 5. 添加基础信息 ----------
|
||||
gfx.DrawString($"样品名称: {SampleId}", normalFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 25;
|
||||
gfx.DrawString($"测试温度: {TestTemperature:F1} °C", normalFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 25;
|
||||
// ... 所有需要显示的基础信息
|
||||
|
||||
yPosition += 10;
|
||||
|
||||
// ---------- 6. 插入温升曲线图 ----------
|
||||
if (TemperatureCurveModel != null)
|
||||
{
|
||||
var image = XImage.FromStream(() => new MemoryStream(imgStream.ToArray()));
|
||||
gfx.DrawImage(image, 40, yPosition, 500, 330);
|
||||
yPosition += 350;
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
var exporter = new PngExporter { Width = 600, Height = 400 };
|
||||
exporter.Export(TemperatureCurveModel, stream);
|
||||
stream.Position = 0;
|
||||
var imgStream = new MemoryStream(stream.ToArray(), 0, (int)stream.Length, false, true);
|
||||
var image = XImage.FromStream(() => imgStream);
|
||||
gfx.DrawImage(image, 40, yPosition, 500, 330);
|
||||
yPosition += 350;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 表格标题
|
||||
gfx.DrawString("测量结果明细", headerFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 25;
|
||||
// ---------- 7. 创建表格 ----------
|
||||
gfx.DrawString("测量结果明细", headerFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 25;
|
||||
|
||||
// 表头(已替换特殊符号)
|
||||
string[] headers = { "序号", "热导率λ(W/(m.K))", "热扩散率α(10^-7 m^2/s)", "体积热容VHC(kJ/(m^3.K))", "比热容Cp(J/(kg.K))" };
|
||||
double[] colWidths = { 50, 115, 125, 125, 125 };
|
||||
double startX = 40;
|
||||
// 表头
|
||||
string[] headers = { "序号", "热导率λ(W/(m·K))", "热扩散率α(10⁻⁷ m²/s)", "体积热容VHC(kJ/(m³·K))", "比热容Cp(J/(kg·K))" };
|
||||
double[] colWidths = { 50, 100, 100, 120, 100 };
|
||||
double startX = 40;
|
||||
double currentRowY = yPosition;
|
||||
|
||||
// 辅助函数:绘制表头(用于新页)
|
||||
void DrawHeader(XGraphics g, double y)
|
||||
{
|
||||
// 绘制表头
|
||||
for (int i = 0; i < headers.Length; i++)
|
||||
{
|
||||
double cellX = startX + colWidths.Take(i).Sum();
|
||||
g.DrawRectangle(XPens.Black, cellX, y, colWidths[i], 20);
|
||||
var textRect = new XRect(cellX, y, colWidths[i], 20);
|
||||
g.DrawString(headers[i], normalFont, XBrushes.Black, textRect, XStringFormats.Center);
|
||||
gfx.DrawRectangle(XPens.Black, cellX, currentRowY, colWidths[i], 20);
|
||||
var textRect = new XRect(cellX, currentRowY, colWidths[i], 20);
|
||||
gfx.DrawString(headers[i], normalFont, XBrushes.Black, textRect, XStringFormats.Center);
|
||||
}
|
||||
}
|
||||
currentRowY += 20;
|
||||
|
||||
// 绘制第一页的表头
|
||||
DrawHeader(gfx, yPosition);
|
||||
double currentRowY = yPosition + 20; // 表头高度20
|
||||
const double rowHeight = 20;
|
||||
const double bottomMargin = 50; // 页面底部留白
|
||||
|
||||
// 遍历所有测量数据
|
||||
foreach (var m in Measurements)
|
||||
{
|
||||
// 检查是否需要换页
|
||||
if (currentRowY + rowHeight > page.Height - bottomMargin)
|
||||
// 填充数据行 (假设 Measurements 是列表)
|
||||
for (int i = 0; i < Measurements.Count; i++)
|
||||
{
|
||||
gfx.Dispose();
|
||||
page = document.AddPage();
|
||||
page.Width = XUnit.FromMillimeter(210);
|
||||
page.Height = XUnit.FromMillimeter(297);
|
||||
gfx = XGraphics.FromPdfPage(page);
|
||||
gfx.DrawString("ASTM D7896-19 瞬态热线法测试报告(续)", headerFont, XBrushes.Black,
|
||||
new XRect(0, 30, page.Width, 30), XStringFormats.TopCenter);
|
||||
currentRowY = 80;
|
||||
DrawHeader(gfx, currentRowY - 20);
|
||||
}
|
||||
var m = Measurements[i];
|
||||
string[] rowData = {
|
||||
m.Index.ToString(),
|
||||
m.ThermalConductivity.ToString("F6"),
|
||||
(m.ThermalDiffusivity * 1e7).ToString("F3"),
|
||||
m.VolumetricHeatCapacity.ToString("F2"),
|
||||
m.SpecificHeatCapacity.ToString("F2")
|
||||
};
|
||||
|
||||
string[] rowData = {
|
||||
m.Index.ToString(),
|
||||
m.ThermalConductivity.ToString("F6"),
|
||||
(m.ThermalDiffusivity * 1e7).ToString("F3"),
|
||||
m.VolumetricHeatCapacity.ToString("F2"),
|
||||
m.SpecificHeatCapacity.ToString("F2")
|
||||
};
|
||||
for (int j = 0; j < rowData.Length; j++)
|
||||
{
|
||||
double cellX = startX + colWidths.Take(j).Sum();
|
||||
gfx.DrawRectangle(XPens.Black, cellX, currentRowY, colWidths[j], rowHeight);
|
||||
var textRect = new XRect(cellX, currentRowY, colWidths[j], rowHeight);
|
||||
gfx.DrawString(rowData[j], normalFont, XBrushes.Black, textRect, XStringFormats.Center);
|
||||
for (int j = 0; j < rowData.Length; j++)
|
||||
{
|
||||
double cellX = startX + colWidths.Take(j).Sum();
|
||||
gfx.DrawRectangle(XPens.Black, cellX, currentRowY, colWidths[j], 20);
|
||||
var textRect = new XRect(cellX, currentRowY, colWidths[j], 20);
|
||||
gfx.DrawString(rowData[j], normalFont, XBrushes.Black, textRect, XStringFormats.Center);
|
||||
}
|
||||
currentRowY += 20;
|
||||
}
|
||||
currentRowY += rowHeight;
|
||||
yPosition = currentRowY + 10;
|
||||
|
||||
// ---------- 8. 添加平均值 ----------
|
||||
gfx.DrawString($"平均热导率: {AverageThermalConductivity:F6} W/(m·K)", normalFont, XBrushes.Black, new XPoint(40, yPosition)); yPosition += 20;
|
||||
// ... 其他平均值
|
||||
|
||||
// ---------- 9. 添加生成时间和页脚 ----------
|
||||
gfx.DrawString($"生成时间: {DateTime.Now:yyyy-MM-dd HH:mm:ss}", normalFont, XBrushes.Black, new XPoint(40, page.Height - 30));
|
||||
}
|
||||
|
||||
// 添加空行,避免平均值与表格重叠
|
||||
currentRowY += 60;
|
||||
|
||||
// 检查是否需要换页(平均值部分)
|
||||
if (currentRowY + 80 > page.Height - bottomMargin)
|
||||
{
|
||||
gfx.Dispose();
|
||||
page = document.AddPage();
|
||||
page.Width = XUnit.FromMillimeter(210);
|
||||
page.Height = XUnit.FromMillimeter(297);
|
||||
gfx = XGraphics.FromPdfPage(page);
|
||||
currentRowY = 50;
|
||||
}
|
||||
|
||||
// 平均值(单位已替换)
|
||||
gfx.DrawString($"平均热导率: {AverageThermalConductivity:F6} W/(m.K)", normalFont, XBrushes.Black, new XPoint(40, currentRowY)); currentRowY += 20;
|
||||
gfx.DrawString($"平均热扩散率: {AverageThermalDiffusivity:E6} m^2/s", normalFont, XBrushes.Black, new XPoint(40, currentRowY)); currentRowY += 20;
|
||||
gfx.DrawString($"平均体积热容: {AverageVolumetricHeatCapacity:F2} kJ/(m^3.K)", normalFont, XBrushes.Black, new XPoint(40, currentRowY)); currentRowY += 20;
|
||||
// 比热容转换:VHC 单位 kJ/(m^3.K) 先转为 J/(m^3.K) 再除以密度得到 J/(kg.K)
|
||||
double avgCp_J_per_kgK = (AverageVolumetricHeatCapacity * 1000) / SampleDensity;
|
||||
gfx.DrawString($"平均比热容: {avgCp_J_per_kgK:F0} J/(kg.K)", normalFont, XBrushes.Black, new XPoint(40, currentRowY)); currentRowY += 40;
|
||||
|
||||
// 页脚
|
||||
gfx.DrawString($"生成时间: {DateTime.Now:yyyy-MM-dd HH:mm:ss}", normalFont, XBrushes.Black, new XPoint(40, page.Height - 30));
|
||||
gfx.Dispose();
|
||||
|
||||
// 10. 保存文档
|
||||
document.Save(filePath);
|
||||
}
|
||||
}
|
||||
@@ -1038,7 +1049,7 @@ public partial class D7896ViewModel : ObservableObject
|
||||
try
|
||||
{
|
||||
float rawPressure = await _plcService.ReadFloatAsync(_config.PlcRegisterAddresses.Pressure);
|
||||
ChamberPressure = rawPressure / 10.0;
|
||||
ChamberPressure = rawPressure;
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
@@ -134,9 +134,9 @@
|
||||
<CheckBox IsChecked="{Binding IsCleanConfirmed}" Content="采样池清洁 (7.1)" Margin="0,0,15,0"/>
|
||||
<CheckBox IsChecked="{Binding BubbleRemoved}" Content="气泡清除 (7.6)" Margin="0,0,15,0"/>
|
||||
<CheckBox IsChecked="{Binding PlatinumCompatible}" Content="铂兼容性 (1.4)" Margin="0,0,15,0"/>
|
||||
<CheckBox IsChecked="{Binding AmbientCalibrated}" Content="采样池温度校准 (8.1)" Margin="0,0,15,0"/>
|
||||
<CheckBox IsChecked="{Binding AmbientCalibrated}" Content="环境温度校准 (8.1)" Margin="0,0,15,0"/>
|
||||
<TextBlock Text="状态:" VerticalAlignment="Center" Margin="20,0,5,0"/>
|
||||
<TextBox Text="{Binding StatusMessage}" Width="200" IsReadOnly="True" Background="#FFF7E6"/>
|
||||
<TextBox Text="{Binding StatusMessage}" Width="260" IsReadOnly="True" Background="#FFF7E6"/>
|
||||
</WrapPanel>
|
||||
</StackPanel>
|
||||
</Border>
|
||||
@@ -319,21 +319,19 @@
|
||||
</Border>
|
||||
|
||||
<!-- 7. 系统校准(可选,可收起到折叠区域,为节省空间放在底部) -->
|
||||
<!--<Border Grid.Row="6" Background="White" CornerRadius="6" Padding="8" Margin="0,5" Effect="{StaticResource CardShadow}">
|
||||
<Border Grid.Row="6" Background="White" CornerRadius="6" Padding="8" Margin="0,5" Effect="{StaticResource CardShadow}">
|
||||
<StackPanel>
|
||||
<TextBlock Text="系统校准 (附录A3)" FontWeight="Bold" Margin="0,0,0,5"/>
|
||||
<WrapPanel>
|
||||
<TextBlock Text="参考液:" VerticalAlignment="Center"/>
|
||||
<ComboBox ItemsSource="{Binding ReferenceLiquids}" SelectedItem="{Binding SelectedReferenceLiquid}" Width="100" Margin="5,0"/>
|
||||
<ComboBox ItemsSource="{Binding ReferenceLiquids}" IsReadOnly="True" SelectedItem="{Binding SelectedReferenceLiquid}" Width="100" Margin="5,0"/>
|
||||
<TextBlock Text="参考热导率 (W/m·K):" VerticalAlignment="Center" Margin="10,0,5,0"/>
|
||||
<TextBox Text="{Binding ReferenceConductivity}" Width="70" Margin="0,0,10,0"/>
|
||||
-->
|
||||
<!--<Button Content="开始校准" Command="{Binding PerformSystemCalibrationCommand}" Width="100"/>-->
|
||||
<!--
|
||||
<!--<Button Content="开始校准" Command="{Binding PerformSystemCalibrationCommand}" Width="100"/>-->
|
||||
</WrapPanel>
|
||||
<TextBlock Text="{Binding CalibrationStatus}" FontSize="11" Foreground="Blue" Margin="0,5,0,0"/>
|
||||
</StackPanel>
|
||||
</Border>-->
|
||||
</Border>
|
||||
</Grid>
|
||||
</ScrollViewer>
|
||||
</UserControl>
|
||||
64
Window1.xaml
64
Window1.xaml
@@ -1,64 +0,0 @@
|
||||
<Window x:Class="ConstantCurrentControl.Window1"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
Title="SN-5A 恒流源控制" Height="500" Width="650"
|
||||
WindowStartupLocation="CenterScreen">
|
||||
<Grid Margin="10">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="*"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- 串口设置 -->
|
||||
<GroupBox Header="串口设置" Grid.Row="0" Margin="5">
|
||||
<StackPanel Orientation="Horizontal" Margin="5">
|
||||
<Label Content="端口:"/>
|
||||
<ComboBox x:Name="cmbPort" Width="80" IsEditable="True"/>
|
||||
<Label Content="波特率:" Margin="10,0,0,0"/>
|
||||
<ComboBox x:Name="cmbBaudrate" Width="80" SelectedIndex="1">
|
||||
<ComboBoxItem>9600</ComboBoxItem>
|
||||
<ComboBoxItem>19200</ComboBoxItem>
|
||||
<ComboBoxItem>38400</ComboBoxItem>
|
||||
<ComboBoxItem>115200</ComboBoxItem>
|
||||
</ComboBox>
|
||||
<Button x:Name="btnConnect" Content="连接" Click="BtnConnect_Click" Width="60" Margin="10,0,0,0"/>
|
||||
<Button x:Name="btnDisconnect" Content="断开" Click="BtnDisconnect_Click" Width="60" Margin="5,0,0,0" IsEnabled="False"/>
|
||||
</StackPanel>
|
||||
</GroupBox>
|
||||
|
||||
<!-- 参数设置 -->
|
||||
<GroupBox Header="输出参数(最优稳定性)" Grid.Row="1" Margin="5">
|
||||
<StackPanel Margin="5">
|
||||
|
||||
<StackPanel Orientation="Horizontal" Margin="0,5,0,0">
|
||||
<Label Content="频率:"/>
|
||||
<RadioButton x:Name="rb50Hz" Content="50 Hz" GroupName="Freq" IsChecked="True" Margin="5,0,15,0"/>
|
||||
<RadioButton x:Name="rb60Hz" Content="60 Hz" GroupName="Freq"/>
|
||||
<Label Content="设定电流 (A):" Margin="20,0,0,0"/>
|
||||
<TextBox x:Name="txtSetCurrent" Text="0.650" Width="80" TextAlignment="Center"/>
|
||||
<Label Content="A"/>
|
||||
<Button x:Name="btnSet" Content="直接设定" Click="BtnSet_Click" Width="80" Margin="10,0,0,0"/>
|
||||
<Button x:Name="btnStop" Content="禁止输出" Click="BtnStop_Click" Width="80" Margin="5,0,0,0"/>
|
||||
</StackPanel>
|
||||
<CheckBox x:Name="chkSmooth" Content="启用平滑过渡 (步进0.05A, 间隔200ms)" IsChecked="True" Margin="0,5,0,0"/>
|
||||
</StackPanel>
|
||||
</GroupBox>
|
||||
|
||||
|
||||
|
||||
<!-- 日志 -->
|
||||
<GroupBox Header="通讯日志" Grid.Row="3" Margin="5">
|
||||
<ListBox x:Name="lstLog" Height="120" FontFamily="Consolas"/>
|
||||
</GroupBox>
|
||||
|
||||
<!-- 说明 -->
|
||||
<TextBlock Grid.Row="4" TextWrapping="Wrap" FontSize="11" Foreground="Gray" Margin="5">
|
||||
※ 协议:12字节命令 (FE FE + 识别码 + 电流高/低 + 模式+频率+跟踪 + FF FF)
|
||||
※ 开启跟踪 (byte8=1) 和 PC调节 (byte6=1) 可获最低噪声与最小温漂
|
||||
※ 平滑过渡可避免电流突变产生的冲击噪声
|
||||
</TextBlock>
|
||||
</Grid>
|
||||
</Window>
|
||||
252
Window1.xaml.cs
252
Window1.xaml.cs
@@ -1,252 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace ConstantCurrentControl
|
||||
{
|
||||
public partial class Window1 : Window
|
||||
{
|
||||
private SerialPort _serialPort;
|
||||
private bool _isConnected = false;
|
||||
private DispatcherTimer _readTimer;
|
||||
private List<byte> _receiveBuffer = new List<byte>(); // 接收缓存
|
||||
|
||||
public Window1()
|
||||
{
|
||||
InitializeComponent();
|
||||
Loaded += MainWindow_Loaded;
|
||||
}
|
||||
|
||||
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
cmbPort.ItemsSource = SerialPort.GetPortNames();
|
||||
if (cmbPort.Items.Count > 0) cmbPort.SelectedIndex = 0;
|
||||
|
||||
// 初始化定时器(50ms 间隔,UI 线程)
|
||||
_readTimer = new DispatcherTimer();
|
||||
_readTimer.Interval = TimeSpan.FromMilliseconds(50);
|
||||
_readTimer.Tick += ReadTimer_Tick;
|
||||
}
|
||||
|
||||
#region 串口连接
|
||||
private async void BtnConnect_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (cmbPort.SelectedItem == null)
|
||||
{
|
||||
MessageBox.Show("请选择串口号");
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
string port = cmbPort.SelectedItem.ToString();
|
||||
int baud = int.Parse((cmbBaudrate.SelectedItem as ComboBoxItem).Content.ToString());
|
||||
_serialPort = new SerialPort(port, baud, Parity.None, 8, StopBits.One);
|
||||
_serialPort.ReadTimeout = 100;
|
||||
_serialPort.WriteTimeout = 1000;
|
||||
_serialPort.Open();
|
||||
|
||||
_isConnected = true;
|
||||
btnConnect.IsEnabled = false;
|
||||
btnDisconnect.IsEnabled = true;
|
||||
AddLog($"已连接 {port} @ {baud} bps");
|
||||
|
||||
// 清空接收缓存和串口缓冲区
|
||||
_receiveBuffer.Clear();
|
||||
_serialPort.DiscardInBuffer();
|
||||
|
||||
// 启动定时器读取
|
||||
_readTimer.Start();
|
||||
|
||||
// 发送配置命令(不跟踪模式,byte8=0)
|
||||
await ConfigureOptimalMode();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"连接失败: {ex.Message}");
|
||||
_isConnected = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void BtnDisconnect_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_readTimer != null) _readTimer.Stop();
|
||||
if (_serialPort != null && _serialPort.IsOpen)
|
||||
{
|
||||
_serialPort.Close();
|
||||
_serialPort.Dispose();
|
||||
_serialPort = null;
|
||||
}
|
||||
_isConnected = false;
|
||||
btnConnect.IsEnabled = true;
|
||||
btnDisconnect.IsEnabled = false;
|
||||
AddLog("已断开连接");
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 定时器读取数据
|
||||
private void ReadTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
if (!_isConnected || _serialPort == null || !_serialPort.IsOpen)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
int bytesToRead = _serialPort.BytesToRead;
|
||||
if (bytesToRead > 0)
|
||||
{
|
||||
byte[] buffer = new byte[bytesToRead];
|
||||
_serialPort.Read(buffer, 0, bytesToRead);
|
||||
_receiveBuffer.AddRange(buffer);
|
||||
|
||||
// 解析完整帧(12字节,以 FE FE 开头,FF FF 结尾)
|
||||
ParseCompleteFrames();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AddLog($"读取错误: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void ParseCompleteFrames()
|
||||
{
|
||||
for (int i = 0; i <= _receiveBuffer.Count - 12; i++)
|
||||
{
|
||||
if (_receiveBuffer[i] == 0xFE && _receiveBuffer[i + 1] == 0xFE &&
|
||||
_receiveBuffer[i + 10] == 0xFF && _receiveBuffer[i + 11] == 0xFF)
|
||||
{
|
||||
byte[] frame = _receiveBuffer.Skip(i).Take(12).ToArray();
|
||||
ProcessFrame(frame);
|
||||
// 移除已处理的帧
|
||||
_receiveBuffer.RemoveRange(0, i + 12);
|
||||
i = -1; // 重新扫描
|
||||
}
|
||||
}
|
||||
// 防止缓存无限增长
|
||||
if (_receiveBuffer.Count > 200)
|
||||
_receiveBuffer.RemoveRange(0, _receiveBuffer.Count - 200);
|
||||
}
|
||||
|
||||
private void ProcessFrame(byte[] frame)
|
||||
{
|
||||
// 解析电流(byte4, byte5)
|
||||
int rawCurrent = (frame[4] << 8) | frame[5];
|
||||
double current = rawCurrent / 1000.0;
|
||||
|
||||
AddLog($"回采帧: {BitConverter.ToString(frame)} 电流={current:F3}A");
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 发送命令(只写,不等待读)
|
||||
private async Task<bool> SendCommand(byte[] command)
|
||||
{
|
||||
if (!_isConnected || _serialPort == null || !_serialPort.IsOpen)
|
||||
{
|
||||
AddLog("串口未打开");
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await _serialPort.BaseStream.WriteAsync(command, 0, command.Length);
|
||||
AddLog($"发送: {BitConverter.ToString(command)}");
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AddLog($"发送失败: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ConfigureOptimalMode()
|
||||
{
|
||||
byte[] configCmd = new byte[12];
|
||||
configCmd[0] = 0xFE;
|
||||
configCmd[1] = 0xFE;
|
||||
configCmd[2] = 0x55;
|
||||
configCmd[3] = 0xAA;
|
||||
configCmd[4] = 0x00;
|
||||
configCmd[5] = 0x00;
|
||||
configCmd[6] = 0x01; // PC调节
|
||||
configCmd[7] = rb50Hz.IsChecked == true ? (byte)1 : (byte)2;
|
||||
configCmd[8] = 0x00; // 不跟踪(你实测稳定)
|
||||
configCmd[9] = 0x00;
|
||||
configCmd[10] = 0xFF;
|
||||
configCmd[11] = 0xFF;
|
||||
await SendCommand(configCmd);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region UI操作
|
||||
private async void BtnSet_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (!_isConnected)
|
||||
{
|
||||
MessageBox.Show("请先连接串口");
|
||||
return;
|
||||
}
|
||||
if (!double.TryParse(txtSetCurrent.Text, out double target) || target < 0 || target > 5)
|
||||
{
|
||||
MessageBox.Show("电流需为 0~5.000 A");
|
||||
return;
|
||||
}
|
||||
|
||||
int raw = (int)(target * 1000);
|
||||
byte cmdH = (byte)((raw >> 8) & 0xFF);
|
||||
byte cmdL = (byte)(raw & 0xFF);
|
||||
|
||||
byte[] currentCmd = new byte[12];
|
||||
currentCmd[0] = 0xFE;
|
||||
currentCmd[1] = 0xFE;
|
||||
currentCmd[2] = 0x55;
|
||||
currentCmd[3] = 0xAA;
|
||||
currentCmd[4] = cmdH;
|
||||
currentCmd[5] = cmdL;
|
||||
currentCmd[6] = 0x01;
|
||||
currentCmd[7] = rb50Hz.IsChecked == true ? (byte)1 : (byte)2;
|
||||
currentCmd[8] = 0x00; // 不跟踪
|
||||
currentCmd[9] = 0x00;
|
||||
currentCmd[10] = 0xFF;
|
||||
currentCmd[11] = 0xFF;
|
||||
|
||||
await SendCommand(currentCmd);
|
||||
AddLog($"设定电流 = {target:F3} A");
|
||||
}
|
||||
|
||||
private async void BtnStop_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (!_isConnected) return;
|
||||
byte[] stopCmd = new byte[12];
|
||||
stopCmd[0] = 0xFE;
|
||||
stopCmd[1] = 0xFE;
|
||||
stopCmd[2] = 0x55;
|
||||
stopCmd[3] = 0xAA;
|
||||
stopCmd[4] = 0x00;
|
||||
stopCmd[5] = 0x00;
|
||||
stopCmd[6] = 0x01;
|
||||
stopCmd[7] = rb50Hz.IsChecked == true ? (byte)1 : (byte)2;
|
||||
stopCmd[8] = 0x00; // 不跟踪
|
||||
stopCmd[9] = 0x00;
|
||||
stopCmd[10] = 0xFF;
|
||||
stopCmd[11] = 0xFF;
|
||||
await SendCommand(stopCmd);
|
||||
AddLog("禁止输出 (0A)");
|
||||
}
|
||||
|
||||
private void AddLog(string msg)
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
lstLog.Items.Insert(0, $"{DateTime.Now:HH:mm:ss} {msg}");
|
||||
if (lstLog.Items.Count > 50) lstLog.Items.RemoveAt(lstLog.Items.Count - 1);
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
BIN
瞬态热线法说明书.docx
BIN
瞬态热线法说明书.docx
Binary file not shown.
Reference in New Issue
Block a user