查询返回数据转double
This commit is contained in:
@@ -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