spaw7000设备命令修改

This commit is contained in:
hsc
2026-07-14 13:22:15 +08:00
parent 3318c720b7
commit 2fe307c142
8 changed files with 214 additions and 127 deletions

View File

@@ -2,7 +2,9 @@ using DeviceCommand.Device;
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;
@@ -57,29 +59,27 @@ namespace DeviceEditModule.ViewModels
set => SetProperty(ref _channel, value);
}
private double _voltageRange = 300.0;
/// <summary>电压量程V。</summary>
public double VoltageRange
private SPAW7000.VoltageRange _voltageRange = SPAW7000.VoltageRange.V_300;
/// <summary>电压量程枚举。</summary>
public SPAW7000.VoltageRange VoltageRange
{
get => _voltageRange;
set => SetProperty(ref _voltageRange, value);
}
private double _currentRange = 5.0;
/// <summary>电流量程A。</summary>
public double CurrentRange
private SPAW7000.CurrentRange _currentRange = SPAW7000.CurrentRange.A_5;
/// <summary>电流量程枚举。</summary>
public SPAW7000.CurrentRange CurrentRange
{
get => _currentRange;
set => SetProperty(ref _currentRange, value);
}
private string _couplingMode = "DC";
/// <summary>耦合模式AC / DC / ACDC。</summary>
public string CouplingMode
{
get => _couplingMode;
set => SetProperty(ref _couplingMode, 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>
@@ -162,19 +162,18 @@ namespace DeviceEditModule.ViewModels
#region
public ICommand QueryIdentityCommand { get; }
public ICommand ResetDeviceCommand { get; }
public ICommand QueryAllMeasureCommand { get; }
public ICommand SetVoltageRangeCommand { get; }
public ICommand SetCurrentRangeCommand { get; }
public ICommand SetCouplingModeCommand { 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; }
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
@@ -182,26 +181,34 @@ namespace DeviceEditModule.ViewModels
{
_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} 电压量程已设为 {VoltageRange} V"); }));
SetCurrentRangeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(Channel, CurrentRange, Ct()); AppendLog($"通道 {Channel} 电流量程已设为 {CurrentRange} A"); }));
SetCouplingModeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(Channel, CouplingMode, Ct()); AppendLog($"通道 {Channel} 耦合模式已设为 {CouplingMode}"); }));
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("屏幕触摸已锁定"); }));
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}"); }));
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 () =>
{
MeasuredVoltage = await _device!.(Channel, Ct());
MeasuredCurrent = await _device!.(Channel, Ct());
MeasuredPower = await _device!.(Channel, Ct());
MeasuredFrequency = await _device!.(Channel, Ct());
MeasuredPowerFactor = await _device!.(Channel, Ct());
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}");
}));
@@ -235,8 +242,8 @@ namespace DeviceEditModule.ViewModels
}
}
_device = found;
DeviceName = foundName ?? "SPAW7000 (未找到)";
_device = found;
DeviceName = foundName ?? "SPAW7000 (未找到)";
IsConnected = _device?.IsConnected ?? false;
AppendLog(found != null
@@ -256,6 +263,41 @@ namespace DeviceEditModule.ViewModels
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)
@@ -300,4 +342,4 @@ namespace DeviceEditModule.ViewModels
_cts?.Dispose();
}
}
}
}

View File

@@ -5,6 +5,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:prism="http://prismlibrary.com/"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare"
mc:Ignorable="d"
prism:ViewModelLocator.AutoWireViewModel="True"
@@ -92,38 +93,44 @@
<ComboBox Width="80" Height="32" Margin="4,0"
SelectedItem="{Binding Channel}"
VerticalContentAlignment="Center" FontSize="12">
<ComboBoxItem Content="1"/>
<ComboBoxItem Content="2"/>
<ComboBoxItem Content="3"/>
<ComboBoxItem Content="4"/>
<sys:Int32>1</sys:Int32>
<sys:Int32>2</sys:Int32>
<sys:Int32>3</sys:Int32>
<sys:Int32>4</sys:Int32>
<sys:Int32>5</sys:Int32>
<sys:Int32>6</sys:Int32>
<sys:Int32>7</sys:Int32>
<sys:Int32>8</sys:Int32>
</ComboBox>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="电压量程 (V)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
Text="{Binding VoltageRange, UpdateSourceTrigger=PropertyChanged}"/>
<ComboBox Width="100" Height="32" Margin="4,0"
ItemsSource="{Binding VoltageRangeOptions}"
SelectedItem="{Binding VoltageRange}"
VerticalContentAlignment="Center" FontSize="12"/>
<Button Content="设置" Command="{Binding SetVoltageRangeCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="电流量程 (A)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
Text="{Binding CurrentRange, UpdateSourceTrigger=PropertyChanged}"/>
<ComboBox Width="100" Height="32" Margin="4,0"
ItemsSource="{Binding CurrentRangeOptions}"
SelectedItem="{Binding CurrentRange}"
VerticalContentAlignment="Center" FontSize="12"/>
<Button Content="设置" Command="{Binding SetCurrentRangeCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<!--<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="耦合模式" Style="{StaticResource ParamLabel}"/>
<ComboBox Width="90" Height="32" Margin="4,0"
SelectedItem="{Binding CouplingMode}"
ItemsSource="{Binding InputMeasureModeOptions}"
VerticalContentAlignment="Center" FontSize="12">
<ComboBoxItem Content="AC"/>
<ComboBoxItem Content="DC"/>
<ComboBoxItem Content="ACDC"/>
</ComboBox>
<Button Content="设置" Command="{Binding SetCouplingModeCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
</StackPanel>-->
</StackPanel>
</GroupBox>
@@ -136,8 +143,8 @@
<ComboBox Width="80" Height="32" Margin="4,0"
SelectedItem="{Binding Resolution}"
VerticalContentAlignment="Center" FontSize="12">
<ComboBoxItem Content="5"/>
<ComboBoxItem Content="6"/>
<sys:Int32>5</sys:Int32>
<sys:Int32>6</sys:Int32>
</ComboBox>
<Button Content="设置" Command="{Binding SetResolutionCommand}"
Style="{StaticResource CmdBtn}"/>
@@ -179,12 +186,12 @@
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="型号" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource MeasureBox}" Width="120"
<TextBox Style="{StaticResource MeasureBox}" Width="281"
Text="{Binding DeviceModel, Mode=OneWay}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="序列号" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource MeasureBox}" Width="160"
<TextBox Style="{StaticResource MeasureBox}" Width="281"
Text="{Binding DeviceSerial, Mode=OneWay}"/>
</StackPanel>
</StackPanel>