diff --git a/Helpers/ExportHelper.cs b/Helpers/ExportHelper.cs deleted file mode 100644 index 510ee05..0000000 --- a/Helpers/ExportHelper.cs +++ /dev/null @@ -1,54 +0,0 @@ -using OfficeOpenXml; -using System.IO; - -public static class ExportHelper -{ - public static void ExportBubblePoint(BubblePointEntity entity, string filePath) - { - using var package = new ExcelPackage(); - var sheet = package.Workbook.Worksheets.Add("泡点法测试"); - sheet.Cells["A1"].Value = "工位"; sheet.Cells["B1"].Value = entity.StationId; - sheet.Cells["A2"].Value = "测试日期"; sheet.Cells["B2"].Value = entity.TestDate.ToString("yyyy-MM-dd HH:mm:ss"); - sheet.Cells["A3"].Value = "测试者"; sheet.Cells["B3"].Value = entity.Tester; - sheet.Cells["A4"].Value = "样品类型"; sheet.Cells["B4"].Value = entity.SampleType; - sheet.Cells["A5"].Value = "规格"; sheet.Cells["B5"].Value = entity.SampleSpec; - sheet.Cells["A6"].Value = "室温(°C)"; sheet.Cells["B6"].Value = entity.RoomTemperature; - sheet.Cells["A7"].Value = "浸润时间(h)"; sheet.Cells["B7"].Value = entity.SoakingTime; - sheet.Cells["A8"].Value = "测试液体"; sheet.Cells["B8"].Value = entity.LiquidName; - sheet.Cells["A9"].Value = "液体生产厂家"; sheet.Cells["B9"].Value = entity.LiquidManufacturer; - sheet.Cells["A10"].Value = "泡点压力"; sheet.Cells["B10"].Value = $"{entity.BubblePointPressure} {entity.PressureUnit}"; - sheet.Cells["A11"].Value = "最大孔径(μm)"; sheet.Cells["B11"].Value = entity.MaxPoreSize; - - package.SaveAs(new FileInfo(filePath)); - } - - public static void ExportPoreDistribution(PoreDistributionEntity entity, string filePath) - { - using var package = new ExcelPackage(); - var sheet = package.Workbook.Worksheets.Add("孔分布测试"); - sheet.Cells["A1"].Value = "工位"; sheet.Cells["B1"].Value = entity.StationId; - sheet.Cells["A2"].Value = "测试日期"; sheet.Cells["B2"].Value = entity.TestDate.ToString("yyyy-MM-dd HH:mm:ss"); - sheet.Cells["A3"].Value = "测试者"; sheet.Cells["B3"].Value = entity.Tester; - sheet.Cells["A4"].Value = "样品类型"; sheet.Cells["B4"].Value = entity.SampleType; - sheet.Cells["A5"].Value = "规格"; sheet.Cells["B5"].Value = entity.SampleSpec; - sheet.Cells["A6"].Value = "室温(°C)"; sheet.Cells["B6"].Value = entity.RoomTemperature; - sheet.Cells["A7"].Value = "浸润时间(h)"; sheet.Cells["B7"].Value = entity.SoakingTime; - sheet.Cells["A8"].Value = "测试液体"; sheet.Cells["B8"].Value = entity.LiquidName; - sheet.Cells["A9"].Value = "液体生产厂家"; sheet.Cells["B9"].Value = entity.LiquidManufacturer; - sheet.Cells["A10"].Value = "泡点压力"; sheet.Cells["B10"].Value = $"{entity.BubblePointPressure} {entity.PressureUnit}"; - sheet.Cells["A11"].Value = "平均孔径(μm)"; sheet.Cells["B11"].Value = entity.AveragePoreSize; - - // 数据点表格 - sheet.Cells["A13"].Value = "压力"; sheet.Cells["B13"].Value = "湿膜流量(L/min)"; sheet.Cells["C13"].Value = "干膜流量(L/min)"; - int row = 14; - foreach (var dp in entity.DataPoints) - { - sheet.Cells[$"A{row}"].Value = dp.Pressure; - sheet.Cells[$"B{row}"].Value = dp.WetFlow; - sheet.Cells[$"C{row}"].Value = dp.DryFlow; - row++; - } - - package.SaveAs(new FileInfo(filePath)); - } -} \ No newline at end of file diff --git a/Helpers/PlcConfiguration.cs b/Helpers/PlcConfiguration.cs index 4d26440..559ce73 100644 --- a/Helpers/PlcConfiguration.cs +++ b/Helpers/PlcConfiguration.cs @@ -73,6 +73,7 @@ Task ReadHoldingRegistersAsync(ushort startAddress, ushort count); + Task WriteSingleRegisterAsync(ushort registerAddress, ushort value); } diff --git a/Helpers/PoreCalculator.cs b/Helpers/PoreCalculator.cs deleted file mode 100644 index b1d6c46..0000000 --- a/Helpers/PoreCalculator.cs +++ /dev/null @@ -1,31 +0,0 @@ -using MembranePoreTester.Models; - -namespace MembranePoreTester.Helpers -{ - public static class PoreCalculator - { - public static double PressureToPore(double pressure, string unit, TestLiquid liquid) - { - if (liquid == null) return 0; - return unit switch - { - "Pa" => liquid.C_Pa / pressure, - "cmHg" => liquid.C_cmHg / pressure, - "psi" => liquid.C_psi / pressure, - _ => 0 - }; - } - - public static double PoreToPressure(double pore, string unit, TestLiquid liquid) - { - if (liquid == null) return 0; - return unit switch - { - "Pa" => liquid.C_Pa / pore, - "cmHg" => liquid.C_cmHg / pore, - "psi" => liquid.C_psi / pore, - _ => 0 - }; - } - } -} \ No newline at end of file diff --git a/Helpers/PoreDistributionAnalysis.cs b/Helpers/PoreDistributionAnalysis.cs deleted file mode 100644 index c8d962d..0000000 --- a/Helpers/PoreDistributionAnalysis.cs +++ /dev/null @@ -1,69 +0,0 @@ -using MembranePoreTester.Models; -using System.Collections.Generic; -using System.Linq; - -namespace MembranePoreTester.Helpers -{ - public static class PoreDistributionAnalysis - { - public static double CalculateAveragePore( - IEnumerable points, - string unit, - TestLiquid liquid) - { - var sorted = points.OrderBy(p => p.Pressure).ToList(); - if (sorted.Count < 2) return 0; - - double[] pressures = sorted.Select(p => p.Pressure).ToArray(); - double[] dryFlows = sorted.Select(p => p.DryFlow).ToArray(); - double[] wetFlows = sorted.Select(p => p.WetFlow).ToArray(); - - double halfDryFlow = dryFlows.Max() / 2.0; - - // 找到湿膜流量等于 halfDryFlow 的压力(交点) - for (int i = 0; i < wetFlows.Length - 1; i++) - { - if ((wetFlows[i] <= halfDryFlow && wetFlows[i + 1] >= halfDryFlow) || - (wetFlows[i] >= halfDryFlow && wetFlows[i + 1] <= halfDryFlow)) - { - double p = Interpolation.Linear( - new[] { wetFlows[i], wetFlows[i + 1] }, - new[] { pressures[i], pressures[i + 1] }, - halfDryFlow); - return PoreCalculator.PressureToPore(p, unit, liquid); - } - } - return 0; - } - - public static double CalculatePoreRangePercentage( - IEnumerable points, - string unit, - TestLiquid liquid, - double lowerPore, - double upperPore) - { - var sorted = points.OrderBy(p => p.Pressure).ToList(); - if (sorted.Count < 2) return 0; - - double[] pressures = sorted.Select(p => p.Pressure).ToArray(); - double[] wetFlows = sorted.Select(p => p.WetFlow).ToArray(); - double[] dryFlows = sorted.Select(p => p.DryFlow).ToArray(); - - // 大孔径对应低压,小孔径对应高压 - double pLower = PoreCalculator.PoreToPressure(upperPore, unit, liquid); - double pUpper = PoreCalculator.PoreToPressure(lowerPore, unit, liquid); - - // 插值 - double qWetLower = Interpolation.Linear(pressures, wetFlows, pLower); - double qDryLower = Interpolation.Linear(pressures, dryFlows, pLower); - double qWetUpper = Interpolation.Linear(pressures, wetFlows, pUpper); - double qDryUpper = Interpolation.Linear(pressures, dryFlows, pUpper); - - double ratioLow = qWetLower / qDryLower; - double ratioHigh = qWetUpper / qDryUpper; - - return (ratioHigh - ratioLow) * 100; - } - } -} \ No newline at end of file diff --git a/Helpers/ReportGenerator.cs b/Helpers/ReportGenerator.cs deleted file mode 100644 index f52176c..0000000 --- a/Helpers/ReportGenerator.cs +++ /dev/null @@ -1,227 +0,0 @@ -using MembranePoreTester.Models; -using System; -using System.IO; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Documents; -using System.Windows.Xps.Packaging; - -namespace MembranePoreTester.Helpers -{ - public static class ReportGenerator - { - public static void GenerateBubblePointReport(BubblePointRecord record) - { - FlowDocument doc = CreateBubblePointDocument(record); - ShowPrintPreview(doc, "泡点法测试报告"); - } - - public static void GeneratePoreDistributionReport(PoreDistributionRecord record) - { - FlowDocument doc = CreatePoreDistributionDocument(record); - ShowPrintPreview(doc, "孔分布测试报告"); - } - - private static FlowDocument CreateBubblePointDocument(BubblePointRecord record) - { - FlowDocument doc = new FlowDocument(); - doc.PageWidth = 800; // 适应打印 - - // 标题 - doc.Blocks.Add(new Paragraph(new Run("泡点法测试报告")) - { - FontSize = 24, - FontWeight = FontWeights.Bold, - TextAlignment = TextAlignment.Center - }); - - // 日期/测试者 - doc.Blocks.Add(new Paragraph(new Run($"测试日期: {record.TestDate:yyyy-MM-dd} 测试者: {record.Tester}")) - { - TextAlignment = TextAlignment.Right - }); - - // 样品信息表格 - Table table = new Table(); - table.Columns.Add(new TableColumn { Width = new GridLength(150) }); - table.Columns.Add(new TableColumn { Width = new GridLength(200) }); - - TableRowGroup group = new TableRowGroup(); - AddRow(group, "膜类型", record.SampleType); - AddRow(group, "规格", record.SampleSpec); - AddRow(group, "室温(°C)", record.RoomTemperature.ToString("F1")); - AddRow(group, "浸润时间(h)", record.SoakingTime.ToString("F1")); - AddRow(group, "测试液体", record.Liquid?.Name); - AddRow(group, "液体生产厂家", record.LiquidManufacturer); - AddRow(group, "泡点压力", $"{record.BubblePointPressure} {record.PressureUnit}"); - AddRow(group, "最大孔径(μm)", record.MaxPoreSize.ToString("F3")); - - table.RowGroups.Add(group); - doc.Blocks.Add(table); - - doc.Blocks.Add(new Paragraph(new Run("本报告依据 GB/T 32361-2015 生成。")) - { - FontStyle = FontStyles.Italic, - TextAlignment = TextAlignment.Center, - Margin = new Thickness(0, 20, 0, 0) - }); - - return doc; - } - - private static FlowDocument CreatePoreDistributionDocument(PoreDistributionRecord record) - { - FlowDocument doc = new FlowDocument(); - doc.PageWidth = 800; - - // 标题 - doc.Blocks.Add(new Paragraph(new Run("孔分布测试报告")) - { - FontSize = 24, - FontWeight = FontWeights.Bold, - TextAlignment = TextAlignment.Center - }); - - doc.Blocks.Add(new Paragraph(new Run($"测试日期: {record.TestDate:yyyy-MM-dd} 测试者: {record.Tester}")) - { - TextAlignment = TextAlignment.Right - }); - - // 样品信息表格 - Table infoTable = new Table(); - infoTable.Columns.Add(new TableColumn { Width = new GridLength(150) }); - infoTable.Columns.Add(new TableColumn { Width = new GridLength(200) }); - TableRowGroup group = new TableRowGroup(); - AddRow(group, "膜类型", record.SampleType); - AddRow(group, "规格", record.SampleSpec); - AddRow(group, "室温(°C)", record.RoomTemperature.ToString("F1")); - AddRow(group, "浸润时间(h)", record.SoakingTime.ToString("F1")); - AddRow(group, "测试液体", record.Liquid?.Name); - AddRow(group, "液体生产厂家", record.LiquidManufacturer); - infoTable.RowGroups.Add(group); - doc.Blocks.Add(infoTable); - - // 计算结果 - doc.Blocks.Add(new Paragraph(new Run("测试结果")) - { - FontSize = 18, - FontWeight = FontWeights.Bold, - Margin = new Thickness(0, 10, 0, 5) - }); - - Table resultTable = new Table(); - resultTable.Columns.Add(new TableColumn { Width = new GridLength(150) }); - resultTable.Columns.Add(new TableColumn { Width = new GridLength(200) }); - TableRowGroup resultGroup = new TableRowGroup(); - AddRow(resultGroup, "泡点压力", $"{record.BubblePointPressure} {record.PressureUnit}"); - AddRow(resultGroup, "平均孔径(μm)", record.AveragePoreSize.ToString("F3")); - resultTable.RowGroups.Add(resultGroup); - doc.Blocks.Add(resultTable); - - // 数据点列表 - doc.Blocks.Add(new Paragraph(new Run("压力-流量数据")) - { - FontSize = 14, - FontWeight = FontWeights.Bold, - Margin = new Thickness(0, 10, 0, 5) - }); - - Table dataTable = new Table(); - dataTable.Columns.Add(new TableColumn { Width = new GridLength(80) }); - dataTable.Columns.Add(new TableColumn { Width = new GridLength(80) }); - dataTable.Columns.Add(new TableColumn { Width = new GridLength(80) }); - dataTable.CellSpacing = 2; - - TableRowGroup dataGroup = new TableRowGroup(); - - // 表头 - TableRow headerRow = new TableRow(); - headerRow.Cells.Add(new TableCell(new Paragraph(new Run("压力")))); - headerRow.Cells.Add(new TableCell(new Paragraph(new Run("湿膜流量(L/min)")))); - headerRow.Cells.Add(new TableCell(new Paragraph(new Run("干膜流量(L/min)")))); - headerRow.FontWeight = FontWeights.Bold; - dataGroup.Rows.Add(headerRow); - - foreach (var dp in record.DataPoints) - { - TableRow row = new TableRow(); - row.Cells.Add(new TableCell(new Paragraph(new Run(dp.Pressure.ToString("F2"))))); - row.Cells.Add(new TableCell(new Paragraph(new Run(dp.WetFlow.ToString("F3"))))); - row.Cells.Add(new TableCell(new Paragraph(new Run(dp.DryFlow.ToString("F3"))))); - dataGroup.Rows.Add(row); - } - - dataTable.RowGroups.Add(dataGroup); - doc.Blocks.Add(dataTable); - - // 孔分布结果 - if (record.PoreDistributions != null && record.PoreDistributions.Any()) - { - doc.Blocks.Add(new Paragraph(new Run("孔分布")) - { - FontSize = 14, - FontWeight = FontWeights.Bold, - Margin = new Thickness(0, 10, 0, 5) - }); - - Table distTable = new Table(); - distTable.Columns.Add(new TableColumn { Width = new GridLength(80) }); - distTable.Columns.Add(new TableColumn { Width = new GridLength(80) }); - distTable.Columns.Add(new TableColumn { Width = new GridLength(80) }); - - TableRowGroup distGroup = new TableRowGroup(); - - TableRow distHeader = new TableRow(); - distHeader.Cells.Add(new TableCell(new Paragraph(new Run("孔径下限(μm)")))); - distHeader.Cells.Add(new TableCell(new Paragraph(new Run("孔径上限(μm)")))); - distHeader.Cells.Add(new TableCell(new Paragraph(new Run("百分比(%)")))); - distHeader.FontWeight = FontWeights.Bold; - distGroup.Rows.Add(distHeader); - - foreach (var pd in record.PoreDistributions) - { - TableRow row = new TableRow(); - row.Cells.Add(new TableCell(new Paragraph(new Run(pd.LowerPore.ToString("F3"))))); - row.Cells.Add(new TableCell(new Paragraph(new Run(pd.UpperPore.ToString("F3"))))); - row.Cells.Add(new TableCell(new Paragraph(new Run(pd.Percentage.ToString("F1"))))); - distGroup.Rows.Add(row); - } - - distTable.RowGroups.Add(distGroup); - doc.Blocks.Add(distTable); - } - - doc.Blocks.Add(new Paragraph(new Run("本报告依据 GB/T 32361-2015 生成。")) - { - FontStyle = FontStyles.Italic, - TextAlignment = TextAlignment.Center, - Margin = new Thickness(0, 20, 0, 0) - }); - - return doc; - } - - private static void AddRow(TableRowGroup group, string label, string value) - { - TableRow row = new TableRow(); - row.Cells.Add(new TableCell(new Paragraph(new Run(label)))); - row.Cells.Add(new TableCell(new Paragraph(new Run(value ?? "")))); - group.Rows.Add(row); - } - - private static void ShowPrintPreview(FlowDocument document, string title) - { - PrintDialog printDialog = new PrintDialog(); - if (printDialog.ShowDialog() == true) - { - // 调整文档页面大小以匹配打印机 - document.PageHeight = printDialog.PrintableAreaHeight; - document.PageWidth = printDialog.PrintableAreaWidth; - document.PagePadding = new Thickness(50); - - IDocumentPaginatorSource dps = document; - printDialog.PrintDocument(dps.DocumentPaginator, title); - } - } - } -} \ No newline at end of file diff --git a/Models/BubblePointRecord.cs b/Models/BubblePointRecord.cs deleted file mode 100644 index a73258f..0000000 --- a/Models/BubblePointRecord.cs +++ /dev/null @@ -1,30 +0,0 @@ -namespace MembranePoreTester.Models -{ - public class BubblePointRecord - { - public string SampleType { get; set; } // 平板膜/中空纤维膜 - public string SampleSpec { get; set; } // 规格 - public double RoomTemperature { get; set; } // 室温(°C) - public double SoakingTime { get; set; } // 浸润时间(小时) - public TestLiquid Liquid { get; set; } // 测试液体 - public string LiquidManufacturer { get; set; } // 生产厂家 - public double BubblePointPressure { get; set; } // 泡点压力(数值) - public string PressureUnit { get; set; } // Pa/cmHg/psi - public DateTime TestDate { get; set; } = DateTime.Now; - public string Tester { get; set; } - - public double MaxPoreSize => CalculateMaxPore(); - - private double CalculateMaxPore() - { - if (Liquid == null) return 0; - return PressureUnit switch - { - "Pa" => Liquid.C_Pa / BubblePointPressure, - "cmHg" => Liquid.C_cmHg / BubblePointPressure, - "psi" => Liquid.C_psi / BubblePointPressure, - _ => 0 - }; - } - } -} \ No newline at end of file diff --git a/Models/DataPoint.cs b/Models/DataPoint.cs deleted file mode 100644 index 34501e9..0000000 --- a/Models/DataPoint.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace MembranePoreTester.Models -{ - public class DataPoint - { - public double Pressure { get; set; } // 压力 - public double WetFlow { get; set; } // 湿膜流量(L/min) - public double DryFlow { get; set; } // 干膜流量(L/min) - - } -} \ No newline at end of file diff --git a/Models/PoreDistributionRecord.cs b/Models/PoreDistributionRecord.cs deleted file mode 100644 index 8646c80..0000000 --- a/Models/PoreDistributionRecord.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections.ObjectModel; - -namespace MembranePoreTester.Models -{ - /// - /// 孔分布测试的记录模型,存储一次孔分布测试的所有输入数据和计算结果。 - /// 对应 GB/T 32361-2015 标准中的平均流量法测试。 - /// - public class PoreDistributionRecord - { - /// - /// 膜类型:平板膜 或 中空纤维膜。 - /// - public string SampleType { get; set; } - - /// - /// 样品规格,如直径、厚度或型号等。 - /// - public string SampleSpec { get; set; } - - /// - /// 测试时的室温,单位:摄氏度(°C)。 - /// - public double RoomTemperature { get; set; } - - /// - /// 样品在测试液体中的浸润时间,单位:小时(h)。 - /// - public double SoakingTime { get; set; } - - /// - /// 测试使用的液体对象,包含液体名称、表面张力等信息。 - /// - public TestLiquid Liquid { get; set; } - - /// - /// 测试液体的生产厂家,用于溯源。 - /// - public string LiquidManufacturer { get; set; } - - /// - /// 压力单位:Pa、cmHg 或 psi。所有压力相关输入均以此单位为准。 - /// - public string PressureUnit { get; set; } - - /// - /// 压力-流量数据点集合,每个点包含压力、湿膜流量、干膜流量。 - /// 使用 ObservableCollection 以便在界面添加/删除时自动更新。 - /// - public ObservableCollection DataPoints { get; set; } = new(); - - /// - /// 测试日期,默认为当前日期时间。 - /// - public DateTime TestDate { get; set; } = DateTime.Now; - - /// - /// 测试人员姓名。 - /// - public string Tester { get; set; } - - // ---------- 计算结果(由计算逻辑填充) ---------- - - /// - /// 泡点压力(由用户单独记录或从数据点推断),用于最大孔径计算。 - /// - public double BubblePointPressure { get; set; } - - /// - /// 计算出的平均孔径,单位:微米(μm)。 - /// 对应标准中平均流量法的平均孔径结果。 - /// - public double AveragePoreSize { get; set; } - - /// - /// 孔分布计算结果列表,每个元素表示一个孔径区间的流量百分比。 - /// 对应标准中孔分布的多个区间结果。 - /// - public List PoreDistributions { get; set; } = new(); - } -} \ No newline at end of file diff --git a/Models/PoreDistributionResult.cs b/Models/PoreDistributionResult.cs deleted file mode 100644 index 119f504..0000000 --- a/Models/PoreDistributionResult.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace MembranePoreTester.Models -{ - public class PoreDistributionResult - { - public double LowerPore { get; set; } // 孔径下限(μm) - public double UpperPore { get; set; } // 孔径上限(μm) - public double Percentage { get; set; } // 流量百分比 - } -} \ No newline at end of file diff --git a/Models/TestLiquid.cs b/Models/TestLiquid.cs deleted file mode 100644 index 88d7f05..0000000 --- a/Models/TestLiquid.cs +++ /dev/null @@ -1,22 +0,0 @@ -namespace MembranePoreTester.Models -{ - public class TestLiquid - { - public string Name { get; set; } - public double SurfaceTension { get; set; } // mN/m, 25°C - - // Cγ 值 - public double C_Pa => 2860 * SurfaceTension; - public double C_cmHg => 2.15 * SurfaceTension; - public double C_psi => 0.415 * SurfaceTension; - - public static List Predefined => new() - { - new TestLiquid { Name = "水", SurfaceTension = 72.0 }, - new TestLiquid { Name = "石油馏分", SurfaceTension = 30.0 }, - new TestLiquid { Name = "乙醇", SurfaceTension = 22.3 }, - new TestLiquid { Name = "液态石蜡", SurfaceTension = 34.7 }, - new TestLiquid { Name = "BSD16", SurfaceTension = 16.0 } // 新增 - }; - } -} \ No newline at end of file diff --git a/Report/Report.cs b/Report/Report.cs deleted file mode 100644 index b75f571..0000000 --- a/Report/Report.cs +++ /dev/null @@ -1,59 +0,0 @@ -using MembranePoreTester.Models; -using System; -using System.Collections.Generic; -using System.Text; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Documents; - -namespace MembranePoreTester.Report -{ - public class Report - { - public static void GenerateBubblePointReport(BubblePointRecord record) - { - FlowDocument doc = new FlowDocument(); - doc.PageWidth = 800; // 适应打印 - - Section header = new Section(); - header.Blocks.Add(new Paragraph(new Run("泡点法测试报告")) - { FontSize = 24, FontWeight = FontWeights.Bold, TextAlignment = TextAlignment.Center }); - header.Blocks.Add(new Paragraph(new Run($"测试日期: {record.TestDate:yyyy-MM-dd} 测试者: {record.Tester}")) - { TextAlignment = TextAlignment.Right }); - - // 样品信息表格 - Table table = new Table(); - table.Columns.Add(new TableColumn { Width = new GridLength(150) }); - table.Columns.Add(new TableColumn { Width = new GridLength(200) }); - table.RowGroups.Add(new TableRowGroup()); - - AddRow(table, "膜类型", record.SampleType); - AddRow(table, "规格", record.SampleSpec); - AddRow(table, "室温(°C)", record.RoomTemperature.ToString("F1")); - AddRow(table, "浸润时间(h)", record.SoakingTime.ToString("F1")); - AddRow(table, "测试液体", record.Liquid?.Name); - AddRow(table, "液体生产厂家", record.LiquidManufacturer); - AddRow(table, "泡点压力", $"{record.BubblePointPressure} {record.PressureUnit}"); - AddRow(table, "最大孔径(μm)", record.MaxPoreSize.ToString("F3")); - - doc.Blocks.Add(table); - doc.Blocks.Add(new Paragraph(new Run("本报告依据GB/T 32361-2015生成。")) { FontStyle = FontStyles.Italic }); - - // 打印预览 - PrintDialog printDialog = new PrintDialog(); - if (printDialog.ShowDialog() == true) - { - IDocumentPaginatorSource dps = doc; - printDialog.PrintDocument(dps.DocumentPaginator, "泡点法测试报告"); - } - } - - private static void AddRow(Table table, string label, string value) - { - TableRow row = new TableRow(); - row.Cells.Add(new TableCell(new Paragraph(new Run(label)))); - row.Cells.Add(new TableCell(new Paragraph(new Run(value ?? "")))); - table.RowGroups[0].Rows.Add(row); - } - } -} diff --git a/ViewModels/BubblePointViewModel.cs b/ViewModels/BubblePointViewModel.cs deleted file mode 100644 index 2086fd4..0000000 --- a/ViewModels/BubblePointViewModel.cs +++ /dev/null @@ -1,232 +0,0 @@ -using MembranePoreTester.Communication; -using MembranePoreTester.Helpers; -using MembranePoreTester.Models; -using Microsoft.Win32; -using System.Collections.Generic; -using System.Windows; -using System.Windows.Input; - -namespace MembranePoreTester.ViewModels -{ - public class BubblePointViewModel : ViewModelBase - { - private BubblePointRecord _record = new(); - private TestLiquid _selectedLiquid; - private bool _isCustomLiquid; - private double _customSurfaceTension = 30.0; - - - // 添加字段 - private readonly IPlcService _plcService; - private readonly PlcConfiguration _plcConfig; - - public ICommand ReadPlcCommand { get; } - - public BubblePointRecord Record - { - get => _record; - set => SetProperty(ref _record, value); - } - - public List Liquids => TestLiquid.Predefined; - public List PressureUnits => new() { "Pa", "cmHg", "psi" }; - public List MembraneTypes => new() { "平板膜", "中空纤维膜" }; - - public TestLiquid SelectedLiquid - { - get => _selectedLiquid; - set - { - if (SetProperty(ref _selectedLiquid, value)) - { - Record.Liquid = value; - OnPropertyChanged(nameof(Record.MaxPoreSize)); - } - } - } - - public bool IsCustomLiquid - { - get => _isCustomLiquid; - set - { - if (SetProperty(ref _isCustomLiquid, value)) - { - UpdateCustomLiquid(); - } - } - } - - public double CustomSurfaceTension - { - get => _customSurfaceTension; - set - { - if (SetProperty(ref _customSurfaceTension, value)) - { - UpdateCustomLiquid(); - } - } - } - - private void UpdateCustomLiquid() - { - if (IsCustomLiquid) - { - Record.Liquid = new TestLiquid - { - Name = "自定义", - SurfaceTension = CustomSurfaceTension - }; - OnPropertyChanged(nameof(Record.MaxPoreSize)); - } - } - - public ICommand CalculateCommand { get; } - public ICommand GenerateReportCommand { get; } - - public ICommand OpenPressureCalibCommand { get; } - - public BubblePointViewModel() - { - - _plcService = App.PlcService; - _plcConfig = App.PlcConfig; - CalculateCommand = new RelayCommand(Calculate); - GenerateReportCommand = new RelayCommand(GenerateReport); - SelectedLiquid = Liquids[0]; - ReadPlcCommand = new RelayCommand(async () => await ReadPlcAsync()); - SaveCommand = new RelayCommand(SaveToDatabase); - ExportCommand = new RelayCommand(ExportToExcel); - OpenPressureCalibCommand = new RelayCommand(OpenPressureCalibration); - } - - - private async Task ReadPlcAsync() - { - try - { - float rawPressure = await _plcService.ReadPressureAsync(StationId); - Record.BubblePointPressure = rawPressure * _plcConfig.PressureFactor; - OnPropertyChanged(nameof(Record.BubblePointPressure)); - } - catch (Exception ex) - { - MessageBox.Show($"读取PLC失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); - } - } - - - public double MaxPoreSize => Record.MaxPoreSize; - - private void Calculate() - { - // 触发最大孔径重新计算(通过绑定已自动刷新) - OnPropertyChanged(nameof(Record.MaxPoreSize)); - } - - private void GenerateReport() - { - ReportGenerator.GenerateBubblePointReport(Record); - } - - public void UpdateBubblePointPressure(double pressure) - { - Record.BubblePointPressure = pressure; - OnPropertyChanged(nameof(Record.BubblePointPressure)); - OnPropertyChanged(nameof(MaxPoreSize)); // 最大孔径依赖于压力 - } - - - - private int _stationId; - public int StationId - { - get => _stationId; - set => SetProperty(ref _stationId, value); - } - - // 保存当前记录到数据库 - public void SaveToDatabase() - { - var entity = new BubblePointEntity - { - StationId = StationId, - TestDate = Record.TestDate, - Tester = Record.Tester, - SampleType = Record.SampleType, - SampleSpec = Record.SampleSpec, - RoomTemperature = Record.RoomTemperature, - SoakingTime = Record.SoakingTime, - LiquidName = Record.Liquid?.Name, - LiquidSurfaceTension = Record.Liquid?.SurfaceTension ?? 0, - LiquidManufacturer = Record.LiquidManufacturer, - BubblePointPressure = Record.BubblePointPressure, - PressureUnit = Record.PressureUnit, - MaxPoreSize = Record.MaxPoreSize - }; - using var db = new AppDbContext(); - db.BubblePointRecords.Add(entity); - db.SaveChanges(); - } - - public void LoadFromDatabase(int recordId) - { - using var db = new AppDbContext(); - var entity = db.BubblePointRecords.Find(recordId); - if (entity == null) return; - - Record.SampleType = entity.SampleType; - Record.SampleSpec = entity.SampleSpec; - Record.RoomTemperature = entity.RoomTemperature; - Record.SoakingTime = entity.SoakingTime; - Record.Liquid = TestLiquid.Predefined.FirstOrDefault(l => l.Name == entity.LiquidName) - ?? new TestLiquid { Name = entity.LiquidName, SurfaceTension = entity.LiquidSurfaceTension }; - Record.LiquidManufacturer = entity.LiquidManufacturer; - Record.BubblePointPressure = entity.BubblePointPressure; - Record.PressureUnit = entity.PressureUnit; - Record.TestDate = entity.TestDate; - Record.Tester = entity.Tester; - - OnPropertyChanged(nameof(Record)); - OnPropertyChanged(nameof(MaxPoreSize)); // 触发界面更新 - } - - - public ICommand SaveCommand { get; } - - public ICommand ExportCommand { get; } - - private void OpenPressureCalibration() - { - // 使用简单的输入框获取新零点系数和量程系数 - string zeroStr = Microsoft.VisualBasic.Interaction.InputBox("请输入压力零点系数", "压力校准", "0"); - string spanStr = Microsoft.VisualBasic.Interaction.InputBox("请输入压力量程系数", "压力校准", "1"); - if (float.TryParse(zeroStr, out float zero) && float.TryParse(spanStr, out float span)) - { - Task.Run(async () => - { - await WriteFloatAsync(_plcConfig.PressureCalibZero, zero); - await WriteFloatAsync(_plcConfig.PressureCalibSpan, span); - MessageBox.Show("压力校准系数已写入", "完成"); - }); - } - } - - private void ExportToExcel() - { - var saveFileDialog = new SaveFileDialog - { - Filter = "Excel文件|*.xlsx", - FileName = $"泡点法_工位{StationId}_{DateTime.Now:yyyyMMddHHmmss}.xlsx" - }; - if (saveFileDialog.ShowDialog() == true) - { - // 转换为Entity后导出 - var entity = new BubblePointEntity { /* 从Record复制 */ }; - ExportHelper.ExportBubblePoint(entity, saveFileDialog.FileName); - MessageBox.Show("导出成功"); - } - } - } -} \ No newline at end of file diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs deleted file mode 100644 index 35c7c93..0000000 --- a/ViewModels/MainViewModel.cs +++ /dev/null @@ -1,160 +0,0 @@ -using MembranePoreTester.Communication; -using System.Collections.ObjectModel; -using System.ComponentModel; // 用于 PropertyChanged -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Input; - -namespace MembranePoreTester.ViewModels -{ - public class MainViewModel : ViewModelBase - { - public ObservableCollection Stations { get; } = new(); - - public class StationItem : ViewModelBase - { - private readonly IPlcService _plcService; - private readonly PlcConfiguration _plcConfig; - private bool _isPressing; - private string _pressButtonText = "加压"; - private string _highLowPressure = "低压"; - private bool _enableStatus; // M21 状态 - - public string Name { get; set; } - public BubblePointViewModel BubblePointVM { get; set; } - public PoreDistributionViewModel PoreDistributionVM { get; set; } - public int StationId { get; set; } - - public string HighLowPressure - { - get => _highLowPressure; - set - { - if (SetProperty(ref _highLowPressure, value)) - { - // 当选择变化时,写入 PLC 压力模式寄存器 - Task.Run(async () => await WritePressureModeAsync(value)); - } - } - } - - public string PressButtonText - { - get => _pressButtonText; - set => SetProperty(ref _pressButtonText, value); - } - - // 使能状态(只读) - public bool EnableStatus - { - get => _enableStatus; - private set => SetProperty(ref _enableStatus, value); - } - // 使能状态显示文本 - public string EnableStatusText => EnableStatus ? "运行中" : "未启动"; - - // 使能状态显示颜色(绿色表示运行中,灰色表示未启动) - public string EnableStatusColor => EnableStatus ? "Green" : "Gray"; - - // 定时器,用于轮询 M21 状态 - private System.Windows.Threading.DispatcherTimer _timer; - - public ICommand PressCommand { get; } - //public ICommand BurstCommand { get; } - public ICommand StartCommand { get; } - public ICommand StopCommand { get; } - public ICommand EnableCommand { get; } // 备用,但可以使用复选框直接绑定 - - public StationItem() - { - _plcService = App.PlcService; - _plcConfig = App.PlcConfig; - - PressCommand = new RelayCommand(async () => await TogglePressAsync()); - //BurstCommand = new RelayCommand(async () => await ReadBurstPressureAsync()); - StartCommand = new RelayCommand(async () => await WriteCoilAsync(_plcConfig.StartCoil, true)); - StopCommand = new RelayCommand(async () => await WriteCoilAsync(_plcConfig.StopCoil, true)); - // 启动定时器,每秒读取一次 M21 状态 - _timer = new System.Windows.Threading.DispatcherTimer(); - _timer.Interval = TimeSpan.FromSeconds(1); - _timer.Tick += async (s, e) => await ReadEnableStatusAsync(); - _timer.Start(); - } - - private async Task TogglePressAsync() - { - _isPressing = !_isPressing; - await WriteCoilAsync(_plcConfig.PressCoil, _isPressing); - PressButtonText = _isPressing ? "停止加压" : "加压"; - } - - - private async Task ReadEnableStatusAsync() - { - try - { - bool status = await _plcService.ReadCoilAsync(_plcConfig.EnableCoil); // 读取 M21 - EnableStatus = status; - } - catch (Exception ex) - { - // 读取出错时保持原状态或显示错误 - System.Diagnostics.Debug.WriteLine($"读取使能状态失败: {ex.Message}"); - } - } - - private async Task WriteCoilAsync(ushort coil, bool value) - { - try - { - await _plcService.WriteCoilAsync(coil, value); - } - catch (Exception ex) - { - MessageBox.Show($"写线圈失败: {ex.Message}"); - } - } - - private async Task WritePressureModeAsync(string mode) - { - ushort val = mode == "高压" ? (ushort)1 : (ushort)0; - try - { - await _plcService.WriteRegisterAsync(_plcConfig.PressureModeRegister, val); - } - catch (Exception ex) - { - MessageBox.Show($"写压力模式失败: {ex.Message}"); - } - } - - - - - - - - } - - - - - - - - public MainViewModel() - { - for (int i = 1; i <= 3; i++) - { - var station = new StationItem - { - Name = $"工位 {i}", - BubblePointVM = new BubblePointViewModel { StationId = i }, - PoreDistributionVM = new PoreDistributionViewModel { StationId = i }, - StationId = i - }; - Stations.Add(station); - } - } - } -} \ No newline at end of file diff --git a/ViewModels/PoreDistributionViewModel.cs b/ViewModels/PoreDistributionViewModel.cs deleted file mode 100644 index 1a5a625..0000000 --- a/ViewModels/PoreDistributionViewModel.cs +++ /dev/null @@ -1,409 +0,0 @@ -using MembranePoreTester.Communication; -using MembranePoreTester.Helpers; -using MembranePoreTester.Models; -using Microsoft.EntityFrameworkCore; -using Microsoft.Win32; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using System.Windows; -using System.Windows.Input; - - -namespace MembranePoreTester.ViewModels -{ - public class PoreDistributionViewModel : ViewModelBase, IStationViewModel - { - private PoreDistributionRecord _record = new(); - private TestLiquid _selectedLiquid; - private bool _isCustomLiquid; - private double _customSurfaceTension = 30.0; - private double _lowerPore = 0.2; - private double _upperPore = 0.8; - private double _rangePercentage; - - - - // 添加字段 - private readonly IPlcService _plcService; - private readonly PlcConfiguration _plcConfig; - private DataPoint _selectedDataPoint; - - - // 添加属性 - public DataPoint SelectedDataPoint - { - get => _selectedDataPoint; - set => SetProperty(ref _selectedDataPoint, value); - } - - public ICommand ReadPlcCommand { get; } - - public PoreDistributionRecord Record - { - get => _record; - set => SetProperty(ref _record, value); - } - - public List Liquids => TestLiquid.Predefined; - public List PressureUnits => new() { "Pa", "cmHg", "psi" }; - public List MembraneTypes => new() { "平板膜", "中空纤维膜" }; - - public TestLiquid SelectedLiquid - { - get => _selectedLiquid; - set - { - if (SetProperty(ref _selectedLiquid, value)) - { - Record.Liquid = value; - } - } - } - - public bool IsCustomLiquid - { - get => _isCustomLiquid; - set - { - if (SetProperty(ref _isCustomLiquid, value)) - { - UpdateCustomLiquid(); - } - } - } - - public double CustomSurfaceTension - { - get => _customSurfaceTension; - set - { - if (SetProperty(ref _customSurfaceTension, value)) - { - UpdateCustomLiquid(); - } - } - } - - public double LowerPore - { - get => _lowerPore; - set => SetProperty(ref _lowerPore, value); - } - - public double UpperPore - { - get => _upperPore; - set => SetProperty(ref _upperPore, value); - } - - public double RangePercentage - { - get => _rangePercentage; - set => SetProperty(ref _rangePercentage, value); - } - - public ICommand AddDataPointCommand { get; } - public ICommand RemoveDataPointCommand { get; } - public ICommand CalculateCommand { get; } - public ICommand GenerateReportCommand { get; } - - public PoreDistributionViewModel() - { - _plcService = App.PlcService; - _plcConfig = App.PlcConfig; - - AddDataPointCommand = new RelayCommand(AddDataPoint); - RemoveDataPointCommand = new RelayCommand(RemoveDataPoint, () => Record.DataPoints.Any()); - CalculateCommand = new RelayCommand(Calculate); - GenerateReportCommand = new RelayCommand(GenerateReport); - SelectedLiquid = Liquids[0]; - Record.PressureUnit = PressureUnits[0]; // 默认 "Pa" - - ReadPlcCommand = new RelayCommand(async () => await ReadPlcAsync()); - SaveCommand = new RelayCommand(SaveToDatabase); - ExportCommand = new RelayCommand(ExportToExcel); - OpenFlowCalibCommand = new RelayCommand(OpenFlowCalibration); - - - Record.DataPoints.CollectionChanged += (s, e) => UpdatePlot(); - } - - - private async Task ReadPlcAsync() - { - try - { - // 始终读取压力 - float rawPressure = await _plcService.ReadPressureAsync(StationId); - double pressure = rawPressure * _plcConfig.PressureFactor; - - if (SelectedDataPoint != null) - { - // 更新选中行 - SelectedDataPoint.Pressure = pressure; - if (TestMode == "湿膜") - { - float rawWet = await _plcService.ReadWetFlowAsync(); - SelectedDataPoint.WetFlow = rawWet * _plcConfig.WetFlowFactor; - } - else - { - float rawDry = await _plcService.ReadDryFlowAsync(); - SelectedDataPoint.DryFlow = rawDry * _plcConfig.DryFlowFactor; - } - } - else - { - // 新增一行 - var newPoint = new DataPoint { Pressure = pressure }; - if (TestMode == "湿膜") - { - float rawWet = await _plcService.ReadWetFlowAsync(); - newPoint.WetFlow = rawWet * _plcConfig.WetFlowFactor; - } - else - { - float rawDry = await _plcService.ReadDryFlowAsync(); - newPoint.DryFlow = rawDry * _plcConfig.DryFlowFactor; - } - Record.DataPoints.Add(newPoint); - } - } - catch (Exception ex) - { - MessageBox.Show($"读取PLC失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); - } - } - - private void AddDataPoint() - { - Record.DataPoints.Add(new DataPoint()); - } - - private void RemoveDataPoint() - { - if (Record.DataPoints.Any()) - Record.DataPoints.RemoveAt(Record.DataPoints.Count - 1); - } - - private void UpdateCustomLiquid() - { - if (IsCustomLiquid) - { - Record.Liquid = new TestLiquid - { - Name = "自定义", - SurfaceTension = CustomSurfaceTension - }; - } - } - - // 添加私有字段和公共属性 - private double _averagePoreSize; - public double AveragePoreSize - { - get => _averagePoreSize; - set => SetProperty(ref _averagePoreSize, value); - } - - - // 修改 Calculate 方法: - private void Calculate() - { - if (Record.DataPoints.Count < 2) return; - - AveragePoreSize = PoreDistributionAnalysis.CalculateAveragePore( - Record.DataPoints, Record.PressureUnit, Record.Liquid); - - RangePercentage = PoreDistributionAnalysis.CalculatePoreRangePercentage( - Record.DataPoints, Record.PressureUnit, Record.Liquid, LowerPore, UpperPore); - } - - private void GenerateReport() - { - ReportGenerator.GeneratePoreDistributionReport(Record); - } - - private void OpenFlowCalibration() - { - string zeroStr = Microsoft.VisualBasic.Interaction.InputBox("请输入流量零点系数", "流量校准", "0"); - string spanStr = Microsoft.VisualBasic.Interaction.InputBox("请输入流量量程系数", "流量校准", "1"); - if (float.TryParse(zeroStr, out float zero) && float.TryParse(spanStr, out float span)) - { - Task.Run(async () => - { - await WriteFloatAsync(_plcConfig.FlowCalibZero, zero); - await WriteFloatAsync(_plcConfig.FlowCalibSpan, span); - MessageBox.Show("流量校准系数已写入", "完成"); - }); - } - } - - private int _stationId; - public int StationId - { - get => _stationId; - set => SetProperty(ref _stationId, value); - } - - public void SaveToDatabase() - { - var entity = new PoreDistributionEntity - { - StationId = this.StationId, - TestDate = Record.TestDate, - Tester = Record.Tester, - SampleType = Record.SampleType, - SampleSpec = Record.SampleSpec, - RoomTemperature = Record.RoomTemperature, - SoakingTime = Record.SoakingTime, - LiquidName = Record.Liquid?.Name, - LiquidSurfaceTension = Record.Liquid?.SurfaceTension ?? 0, - LiquidManufacturer = Record.LiquidManufacturer, - PressureUnit = Record.PressureUnit, - BubblePointPressure = Record.BubblePointPressure, - AveragePoreSize = AveragePoreSize, // 使用计算后的属性 - DataPoints = Record.DataPoints.Select(dp => new DataPointEntity - { - Pressure = dp.Pressure, - WetFlow = dp.WetFlow, - DryFlow = dp.DryFlow - }).ToList() - }; - - using var db = new AppDbContext(); - db.PoreDistributionRecords.Add(entity); - db.SaveChanges(); - - MessageBox.Show("保存成功!", "提示"); - } - - public void LoadFromDatabase(int recordId) - { - using var db = new AppDbContext(); - var entity = db.PoreDistributionRecords - .Include(p => p.DataPoints) - .FirstOrDefault(p => p.Id == recordId); - if (entity == null) return; - - Record.SampleType = entity.SampleType; - Record.SampleSpec = entity.SampleSpec; - Record.RoomTemperature = entity.RoomTemperature; - Record.SoakingTime = entity.SoakingTime; - Record.Liquid = TestLiquid.Predefined.FirstOrDefault(l => l.Name == entity.LiquidName) - ?? new TestLiquid { Name = entity.LiquidName, SurfaceTension = entity.LiquidSurfaceTension }; - Record.LiquidManufacturer = entity.LiquidManufacturer; - Record.PressureUnit = entity.PressureUnit; - Record.BubblePointPressure = entity.BubblePointPressure; - Record.TestDate = entity.TestDate; - Record.Tester = entity.Tester; - - Record.DataPoints.Clear(); - foreach (var dp in entity.DataPoints) - { - Record.DataPoints.Add(new DataPoint - { - Pressure = dp.Pressure, - WetFlow = dp.WetFlow, - DryFlow = dp.DryFlow - }); - } - - // 重新计算平均孔径和分布(触发计算命令) - Calculate(); - } - - - public ICommand SaveCommand { get; } - - - public ICommand OpenFlowCalibCommand { get; } - public ICommand ExportCommand { get; } - - - - private void ExportToExcel() - { - var saveFileDialog = new SaveFileDialog - { - Filter = "Excel文件|*.xlsx", - FileName = $"泡点法_工位{StationId}_{DateTime.Now:yyyyMMddHHmmss}.xlsx" - }; - if (saveFileDialog.ShowDialog() == true) - { - // 转换为Entity后导出 - var entity = new BubblePointEntity { /* 从Record复制 */ }; - ExportHelper.ExportBubblePoint(entity, saveFileDialog.FileName); - MessageBox.Show("导出成功"); - } - } - - - private string _testMode = "湿膜"; - public string TestMode - { - get => _testMode; - set => SetProperty(ref _testMode, value); - } - - private int _selectedFlowModeIndex; // 0=大流量,1=小流量 - public int SelectedFlowModeIndex - { - get => _selectedFlowModeIndex; - set - { - if (SetProperty(ref _selectedFlowModeIndex, value)) - { - ushort reg = StationId switch - { - 1 => _plcConfig.FlowModeRegister1, - 2 => _plcConfig.FlowModeRegister2, - 3 => _plcConfig.FlowModeRegister3, - _ => 0 - }; - Task.Run(async () => await _plcService.WriteSingleRegisterAsync(reg, (ushort)value)); - } - } - } - - - private OxyPlot.PlotModel _plotModel; - public OxyPlot.PlotModel PlotModel - { - get => _plotModel; - set => SetProperty(ref _plotModel, value); - } - - - private void UpdatePlot() - { - var sorted = Record.DataPoints.OrderBy(p => p.Pressure).ToList(); - if (sorted.Count == 0) - { - PlotModel = null; - return; - } - - var model = new OxyPlot.PlotModel { Title = "流量-压力曲线" }; - model.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = OxyPlot.Axes.AxisPosition.Bottom, Title = "压力" }); - model.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = OxyPlot.Axes.AxisPosition.Left, Title = "流量 (L/min)" }); - - var wetSeries = new OxyPlot.Series.LineSeries { Title = "湿膜流量", Color = OxyPlot.OxyColors.Blue, MarkerType = OxyPlot.MarkerType.Circle }; - var drySeries = new OxyPlot.Series.LineSeries { Title = "干膜流量", Color = OxyPlot.OxyColors.Red, MarkerType = OxyPlot.MarkerType.Circle }; - - foreach (var dp in sorted) - { - wetSeries.Points.Add(new OxyPlot.DataPoint(dp.Pressure, dp.WetFlow)); - drySeries.Points.Add(new OxyPlot.DataPoint(dp.Pressure, dp.DryFlow)); - } - - model.Series.Add(wetSeries); - model.Series.Add(drySeries); - - PlotModel = model; - } - - - } -} \ No newline at end of file diff --git a/ViewModels/StationViewModel.cs b/ViewModels/StationViewModel.cs deleted file mode 100644 index 007254a..0000000 --- a/ViewModels/StationViewModel.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System.Windows.Input; -using MembranePoreTester.Communication; -using System.Threading.Tasks; -using System.Windows; - -namespace MembranePoreTester.ViewModels -{ - public class StationViewModel : ViewModelBase - { - private readonly IPlcService _plcService; - private readonly PlcConfiguration _plcConfig; - private bool _isPressing; - private string _pressButtonText = "加压"; - private string _highLowPressure = "低压"; - private bool _enableChecked; - - public int StationId { get; set; } - - public BubblePointViewModel BubblePointVM { get; } = new(); - public PoreDistributionViewModel PoreDistributionVM { get; } = new(); - - public string HighLowPressure - { - get => _highLowPressure; - set => SetProperty(ref _highLowPressure, value); - } - - public string PressButtonText - { - get => _pressButtonText; - set => SetProperty(ref _pressButtonText, value); - } - - public bool EnableChecked - { - get => _enableChecked; - set => SetProperty(ref _enableChecked, value); - } - - public ICommand PressCommand { get; } - public ICommand BurstCommand { get; } - public ICommand StartCommand { get; } - public ICommand StopCommand { get; } - public ICommand EnableCommand { get; } // 用于使能复选框 - - public StationViewModel() - { - _plcService = App.PlcService; - _plcConfig = App.PlcConfig; - BubblePointVM.StationId = StationId; - PoreDistributionVM.StationId = StationId; - // 初始化按钮文字 - _pressButtonText = "加压"; // 直接设置字段,避免触发属性通知循环 - PressButtonText = "加压"; // 或者通过属性设置 - System.Diagnostics.Debug.WriteLine($"工位{StationId} PressButtonText初始值: {PressButtonText}"); - PressCommand = new RelayCommand(async () => await TogglePressAsync()); - BurstCommand = new RelayCommand(async () => await ReadBurstPressureAsync()); - StartCommand = new RelayCommand(async () => await WriteCoilAsync(_plcConfig.StartCoil, true)); - StopCommand = new RelayCommand(async () => await WriteCoilAsync(_plcConfig.StopCoil, true)); - // 使能复选框点击时写入对应线圈 - EnableCommand = new RelayCommand(async () => await WriteCoilAsync(_plcConfig.EnableCoil, EnableChecked)); - } - - private async Task TogglePressAsync() - { - _isPressing = !_isPressing; - await WriteCoilAsync(_plcConfig.PressCoil, _isPressing); - PressButtonText = _isPressing ? "停止加压" : "加压"; - } - - private async Task ReadBurstPressureAsync() - { - try - { - float pressure = await ((ModbusTcpPlcService)_plcService).ReadPressureAsync(StationId); - BubblePointVM.UpdateBubblePointPressure(pressure * _plcConfig.PressureFactor); - MessageBox.Show($"涨破压力: {pressure} {BubblePointVM.Record.PressureUnit}", "提示"); - } - catch (Exception ex) - { - MessageBox.Show($"读取压力失败: {ex.Message}"); - } - } - - private async Task WriteCoilAsync(ushort coil, bool value) - { - try - { - await ((ModbusTcpPlcService)_plcService).WriteCoilAsync(coil, value); - } - catch (Exception ex) - { - MessageBox.Show($"写线圈失败: {ex.Message}"); - } - } - } -} \ No newline at end of file diff --git a/Views/BubblePointView.xaml b/Views/BubblePointView.xaml deleted file mode 100644 index 2d75cc8..0000000 --- a/Views/BubblePointView.xaml +++ /dev/null @@ -1,89 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -