设备列表
This commit is contained in:
27
DeviceCommand/Base/SerialPortConfig.cs
Normal file
27
DeviceCommand/Base/SerialPortConfig.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.IO.Ports;
|
||||
|
||||
namespace DeviceCommand.Base
|
||||
{
|
||||
/// <summary>
|
||||
/// 串口通信参数(DeviceCommand 内部纯数据类,供设备类构造函数使用)。
|
||||
/// 与 UIShare.UIViewModel.SerialPortConfigVM 字段一一对应,
|
||||
/// StopBits / Parity 在此处使用 System.IO.Ports 强类型枚举,
|
||||
/// 由 DeviceManager 从字符串解析后填入。
|
||||
/// </summary>
|
||||
public class SerialPortConfig
|
||||
{
|
||||
public string PortName { get; set; } = "COM1";
|
||||
|
||||
public int BaudRate { get; set; } = 9600;
|
||||
|
||||
public int DataBits { get; set; } = 8;
|
||||
|
||||
public StopBits StopBits { get; set; } = StopBits.One;
|
||||
|
||||
public Parity Parity { get; set; } = Parity.None;
|
||||
|
||||
public int ReadTimeout { get; set; } = 3000;
|
||||
|
||||
public int WriteTimeout { get; set; } = 3000;
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,15 @@ namespace DeviceCommand.Base
|
||||
_serialPort = new SerialPort();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过 <see cref="SerialPortConfig"/> 一次性配置串口通信参数。
|
||||
/// </summary>
|
||||
public Serial_Port(SerialPortConfig config) : this()
|
||||
{
|
||||
if (config == null) return;
|
||||
ConfigureDevice(config.PortName, config.BaudRate, config.DataBits, config.StopBits, config.Parity, config.ReadTimeout, config.WriteTimeout);
|
||||
}
|
||||
|
||||
public void ConfigureDevice(string portName, int baudRate, int dataBits = 8, StopBits stopBits = StopBits.One, Parity parity = Parity.None, int readTimeout = 3000, int writeTimeout = 3000)
|
||||
{
|
||||
PortName = portName;
|
||||
|
||||
@@ -23,6 +23,15 @@ namespace DeviceCommand.Base
|
||||
_tcpClient = new TcpClient();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过 <see cref="TcpConfig"/> 一次性配置 TCP 通信参数。
|
||||
/// </summary>
|
||||
public Tcp(TcpConfig config) : this()
|
||||
{
|
||||
if (config == null) return;
|
||||
ConfigureDevice(config.IPAddress, config.Port, config.SendTimeout, config.ReceiveTimeout);
|
||||
}
|
||||
|
||||
public void ConfigureDevice(string ipAddress, int port, int sendTimeout = 3000, int receiveTimeout = 3000)
|
||||
{
|
||||
IPAddress = ipAddress;
|
||||
|
||||
17
DeviceCommand/Base/TcpConfig.cs
Normal file
17
DeviceCommand/Base/TcpConfig.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace DeviceCommand.Base
|
||||
{
|
||||
/// <summary>
|
||||
/// TCP 通信参数(DeviceCommand 内部纯数据类,供设备类构造函数使用)。
|
||||
/// 与 UIShare.UIViewModel.TcpConfigVM 字段一一对应,由 DeviceManager 在实例化时填充。
|
||||
/// </summary>
|
||||
public class TcpConfig
|
||||
{
|
||||
public string IPAddress { get; set; } = "127.0.0.1";
|
||||
|
||||
public int Port { get; set; } = 502;
|
||||
|
||||
public int SendTimeout { get; set; } = 3000;
|
||||
|
||||
public int ReceiveTimeout { get; set; } = 3000;
|
||||
}
|
||||
}
|
||||
@@ -14,11 +14,10 @@ namespace DeviceCommand.Device
|
||||
private const string ScpiDelimiter = "\n";
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数:初始化 IT7800E 交直流电源通信参数
|
||||
/// 构造函数:传入 <see cref="TcpConfig"/> 一次性初始化 IT7800E 交直流电源通信参数。
|
||||
/// </summary>
|
||||
public IT7800E(string ipAddress, int port, int sendTimeout, int receiveTimeout)
|
||||
public IT7800E(TcpConfig config) : base(config)
|
||||
{
|
||||
ConfigureDevice(ipAddress, port, sendTimeout, receiveTimeout);
|
||||
}
|
||||
|
||||
#region 1. IEEE 488.2 公共命令
|
||||
|
||||
@@ -14,11 +14,10 @@ namespace DeviceCommand.Device
|
||||
private const string ScpiDelimiter = "\n";
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数:初始化 N36200/N36300 设备通信参数
|
||||
/// 构造函数:传入 <see cref="TcpConfig"/> 一次性初始化 N36200/N36300 设备通信参数。
|
||||
/// </summary>
|
||||
public N36200(string ipAddress, int port, int sendTimeout, int receiveTimeout)
|
||||
public N36200(TcpConfig config) : base(config)
|
||||
{
|
||||
ConfigureDevice(ipAddress, port, sendTimeout, receiveTimeout);
|
||||
}
|
||||
|
||||
#region 3.1. IEEE 488.2 公共命令
|
||||
|
||||
@@ -22,9 +22,8 @@ namespace DeviceCommand.Device
|
||||
// 手册第 4 页明确规定:每条命令后面都要加结束符 0x0A (\n)
|
||||
private const string SCPIDelimiter = "\n";
|
||||
|
||||
public N36600(string ipAddress, int port, int sendTimeout, int receiveTimeout)
|
||||
public N36600(TcpConfig config) : base(config)
|
||||
{
|
||||
ConfigureDevice(ipAddress, port, sendTimeout, receiveTimeout);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -14,11 +14,10 @@ namespace DeviceCommand.Device
|
||||
private const string ScpiDelimiter = "\n";
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数:初始化 N69200 电子负载通信参数
|
||||
/// 构造函数:传入 <see cref="TcpConfig"/> 一次性初始化 N69200 电子负载通信参数。
|
||||
/// </summary>
|
||||
public N69200(string ipAddress, int port, int sendTimeout, int receiveTimeout)
|
||||
public N69200(TcpConfig config) : base(config)
|
||||
{
|
||||
ConfigureDevice(ipAddress, port, sendTimeout, receiveTimeout);
|
||||
}
|
||||
|
||||
#region 3.1. IEEE 488.2 公共命令
|
||||
|
||||
@@ -14,11 +14,11 @@ namespace DeviceCommand.Device
|
||||
private const string ScpiDelimiter = "\n";
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数:初始化示波器通信参数 (鼎阳示波器网口 Socket 默认端口通常为 5025)
|
||||
/// 构造函数:传入 <see cref="TcpConfig"/> 一次性初始化示波器通信参数。
|
||||
/// 鼎阳示波器网口 Socket 默认端口通常为 5025,请在配置中设置。
|
||||
/// </summary>
|
||||
public SDS2000X_HD(string ipAddress, int port = 5025, int sendTimeout = 3000, int receiveTimeout = 3000)
|
||||
public SDS2000X_HD(TcpConfig config) : base(config)
|
||||
{
|
||||
ConfigureDevice(ipAddress, port, sendTimeout, receiveTimeout);
|
||||
}
|
||||
|
||||
#region 1. IEEE 488.2 公共命令
|
||||
|
||||
@@ -14,11 +14,10 @@ namespace DeviceCommand.Device
|
||||
private const string ScpiDelimiter = "\n";
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数:初始化 SPAW7000 功率分析记录仪通信参数
|
||||
/// 构造函数:传入 <see cref="TcpConfig"/> 一次性初始化 SPAW7000 功率分析记录仪通信参数。
|
||||
/// </summary>
|
||||
public SPAW7000(string ipAddress, int port, int sendTimeout, int receiveTimeout)
|
||||
public SPAW7000(TcpConfig config) : base(config)
|
||||
{
|
||||
ConfigureDevice(ipAddress, port, sendTimeout, receiveTimeout);
|
||||
}
|
||||
|
||||
#region 1. IEEE 488.2 公共命令
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace MainModule.ViewModels
|
||||
public ICommand RefreshCommand { get; set; }
|
||||
public ICommand BackToProtocolCommand { get; set; }
|
||||
|
||||
public AutomatedTestingViewModel(IContainerProvider container) : base(container)
|
||||
public AutomatedTestingViewModel(IContainerExtension container) : base(container)
|
||||
{
|
||||
// 每个 AutomatedTestingViewModel 实例创建独立的容器作用域
|
||||
_scope = container.CreateScope();
|
||||
@@ -55,7 +55,7 @@ namespace MainModule.ViewModels
|
||||
// 在该作用域内解析 ScopedContext —— 当前作用域唯一
|
||||
_scopedContext = _scope.Resolve<ScopedContext>();
|
||||
_stepRunning = _scope.Resolve<StepRunning>();
|
||||
_systemConfig = _scope.Resolve<SystemConfig>();;
|
||||
_systemConfig = _scope.Resolve<SystemConfig>();
|
||||
// 关键:从同一个 _scope 解析 5 个子 VM,DI 会把同一个 ScopedContext 注入它们
|
||||
CommandTreeVM = _scope.Resolve<CommandTreeViewModel>();
|
||||
StepsManagerVM = _scope.Resolve<StepsManagerViewModel>();
|
||||
@@ -64,7 +64,6 @@ namespace MainModule.ViewModels
|
||||
ParametersManagerVM = _scope.Resolve<ParametersManagerViewModel>();
|
||||
RefreshCommand = new DelegateCommand(OnRefresh);
|
||||
BackToProtocolCommand = new DelegateCommand(OnBackToProtocol);
|
||||
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
@@ -1,45 +1,140 @@
|
||||
using DeviceCommand.Base;
|
||||
using DeviceCommand.Device;
|
||||
using Logger;
|
||||
using Prism.Ioc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Reflection;
|
||||
using UIShare.UIViewModel;
|
||||
|
||||
namespace UIShare.GlobalVariable
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备管理器:根据 <see cref="SystemConfig.DeviceList"/> 反射实例化所有启用的设备,
|
||||
/// 通过 <see cref="IBaseInterface"/> 多态统一管理,避免为每种设备单独硬编码字段。
|
||||
/// </summary>
|
||||
public class DeviceManager
|
||||
{
|
||||
public SystemConfig _systemConfig { get; set; }
|
||||
public IContainerProvider _containerProvider { get; set; }
|
||||
public IT7800E _iT7800E { get; set; }
|
||||
public N36200 _n36200 { get; set; }
|
||||
public N36600 _n36600 { get; set; }
|
||||
public N69200 _n69200 { get; set; }
|
||||
public SDS2000X_HD _sDS2000X_HD { get; set; }
|
||||
public SPAW7000 _sPAW7000 { get; set; }
|
||||
public IList<IBaseInterface> DeviceList { get; set; }
|
||||
public DeviceManager(IContainerProvider containerProvider,SystemConfig systemConfig)
|
||||
public IList<IBaseInterface> DeviceList { get; private set; } = new List<IBaseInterface>();
|
||||
|
||||
/// <summary>按 DeviceName 索引的设备字典,便于业务层按名取实例。</summary>
|
||||
public IDictionary<string, IBaseInterface> DeviceMap { get; private set; }
|
||||
= new Dictionary<string, IBaseInterface>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>类名 → Type 的反射缓存(仅扫描一次)。</summary>
|
||||
private static readonly IReadOnlyDictionary<string, Type> _deviceTypeMap = BuildDeviceTypeMap();
|
||||
|
||||
public DeviceManager(SystemConfig systemConfig)
|
||||
{
|
||||
_containerProvider = containerProvider;
|
||||
_systemConfig = systemConfig;
|
||||
InitDevices();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扫描 <see cref="IBaseInterface"/> 所在程序集中所有可实例化的实现类,
|
||||
/// 以类型名为键建立映射。这样新增设备类无需修改 DeviceManager。
|
||||
/// </summary>
|
||||
private static IReadOnlyDictionary<string, Type> BuildDeviceTypeMap()
|
||||
{
|
||||
try
|
||||
{
|
||||
return typeof(IBaseInterface).Assembly
|
||||
.GetTypes()
|
||||
.Where(t => t.IsClass && !t.IsAbstract && typeof(IBaseInterface).IsAssignableFrom(t))
|
||||
.ToDictionary(t => t.Name, t => t, StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
catch (ReflectionTypeLoadException ex)
|
||||
{
|
||||
LoggerHelper.Error($"扫描设备类型失败:{ex.Message}");
|
||||
return new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitDevices()
|
||||
{
|
||||
foreach(var Config in _systemConfig.DeviceList)
|
||||
DeviceMap = new Dictionary<string, IBaseInterface>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
if (_systemConfig?.DeviceList == null) return;
|
||||
|
||||
foreach (var config in _systemConfig.DeviceList)
|
||||
{
|
||||
if (Config.ConnectionType == "Tcp")
|
||||
{
|
||||
if (config == null || !config.IsEnabled) continue;
|
||||
|
||||
}
|
||||
else if (Config.ConnectionType == "Serial")
|
||||
if (string.IsNullOrWhiteSpace(config.DeviceType) ||
|
||||
!_deviceTypeMap.TryGetValue(config.DeviceType, out var deviceType))
|
||||
{
|
||||
LoggerHelper.Warn($"未识别的设备类型 [{config.DeviceType}],已跳过 [{config.DeviceName}]。");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
IBaseInterface? instance = config.ConnectionType switch
|
||||
{
|
||||
"Tcp" => CreateTcpDevice(deviceType, config.TcpConfig),
|
||||
"Serial" => CreateSerialDevice(deviceType, config.SerialPortConfig),
|
||||
_ => null
|
||||
};
|
||||
|
||||
if (instance == null)
|
||||
{
|
||||
LoggerHelper.Warn($"设备 [{config.DeviceName}] 连接方式 [{config.ConnectionType}] 不支持,已跳过。");
|
||||
continue;
|
||||
}
|
||||
|
||||
DeviceList.Add(instance);
|
||||
if (!string.IsNullOrWhiteSpace(config.DeviceName))
|
||||
{
|
||||
DeviceMap[config.DeviceName] = instance;
|
||||
}
|
||||
|
||||
LoggerHelper.Info($"已加载设备 [{config.DeviceName} / {config.DeviceType} / {config.ConnectionType}]");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var inner = ex.InnerException?.Message ?? ex.Message;
|
||||
LoggerHelper.ErrorWithNotify($"设备 [{config.DeviceName}] 实例化失败:{inner}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实例化 TCP 类设备:将 <see cref="TcpConfigVM"/> 转为 <see cref="TcpConfig"/> POCO,
|
||||
/// 调用设备类的 (TcpConfig) 构造函数。
|
||||
/// </summary>
|
||||
private static IBaseInterface? CreateTcpDevice(Type type, TcpConfigVM? vm)
|
||||
{
|
||||
vm ??= new TcpConfigVM();
|
||||
var cfg = new TcpConfig
|
||||
{
|
||||
IPAddress = vm.IPAddress,
|
||||
Port = vm.Port,
|
||||
SendTimeout = vm.SendTimeout,
|
||||
ReceiveTimeout = vm.ReceiveTimeout
|
||||
};
|
||||
return Activator.CreateInstance(type, cfg) as IBaseInterface;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实例化串口类设备:将 <see cref="SerialPortConfigVM"/> 转为 <see cref="SerialPortConfig"/> POCO,
|
||||
/// StopBits / Parity 从字符串解析为枚举,调用设备类的 (SerialPortConfig) 构造函数。
|
||||
/// </summary>
|
||||
private static IBaseInterface? CreateSerialDevice(Type type, SerialPortConfigVM? vm)
|
||||
{
|
||||
vm ??= new SerialPortConfigVM();
|
||||
var cfg = new SerialPortConfig
|
||||
{
|
||||
PortName = vm.PortName,
|
||||
BaudRate = vm.BaudRate,
|
||||
DataBits = vm.DataBits,
|
||||
StopBits = Enum.TryParse<StopBits>(vm.StopBits, true, out var sb) ? sb : StopBits.One,
|
||||
Parity = Enum.TryParse<Parity>(vm.Parity, true, out var pa) ? pa : Parity.None,
|
||||
ReadTimeout = vm.ReadTimeout,
|
||||
WriteTimeout = vm.WriteTimeout
|
||||
};
|
||||
return Activator.CreateInstance(type, cfg) as IBaseInterface;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user