diff --git a/DeviceCommand/Devices/DG1000Z.cs b/DeviceCommand/Devices/DG1000Z.cs new file mode 100644 index 0000000..5abec61 --- /dev/null +++ b/DeviceCommand/Devices/DG1000Z.cs @@ -0,0 +1,240 @@ +using Common.Attributes; +using DeviceCommand.Base; +using Model.Models; +using System; +using System.Globalization; +using System.Threading; +using System.Threading.Tasks; + +namespace DeviceCommand.Devices +{ + #region 枚举定义 + + /// + /// 信号发生器输出波形类型 + /// + public enum SignalWaveform + { + /// 正弦波 + SINusoid, + /// 方波 + SQUare, + /// 锯齿波 + RAMP, + /// 脉冲波 (重点用于CP信号模拟) + PULSe, + /// 噪声 + NOISe, + /// 任意波 (USER) + USER + } + + /// + /// 频率计输入信号的耦合方式 + /// + public enum CounterCoupling + { + /// 交流耦合 + AC, + /// 直流耦合 + DC + } + + #endregion + + [ACPCommand] + public class DG1000Z : Tcp + { + // 使用换行符 \n (ASCII 0x0A) 作为 SCPI 结束符 + private const string ScpiDelimiter = "\n"; + + public DG1000Z(TcpConfig config) : base(config) + { + } + + #region 1. IEEE 488.2 公共命令 + + public virtual async Task 清除状态寄存器(CancellationToken ct = default) + { + await SendAsync($"*CLS{ScpiDelimiter}", ct); + } + + public virtual async Task 查询设备标识(CancellationToken ct = default) + { + return await WriteReadAsync($"*IDN?{ScpiDelimiter}", ScpiDelimiter, ct); + } + + public virtual async Task 重置设备(CancellationToken ct = default) + { + await SendAsync($"*RST{ScpiDelimiter}", ct); + } + + public virtual async Task 查询系统SCPI版本(CancellationToken ct = default) + { + return await WriteReadAsync($":SYSTem:VERSion?{ScpiDelimiter}", ScpiDelimiter, ct); + } + + #endregion + + #region 2. 通道输出控制 (CH1 / CH2) + + /// + /// 打开或关闭指定通道的输出 + /// + /// 通道 1 或 2,默认 1 + /// true为开启,false为关闭 + public virtual async Task 设置通道输出状态(int 通道号 = 1, bool 开启 = true, CancellationToken ct = default) + { + string 参数 = 开启 ? "ON" : "OFF"; + await SendAsync($":OUTPut{通道号}:STATe {参数}{ScpiDelimiter}", ct); + } + + public virtual async Task 查询通道输出状态(int 通道号 = 1, CancellationToken ct = default) + { + return await WriteReadAsync($":OUTPut{通道号}:STATe?{ScpiDelimiter}", ScpiDelimiter, ct); + } + + #endregion + + #region 3. 波形、频率与幅度设置 (基础输出) + + /// + /// 设置指定通道的波形类型 (如正弦波、方波等) + /// + public virtual async Task 设置波形类型(int 通道号 = 1, SignalWaveform 波形 = SignalWaveform.SINusoid, CancellationToken ct = default) + { + await SendAsync($":SOURce{通道号}:FUNCtion:SHAPe {波形}{ScpiDelimiter}", ct); + } + + /// + /// 设置指定通道的输出频率 (单位: Hz) + /// + public virtual async Task 设置频率(int 通道号 = 1, double 频率Hz = 1000, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, ":SOURce{0}:FREQuency {1:F2}{2}", 通道号, 频率Hz, ScpiDelimiter); + await SendAsync(cmd, ct); + } + + public virtual async Task 查询频率(int 通道号 = 1, CancellationToken ct = default) + { + return await WriteReadAsync($":SOURce{通道号}:FREQuency?{ScpiDelimiter}", ScpiDelimiter, ct); + } + + /// + /// 设置指定通道的输出幅度 (单位: Vpp,峰峰值) + /// + public virtual async Task 设置幅度(int 通道号 = 1, double 幅度Vpp = 5, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, ":SOURce{0}:VOLTage:AMPLitude {1:F2}{2}", 通道号, 幅度Vpp, ScpiDelimiter); + await SendAsync(cmd, ct); + } + + public virtual async Task 查询幅度(int 通道号 = 1, CancellationToken ct = default) + { + return await WriteReadAsync($":SOURce{通道号}:VOLTage:AMPLitude?{ScpiDelimiter}", ScpiDelimiter, ct); + } + + /// + /// 设置指定通道的直流偏移电压 (单位: Vdc) + /// + public virtual async Task 设置偏移电压(int 通道号 = 1, double 偏移Vdc = 0, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, ":SOURce{0}:VOLTage:OFFSet {1:F2}{2}", 通道号, 偏移Vdc, ScpiDelimiter); + await SendAsync(cmd, ct); + } + + public virtual async Task 查询偏移电压(int 通道号 = 1, CancellationToken ct = default) + { + return await WriteReadAsync($":SOURce{通道号}:VOLTage:OFFSet?{ScpiDelimiter}", ScpiDelimiter, ct); + } + + #endregion + + #region 4. 方波 / 脉冲波占空比设置 (重点用于CP信号模拟) + + /// + /// 设置方波信号的占空比 (0.01% ~ 99.99%)[cite: 1] + /// + public virtual async Task 设置方波占空比(int 通道号 = 1, double 占空比 = 50.0, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, ":SOURce{0}:FUNCtion:SQUare:DCYCle {1:F2}{2}", 通道号, 占空比, ScpiDelimiter); + await SendAsync(cmd, ct); + } + + /// + /// 设置脉冲波信号的占空比 (0.01% ~ 99.99%)[cite: 1] + /// 注意:使用此命令前必须先设置为 PULSe 波形类型 + /// + public virtual async Task 设置脉冲波占空比(int 通道号 = 1, double 占空比 = 50.0, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, ":SOURce{0}:FUNCtion:PULSe:DCYCle {1:F2}{2}", 通道号, 占空比, ScpiDelimiter); + await SendAsync(cmd, ct); + } + + #endregion + + #region 5. 频率计测量功能 (高频数据轮询核心) + + /// + /// 打开或关闭内置频率计功能 + /// + public virtual async Task 设置频率计开关(bool 开启 = true, CancellationToken ct = default) + { + string 参数 = 开启 ? "ON" : "OFF"; + await SendAsync($":COUNter:STATe {参数}{ScpiDelimiter}", ct); + } + + public virtual async Task 查询频率计开关状态(CancellationToken ct = default) + { + return await WriteReadAsync($":COUNter:STATe?{ScpiDelimiter}", ScpiDelimiter, ct); + } + + /// + /// 设置频率计输入信号的耦合方式 + /// + public virtual async Task 设置频率计耦合方式(CounterCoupling 耦合 = CounterCoupling.AC, CancellationToken ct = default) + { + await SendAsync($":COUNter:COUPling {耦合}{ScpiDelimiter}", ct); + } + + /// + /// 设置频率计的触发电平 (单位: V) + /// + public virtual async Task 设置频率计触发电平(double 电平V = 0.1, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, ":COUNter:LEVel {0:F2}{1}", 电平V, ScpiDelimiter); + await SendAsync(cmd, ct); + } + + [Monitorable("信号发生器频率计测量值")] + public virtual async Task 查询频率计测量值(CancellationToken ct = default) + { + // 返回格式: 频率, 周期, 占空比, 正脉宽, 负脉宽 + return await WriteReadAsync($":COUNter:MEASure?{ScpiDelimiter}", ScpiDelimiter, ct); + } + + #endregion + + #region 6. 系统维护与远程控制扩展命令 + + public virtual async Task 切换远程控制模式(CancellationToken ct = default) + { + // 信号发生器通常通过 LAN/USB 和 VISA 通信即自动进入远程控制, + // 但为了兼容模板,保留一个类似的方法。 + await Task.CompletedTask; + } + + public virtual async Task 切换本地控制模式(CancellationToken ct = default) + { + // 通常通过前面板按键退出远程。不过可提供 SCPI 关闭屏幕保护等。 + await Task.CompletedTask; + } + + public virtual async Task 查询错误信息(CancellationToken ct = default) + { + return await WriteReadAsync($":SYSTem:ERRor?{ScpiDelimiter}", ScpiDelimiter, ct); + } + + #endregion + } +} \ No newline at end of file diff --git a/DeviceCommand/Devices/IT6000C.cs b/DeviceCommand/Devices/IT6000C.cs new file mode 100644 index 0000000..ef2044f --- /dev/null +++ b/DeviceCommand/Devices/IT6000C.cs @@ -0,0 +1,331 @@ +using Common.Attributes; +using DeviceCommand.Base; +using Model.Models; +using System; +using System.Globalization; +using System.Threading; +using System.Threading.Tasks; + +namespace DeviceCommand.Devices +{ + #region 枚举定义 + + /// + /// IT6000C 优先工作模式 (CV优先 / CC优先) + /// + public enum IT6000CPriorityMode + { + /// 恒压优先模式 (CV优先) + VOLTage, + /// 恒流优先模式 (CC优先) + CURRent + } + + /// + /// IT6000C 瞬变功能模式 + /// + public enum IT6000CFunctionMode + { + /// 固定模式 (普通输出) + FIXed, + /// List 列表模式 + LIST, + /// 电池测试模式 + BATTer, + /// 光伏/太阳能板曲线模拟模式 + SOLar, + /// 汽车波形模拟模式 + CARProfile + } + + #endregion + + [ACPCommand] + public class IT6000C : Tcp + { + // SCPI 指令结束符 + private const string ScpiDelimiter = "\n"; + + public IT6000C(TcpConfig config) : base(config) + { + } + + #region 1. IEEE 488.2 公共命令 + + public virtual async Task 清除状态寄存器(CancellationToken ct = default) + { + await SendAsync($"*CLS{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 查询设备标识(CancellationToken ct = default) + { + return await WriteReadAsync($"*IDN?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + public virtual async Task 重置设备(CancellationToken ct = default) + { + await SendAsync($"*RST{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 查询状态字节(CancellationToken ct = default) + { + return await WriteReadAsync($"*STB?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + public virtual async Task 查询SCPI版本号(CancellationToken ct = default) + { + return await WriteReadAsync($":SYSTem:VERSion?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + #endregion + + #region 2. 系统远程与状态控制 + + public virtual async Task 切换远程控制模式(CancellationToken ct = default) + { + await SendAsync($"SYSTem:REMote{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 切换本地控制模式(CancellationToken ct = default) + { + await SendAsync($"SYSTem:LOCal{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 锁定远程键盘(CancellationToken ct = default) + { + await SendAsync($"SYSTem:RWLock{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 查询错误信息(CancellationToken ct = default) + { + return await WriteReadAsync($"SYSTem:ERRor?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + public virtual async Task 清除错误队列(CancellationToken ct = default) + { + await SendAsync($"SYSTem:CLEar{ScpiDelimiter}", ct); //[cite: 1] + } + + #endregion + + #region 3. 功能模式与优先级设置 + + /// + /// 设置电源优先工作模式 (CV 电压优先 或 CC 电流优先) + /// + public virtual async Task 设置优先模式(IT6000CPriorityMode 模式, CancellationToken ct = default) + { + await SendAsync($"FUNCTION {模式}{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 查询优先模式(CancellationToken ct = default) + { + return await WriteReadAsync($"FUNCTION?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + /// + /// 设置瞬变/功能模式 (固定, LIST, 电池测试, PV曲线, 汽车波形) + /// + public virtual async Task 设置功能模式(IT6000CFunctionMode 模式, CancellationToken ct = default) + { + await SendAsync($"FUNCTION:MODE {模式}{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 查询功能模式(CancellationToken ct = default) + { + return await WriteReadAsync($"FUNCTION:MODE?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + #endregion + + #region 4. 输出开关与保护清除 + + public virtual async Task 设置输出开关(bool 开启, CancellationToken ct = default) + { + string 参数 = 开启 ? "ON" : "OFF"; + await SendAsync($"OUTPut {参数}{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 查询输出开关状态(CancellationToken ct = default) + { + return await WriteReadAsync($"OUTPut:STATe?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + public virtual async Task 清除保护告警(CancellationToken ct = default) + { + await SendAsync($"OUTPut:PROTection:CLEar{ScpiDelimiter}", ct); //[cite: 1] + } + + #endregion + + #region 5. 输出电压 / 电流设定 + + /// + /// 设置输出电压 (仅在 CV 模式下生效) + /// + public virtual async Task 设置电压(double 电压, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, "VOLTage {0:F2}{1}", 电压, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + public virtual async Task 查询电压设定值(CancellationToken ct = default) + { + return await WriteReadAsync($"VOLTage?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + /// + /// 设置输出电流 (仅在 CC 模式下生效) + /// + public virtual async Task 设置电流(double 电流, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, "CURRent {0:F3}{1}", 电流, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + public virtual async Task 查询电流设定值(CancellationToken ct = default) + { + return await WriteReadAsync($"CURRent?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + /// + /// 设置 CV 优先模式下的电流限制上限值 (I+, 限流保护) + /// + public virtual async Task 设置电流限制上限(double 电流, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, "CURRent:LIMit:POSitive {0:F3}{1}", 电流, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + public virtual async Task 查询电流限制上限(CancellationToken ct = default) + { + return await WriteReadAsync($"CURRent:LIMit:POSitive?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + /// + /// 设置电压上升/下降斜率 (单位: V/s, 数值越大变化越快)[cite: 1] + /// + public virtual async Task 设置电压斜率(double 斜率Vs, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, "VOLTage:SLEW {0:F4}{1}", 斜率Vs, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + #endregion + + #region 6. 保护功能配置 (OVP, OCP, OPP) + + /// + /// 开启或关闭过压保护 (OVP)[cite: 1] + /// + public virtual async Task 设置过压保护开关(bool 开启, CancellationToken ct = default) + { + string 参数 = 开启 ? "ON" : "OFF"; + await SendAsync($"VOLTage:PROTection:STATe {参数}{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 设置过压保护阈值(double 电压, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, "VOLTage:PROTection {0:F2}{1}", 电压, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + /// + /// 开启或关闭过流保护 (OCP)[cite: 1] + /// + public virtual async Task 设置过流保护开关(bool 开启, CancellationToken ct = default) + { + string 参数 = 开启 ? "ON" : "OFF"; + await SendAsync($"CURRent:PROTection:STATe {参数}{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 设置过流保护阈值(double 电流, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, "CURRent:PROTection {0:F3}{1}", 电流, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + /// + /// 开启或关闭过功率保护 (OPP)[cite: 1] + /// + public virtual async Task 设置过功率保护开关(bool 开启, CancellationToken ct = default) + { + string 参数 = 开启 ? "ON" : "OFF"; + await SendAsync($"POWER:PROTection:STATe {参数}{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 设置过功率保护阈值(double 功率, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, "POWER:PROTection {0:F2}{1}", 功率, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + #endregion + + #region 7. 测量回读 (MEASure 与 FETCh) + + [Monitorable("直流源载实际电压")] + public virtual async Task 查询实际电压(CancellationToken ct = default) + { + return await WriteReadAsync($"MEASure:VOLTage?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + [Monitorable("直流源载实际电流")] + public virtual async Task 查询实际电流(CancellationToken ct = default) + { + return await WriteReadAsync($"MEASure:CURRent?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + [Monitorable("直流源载实际功率")] + public virtual async Task 查询实际功率(CancellationToken ct = default) + { + return await WriteReadAsync($"MEASure:POWER?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + public virtual async Task 查询瞬时电压最大值(CancellationToken ct = default) + { + return await WriteReadAsync($"FETCh:VOLTage:MAXimum?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + public virtual async Task 查询瞬时电流最小值(CancellationToken ct = default) + { + return await WriteReadAsync($"FETCh:CURRent:MINimum?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + #endregion + + #region 8. 负载电阻/CR模式设置 (仅限 CC 优先模式) + + /// + /// 开启或关闭恒阻 (CR) 负载模式[cite: 1] + /// + /// 执行该指令前,必须确保设备处于 CC 优先模式,否则会报错 + public virtual async Task 设置CR模式开关(bool 开启, CancellationToken ct = default) + { + string 参数 = 开启 ? "ON" : "OFF"; + await SendAsync($"SINK:RESistance:STATe {参数}{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 查询CR模式开关(CancellationToken ct = default) + { + return await WriteReadAsync($"SINK:RESistance:STATe?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + /// + /// 设置恒阻 (CR) 模式下的电阻值 (单位: Ω)[cite: 1] + /// + /// 必须在先开启 CR 模式才能使用,设置为 0 相当于关闭 CR 模式 + public virtual async Task 设置CR电阻值(double 电阻, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, "SINK:RESistance {0:F2}{1}", 电阻, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + public virtual async Task 查询CR电阻值(CancellationToken ct = default) + { + return await WriteReadAsync($"SINK:RESistance?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + #endregion + } +} \ No newline at end of file diff --git a/DeviceCommand/Devices/IT6720 .cs b/DeviceCommand/Devices/IT6720 .cs new file mode 100644 index 0000000..12197f8 --- /dev/null +++ b/DeviceCommand/Devices/IT6720 .cs @@ -0,0 +1,295 @@ +using Common.Attributes; +using DeviceCommand.Base; +using Model.Models; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Threading; +using System.Threading.Tasks; + +namespace DeviceCommand.Devices +{ + [ACPCommand] + public class IT6720 : Serial_Port // 直接继承您提供的 Serial_Port 基类 + { + // 帧格式:同步头 AA + 地址 + 命令字 + 数据[4-25] + 校验和,共 26 字节 + private const byte FrameSyncByte = 0xAA; + private const int FrameLength = 26; + private readonly byte _deviceAddress; // 电源地址 + + public IT6720(SerialPortConfig config, byte address = 0x00) : base(config) + { + _deviceAddress = address; + } + + #region 核心协议封装(不变) + + /// + /// 构建 26 字节的发送帧,并计算校验和 + /// + private byte[] BuildFrame(byte command, byte[] dataBytes) + { + List frame = new List { FrameSyncByte, _deviceAddress, command }; + + if (dataBytes != null && dataBytes.Length > 0) + { + frame.AddRange(dataBytes); + } + + // 数据区必须占满 23 字节 (索引 3 到 24) + while (frame.Count < 25) + { + frame.Add(0x00); + } + + // 计算校验和:前 25 字节之和取低 8 位 + byte checkSum = 0; + for (int i = 0; i < 25; i++) + { + checkSum += frame[i]; + } + frame.Add(checkSum); + + return frame.ToArray(); + } + + /// + /// 检查返回的 26 字节响应帧是否正确 + /// + private bool ValidateResponseFrame(byte[] response) + { + if (response == null || response.Length != FrameLength) return false; + if (response[0] != FrameSyncByte) return false; + if (response[1] != _deviceAddress) return false; + + byte checkSum = 0; + for (int i = 0; i < 25; i++) + { + checkSum += response[i]; + } + return response[25] == checkSum; + } + + /// + /// 将实际电压/电流值转换成协议要求的整型 (乘以 1000 变成整形) + /// + private int ConvertToProtocolValue(double value) + { + return (int)Math.Round(value * 1000); + } + + /// + /// 将协议返回的 4 字节小端序转换为实际数值 (除以 1000.0) + /// + private double Convert4BytesToValue(byte b0, byte b1, byte b2, byte b3) + { + uint raw = (uint)(b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)); + return raw / 1000.0; + } + + /// + /// 将协议返回的 2 字节小端序转换为实际数值 (除以 1000.0) + /// + private double Convert2BytesToValue(byte b0, byte b1) + { + ushort raw = (ushort)(b0 | (b1 << 8)); + return raw / 1000.0; + } + + #endregion + + #region 1. 核心二进制收发方法(使用基类新方法) + + /// + /// 发送命令帧并读取仪器返回的 26 字节数据帧 + /// + private async Task SendAndReadFrameAsync(byte command, byte[] dataBytes, CancellationToken ct = default) + { + byte[] sendFrame = BuildFrame(command, dataBytes); + + // 使用基类提供的 WriteBytesAsync 发送二进制数据 + await WriteBytesAsync(sendFrame, ct).ConfigureAwait(false); + + // 使用基类提供的 ReadBytesAsync 读取定长 26 字节二进制数据 + // 注意:这里的 ReadTimeout 由基类统一控制 + return await ReadBytesAsync(FrameLength, ct).ConfigureAwait(false); + } + + #endregion + + #region 2. 操作模式与输出控制 + + /// + /// 切换电源控制模式:PC 远程控制 / 面板控制 + /// + public virtual async Task 切换远程控制模式(bool 远程控制 = true, CancellationToken ct = default) + { + byte[] data = new byte[] { (byte)(远程控制 ? 0x01 : 0x00) }; + byte[] response = await SendAndReadFrameAsync(0x20, data, ct); + + if (!ValidateResponseFrame(response)) + throw new Exception("IT6720 切换控制模式时通讯校验失败。"); + } + + public virtual async Task 切换本地控制模式(CancellationToken ct = default) + { + await 切换远程控制模式(false, ct); + } + + /// + /// 开启或关闭电源输出 + /// + public virtual async Task 设置输出开关(bool 开启 = true, CancellationToken ct = default) + { + byte[] data = new byte[] { (byte)(开启 ? 0x01 : 0x00) }; + byte[] response = await SendAndReadFrameAsync(0x21, data, ct); + + if (!ValidateResponseFrame(response)) + throw new Exception("IT6720 设置输出时通讯校验失败。"); + } + + public virtual async Task 查询输出开关状态(CancellationToken ct = default) + { + var (_, _, status) = await 查询实时测量值(ct); + return (status & 0x01) != 0 ? "ON" : "OFF"; + } + + #endregion + + #region 3. 电压 / 电流设定 + + /// + /// 设置电源的电压上限 + /// + public virtual async Task 设置电压上限(double 电压, CancellationToken ct = default) + { + int raw = ConvertToProtocolValue(电压); + byte[] data = new byte[] + { + (byte)(raw & 0xFF), (byte)((raw >> 8) & 0xFF), + (byte)((raw >> 16) & 0xFF), (byte)((raw >> 24) & 0xFF) + }; + byte[] response = await SendAndReadFrameAsync(0x22, data, ct); + if (!ValidateResponseFrame(response)) + throw new Exception("IT6720 设置电压上限时通讯校验失败。"); + } + + /// + /// 设置电源的输出电压 + /// + public virtual async Task 设置输出电压(double 电压, CancellationToken ct = default) + { + int raw = ConvertToProtocolValue(电压); + byte[] data = new byte[] + { + (byte)(raw & 0xFF), (byte)((raw >> 8) & 0xFF), + (byte)((raw >> 16) & 0xFF), (byte)((raw >> 24) & 0xFF) + }; + byte[] response = await SendAndReadFrameAsync(0x23, data, ct); + if (!ValidateResponseFrame(response)) + throw new Exception("IT6720 设置输出电压时通讯校验失败。"); + } + + /// + /// 设置电源的输出电流限流值 + /// + public virtual async Task 设置输出电流(double 电流, CancellationToken ct = default) + { + int raw = ConvertToProtocolValue(电流); + byte[] data = new byte[] + { + (byte)(raw & 0xFF), (byte)((raw >> 8) & 0xFF) + }; + byte[] response = await SendAndReadFrameAsync(0x24, data, ct); + if (!ValidateResponseFrame(response)) + throw new Exception("IT6720 设置输出电流时通讯校验失败。"); + } + + #endregion + + #region 4. 读取实时数据与状态(对应 0x26H) + + /// + /// 读取电源的实时电流、电压、设定值及电源状态 + /// 返回元组:实际电流(A), 实际电压(V), 状态字节(int) + /// + [Monitorable("IT6720 实时测量数据")] + public virtual async Task<(double ActualCurrent, double ActualVoltage, int StatusByte)> 查询实时测量值(CancellationToken ct = default) + { + byte[] response = await SendAndReadFrameAsync(0x26, new byte[0], ct); + + if (!ValidateResponseFrame(response)) + throw new Exception("IT6720 读取数据时通讯校验失败。"); + + double actualCurrent = Convert2BytesToValue(response[4], response[5]); + double actualVoltage = Convert4BytesToValue(response[6], response[7], response[8], response[9]); + int statusByte = response[10]; + + return (actualCurrent, actualVoltage, statusByte); + } + + [Monitorable("IT6720 实际电压")] + public virtual async Task 查询实际电压(CancellationToken ct = default) + { + var (_, voltage, _) = await 查询实时测量值(ct); + return voltage.ToString("F2", CultureInfo.InvariantCulture); + } + + [Monitorable("IT6720 实际电流")] + public virtual async Task 查询实际电流(CancellationToken ct = default) + { + var (current, _, _) = await 查询实时测量值(ct); + return current.ToString("F3", CultureInfo.InvariantCulture); + } + + /// + /// 读取状态字,判断是否处于恒压 (CV) 模式或恒流 (CC) 模式 + /// 状态字节位详解:位 2,3 = 1(CV), 2(CC) + /// + public virtual async Task 查询输出模式(CancellationToken ct = default) + { + var (_, _, status) = await 查询实时测量值(ct); + int mode = (status >> 2) & 0x03; + return mode switch + { + 1 => "CV", + 2 => "CC", + _ => "UNKNOWN" + }; + } + + #endregion + + #region 5. 读取设备信息(命令字 31H) + + /// + /// 读取电源的序列号、型号及软件版本 + /// + public virtual async Task 查询设备信息(CancellationToken ct = default) + { + byte[] response = await SendAndReadFrameAsync(0x31, new byte[0], ct); + if (!ValidateResponseFrame(response)) return "Invalid Response"; + + string model = System.Text.Encoding.ASCII.GetString(response, 4, 5).Trim('\0'); + string swVersion = $"{response[9]:X2}.{response[10]:X2}"; + string serialNum = System.Text.Encoding.ASCII.GetString(response, 11, 10).Trim('\0'); + + return $"Model:{model}, SW:{swVersion}, SN:{serialNum}"; + } + + #endregion + + #region 6. 扩展系统命令(兼容接口) + + /// + /// 基类自带的超时和清空缓冲区已经处理了大部分错误。 + /// 如需在面板显示具体错误,需通过状态位自行判断或查看面板 ERROR 灯。 + /// + public virtual async Task 查询错误信息(CancellationToken ct = default) + { + return "IT6720 无标准 SCPI 错误信息查询,请检查前面板 ERROR 指示灯。"; + } + + #endregion + } +} \ No newline at end of file diff --git a/DeviceCommand/Devices/IT7800E.cs b/DeviceCommand/Devices/IT7800E.cs new file mode 100644 index 0000000..ae85451 --- /dev/null +++ b/DeviceCommand/Devices/IT7800E.cs @@ -0,0 +1,263 @@ +using Common.Attributes; +using DeviceCommand.Base; +using Model.Models; +using System; +using System.Globalization; +using System.Threading; +using System.Threading.Tasks; + +namespace DeviceCommand.Devices +{ + #region 枚举定义 + + /// + /// 电源电气/耦合模式 + /// + public enum PowerCouplingMode + { + /// 纯交流模式 + AC, + /// 纯直流模式 + DC, + /// 交直流混合模式 + ACDC, + /// 直交流混合模式 + DCAC + } + + /// + /// 设备操作运行角色/模式 + /// + public enum DeviceOperationMode + { + /// 电压源模式(常规源) + VOLTage, + /// 电子负载模式(放电拉载) + LOAD, + /// 电流源模式 + CURRent + } + + #endregion + + [ACPCommand] + public class IT7800E : Tcp + { + // 八台产品共用一个,使用换行符 \n (ASCII 0x0A) 作为 SCPI 结束符[cite: 1] + private const string ScpiDelimiter = "\n"; + + public IT7800E(TcpConfig config) : base(config) + { + } + + #region 1. IEEE 488.2 公共命令 + + public virtual async Task 清除错误队列和状态字节(CancellationToken ct = default) + { + await SendAsync($"*CLS{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 查询设备标识(CancellationToken ct = default) + { + return await WriteReadAsync($"*IDN?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + public virtual async Task 重置设备(CancellationToken ct = default) + { + await SendAsync($"*RST{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 读取状态字节(CancellationToken ct = default) + { + return await WriteReadAsync($"*STB?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + #endregion + + #region 2. 耦合模式与拉载开关控制 + + /// + /// 设置电源输出源的电气模式/工作耦合模式 (AC, DC, ACDC, DCAC)[cite: 1] + /// + public virtual async Task 设置电源模式(PowerCouplingMode 模式, CancellationToken ct = default) + { + await SendAsync($":SOURce:FUNCtion {模式}{ScpiDelimiter}", ct); //[cite: 1] + } + + /// + /// 查询电源当前的工作电气模式[cite: 1] + /// + public virtual async Task 查询电源模式(CancellationToken ct = default) + { + return await WriteReadAsync($":SOURce:FUNCtion?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + public virtual async Task 设置DC输出(bool 开启, CancellationToken ct = default) + { + string 参数 = 开启 ? "ON" : "OFF"; //[cite: 1] + await SendAsync($":OUTPut {参数}{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 查询DC输出状态(CancellationToken ct = default) + { + return await WriteReadAsync($":OUTPut?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + #endregion + + #region 3. 设定输出参数设置 (AC 电压/频率/DC 电压) + + public virtual async Task 设置交流电压(double 电压, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, ":SOURce:VOLTage {0:F2}{1}", 电压, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + public virtual async Task 设置直流电压(double 电压, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, ":SOURce:VOLTage:DC {0:F2}{1}", 电压, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + public virtual async Task 设置频率(double 频率, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, ":SOURce:FREQuency {0:F2}{1}", 频率, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + public virtual async Task 设置电流(double 电流, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, ":SOURce:CURRent {0:F3}{1}", 电流, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + #endregion + + #region 4. 测量与数据回测(高频数据轮询核心) + + [Monitorable("交流电源电压")] + public virtual async Task 查询实际电压(CancellationToken ct = default) + { + return await WriteReadAsync($":MEASure:VOLTage?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + [Monitorable("交流电源电流")] + public virtual async Task 查询实际电流(CancellationToken ct = default) + { + return await WriteReadAsync($":MEASure:CURRent?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + [Monitorable("交流电源功率")] + public virtual async Task 查询实际功率(CancellationToken ct = default) + { + return await WriteReadAsync($":MEASure:POWer?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + public virtual async Task 查询视在功率(CancellationToken ct = default) + { + return await WriteReadAsync($":MEASure:POWer:APParent?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + public virtual async Task 查询实际频率(CancellationToken ct = default) + { + return await WriteReadAsync($":MEASure:FREQuency?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + public virtual async Task 查询功率因数(CancellationToken ct = default) + { + return await WriteReadAsync($":MEASure:POWer:PFACtor?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + #endregion + + #region 5. 保护参数设置与系统监控 + + public virtual async Task 设置过流保护_OCP(double 电流, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, ":SOURce:CURRent:PROTection:RMS {0:F3}{1}", 电流, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + public virtual async Task 设置过压保护_OVP(double 电压, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, ":SOURce:VOLTage:PROTection:PEAK {0:F2}{1}", 电压, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + public virtual async Task 查询错误信息(CancellationToken ct = default) + { + return await WriteReadAsync($":SYSTem:ERRor?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + public virtual async Task 切换远程控制模式(CancellationToken ct = default) + { + await SendAsync($":SYSTem:REMote{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 切换本地控制模式(CancellationToken ct = default) + { + await SendAsync($":SYSTem:LOCal{ScpiDelimiter}", ct); //[cite: 1] + } + + public virtual async Task 清除保护告警(CancellationToken ct = default) + { + await SendAsync($":OUTPut:PROTection:CLEar{ScpiDelimiter}", ct); //[cite: 1] + } + + #endregion + + #region 6. 充放电与能量统计扩展命令 + + /// + /// 清除最近的安时(Ah)与瓦时(Wh)统计数据[cite: 1] + /// + public virtual async Task 清除充放电电量统计(CancellationToken ct = default) + { + await SendAsync($"SENSe:AHOur:RESet{ScpiDelimiter}", ct); //[cite: 1] + await SendAsync($"SENSe:WHOur:RESet{ScpiDelimiter}", ct); //[cite: 1] + } + + /// + /// 读取当前充放电累计的安时值 (单位: Ah)[cite: 1] + /// + public virtual async Task 查询累计安时_Ah(CancellationToken ct = default) + { + return await WriteReadAsync($"FETCh:AHOur?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + /// + /// 读取当前充放电累计的瓦时值 (单位: Wh)[cite: 1] + /// + public virtual async Task 查询累计瓦时_Wh(CancellationToken ct = default) + { + return await WriteReadAsync($"FETCh:WHOur?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + /// + /// 读取充放电积分持续时间 (单位: 秒)[cite: 1] + /// + public virtual async Task 查询积分时间(CancellationToken ct = default) + { + return await WriteReadAsync($"FETCh:ENERgy:TIME?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1] + } + + /// + /// 切换设备的操作模式 (VOLTage: 电源模式, LOAD: 负载模式/放电, CURRent: 电流源)[cite: 1] + /// + public virtual async Task 设置操作模式(DeviceOperationMode 模式, CancellationToken ct = default) + { + await SendAsync($"SYSTem:OPERation:MODE {模式}{ScpiDelimiter}", ct); //[cite: 1] + } + + /// + /// 设置放电截止电压/电压下限 (单位: V,用于防止电池过放)[cite: 1] + /// + public virtual async Task 设置电压下限(double 电压, CancellationToken ct = default) + { + string cmd = string.Format(CultureInfo.InvariantCulture, ":SOURce:VOLTage:LIMit:LOW {0:F2}{1}", 电压, ScpiDelimiter); //[cite: 1] + await SendAsync(cmd, ct); + } + + #endregion + } +} \ No newline at end of file