diff --git a/BOB/App.xaml.cs b/BOB/App.xaml.cs index 5fd252b..6c8b772 100644 --- a/BOB/App.xaml.cs +++ b/BOB/App.xaml.cs @@ -34,7 +34,7 @@ namespace BOB } else { - SystemConfig.Instance.Title = "设备2";//模拟打开的设备 + SystemConfig.Instance.Title = "设备1";//模拟打开的设备 } base.OnStartup(e); } diff --git a/BOB/Singleton/Devices.cs b/BOB/Singleton/Devices.cs index 2634301..30589cd 100644 --- a/BOB/Singleton/Devices.cs +++ b/BOB/Singleton/Devices.cs @@ -21,9 +21,9 @@ namespace BOB.Singleton private readonly ProxyGenerator _proxyGen = new ProxyGenerator(); private readonly IInterceptor _loggingInterceptor = new LoggingInterceptor(); - private ITcp E36233ADevice { get; set; } + //private ITcp E36233ADevice { get; set; } private ISerialPort IT6724CDevice { get; set; } - private ISerialPort IT6724CReverseDevice{ get; set; } + //private ISerialPort IT6724CReverseDevice{ get; set; } private IModbusDevice EAEL9080Device { get; set; } private IModbusDevice IOBoardevice { get; set; } private IModbusDevice LQ7500_DDevice { get; set; } @@ -31,6 +31,7 @@ namespace BOB.Singleton private IModbusDevice WS_68030_380TDevice { get; set; } private ITcp SQ0030G1DTDevice { get; set; } private IModbusDevice ZXKSTDevice { get; set; } + private Backfeed BackfeedDevice { get; set; } public Devices() { @@ -46,13 +47,16 @@ namespace BOB.Singleton case "DeviceCommand.Device.E36233A": if (device.CommunicationConfig is TcpConfig tcp1) { - E36233ADevice = _proxyGen.CreateClassProxyWithTarget( - new E36233A(tcp1.IPAddress, tcp1.Port, tcp1.ReadTimeout, tcp1.WriteTimeout), - _loggingInterceptor - ); - DeviceDic["E36233A"] = E36233ADevice; + BackfeedDevice=new Backfeed(tcp1); + DeviceDic["Backfeed"] = BackfeedDevice; + } + break; + case "DeviceCommand.Device.IT6724CReverse": + if (device.CommunicationConfig is SerialPortConfig sp3) + { + BackfeedDevice = new Backfeed(sp3); + DeviceDic["Backfeed"] = BackfeedDevice; } - else throw new InvalidOperationException("E36233A 必须使用 TcpConfig"); break; case "DeviceCommand.Device.IT6724C": @@ -67,18 +71,6 @@ namespace BOB.Singleton else throw new InvalidOperationException("IT6724C 必须使用 SerialPortConfig"); break; - case "DeviceCommand.Device.IT6724CReverse": - if (device.CommunicationConfig is SerialPortConfig sp3) - { - IT6724CReverseDevice = _proxyGen.CreateClassProxyWithTarget( - new IT6724C(sp3.COMPort, sp3.BaudRate, sp3.DataBit, sp3.StopBit, sp3.ParityBit, sp3.ReadTimeout, sp3.WriteTimeout), - _loggingInterceptor - ); - DeviceDic["IT6724CReverse"] = IT6724CReverseDevice; - } - else throw new InvalidOperationException("IT6724CReverse 必须使用 SerialPortConfig"); - break; - case "DeviceCommand.Device.LQ7500_D": if (device.CommunicationConfig is SerialPortConfig sp2) { @@ -208,6 +200,19 @@ namespace BOB.Singleton bool result = await iserialPortDevice.ConnectAsync(CancellationToken.None); LoggerHelper.SuccessWithNotify($"{name} 连接成功 (ISerialPort)"); } + // 检查并处理 Backfeed 类型 + else if (device is Backfeed backfeedDevice) + { + if(backfeedDevice.CurrentInstance is ITcp itcpDevice1) + { + bool result = await itcpDevice1.ConnectAsync(CancellationToken.None); + } + else if (backfeedDevice.CurrentInstance is ISerialPort iserialPortDevice1) + { + bool result = await iserialPortDevice1.ConnectAsync(CancellationToken.None); + } + LoggerHelper.SuccessWithNotify($"{name} 连接成功 (Backfeed)"); + } else { LoggerHelper.WarnWithNotify($"{name} 不是有效的设备类型"); diff --git a/BOB/ViewModels/CommandTreeViewModel.cs b/BOB/ViewModels/CommandTreeViewModel.cs index 194516c..580c056 100644 --- a/BOB/ViewModels/CommandTreeViewModel.cs +++ b/BOB/ViewModels/CommandTreeViewModel.cs @@ -1,4 +1,5 @@ using BOB.Models; +using BOB.Singleton; using Common.Attributes; using Logger; using Microsoft.IdentityModel.Logging; @@ -89,9 +90,11 @@ namespace BOB.ViewModels public ICommand SearchEnterCommand { get; set; } public ICommand TreeDoubleClickCommand { get; set; } public ICommand ReloadCommand { get; set; } - public CommandTreeViewModel(GlobalVariables _GlobalVariables) + private Devices _devices { get; set; } + public CommandTreeViewModel(GlobalVariables _GlobalVariables, Devices devices) { _globalVariables= _GlobalVariables; + _devices = devices; LoadedCommand = new DelegateCommand(Loaded); SearchEnterCommand = new DelegateCommand(Search); TreeDoubleClickCommand = new DelegateCommand(TreeDoubleClick); @@ -143,8 +146,8 @@ namespace BOB.ViewModels private void Loaded() { - Directory.CreateDirectory(SystemConfig.Instance.DLLFilePath); - Directory.CreateDirectory(SystemConfig.Instance.SubProgramFilePath); + //Directory.CreateDirectory(SystemConfig.Instance.DLLFilePath); + //Directory.CreateDirectory(SystemConfig.Instance.SubProgramFilePath); LoadAllAssemblies(); LoadSubPrograms(); LoadInstructionsToTreeView(); @@ -267,7 +270,6 @@ namespace BOB.ViewModels foreach (var assembly in Assemblies) { List validTypes = new List(); - try { var types = assembly.GetTypes().Where(t => @@ -307,6 +309,7 @@ namespace BOB.ViewModels foreach (var type in validTypes) { + if (!_devices.DeviceDic.ContainsKey(type.Name)&& assembly.GetName().Name!= "Command") continue; var typeNode = new InstructionNode { Name = type.Name, @@ -356,7 +359,7 @@ namespace BOB.ViewModels private void GetPublicMethods(Type type, HashSet methods) { // 获取当前类型的所有公共方法(包括继承的) - var allMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static) + var allMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly) .Where(m => !m.IsSpecialName && m.DeclaringType != typeof(object)) .ToList(); diff --git a/DeviceCommand.zip b/DeviceCommand.zip new file mode 100644 index 0000000..6a953df Binary files /dev/null and b/DeviceCommand.zip differ diff --git a/DeviceCommand/Base/Serial_Port.cs b/DeviceCommand/Base/Serial_Port.cs index a1a32a1..afb4ce0 100644 --- a/DeviceCommand/Base/Serial_Port.cs +++ b/DeviceCommand/Base/Serial_Port.cs @@ -62,7 +62,10 @@ namespace DeviceCommand.Base await commLock.WaitAsync(ct); try { - if (!SerialPort.IsOpen) return; + if (!SerialPort.IsOpen) + { + throw new InvalidOperationException("串口未打开,无法进行操作。"); + } byte[] bytes = Encoding.UTF8.GetBytes(data); var timeoutTask = Task.Delay(WriteTimeout > 0 ? WriteTimeout : Timeout.Infinite, ct); @@ -83,7 +86,10 @@ namespace DeviceCommand.Base await commLock.WaitAsync(ct); try { - if (!SerialPort.IsOpen) return null; + if (!SerialPort.IsOpen) + { + throw new InvalidOperationException("串口未打开,无法进行操作。"); + } delimiter ??= "\n"; var sb = new StringBuilder(); diff --git a/DeviceCommand/Base/TCP.cs b/DeviceCommand/Base/TCP.cs index ab419ea..028322d 100644 --- a/DeviceCommand/Base/TCP.cs +++ b/DeviceCommand/Base/TCP.cs @@ -52,7 +52,11 @@ namespace DeviceCommand.Base public void Close() { - if(TcpClient.Connected) TcpClient.Close(); + if (!TcpClient.Connected) + { + throw new InvalidOperationException("TCP 没有连接成功"); + } + if (TcpClient.Connected) TcpClient.Close(); } public async Task SendAsync(byte[] buffer, CancellationToken ct = default) @@ -60,7 +64,10 @@ namespace DeviceCommand.Base await _commLock.WaitAsync(ct); try { - if (!TcpClient.Connected) return; + if (!TcpClient.Connected) + { + throw new InvalidOperationException("TCP 没有连接成功"); + } NetworkStream stream = TcpClient.GetStream(); var timeoutTask = Task.Delay(SendTimeout > 0 ? SendTimeout : Timeout.Infinite, ct); @@ -79,6 +86,11 @@ namespace DeviceCommand.Base public async Task SendAsync(string str, CancellationToken ct = default) { + if (!TcpClient.Connected) + { + throw new InvalidOperationException("TCP 没有连接成功"); + } + await SendAsync(Encoding.UTF8.GetBytes(str), ct); } @@ -87,7 +99,11 @@ namespace DeviceCommand.Base await _commLock.WaitAsync(ct); try { - if (!TcpClient.Connected) return null!; + if (!TcpClient.Connected) + { + throw new InvalidOperationException("TCP 没有连接成功"); + } + NetworkStream stream = TcpClient.GetStream(); byte[] buffer = new byte[length]; int offset = 0; @@ -116,7 +132,11 @@ namespace DeviceCommand.Base await _commLock.WaitAsync(ct); try { - if (!TcpClient.Connected) return null!; + if (!TcpClient.Connected) + { + throw new InvalidOperationException("TCP 没有连接成功"); + } + delimiter ??= "\n"; var sb = new StringBuilder(); byte[] buffer = new byte[1024]; @@ -153,6 +173,11 @@ namespace DeviceCommand.Base public async Task WriteReadAsync(string command, string delimiter = "\n", CancellationToken ct = default) { + if (!TcpClient.Connected) + { + throw new InvalidOperationException("TCP 没有连接成功"); + } + await SendAsync(command, ct); return await ReadAsync(delimiter, ct); } diff --git a/DeviceCommand/Device/Backfeed.cs b/DeviceCommand/Device/Backfeed.cs new file mode 100644 index 0000000..3005cba --- /dev/null +++ b/DeviceCommand/Device/Backfeed.cs @@ -0,0 +1,180 @@ +using Common.Attributes; +using Model; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace DeviceCommand.Device +{ + [BOBCommand] + public class Backfeed + { + private string CurrentDevice { get; set; } + private E36233A _E36233A { get; set; } + private IT6724CReverse _IT6724CReverse { get; set; } + public object CurrentInstance + { + get + { + if (CurrentDevice == "E36233A") + return _E36233A; + else if (CurrentDevice == "IT6724CReverse") + return _IT6724CReverse; + else + throw new InvalidOperationException("还没初始化实例"); + } + } + public Backfeed(ICommunicationConfig config) + { + if (config is TcpConfig tcpConfig) + { + _E36233A = new E36233A(tcpConfig.IPAddress, tcpConfig.Port, tcpConfig.WriteTimeout, tcpConfig.ReadTimeout); + CurrentDevice = "E36233A"; + } + else if (config is SerialPortConfig serialPortConfig) + { + _IT6724CReverse = new IT6724CReverse(serialPortConfig.COMPort, serialPortConfig.BaudRate, serialPortConfig.DataBit, serialPortConfig.StopBit, serialPortConfig.ParityBit, serialPortConfig.ReadTimeout, serialPortConfig.WriteTimeout); + CurrentDevice = "IT6724CReverse"; + } + } + + #region 统一接口 - 设置命令 + + // 清除输出保护 + public virtual async Task 清除输出保护(CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.清除输出保护(ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.清除输出保护(ct); + } + + // 设置电源输出 + public virtual async Task 设置电源输出(bool 开关, CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.设置电源输出(开关, ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.设置电源输出(开关, ct); + } + + // 设置为远程模式 + public virtual async Task 设置为远程模式(CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.设置为远程模式(ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.设置为远程模式(ct); + } + + // 蜂鸣器测试 + public virtual async Task 蜂鸣器测试(CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.蜂鸣器测试(ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.蜂鸣器测试(ct); + } + + // 设置电流 + public virtual async Task 设置电流(double 电流, CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.设置电流(电流, ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.设置电流(电流, ct); + } + + // 设置电流保护OCP电流 + public virtual async Task 设置电流保护OCP电流(double 电流, CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.设置电流保护OCP电流(电流, ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.设置电流保护OCP电流(电流, ct); + } + + // 设置OCP开关 + public virtual async Task 设置OCP开关(bool 开关, CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.设置OCP开关(开关, ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.设置OCP开关(开关, ct); + } + + // 设置电压 + public virtual async Task 设置电压(double 电压, CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.设置电压(电压, ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.设置电压(电压, ct); + } + + // 设置电压保护OVP电压 + public virtual async Task 设置电压保护OVP电压(double 电压, CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.设置电压保护OVP电压(电压, ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.设置电压保护OVP电压(电压, ct); + } + + // 设置OVP开关 + public virtual async Task 设置OVP开关(bool 开关, CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.设置OVP开关(开关, ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.设置OVP开关(开关, ct); + } + + // 设置功率保护功率 + public virtual async Task 设置功率保护功率(double 电压, CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.设置功率保护功率(电压, ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.设置功率保护功率(电压, ct); + } + + // 设置功率保护开关 + public virtual async Task 设置功率保护开关(bool 开关, CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.设置功率保护开关(开关, ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.设置功率保护开关(开关, ct); + } + + // 设置电流斜率 + public virtual async Task 设置电流斜率(double 上升斜率, double 下降斜率, CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.设置电流斜率(上升斜率, 下降斜率, ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.设置电流斜率(上升斜率, 下降斜率, ct); + } + + // 设置电压斜率 + public virtual async Task 设置电压斜率(double 上升斜率, double 下降斜率, CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.设置电压斜率(上升斜率, 下降斜率, ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.设置电压斜率(上升斜率, 下降斜率, ct); + } + + // 发送自定义命令 + public virtual async Task 发送自定义命令(string 指令, CancellationToken ct = default) + { + if (CurrentDevice == "E36233A") + await _E36233A.发送自定义命令(指令, ct); + else if (CurrentDevice == "IT6724CReverse") + await _IT6724CReverse.发送自定义命令(指令, ct); + } + + #endregion + } +} diff --git a/DeviceCommand/Device/E36233A.cs b/DeviceCommand/Device/E36233A.cs index 1b6fc57..09abd33 100644 --- a/DeviceCommand/Device/E36233A.cs +++ b/DeviceCommand/Device/E36233A.cs @@ -16,143 +16,84 @@ namespace DeviceCommand.Device } public E36233A() - { + { } - #region 基础系统命令(必须保留) + #region 设置命令 - /// 查询设备身份 - public virtual Task QueryIDAsync(CancellationToken ct = default) - => SendCommandReadAsync("*IDN?", ct); - - /// 清除错误与状态 - public virtual Task ClearStatusAsync(CancellationToken ct = default) - => SendAsync("*CLS\r\n", ct); - - /// 系统复位 - public virtual Task ResetAsync(CancellationToken ct = default) - => SendAsync("*RST\r\n", ct); - - #endregion - - #region 通道与输出控制 - - /// 选择通道 CH1 或 CH2 - public virtual Task SelectChannelAsync(int channel, CancellationToken ct = default) + public async Task 清除输出保护(CancellationToken ct = default) { - if (channel < 1 || channel > 2) - throw new ArgumentOutOfRangeException(nameof(channel)); - - return SendAsync($"INST:SEL CH{channel}\r\n", ct); + await SendAsync($"PROTection:CLEar\r\n", ct); } - /// 开关输出 - public virtual Task SetOutputAsync(bool on, string channels = "1", CancellationToken ct = default) - => SendAsync($"OUTP {(on ? "ON" : "OFF")},(@{channels})\r\n", ct); - - #endregion - - #region 电压/电流设置 - - /// 设置电压 (0~30.9V) - public virtual Task SetVoltageAsync(double volt, string channels = "1", CancellationToken ct = default) + public async Task 设置电源输出(bool 开关, CancellationToken ct = default) { - if (volt < 0 || volt > 30.9) throw new ArgumentOutOfRangeException(nameof(volt)); - return SendAsync($"SOUR:VOLT {volt},(@{channels})\r\n", ct); + await SendAsync($"OUTPut {(开关 ? 1 : 0)}\r\n", ct); } - /// 设置电流限值 (0~20.6A) - public virtual Task SetCurrentAsync(double curr, string channels = "1", CancellationToken ct = default) + public async Task 设置为远程模式(CancellationToken ct = default) { - if (curr < 0 || curr > 20.6) throw new ArgumentOutOfRangeException(nameof(curr)); - return SendAsync($"SOUR:CURR {curr},(@{channels})\r\n", ct); + await SendAsync($"SYSTem:REMote\r\n", ct); } - /// 设置过压保护 - public virtual Task SetOVPAsync(double ovp, string channels = "1", CancellationToken ct = default) - => SendAsync($"SOUR:VOLT:PROT {ovp},(@{channels})\r\n", ct); - - /// 设置过流保护 - public virtual Task SetOCPAsync(double ocp, string channels = "1", CancellationToken ct = default) - => SendAsync($"SOUR:CURR:PROT {ocp},(@{channels})\r\n", ct); - - /// 清除 OVP/OCP 错误 - public virtual Task ClearProtectionAsync(string channels = "1", CancellationToken ct = default) - => SendAsync($"SOUR:VOLT:PROT:CLE (@{channels})\r\n", ct); - - #endregion - - #region 斜率设置(用于启动测试) - - /// 设置电压上升斜率 - public virtual Task SetRiseSlewAsync(double slew, string channels = "1", CancellationToken ct = default) - => SendAsync($"SOUR:VOLT:SLEW:RIS {slew},(@{channels})\r\n", ct); - - /// 设置电压下降斜率 - public virtual Task SetFallSlewAsync(double slew, string channels = "1", CancellationToken ct = default) - => SendAsync($"SOUR:VOLT:SLEW:FALL {slew},(@{channels})\r\n", ct); - - #endregion - - #region 测量 - - /// 测量电压(V) - public virtual async Task MeasureVoltageAsync(string channels = "1", CancellationToken ct = default) + public async Task 蜂鸣器测试(CancellationToken ct = default) { - string result = await SendCommandReadAsync($"MEAS:VOLT? (@{channels})", ct); - return double.TryParse(result, out var value) ? value : 0; + await SendAsync($"SYSTem:BEEPer\r\n", ct); } - /// 测量电流(A) - public virtual async Task MeasureCurrentAsync(string channels = "1", CancellationToken ct = default) + public async Task 设置电流(double 电流, CancellationToken ct = default) { - string result = await SendCommandReadAsync($"MEAS:CURR? (@{channels})", ct); - return double.TryParse(result, out var value) ? value : 0; + await SendAsync($"CURRent {电流}\r\n", ct); } - #endregion - - #region 保护状态查询 - - public virtual async Task QueryOVPTrippedAsync(string channels = "1", CancellationToken ct = default) + public async Task 设置电流保护OCP电流(double 电流, CancellationToken ct = default) { - string result = await SendCommandReadAsync($"SOUR:VOLT:PROT:TRIP? (@{channels})", ct); - return result == "1"; + await SendAsync($"CURRent:PROTection {电流}\r\n", ct); } - public virtual async Task QueryOCPTrippedAsync(string channels = "1", CancellationToken ct = default) + public async Task 设置OCP开关(bool 开关, CancellationToken ct = default) { - string result = await SendCommandReadAsync($"SOUR:CURR:PROT:TRIP? (@{channels})", ct); - return result == "1"; + await SendAsync($"CURRent:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct); } - #endregion - - #region 序列(用于阶梯电压/耐久测试) - - public virtual Task SetVoltageListAsync(double[] voltages, string channels = "1", CancellationToken ct = default) - => SendAsync($"SOUR:LIST:VOLT {string.Join(",", voltages)},(@{channels})\r\n", ct); - - public virtual Task SetCurrentListAsync(double[] currents, string channels = "1", CancellationToken ct = default) - => SendAsync($"SOUR:LIST:CURR {string.Join(",", currents)},(@{channels})\r\n", ct); - - public virtual Task SetDwellListAsync(double[] times, string channels = "1", CancellationToken ct = default) - => SendAsync($"SOUR:LIST:DWEL {string.Join(",", times)},(@{channels})\r\n", ct); - - public virtual Task SetSequenceRepeatAsync(string count, string channels = "1", CancellationToken ct = default) - => SendAsync($"SOUR:LIST:COUN {count},(@{channels})\r\n", ct); - - public virtual Task StartSequenceAsync(string channels = "1", CancellationToken ct = default) - => SendAsync($"INIT (@{channels})\r\n", ct); - - #endregion - - #region 辅助方法 - - protected virtual async Task SendCommandReadAsync(string cmd, CancellationToken ct = default) + public async Task 设置电压(double 电压, CancellationToken ct = default) { - await SendAsync(cmd + "\r\n", ct); - return await ReadAsync("\n", ct); + await SendAsync($"VOLTage {电压}\r\n", ct); + } + + public async Task 设置电压保护OVP电压(double 电压, CancellationToken ct = default) + { + await SendAsync($"VOLT:PROTection {电压}\r\n", ct); + } + + public async Task 设置OVP开关(bool 开关, CancellationToken ct = default) + { + await SendAsync($"VOLT:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct); + } + + public async Task 设置功率保护功率(double 电压, CancellationToken ct = default) + { + await SendAsync($"POWer:PROTection {电压}\r\n", ct); + } + + public async Task 设置功率保护开关(bool 开关, CancellationToken ct = default) + { + await SendAsync($"POWer:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct); + } + + public async Task 设置电流斜率(double 上升斜率, double 下降斜率, CancellationToken ct = default) + { + await SendAsync($"CURRent:SLEW {上升斜率},{下降斜率}\r\n", ct); + } + + public async Task 设置电压斜率(double 上升斜率, double 下降斜率, CancellationToken ct = default) + { + await SendAsync($"VOLTage:SLEW {上升斜率},{下降斜率}\r\n", ct); + } + + public async Task 发送自定义命令(string 指令, CancellationToken ct = default) + { + await SendAsync($"{指令}\r\n", ct); } #endregion diff --git a/DeviceCommand/Device/EAEL9080.cs b/DeviceCommand/Device/EAEL9080.cs index 025fcdc..8f9057c 100644 --- a/DeviceCommand/Device/EAEL9080.cs +++ b/DeviceCommand/Device/EAEL9080.cs @@ -17,45 +17,45 @@ namespace DeviceCommand.Device #region 一、基础控制 - public virtual async Task SetRemoteControlAsync(bool activate, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置远程控制(bool 激活, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(activate ? 0xFF00 : 0x0000); + ushort value = (ushort)(激活 ? 0xFF00 : 0x0000); await WriteSingleRegisterAsync(slaveAddress, 402, value, ct); } - public virtual async Task SetOutputAsync(bool on, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置输出(bool 开启, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(on ? 0xFF00 : 0x0000); + ushort value = (ushort)(开启 ? 0xFF00 : 0x0000); await WriteSingleRegisterAsync(slaveAddress, 405, value, ct); } - public virtual async Task SetWorkModeAsync(bool useResistanceMode, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置工作模式(bool 使用电阻模式, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(useResistanceMode ? 0xFF00 : 0x0000); + ushort value = (ushort)(使用电阻模式 ? 0xFF00 : 0x0000); await WriteSingleRegisterAsync(slaveAddress, 409, value, ct); } - public virtual async Task ReadDeviceStatusAsync(byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询设备状态(byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 505, 2, ct); return (uint)(reg[0] << 16 | reg[1]); } - public virtual async Task IsDcOnAsync(byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询直流开启状态(byte slaveAddress = 1, CancellationToken ct = default) { - uint status = await ReadDeviceStatusAsync(slaveAddress, ct); + uint status = await 查询设备状态(slaveAddress, ct); return (status & (1 << 7)) != 0; } - public virtual async Task IsRemoteActiveAsync(byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询远程控制状态(byte slaveAddress = 1, CancellationToken ct = default) { - uint status = await ReadDeviceStatusAsync(slaveAddress, ct); + uint status = await 查询设备状态(slaveAddress, ct); return (status & (1 << 10)) != 0; } - public virtual async Task IsSinkModeActiveAsync(byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询Sink模式状态(byte slaveAddress = 1, CancellationToken ct = default) { - uint status = await ReadDeviceStatusAsync(slaveAddress, ct); + uint status = await 查询设备状态(slaveAddress, ct); return (status & (1 << 12)) != 0; } @@ -63,117 +63,117 @@ namespace DeviceCommand.Device #region 二、电压 / 电流 / 功率 / 电阻设定值 - public virtual async Task SetVoltageAsync(double voltage, double ratedVoltage, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置电压(double 电压, double 额定电压, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(voltage / ratedVoltage * 0xCCCC); + ushort value = (ushort)(电压 / 额定电压 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 500, value, ct); } - public virtual async Task GetVoltageAsync(double ratedVoltage, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询电压(double 额定电压, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 500, 1, ct); - return reg[0] / (double)0xCCCC * ratedVoltage; + return reg[0] / (double)0xCCCC * 额定电压; } - public virtual async Task SetCurrentAsync(double current, double ratedCurrent, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置电流(double 电流, double 额定电流, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(current / ratedCurrent * 0xCCCC); + ushort value = (ushort)(电流 / 额定电流 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 501, value, ct); } - public virtual async Task GetCurrentAsync(double ratedCurrent, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询电流(double 额定电流, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 501, 1, ct); - return reg[0] / (double)0xCCCC * ratedCurrent; + return reg[0] / (double)0xCCCC * 额定电流; } - public virtual async Task SetPowerAsync(double power, double ratedPower, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置功率(double 功率, double 额定功率, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(power / ratedPower * 0xCCCC); + ushort value = (ushort)(功率 / 额定功率 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 502, value, ct); } - public virtual async Task GetPowerAsync(double ratedPower, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询功率(double 额定功率, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 502, 1, ct); - return reg[0] / (double)0xCCCC * ratedPower; + return reg[0] / (double)0xCCCC * 额定功率; } - public virtual async Task SetResistanceAsync(double resistance, double maxResistance, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置电阻(double 电阻, double 最大电阻, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(resistance / maxResistance * 0xCCCC); + ushort value = (ushort)(电阻 / 最大电阻 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 503, value, ct); } - public virtual async Task GetResistanceAsync(double maxResistance, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询电阻(double 最大电阻, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 503, 1, ct); - return reg[0] / (double)0xCCCC * maxResistance; + return reg[0] / (double)0xCCCC * 最大电阻; } #endregion #region 三、Sink 模式 - public virtual async Task SetSinkPowerAsync(double power, double sinkRatedPower, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置Sink功率(double 功率, double Sink额定功率, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(power / sinkRatedPower * 0xCCCC); + ushort value = (ushort)(功率 / Sink额定功率 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 498, value, ct); } - public virtual async Task SetSinkCurrentAsync(double current, double sinkRatedCurrent, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置Sink电流(double 电流, double Sink额定电流, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(current / sinkRatedCurrent * 0xCCCC); + ushort value = (ushort)(电流 / Sink额定电流 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 499, value, ct); } - public virtual async Task SetSinkResistanceAsync(double resistance, double sinkMaxResistance, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置Sink电阻(double 电阻, double Sink最大电阻, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(resistance / sinkMaxResistance * 0xCCCC); + ushort value = (ushort)(电阻 / Sink最大电阻 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 504, value, ct); } - public virtual async Task GetSinkPowerAsync(double sinkRatedPower, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询Sink功率(double Sink额定功率, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 498, 1, ct); - return reg[0] / (double)0xCCCC * sinkRatedPower; + return reg[0] / (double)0xCCCC * Sink额定功率; } - public virtual async Task GetSinkCurrentAsync(double sinkRatedCurrent, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询Sink电流(double Sink额定电流, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 499, 1, ct); - return reg[0] / (double)0xCCCC * sinkRatedCurrent; + return reg[0] / (double)0xCCCC * Sink额定电流; } - public virtual async Task GetSinkResistanceAsync(double sinkMaxResistance, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询Sink电阻(double Sink最大电阻, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 504, 1, ct); - return reg[0] / (double)0xCCCC * sinkMaxResistance; + return reg[0] / (double)0xCCCC * Sink最大电阻; } #endregion #region 四、实时测量 - public virtual async Task ReadActualVoltageAsync(double ratedVoltage, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询实时电压(double 额定电压, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 507, 1, ct); - return reg[0] / (double)0xCCCC * ratedVoltage; + return reg[0] / (double)0xCCCC * 额定电压; } - public virtual async Task ReadActualCurrentAsync(double ratedCurrent, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询实时电流(double 额定电流, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 508, 1, ct); - double current = reg[0] / (double)0xCCCC * ratedCurrent; - if (await IsSinkModeActiveAsync(slaveAddress, ct)) current = -current; + double current = reg[0] / (double)0xCCCC * 额定电流; + if (await 查询Sink模式状态(slaveAddress, ct)) current = -current; return current; } - public virtual async Task ReadActualPowerAsync(double ratedPower, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询实时功率(double 额定功率, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 509, 1, ct); - double power = reg[0] / (double)0xCCCC * ratedPower; - if (await IsSinkModeActiveAsync(slaveAddress, ct)) power = -power; + double power = reg[0] / (double)0xCCCC * 额定功率; + if (await 查询Sink模式状态(slaveAddress, ct)) power = -power; return power; } @@ -181,21 +181,21 @@ namespace DeviceCommand.Device #region 五、保护阈值 - public virtual async Task SetOverVoltageLimitAsync(double voltage, double ratedVoltage, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置过电压保护(double 电压, double 额定电压, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(voltage / ratedVoltage * 0xCCCC); + ushort value = (ushort)(电压 / 额定电压 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 510, value, ct); } - public virtual async Task SetOverCurrentLimitAsync(double current, double ratedCurrent, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置过电流保护(double 电流, double 额定电流, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(current / ratedCurrent * 0xCCCC); + ushort value = (ushort)(电流 / 额定电流 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 511, value, ct); } - public virtual async Task SetOverPowerLimitAsync(double power, double ratedPower, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置过功率保护(double 功率, double 额定功率, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(power / ratedPower * 0xCCCC); + ushort value = (ushort)(功率 / 额定功率 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 512, value, ct); } diff --git a/DeviceCommand/Device/IOBoard.cs b/DeviceCommand/Device/IOBoard.cs index 0c23c3c..0ba3c44 100644 --- a/DeviceCommand/Device/IOBoard.cs +++ b/DeviceCommand/Device/IOBoard.cs @@ -9,26 +9,26 @@ using System.Threading.Tasks; namespace DeviceCommand.Device { [BOBCommand] - public class IOBoard:ModbusTcp + public class IOBoard : ModbusTcp { - public IOBoard(string IpAddress, int port, int SendTimeout, int ReceiveTimeout) + public IOBoard(string Ip地址, int 端口, int 发送超时, int 接收超时) { - ConfigureDevice(IpAddress, port, SendTimeout, ReceiveTimeout); - + ConfigureDevice(Ip地址, 端口, 发送超时, 接收超时); } + public IOBoard() { - - } - public virtual async Task WriteOutput(byte slaveId, ushort startAddress, ushort value) - { - await WriteSingleRegisterAsync(slaveId, startAddress, value); + } - public virtual async Task WriteOutputsBatch(byte slaveId, ushort startAddress, ushort[] values) + public virtual async Task 写单一输出(byte 从站地址, ushort 起始地址, ushort 值) { - await WriteMultipleRegistersAsync(slaveId, startAddress, values); + await WriteSingleRegisterAsync(从站地址, 起始地址, 值); } + public virtual async Task 写多输出(byte 从站地址, ushort 起始地址, ushort[] 值数组) + { + await WriteMultipleRegistersAsync(从站地址, 起始地址, 值数组); + } } } diff --git a/DeviceCommand/Device/IT6724C.cs b/DeviceCommand/Device/IT6724C.cs index 13042bb..d6a8b75 100644 --- a/DeviceCommand/Device/IT6724C.cs +++ b/DeviceCommand/Device/IT6724C.cs @@ -20,76 +20,76 @@ namespace DeviceCommand.Device #region 设置命令 /// - /// 设置输出状态,true 为开,false 为关 + /// 设置电源输出状态,true 为开,false 为关 /// - public virtual Task SetOutputAsync(bool state, CancellationToken ct = default) - => SendAsync($"OUTPut {(state ? 1 : 0)}\r\n", ct); + public virtual async Task 设置电源输出(bool 开关, CancellationToken ct = default) + =>await SendAsync($"OUTPut {(开关 ? 1 : 0)}\r\n", ct); /// /// 切换到远程控制模式 /// - public virtual Task SetRemoteModeAsync(CancellationToken ct = default) - => SendAsync("SYSTem:REMote\r\n", ct); + public virtual async Task 设置为远程模式(CancellationToken ct = default) + =>await SendAsync("SYSTem:REMote\r\n", ct); /// /// 蜂鸣器测试 /// - public virtual Task BeeperTestAsync(CancellationToken ct = default) - => SendAsync("SYSTem:BEEPer\r\n", ct); + public virtual async Task 蜂鸣器测试(CancellationToken ct = default) + =>await SendAsync("SYSTem:BEEPer\r\n", ct); /// /// 设置输出电流(单位:A) /// - public virtual Task SetCurrentAsync(double current, CancellationToken ct = default) - => SendAsync($"CURRent {current}\r\n", ct); + public virtual async Task 设置电流(double 电流, CancellationToken ct = default) + =>await SendAsync($"CURRent {电流}\r\n", ct); /// /// 设置过流保护电流(OCP,单位:A) /// - public virtual Task SetOCPCurrentAsync(double current, CancellationToken ct = default) - => SendAsync($"CURRent:PROTection {current}\r\n", ct); + public virtual async Task 设置电流保护OCP电流(double 电流, CancellationToken ct = default) + =>await SendAsync($"CURRent:PROTection {电流}\r\n", ct); /// /// 设置过流保护状态,true 为启用,false 为禁用 /// - public virtual Task SetOCPStateAsync(bool state, CancellationToken ct = default) - => SendAsync($"CURRent:PROTection:STATe {(state ? 1 : 0)}\r\n", ct); + public virtual async Task 设置OCP开关(bool 开关, CancellationToken ct = default) + =>await SendAsync($"CURRent:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct); /// /// 清除过流保护触发状态 /// - public virtual Task ClearOCPAsync(CancellationToken ct = default) - => SendAsync("CURRent:PROTection:CLEar\r\n", ct); + public virtual async Task 清除电流保护(CancellationToken ct = default) + =>await SendAsync("CURRent:PROTection:CLEar\r\n", ct); /// /// 设置输出电压(单位:V) /// - public virtual Task SetVoltageAsync(double voltage, CancellationToken ct = default) - => SendAsync($"VOLTage {voltage}\r\n", ct); + public virtual async Task 设置电压(double 电压, CancellationToken ct = default) + =>await SendAsync($"VOLTage {电压}\r\n", ct); /// /// 设置过压保护电压(OVP,单位:V) /// - public virtual Task SetOVPVoltageAsync(double voltage, CancellationToken ct = default) - => SendAsync($"VOLT:PROTection {voltage}\r\n", ct); + public virtual async Task 设置电压保护OVP电压(double 电压, CancellationToken ct = default) + =>await SendAsync($"VOLT:PROTection {电压}\r\n", ct); /// /// 设置过压保护状态,true 为启用,false 为禁用 /// - public virtual Task SetOVPStateAsync(bool state, CancellationToken ct = default) - => SendAsync($"VOLT:PROTection:STATe {(state ? 1 : 0)}\r\n", ct); + public virtual async Task 设置OVP开关(bool 开关, CancellationToken ct = default) + =>await SendAsync($"VOLT:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct); /// /// 清除过压保护触发状态 /// - public virtual Task ClearOVPAsync(CancellationToken ct = default) - => SendAsync("VOLT:PROTection:CLEar\r\n", ct); + public virtual async Task 清除电压保护(CancellationToken ct = default) + => await SendAsync("VOLT:PROTection:CLEar\r\n", ct); /// /// 发送自定义命令 /// - public virtual Task SendCustomCommandAsync(string command, CancellationToken ct = default) - => SendAsync($"{command}\r\n", ct); + public virtual async Task 发送自定义命令(string 指令, CancellationToken ct = default) + =>await SendAsync($"{指令}\r\n", ct); #endregion @@ -98,7 +98,7 @@ namespace DeviceCommand.Device /// /// 查询仪器标识,返回制造商、型号、序列号、固件版本 /// - public virtual async Task QueryIDAsync(CancellationToken ct = default) + public virtual async Task 查询设备信息(CancellationToken ct = default) { await SendAsync("*IDN?\r\n", ct); return await ReadAsync(ct: ct); @@ -107,7 +107,7 @@ namespace DeviceCommand.Device /// /// 查询过流保护是否触发,返回 true 表示触发 /// - public virtual async Task QueryOCPTrippedAsync(CancellationToken ct = default) + public virtual async Task 查询电流保护状态(CancellationToken ct = default) { await SendAsync("CURRent:PROTection:TRIPed?\r\n", ct); var result = await ReadAsync(ct: ct); @@ -117,7 +117,7 @@ namespace DeviceCommand.Device /// /// 查询过压保护是否触发,返回 true 表示触发 /// - public virtual async Task QueryOVPTrippedAsync(CancellationToken ct = default) + public virtual async Task 查询电压保护状态(CancellationToken ct = default) { await SendAsync("VOLT:PROTection:TRIPed?\r\n", ct); var result = await ReadAsync(ct: ct); @@ -127,7 +127,7 @@ namespace DeviceCommand.Device /// /// 查询实际输出电流(单位:A) /// - public virtual async Task QueryCurrentAsync(CancellationToken ct = default) + public virtual async Task 查询实时电流(CancellationToken ct = default) { await SendAsync("MEASure:CURRent?\r\n", ct); var result = await ReadAsync(ct: ct); @@ -137,7 +137,7 @@ namespace DeviceCommand.Device /// /// 查询实际输出电压(单位:V) /// - public virtual async Task QueryVoltageAsync(CancellationToken ct = default) + public virtual async Task 查询实时电压(CancellationToken ct = default) { await SendAsync("MEASure:VOLTage?\r\n", ct); var result = await ReadAsync(ct: ct); @@ -147,7 +147,7 @@ namespace DeviceCommand.Device /// /// 查询实际输出功率(单位:W) /// - public virtual async Task QueryPowerAsync(CancellationToken ct = default) + public virtual async Task 查询实时功率(CancellationToken ct = default) { await SendAsync("MEASure:POWer?\r\n", ct); var result = await ReadAsync(ct: ct); diff --git a/DeviceCommand/Device/IT6724CReverse.cs b/DeviceCommand/Device/IT6724CReverse.cs index b4851e0..4494a9a 100644 --- a/DeviceCommand/Device/IT6724CReverse.cs +++ b/DeviceCommand/Device/IT6724CReverse.cs @@ -19,86 +19,50 @@ namespace DeviceCommand.Device #region 设置命令 - public virtual Task SetOutputAsync(bool state, CancellationToken ct = default) - => SendAsync($"OUTPut {(state ? 1 : 0)}\r\n", ct); + public virtual async Task 清除输出保护(CancellationToken ct = default) + =>await SendAsync("PROTection:CLEar\r\n", ct); - public virtual Task SetRemoteModeAsync(CancellationToken ct = default) - => SendAsync("SYSTem:REMote\r\n", ct); + public virtual async Task 设置电源输出(bool 开关, CancellationToken ct = default) + =>await SendAsync($"OUTPut {(开关 ? 1 : 0)}\r\n", ct); - public virtual Task BeeperTestAsync(CancellationToken ct = default) - => SendAsync("SYSTem:BEEPer\r\n", ct); + public virtual async Task 设置为远程模式(CancellationToken ct = default) + =>await SendAsync("SYSTem:REMote\r\n", ct); - public virtual Task SetCurrentAsync(double current, CancellationToken ct = default) - => SendAsync($"CURRent {current}\r\n", ct); + public virtual async Task 蜂鸣器测试(CancellationToken ct = default) + =>await SendAsync("SYSTem:BEEPer\r\n", ct); - public virtual Task SetOCPCurrentAsync(double current, CancellationToken ct = default) - => SendAsync($"CURRent:PROTection {current}\r\n", ct); + public virtual async Task 设置电流(double 电流, CancellationToken ct = default) + =>await SendAsync($"CURRent {电流}\r\n", ct); - public virtual Task SetOCPStateAsync(bool state, CancellationToken ct = default) - => SendAsync($"CURRent:PROTection:STATe {(state ? 1 : 0)}\r\n", ct); + public virtual async Task 设置电流保护OCP电流(double 电流, CancellationToken ct = default) + =>await SendAsync($"CURRent:PROTection {电流}\r\n", ct); - public virtual Task ClearOCPAsync(CancellationToken ct = default) - => SendAsync("CURRent:PROTection:CLEar\r\n", ct); + public virtual async Task 设置OCP开关(bool 开关, CancellationToken ct = default) + =>await SendAsync($"CURRent:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct); - public virtual Task SetVoltageAsync(double voltage, CancellationToken ct = default) - => SendAsync($"VOLTage {voltage}\r\n", ct); + public virtual async Task 设置电压(double 电压, CancellationToken ct = default) + =>await SendAsync($"VOLTage {电压}\r\n", ct); - public virtual Task SetOVPVoltageAsync(double voltage, CancellationToken ct = default) - => SendAsync($"VOLT:PROTection {voltage}\r\n", ct); + public virtual async Task 设置电压保护OVP电压(double 电压, CancellationToken ct = default) + =>await SendAsync($"VOLT:PROTection {电压}\r\n", ct); - public virtual Task SetOVPStateAsync(bool state, CancellationToken ct = default) - => SendAsync($"VOLT:PROTection:STATe {(state ? 1 : 0)}\r\n", ct); + public virtual async Task 设置OVP开关(bool 开关, CancellationToken ct = default) + =>await SendAsync($"VOLT:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct); - public virtual Task ClearOVPAsync(CancellationToken ct = default) - => SendAsync("VOLT:PROTection:CLEar\r\n", ct); + public virtual async Task 设置功率保护功率(double 电压, CancellationToken ct = default) + =>await SendAsync($"POWer:PROTection {电压}\r\n", ct); - public virtual Task SendCustomCommandAsync(string command, CancellationToken ct = default) - => SendAsync($"{command}\r\n", ct); + public virtual async Task 设置功率保护开关(bool 开关, CancellationToken ct = default) + =>await SendAsync($"POWer:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct); - #endregion + public virtual async Task 设置电流斜率(double 上升斜率, double 下降斜率, CancellationToken ct = default) + =>await SendAsync($"CURRent:SLEW {上升斜率},{下降斜率}\r\n", ct); - #region 查询命令 + public virtual async Task 设置电压斜率(double 上升斜率, double 下降斜率, CancellationToken ct = default) + =>await SendAsync($"VOLTage:SLEW {上升斜率},{下降斜率}\r\n", ct); - public virtual async Task QueryIDAsync(CancellationToken ct = default) - { - await SendAsync("*IDN?\r\n", ct); - return await ReadAsync(ct: ct); - } - - public virtual async Task QueryOCPTrippedAsync(CancellationToken ct = default) - { - await SendAsync("CURRent:PROTection:TRIPed?\r\n", ct); - var result = await ReadAsync(ct: ct); - return result == "1"; - } - - public virtual async Task QueryOVPTrippedAsync(CancellationToken ct = default) - { - await SendAsync("VOLT:PROTection:TRIPed?\r\n", ct); - var result = await ReadAsync(ct: ct); - return result == "1"; - } - - public virtual async Task QueryCurrentAsync(CancellationToken ct = default) - { - await SendAsync("MEASure:CURRent?\r\n", ct); - var result = await ReadAsync(ct: ct); - return Convert.ToDouble(result); - } - - public virtual async Task QueryVoltageAsync(CancellationToken ct = default) - { - await SendAsync("MEASure:VOLTage?\r\n", ct); - var result = await ReadAsync(ct: ct); - return Convert.ToDouble(result); - } - - public virtual async Task QueryPowerAsync(CancellationToken ct = default) - { - await SendAsync("MEASure:POWer?\r\n", ct); - var result = await ReadAsync(ct: ct); - return Convert.ToDouble(result); - } + public virtual async Task 发送自定义命令(string 指令, CancellationToken ct = default) + =>await SendAsync($"{指令}\r\n", ct); #endregion } diff --git a/DeviceCommand/Device/LQ7500-D.cs b/DeviceCommand/Device/LQ7500-D.cs index bd4ad63..e9b426e 100644 --- a/DeviceCommand/Device/LQ7500-D.cs +++ b/DeviceCommand/Device/LQ7500-D.cs @@ -11,138 +11,138 @@ namespace DeviceCommand.Device [BOBCommand] public class LQ7500_D : ModbusRtu { - public LQ7500_D(string COMPort, int BaudRate, int DataBits, StopBits stopBits, Parity parity, int ReadTimeout, int ReceiveTimeout) + public LQ7500_D(string 串口, int 波特率, int 数据位, StopBits 停止位, Parity 校验位, int 读取超时, int 接收超时) { - ConfigureDevice(COMPort, BaudRate, DataBits, stopBits, parity, ReadTimeout, ReceiveTimeout); + ConfigureDevice(串口, 波特率, 数据位, 停止位, 校验位, 读取超时, 接收超时); } public LQ7500_D() { } - public byte SlaveAddress { get; set; } = 1; // default slave address + public byte 从站地址 { get; set; } = 1; // 默认从站地址 private static readonly int[] Pow10Table = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; - private async Task EnsureConnectionAsync() + private async Task 确保连接() { if (SerialPort == null || !SerialPort.IsOpen) await ConnectAsync(); } - #region Read Methods + #region 读取方法 - public virtual async Task ReadTemperatureResolutionAsync() + public virtual async Task 读取温度分辨率() { - await EnsureConnectionAsync(); - return (await ReadHoldingRegistersAsync(SlaveAddress, 35, 1))[0]; + await 确保连接(); + return (await ReadHoldingRegistersAsync(从站地址, 35, 1))[0]; } - public virtual async Task ReadInternalSensorTemperatureAsync(ushort resolution = 0) + public virtual async Task 读取内部传感器温度(ushort 分辨率 = 0) { - await EnsureConnectionAsync(); - if (resolution == 0) resolution = await ReadTemperatureResolutionAsync(); - var data = await ReadHoldingRegistersAsync(SlaveAddress, 0, 1); - return Math.Round(data[0] / (double)Pow10Table[resolution], resolution + 1); + await 确保连接(); + if (分辨率 == 0) 分辨率 = await 读取温度分辨率(); + var data = await ReadHoldingRegistersAsync(从站地址, 0, 1); + return Math.Round(data[0] / (double)Pow10Table[分辨率], 分辨率 + 1); } - public virtual async Task ReadExternalSensorTemperatureAsync(ushort resolution = 0) + public virtual async Task 读取外部传感器温度(ushort 分辨率 = 0) { - await EnsureConnectionAsync(); - if (resolution == 0) resolution = await ReadTemperatureResolutionAsync(); - var data = await ReadHoldingRegistersAsync(SlaveAddress, 1, 1); - return data[0] / (double)Pow10Table[resolution]; + await 确保连接(); + if (分辨率 == 0) 分辨率 = await 读取温度分辨率(); + var data = await ReadHoldingRegistersAsync(从站地址, 1, 1); + return data[0] / (double)Pow10Table[分辨率]; } - public virtual async Task ReadFaultCodeAsync() + public virtual async Task 读取故障代码() { - await EnsureConnectionAsync(); - return (await ReadHoldingRegistersAsync(SlaveAddress, 1, 1))[0]; + await 确保连接(); + return (await ReadHoldingRegistersAsync(从站地址, 1, 1))[0]; } - public virtual async Task ReadDeviceStatusAsync() + public virtual async Task 读取设备状态() { - await EnsureConnectionAsync(); - return (await ReadHoldingRegistersAsync(SlaveAddress, 3, 1))[0]; + await 确保连接(); + return (await ReadHoldingRegistersAsync(从站地址, 3, 1))[0]; } - public virtual async Task ReadPumpStatusAsync() + public virtual async Task 读取泵状态() { - await EnsureConnectionAsync(); - return (await ReadHoldingRegistersAsync(SlaveAddress, 4, 1))[0]; + await 确保连接(); + return (await ReadHoldingRegistersAsync(从站地址, 4, 1))[0]; } - public virtual async Task ReadFlowAsync() + public virtual async Task 读取流量() { - await EnsureConnectionAsync(); - var data = await ReadHoldingRegistersAsync(SlaveAddress, 17, 1); + await 确保连接(); + var data = await ReadHoldingRegistersAsync(从站地址, 17, 1); return Math.Round(data[0] / 10.0, 1); } #endregion - #region Write Methods + #region 写入方法 - public virtual async Task WriteFlowSettingAsync(double value) + public virtual async Task 写入流量设置(double 值) { - await EnsureConnectionAsync(); - short temp = (short)(value * 10); + await 确保连接(); + short temp = (short)(值 * 10); ushort result = Unsafe.As(ref temp); - await WriteSingleRegisterAsync(SlaveAddress, 24, result); + await WriteSingleRegisterAsync(从站地址, 24, result); } - public virtual async Task WriteDeviceStatusAsync(bool on) + public virtual async Task 写入设备状态(bool 开) { - await EnsureConnectionAsync(); - await WriteSingleRegisterAsync(SlaveAddress, 3, (ushort)(on ? 1 : 0)); + await 确保连接(); + await WriteSingleRegisterAsync(从站地址, 3, (ushort)(开 ? 1 : 0)); } - public virtual async Task WritePumpStatusAsync(bool on) + public virtual async Task 写入泵状态(bool 开) { - await EnsureConnectionAsync(); - await WriteSingleRegisterAsync(SlaveAddress, 4, (ushort)(on ? 1 : 0)); + await 确保连接(); + await WriteSingleRegisterAsync(从站地址, 4, (ushort)(开 ? 1 : 0)); } - public virtual async Task SetTemperatureAsync(double temperature) + public virtual async Task 设置温度(double 温度) { - await EnsureConnectionAsync(); - short temp = (short)(temperature * 100); + await 确保连接(); + short temp = (short)(温度 * 100); ushort result = Unsafe.As(ref temp); - await WriteSingleRegisterAsync(SlaveAddress, 2, result); + await WriteSingleRegisterAsync(从站地址, 2, result); } - public virtual async Task SetTemperatureUpperLimitAsync(double temperature) + public virtual async Task 设置温度上限(double 温度) { - await EnsureConnectionAsync(); - short temp = (short)(temperature * 100); + await 确保连接(); + short temp = (short)(温度 * 100); ushort result = Unsafe.As(ref temp); - await WriteSingleRegisterAsync(SlaveAddress, 5, result); + await WriteSingleRegisterAsync(从站地址, 5, result); } - public virtual async Task SetTemperatureLowerLimitAsync(double temperature) + public virtual async Task 设置温度下限(double 温度) { - await EnsureConnectionAsync(); - short temp = (short)(temperature * 100); + await 确保连接(); + short temp = (short)(温度 * 100); ushort result = Unsafe.As(ref temp); - await WriteSingleRegisterAsync(SlaveAddress, 6, result); + await WriteSingleRegisterAsync(从站地址, 6, result); } - public virtual async Task SetSoftwareOverTemperatureAsync(double upper, double lower) + public virtual async Task 设置软件超温(double 上限温度, double 下限温度) { - await EnsureConnectionAsync(); - short v1 = (short)(upper * 100); - short v2 = (short)(lower * 100); + await 确保连接(); + short v1 = (short)(上限温度 * 100); + short v2 = (short)(下限温度 * 100); ushort[] data = MemoryMarshal.Cast(new short[] { v1, v2 }).ToArray(); - await WriteSingleRegisterAsync(SlaveAddress, 30, data[0]); + await WriteSingleRegisterAsync(从站地址, 30, data[0]); await Task.Delay(5); - await WriteSingleRegisterAsync(SlaveAddress, 31, data[1]); + await WriteSingleRegisterAsync(从站地址, 31, data[1]); } - public virtual async Task SetHardwareOverTemperatureAsync(double upper) + public virtual async Task 设置硬件超温(double 上限温度) { - await EnsureConnectionAsync(); - short v1 = (short)(upper * 100); + await 确保连接(); + short v1 = (short)(上限温度 * 100); ushort result = Unsafe.As(ref v1); - await WriteSingleRegisterAsync(SlaveAddress, 32, result); + await WriteSingleRegisterAsync(从站地址, 32, result); } #endregion diff --git a/DeviceCommand/Device/PSB11000.cs b/DeviceCommand/Device/PSB11000.cs index c7b839a..04a423d 100644 --- a/DeviceCommand/Device/PSB11000.cs +++ b/DeviceCommand/Device/PSB11000.cs @@ -18,48 +18,42 @@ namespace DeviceCommand.Device #region 一、基础控制寄存器 - public virtual async Task SetRemoteControlAsync(bool activate, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置远程控制(bool 激活, byte slaveAddress = 1, CancellationToken ct = default) { - try - { - ushort value = (ushort)(activate ? 0xFF00 : 0x0000); - await WriteSingleRegisterAsync(slaveAddress, 402, value, ct); - } - catch (Exception) { - - } + ushort value = (ushort)(激活 ? 0xFF00 : 0x0000); + await WriteSingleRegisterAsync(slaveAddress, 402, value, ct); } - public virtual async Task SetOutputAsync(bool on, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置输出(bool 开启, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(on ? 0xFF00 : 0x0000); + ushort value = (ushort)(开启 ? 0xFF00 : 0x0000); await WriteSingleRegisterAsync(slaveAddress, 405, value, ct); } - public virtual async Task SetWorkModeAsync(bool useResistanceMode, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置工作模式(bool 使用电阻模式, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(useResistanceMode ? 0xFF00 : 0x0000); + ushort value = (ushort)(使用电阻模式 ? 0xFF00 : 0x0000); await WriteSingleRegisterAsync(slaveAddress, 409, value, ct); } - public virtual async Task ResetAlarmAsync(byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 重置报警(byte slaveAddress = 1, CancellationToken ct = default) { await WriteSingleRegisterAsync(slaveAddress, 411, 0xFF00, ct); } - public virtual async Task QueryRemoteControlAsync(byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询远程控制(byte slaveAddress = 1, CancellationToken ct = default) { ushort[] result = await ReadHoldingRegistersAsync(slaveAddress, 402, 1, ct); return result[0] == 0xFF00; } - public virtual async Task QueryOutputAsync(byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询输出(byte slaveAddress = 1, CancellationToken ct = default) { ushort[] result = await ReadHoldingRegistersAsync(slaveAddress, 405, 1, ct); return result[0] == 0xFF00; } - public virtual async Task QueryWorkModeAsync(byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询工作模式(byte slaveAddress = 1, CancellationToken ct = default) { ushort[] result = await ReadHoldingRegistersAsync(slaveAddress, 409, 1, ct); return result[0] == 0xFF00; @@ -69,70 +63,70 @@ namespace DeviceCommand.Device #region 二、电压/电流/功率/电阻设定值 - public virtual async Task SetVoltageAsync(double voltage, double ratedVoltage, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置电压(double 电压, double 额定电压, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(voltage / ratedVoltage * 0xCCCC); + ushort value = (ushort)(电压 / 额定电压 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 500, value, ct); } - public virtual async Task GetVoltageAsync(double ratedVoltage, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询电压(double 额定电压, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 500, 1, ct); - return reg[0] / (double)0xCCCC * ratedVoltage; + return reg[0] / (double)0xCCCC * 额定电压; } - public virtual async Task SetCurrentAsync(double current, double ratedCurrent, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置电流(double 电流, double 额定电流, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(current / ratedCurrent * 0xCCCC); + ushort value = (ushort)(电流 / 额定电流 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 501, value, ct); } - public virtual async Task GetCurrentAsync(double ratedCurrent, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询电流(double 额定电流, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 501, 1, ct); - return reg[0] / (double)0xCCCC * ratedCurrent; + return reg[0] / (double)0xCCCC * 额定电流; } - public virtual async Task SetPowerAsync(double power, double ratedPower, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置功率(double 功率, double 额定功率, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(power / ratedPower * 0xCCCC); + ushort value = (ushort)(功率 / 额定功率 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 502, value, ct); } - public virtual async Task GetPowerAsync(double ratedPower, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询功率(double 额定功率, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 502, 1, ct); - return reg[0] / (double)0xCCCC * ratedPower; + return reg[0] / (double)0xCCCC * 额定功率; } - public virtual async Task SetResistanceAsync(double resistance, double maxResistance, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 设置电阻(double 电阻, double 最大电阻, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(resistance / maxResistance * 0xCCCC); + ushort value = (ushort)(电阻 / 最大电阻 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 503, value, ct); } - public virtual async Task GetResistanceAsync(double maxResistance, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询电阻(double 最大电阻, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 503, 1, ct); - return reg[0] / (double)0xCCCC * maxResistance; + return reg[0] / (double)0xCCCC * 最大电阻; } #endregion - // 后面三、四、五区域的所有方法也同样加上 virtual - // 例如: - public virtual async Task SetSinkPowerAsync(double power, double sinkRatedPower, byte slaveAddress = 1, CancellationToken ct = default) + #region 三、Sink功率设定 + + public virtual async Task 设置Sink功率(double 功率, double Sink额定功率, byte slaveAddress = 1, CancellationToken ct = default) { - ushort value = (ushort)(power / sinkRatedPower * 0xCCCC); + ushort value = (ushort)(功率 / Sink额定功率 * 0xCCCC); await WriteSingleRegisterAsync(slaveAddress, 498, value, ct); } - public virtual async Task GetSinkPowerAsync(double sinkRatedPower, byte slaveAddress = 1, CancellationToken ct = default) + public virtual async Task 查询Sink功率(double Sink额定功率, byte slaveAddress = 1, CancellationToken ct = default) { ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 498, 1, ct); - return reg[0] / (double)0xCCCC * sinkRatedPower; + return reg[0] / (double)0xCCCC * Sink额定功率; } - // 类似方法全部加上 virtual,保证代理可以拦截 + #endregion } } diff --git a/DeviceCommand/Device/SQ0030G1D.cs b/DeviceCommand/Device/SQ0030G1D.cs index 6e83de6..71a56f6 100644 --- a/DeviceCommand/Device/SQ0030G1D.cs +++ b/DeviceCommand/Device/SQ0030G1D.cs @@ -18,10 +18,10 @@ namespace DeviceCommand.Device // ===================== 基础封装 ===================== - protected virtual Task WriteAsync(string cmd, CancellationToken ct = default) + protected virtual Task 写命令(string cmd, CancellationToken ct = default) => SendAsync(cmd + "\n", ct); - protected virtual async Task QueryAsync(string cmd, CancellationToken ct = default) + protected virtual async Task 查询命令(string cmd, CancellationToken ct = default) { await SendAsync(cmd + "\n", ct); return await ReadAsync(ct: ct); @@ -29,85 +29,84 @@ namespace DeviceCommand.Device // ===================== 1. 基础控制 ===================== - public virtual Task GetIdAsync(CancellationToken ct = default) - => QueryAsync("*IDN?", ct); + public async Task 启动电源() + { + await 写命令($"OUTPut 1\n"); + } - public virtual Task ResetAsync(CancellationToken ct = default) - => WriteAsync("*RST", ct); + public async Task 关闭电源() + { + await 写命令($"OUTPut 0\n"); + } - public virtual Task SetRemoteAsync(CancellationToken ct = default) - => WriteAsync("SYST:REM", ct); + public virtual Task 获取设备ID(CancellationToken ct = default) + => 查询命令("*IDN?", ct); - public virtual Task SetLocalAsync(CancellationToken ct = default) - => WriteAsync("SYST:LOC", ct); + public virtual Task 重置设备(CancellationToken ct = default) + => 写命令("*RST", ct); - public virtual Task SetOutputAsync(bool on, CancellationToken ct = default) - => WriteAsync($"OUTP:STAT {(on ? "ON" : "OFF")}", ct); + public virtual Task 设置远程控制(CancellationToken ct = default) + => 写命令("SYST:REM", ct); - public virtual async Task GetOutputAsync(CancellationToken ct = default) - => (await QueryAsync("OUTP:STAT?", ct)).Trim().ToUpper() == "ON"; + public virtual Task 设置本地控制(CancellationToken ct = default) + => 写命令("SYST:LOC", ct); + + public virtual Task 设置输出状态(bool on, CancellationToken ct = default) + => 写命令($"OUTP:STAT {(on ? "ON" : "OFF")}", ct); + + public virtual async Task 获取输出状态(CancellationToken ct = default) + => (await 查询命令("OUTP:STAT?", ct)).Trim().ToUpper() == "ON"; // ===================== 2. 电压/频率控制 ===================== - public virtual Task SetVoltageAsync(double value, CancellationToken ct = default) - => WriteAsync($"VOLT {value}", ct); + public virtual Task 设置输出电压(double value, CancellationToken ct = default) + => 写命令($"VOLT {value}", ct); - public virtual async Task GetVoltageAsync(CancellationToken ct = default) - => double.Parse((await QueryAsync("VOLT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture); + public virtual async Task 获取输出电压(CancellationToken ct = default) + => double.Parse((await 查询命令("VOLT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture); - public virtual Task SetFrequencyAsync(double freq, CancellationToken ct = default) - => WriteAsync($"FREQ {freq}", ct); + public virtual Task 设置输出频率(double freq, CancellationToken ct = default) + => 写命令($"FREQ {freq}", ct); - public virtual async Task GetFrequencyAsync(CancellationToken ct = default) - => double.Parse((await QueryAsync("FREQ?", ct)).Replace("Hz", ""), CultureInfo.InvariantCulture); + public virtual async Task 获取输出频率(CancellationToken ct = default) + => double.Parse((await 查询命令("FREQ?", ct)).Replace("Hz", ""), CultureInfo.InvariantCulture); // ===================== 3. 保护 ===================== - public virtual Task SetOVPAsync(double v, CancellationToken ct = default) - => WriteAsync($"VOLT:PROT {v}", ct); + public virtual Task 设置过压保护(double v, CancellationToken ct = default) + => 写命令($"VOLT:PROT {v}", ct); - public virtual async Task GetOVPAsync(CancellationToken ct = default) - => double.Parse((await QueryAsync("VOLT:PROT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture); + public virtual async Task 获取过压保护(CancellationToken ct = default) + => double.Parse((await 查询命令("VOLT:PROT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture); - public virtual Task SetOCPAsync(double a, CancellationToken ct = default) - => WriteAsync($"CURR:PROT {a}", ct); + public virtual Task 设置过流保护(double a, CancellationToken ct = default) + => 写命令($"CURR:PROT {a}", ct); - public virtual async Task GetOCPAsync(CancellationToken ct = default) - => double.Parse((await QueryAsync("CURR:PROT?", ct)).Replace("A", ""), CultureInfo.InvariantCulture); + public virtual async Task 获取过流保护(CancellationToken ct = default) + => double.Parse((await 查询命令("CURR:PROT?", ct)).Replace("A", ""), CultureInfo.InvariantCulture); - public virtual Task ClearProtectionAsync(CancellationToken ct = default) - => WriteAsync("SYST:PROT:CLE", ct); + public virtual Task 清除保护(CancellationToken ct = default) + => 写命令("SYST:PROT:CLE", ct); // ===================== 4. 测量 ===================== - public virtual async Task MeasureVoltageAsync(CancellationToken ct = default) - => double.Parse((await QueryAsync("MEAS:VOLT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture); + public virtual async Task 测量输出电压(CancellationToken ct = default) + => double.Parse((await 查询命令("MEAS:VOLT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture); - public virtual async Task MeasureCurrentAsync(CancellationToken ct = default) - => double.Parse((await QueryAsync("MEAS:CURR?", ct)).Replace("A", ""), CultureInfo.InvariantCulture); + public virtual async Task 测量输出电流(CancellationToken ct = default) + => double.Parse((await 查询命令("MEAS:CURR?", ct)).Replace("A", ""), CultureInfo.InvariantCulture); - public virtual async Task MeasurePowerAsync(CancellationToken ct = default) - => double.Parse((await QueryAsync("MEAS:POW?", ct)).Replace("W", ""), CultureInfo.InvariantCulture); + public virtual async Task 测量输出功率(CancellationToken ct = default) + => double.Parse((await 查询命令("MEAS:POW?", ct)).Replace("W", ""), CultureInfo.InvariantCulture); - public virtual async Task<(double volt, double curr, double pow)> MeasureArrayAsync(CancellationToken ct = default) + public virtual async Task<(double volt, double curr, double pow)> 测量所有参数(CancellationToken ct = default) { - var parts = (await QueryAsync("MEAS:ARR?", ct)).Split(','); + var parts = (await 查询命令("MEAS:ARR?", ct)).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) ); } - - // ===================== 5.(可选)瞬态 ===================== - - public virtual Task SetTransientOnAsync(bool on, CancellationToken ct = default) - => WriteAsync($"TRAN:STAT {(on ? "ON" : "OFF")}", ct); - - public virtual Task SetTransientStepVoltageAsync(int step, double volt, CancellationToken ct = default) - => WriteAsync($"TRAN:STEP {step}:VOLT {volt}", ct); - - public virtual Task RunTransientAsync(CancellationToken ct = default) - => WriteAsync("TRAN:RUN", ct); } } diff --git a/DeviceCommand/Device/WS-68030-380T.cs b/DeviceCommand/Device/WS-68030-380T.cs index 27531e5..4f4e21b 100644 --- a/DeviceCommand/Device/WS-68030-380T.cs +++ b/DeviceCommand/Device/WS-68030-380T.cs @@ -18,72 +18,32 @@ namespace DeviceCommand.Device private const byte SlaveAddress = 1; - #region 设置模式 - public virtual async Task Set_Mode_LocalAuto(CancellationToken ct = default) - => await WriteSingleRegisterAsync(SlaveAddress, 51, 0, ct); + + public async Task 设置功率_KW(float 功率, CancellationToken ct = default) + { + var send = ConvertFromFloat(功率); + await WriteMultipleRegistersAsync(1, 58, send).WaitAsync(ct); + } - public virtual async Task Set_Mode_LocalManual(CancellationToken ct = default) - => await WriteSingleRegisterAsync(SlaveAddress, 51, 1, ct); + public async Task 加载(CancellationToken ct = default) + { + var tmp = new ushort(); + ushort send = (ushort)(tmp | (1 << 8)); + await WriteSingleRegisterAsync(1, 70, send).WaitAsync(ct); + } - public virtual async Task Set_Mode_ATEControl(CancellationToken ct = default) - => await WriteSingleRegisterAsync(SlaveAddress, 51, 2, ct); + public async Task 卸载(CancellationToken ct = default) + { + await WriteSingleRegisterAsync(1, 70, new()).WaitAsync(ct); + } - public virtual async Task Set_AlarmMute(CancellationToken ct = default) - => await WriteSingleRegisterAsync(SlaveAddress, 52, 1, ct); + public async Task 读取功率_KW(CancellationToken ct = default) + { + var re = await ReadHoldingRegistersAsync(1, 58, 2).WaitAsync(ct); + return ConvertToFloat(re); + } - public virtual async Task Set_FaultReset(CancellationToken ct = default) - => await WriteSingleRegisterAsync(SlaveAddress, 53, 1, ct); - public virtual async Task Set_SystemStop(CancellationToken ct = default) - => await WriteSingleRegisterAsync(SlaveAddress, 54, 1, ct); - #endregion - - #region 负载模式 - public virtual async Task Set_ATE_Load_CurrentMode(CancellationToken ct = default) - => await WriteSingleRegisterAsync(SlaveAddress, 100, 0, ct); - - public virtual async Task Set_ATE_Load_PowerMode(CancellationToken ct = default) - => await WriteSingleRegisterAsync(SlaveAddress, 100, 1, ct); - - public virtual async Task Set_ATE_Load_Uninstall(CancellationToken ct = default) - => await WriteSingleRegisterAsync(SlaveAddress, 103, 0, ct); - - public virtual async Task Set_ATE_Load_Load(CancellationToken ct = default) - => await WriteSingleRegisterAsync(SlaveAddress, 103, 1, ct); - #endregion - - #region 设置电流/功率 - public virtual async Task Set_ATE_Load_A_Current(float current, CancellationToken ct = default) - => await WriteMultipleRegistersAsync(SlaveAddress, 105, ConvertFromFloat(current), ct); - - public virtual async Task Set_ATE_Load_A_Power(float power, CancellationToken ct = default) - => await WriteMultipleRegistersAsync(SlaveAddress, 109, ConvertFromFloat(power), ct); - - public virtual async Task Set_ATE_Load_B_Current(float current, CancellationToken ct = default) - => await WriteMultipleRegistersAsync(SlaveAddress, 115, ConvertFromFloat(current), ct); - - public virtual async Task Set_ATE_Load_B_Power(float power, CancellationToken ct = default) - => await WriteMultipleRegistersAsync(SlaveAddress, 119, ConvertFromFloat(power), ct); - - public virtual async Task Set_ATE_Load_C_Current(float current, CancellationToken ct = default) - => await WriteMultipleRegistersAsync(SlaveAddress, 125, ConvertFromFloat(current), ct); - - public virtual async Task Set_ATE_Load_C_Power(float power, CancellationToken ct = default) - => await WriteMultipleRegistersAsync(SlaveAddress, 129, ConvertFromFloat(power), ct); - #endregion - - #region 查询 - public virtual async Task Get_A_Voltage(CancellationToken ct = default) - => ConvertToFloat(await ReadHoldingRegistersAsync(SlaveAddress, 150, 2, ct)); - - public virtual async Task Get_A_Current(CancellationToken ct = default) - => ConvertToFloat(await ReadHoldingRegistersAsync(SlaveAddress, 152, 2, ct)); - - public virtual async Task Get_A_ActivePower(CancellationToken ct = default) - => ConvertToFloat(await ReadHoldingRegistersAsync(SlaveAddress, 154, 2, ct)); - - // …同理其他相和三相测量,直接调用基类 ReadHoldingRegistersAsync - #endregion #region 辅助方法 protected virtual float ConvertToFloat(ushort[] values) diff --git a/DeviceCommand/DeviceCommand.csproj b/DeviceCommand/DeviceCommand.csproj index 4364273..a861802 100644 --- a/DeviceCommand/DeviceCommand.csproj +++ b/DeviceCommand/DeviceCommand.csproj @@ -14,6 +14,7 @@ + diff --git a/Logger/LoggingInterceptor.cs b/Logger/LoggingInterceptor.cs index d1c5381..3a0c7e7 100644 --- a/Logger/LoggingInterceptor.cs +++ b/Logger/LoggingInterceptor.cs @@ -100,26 +100,11 @@ public class LoggingInterceptor : IInterceptor } catch (Exception ex) { - // 如果出现异常,记录异常并设置默认返回值 sw.Stop(); LoggerHelper.ErrorWithNotify($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 异常:{ex.Message}", ex.StackTrace); - // 根据返回类型设置默认值 - if (resultType == typeof(bool)) - defaultReturnValue = false; // 默认为 false - else if (resultType == typeof(bool[])) - defaultReturnValue = new bool[0]; // 默认为空数组 - else if (resultType == typeof(ushort[])) - defaultReturnValue = new ushort[0]; // 默认为空数组 - else if (resultType == typeof(string[])) - defaultReturnValue = new string[0]; // 默认为空数组 - else if (resultType == typeof(byte[])) - defaultReturnValue = new byte[0]; // 默认为空字节数组 - else if (resultType == typeof(void)) - defaultReturnValue = null; // 无返回值,设置为 null - - // 返回默认值 - invocation.ReturnValue = Task.FromResult(defaultReturnValue); + var exception = new Exception($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 时发生异常:{ex.Message}"); + invocation.ReturnValue = Task.FromException(exception); return; } }