查询返回数据转double
This commit is contained in:
@@ -101,36 +101,36 @@ namespace DeviceEditModule.ViewModels
|
||||
|
||||
#region 测量结果属性
|
||||
|
||||
private string _measuredVoltage = "—";
|
||||
public string MeasuredVoltage
|
||||
private double _measuredVoltage;
|
||||
public double MeasuredVoltage
|
||||
{
|
||||
get => _measuredVoltage;
|
||||
set => SetProperty(ref _measuredVoltage, value);
|
||||
}
|
||||
|
||||
private string _measuredCurrent = "—";
|
||||
public string MeasuredCurrent
|
||||
private double _measuredCurrent;
|
||||
public double MeasuredCurrent
|
||||
{
|
||||
get => _measuredCurrent;
|
||||
set => SetProperty(ref _measuredCurrent, value);
|
||||
}
|
||||
|
||||
private string _measuredPower = "—";
|
||||
public string MeasuredPower
|
||||
private double _measuredPower;
|
||||
public double MeasuredPower
|
||||
{
|
||||
get => _measuredPower;
|
||||
set => SetProperty(ref _measuredPower, value);
|
||||
}
|
||||
|
||||
private string _measuredFrequency = "—";
|
||||
public string MeasuredFrequency
|
||||
private double _measuredFrequency;
|
||||
public double MeasuredFrequency
|
||||
{
|
||||
get => _measuredFrequency;
|
||||
set => SetProperty(ref _measuredFrequency, value);
|
||||
}
|
||||
|
||||
private string _measuredPowerFactor = "—";
|
||||
public string MeasuredPowerFactor
|
||||
private double _measuredPowerFactor;
|
||||
public double MeasuredPowerFactor
|
||||
{
|
||||
get => _measuredPowerFactor;
|
||||
set => SetProperty(ref _measuredPowerFactor, value);
|
||||
@@ -196,20 +196,14 @@ namespace DeviceEditModule.ViewModels
|
||||
// 只修改这里:保持你原本的 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());
|
||||
// 设备层已返回 double 数值
|
||||
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());
|
||||
|
||||
// 转换科学计数法格式,如果失败会自动保持原样
|
||||
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}");
|
||||
AppendLog($"CH{Channel} 测量 → U:{MeasuredVoltage:F2}V I:{MeasuredCurrent:F3}A P:{MeasuredPower:F2}W F:{MeasuredFrequency:F2}Hz PF:{MeasuredPowerFactor:F3}");
|
||||
}));
|
||||
|
||||
Initialize();
|
||||
@@ -263,41 +257,6 @@ 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)
|
||||
|
||||
Reference in New Issue
Block a user