249 lines
9.7 KiB
C#
249 lines
9.7 KiB
C#
using Common.Attributes;
|
||
using DeviceCommand.Base;
|
||
using System;
|
||
using System.Globalization;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace DeviceCommand.Device
|
||
{
|
||
/// <summary>
|
||
/// Sequoia SQ0030G1D 控制类(SCPI 指令封装)
|
||
/// </summary>
|
||
[BOBCommand]
|
||
public class SQ0030G1D : Tcp
|
||
{
|
||
public SQ0030G1D(string IpAddress, int port, int SendTimeout, int ReceiveTimeout)
|
||
{
|
||
ConfigureDevice(IpAddress, port, SendTimeout, ReceiveTimeout);
|
||
ConnectAsync();
|
||
}
|
||
public SQ0030G1D()
|
||
{
|
||
ConnectAsync();
|
||
}
|
||
#region ========== 通用 SCPI 封装 ==========
|
||
|
||
/// <summary>
|
||
/// 发送 SCPI 指令(无返回)
|
||
/// </summary>
|
||
protected async Task WriteAsync(string cmd, CancellationToken ct = default)
|
||
{
|
||
await SendAsync(cmd + "\n", ct);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发送 SCPI 指令并读取返回
|
||
/// </summary>
|
||
protected async Task<string> QueryAsync(string cmd, CancellationToken ct = default)
|
||
{
|
||
await SendAsync(cmd + "\n", ct);
|
||
return await ReadAsync(ct: ct);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region ========== 一、设备基础控制 ==========
|
||
|
||
/// <summary>
|
||
/// 查询设备标识 (*IDN?)
|
||
/// </summary>
|
||
public async Task<string> GetIdAsync(CancellationToken ct = default)
|
||
=> await QueryAsync("*IDN?", ct);
|
||
|
||
/// <summary>
|
||
/// 恢复出厂设置(*RST)
|
||
/// </summary>
|
||
public async Task ResetAsync(CancellationToken ct = default)
|
||
=> await WriteAsync("*RST", ct);
|
||
|
||
/// <summary>
|
||
/// 保存参数到内存 (*SAV n)
|
||
/// </summary>
|
||
public async Task SavePresetAsync(int index, CancellationToken ct = default)
|
||
=> await WriteAsync($"*SAV {index}", ct);
|
||
|
||
/// <summary>
|
||
/// 从内存调用参数 (*RCL n)
|
||
/// </summary>
|
||
public async Task RecallPresetAsync(int index, CancellationToken ct = default)
|
||
=> await WriteAsync($"*RCL {index}", ct);
|
||
|
||
/// <summary>
|
||
/// 切换到本地模式(SYST:LOC)
|
||
/// </summary>
|
||
public async Task SetLocalAsync(CancellationToken ct = default)
|
||
=> await WriteAsync("SYST:LOC", ct);
|
||
|
||
/// <summary>
|
||
/// 切换到远程模式(SYST:REM)
|
||
/// </summary>
|
||
public async Task SetRemoteAsync(CancellationToken ct = default)
|
||
=> await WriteAsync("SYST:REM", ct);
|
||
|
||
/// <summary>
|
||
/// 控制输出开关(ON/OFF)
|
||
/// </summary>
|
||
public async Task SetOutputAsync(bool on, CancellationToken ct = default)
|
||
=> await WriteAsync($"OUTP:STAT {(on ? "ON" : "OFF")}", ct);
|
||
|
||
/// <summary>
|
||
/// 查询输出状态(返回 true 表示 ON)
|
||
/// </summary>
|
||
public async Task<bool> GetOutputAsync(CancellationToken ct = default)
|
||
=> (await QueryAsync("OUTP:STAT?", ct)).Trim().ToUpper() == "ON";
|
||
|
||
#endregion
|
||
|
||
#region ========== 二、电压控制 ==========
|
||
|
||
public async Task SetVoltageAsync(double voltage, CancellationToken ct = default)
|
||
=> await WriteAsync($"VOLT {voltage}", ct);
|
||
|
||
public async Task<double> GetVoltageSettingAsync(CancellationToken ct = default)
|
||
=> double.Parse((await QueryAsync("VOLT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture);
|
||
|
||
public async Task SetVoltageOffsetAsync(double value, CancellationToken ct = default)
|
||
=> await WriteAsync($"VOLT:OFFS {value}", ct);
|
||
|
||
public async Task<double> GetVoltageOffsetAsync(CancellationToken ct = default)
|
||
=> double.Parse((await QueryAsync("VOLT:OFFS?", ct)).Replace("V", ""), CultureInfo.InvariantCulture);
|
||
|
||
public async Task SetVoltageMaxAsync(double value, CancellationToken ct = default)
|
||
=> await WriteAsync($"VOLT:MAX {value}", ct);
|
||
|
||
#endregion
|
||
|
||
#region ========== 三、电流控制 ==========
|
||
|
||
public async Task SetCurrentAsync(double current, CancellationToken ct = default)
|
||
=> await WriteAsync($"CURR {current}", ct);
|
||
|
||
public async Task<double> GetCurrentSettingAsync(CancellationToken ct = default)
|
||
=> double.Parse((await QueryAsync("CURR?", ct)).Replace("A", ""), CultureInfo.InvariantCulture);
|
||
|
||
public async Task SetRegenerativeLimitAsync(double value, CancellationToken ct = default)
|
||
=> await WriteAsync($"CURR:REG {value}", ct);
|
||
|
||
public async Task<double> GetRegenerativeLimitAsync(CancellationToken ct = default)
|
||
=> double.Parse((await QueryAsync("CURR:REG?", ct)).Replace("A", ""), CultureInfo.InvariantCulture);
|
||
|
||
#endregion
|
||
|
||
#region ========== 四、频率控制 ==========
|
||
|
||
public async Task SetFrequencyAsync(double freq, CancellationToken ct = default)
|
||
=> await WriteAsync($"FREQ {freq}", ct);
|
||
|
||
public async Task<double> GetFrequencyAsync(CancellationToken ct = default)
|
||
=> double.Parse((await QueryAsync("FREQ?", ct)).Replace("Hz", ""), CultureInfo.InvariantCulture);
|
||
|
||
#endregion
|
||
|
||
#region ========== 五、功率控制 ==========
|
||
|
||
public async Task SetPowerAsync(double power, CancellationToken ct = default)
|
||
=> await WriteAsync($"POW {power}", ct);
|
||
|
||
public async Task<double> GetPowerSettingAsync(CancellationToken ct = default)
|
||
=> double.Parse((await QueryAsync("POW?", ct)).Replace("W", ""), CultureInfo.InvariantCulture);
|
||
|
||
public async Task SetReactivePowerAsync(double var, CancellationToken ct = default)
|
||
=> await WriteAsync($"POW:REAC {var}", ct);
|
||
|
||
#endregion
|
||
|
||
#region ========== 六、测量查询 ==========
|
||
|
||
public async Task<double> MeasureVoltageAsync(CancellationToken ct = default)
|
||
=> double.Parse((await QueryAsync("MEAS:VOLT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture);
|
||
|
||
public async Task<double> MeasureCurrentAsync(CancellationToken ct = default)
|
||
=> double.Parse((await QueryAsync("MEAS:CURR?", ct)).Replace("A", ""), CultureInfo.InvariantCulture);
|
||
|
||
public async Task<double> MeasurePowerAsync(CancellationToken ct = default)
|
||
=> double.Parse((await QueryAsync("MEAS:POW?", ct)).Replace("W", ""), CultureInfo.InvariantCulture);
|
||
|
||
/// <summary>
|
||
/// 同时测量电压、电流、功率
|
||
/// </summary>
|
||
public async Task<(double volt, double curr, double pow)> MeasureArrayAsync(CancellationToken ct = default)
|
||
{
|
||
string result = await QueryAsync("MEAS:ARR?", ct);
|
||
var parts = result.Split(',');
|
||
|
||
return (
|
||
double.Parse(parts[0].Replace("V", ""), CultureInfo.InvariantCulture),
|
||
double.Parse(parts[1].Replace("A", ""), CultureInfo.InvariantCulture),
|
||
double.Parse(parts[2].Replace("W", ""), CultureInfo.InvariantCulture)
|
||
);
|
||
}
|
||
|
||
public async Task<double> MeasureHarmonicAsync(int n, CancellationToken ct = default)
|
||
=> double.Parse((await QueryAsync($"MEAS:HARM? {n}", ct)).Replace("%", ""), CultureInfo.InvariantCulture);
|
||
|
||
#endregion
|
||
|
||
#region ========== 七、保护设定 ==========
|
||
|
||
public async Task SetOVPAsync(double v, CancellationToken ct = default)
|
||
=> await WriteAsync($"VOLT:PROT {v}", ct);
|
||
|
||
public async Task<double> GetOVPAsync(CancellationToken ct = default)
|
||
=> double.Parse((await QueryAsync("VOLT:PROT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture);
|
||
|
||
public async Task SetOCPAsync(double a, CancellationToken ct = default)
|
||
=> await WriteAsync($"CURR:PROT {a}", ct);
|
||
|
||
public async Task<double> GetOCPAsync(CancellationToken ct = default)
|
||
=> double.Parse((await QueryAsync("CURR:PROT?", ct)).Replace("A", ""), CultureInfo.InvariantCulture);
|
||
|
||
public async Task<bool> GetOTPStatusAsync(CancellationToken ct = default)
|
||
=> (await QueryAsync("SYST:PROT:OTP?", ct)).Trim().ToUpper() == "ON";
|
||
|
||
public async Task ClearProtectionAsync(CancellationToken ct = default)
|
||
=> await WriteAsync("SYST:PROT:CLE", ct);
|
||
|
||
#endregion
|
||
|
||
#region ========== 八、电子负载 ==========
|
||
|
||
public async Task SetLoadModeCurrentAsync(CancellationToken ct = default)
|
||
=> await WriteAsync("LOAD:MODE CURR", ct);
|
||
|
||
public async Task SetLoadModePowerAsync(CancellationToken ct = default)
|
||
=> await WriteAsync("LOAD:MODE POW", ct);
|
||
|
||
public async Task SetLoadModeRLCAsync(CancellationToken ct = default)
|
||
=> await WriteAsync("LOAD:MODE RLC", ct);
|
||
|
||
public async Task SetLoadRLCResistanceAsync(double res, CancellationToken ct = default)
|
||
=> await WriteAsync($"LOAD:RLC:RES {res}", ct);
|
||
|
||
#endregion
|
||
|
||
#region ========== 九、瞬态编程 ==========
|
||
|
||
public async Task SetTransientStateAsync(bool on, CancellationToken ct = default)
|
||
=> await WriteAsync($"TRAN:STAT {(on ? "ON" : "OFF")}", ct);
|
||
|
||
public async Task SetTransientStepVoltageAsync(int step, double volt, CancellationToken ct = default)
|
||
=> await WriteAsync($"TRAN:STEP {step}:VOLT {volt}", ct);
|
||
|
||
public async Task RunTransientAsync(CancellationToken ct = default)
|
||
=> await WriteAsync("TRAN:RUN", ct);
|
||
|
||
#endregion
|
||
|
||
#region ========== 十、多设备同步 ==========
|
||
|
||
public async Task SetSyncSourceExternalAsync(CancellationToken ct = default)
|
||
=> await WriteAsync("SYNC:SOUR EXT", ct);
|
||
|
||
public async Task<string> GetSyncStatusAsync(CancellationToken ct = default)
|
||
=> await QueryAsync("SYNC:STAT?", ct);
|
||
|
||
#endregion
|
||
}
|
||
}
|