查询返回数据转double
This commit is contained in:
@@ -208,9 +208,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); //[cite: 1]
|
||||
string response = await WriteReadAsync($":MEASure:VOLTage?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return double.TryParse(response, out double result) ? result : double.NaN;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -219,9 +220,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); //[cite: 1]
|
||||
string response = await WriteReadAsync($":MEASure:CURRent?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return double.TryParse(response, out double result) ? result : double.NaN;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -230,9 +232,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); //[cite: 1]
|
||||
string response = await WriteReadAsync($":MEASure:POWer?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return double.TryParse(response, out double result) ? result : double.NaN;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -240,9 +243,10 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>视在功率测量值 (单位: VA)</returns>
|
||||
public virtual async Task<string> 查询视在功率(CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询视在功率(CancellationToken ct = default)
|
||||
{
|
||||
return await WriteReadAsync($":MEASure:POWer:APParent?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1]
|
||||
string response = await WriteReadAsync($":MEASure:POWer:APParent?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return double.TryParse(response, out double result) ? result : double.NaN;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -250,9 +254,10 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>实际频率测量值 (单位: Hz)</returns>
|
||||
public virtual async Task<string> 查询实际频率(CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询实际频率(CancellationToken ct = default)
|
||||
{
|
||||
return await WriteReadAsync($":MEASure:FREQuency?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1]
|
||||
string response = await WriteReadAsync($":MEASure:FREQuency?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return double.TryParse(response, out double result) ? result : double.NaN;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -260,11 +265,11 @@ namespace DeviceCommand.Devices
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>功率因数测量值</returns>
|
||||
public virtual async Task<string> 查询功率因数(CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询功率因数(CancellationToken ct = default)
|
||||
{
|
||||
return await WriteReadAsync($":MEASure:POWer:PFACtor?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1]
|
||||
string response = await WriteReadAsync($":MEASure:POWer:PFACtor?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return double.TryParse(response, out double result) ? result : double.NaN;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 5. 保护参数设置与系统监控
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -212,10 +212,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)
|
||||
{
|
||||
// 修正:去除前缀冒号,改用标准 MEAS:CURR?
|
||||
return await WriteReadAsync($"MEAS:CURR?{SCPIDelimiter}", SCPIDelimiter, ct);
|
||||
string response = await WriteReadAsync($"MEAS:CURR?{SCPIDelimiter}", SCPIDelimiter, ct);
|
||||
return double.TryParse(response, out double result) ? result : double.NaN;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -224,10 +224,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)
|
||||
{
|
||||
// 修正:去除前缀冒号,改用标准 MEAS:POW?
|
||||
return await WriteReadAsync($"MEAS:POW?{SCPIDelimiter}", SCPIDelimiter, ct);
|
||||
string response = await WriteReadAsync($"MEAS:POW?{SCPIDelimiter}", SCPIDelimiter, ct);
|
||||
return double.TryParse(response, out double result) ? result : double.NaN;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -236,10 +236,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)
|
||||
{
|
||||
// 修正:去除前缀冒号,改用标准 MEAS:VOLT?
|
||||
return await WriteReadAsync($"MEAS:VOLT?{SCPIDelimiter}", SCPIDelimiter, ct);
|
||||
string response = await WriteReadAsync($"MEAS:VOLT?{SCPIDelimiter}", SCPIDelimiter, ct);
|
||||
return double.TryParse(response, out double result) ? result : double.NaN;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -253,16 +253,15 @@ namespace DeviceCommand.Devices
|
||||
return await WriteReadAsync($"MEAS:VAP?{SCPIDelimiter}", SCPIDelimiter, ct);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 3.5.5 查询通道输出端子上测得的输出时间长度
|
||||
/// </summary>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>输出时间长度</returns>
|
||||
public virtual async Task<string> 查询总输出时间长度(CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询总输出时间长度(CancellationToken ct = default)
|
||||
{
|
||||
// 修正:去除前缀冒号
|
||||
return await WriteReadAsync($"MEAS:TIME:OUTP?{SCPIDelimiter}", SCPIDelimiter, ct);
|
||||
string response = await WriteReadAsync($"MEAS:TIME:OUTP?{SCPIDelimiter}", SCPIDelimiter, ct);
|
||||
return double.TryParse(response, out double result) ? result : double.NaN;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -435,6 +434,7 @@ namespace DeviceCommand.Devices
|
||||
string cmd = string.Format(CultureInfo.InvariantCulture, "VOLT:LIM {0:F3}{1}", 电压, SCPIDelimiter);
|
||||
await SendAsync(cmd, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 3.7.2.9 清除电压上限限制(恢复为设备物理允许的最大电压值)
|
||||
/// </summary>
|
||||
@@ -444,6 +444,7 @@ namespace DeviceCommand.Devices
|
||||
// 发送 MAXimum 设为最大值以达到"清除限制"的效果
|
||||
await SendAsync($"VOLT:LIMIT MAX{SCPIDelimiter}", ct); // 对应手册 3.7.2.8
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 3.7.3.1 设置输出功率值 (W)
|
||||
/// </summary>
|
||||
@@ -548,4 +549,4 @@ namespace DeviceCommand.Devices
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ using Model.Models;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -22,6 +23,29 @@ namespace DeviceCommand.Devices
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从示波器返回的 SCPI 响应字符串中提取数值部分并转换为 double。
|
||||
/// 响应格式示例: "C1:PAVA PKPK,2.48E-03V" → 按逗号分割后提取 2.48E-03
|
||||
/// </summary>
|
||||
/// <param name="raw">示波器返回的原始字符串</param>
|
||||
/// <returns>提取到的数值,解析失败时返回 0</returns>
|
||||
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 1. IEEE 488.2 公共命令
|
||||
|
||||
/// <summary>
|
||||
@@ -175,9 +199,10 @@ namespace DeviceCommand.Devices
|
||||
/// <param name="channel">通道号 (1 - 4)</param>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>当前电压档位值 (Volts/Div)</returns>
|
||||
public virtual async Task<string> 查询通道电压档位(int channel, CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询通道电压档位(int channel, CancellationToken ct = default)
|
||||
{
|
||||
return await WriteReadAsync($"C{channel}:VDIV?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
string res = await WriteReadAsync($"C{channel}:VDIV?{ScpiDelimiter}", ScpiDelimiter, ct);
|
||||
return ExtractDouble(res);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -278,11 +303,12 @@ namespace DeviceCommand.Devices
|
||||
/// <param name="channel">通道号 (1 - 4)</param>
|
||||
/// <param name="paramName">测量参数名称 (如 "PKPK", "FREQ", "RMS" 等)</param>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>测量参数的实时数值字符串</returns>
|
||||
public virtual async Task<string> 查询通道测量项参数(int channel, string paramName, CancellationToken ct = default)
|
||||
/// <returns>测量参数的实时数值 (double)</returns>
|
||||
public virtual async Task<double> 查询通道测量项参数(int channel, string paramName, CancellationToken ct = default)
|
||||
{
|
||||
string query = string.Format(CultureInfo.InvariantCulture, "C{0}:PAVA? {1}{2}", channel, paramName.ToUpper(), ScpiDelimiter);
|
||||
return await WriteReadAsync(query, ScpiDelimiter, ct);
|
||||
string res = await WriteReadAsync(query, ScpiDelimiter, ct);
|
||||
return ExtractDouble(res);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -291,7 +317,7 @@ namespace DeviceCommand.Devices
|
||||
/// <param name="channel">通道号 (1 - 4)</param>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>电压峰峰值 (Vpp)</returns>
|
||||
public virtual async Task<string> 查询实际电压峰峰值(int channel, CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询实际电压峰峰值(int channel, CancellationToken ct = default)
|
||||
{
|
||||
return await 查询通道测量项参数(channel, "PKPK", ct);
|
||||
}
|
||||
@@ -302,7 +328,7 @@ namespace DeviceCommand.Devices
|
||||
/// <param name="channel">通道号 (1 - 4)</param>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>频率值 (单位: Hz)</returns>
|
||||
public virtual async Task<string> 查询实际频率(int channel, CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询实际频率(int channel, CancellationToken ct = default)
|
||||
{
|
||||
return await 查询通道测量项参数(channel, "FREQ", ct);
|
||||
}
|
||||
@@ -313,7 +339,7 @@ namespace DeviceCommand.Devices
|
||||
/// <param name="channel">通道号 (1 - 4)</param>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>真均方根电压值 (Vrms)</returns>
|
||||
public virtual async Task<string> 查询实际电压均方根(int channel, CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询实际电压均方根(int channel, CancellationToken ct = default)
|
||||
{
|
||||
return await 查询通道测量项参数(channel, "RMS", ct);
|
||||
}
|
||||
|
||||
@@ -22,6 +22,21 @@ namespace DeviceCommand.Devices
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 辅助方法:解析科学计数法字符串为普通 double。解析失败返回 double.NaN。
|
||||
/// </summary>
|
||||
private double ParseScientificResponse(string response)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(response)) return double.NaN;
|
||||
|
||||
// 使用 NumberStyles.Float 允许原生的科学计数法解析 (例如 "1.23E+04")
|
||||
if (double.TryParse(response.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out double result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return double.NaN;
|
||||
}
|
||||
|
||||
#region 1. IEEE 488.2 公共命令
|
||||
|
||||
/// <summary>
|
||||
@@ -79,7 +94,7 @@ namespace DeviceCommand.Devices
|
||||
/// <param name="channel">通道号/单元号</param>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>实时 RMS 电压值 (单位: V)</returns>
|
||||
public virtual async Task<string> 查询实际电压(int channel, CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询实际电压(int channel, CancellationToken ct = default)
|
||||
{
|
||||
// 1. 先配置 ITEM1 为指定通道的电压有效值 (URMS)
|
||||
string setItem = string.Format(CultureInfo.InvariantCulture, ":NUMeric:NORMal:ITEM1 URMS,{0}{1}", channel, ScpiDelimiter);
|
||||
@@ -87,7 +102,8 @@ namespace DeviceCommand.Devices
|
||||
|
||||
// 2. 再读取 ITEM1 的值
|
||||
string query = string.Format(CultureInfo.InvariantCulture, ":NUMeric:NORMal:VALue? 1{0}", ScpiDelimiter);
|
||||
return await WriteReadAsync(query, ScpiDelimiter, ct);
|
||||
string response = await WriteReadAsync(query, ScpiDelimiter, ct);
|
||||
return ParseScientificResponse(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -96,7 +112,7 @@ namespace DeviceCommand.Devices
|
||||
/// <param name="channel">通道号/单元号</param>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>实时 RMS 电流值 (单位: A)</returns>
|
||||
public virtual async Task<string> 查询实际电流(int channel, CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询实际电流(int channel, CancellationToken ct = default)
|
||||
{
|
||||
// 1. 配置 ITEM1 为指定通道的电流有效值 (IRMS)
|
||||
string setItem = string.Format(CultureInfo.InvariantCulture, ":NUMeric:NORMal:ITEM1 IRMS,{0}{1}", channel, ScpiDelimiter);
|
||||
@@ -104,7 +120,8 @@ namespace DeviceCommand.Devices
|
||||
|
||||
// 2. 读取 ITEM1 的值
|
||||
string query = string.Format(CultureInfo.InvariantCulture, ":NUMeric:NORMal:VALue? 1{0}", ScpiDelimiter);
|
||||
return await WriteReadAsync(query, ScpiDelimiter, ct);
|
||||
string response = await WriteReadAsync(query, ScpiDelimiter, ct);
|
||||
return ParseScientificResponse(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -113,7 +130,7 @@ namespace DeviceCommand.Devices
|
||||
/// <param name="channel">通道号/单元号</param>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>实时有功功率值 (单位: W)</returns>
|
||||
public virtual async Task<string> 查询实际功率(int channel, CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询实际功率(int channel, CancellationToken ct = default)
|
||||
{
|
||||
// 1. 配置 ITEM1 为指定通道的有功功率 (P)
|
||||
string setItem = string.Format(CultureInfo.InvariantCulture, ":NUMeric:NORMal:ITEM1 P,{0}{1}", channel, ScpiDelimiter);
|
||||
@@ -121,7 +138,8 @@ namespace DeviceCommand.Devices
|
||||
|
||||
// 2. 读取 ITEM1 的值
|
||||
string query = string.Format(CultureInfo.InvariantCulture, ":NUMeric:NORMal:VALue? 1{0}", ScpiDelimiter);
|
||||
return await WriteReadAsync(query, ScpiDelimiter, ct);
|
||||
string response = await WriteReadAsync(query, ScpiDelimiter, ct);
|
||||
return ParseScientificResponse(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -130,7 +148,7 @@ namespace DeviceCommand.Devices
|
||||
/// <param name="channel">通道号/单元号</param>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>实时频率值 (单位: Hz)</returns>
|
||||
public virtual async Task<string> 查询频率(int channel, CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询频率(int channel, CancellationToken ct = default)
|
||||
{
|
||||
// 1. 配置 ITEM1 为指定通道的电压频率 (FU)
|
||||
string setItem = string.Format(CultureInfo.InvariantCulture, ":NUMeric:NORMal:ITEM1 FU,{0}{1}", channel, ScpiDelimiter);
|
||||
@@ -138,7 +156,8 @@ namespace DeviceCommand.Devices
|
||||
|
||||
// 2. 读取 ITEM1 的值
|
||||
string query = string.Format(CultureInfo.InvariantCulture, ":NUMeric:NORMal:VALue? 1{0}", ScpiDelimiter);
|
||||
return await WriteReadAsync(query, ScpiDelimiter, ct);
|
||||
string response = await WriteReadAsync(query, ScpiDelimiter, ct);
|
||||
return ParseScientificResponse(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -147,7 +166,7 @@ namespace DeviceCommand.Devices
|
||||
/// <param name="channel">通道号/单元号</param>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <returns>功率因数值</returns>
|
||||
public virtual async Task<string> 查询功率因数(int channel, CancellationToken ct = default)
|
||||
public virtual async Task<double> 查询功率因数(int channel, CancellationToken ct = default)
|
||||
{
|
||||
// 1. 配置 ITEM1 为指定通道的功率因数 (LAMBda)
|
||||
string setItem = string.Format(CultureInfo.InvariantCulture, ":NUMeric:NORMal:ITEM1 LAMBda,{0}{1}", channel, ScpiDelimiter);
|
||||
@@ -155,7 +174,8 @@ namespace DeviceCommand.Devices
|
||||
|
||||
// 2. 读取 ITEM1 的值
|
||||
string query = string.Format(CultureInfo.InvariantCulture, ":NUMeric:NORMal:VALue? 1{0}", ScpiDelimiter);
|
||||
return await WriteReadAsync(query, ScpiDelimiter, ct);
|
||||
string response = await WriteReadAsync(query, ScpiDelimiter, ct);
|
||||
return ParseScientificResponse(response);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -287,4 +307,4 @@ namespace DeviceCommand.Devices
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user