添加项目文件。
This commit is contained in:
153
DeviceEditModule/ViewModels/DialogMangerViewModel.cs
Normal file
153
DeviceEditModule/ViewModels/DialogMangerViewModel.cs
Normal file
@@ -0,0 +1,153 @@
|
||||
using Prism.Commands;
|
||||
using Prism.Ioc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows.Input;
|
||||
using UIShare.PubEvent;
|
||||
using UIShare.UIViewModel;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
namespace DeviceEditModule.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// 弹窗管理器 ViewModel。
|
||||
/// <para>
|
||||
/// 职责:<br/>
|
||||
/// 1. 订阅 <see cref="AddDialogTabEvent"/>,将外部弹窗注册为 Tab;<br/>
|
||||
/// 2. 维护 <see cref="TagItems"/> 集合,控制选中/关闭逻辑;<br/>
|
||||
/// 3. 最小化:通过 <see cref="MinimizeRequested"/> 事件通知 View 执行 P/Invoke 窗口操作;<br/>
|
||||
/// 4. 关闭:调用 <see cref="RequestClose"/>。
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public class DialogMangerViewModel : DialogViewModelBase, IDisposable
|
||||
{
|
||||
#region 属性
|
||||
|
||||
/// <summary>所有 Tab 项集合,绑定到标签条的 ItemsControl。</summary>
|
||||
public ObservableCollection<DialogTabItemVM> TagItems { get; } = new();
|
||||
|
||||
/// <summary>以设备指纹为 key 追踪已打开的 Tab,防止同一物理设备重复打开。</summary>
|
||||
private readonly Dictionary<string, DialogTabItemVM> _tabDictionary = new();
|
||||
|
||||
private DialogTabItemVM? _selectedTag;
|
||||
/// <summary>当前激活的 Tab,其 Content 显示在内容区。</summary>
|
||||
public DialogTabItemVM? SelectedTag
|
||||
{
|
||||
get => _selectedTag;
|
||||
set => SetProperty(ref _selectedTag, value);
|
||||
}
|
||||
|
||||
/// <summary>是否没有任何 Tab(用于绑定空状态提示的 Visibility)。</summary>
|
||||
public bool HasNoTabs => TagItems.Count == 0;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
|
||||
public ICommand MinimizeCommand { get; }
|
||||
public ICommand CloseCommand { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region 事件
|
||||
|
||||
/// <summary>
|
||||
/// View 订阅此事件后,在事件回调里执行 P/Invoke 最小化。
|
||||
/// 这样 ViewModel 无需引用任何 UI 或 Win32 类型。
|
||||
/// </summary>
|
||||
public event EventHandler? MinimizeRequested;
|
||||
public event EventHandler? RestoreRequested;
|
||||
#endregion
|
||||
|
||||
public DialogMangerViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
MinimizeCommand = new DelegateCommand(OnMinimize);
|
||||
CloseCommand = new DelegateCommand(OnClose);
|
||||
|
||||
// 订阅来自其他模块的"添加 Tab"事件
|
||||
_eventAggregator.GetEvent<AddDialogTabEvent>().Subscribe(OnAddTab, ThreadOption.UIThread);
|
||||
_eventAggregator.GetEvent<CancelMinimizeEvent>().Subscribe(()=>RestoreRequested.Invoke(this, EventArgs.Empty));
|
||||
}
|
||||
|
||||
#region 命令处理
|
||||
|
||||
private void OnMinimize() => MinimizeRequested?.Invoke(this, EventArgs.Empty);
|
||||
|
||||
private void OnClose()
|
||||
{
|
||||
// 关闭前清空所有 Tab,释放内容引用,并清除去重字典
|
||||
_tabDictionary.Clear();
|
||||
TagItems.Clear();
|
||||
SelectedTag = null;
|
||||
RequestClose.Invoke();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Tab 管理
|
||||
|
||||
/// <summary>收到事件:若指纹已存在则激活,否则创建并追加到集合。</summary>
|
||||
private void OnAddTab(DialogTabInfo info)
|
||||
{
|
||||
// 已有相同指纹的 Tab → 直接激活,不重复创建
|
||||
if (!string.IsNullOrEmpty(info.Fingerprint) && _tabDictionary.TryGetValue(info.Fingerprint, out var existing))
|
||||
{
|
||||
ActivateTab(existing);
|
||||
return;
|
||||
}
|
||||
|
||||
var tab = new DialogTabItemVM(OnSelectTab, OnCloseTab)
|
||||
{
|
||||
Title = info.Title,
|
||||
Fingerprint = info.Fingerprint,
|
||||
Content = info.Content
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(info.Fingerprint))
|
||||
_tabDictionary[info.Fingerprint] = tab;
|
||||
|
||||
TagItems.Add(tab);
|
||||
RaisePropertyChanged(nameof(HasNoTabs));
|
||||
ActivateTab(tab);
|
||||
}
|
||||
|
||||
/// <summary>激活(选中)指定 Tab,其余全部取消选中。</summary>
|
||||
private void OnSelectTab(DialogTabItemVM tab) => ActivateTab(tab);
|
||||
|
||||
/// <summary>关闭指定 Tab,自动选中相邻 Tab(或清空内容区)。</summary>
|
||||
private void OnCloseTab(DialogTabItemVM tab)
|
||||
{
|
||||
int idx = TagItems.IndexOf(tab);
|
||||
if (!string.IsNullOrEmpty(tab.Fingerprint))
|
||||
_tabDictionary.Remove(tab.Fingerprint);
|
||||
TagItems.Remove(tab);
|
||||
RaisePropertyChanged(nameof(HasNoTabs));
|
||||
|
||||
if (TagItems.Count == 0)
|
||||
{
|
||||
SelectedTag = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// 优先选左侧邻 Tab,无左侧则选当前索引(已向左移一位)
|
||||
ActivateTab(TagItems[Math.Max(0, idx - 1)]);
|
||||
}
|
||||
|
||||
private void ActivateTab(DialogTabItemVM tab)
|
||||
{
|
||||
foreach (var t in TagItems)
|
||||
t.IsSelected = false;
|
||||
|
||||
tab.IsSelected = true;
|
||||
SelectedTag = tab;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_eventAggregator.GetEvent<AddDialogTabEvent>().Unsubscribe(OnAddTab);
|
||||
}
|
||||
}
|
||||
}
|
||||
310
DeviceEditModule/ViewModels/IT7800EViewModel.cs
Normal file
310
DeviceEditModule/ViewModels/IT7800EViewModel.cs
Normal file
@@ -0,0 +1,310 @@
|
||||
using DeviceCommand.Devices;
|
||||
using Prism.Commands;
|
||||
using Prism.Ioc;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Windows.Input;
|
||||
using UIShare.GlobalVariable;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
namespace DeviceEditModule.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// IT7800E 交直流电源控制面板 ViewModel。
|
||||
/// <para>
|
||||
/// 注册为 Navigation View,既可由 Region 导航进入,
|
||||
/// 也可由外部直接实例化后作为 Tab 内容塞入 DialogMangerView:
|
||||
/// <code>
|
||||
/// var view = container.Resolve<IT7800EView>();
|
||||
/// (view.DataContext as IT7800EViewModel)?.Initialize("IT7800E");
|
||||
/// _eventAggregator.GetEvent<AddDialogTabEvent>().Publish(
|
||||
/// new DialogTabInfo { Title = "IT7800E", Content = view });
|
||||
/// </code>
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public class IT7800EViewModel : NavigateViewModelBase, IDisposable
|
||||
{
|
||||
#region 私有字段
|
||||
|
||||
private readonly DeviceManager _deviceManager;
|
||||
private IT7800E? _device;
|
||||
private CancellationTokenSource? _cts;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 设备信息属性
|
||||
|
||||
private string _deviceName = "IT7800E";
|
||||
public string DeviceName
|
||||
{
|
||||
get => _deviceName;
|
||||
set => SetProperty(ref _deviceName, value);
|
||||
}
|
||||
|
||||
private bool _isConnected;
|
||||
public bool IsConnected
|
||||
{
|
||||
get => _isConnected;
|
||||
set => SetProperty(ref _isConnected, value);
|
||||
}
|
||||
|
||||
private bool _isBusy;
|
||||
/// <summary>正在执行设备命令时为 true,用于 UI 忙碌状态指示。</summary>
|
||||
public bool IsBusy
|
||||
{
|
||||
get => _isBusy;
|
||||
set => SetProperty(ref _isBusy, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 输入参数属性
|
||||
|
||||
private double _acVoltage = 220.0;
|
||||
/// <summary>待设置的交流电压值(V)。</summary>
|
||||
public double AcVoltage
|
||||
{
|
||||
get => _acVoltage;
|
||||
set => SetProperty(ref _acVoltage, value);
|
||||
}
|
||||
|
||||
private double _dcVoltage = 0.0;
|
||||
/// <summary>待设置的直流偏置电压值(V)。</summary>
|
||||
public double DcVoltage
|
||||
{
|
||||
get => _dcVoltage;
|
||||
set => SetProperty(ref _dcVoltage, value);
|
||||
}
|
||||
|
||||
private double _frequency = 50.0;
|
||||
/// <summary>待设置的交流频率(Hz)。</summary>
|
||||
public double Frequency
|
||||
{
|
||||
get => _frequency;
|
||||
set => SetProperty(ref _frequency, value);
|
||||
}
|
||||
|
||||
private double _currentLimit = 10.0;
|
||||
/// <summary>待设置的限流值(A)。</summary>
|
||||
public double CurrentLimit
|
||||
{
|
||||
get => _currentLimit;
|
||||
set => SetProperty(ref _currentLimit, value);
|
||||
}
|
||||
|
||||
private PowerCouplingMode _selectedMode = PowerCouplingMode.AC;
|
||||
/// <summary>待设置的电源工作模式:AC / DC / ACDC。</summary>
|
||||
public PowerCouplingMode SelectedMode
|
||||
{
|
||||
get => _selectedMode;
|
||||
set => SetProperty(ref _selectedMode, value);
|
||||
}
|
||||
|
||||
private double _ovpValue = 260.0;
|
||||
/// <summary>过压保护值(V)。</summary>
|
||||
public double OvpValue
|
||||
{
|
||||
get => _ovpValue;
|
||||
set => SetProperty(ref _ovpValue, value);
|
||||
}
|
||||
|
||||
private double _ocpValue = 15.0;
|
||||
/// <summary>过流保护值(A)。</summary>
|
||||
public double OcpValue
|
||||
{
|
||||
get => _ocpValue;
|
||||
set => SetProperty(ref _ocpValue, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 测量结果属性
|
||||
|
||||
private string _measuredVoltage = "—";
|
||||
public string MeasuredVoltage
|
||||
{
|
||||
get => _measuredVoltage;
|
||||
set => SetProperty(ref _measuredVoltage, value);
|
||||
}
|
||||
|
||||
private string _measuredCurrent = "—";
|
||||
public string MeasuredCurrent
|
||||
{
|
||||
get => _measuredCurrent;
|
||||
set => SetProperty(ref _measuredCurrent, value);
|
||||
}
|
||||
|
||||
private string _measuredPower = "—";
|
||||
public string MeasuredPower
|
||||
{
|
||||
get => _measuredPower;
|
||||
set => SetProperty(ref _measuredPower, value);
|
||||
}
|
||||
|
||||
private string _measuredFrequency = "—";
|
||||
public string MeasuredFrequency
|
||||
{
|
||||
get => _measuredFrequency;
|
||||
set => SetProperty(ref _measuredFrequency, value);
|
||||
}
|
||||
|
||||
private string _responseLog = string.Empty;
|
||||
/// <summary>命令响应日志(最新消息在顶部)。</summary>
|
||||
public string ResponseLog
|
||||
{
|
||||
get => _responseLog;
|
||||
set => SetProperty(ref _responseLog, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
|
||||
public ICommand QueryIdentityCommand { get; }
|
||||
public ICommand ResetDeviceCommand { get; }
|
||||
public ICommand OutputOnCommand { get; }
|
||||
public ICommand OutputOffCommand { get; }
|
||||
public ICommand SetModeCommand { get; }
|
||||
public ICommand SetAcVoltageCommand { get; }
|
||||
public ICommand SetDcVoltageCommand { get; }
|
||||
public ICommand SetFrequencyCommand { get; }
|
||||
public ICommand SetCurrentCommand { get; }
|
||||
public ICommand QueryAllMeasureCommand { get; }
|
||||
public ICommand SetRemoteModeCommand { get; }
|
||||
public ICommand SetLocalModeCommand { get; }
|
||||
public ICommand SetOvpCommand { get; }
|
||||
public ICommand SetOcpCommand { get; }
|
||||
public ICommand ClearAlarmCommand { get; }
|
||||
public ICommand ClearErrorCommand { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
public IT7800EViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
_deviceManager = containerProvider.Resolve<DeviceManager>();
|
||||
|
||||
QueryIdentityCommand = new DelegateCommand(async () => await Exec(async () => AppendLog("IDN: " + await _device!.查询设备标识(Ct()))));
|
||||
ResetDeviceCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.重置设备(Ct()); AppendLog("设备已重置"); }));
|
||||
OutputOnCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置DC输出(true, Ct()); AppendLog("输出已开启"); }));
|
||||
OutputOffCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置DC输出(false, Ct()); AppendLog("输出已关闭"); }));
|
||||
SetModeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置电源模式(SelectedMode, Ct()); AppendLog($"模式已设为 {SelectedMode}"); }));
|
||||
SetAcVoltageCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置交流电压(AcVoltage, Ct()); AppendLog($"AC电压已设为 {AcVoltage} V"); }));
|
||||
SetDcVoltageCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置直流电压(DcVoltage, Ct()); AppendLog($"DC偏置已设为 {DcVoltage} V"); }));
|
||||
SetFrequencyCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置频率(Frequency, Ct()); AppendLog($"频率已设为 {Frequency} Hz"); }));
|
||||
SetCurrentCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置电流(CurrentLimit, Ct()); AppendLog($"限流已设为 {CurrentLimit} A"); }));
|
||||
SetOvpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置过压保护_OVP(OvpValue, Ct()); AppendLog($"OVP已设为 {OvpValue} V"); }));
|
||||
SetOcpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置过流保护_OCP(OcpValue, Ct()); AppendLog($"OCP已设为 {OcpValue} A"); }));
|
||||
SetRemoteModeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.切换远程控制模式(Ct()); AppendLog("已切换到远程控制模式"); }));
|
||||
SetLocalModeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.切换本地控制模式(Ct()); AppendLog("已切换到本地控制模式"); }));
|
||||
ClearAlarmCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.清除保护告警(Ct()); AppendLog("保护告警已清除"); }));
|
||||
ClearErrorCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.清除错误队列和状态字节(Ct()); AppendLog("错误队列已清除"); }));
|
||||
|
||||
QueryAllMeasureCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
MeasuredVoltage = await _device!.查询实际电压(Ct());
|
||||
MeasuredCurrent = await _device!.查询实际电流(Ct());
|
||||
MeasuredPower = await _device!.查询实际功率(Ct());
|
||||
MeasuredFrequency = await _device!.查询实际频率(Ct());
|
||||
AppendLog($"测量 → 电压:{MeasuredVoltage}V 电流:{MeasuredCurrent}A 功率:{MeasuredPower}W 频率:{MeasuredFrequency}Hz");
|
||||
}));
|
||||
|
||||
Initialize();
|
||||
}
|
||||
|
||||
#region 初始化 / Navigation
|
||||
|
||||
/// <summary>
|
||||
/// 从 DeviceManager 中查找 IT7800E 设备实例。
|
||||
/// 优先按 <paramref name="deviceName"/> 查找,否则取第一个匹配类型的设备。
|
||||
/// </summary>
|
||||
public void Initialize(string? deviceName = null)
|
||||
{
|
||||
IT7800E? found = null;
|
||||
string? foundName = null;
|
||||
|
||||
if (deviceName != null &&
|
||||
_deviceManager.DeviceMap.TryGetValue(deviceName, out var d) &&
|
||||
d is IT7800E e)
|
||||
{
|
||||
found = e;
|
||||
foundName = deviceName;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var kv in _deviceManager.DeviceMap)
|
||||
{
|
||||
if (kv.Value is IT7800E it)
|
||||
{
|
||||
found = it;
|
||||
foundName = kv.Key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_device = found;
|
||||
DeviceName = foundName ?? "IT7800E (未找到)";
|
||||
IsConnected = _device?.IsConnected ?? false;
|
||||
|
||||
AppendLog(found != null
|
||||
? $"已关联设备 [{DeviceName}],连接状态:{(IsConnected ? "已连接" : "未连接")}"
|
||||
: "未在 DeviceManager 中找到 IT7800E 设备,请先初始化设备配置。");
|
||||
}
|
||||
|
||||
public override void OnNavigatedTo(NavigationContext context)
|
||||
{
|
||||
var name = context.Parameters.GetValue<string?>("DeviceName");
|
||||
Initialize(name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 辅助
|
||||
|
||||
private CancellationToken Ct() => (_cts = new CancellationTokenSource(TimeSpan.FromSeconds(10))).Token;
|
||||
|
||||
private async Task Exec(Func<Task> action)
|
||||
{
|
||||
if (_device == null)
|
||||
{
|
||||
AppendLog("错误:未关联到设备实例,请检查设备配置。");
|
||||
return;
|
||||
}
|
||||
if (IsBusy) return;
|
||||
IsBusy = true;
|
||||
try
|
||||
{
|
||||
await action();
|
||||
IsConnected = _device.IsConnected;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
AppendLog("命令超时或已取消。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AppendLog($"错误:{ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void AppendLog(string message)
|
||||
{
|
||||
var line = $"[{DateTime.Now:HH:mm:ss}] {message}";
|
||||
ResponseLog = ResponseLog.Length > 4000
|
||||
? line + "\n" + ResponseLog[..3000]
|
||||
: line + "\n" + ResponseLog;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_cts?.Cancel();
|
||||
_cts?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
309
DeviceEditModule/ViewModels/N36200ViewModel.cs
Normal file
309
DeviceEditModule/ViewModels/N36200ViewModel.cs
Normal file
@@ -0,0 +1,309 @@
|
||||
using DeviceCommand.Devices;
|
||||
using Prism.Commands;
|
||||
using Prism.Ioc;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Windows.Input;
|
||||
using UIShare.GlobalVariable;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
namespace DeviceEditModule.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// N36200 宽范围可编程直流电源控制面板 ViewModel。
|
||||
/// <para>
|
||||
/// 注册为 Navigation View,可通过 EventAggregator 添加为 DialogMangerView 中的 Tab:
|
||||
/// <code>
|
||||
/// var view = container.Resolve<N36200View>();
|
||||
/// (view.DataContext as N36200ViewModel)?.Initialize("N36200");
|
||||
/// _eventAggregator.GetEvent<AddDialogTabEvent>().Publish(
|
||||
/// new DialogTabInfo { Title = "N36200", Content = view });
|
||||
/// </code>
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public class N36200ViewModel : NavigateViewModelBase, IDisposable
|
||||
{
|
||||
#region 私有字段
|
||||
|
||||
private readonly DeviceManager _deviceManager;
|
||||
private N36200? _device;
|
||||
private CancellationTokenSource? _cts;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 设备信息属性
|
||||
|
||||
private string _deviceName = "N36200";
|
||||
public string DeviceName
|
||||
{
|
||||
get => _deviceName;
|
||||
set => SetProperty(ref _deviceName, value);
|
||||
}
|
||||
|
||||
private bool _isConnected;
|
||||
public bool IsConnected
|
||||
{
|
||||
get => _isConnected;
|
||||
set => SetProperty(ref _isConnected, value);
|
||||
}
|
||||
|
||||
private bool _isBusy;
|
||||
public bool IsBusy
|
||||
{
|
||||
get => _isBusy;
|
||||
set => SetProperty(ref _isBusy, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 输入参数属性
|
||||
|
||||
private double _voltage = 12.0;
|
||||
/// <summary>待设置的输出电压值(V)。</summary>
|
||||
public double Voltage
|
||||
{
|
||||
get => _voltage;
|
||||
set => SetProperty(ref _voltage, value);
|
||||
}
|
||||
|
||||
private double _currentLimit = 5.0;
|
||||
/// <summary>待设置的限流值(A)。</summary>
|
||||
public double CurrentLimit
|
||||
{
|
||||
get => _currentLimit;
|
||||
set => SetProperty(ref _currentLimit, value);
|
||||
}
|
||||
|
||||
private string _selectedMode = "NORMal";
|
||||
/// <summary>待设置的运行模式:NORMal / CHARge / SEQuence / CPOWer / CARWave / APG。</summary>
|
||||
public string SelectedMode
|
||||
{
|
||||
get => _selectedMode;
|
||||
set => SetProperty(ref _selectedMode, value);
|
||||
}
|
||||
|
||||
private double _ovpValue = 15.0;
|
||||
/// <summary>过压保护值(V)。</summary>
|
||||
public double OvpValue
|
||||
{
|
||||
get => _ovpValue;
|
||||
set => SetProperty(ref _ovpValue, value);
|
||||
}
|
||||
|
||||
private double _ocpValue = 6.0;
|
||||
/// <summary>过流保护值(A)。</summary>
|
||||
public double OcpValue
|
||||
{
|
||||
get => _ocpValue;
|
||||
set => SetProperty(ref _ocpValue, value);
|
||||
}
|
||||
|
||||
private double _uvpValue = 0.0;
|
||||
/// <summary>欠压保护值(V)。</summary>
|
||||
public double UvpValue
|
||||
{
|
||||
get => _uvpValue;
|
||||
set => SetProperty(ref _uvpValue, value);
|
||||
}
|
||||
|
||||
private double _oppValue = 100.0;
|
||||
/// <summary>过功率保护值(W)。</summary>
|
||||
public double OppValue
|
||||
{
|
||||
get => _oppValue;
|
||||
set => SetProperty(ref _oppValue, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 测量结果属性
|
||||
|
||||
private string _measuredVoltage = "—";
|
||||
public string MeasuredVoltage
|
||||
{
|
||||
get => _measuredVoltage;
|
||||
set => SetProperty(ref _measuredVoltage, value);
|
||||
}
|
||||
|
||||
private string _measuredCurrent = "—";
|
||||
public string MeasuredCurrent
|
||||
{
|
||||
get => _measuredCurrent;
|
||||
set => SetProperty(ref _measuredCurrent, value);
|
||||
}
|
||||
|
||||
private string _measuredPower = "—";
|
||||
public string MeasuredPower
|
||||
{
|
||||
get => _measuredPower;
|
||||
set => SetProperty(ref _measuredPower, value);
|
||||
}
|
||||
|
||||
private string _deviceStatus = "—";
|
||||
/// <summary>设备状态字(OUTPut:STATe? 查询结果)。</summary>
|
||||
public string DeviceStatus
|
||||
{
|
||||
get => _deviceStatus;
|
||||
set => SetProperty(ref _deviceStatus, value);
|
||||
}
|
||||
|
||||
private string _responseLog = string.Empty;
|
||||
/// <summary>命令响应日志(最新消息在顶部)。</summary>
|
||||
public string ResponseLog
|
||||
{
|
||||
get => _responseLog;
|
||||
set => SetProperty(ref _responseLog, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
|
||||
public ICommand QueryIdentityCommand { get; }
|
||||
public ICommand ResetDeviceCommand { get; }
|
||||
public ICommand OutputOnCommand { get; }
|
||||
public ICommand OutputOffCommand { get; }
|
||||
public ICommand SetVoltageCommand { get; }
|
||||
public ICommand SetCurrentCommand { get; }
|
||||
public ICommand SetModeCommand { get; }
|
||||
public ICommand QueryAllMeasureCommand { get; }
|
||||
public ICommand QueryStatusCommand { get; }
|
||||
public ICommand SetOvpCommand { get; }
|
||||
public ICommand SetOcpCommand { get; }
|
||||
public ICommand SetUvpCommand { get; }
|
||||
public ICommand SetOppCommand { get; }
|
||||
public ICommand ClearAlarmCommand { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
public N36200ViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
_deviceManager = containerProvider.Resolve<DeviceManager>();
|
||||
|
||||
QueryIdentityCommand = new DelegateCommand(async () => await Exec(async () => AppendLog("IDN: " + await _device!.查询设备标识(Ct()))));
|
||||
ResetDeviceCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.重置设备(Ct()); AppendLog("设备已重置(耗时约10s)"); }));
|
||||
OutputOnCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置DC输出(true, Ct()); AppendLog("输出已开启"); }));
|
||||
OutputOffCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置DC输出(false, Ct()); AppendLog("输出已关闭"); }));
|
||||
SetVoltageCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置电压(Voltage, Ct()); AppendLog($"电压已设为 {Voltage} V"); }));
|
||||
SetCurrentCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置电流(CurrentLimit, Ct()); AppendLog($"限流已设为 {CurrentLimit} A"); }));
|
||||
SetModeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置运行模式(SelectedMode, Ct()); AppendLog($"模式已设为 {SelectedMode}"); }));
|
||||
SetOvpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置过压保护_OVP(OvpValue, Ct()); AppendLog($"OVP已设为 {OvpValue} V"); }));
|
||||
SetOcpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置过流保护_OCP(OcpValue, Ct()); AppendLog($"OCP已设为 {OcpValue} A"); }));
|
||||
SetUvpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置过欠压保护_UVP(UvpValue, Ct()); AppendLog($"UVP已设为 {UvpValue} V"); }));
|
||||
SetOppCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置过功率保护_OPP(OppValue, Ct()); AppendLog($"OPP已设为 {OppValue} W"); }));
|
||||
ClearAlarmCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.清除告警(Ct()); AppendLog("告警已清除"); }));
|
||||
|
||||
QueryStatusCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
DeviceStatus = await _device!.查询设备状态字(Ct());
|
||||
AppendLog($"状态字: {DeviceStatus}");
|
||||
}));
|
||||
|
||||
QueryAllMeasureCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
MeasuredVoltage = await _device!.查询实际电压(Ct());
|
||||
MeasuredCurrent = await _device!.查询实际电流(Ct());
|
||||
MeasuredPower = await _device!.查询实际功率(Ct());
|
||||
AppendLog($"测量 → 电压:{MeasuredVoltage}V 电流:{MeasuredCurrent}A 功率:{MeasuredPower}W");
|
||||
}));
|
||||
|
||||
Initialize();
|
||||
}
|
||||
|
||||
#region 初始化 / Navigation
|
||||
|
||||
/// <summary>
|
||||
/// 从 DeviceManager 中查找 N36200 设备实例。
|
||||
/// 优先按 <paramref name="deviceName"/> 查找,否则取第一个匹配类型的设备。
|
||||
/// </summary>
|
||||
public void Initialize(string? deviceName = null)
|
||||
{
|
||||
N36200? found = null;
|
||||
string? foundName = null;
|
||||
|
||||
if (deviceName != null &&
|
||||
_deviceManager.DeviceMap.TryGetValue(deviceName, out var d) &&
|
||||
d is N36200 n)
|
||||
{
|
||||
found = n;
|
||||
foundName = deviceName;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var kv in _deviceManager.DeviceMap)
|
||||
{
|
||||
if (kv.Value is N36200 n36)
|
||||
{
|
||||
found = n36;
|
||||
foundName = kv.Key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_device = found;
|
||||
DeviceName = foundName ?? "N36200 (未找到)";
|
||||
IsConnected = _device?.IsConnected ?? false;
|
||||
|
||||
AppendLog(found != null
|
||||
? $"已关联设备 [{DeviceName}],连接状态:{(IsConnected ? "已连接" : "未连接")}"
|
||||
: "未在 DeviceManager 中找到 N36200 设备,请先初始化设备配置。");
|
||||
}
|
||||
|
||||
public override void OnNavigatedTo(NavigationContext context)
|
||||
{
|
||||
var name = context.Parameters.GetValue<string?>("DeviceName");
|
||||
Initialize(name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 辅助
|
||||
|
||||
private CancellationToken Ct() => (_cts = new CancellationTokenSource(TimeSpan.FromSeconds(10))).Token;
|
||||
|
||||
private async Task Exec(Func<Task> action)
|
||||
{
|
||||
if (_device == null)
|
||||
{
|
||||
AppendLog("错误:未关联到设备实例,请检查设备配置。");
|
||||
return;
|
||||
}
|
||||
if (IsBusy) return;
|
||||
IsBusy = true;
|
||||
try
|
||||
{
|
||||
await action();
|
||||
IsConnected = _device.IsConnected;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
AppendLog("命令超时或已取消。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AppendLog($"错误:{ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void AppendLog(string message)
|
||||
{
|
||||
var line = $"[{DateTime.Now:HH:mm:ss}] {message}";
|
||||
ResponseLog = ResponseLog.Length > 4000
|
||||
? line + "\n" + ResponseLog[..3000]
|
||||
: line + "\n" + ResponseLog;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_cts?.Cancel();
|
||||
_cts?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
280
DeviceEditModule/ViewModels/N36600ViewModel.cs
Normal file
280
DeviceEditModule/ViewModels/N36600ViewModel.cs
Normal file
@@ -0,0 +1,280 @@
|
||||
using DeviceCommand.Devices;
|
||||
using Prism.Commands;
|
||||
using Prism.Ioc;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Windows.Input;
|
||||
using UIShare.GlobalVariable;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
namespace DeviceEditModule.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// N36600 便携式宽范围可编程直流电源控制面板 ViewModel。
|
||||
/// </summary>
|
||||
public class N36600ViewModel : NavigateViewModelBase, IDisposable
|
||||
{
|
||||
#region 私有字段
|
||||
|
||||
private readonly DeviceManager _deviceManager;
|
||||
private N36600? _device;
|
||||
private CancellationTokenSource? _cts;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 设备信息属性
|
||||
|
||||
private string _deviceName = "N36600";
|
||||
public string DeviceName
|
||||
{
|
||||
get => _deviceName;
|
||||
set => SetProperty(ref _deviceName, value);
|
||||
}
|
||||
|
||||
private bool _isConnected;
|
||||
public bool IsConnected
|
||||
{
|
||||
get => _isConnected;
|
||||
set => SetProperty(ref _isConnected, value);
|
||||
}
|
||||
|
||||
private bool _isBusy;
|
||||
public bool IsBusy
|
||||
{
|
||||
get => _isBusy;
|
||||
set => SetProperty(ref _isBusy, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 输入参数属性
|
||||
|
||||
private double _voltage = 12.0;
|
||||
/// <summary>待设置的输出电压值(V)。</summary>
|
||||
public double Voltage
|
||||
{
|
||||
get => _voltage;
|
||||
set => SetProperty(ref _voltage, value);
|
||||
}
|
||||
|
||||
private double _currentLimit = 5.0;
|
||||
/// <summary>待设置的输出电流值(A)。</summary>
|
||||
public double CurrentLimit
|
||||
{
|
||||
get => _currentLimit;
|
||||
set => SetProperty(ref _currentLimit, value);
|
||||
}
|
||||
|
||||
private double _power = 60.0;
|
||||
/// <summary>待设置的输出功率值(W)。</summary>
|
||||
public double Power
|
||||
{
|
||||
get => _power;
|
||||
set => SetProperty(ref _power, value);
|
||||
}
|
||||
|
||||
private double _ovpValue = 15.0;
|
||||
/// <summary>过压保护值(V)。</summary>
|
||||
public double OvpValue
|
||||
{
|
||||
get => _ovpValue;
|
||||
set => SetProperty(ref _ovpValue, value);
|
||||
}
|
||||
|
||||
private double _ocpValue = 6.0;
|
||||
/// <summary>过流保护值(A)。</summary>
|
||||
public double OcpValue
|
||||
{
|
||||
get => _ocpValue;
|
||||
set => SetProperty(ref _ocpValue, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 测量结果属性
|
||||
|
||||
private string _measuredVoltage = "—";
|
||||
public string MeasuredVoltage
|
||||
{
|
||||
get => _measuredVoltage;
|
||||
set => SetProperty(ref _measuredVoltage, value);
|
||||
}
|
||||
|
||||
private string _measuredCurrent = "—";
|
||||
public string MeasuredCurrent
|
||||
{
|
||||
get => _measuredCurrent;
|
||||
set => SetProperty(ref _measuredCurrent, value);
|
||||
}
|
||||
|
||||
private string _measuredPower = "—";
|
||||
public string MeasuredPower
|
||||
{
|
||||
get => _measuredPower;
|
||||
set => SetProperty(ref _measuredPower, value);
|
||||
}
|
||||
|
||||
private string _outputState = "—";
|
||||
/// <summary>当前 DC 输出状态(OUTPut? 查询结果)。</summary>
|
||||
public string OutputState
|
||||
{
|
||||
get => _outputState;
|
||||
set => SetProperty(ref _outputState, value);
|
||||
}
|
||||
|
||||
private string _responseLog = string.Empty;
|
||||
/// <summary>命令响应日志(最新消息在顶部)。</summary>
|
||||
public string ResponseLog
|
||||
{
|
||||
get => _responseLog;
|
||||
set => SetProperty(ref _responseLog, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
|
||||
public ICommand QueryIdentityCommand { get; }
|
||||
public ICommand OutputOnCommand { get; }
|
||||
public ICommand OutputOffCommand { get; }
|
||||
public ICommand SetVoltageCommand { get; }
|
||||
public ICommand SetCurrentCommand { get; }
|
||||
public ICommand SetPowerCommand { get; }
|
||||
public ICommand SetOvpCommand { get; }
|
||||
public ICommand SetOcpCommand { get; }
|
||||
public ICommand QueryAllMeasureCommand { get; }
|
||||
public ICommand QueryOutputStateCommand { get; }
|
||||
public ICommand SetRemoteModeCommand { get; }
|
||||
public ICommand SetLocalModeCommand { get; }
|
||||
public ICommand ClearVoltageProtectionCommand { get; }
|
||||
public ICommand ClearCurrentProtectionCommand { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
public N36600ViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
_deviceManager = containerProvider.Resolve<DeviceManager>();
|
||||
|
||||
QueryIdentityCommand = new DelegateCommand(async () => await Exec(async () => AppendLog("IDN: " + await _device!.查询设备标识(Ct()))));
|
||||
OutputOnCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置DC输出(true, Ct()); AppendLog("输出已开启"); }));
|
||||
OutputOffCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置DC输出(false, Ct()); AppendLog("输出已关闭"); }));
|
||||
SetVoltageCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置电压(Voltage, Ct()); AppendLog($"电压已设为 {Voltage} V"); }));
|
||||
SetCurrentCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置电流(CurrentLimit, Ct()); AppendLog($"电流已设为 {CurrentLimit} A"); }));
|
||||
SetPowerCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置功率(Power, Ct()); AppendLog($"功率已设为 {Power} W"); }));
|
||||
SetOvpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置过压保护值(OvpValue, Ct()); AppendLog($"OVP已设为 {OvpValue} V"); }));
|
||||
SetOcpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置过流保护值(OcpValue, Ct()); AppendLog($"OCP已设为 {OcpValue} A"); }));
|
||||
SetRemoteModeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.切换远程控制模式(Ct()); AppendLog("已切换到远程控制模式"); }));
|
||||
SetLocalModeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.切换本地控制模式(Ct()); AppendLog("已切换到本地控制模式"); }));
|
||||
ClearVoltageProtectionCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.清除电压保护状态(Ct()); AppendLog("电压保护状态已清除"); }));
|
||||
ClearCurrentProtectionCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.清除电流保护状态(Ct()); AppendLog("电流保护状态已清除"); }));
|
||||
|
||||
QueryOutputStateCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
OutputState = await _device!.查询DC输出状态(Ct());
|
||||
AppendLog($"输出状态: {OutputState}");
|
||||
}));
|
||||
|
||||
QueryAllMeasureCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
MeasuredVoltage = await _device!.查询实际电压(Ct());
|
||||
MeasuredCurrent = await _device!.查询实际电流(Ct());
|
||||
MeasuredPower = await _device!.查询实际功率(Ct());
|
||||
AppendLog($"测量 → 电压:{MeasuredVoltage}V 电流:{MeasuredCurrent}A 功率:{MeasuredPower}W");
|
||||
}));
|
||||
|
||||
Initialize();
|
||||
}
|
||||
|
||||
#region 初始化 / Navigation
|
||||
|
||||
public void Initialize(string? deviceName = null)
|
||||
{
|
||||
N36600? found = null;
|
||||
string? foundName = null;
|
||||
|
||||
if (deviceName != null &&
|
||||
_deviceManager.DeviceMap.TryGetValue(deviceName, out var d) &&
|
||||
d is N36600 n)
|
||||
{
|
||||
found = n;
|
||||
foundName = deviceName;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var kv in _deviceManager.DeviceMap)
|
||||
{
|
||||
if (kv.Value is N36600 n36)
|
||||
{
|
||||
found = n36;
|
||||
foundName = kv.Key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_device = found;
|
||||
DeviceName = foundName ?? "N36600 (未找到)";
|
||||
IsConnected = _device?.IsConnected ?? false;
|
||||
|
||||
AppendLog(found != null
|
||||
? $"已关联设备 [{DeviceName}],连接状态:{(IsConnected ? "已连接" : "未连接")}"
|
||||
: "未在 DeviceManager 中找到 N36600 设备,请先初始化设备配置。");
|
||||
}
|
||||
|
||||
public override void OnNavigatedTo(NavigationContext context)
|
||||
{
|
||||
var name = context.Parameters.GetValue<string?>("DeviceName");
|
||||
Initialize(name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 辅助
|
||||
|
||||
private CancellationToken Ct() => (_cts = new CancellationTokenSource(TimeSpan.FromSeconds(10))).Token;
|
||||
|
||||
private async Task Exec(Func<Task> action)
|
||||
{
|
||||
if (_device == null)
|
||||
{
|
||||
AppendLog("错误:未关联到设备实例,请检查设备配置。");
|
||||
return;
|
||||
}
|
||||
if (IsBusy) return;
|
||||
IsBusy = true;
|
||||
try
|
||||
{
|
||||
await action();
|
||||
IsConnected = _device.IsConnected;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
AppendLog("命令超时或已取消。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AppendLog($"错误:{ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void AppendLog(string message)
|
||||
{
|
||||
var line = $"[{DateTime.Now:HH:mm:ss}] {message}";
|
||||
ResponseLog = ResponseLog.Length > 4000
|
||||
? line + "\n" + ResponseLog[..3000]
|
||||
: line + "\n" + ResponseLog;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_cts?.Cancel();
|
||||
_cts?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
314
DeviceEditModule/ViewModels/N69200ViewModel.cs
Normal file
314
DeviceEditModule/ViewModels/N69200ViewModel.cs
Normal file
@@ -0,0 +1,314 @@
|
||||
using DeviceCommand.Devices;
|
||||
using Prism.Commands;
|
||||
using Prism.Ioc;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Windows.Input;
|
||||
using UIShare.GlobalVariable;
|
||||
using UIShare.ViewModelBase;
|
||||
using static DeviceCommand.Devices.N69200;
|
||||
|
||||
namespace DeviceEditModule.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// N69200 可编程直流电子负载控制面板 ViewModel。
|
||||
/// </summary>
|
||||
public class N69200ViewModel : NavigateViewModelBase, IDisposable
|
||||
{
|
||||
#region 私有字段
|
||||
|
||||
private readonly DeviceManager _deviceManager;
|
||||
private N69200? _device;
|
||||
private CancellationTokenSource? _cts;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 设备信息属性
|
||||
|
||||
private string _deviceName = "N69200";
|
||||
public string DeviceName
|
||||
{
|
||||
get => _deviceName;
|
||||
set => SetProperty(ref _deviceName, value);
|
||||
}
|
||||
|
||||
private bool _isConnected;
|
||||
public bool IsConnected
|
||||
{
|
||||
get => _isConnected;
|
||||
set => SetProperty(ref _isConnected, value);
|
||||
}
|
||||
|
||||
private bool _isBusy;
|
||||
public bool IsBusy
|
||||
{
|
||||
get => _isBusy;
|
||||
set => SetProperty(ref _isBusy, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 输入参数属性
|
||||
|
||||
private DeviceWorkMode _selectedMode = DeviceWorkMode.CC;
|
||||
/// <summary>工作模式:CC / CV / CP / CR。</summary>
|
||||
public DeviceWorkMode SelectedMode
|
||||
{
|
||||
get => _selectedMode;
|
||||
set => SetProperty(ref _selectedMode, value);
|
||||
}
|
||||
|
||||
private double _loadValue = 1.0;
|
||||
/// <summary>设定值(根据 SelectedMode 代表 A / V / W / Ω)。</summary>
|
||||
public double LoadValue
|
||||
{
|
||||
get => _loadValue;
|
||||
set => SetProperty(ref _loadValue, value);
|
||||
}
|
||||
|
||||
private double _ovpValue = 60.0;
|
||||
/// <summary>过压保护值(V)。</summary>
|
||||
public double OvpValue
|
||||
{
|
||||
get => _ovpValue;
|
||||
set => SetProperty(ref _ovpValue, value);
|
||||
}
|
||||
|
||||
private double _ocpValue = 10.0;
|
||||
/// <summary>过流保护值(A)。</summary>
|
||||
public double OcpValue
|
||||
{
|
||||
get => _ocpValue;
|
||||
set => SetProperty(ref _ocpValue, value);
|
||||
}
|
||||
|
||||
private double _oppValue = 200.0;
|
||||
/// <summary>过功率保护值(W)。</summary>
|
||||
public double OppValue
|
||||
{
|
||||
get => _oppValue;
|
||||
set => SetProperty(ref _oppValue, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 测量结果属性
|
||||
|
||||
private string _measuredVoltage = "—";
|
||||
public string MeasuredVoltage
|
||||
{
|
||||
get => _measuredVoltage;
|
||||
set => SetProperty(ref _measuredVoltage, value);
|
||||
}
|
||||
|
||||
private string _measuredCurrent = "—";
|
||||
public string MeasuredCurrent
|
||||
{
|
||||
get => _measuredCurrent;
|
||||
set => SetProperty(ref _measuredCurrent, value);
|
||||
}
|
||||
|
||||
private string _measuredPower = "—";
|
||||
public string MeasuredPower
|
||||
{
|
||||
get => _measuredPower;
|
||||
set => SetProperty(ref _measuredPower, value);
|
||||
}
|
||||
|
||||
private string _currentMode = "—";
|
||||
/// <summary>当前负载模式(MODE? 查询结果)。</summary>
|
||||
public string CurrentMode
|
||||
{
|
||||
get => _currentMode;
|
||||
set => SetProperty(ref _currentMode, value);
|
||||
}
|
||||
|
||||
private string _statusByte = "—";
|
||||
/// <summary>状态字节(*STB? 查询结果)。</summary>
|
||||
public string StatusByte
|
||||
{
|
||||
get => _statusByte;
|
||||
set => SetProperty(ref _statusByte, value);
|
||||
}
|
||||
|
||||
private string _responseLog = string.Empty;
|
||||
/// <summary>命令响应日志(最新消息在顶部)。</summary>
|
||||
public string ResponseLog
|
||||
{
|
||||
get => _responseLog;
|
||||
set => SetProperty(ref _responseLog, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
|
||||
public ICommand QueryIdentityCommand { get; }
|
||||
public ICommand ResetDeviceCommand { get; }
|
||||
public ICommand InputOnCommand { get; }
|
||||
public ICommand InputOffCommand { get; }
|
||||
public ICommand SetModeCommand { get; }
|
||||
public ICommand QueryModeCommand { get; }
|
||||
public ICommand SetLoadValueCommand { get; }
|
||||
public ICommand QueryAllMeasureCommand { get; }
|
||||
public ICommand QueryStatusCommand { get; }
|
||||
public ICommand SetOvpCommand { get; }
|
||||
public ICommand SetOcpCommand { get; }
|
||||
public ICommand SetOppCommand { get; }
|
||||
public ICommand ClearAlarmCommand { get; }
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
public N69200ViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
_deviceManager = containerProvider.Resolve<DeviceManager>();
|
||||
|
||||
QueryIdentityCommand = new DelegateCommand(async () => await Exec(async () => AppendLog("IDN: " + await _device!.查询设备标识(Ct()))));
|
||||
ResetDeviceCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.重置设备(Ct()); AppendLog("设备已重置"); }));
|
||||
InputOnCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置DC输入(true, Ct()); AppendLog("输入已开启"); }));
|
||||
InputOffCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置DC输入(false, Ct()); AppendLog("输入已关闭"); }));
|
||||
SetModeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置负载模式(SelectedMode, Ct()); AppendLog($"模式已设为 {SelectedMode}"); }));
|
||||
QueryModeCommand = new DelegateCommand(async () => await Exec(async () => { CurrentMode = await _device!.查询负载模式(Ct()); AppendLog($"当前模式: {CurrentMode}"); }));
|
||||
SetOvpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置过压保护值_OVP(OvpValue, Ct()); AppendLog($"OVP已设为 {OvpValue} V"); }));
|
||||
SetOcpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置过流保护值_OCP(OcpValue, Ct()); AppendLog($"OCP已设为 {OcpValue} A"); }));
|
||||
SetOppCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置过功率保护值_OPP(OppValue, Ct()); AppendLog($"OPP已设为 {OppValue} W"); }));
|
||||
ClearAlarmCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.清除保护告警(Ct()); AppendLog("保护告警已清除"); }));
|
||||
|
||||
|
||||
SetLoadValueCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
switch (SelectedMode)
|
||||
{
|
||||
case DeviceWorkMode.CC:
|
||||
await _device!.设置恒电流CC(LoadValue, Ct());
|
||||
AppendLog($"恒流 CC 已设为 {LoadValue} A");
|
||||
break;
|
||||
case DeviceWorkMode.CV:
|
||||
await _device!.设置恒电压CV(LoadValue, Ct());
|
||||
AppendLog($"恒压 CV 已设为 {LoadValue} V");
|
||||
break;
|
||||
case DeviceWorkMode.CP:
|
||||
await _device!.设置恒功率CP(LoadValue, Ct());
|
||||
AppendLog($"恒功率 CP 已设为 {LoadValue} W");
|
||||
break;
|
||||
case DeviceWorkMode.CR:
|
||||
await _device!.设置恒电阻CR(LoadValue, Ct());
|
||||
AppendLog($"恒电阻 CR 已设为 {LoadValue} Ω");
|
||||
break;
|
||||
default:
|
||||
AppendLog($"模式 {SelectedMode} 不支持设置设定值");
|
||||
break;
|
||||
}
|
||||
}));
|
||||
|
||||
QueryStatusCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
StatusByte = await _device!.读取状态字节(Ct());
|
||||
AppendLog($"状态字节: {StatusByte}");
|
||||
}));
|
||||
|
||||
QueryAllMeasureCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
MeasuredVoltage = await _device!.查询实际电压(Ct());
|
||||
MeasuredCurrent = await _device!.查询实际电流(Ct());
|
||||
MeasuredPower = await _device!.查询实际功率(Ct());
|
||||
AppendLog($"测量 → 电压:{MeasuredVoltage}V 电流:{MeasuredCurrent}A 功率:{MeasuredPower}W");
|
||||
}));
|
||||
|
||||
Initialize();
|
||||
}
|
||||
|
||||
#region 初始化 / Navigation
|
||||
|
||||
public void Initialize(string? deviceName = null)
|
||||
{
|
||||
N69200? found = null;
|
||||
string? foundName = null;
|
||||
|
||||
if (deviceName != null &&
|
||||
_deviceManager.DeviceMap.TryGetValue(deviceName, out var d) &&
|
||||
d is N69200 n)
|
||||
{
|
||||
found = n;
|
||||
foundName = deviceName;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var kv in _deviceManager.DeviceMap)
|
||||
{
|
||||
if (kv.Value is N69200 n69)
|
||||
{
|
||||
found = n69;
|
||||
foundName = kv.Key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_device = found;
|
||||
DeviceName = foundName ?? "N69200 (未找到)";
|
||||
IsConnected = _device?.IsConnected ?? false;
|
||||
|
||||
AppendLog(found != null
|
||||
? $"已关联设备 [{DeviceName}],连接状态:{(IsConnected ? "已连接" : "未连接")}"
|
||||
: "未在 DeviceManager 中找到 N69200 设备,请先初始化设备配置。");
|
||||
}
|
||||
|
||||
public override void OnNavigatedTo(NavigationContext context)
|
||||
{
|
||||
var name = context.Parameters.GetValue<string?>("DeviceName");
|
||||
Initialize(name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 辅助
|
||||
|
||||
private CancellationToken Ct() => (_cts = new CancellationTokenSource(TimeSpan.FromSeconds(10))).Token;
|
||||
|
||||
private async Task Exec(Func<Task> action)
|
||||
{
|
||||
if (_device == null)
|
||||
{
|
||||
AppendLog("错误:未关联到设备实例,请检查设备配置。");
|
||||
return;
|
||||
}
|
||||
if (IsBusy) return;
|
||||
IsBusy = true;
|
||||
try
|
||||
{
|
||||
await action();
|
||||
IsConnected = _device.IsConnected;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
AppendLog("命令超时或已取消。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AppendLog($"错误:{ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void AppendLog(string message)
|
||||
{
|
||||
var line = $"[{DateTime.Now:HH:mm:ss}] {message}";
|
||||
ResponseLog = ResponseLog.Length > 4000
|
||||
? line + "\n" + ResponseLog[..3000]
|
||||
: line + "\n" + ResponseLog;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_cts?.Cancel();
|
||||
_cts?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
387
DeviceEditModule/ViewModels/SDS2000X_HDViewModel.cs
Normal file
387
DeviceEditModule/ViewModels/SDS2000X_HDViewModel.cs
Normal file
@@ -0,0 +1,387 @@
|
||||
using DeviceCommand.Devices;
|
||||
using Prism.Commands;
|
||||
using Prism.Ioc;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using UIShare.GlobalVariable;
|
||||
using UIShare.ViewModelBase;
|
||||
using static DeviceCommand.Devices.SDS2000X_HD;
|
||||
|
||||
namespace DeviceEditModule.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// SDS2000X_HD 数字存储示波器控制面板 ViewModel。
|
||||
/// </summary>
|
||||
public class SDS2000X_HDViewModel : NavigateViewModelBase, IDisposable
|
||||
{
|
||||
#region 私有字段
|
||||
|
||||
private readonly DeviceManager _deviceManager;
|
||||
private SDS2000X_HD? _device;
|
||||
private CancellationTokenSource? _cts;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 设备信息属性
|
||||
|
||||
private string _deviceName = "SDS2000X_HD";
|
||||
public string DeviceName
|
||||
{
|
||||
get => _deviceName;
|
||||
set => SetProperty(ref _deviceName, value);
|
||||
}
|
||||
|
||||
private bool _isConnected;
|
||||
public bool IsConnected
|
||||
{
|
||||
get => _isConnected;
|
||||
set => SetProperty(ref _isConnected, value);
|
||||
}
|
||||
|
||||
private bool _isBusy;
|
||||
public bool IsBusy
|
||||
{
|
||||
get => _isBusy;
|
||||
set => SetProperty(ref _isBusy, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 输入参数属性
|
||||
|
||||
private int _channel = 1;
|
||||
/// <summary>当前操作通道(1~4)。</summary>
|
||||
public int Channel
|
||||
{
|
||||
get => _channel;
|
||||
set => SetProperty(ref _channel, value);
|
||||
}
|
||||
|
||||
private double _voltsPerDiv = 1.0;
|
||||
/// <summary>垂直电压档位(V/div)。</summary>
|
||||
public double VoltsPerDiv
|
||||
{
|
||||
get => _voltsPerDiv;
|
||||
set => SetProperty(ref _voltsPerDiv, value);
|
||||
}
|
||||
|
||||
private double _offset = 0.0;
|
||||
/// <summary>垂直偏移(V)。</summary>
|
||||
public double Offset
|
||||
{
|
||||
get => _offset;
|
||||
set => SetProperty(ref _offset, value);
|
||||
}
|
||||
|
||||
private bool _is50Ohm;
|
||||
/// <summary>是否使用 50Ω 输入阻抗,false 为 1MΩ。</summary>
|
||||
public bool Is50Ohm
|
||||
{
|
||||
get => _is50Ohm;
|
||||
set => SetProperty(ref _is50Ohm, value);
|
||||
}
|
||||
|
||||
private double _timeBase = 0.001;
|
||||
/// <summary>水平时基档位(s/div)。</summary>
|
||||
public double TimeBase
|
||||
{
|
||||
get => _timeBase;
|
||||
set => SetProperty(ref _timeBase, value);
|
||||
}
|
||||
|
||||
private double _triggerLevel = 0.0;
|
||||
/// <summary>触发电平(V)。</summary>
|
||||
public double TriggerLevel
|
||||
{
|
||||
get => _triggerLevel;
|
||||
set => SetProperty(ref _triggerLevel, value);
|
||||
}
|
||||
|
||||
private string _triggerSource = "C1";
|
||||
/// <summary>触发源(C1/C2/C3/C4/EX/LINE)。</summary>
|
||||
public string TriggerSource
|
||||
{
|
||||
get => _triggerSource;
|
||||
set => SetProperty(ref _triggerSource, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 测量结果属性
|
||||
|
||||
private string _measuredVpp = "—";
|
||||
public string MeasuredVpp
|
||||
{
|
||||
get => _measuredVpp;
|
||||
set => SetProperty(ref _measuredVpp, value);
|
||||
}
|
||||
|
||||
private string _measuredFrequency = "—";
|
||||
public string MeasuredFrequency
|
||||
{
|
||||
get => _measuredFrequency;
|
||||
set => SetProperty(ref _measuredFrequency, value);
|
||||
}
|
||||
|
||||
private string _measuredRms = "—";
|
||||
public string MeasuredRms
|
||||
{
|
||||
get => _measuredRms;
|
||||
set => SetProperty(ref _measuredRms, value);
|
||||
}
|
||||
|
||||
private string _responseLog = string.Empty;
|
||||
/// <summary>命令响应日志(最新消息在顶部)。</summary>
|
||||
public string ResponseLog
|
||||
{
|
||||
get => _responseLog;
|
||||
set => SetProperty(ref _responseLog, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
|
||||
public ICommand QueryIdentityCommand { get; }
|
||||
public ICommand ResetDeviceCommand { get; }
|
||||
public ICommand RunCommand { get; }
|
||||
public ICommand StopCommand { get; }
|
||||
public ICommand SingleCommand { get; }
|
||||
public ICommand ForceTriggerCommand { get; }
|
||||
public ICommand SetChannelOnCommand { get; }
|
||||
public ICommand SetChannelOffCommand { get; }
|
||||
public ICommand SetVoltsDivCommand { get; }
|
||||
public ICommand SetOffsetCommand { get; }
|
||||
|
||||
// 🛠️ 补全:设置阻抗的 Command
|
||||
public ICommand SetImpedance50Command { get; }
|
||||
public ICommand SetImpedance1MCommand { get; }
|
||||
|
||||
public ICommand SetTimeBaseCommand { get; }
|
||||
public ICommand SetTriggerLevelCommand { get; }
|
||||
public ICommand SetTriggerSourceCommand { get; }
|
||||
public ICommand QueryMeasurementsCommand { get; }
|
||||
public ICommand QueryVppCommand { get; }
|
||||
public ICommand QueryFrequencyCommand { get; }
|
||||
public ICommand QueryRmsCommand { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
public SDS2000X_HDViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
_deviceManager = containerProvider.Resolve<DeviceManager>();
|
||||
|
||||
QueryIdentityCommand = new DelegateCommand(async () => await Exec(async () => AppendLog("IDN: " + await _device!.查询设备标识(Ct()))));
|
||||
ResetDeviceCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.重置设备(Ct()); AppendLog("设备已重置"); }));
|
||||
RunCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.启动捕获_RUN(Ct()); AppendLog("已开始捕获"); }));
|
||||
StopCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.停止捕获_STOP(Ct()); AppendLog("已停止捕获"); }));
|
||||
SingleCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.单次触发_SINGLE(Ct()); AppendLog("已触发单次捕获"); }));
|
||||
ForceTriggerCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.强制触发(Ct()); AppendLog("已强制触发"); }));
|
||||
SetChannelOnCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置通道开关(Channel, true, Ct()); AppendLog($"通道 {Channel} 已开启"); }));
|
||||
SetChannelOffCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置通道开关(Channel, false, Ct()); AppendLog($"通道 {Channel} 已关闭"); }));
|
||||
SetVoltsDivCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置通道电压档位(Channel, VoltsPerDiv, Ct()); AppendLog($"C{Channel} 电压档位已设为 {VoltsPerDiv} V/div"); }));
|
||||
SetOffsetCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置通道垂直偏移(Channel, Offset, Ct()); AppendLog($"C{Channel} 垂直偏移已设为 {Offset} V"); }));
|
||||
|
||||
// 🛠️ 绑定:设置 50Ω 和 1MΩ 阻抗控制
|
||||
SetImpedance50Command = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
await _device!.设置通道阻抗(Channel, ImpedanceType.FIFty, Ct());
|
||||
Is50Ohm = true;
|
||||
AppendLog($"C{Channel} 输入阻抗已设为 50Ω");
|
||||
}));
|
||||
|
||||
SetImpedance1MCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
await _device!.设置通道阻抗(Channel, ImpedanceType.ONEM, Ct());
|
||||
Is50Ohm = false;
|
||||
AppendLog($"C{Channel} 输入阻抗已设为 1MΩ");
|
||||
}));
|
||||
|
||||
SetTimeBaseCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置水平时基(TimeBase, Ct()); AppendLog($"水平时基已设为 {TimeBase} s/div"); }));
|
||||
SetTriggerLevelCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置触发电平(TriggerLevel, Ct()); AppendLog($"触发电平已设为 {TriggerLevel} V"); }));
|
||||
SetTriggerSourceCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置触发源(TriggerSource, Ct()); AppendLog($"触发源已设为 {TriggerSource}"); }));
|
||||
|
||||
QueryMeasurementsCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
// 1. 保留设备原生吐出的完整原始字符串
|
||||
string rawVpp = await _device!.查询实际电压峰峰值(Channel, Ct());
|
||||
string rawFreq = await _device!.查询实际频率(Channel, Ct());
|
||||
string rawRms = await _device!.查询实际电压均方根(Channel, Ct());
|
||||
|
||||
// 2. 清洗数据并将科学计数法转为常规小数后赋值给 UI 属性
|
||||
MeasuredVpp = ParseMeasurement(rawVpp);
|
||||
MeasuredFrequency = ParseMeasurement(rawFreq);
|
||||
MeasuredRms = ParseMeasurement(rawRms);
|
||||
|
||||
// 3. 在日志中同时体现设备原始报文与解析呈现值,极方便联调
|
||||
AppendLog($"C{Channel} 测量原始数据 → Vpp:{rawVpp.Trim()} Freq:{rawFreq.Trim()} RMS:{rawRms.Trim()}");
|
||||
AppendLog($"C{Channel} 界面呈现数值 → Vpp:{MeasuredVpp}V Freq:{MeasuredFrequency}Hz RMS:{MeasuredRms}V");
|
||||
}));
|
||||
|
||||
QueryVppCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
string raw = await _device!.查询实际电压峰峰值(Channel, Ct());
|
||||
MeasuredVpp = ParseMeasurement(raw);
|
||||
AppendLog($"C{Channel} Vpp: {MeasuredVpp} (Raw: {raw.Trim()})");
|
||||
}));
|
||||
|
||||
QueryFrequencyCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
string raw = await _device!.查询实际频率(Channel, Ct());
|
||||
MeasuredFrequency = ParseMeasurement(raw);
|
||||
AppendLog($"C{Channel} Freq: {MeasuredFrequency} (Raw: {raw.Trim()})");
|
||||
}));
|
||||
|
||||
QueryRmsCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
string raw = await _device!.查询实际电压均方根(Channel, Ct());
|
||||
MeasuredRms = ParseMeasurement(raw);
|
||||
AppendLog($"C{Channel} RMS: {MeasuredRms} (Raw: {raw.Trim()})");
|
||||
}));
|
||||
|
||||
Initialize();
|
||||
}
|
||||
|
||||
#region 初始化 / Navigation
|
||||
|
||||
public void Initialize(string? deviceName = null)
|
||||
{
|
||||
SDS2000X_HD? found = null;
|
||||
string? foundName = null;
|
||||
|
||||
if (deviceName != null &&
|
||||
_deviceManager.DeviceMap.TryGetValue(deviceName, out var d) &&
|
||||
d is SDS2000X_HD s)
|
||||
{
|
||||
found = s;
|
||||
foundName = deviceName;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var kv in _deviceManager.DeviceMap)
|
||||
{
|
||||
if (kv.Value is SDS2000X_HD sds)
|
||||
{
|
||||
found = sds;
|
||||
foundName = kv.Key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_device = found;
|
||||
DeviceName = foundName ?? "SDS2000X_HD (未找到)";
|
||||
IsConnected = _device?.IsConnected ?? false;
|
||||
|
||||
AppendLog(found != null
|
||||
? $"已关联设备 [{DeviceName}],连接状态:{(IsConnected ? "已连接" : "未连接")}"
|
||||
: "未在 DeviceManager 中找到 SDS2000X_HD 设备,请先初始化设备配置。");
|
||||
}
|
||||
|
||||
public override void OnNavigatedTo(NavigationContext context)
|
||||
{
|
||||
var name = context.Parameters.GetValue<string?>("DeviceName");
|
||||
Initialize(name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 辅助
|
||||
|
||||
private CancellationToken Ct() => (_cts = new CancellationTokenSource(TimeSpan.FromSeconds(10))).Token;
|
||||
|
||||
private async Task Exec(Func<Task> action)
|
||||
{
|
||||
if (_device == null)
|
||||
{
|
||||
AppendLog("错误:未关联到设备实例,请检查设备配置。");
|
||||
return;
|
||||
}
|
||||
if (IsBusy) return;
|
||||
IsBusy = true;
|
||||
try
|
||||
{
|
||||
await action();
|
||||
IsConnected = _device.IsConnected;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
AppendLog("命令超时或已取消。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AppendLog($"错误:{ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void AppendLog(string message)
|
||||
{
|
||||
var line = $"[{DateTime.Now:HH:mm:ss}] {message}";
|
||||
ResponseLog = ResponseLog.Length > 4000
|
||||
? line + "\n" + ResponseLog[..3000]
|
||||
: line + "\n" + ResponseLog;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清洗示波器原始返回的测量字符串(例如 "C1:PAVA RMS,5.57E-03V")并安全转化为无科学计数法的小数形式。
|
||||
/// </summary>
|
||||
/// <param name="rawResponse">设备原始应答数据</param>
|
||||
/// <returns>可直接绑定到 UI 呈现的字符串数字</returns>
|
||||
private string ParseMeasurement(string rawResponse)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(rawResponse)) return "—";
|
||||
|
||||
try
|
||||
{
|
||||
string cleanData = rawResponse.Trim();
|
||||
|
||||
// 1. 斩断报头,提取逗号后面的具体内容(如 "5.57E-03V" 或 "****")
|
||||
int commaIndex = cleanData.IndexOf(',');
|
||||
if (commaIndex == -1) return "—";
|
||||
|
||||
string valStr = cleanData.Substring(commaIndex + 1);
|
||||
|
||||
var match = Regex.Match(valStr, @"[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?");
|
||||
if (match.Success)
|
||||
{
|
||||
valStr = match.Value;
|
||||
}
|
||||
|
||||
// 3. 校验并拦截设备未测出时的无效星号 "****"
|
||||
if (valStr.Contains("*") || string.IsNullOrWhiteSpace(valStr))
|
||||
{
|
||||
return "0"; // 回归为零或 "—",防止触发数据异常
|
||||
}
|
||||
|
||||
// 4. 解析科学计数法,并重新以不带科学计数法的小数样式展开
|
||||
if (double.TryParse(valStr, NumberStyles.Any, CultureInfo.InvariantCulture, out double result))
|
||||
{
|
||||
// "0.######" 样式会自动消除末尾无用的冗余零,并显示为普通小数(如 0.00557)
|
||||
return result.ToString("0.######", CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 捕获可能产生的边缘转换故障,保证轮询线程绝不崩溃
|
||||
}
|
||||
|
||||
return "—";
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_cts?.Cancel();
|
||||
_cts?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
345
DeviceEditModule/ViewModels/SPAW7000ViewModel.cs
Normal file
345
DeviceEditModule/ViewModels/SPAW7000ViewModel.cs
Normal file
@@ -0,0 +1,345 @@
|
||||
using DeviceCommand.Devices;
|
||||
using Prism.Commands;
|
||||
using Prism.Ioc;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using UIShare.GlobalVariable;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
namespace DeviceEditModule.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// SPAW7000 功率分析记录仪控制面板 ViewModel。
|
||||
/// </summary>
|
||||
public class SPAW7000ViewModel : NavigateViewModelBase, IDisposable
|
||||
{
|
||||
#region 私有字段
|
||||
|
||||
private readonly DeviceManager _deviceManager;
|
||||
private SPAW7000? _device;
|
||||
private CancellationTokenSource? _cts;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 设备信息属性
|
||||
|
||||
private string _deviceName = "SPAW7000";
|
||||
public string DeviceName
|
||||
{
|
||||
get => _deviceName;
|
||||
set => SetProperty(ref _deviceName, value);
|
||||
}
|
||||
|
||||
private bool _isConnected;
|
||||
public bool IsConnected
|
||||
{
|
||||
get => _isConnected;
|
||||
set => SetProperty(ref _isConnected, value);
|
||||
}
|
||||
|
||||
private bool _isBusy;
|
||||
public bool IsBusy
|
||||
{
|
||||
get => _isBusy;
|
||||
set => SetProperty(ref _isBusy, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 输入参数属性
|
||||
|
||||
private int _channel = 1;
|
||||
/// <summary>当前操作通道。</summary>
|
||||
public int Channel
|
||||
{
|
||||
get => _channel;
|
||||
set => SetProperty(ref _channel, value);
|
||||
}
|
||||
|
||||
private SPAW7000.VoltageRange _voltageRange = SPAW7000.VoltageRange.V_300;
|
||||
/// <summary>电压量程枚举。</summary>
|
||||
public SPAW7000.VoltageRange VoltageRange
|
||||
{
|
||||
get => _voltageRange;
|
||||
set => SetProperty(ref _voltageRange, value);
|
||||
}
|
||||
|
||||
private SPAW7000.CurrentRange _currentRange = SPAW7000.CurrentRange.A_5;
|
||||
/// <summary>电流量程枚举。</summary>
|
||||
public SPAW7000.CurrentRange CurrentRange
|
||||
{
|
||||
get => _currentRange;
|
||||
set => SetProperty(ref _currentRange, value);
|
||||
}
|
||||
|
||||
public SPAW7000.VoltageRange[] VoltageRangeOptions { get; } =
|
||||
(SPAW7000.VoltageRange[])Enum.GetValues(typeof(SPAW7000.VoltageRange));
|
||||
|
||||
public SPAW7000.CurrentRange[] CurrentRangeOptions { get; } =
|
||||
(SPAW7000.CurrentRange[])Enum.GetValues(typeof(SPAW7000.CurrentRange));
|
||||
|
||||
private int _resolution = 6;
|
||||
/// <summary>显示分辨率(5 或 6)。</summary>
|
||||
public int Resolution
|
||||
{
|
||||
get => _resolution;
|
||||
set => SetProperty(ref _resolution, value);
|
||||
}
|
||||
|
||||
private int _brightness = 5;
|
||||
/// <summary>屏幕亮度(1~10)。</summary>
|
||||
public int Brightness
|
||||
{
|
||||
get => _brightness;
|
||||
set => SetProperty(ref _brightness, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 测量结果属性
|
||||
|
||||
private string _measuredVoltage = "—";
|
||||
public string MeasuredVoltage
|
||||
{
|
||||
get => _measuredVoltage;
|
||||
set => SetProperty(ref _measuredVoltage, value);
|
||||
}
|
||||
|
||||
private string _measuredCurrent = "—";
|
||||
public string MeasuredCurrent
|
||||
{
|
||||
get => _measuredCurrent;
|
||||
set => SetProperty(ref _measuredCurrent, value);
|
||||
}
|
||||
|
||||
private string _measuredPower = "—";
|
||||
public string MeasuredPower
|
||||
{
|
||||
get => _measuredPower;
|
||||
set => SetProperty(ref _measuredPower, value);
|
||||
}
|
||||
|
||||
private string _measuredFrequency = "—";
|
||||
public string MeasuredFrequency
|
||||
{
|
||||
get => _measuredFrequency;
|
||||
set => SetProperty(ref _measuredFrequency, value);
|
||||
}
|
||||
|
||||
private string _measuredPowerFactor = "—";
|
||||
public string MeasuredPowerFactor
|
||||
{
|
||||
get => _measuredPowerFactor;
|
||||
set => SetProperty(ref _measuredPowerFactor, value);
|
||||
}
|
||||
|
||||
private string _deviceModel = "—";
|
||||
public string DeviceModel
|
||||
{
|
||||
get => _deviceModel;
|
||||
set => SetProperty(ref _deviceModel, value);
|
||||
}
|
||||
|
||||
private string _deviceSerial = "—";
|
||||
public string DeviceSerial
|
||||
{
|
||||
get => _deviceSerial;
|
||||
set => SetProperty(ref _deviceSerial, value);
|
||||
}
|
||||
|
||||
private string _responseLog = string.Empty;
|
||||
/// <summary>命令响应日志(最新消息在顶部)。</summary>
|
||||
public string ResponseLog
|
||||
{
|
||||
get => _responseLog;
|
||||
set => SetProperty(ref _responseLog, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
|
||||
public ICommand QueryIdentityCommand { get; }
|
||||
public ICommand ResetDeviceCommand { get; }
|
||||
public ICommand QueryAllMeasureCommand { get; }
|
||||
public ICommand SetVoltageRangeCommand { get; }
|
||||
public ICommand SetCurrentRangeCommand { get; }
|
||||
public ICommand SetResolutionCommand { get; }
|
||||
public ICommand SetBrightnessCommand { get; }
|
||||
public ICommand SetTouchLockOnCommand { get; }
|
||||
public ICommand SetTouchLockOffCommand { get; }
|
||||
public ICommand QueryModelCommand { get; }
|
||||
public ICommand QuerySerialCommand { get; }
|
||||
public ICommand QueryStatusByteCommand { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
public SPAW7000ViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
_deviceManager = containerProvider.Resolve<DeviceManager>();
|
||||
|
||||
QueryIdentityCommand = new DelegateCommand(async () => await Exec(async () => AppendLog("IDN: " + await _device!.查询设备标识(Ct()))));
|
||||
ResetDeviceCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.重置设备(Ct()); AppendLog("设备已重置"); }));
|
||||
SetVoltageRangeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置电压量程(Channel, VoltageRange, Ct()); AppendLog($"通道 {Channel} 电压量程已设为 {(int)VoltageRange} V"); }));
|
||||
SetCurrentRangeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置电流量程(Channel, CurrentRange, Ct()); AppendLog($"通道 {Channel} 电流量程已设为 {(int)CurrentRange} A"); }));
|
||||
SetResolutionCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置显示分辨率(Resolution, Ct()); AppendLog($"显示分辨率已设为 {Resolution} 位"); }));
|
||||
SetBrightnessCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置显示亮度(Brightness, Ct()); AppendLog($"屏幕亮度已设为 {Brightness}"); }));
|
||||
SetTouchLockOnCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置屏幕触摸锁定(true, Ct()); AppendLog("屏幕触摸已锁定"); }));
|
||||
SetTouchLockOffCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置屏幕触摸锁定(false, Ct()); AppendLog("屏幕触摸已解锁"); }));
|
||||
QueryModelCommand = new DelegateCommand(async () => await Exec(async () => { DeviceModel = await _device!.查询设备型号(Ct()); AppendLog($"型号: {DeviceModel}"); }));
|
||||
QuerySerialCommand = new DelegateCommand(async () => await Exec(async () => { DeviceSerial = await _device!.查询设备序列号(Ct()); AppendLog($"序列号: {DeviceSerial}"); }));
|
||||
QueryStatusByteCommand = new DelegateCommand(async () => await Exec(async () => AppendLog("STB: " + await _device!.读取状态字节(Ct()))));
|
||||
|
||||
// 只修改这里:保持你原本的 5 次驱动查询调用不变,仅对读回来的科学计数法进行两位小数格式化
|
||||
QueryAllMeasureCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
string rawU = await _device!.查询实际电压(Channel, Ct());
|
||||
string rawI = await _device!.查询实际电流(Channel, Ct());
|
||||
string rawP = await _device!.查询实际功率(Channel, Ct());
|
||||
string rawF = await _device!.查询频率(Channel, Ct());
|
||||
string rawPF = await _device!.查询功率因数(Channel, Ct());
|
||||
|
||||
// 转换科学计数法格式,如果失败会自动保持原样
|
||||
MeasuredVoltage = FormatToDecimal(rawU, "F2"); // 电压两位小数
|
||||
MeasuredCurrent = FormatToDecimal(rawI, "F3"); // 电流通常较小,建议保留3位小数
|
||||
MeasuredPower = FormatToDecimal(rawP, "F2"); // 功率两位小数
|
||||
MeasuredFrequency = FormatToDecimal(rawF, "F2"); // 频率两位小数
|
||||
MeasuredPowerFactor = FormatToDecimal(rawPF, "F3"); // 功率因数保留3位小数
|
||||
|
||||
AppendLog($"CH{Channel} 测量 → U:{MeasuredVoltage}V I:{MeasuredCurrent}A P:{MeasuredPower}W F:{MeasuredFrequency}Hz PF:{MeasuredPowerFactor}");
|
||||
}));
|
||||
|
||||
Initialize();
|
||||
}
|
||||
|
||||
#region 初始化 / Navigation
|
||||
|
||||
public void Initialize(string? deviceName = null)
|
||||
{
|
||||
SPAW7000? found = null;
|
||||
string? foundName = null;
|
||||
|
||||
if (deviceName != null &&
|
||||
_deviceManager.DeviceMap.TryGetValue(deviceName, out var d) &&
|
||||
d is SPAW7000 s)
|
||||
{
|
||||
found = s;
|
||||
foundName = deviceName;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var kv in _deviceManager.DeviceMap)
|
||||
{
|
||||
if (kv.Value is SPAW7000 spaw)
|
||||
{
|
||||
found = spaw;
|
||||
foundName = kv.Key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_device = found;
|
||||
DeviceName = foundName ?? "SPAW7000 (未找到)";
|
||||
IsConnected = _device?.IsConnected ?? false;
|
||||
|
||||
AppendLog(found != null
|
||||
? $"已关联设备 [{DeviceName}],连接状态:{(IsConnected ? "已连接" : "未连接")}"
|
||||
: "未在 DeviceManager 中找到 SPAW7000 设备,请先初始化设备配置。");
|
||||
}
|
||||
|
||||
public override void OnNavigatedTo(NavigationContext context)
|
||||
{
|
||||
var name = context.Parameters.GetValue<string?>("DeviceName");
|
||||
Initialize(name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 辅助
|
||||
|
||||
private CancellationToken Ct() => (_cts = new CancellationTokenSource(TimeSpan.FromSeconds(10))).Token;
|
||||
|
||||
/// <summary>
|
||||
/// 将科学计数法字符串转换为标准小数格式
|
||||
/// </summary>
|
||||
private string FormatToDecimal(string rawInput, string decimalFormat = "F2")
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(rawInput))
|
||||
return "—";
|
||||
|
||||
// 去除可能夹杂的命令头,保留数据部分
|
||||
string cleanInput = CleanResponseHeader(rawInput);
|
||||
|
||||
// 解析科学计数法(NumberStyles.Any 和 InvariantCulture 是关键)
|
||||
if (double.TryParse(cleanInput, NumberStyles.Any, CultureInfo.InvariantCulture, out double value))
|
||||
{
|
||||
return value.ToString(decimalFormat, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
return cleanInput;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 辅助清洗:剥离可能伴随吐回的命令头或双引号
|
||||
/// </summary>
|
||||
private string CleanResponseHeader(string response)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(response)) return string.Empty;
|
||||
string result = response.Trim();
|
||||
if (result.Contains(" "))
|
||||
{
|
||||
int lastSpaceIndex = result.LastIndexOf(' ');
|
||||
result = result.Substring(lastSpaceIndex + 1).Trim();
|
||||
}
|
||||
return result.Replace("\"", "").Replace("'", "").Trim();
|
||||
}
|
||||
|
||||
private async Task Exec(Func<Task> action)
|
||||
{
|
||||
if (_device == null)
|
||||
{
|
||||
AppendLog("错误:未关联到设备实例,请检查设备配置。");
|
||||
return;
|
||||
}
|
||||
if (IsBusy) return;
|
||||
IsBusy = true;
|
||||
try
|
||||
{
|
||||
await action();
|
||||
IsConnected = _device.IsConnected;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
AppendLog("命令超时或已取消。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AppendLog($"错误:{ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void AppendLog(string message)
|
||||
{
|
||||
var line = $"[{DateTime.Now:HH:mm:ss}] {message}";
|
||||
ResponseLog = ResponseLog.Length > 4000
|
||||
? line + "\n" + ResponseLog[..3000]
|
||||
: line + "\n" + ResponseLog;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_cts?.Cancel();
|
||||
_cts?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user