using Common.Attributes;
using DeviceCommand.Base;
using Model.Models;
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;

namespace DeviceCommand.Devices
{
    [ADPCommand]
    public class N69200 : Tcp
    {
        // 手册第 15 页 2.2.4 明确规定：命令结束符为换行符 (ASCII 字符 LF，即 \n)
        private const string ScpiDelimiter = "\n";

        /// <summary>
        /// 构造函数：传入 <see cref="TcpConfig"/> 一次性初始化 N69200 电子负载通信参数。
        /// </summary>
        public N69200(TcpConfig config) : base(config)
        {
        }

        #region 3.1. IEEE 488.2 公共命令

        /// <summary>
        /// 清除状态命令。清除标准事件状态寄存器和错误队列 (*CLS)
        /// </summary>
        public virtual async Task 清除错误队列和状态字节(CancellationToken ct = default)
        {
            await SendAsync($"*CLS{ScpiDelimiter}", ct);
        }
        /// <summary>
        /// 查询错误信息
        /// </summary>
        public virtual async Task<string> 查询错误信息(CancellationToken ct = default)
        {
            return await WriteReadAsync($":SYSTem:ERRor?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1]
        }

        /// <summary>
        /// 读取电子负载设备识别字符串 (*IDN?)
        /// </summary>
        public virtual async Task<string> 查询设备标识(CancellationToken ct = default)
        {
            return await WriteReadAsync($"*IDN?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        /// <summary>
        /// 复位命令。使电子负载恢复到出厂默认配置状态 (*RST)
        /// </summary>
        public virtual async Task 重置设备(CancellationToken ct = default)
        {
            await SendAsync($"*RST{ScpiDelimiter}", ct);
        }

        /// <summary>
        /// 读取标准状态字节寄存器 (*STB?)
        /// </summary>
        public virtual async Task<string> 读取状态字节(CancellationToken ct = default)
        {
            return await WriteReadAsync($"*STB?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        /// <summary>
        /// 保存当前系统参数到指定的存储单元 (1~100)
        /// </summary>
        public virtual async Task 保存当前状态(int 单元, CancellationToken ct = default)
        {
            if (单元 < 1 || 单元 > 100)
                throw new ArgumentOutOfRangeException(nameof(单元), "存储单元位置应在 1~100 之间");

            await SendAsync($"*SAV {单元}{ScpiDelimiter}", ct);
        }

        /// <summary>
        /// 调用指定存储单元中保存的系统参数 (1~100)
        /// </summary>
        public virtual async Task 调用存储状态(int 单元, CancellationToken ct = default)
        {
            if (单元 < 1 || 单元 > 100)
                throw new ArgumentOutOfRangeException(nameof(单元), "存储单元位置应在 1~100 之间");

            await SendAsync($"*RCL {单元}{ScpiDelimiter}", ct);
        }

        #endregion

        #region 3.2. 电子负载模式切换及拉载开关

        /// <summary>
        /// 控制电子负载的输入控制开关（INPut:STATe <bool>）
        /// </summary>
        public virtual async Task 设置DC输入(bool 开启, CancellationToken ct = default)
        {
            string 参数 = 开启 ? "1" : "0";
            await SendAsync($"INPut:STATe {参数}{ScpiDelimiter}", ct);
        }

        /// <summary>
        /// 查询电子负载当前的拉载开关状态 (返回 0 或 1)
        /// </summary>
        public virtual async Task<string> 查询DC输入状态(CancellationToken ct = default)
        {
            return await WriteReadAsync($"INPut:STATe?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        /// <summary>
        /// 设置电子负载的基本工作模式 (INPut:FUNCtion <mode>)
        /// (支持 CC, CV, CR, CP, CCD, CVD, CRD, CPD, OCP, OPP, ESR, MPP, SWEEP 等)
        /// </summary>
        public enum DeviceWorkMode
        {
            CC,
            CV,
            CR,
            CP,
            CCD,
            ESR,
            AUTO,
            DISCHARGE,
            CHARGE,
            OCP,
            CVD,
            CRD,
            MPP,
            CVCC,
            CRCC,
            CPCC,
            CVCR,
            WAVE,
            SWEEP,
            OPP,
            CPD,
            SZ
        }
        public virtual async Task 设置负载模式(DeviceWorkMode 模式, CancellationToken ct = default)
        {
            await SendAsync($"INPut:FUNCtion {Enum.GetName(模式)}{ScpiDelimiter}", ct);
        }

        /// <summary>
        /// 查询电子负载当前处于何种工作模式
        /// </summary>
        public virtual async Task<string> 查询负载模式(CancellationToken ct = default)
        {
            return await WriteReadAsync($"INPut:FUNCtion?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        #endregion

        #region 3.3. 负载参数设置 (CC / CV / CP / CR 定态大量程)

        /// <summary>
        /// 设定恒电流模式(CC)大量程下的拉载电流目标值 (单位: A)
        /// </summary>
        public virtual async Task 设置恒电流CC(double 电流, CancellationToken ct = default)
        {
            string cmd = string.Format(CultureInfo.InvariantCulture, "STATic:CC:HIGH:LEVel {0:F4}{1}", 电流, ScpiDelimiter);
            await SendAsync(cmd, ct);
        }

        /// <summary>
        /// 查询恒电流模式大量程下设定的拉载电流值
        /// </summary>
        public virtual async Task<string> 查询恒电流CC设定(CancellationToken ct = default)
        {
            return await WriteReadAsync($"STATic:CC:HIGH:LEVel?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        /// <summary>
        /// 设定恒电压模式(CV)大量程下的拉载电压目标值 (单位: V)
        /// </summary>
        public virtual async Task 设置恒电压CV(double 电压, CancellationToken ct = default)
        {
            string cmd = string.Format(CultureInfo.InvariantCulture, "STATic:CV:HIGH:LEVel {0:F3}{1}", 电压, ScpiDelimiter);
            await SendAsync(cmd, ct);
        }

        /// <summary>
        /// 查询恒电压模式大量程下设定的拉载电压值
        /// </summary>
        public virtual async Task<string> 查询恒电压CV设定(CancellationToken ct = default)
        {
            return await WriteReadAsync($"STATic:CV:HIGH:LEVel?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        /// <summary>
        /// 设定恒功率模式(CP)大量程下的拉载功率目标值 (单位: W)
        /// </summary>
        public virtual async Task 设置恒功率CP(double 功率, CancellationToken ct = default)
        {
            string cmd = string.Format(CultureInfo.InvariantCulture, "STATic:CP:HIGH:LEVel {0:F3}{1}", 功率, ScpiDelimiter);
            await SendAsync(cmd, ct);
        }

        /// <summary>
        /// 查询恒功率模式大量程下设定的拉载功率值
        /// </summary>
        public virtual async Task<string> 查询恒功率CP设定(CancellationToken ct = default)
        {
            return await WriteReadAsync($"STATic:CP:HIGH:LEVel?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        /// <summary>
        /// 设定恒电阻模式(CR)大量程下的等效拉载电阻阻值 (单位: Ω)
        /// </summary>
        public virtual async Task 设置恒电阻CR(double 电阻, CancellationToken ct = default)
        {
            string cmd = string.Format(CultureInfo.InvariantCulture, "STATic:CR:HIGH:LEVel {0:F4}{1}", 电阻, ScpiDelimiter);
            await SendAsync(cmd, ct);
        }

        /// <summary>
        /// 查询恒电阻模式大量程下设定的拉载电阻值
        /// </summary>
        public virtual async Task<string> 查询恒电阻CR设定(CancellationToken ct = default)
        {
            return await WriteReadAsync($"STATic:CR:HIGH:LEVel?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        #endregion

        #region 3.4. 测量与数据回测（高频数据轮询核心）

        /// <summary>
        /// 回读电子负载输入端子上的实时测得电压值 (单位: V)
        /// </summary>
        [Monitorable("高压直流负载电压")]
        public virtual async Task<string> 查询实际电压(CancellationToken ct = default)
        {
            return await WriteReadAsync($"MEASure:VOLTage?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        /// <summary>
        /// 回读电子负载输入端子上的实时测得电流值 (单位: A)
        /// </summary>
        [Monitorable("高压直流负载电流")]
        public virtual async Task<string> 查询实际电流(CancellationToken ct = default)
        {
            return await WriteReadAsync($"MEASure:CURRent?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        /// <summary>
        /// 回读电子负载输入端子上的实时测得功率值 (单位: W)
        /// </summary>
        [Monitorable("高压直流负载功率")]
        public virtual async Task<string> 查询实际功率(CancellationToken ct = default)
        {
            return await WriteReadAsync($"MEASure:POWer?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        #endregion

        #region 3.5. 保护限制参数设置

        /// <summary>
        /// 设置电子负载的电流软保护阈值 (单位: A, 设置为 0 表示关闭)
        /// </summary>
        public virtual async Task 设置过流保护值_OCP(double 电流, CancellationToken ct = default)
        {
            string cmd = string.Format(CultureInfo.InvariantCulture, "INPut:CURRent:PROTection {0:F3}{1}", 电流, ScpiDelimiter);
            await SendAsync(cmd, ct);
        }

        /// <summary>
        /// 设置电子负载的功率软保护阈值 (单位: W, 设置为 0 表示关闭)
        /// </summary>
        public virtual async Task 设置过功率保护值_OPP(double 功率, CancellationToken ct = default)
        {
            string cmd = string.Format(CultureInfo.InvariantCulture, "INPut:POWer:PROTection {0:F3}{1}", 功率, ScpiDelimiter);
            await SendAsync(cmd, ct);
        }

        /// <summary>
        /// 设置电子负载的电压软保护阈值 (单位: V, 设置为 0 表示关闭)
        /// </summary>
        public virtual async Task 设置过压保护值_OVP(double 电压, CancellationToken ct = default)
        {
            string cmd = string.Format(CultureInfo.InvariantCulture, "INPut:VOLTage:PROTection {0:F3}{1}", 电压, ScpiDelimiter);
            await SendAsync(cmd, ct);
        }

        #endregion

        #region 3.6. 系统控制及报警清除

        /// <summary>
        /// 读取负载报警/事件寄存器信息 (MEASure:EVENT?)
        /// </summary>
        public virtual async Task<string> 查询报警事件信息(CancellationToken ct = default)
        {
            return await WriteReadAsync($"MEASure:EVENT?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        /// <summary>
        /// 清除负载当前的保护触发锁定状态 (INPut:CLearPROTect)
        /// </summary>
        public virtual async Task 清除保护告警(CancellationToken ct = default)
        {
            await SendAsync($"INPut:CLearPROTect{ScpiDelimiter}", ct);
        }

        #endregion

        #region 3.7. 手册第 112~124 页中的原子控制命令

        #region 组合测试 (COMBination:*)

        public virtual async Task 设置组合量程_CVCC(int 量程, CancellationToken ct = default)
            => await SendAsync($"COMBination:CVCC:RANGE {量程}{ScpiDelimiter}", ct);

        public virtual async Task 设置恒压速率_CVCC(int 速率, CancellationToken ct = default)
            => await SendAsync($"COMBination:CVCC:HIGH:RATE {速率}{ScpiDelimiter}", ct);

        public virtual async Task 设置恒压大量程电压_CVCC(double 电压, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "COMBination:CVCC:HIGH:LEVel {0:F3}{1}", 电压, ScpiDelimiter), ct);

        public virtual async Task 设置限定电流_CVCC(double 电流, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "COMBination:CVCC:CLIMit {0:F3}{1}", 电流, ScpiDelimiter), ct);

        public virtual async Task 设置组合量程_CRCC(int 量程, CancellationToken ct = default)
            => await SendAsync($"COMBination:CRCC:RANGE {量程}{ScpiDelimiter}", ct);

        public virtual async Task 设置恒阻小量程电阻_CRCC(double 电阻, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "COMBination:CRCC:LOW:LEVel {0:F4}{1}", 电阻, ScpiDelimiter), ct);

        public virtual async Task 设置限定电流_CRCC(double 电流, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "COMBination:CRCC:CLIMit {0:F3}{1}", 电流, ScpiDelimiter), ct);

        public virtual async Task 设置上升斜率_CRCC(double 斜率, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "COMBination:CRCC:LOW:SLEWRate:RAIse {0:F1}{1}", 斜率, ScpiDelimiter), ct);

        public virtual async Task 设置下降斜率_CRCC(double 斜率, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "COMBination:CRCC:LOW:SLEWRate:FALL {0:F1}{1}", 斜率, ScpiDelimiter), ct);

        public virtual async Task 设置组合量程_CPCC(int 量程, CancellationToken ct = default)
            => await SendAsync($"COMBination:CPCC:RANGE {量程}{ScpiDelimiter}", ct);

        public virtual async Task 设置恒功率小量程功率_CPCC(double 功率, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "COMBination:CPCC:LOW:LEVel {0:F3}{1}", 功率, ScpiDelimiter), ct);

        public virtual async Task 设置限定电流_CPCC(double 电流, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "COMBination:CPCC:CLIMit {0:F3}{1}", 电流, ScpiDelimiter), ct);

        public virtual async Task 设置组合量程_CVCR(int 量程, CancellationToken ct = default)
            => await SendAsync($"COMBination:CVCR:RANGE {量程}{ScpiDelimiter}", ct);

        public virtual async Task 设置恒压大量程电压_CVCR(double 电压, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "COMBination:CVCR:HIGH:LEVel {0:F3}{1}", 电压, ScpiDelimiter), ct);

        public virtual async Task 设置限定电阻_CVCR(double 电阻, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "COMBination:CVCR:CLIMit {0:F4}{1}", 电阻, ScpiDelimiter), ct);

        #endregion

        #region 瞬态测试 (DYNAmic:*)

        public virtual async Task 设置瞬态运行方式_CC(int 模式, CancellationToken ct = default)
            => await SendAsync($"DYNAmic:CC:MODe {模式}{ScpiDelimiter}", ct); // 0-连续, 1-单次, 2-A/B

        public virtual async Task 设置瞬态量程_CC(int 量程, CancellationToken ct = default)
            => await SendAsync($"DYNAmic:CC:RANGE {量程}{ScpiDelimiter}", ct);

        public virtual async Task 设置瞬态主值电流_CC(double 电流, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DYNAmic:CC:HIGH:LEVel:MAIN {0:F4}{1}", 电流, ScpiDelimiter), ct);

        public virtual async Task 设置瞬态瞬态值电流_CC(double 电流, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DYNAmic:CC:HIGH:LEVel:TRANSient {0:F4}{1}", 电流, ScpiDelimiter), ct);

        public virtual async Task 设置瞬态上升斜率_CC(double 斜率, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DYNAmic:CC:HIGH:SLEWRate:RAIse {0:F1}{1}", 斜率, ScpiDelimiter), ct);

        public virtual async Task 设置瞬态下降斜率_CC(double 斜率, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DYNAmic:CC:HIGH:SLEWRate:FALL {0:F1}{1}", 斜率, ScpiDelimiter), ct);

        public virtual async Task 设置瞬态主值脉宽_CC(double 时间_ms, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DYNAmic:CC:WIDth:MAIN {0:F1}{1}", 时间_ms, ScpiDelimiter), ct);

        public virtual async Task 设置瞬态瞬态脉宽_CC(double 时间_ms, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DYNAmic:CC:WIDth:TRANSient {0:F1}{1}", 时间_ms, ScpiDelimiter), ct);

        public virtual async Task 设置瞬态运行方式_CV(int 模式, CancellationToken ct = default)
            => await SendAsync($"DYNAmic:CV:MODe {模式}{ScpiDelimiter}", ct);

        public virtual async Task 设置瞬态量程_CV(int 量程, CancellationToken ct = default)
            => await SendAsync($"DYNAmic:CV:RANGE {量程}{ScpiDelimiter}", ct);

        public virtual async Task 设置瞬态主值电压_CV(double 电压, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DYNAmic:CV:HIGH:LEVel:MAIN {0:F3}{1}", 电压, ScpiDelimiter), ct);

        public virtual async Task 设置瞬态瞬态值电压_CV(double 电压, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DYNAmic:CV:HIGH:LEVel:TRANSient {0:F3}{1}", 电压, ScpiDelimiter), ct);

        public virtual async Task 设置瞬态主值脉宽_CV(double 时间_ms, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DYNAmic:CV:WIDth:MAIN {0:F1}{1}", 时间_ms, ScpiDelimiter), ct);

        public virtual async Task 设置瞬态瞬态脉宽_CV(double 时间_ms, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DYNAmic:CV:WIDth:TRANSient {0:F1}{1}", 时间_ms, ScpiDelimiter), ct);

        #endregion

        #region 充放电测试 (DISCHarge:* / CHARge:*)

        public virtual async Task 设置放电电流(double 电流, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DISCHarge:CURRent {0:F3}{1}", 电流, ScpiDelimiter), ct);

        public virtual async Task 设置放电终止电压(double 电压, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DISCHarge:VOLTage {0:F3}{1}", 电压, ScpiDelimiter), ct);

        public virtual async Task 设置放电终止时间(double 时间_s, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DISCHarge:TIMe {0:F0}{1}", 时间_s, ScpiDelimiter), ct);

        public virtual async Task 设置放电终止容量(double 容量_Ah, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "DISCHarge:CAPacity {0:F3}{1}", 容量_Ah, ScpiDelimiter), ct);

        public virtual async Task<string> 查询放电实时时间(CancellationToken ct = default)
            => await WriteReadAsync($"DISCHarge:ECHO:TIMe?{ScpiDelimiter}", ScpiDelimiter, ct);

        public virtual async Task<string> 查询放电实时容量(CancellationToken ct = default)
            => await WriteReadAsync($"DISCHarge:ECHo:CAPacity?{ScpiDelimiter}", ScpiDelimiter, ct);

        public virtual async Task 设置充电电流(double 电流, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "CHARge:CURRent {0:F3}{1}", 电流, ScpiDelimiter), ct);

        public virtual async Task 设置充电电压(double 电压, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "CHARge:VOLTage {0:F3}{1}", 电压, ScpiDelimiter), ct);

        public virtual async Task 设置充电恒压时间(int 时间_s, CancellationToken ct = default)
            => await SendAsync($"CHARge:TIMe {时间_s}{ScpiDelimiter}", ct);

        #endregion

        #region OCP / OPP 保护扫描测试 (OCP:* / OPP:*)

        public virtual async Task 设置OCP初始电流(double 电流, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "OCP:BCURrent {0:F3}{1}", 电流, ScpiDelimiter), ct);

        public virtual async Task 设置OCP步进电流(double 电流, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "OCP:SCURrent {0:F3}{1}", 电流, ScpiDelimiter), ct);

        public virtual async Task 设置OCP单步时间(double 时间_s, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "OCP:Time {0:F1}{1}", 时间_s, ScpiDelimiter), ct);

        public virtual async Task 设置OCP终止电压(double 电压, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "OCP:EVOLtage {0:F3}{1}", 电压, ScpiDelimiter), ct);

        public virtual async Task<string> 查询OCP状态(CancellationToken ct = default)
            => await WriteReadAsync($"OCP:STAtus?{ScpiDelimiter}", ScpiDelimiter, ct);

        public virtual async Task<string> 读取OCP结果电流(CancellationToken ct = default)
            => await WriteReadAsync($"OCP:RCURrent?{ScpiDelimiter}", ScpiDelimiter, ct);

        public virtual async Task 设置OPP初始功率(double 功率, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "OPP:BPOWer {0:F3}{1}", 功率, ScpiDelimiter), ct);

        public virtual async Task 设置OPP步进功率(double 功率, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "OPP:SPOWer {0:F3}{1}", 功率, ScpiDelimiter), ct);

        public virtual async Task 设置OPP单步时间(double 时间_s, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "OPP:TIME {0:F1}{1}", 时间_s, ScpiDelimiter), ct);

        public virtual async Task 设置OPP终止电压(double 电压, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "OPP:EVOLtage {0:F3}{1}", 电压, ScpiDelimiter), ct);

        public virtual async Task<string> 查询OPP状态(CancellationToken ct = default)
            => await WriteReadAsync($"OPP:STAtus?{ScpiDelimiter}", ScpiDelimiter, ct);

        public virtual async Task<string> 读取OPP结果功率(CancellationToken ct = default)
            => await WriteReadAsync($"OPP:RPOWer?{ScpiDelimiter}", ScpiDelimiter, ct);

        #endregion

        #region 扫频 SWEEP 与 波形拉载 WAVE (SWEEP:* / WAVE:*)

        public virtual async Task 设置扫频最小电流(double 电流, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "SWEEP:TCURrent {0:F3}{1}", 电流, ScpiDelimiter), ct);

        public virtual async Task 设置扫频最大电流(double 电流, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "SWEEP:MCURrent {0:F3}{1}", 电流, ScpiDelimiter), ct);

        public virtual async Task 设置扫频起始频率(double 频率_Hz, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "SWEEP:SFRequency {0:F1}{1}", 频率_Hz, ScpiDelimiter), ct);

        public virtual async Task 设置扫频结束频率(double 频率_Hz, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "SWEEP:EFRequency {0:F1}{1}", 频率_Hz, ScpiDelimiter), ct);

        public virtual async Task 设置扫频单步时间(double 时间_ms, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "SWEEP:STPTime {0:F1}{1}", 时间_ms, ScpiDelimiter), ct);

        public virtual async Task 设置扫频占空比(double 占空比, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "SWEEP:DUTYcycle {0:F1}{1}", 占空比, ScpiDelimiter), ct);

        public virtual async Task<string> 读取扫频当前运行频率(CancellationToken ct = default)
            => await WriteReadAsync($"SWEEP:CFRequency?{ScpiDelimiter}", ScpiDelimiter, ct);

        public virtual async Task<string> 读取扫频测试结果(CancellationToken ct = default)
            => await WriteReadAsync($"SWEEP:RESult?{ScpiDelimiter}", ScpiDelimiter, ct);

        public virtual async Task 设置波形量程(int 量程, CancellationToken ct = default)
            => await SendAsync($"WAVE:RANGe {量程}{ScpiDelimiter}", ct);

        public virtual async Task 设置波形类型(int 类型, CancellationToken ct = default)
            => await SendAsync($"WAVE:TYPe {类型}{ScpiDelimiter}", ct); // 0-正弦波, 1-方波, 2-三角波, 3-锯齿波, 4-自定义

        public virtual async Task 设置波形频率(double 频率_Hz, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "WAVE:FREQuency {0:F1}{1}", 频率_Hz, ScpiDelimiter), ct);

        public virtual async Task 设置波形幅值(double 幅值, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "WAVE:SETValue {0:F3}{1}", 幅值, ScpiDelimiter), ct);

        public virtual async Task 设置波形时间(double 时间_ms, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "WAVE:TIMe {0:F1}{1}", 时间_ms, ScpiDelimiter), ct);

        public virtual async Task 设置波形叠加值(double 叠加值, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "WAVE:BVALue {0:F3}{1}", 叠加值, ScpiDelimiter), ct);

        #endregion

        #region 内阻测试 ESR (ESR:*)

        public virtual async Task 设置ESR带载量程(int 量程, CancellationToken ct = default)
            => await SendAsync($"ESR:LOADRange {量程}{ScpiDelimiter}", ct);

        public virtual async Task 设置ESR大量程带载电流(double 电流, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "ESR:CURRenthigh {0:F3}{1}", 电流, ScpiDelimiter), ct);

        public virtual async Task 设置ESR测量方法(int 方法, CancellationToken ct = default)
            => await SendAsync($"ESR:MODE {方法}{ScpiDelimiter}", ct); // 0-方波平均法, 1-单脉冲法

        public virtual async Task 设置ESR采样量程(int 量程, CancellationToken ct = default)
            => await SendAsync($"ESR:MEASurerange {量程}{ScpiDelimiter}", ct); // 0-1000mV, 1-100mV, 2-10mV

        public virtual async Task<string> 查询ESR状态(CancellationToken ct = default)
            => await WriteReadAsync($"ESR:STAtus?{ScpiDelimiter}", ScpiDelimiter, ct);

        public virtual async Task<string> 读取ESR结果(CancellationToken ct = default)
            => await WriteReadAsync($"ESR:RESult?{ScpiDelimiter}", ScpiDelimiter, ct);

        #endregion

        #region MPPT 测试 (MPP:*)

        public virtual async Task 设置MPPT步进电压(double 电压_V, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "MPP:BVOLtage {0:F3}{1}", 电压_V, ScpiDelimiter), ct);

        public virtual async Task 设置MPPT步进时间(double 时间_s, CancellationToken ct = default)
            => await SendAsync(string.Format(CultureInfo.InvariantCulture, "MPP:TIME {0:F1}{1}", 时间_s, ScpiDelimiter), ct);

        public virtual async Task 设置MPPT模式(int 模式, CancellationToken ct = default)
            => await SendAsync($"MPP:MODE {模式}{ScpiDelimiter}", ct); // 0-扫描模式, 1-跟踪模式

        public virtual async Task<string> 查询MPPT状态(CancellationToken ct = default)
            => await WriteReadAsync($"MPP:STATus?{ScpiDelimiter}", ScpiDelimiter, ct);

        public virtual async Task<string> 读取MPPT最大功率(CancellationToken ct = default)
            => await WriteReadAsync($"MPP:MPOWer?{ScpiDelimiter}", ScpiDelimiter, ct);

        public virtual async Task<string> 读取MPPT最大功率电压(CancellationToken ct = default)
            => await WriteReadAsync($"MPP:MVOLtage?{ScpiDelimiter}", ScpiDelimiter, ct);

        public virtual async Task<string> 读取MPPT最大功率电流(CancellationToken ct = default)
            => await WriteReadAsync($"MPP:MCURrent?{ScpiDelimiter}", ScpiDelimiter, ct);

        public virtual async Task<string> 读取MPPT开路电压(CancellationToken ct = default)
            => await WriteReadAsync($"MPP:OVOLtage?{ScpiDelimiter}", ScpiDelimiter, ct);

        public virtual async Task<string> 读取MPPT短路电流(CancellationToken ct = default)
            => await WriteReadAsync($"MPP:SCURrent?{ScpiDelimiter}", ScpiDelimiter, ct);

        #endregion

        #endregion
    }
}