查询返回数据转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;
|
||||
|
||||
@@ -21,6 +22,23 @@ namespace DeviceCommand.Devices
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 辅助方法:从带有单位的字符串中提取数值并转换为 double。
|
||||
/// 例如将 "12.34A", "5.00 V", "1.2e-3 W" 转换为纯数字。解析失败返回 double.NaN。
|
||||
/// </summary>
|
||||
private double ParseResponseWithUnit(string response)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(response)) return double.NaN;
|
||||
|
||||
// 使用正则提取数字部分(支持正负号、小数点、科学计数法 e/E)
|
||||
var match = Regex.Match(response, @"[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?");
|
||||
if (match.Success && double.TryParse(match.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out double result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return double.NaN;
|
||||
}
|
||||
|
||||
#region 3.1. IEEE 488.2 公共命令
|
||||
|
||||
/// <summary>
|
||||
@@ -38,7 +56,7 @@ namespace DeviceCommand.Devices
|
||||
/// <returns>错误代码和描述信息</returns>
|
||||
public virtual async Task<string> 查询错误信息(CancellationToken ct = default)
|
||||
{
|
||||
return await WriteReadAsync($":SYSTem:ERRor?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1]
|
||||
return await WriteReadAsync($":SYSTem:ERRor?{ScpiDelimiter}", ScpiDelimiter, ct); //
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -190,9 +208,10 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>设定的拉载电流值 (单位: A)</returns>
|
||||
public virtual async Task<string> 查询恒电流CC设定(CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询恒电流CC设定(CancellationToken ct = default)
|
||||
{
|
||||
return await WriteReadAsync($"STATic:CC:HIGH:LEVel?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
string response = await WriteReadAsync($"STATic:CC:HIGH:LEVel?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -211,9 +230,10 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>设定的拉载电压值 (单位: V)</returns>
|
||||
public virtual async Task<string> 查询恒电压CV设定(CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询恒电压CV设定(CancellationToken ct = default)
|
||||
{
|
||||
return await WriteReadAsync($"STATic:CV:HIGH:LEVel?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
string response = await WriteReadAsync($"STATic:CV:HIGH:LEVel?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -232,9 +252,10 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>设定的拉载功率值 (单位: W)</returns>
|
||||
public virtual async Task<string> 查询恒功率CP设定(CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询恒功率CP设定(CancellationToken ct = default)
|
||||
{
|
||||
return await WriteReadAsync($"STATic:CP:HIGH:LEVel?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
string response = await WriteReadAsync($"STATic:CP:HIGH:LEVel?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -253,9 +274,10 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>设定的拉载电阻值 (单位: Ω)</returns>
|
||||
public virtual async Task<string> 查询恒电阻CR设定(CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询恒电阻CR设定(CancellationToken ct = default)
|
||||
{
|
||||
return await WriteReadAsync($"STATic:CR:HIGH:LEVel?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
string response = await WriteReadAsync($"STATic:CR:HIGH:LEVel?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -268,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 response = await WriteReadAsync($"MEASure:VOLTage?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -279,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 response = await WriteReadAsync($"MEASure:CURRent?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -290,9 +314,10 @@ namespace DeviceCommand.Devices
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>实时测量功率值 (单位: W)</returns>
|
||||
[Monitorable("高压直流负载功率")]
|
||||
public virtual async Task<string> 查询实际功率(CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询实际功率(CancellationToken ct = default)
|
||||
{
|
||||
return await WriteReadAsync($"MEASure:POWer?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
string response = await WriteReadAsync($"MEASure:POWer?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -650,16 +675,22 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>当前放电已持续时间 (单位: s)</returns>
|
||||
public virtual async Task<string> 查询放电实时时间(CancellationToken ct = default)
|
||||
=> await WriteReadAsync($"DISCHarge:ECHO:TIMe?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
public virtual async Task<double> 查询放电实时时间(CancellationToken ct = default)
|
||||
{
|
||||
string response = await WriteReadAsync($"DISCHarge:ECHO:TIMe?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询放电实时容量
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>当前放电已输出的容量 (单位: Ah)</returns>
|
||||
public virtual async Task<string> 查询放电实时容量(CancellationToken ct = default)
|
||||
=> await WriteReadAsync($"DISCHarge:ECHo:CAPacity?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
public virtual async Task<double> 查询放电实时容量(CancellationToken ct = default)
|
||||
{
|
||||
string response = await WriteReadAsync($"DISCHarge:ECHo:CAPacity?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置充电电流
|
||||
@@ -734,8 +765,11 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>OCP测试触发时的电流值 (单位: A)</returns>
|
||||
public virtual async Task<string> 读取OCP结果电流(CancellationToken ct = default)
|
||||
=> await WriteReadAsync($"OCP:RCURrent?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
public virtual async Task<double> 读取OCP结果电流(CancellationToken ct = default)
|
||||
{
|
||||
string response = await WriteReadAsync($"OCP:RCURrent?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置OPP初始功率
|
||||
@@ -782,8 +816,11 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>OPP测试触发时的功率值 (单位: W)</returns>
|
||||
public virtual async Task<string> 读取OPP结果功率(CancellationToken ct = default)
|
||||
=> await WriteReadAsync($"OPP:RPOWer?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
public virtual async Task<double> 读取OPP结果功率(CancellationToken ct = default)
|
||||
{
|
||||
string response = await WriteReadAsync($"OPP:RPOWer?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -842,8 +879,11 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>当前扫频运行频率 (单位: Hz)</returns>
|
||||
public virtual async Task<string> 读取扫频当前运行频率(CancellationToken ct = default)
|
||||
=> await WriteReadAsync($"SWEEP:CFRequency?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
public virtual async Task<double> 读取扫频当前运行频率(CancellationToken ct = default)
|
||||
{
|
||||
string response = await WriteReadAsync($"SWEEP:CFRequency?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取扫频测试结果
|
||||
@@ -950,8 +990,11 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>ESR测试结果 (单位: Ω)</returns>
|
||||
public virtual async Task<string> 读取ESR结果(CancellationToken ct = default)
|
||||
=> await WriteReadAsync($"ESR:RESult?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
public virtual async Task<double> 读取ESR结果(CancellationToken ct = default)
|
||||
{
|
||||
string response = await WriteReadAsync($"ESR:RESult?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -994,40 +1037,55 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>最大功率值 (单位: W)</returns>
|
||||
public virtual async Task<string> 读取MPPT最大功率(CancellationToken ct = default)
|
||||
=> await WriteReadAsync($"MPP:MPOWer?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
public virtual async Task<double> 读取MPPT最大功率(CancellationToken ct = default)
|
||||
{
|
||||
string response = await WriteReadAsync($"MPP:MPOWer?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取MPPT最大功率电压
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>最大功率点对应电压 (单位: V)</returns>
|
||||
public virtual async Task<string> 读取MPPT最大功率电压(CancellationToken ct = default)
|
||||
=> await WriteReadAsync($"MPP:MVOLtage?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
public virtual async Task<double> 读取MPPT最大功率电压(CancellationToken ct = default)
|
||||
{
|
||||
string response = await WriteReadAsync($"MPP:MVOLtage?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取MPPT最大功率电流
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>最大功率点对应电流 (单位: A)</returns>
|
||||
public virtual async Task<string> 读取MPPT最大功率电流(CancellationToken ct = default)
|
||||
=> await WriteReadAsync($"MPP:MCURrent?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
public virtual async Task<double> 读取MPPT最大功率电流(CancellationToken ct = default)
|
||||
{
|
||||
string response = await WriteReadAsync($"MPP:MCURrent?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取MPPT开路电压
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>开路电压值 (单位: V)</returns>
|
||||
public virtual async Task<string> 读取MPPT开路电压(CancellationToken ct = default)
|
||||
=> await WriteReadAsync($"MPP:OVOLtage?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
public virtual async Task<double> 读取MPPT开路电压(CancellationToken ct = default)
|
||||
{
|
||||
string response = await WriteReadAsync($"MPP:OVOLtage?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取MPPT短路电流
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>短路电流值 (单位: A)</returns>
|
||||
public virtual async Task<string> 读取MPPT短路电流(CancellationToken ct = default)
|
||||
=> await WriteReadAsync($"MPP:SCURrent?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
public virtual async Task<double> 读取MPPT短路电流(CancellationToken ct = default)
|
||||
{
|
||||
string response = await WriteReadAsync($"MPP:SCURrent?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ParseResponseWithUnit(response);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
Reference in New Issue
Block a user