查询返回数据转double
This commit is contained in:
@@ -3,6 +3,7 @@ using DeviceCommand.Base;
|
||||
using Model.Models;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -22,6 +23,25 @@ namespace DeviceCommand.Devices
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从设备返回的 SCPI 响应字符串中提取数值部分并转换为 double。
|
||||
/// 先按逗号分割取数据段,再提取数值。
|
||||
/// </summary>
|
||||
private static double ExtractDouble(string raw)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(raw)) return 0;
|
||||
|
||||
string dataPart = raw;
|
||||
int commaIdx = raw.LastIndexOf(',');
|
||||
if (commaIdx >= 0 && commaIdx < raw.Length - 1)
|
||||
dataPart = raw.Substring(commaIdx + 1);
|
||||
|
||||
var match = Regex.Match(dataPart, @"[+-]?(?:\d+\.?\d*|\.\d+)(?:[Ee][+-]?\d+)?");
|
||||
return match.Success && double.TryParse(match.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out double val)
|
||||
? val
|
||||
: 0;
|
||||
}
|
||||
|
||||
#region 3.1. IEEE 488.2 公共命令
|
||||
|
||||
/// <summary>
|
||||
@@ -270,9 +290,10 @@ namespace DeviceCommand.Devices
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>实时测量电压值 (单位: V)</returns>
|
||||
[Monitorable("高压直流电源电压")]
|
||||
public virtual async Task<string> 查询实际电压(CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询实际电压(CancellationToken ct = default)
|
||||
{
|
||||
return await WriteReadAsync($"MEASure:VOLTage?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
string res = await WriteReadAsync($"MEASure:VOLTage?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ExtractDouble(res);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -281,9 +302,10 @@ namespace DeviceCommand.Devices
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>实时测量电流值 (单位: A)</returns>
|
||||
[Monitorable("高压直流电源电流")]
|
||||
public virtual async Task<string> 查询实际电流(CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询实际电流(CancellationToken ct = default)
|
||||
{
|
||||
return await WriteReadAsync($"MEASure:CURRent?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
string res = await WriteReadAsync($"MEASure:CURRent?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ExtractDouble(res);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -291,10 +313,11 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>实时测量功率值 (单位: W)</returns>
|
||||
[Monitorable("高压直流电源功率")]
|
||||
public virtual async Task<string> 查询实际功率(CancellationToken ct = default)
|
||||
[Monitorable("高压直流电源功率")]
|
||||
public virtual async Task<double> 查询实际功率(CancellationToken ct = default)
|
||||
{
|
||||
return await WriteReadAsync($"MEASure:POWer?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
string res = await WriteReadAsync($"MEASure:POWer?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ExtractDouble(res);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user