Files
VacuumPressureMembranePoreS…/App.xaml.cs

54 lines
1.6 KiB
C#
Raw Normal View History

2026-04-11 08:51:47 +08:00
using MembranePoreTester.Communication;
using Microsoft.Extensions.Configuration;
using OfficeOpenXml;
using System;
2026-02-27 16:58:02 +08:00
using System.IO;
using System.Windows;
2026-02-26 17:07:19 +08:00
2026-02-27 16:03:49 +08:00
namespace MembranePoreTester
2026-02-26 17:07:19 +08:00
{
public partial class App : Application
{
2026-02-27 16:58:02 +08:00
public static IPlcService PlcService { get; private set; }
public static PlcConfiguration PlcConfig { get; private set; }
2026-04-10 18:54:06 +08:00
protected async override void OnStartup(StartupEventArgs e)
2026-02-27 16:58:02 +08:00
{
2026-04-11 08:51:47 +08:00
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
2026-02-27 16:58:02 +08:00
base.OnStartup(e);
2026-03-02 18:50:30 +08:00
using var db = new AppDbContext();
db.Database.EnsureCreated(); // 自动建表
2026-02-27 16:58:02 +08:00
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
var configuration = builder.Build();
PlcConfig = configuration.GetSection("PlcSettings").Get<PlcConfiguration>();
if (PlcConfig == null)
throw new InvalidOperationException("PLC settings missing in appsettings.json");
PlcService = new ModbusTcpPlcService(PlcConfig);
2026-04-10 18:54:06 +08:00
var plcService = App.PlcService as ModbusTcpPlcService;
try
{
await plcService.EnsureConnectedAsync();
}
catch (Exception ex)
{
MessageBox.Show($"PLC 连接失败:{ex.Message}");
}
2026-02-27 16:58:02 +08:00
}
protected override void OnExit(ExitEventArgs e)
{
(PlcService as IDisposable)?.Dispose();
base.OnExit(e);
}
2026-02-26 17:07:19 +08:00
}
2026-02-27 16:03:49 +08:00
}