查询返回数据转double
This commit is contained in:
@@ -3,7 +3,6 @@ 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;
|
||||
@@ -113,22 +112,22 @@ namespace DeviceEditModule.ViewModels
|
||||
|
||||
#region 测量结果属性
|
||||
|
||||
private string _measuredVpp = "—";
|
||||
public string MeasuredVpp
|
||||
private double _measuredVpp;
|
||||
public double MeasuredVpp
|
||||
{
|
||||
get => _measuredVpp;
|
||||
set => SetProperty(ref _measuredVpp, value);
|
||||
}
|
||||
|
||||
private string _measuredFrequency = "—";
|
||||
public string MeasuredFrequency
|
||||
private double _measuredFrequency;
|
||||
public double MeasuredFrequency
|
||||
{
|
||||
get => _measuredFrequency;
|
||||
set => SetProperty(ref _measuredFrequency, value);
|
||||
}
|
||||
|
||||
private string _measuredRms = "—";
|
||||
public string MeasuredRms
|
||||
private double _measuredRms;
|
||||
public double MeasuredRms
|
||||
{
|
||||
get => _measuredRms;
|
||||
set => SetProperty(ref _measuredRms, value);
|
||||
@@ -207,40 +206,30 @@ namespace DeviceEditModule.ViewModels
|
||||
|
||||
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());
|
||||
// 设备层已自动提取纯数值并返回 double
|
||||
MeasuredVpp = await _device!.查询实际电压峰峰值(Channel, Ct());
|
||||
MeasuredFrequency = await _device!.查询实际频率(Channel, Ct());
|
||||
MeasuredRms = 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");
|
||||
AppendLog($"C{Channel} 测量结果 → Vpp:{MeasuredVpp:0.######}V Freq:{MeasuredFrequency:0.######}Hz RMS:{MeasuredRms:0.######}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()})");
|
||||
MeasuredVpp = await _device!.查询实际电压峰峰值(Channel, Ct());
|
||||
AppendLog($"C{Channel} Vpp: {MeasuredVpp:0.######} V");
|
||||
}));
|
||||
|
||||
QueryFrequencyCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
string raw = await _device!.查询实际频率(Channel, Ct());
|
||||
MeasuredFrequency = ParseMeasurement(raw);
|
||||
AppendLog($"C{Channel} Freq: {MeasuredFrequency} (Raw: {raw.Trim()})");
|
||||
MeasuredFrequency = await _device!.查询实际频率(Channel, Ct());
|
||||
AppendLog($"C{Channel} Freq: {MeasuredFrequency:0.######} Hz");
|
||||
}));
|
||||
|
||||
QueryRmsCommand = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
string raw = await _device!.查询实际电压均方根(Channel, Ct());
|
||||
MeasuredRms = ParseMeasurement(raw);
|
||||
AppendLog($"C{Channel} RMS: {MeasuredRms} (Raw: {raw.Trim()})");
|
||||
MeasuredRms = await _device!.查询实际电压均方根(Channel, Ct());
|
||||
AppendLog($"C{Channel} RMS: {MeasuredRms:0.######} V");
|
||||
}));
|
||||
|
||||
Initialize();
|
||||
@@ -330,52 +319,6 @@ namespace DeviceEditModule.ViewModels
|
||||
: 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()
|
||||
|
||||
Reference in New Issue
Block a user