增加了设备映射,整合了反灌电压类

This commit is contained in:
hsc 2025-11-24 13:10:18 +08:00
parent 3027c288c3
commit 4640b47331
18 changed files with 602 additions and 539 deletions

View File

@ -34,7 +34,7 @@ namespace BOB
} }
else else
{ {
SystemConfig.Instance.Title = "设备2";//模拟打开的设备 SystemConfig.Instance.Title = "设备1";//模拟打开的设备
} }
base.OnStartup(e); base.OnStartup(e);
} }

View File

@ -21,9 +21,9 @@ namespace BOB.Singleton
private readonly ProxyGenerator _proxyGen = new ProxyGenerator(); private readonly ProxyGenerator _proxyGen = new ProxyGenerator();
private readonly IInterceptor _loggingInterceptor = new LoggingInterceptor(); private readonly IInterceptor _loggingInterceptor = new LoggingInterceptor();
private ITcp E36233ADevice { get; set; } //private ITcp E36233ADevice { get; set; }
private ISerialPort IT6724CDevice { get; set; } private ISerialPort IT6724CDevice { get; set; }
private ISerialPort IT6724CReverseDevice{ get; set; } //private ISerialPort IT6724CReverseDevice{ get; set; }
private IModbusDevice EAEL9080Device { get; set; } private IModbusDevice EAEL9080Device { get; set; }
private IModbusDevice IOBoardevice { get; set; } private IModbusDevice IOBoardevice { get; set; }
private IModbusDevice LQ7500_DDevice { get; set; } private IModbusDevice LQ7500_DDevice { get; set; }
@ -31,6 +31,7 @@ namespace BOB.Singleton
private IModbusDevice WS_68030_380TDevice { get; set; } private IModbusDevice WS_68030_380TDevice { get; set; }
private ITcp SQ0030G1DTDevice { get; set; } private ITcp SQ0030G1DTDevice { get; set; }
private IModbusDevice ZXKSTDevice { get; set; } private IModbusDevice ZXKSTDevice { get; set; }
private Backfeed BackfeedDevice { get; set; }
public Devices() public Devices()
{ {
@ -46,13 +47,16 @@ namespace BOB.Singleton
case "DeviceCommand.Device.E36233A": case "DeviceCommand.Device.E36233A":
if (device.CommunicationConfig is TcpConfig tcp1) if (device.CommunicationConfig is TcpConfig tcp1)
{ {
E36233ADevice = _proxyGen.CreateClassProxyWithTarget( BackfeedDevice=new Backfeed(tcp1);
new E36233A(tcp1.IPAddress, tcp1.Port, tcp1.ReadTimeout, tcp1.WriteTimeout), DeviceDic["Backfeed"] = BackfeedDevice;
_loggingInterceptor }
); break;
DeviceDic["E36233A"] = E36233ADevice; case "DeviceCommand.Device.IT6724CReverse":
if (device.CommunicationConfig is SerialPortConfig sp3)
{
BackfeedDevice = new Backfeed(sp3);
DeviceDic["Backfeed"] = BackfeedDevice;
} }
else throw new InvalidOperationException("E36233A 必须使用 TcpConfig");
break; break;
case "DeviceCommand.Device.IT6724C": case "DeviceCommand.Device.IT6724C":
@ -67,18 +71,6 @@ namespace BOB.Singleton
else throw new InvalidOperationException("IT6724C 必须使用 SerialPortConfig"); else throw new InvalidOperationException("IT6724C 必须使用 SerialPortConfig");
break; 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": case "DeviceCommand.Device.LQ7500_D":
if (device.CommunicationConfig is SerialPortConfig sp2) if (device.CommunicationConfig is SerialPortConfig sp2)
{ {
@ -208,6 +200,19 @@ namespace BOB.Singleton
bool result = await iserialPortDevice.ConnectAsync(CancellationToken.None); bool result = await iserialPortDevice.ConnectAsync(CancellationToken.None);
LoggerHelper.SuccessWithNotify($"{name} 连接成功 (ISerialPort)"); 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 else
{ {
LoggerHelper.WarnWithNotify($"{name} 不是有效的设备类型"); LoggerHelper.WarnWithNotify($"{name} 不是有效的设备类型");

View File

@ -1,4 +1,5 @@
using BOB.Models; using BOB.Models;
using BOB.Singleton;
using Common.Attributes; using Common.Attributes;
using Logger; using Logger;
using Microsoft.IdentityModel.Logging; using Microsoft.IdentityModel.Logging;
@ -89,9 +90,11 @@ namespace BOB.ViewModels
public ICommand SearchEnterCommand { get; set; } public ICommand SearchEnterCommand { get; set; }
public ICommand TreeDoubleClickCommand { get; set; } public ICommand TreeDoubleClickCommand { get; set; }
public ICommand ReloadCommand { get; set; } public ICommand ReloadCommand { get; set; }
public CommandTreeViewModel(GlobalVariables _GlobalVariables) private Devices _devices { get; set; }
public CommandTreeViewModel(GlobalVariables _GlobalVariables, Devices devices)
{ {
_globalVariables= _GlobalVariables; _globalVariables= _GlobalVariables;
_devices = devices;
LoadedCommand = new DelegateCommand(Loaded); LoadedCommand = new DelegateCommand(Loaded);
SearchEnterCommand = new DelegateCommand(Search); SearchEnterCommand = new DelegateCommand(Search);
TreeDoubleClickCommand = new DelegateCommand<object>(TreeDoubleClick); TreeDoubleClickCommand = new DelegateCommand<object>(TreeDoubleClick);
@ -143,8 +146,8 @@ namespace BOB.ViewModels
private void Loaded() private void Loaded()
{ {
Directory.CreateDirectory(SystemConfig.Instance.DLLFilePath); //Directory.CreateDirectory(SystemConfig.Instance.DLLFilePath);
Directory.CreateDirectory(SystemConfig.Instance.SubProgramFilePath); //Directory.CreateDirectory(SystemConfig.Instance.SubProgramFilePath);
LoadAllAssemblies(); LoadAllAssemblies();
LoadSubPrograms(); LoadSubPrograms();
LoadInstructionsToTreeView(); LoadInstructionsToTreeView();
@ -267,7 +270,6 @@ namespace BOB.ViewModels
foreach (var assembly in Assemblies) foreach (var assembly in Assemblies)
{ {
List<Type> validTypes = new List<Type>(); List<Type> validTypes = new List<Type>();
try try
{ {
var types = assembly.GetTypes().Where(t => var types = assembly.GetTypes().Where(t =>
@ -307,6 +309,7 @@ namespace BOB.ViewModels
foreach (var type in validTypes) foreach (var type in validTypes)
{ {
if (!_devices.DeviceDic.ContainsKey(type.Name)&& assembly.GetName().Name!= "Command") continue;
var typeNode = new InstructionNode var typeNode = new InstructionNode
{ {
Name = type.Name, Name = type.Name,
@ -356,7 +359,7 @@ namespace BOB.ViewModels
private void GetPublicMethods(Type type, HashSet<MethodInfo> methods) private void GetPublicMethods(Type type, HashSet<MethodInfo> 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 && .Where(m => !m.IsSpecialName &&
m.DeclaringType != typeof(object)) m.DeclaringType != typeof(object))
.ToList(); .ToList();

BIN
DeviceCommand.zip Normal file

Binary file not shown.

View File

@ -62,7 +62,10 @@ namespace DeviceCommand.Base
await commLock.WaitAsync(ct); await commLock.WaitAsync(ct);
try try
{ {
if (!SerialPort.IsOpen) return; if (!SerialPort.IsOpen)
{
throw new InvalidOperationException("串口未打开,无法进行操作。");
}
byte[] bytes = Encoding.UTF8.GetBytes(data); byte[] bytes = Encoding.UTF8.GetBytes(data);
var timeoutTask = Task.Delay(WriteTimeout > 0 ? WriteTimeout : Timeout.Infinite, ct); var timeoutTask = Task.Delay(WriteTimeout > 0 ? WriteTimeout : Timeout.Infinite, ct);
@ -83,7 +86,10 @@ namespace DeviceCommand.Base
await commLock.WaitAsync(ct); await commLock.WaitAsync(ct);
try try
{ {
if (!SerialPort.IsOpen) return null; if (!SerialPort.IsOpen)
{
throw new InvalidOperationException("串口未打开,无法进行操作。");
}
delimiter ??= "\n"; delimiter ??= "\n";
var sb = new StringBuilder(); var sb = new StringBuilder();

View File

@ -52,7 +52,11 @@ namespace DeviceCommand.Base
public void Close() 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) public async Task SendAsync(byte[] buffer, CancellationToken ct = default)
@ -60,7 +64,10 @@ namespace DeviceCommand.Base
await _commLock.WaitAsync(ct); await _commLock.WaitAsync(ct);
try try
{ {
if (!TcpClient.Connected) return; if (!TcpClient.Connected)
{
throw new InvalidOperationException("TCP 没有连接成功");
}
NetworkStream stream = TcpClient.GetStream(); NetworkStream stream = TcpClient.GetStream();
var timeoutTask = Task.Delay(SendTimeout > 0 ? SendTimeout : Timeout.Infinite, ct); 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) public async Task SendAsync(string str, CancellationToken ct = default)
{ {
if (!TcpClient.Connected)
{
throw new InvalidOperationException("TCP 没有连接成功");
}
await SendAsync(Encoding.UTF8.GetBytes(str), ct); await SendAsync(Encoding.UTF8.GetBytes(str), ct);
} }
@ -87,7 +99,11 @@ namespace DeviceCommand.Base
await _commLock.WaitAsync(ct); await _commLock.WaitAsync(ct);
try try
{ {
if (!TcpClient.Connected) return null!; if (!TcpClient.Connected)
{
throw new InvalidOperationException("TCP 没有连接成功");
}
NetworkStream stream = TcpClient.GetStream(); NetworkStream stream = TcpClient.GetStream();
byte[] buffer = new byte[length]; byte[] buffer = new byte[length];
int offset = 0; int offset = 0;
@ -116,7 +132,11 @@ namespace DeviceCommand.Base
await _commLock.WaitAsync(ct); await _commLock.WaitAsync(ct);
try try
{ {
if (!TcpClient.Connected) return null!; if (!TcpClient.Connected)
{
throw new InvalidOperationException("TCP 没有连接成功");
}
delimiter ??= "\n"; delimiter ??= "\n";
var sb = new StringBuilder(); var sb = new StringBuilder();
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
@ -153,6 +173,11 @@ namespace DeviceCommand.Base
public async Task<string> WriteReadAsync(string command, string delimiter = "\n", CancellationToken ct = default) public async Task<string> WriteReadAsync(string command, string delimiter = "\n", CancellationToken ct = default)
{ {
if (!TcpClient.Connected)
{
throw new InvalidOperationException("TCP 没有连接成功");
}
await SendAsync(command, ct); await SendAsync(command, ct);
return await ReadAsync(delimiter, ct); return await ReadAsync(delimiter, ct);
} }

View File

@ -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
}
}

View File

@ -19,140 +19,81 @@ namespace DeviceCommand.Device
{ {
} }
#region #region
/// <summary>查询设备身份</summary> public async Task (CancellationToken ct = default)
public virtual Task<string> QueryIDAsync(CancellationToken ct = default)
=> SendCommandReadAsync("*IDN?", ct);
/// <summary>清除错误与状态</summary>
public virtual Task ClearStatusAsync(CancellationToken ct = default)
=> SendAsync("*CLS\r\n", ct);
/// <summary>系统复位</summary>
public virtual Task ResetAsync(CancellationToken ct = default)
=> SendAsync("*RST\r\n", ct);
#endregion
#region
/// <summary>选择通道 CH1 或 CH2</summary>
public virtual Task SelectChannelAsync(int channel, CancellationToken ct = default)
{ {
if (channel < 1 || channel > 2) await SendAsync($"PROTection:CLEar\r\n", ct);
throw new ArgumentOutOfRangeException(nameof(channel));
return SendAsync($"INST:SEL CH{channel}\r\n", ct);
} }
/// <summary>开关输出</summary> public async Task (bool , CancellationToken ct = default)
public virtual Task SetOutputAsync(bool on, string channels = "1", CancellationToken ct = default)
=> SendAsync($"OUTP {(on ? "ON" : "OFF")},(@{channels})\r\n", ct);
#endregion
#region /
/// <summary>设置电压 (0~30.9V)</summary>
public virtual Task SetVoltageAsync(double volt, string channels = "1", CancellationToken ct = default)
{ {
if (volt < 0 || volt > 30.9) throw new ArgumentOutOfRangeException(nameof(volt)); await SendAsync($"OUTPut {(开关 ? 1 : 0)}\r\n", ct);
return SendAsync($"SOUR:VOLT {volt},(@{channels})\r\n", ct);
} }
/// <summary>设置电流限值 (0~20.6A)</summary> public async Task (CancellationToken ct = default)
public virtual Task SetCurrentAsync(double curr, string channels = "1", CancellationToken ct = default)
{ {
if (curr < 0 || curr > 20.6) throw new ArgumentOutOfRangeException(nameof(curr)); await SendAsync($"SYSTem:REMote\r\n", ct);
return SendAsync($"SOUR:CURR {curr},(@{channels})\r\n", ct);
} }
/// <summary>设置过压保护</summary> public async Task (CancellationToken ct = default)
public virtual Task SetOVPAsync(double ovp, string channels = "1", CancellationToken ct = default)
=> SendAsync($"SOUR:VOLT:PROT {ovp},(@{channels})\r\n", ct);
/// <summary>设置过流保护</summary>
public virtual Task SetOCPAsync(double ocp, string channels = "1", CancellationToken ct = default)
=> SendAsync($"SOUR:CURR:PROT {ocp},(@{channels})\r\n", ct);
/// <summary>清除 OVP/OCP 错误</summary>
public virtual Task ClearProtectionAsync(string channels = "1", CancellationToken ct = default)
=> SendAsync($"SOUR:VOLT:PROT:CLE (@{channels})\r\n", ct);
#endregion
#region
/// <summary>设置电压上升斜率</summary>
public virtual Task SetRiseSlewAsync(double slew, string channels = "1", CancellationToken ct = default)
=> SendAsync($"SOUR:VOLT:SLEW:RIS {slew},(@{channels})\r\n", ct);
/// <summary>设置电压下降斜率</summary>
public virtual Task SetFallSlewAsync(double slew, string channels = "1", CancellationToken ct = default)
=> SendAsync($"SOUR:VOLT:SLEW:FALL {slew},(@{channels})\r\n", ct);
#endregion
#region
/// <summary>测量电压(V)</summary>
public virtual async Task<double> MeasureVoltageAsync(string channels = "1", CancellationToken ct = default)
{ {
string result = await SendCommandReadAsync($"MEAS:VOLT? (@{channels})", ct); await SendAsync($"SYSTem:BEEPer\r\n", ct);
return double.TryParse(result, out var value) ? value : 0;
} }
/// <summary>测量电流(A)</summary> public async Task (double , CancellationToken ct = default)
public virtual async Task<double> MeasureCurrentAsync(string channels = "1", CancellationToken ct = default)
{ {
string result = await SendCommandReadAsync($"MEAS:CURR? (@{channels})", ct); await SendAsync($"CURRent {电流}\r\n", ct);
return double.TryParse(result, out var value) ? value : 0;
} }
#endregion public async Task OCP电流(double , CancellationToken ct = default)
#region
public virtual async Task<bool> QueryOVPTrippedAsync(string channels = "1", CancellationToken ct = default)
{ {
string result = await SendCommandReadAsync($"SOUR:VOLT:PROT:TRIP? (@{channels})", ct); await SendAsync($"CURRent:PROTection {电流}\r\n", ct);
return result == "1";
} }
public virtual async Task<bool> 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); await SendAsync($"CURRent:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct);
return result == "1";
} }
#endregion public async Task (double , CancellationToken ct = default)
#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<string> SendCommandReadAsync(string cmd, CancellationToken ct = default)
{ {
await SendAsync(cmd + "\r\n", ct); await SendAsync($"VOLTage {电压}\r\n", ct);
return await ReadAsync("\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 #endregion

View File

@ -17,45 +17,45 @@ namespace DeviceCommand.Device
#region #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); 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); 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); await WriteSingleRegisterAsync(slaveAddress, 409, value, ct);
} }
public virtual async Task<uint> ReadDeviceStatusAsync(byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<uint> (byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 505, 2, ct); ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 505, 2, ct);
return (uint)(reg[0] << 16 | reg[1]); return (uint)(reg[0] << 16 | reg[1]);
} }
public virtual async Task<bool> IsDcOnAsync(byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<bool> (byte slaveAddress = 1, CancellationToken ct = default)
{ {
uint status = await ReadDeviceStatusAsync(slaveAddress, ct); uint status = await (slaveAddress, ct);
return (status & (1 << 7)) != 0; return (status & (1 << 7)) != 0;
} }
public virtual async Task<bool> IsRemoteActiveAsync(byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<bool> (byte slaveAddress = 1, CancellationToken ct = default)
{ {
uint status = await ReadDeviceStatusAsync(slaveAddress, ct); uint status = await (slaveAddress, ct);
return (status & (1 << 10)) != 0; return (status & (1 << 10)) != 0;
} }
public virtual async Task<bool> IsSinkModeActiveAsync(byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<bool> Sink模式状态(byte slaveAddress = 1, CancellationToken ct = default)
{ {
uint status = await ReadDeviceStatusAsync(slaveAddress, ct); uint status = await (slaveAddress, ct);
return (status & (1 << 12)) != 0; return (status & (1 << 12)) != 0;
} }
@ -63,117 +63,117 @@ namespace DeviceCommand.Device
#region / / / #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); await WriteSingleRegisterAsync(slaveAddress, 500, value, ct);
} }
public virtual async Task<double> GetVoltageAsync(double ratedVoltage, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> (double , byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 500, 1, ct); 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); await WriteSingleRegisterAsync(slaveAddress, 501, value, ct);
} }
public virtual async Task<double> GetCurrentAsync(double ratedCurrent, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> (double , byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 501, 1, ct); 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); await WriteSingleRegisterAsync(slaveAddress, 502, value, ct);
} }
public virtual async Task<double> GetPowerAsync(double ratedPower, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> (double , byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 502, 1, ct); 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); await WriteSingleRegisterAsync(slaveAddress, 503, value, ct);
} }
public virtual async Task<double> GetResistanceAsync(double maxResistance, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> (double , byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 503, 1, ct); ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 503, 1, ct);
return reg[0] / (double)0xCCCC * maxResistance; return reg[0] / (double)0xCCCC * ;
} }
#endregion #endregion
#region Sink #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); 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); 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); await WriteSingleRegisterAsync(slaveAddress, 504, value, ct);
} }
public virtual async Task<double> GetSinkPowerAsync(double sinkRatedPower, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> Sink功率(double Sink额定功率, byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 498, 1, ct); ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 498, 1, ct);
return reg[0] / (double)0xCCCC * sinkRatedPower; return reg[0] / (double)0xCCCC * Sink额定功率;
} }
public virtual async Task<double> GetSinkCurrentAsync(double sinkRatedCurrent, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> Sink电流(double Sink额定电流, byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 499, 1, ct); ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 499, 1, ct);
return reg[0] / (double)0xCCCC * sinkRatedCurrent; return reg[0] / (double)0xCCCC * Sink额定电流;
} }
public virtual async Task<double> GetSinkResistanceAsync(double sinkMaxResistance, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> Sink电阻(double Sink最大电阻, byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 504, 1, ct); ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 504, 1, ct);
return reg[0] / (double)0xCCCC * sinkMaxResistance; return reg[0] / (double)0xCCCC * Sink最大电阻;
} }
#endregion #endregion
#region #region
public virtual async Task<double> ReadActualVoltageAsync(double ratedVoltage, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> (double , byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 507, 1, ct); ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 507, 1, ct);
return reg[0] / (double)0xCCCC * ratedVoltage; return reg[0] / (double)0xCCCC * ;
} }
public virtual async Task<double> ReadActualCurrentAsync(double ratedCurrent, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> (double , byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 508, 1, ct); ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 508, 1, ct);
double current = reg[0] / (double)0xCCCC * ratedCurrent; double current = reg[0] / (double)0xCCCC * ;
if (await IsSinkModeActiveAsync(slaveAddress, ct)) current = -current; if (await Sink模式状态(slaveAddress, ct)) current = -current;
return current; return current;
} }
public virtual async Task<double> ReadActualPowerAsync(double ratedPower, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> (double , byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 509, 1, ct); ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 509, 1, ct);
double power = reg[0] / (double)0xCCCC * ratedPower; double power = reg[0] / (double)0xCCCC * ;
if (await IsSinkModeActiveAsync(slaveAddress, ct)) power = -power; if (await Sink模式状态(slaveAddress, ct)) power = -power;
return power; return power;
} }
@ -181,21 +181,21 @@ namespace DeviceCommand.Device
#region #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); 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); 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); await WriteSingleRegisterAsync(slaveAddress, 512, value, ct);
} }

View File

@ -9,26 +9,26 @@ using System.Threading.Tasks;
namespace DeviceCommand.Device namespace DeviceCommand.Device
{ {
[BOBCommand] [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 IOBoard()
{ {
} }
public virtual async Task WriteOutput(byte slaveId, ushort startAddress, ushort value)
public virtual async Task (byte , ushort , ushort )
{ {
await WriteSingleRegisterAsync(slaveId, startAddress, value); await WriteSingleRegisterAsync(, , );
} }
public virtual async Task WriteOutputsBatch(byte slaveId, ushort startAddress, ushort[] values) public virtual async Task (byte , ushort , ushort[] )
{ {
await WriteMultipleRegistersAsync(slaveId, startAddress, values); await WriteMultipleRegistersAsync(, , );
} }
} }
} }

View File

@ -20,76 +20,76 @@ namespace DeviceCommand.Device
#region #region
/// <summary> /// <summary>
/// 设置输出状态true 为开false 为关 /// 设置电源输出状态true 为开false 为关
/// </summary> /// </summary>
public virtual Task SetOutputAsync(bool state, CancellationToken ct = default) public virtual async Task (bool , CancellationToken ct = default)
=> SendAsync($"OUTPut {(state ? 1 : 0)}\r\n", ct); =>await SendAsync($"OUTPut {(开关 ? 1 : 0)}\r\n", ct);
/// <summary> /// <summary>
/// 切换到远程控制模式 /// 切换到远程控制模式
/// </summary> /// </summary>
public virtual Task SetRemoteModeAsync(CancellationToken ct = default) public virtual async Task (CancellationToken ct = default)
=> SendAsync("SYSTem:REMote\r\n", ct); =>await SendAsync("SYSTem:REMote\r\n", ct);
/// <summary> /// <summary>
/// 蜂鸣器测试 /// 蜂鸣器测试
/// </summary> /// </summary>
public virtual Task BeeperTestAsync(CancellationToken ct = default) public virtual async Task (CancellationToken ct = default)
=> SendAsync("SYSTem:BEEPer\r\n", ct); =>await SendAsync("SYSTem:BEEPer\r\n", ct);
/// <summary> /// <summary>
/// 设置输出电流单位A /// 设置输出电流单位A
/// </summary> /// </summary>
public virtual Task SetCurrentAsync(double current, CancellationToken ct = default) public virtual async Task (double , CancellationToken ct = default)
=> SendAsync($"CURRent {current}\r\n", ct); =>await SendAsync($"CURRent {电流}\r\n", ct);
/// <summary> /// <summary>
/// 设置过流保护电流OCP单位A /// 设置过流保护电流OCP单位A
/// </summary> /// </summary>
public virtual Task SetOCPCurrentAsync(double current, CancellationToken ct = default) public virtual async Task OCP电流(double , CancellationToken ct = default)
=> SendAsync($"CURRent:PROTection {current}\r\n", ct); =>await SendAsync($"CURRent:PROTection {电流}\r\n", ct);
/// <summary> /// <summary>
/// 设置过流保护状态true 为启用false 为禁用 /// 设置过流保护状态true 为启用false 为禁用
/// </summary> /// </summary>
public virtual Task SetOCPStateAsync(bool state, CancellationToken ct = default) public virtual async Task OCP开关(bool , CancellationToken ct = default)
=> SendAsync($"CURRent:PROTection:STATe {(state ? 1 : 0)}\r\n", ct); =>await SendAsync($"CURRent:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct);
/// <summary> /// <summary>
/// 清除过流保护触发状态 /// 清除过流保护触发状态
/// </summary> /// </summary>
public virtual Task ClearOCPAsync(CancellationToken ct = default) public virtual async Task (CancellationToken ct = default)
=> SendAsync("CURRent:PROTection:CLEar\r\n", ct); =>await SendAsync("CURRent:PROTection:CLEar\r\n", ct);
/// <summary> /// <summary>
/// 设置输出电压单位V /// 设置输出电压单位V
/// </summary> /// </summary>
public virtual Task SetVoltageAsync(double voltage, CancellationToken ct = default) public virtual async Task (double , CancellationToken ct = default)
=> SendAsync($"VOLTage {voltage}\r\n", ct); =>await SendAsync($"VOLTage {电压}\r\n", ct);
/// <summary> /// <summary>
/// 设置过压保护电压OVP单位V /// 设置过压保护电压OVP单位V
/// </summary> /// </summary>
public virtual Task SetOVPVoltageAsync(double voltage, CancellationToken ct = default) public virtual async Task OVP电压(double , CancellationToken ct = default)
=> SendAsync($"VOLT:PROTection {voltage}\r\n", ct); =>await SendAsync($"VOLT:PROTection {电压}\r\n", ct);
/// <summary> /// <summary>
/// 设置过压保护状态true 为启用false 为禁用 /// 设置过压保护状态true 为启用false 为禁用
/// </summary> /// </summary>
public virtual Task SetOVPStateAsync(bool state, CancellationToken ct = default) public virtual async Task OVP开关(bool , CancellationToken ct = default)
=> SendAsync($"VOLT:PROTection:STATe {(state ? 1 : 0)}\r\n", ct); =>await SendAsync($"VOLT:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct);
/// <summary> /// <summary>
/// 清除过压保护触发状态 /// 清除过压保护触发状态
/// </summary> /// </summary>
public virtual Task ClearOVPAsync(CancellationToken ct = default) public virtual async Task (CancellationToken ct = default)
=> SendAsync("VOLT:PROTection:CLEar\r\n", ct); => await SendAsync("VOLT:PROTection:CLEar\r\n", ct);
/// <summary> /// <summary>
/// 发送自定义命令 /// 发送自定义命令
/// </summary> /// </summary>
public virtual Task SendCustomCommandAsync(string command, CancellationToken ct = default) public virtual async Task (string , CancellationToken ct = default)
=> SendAsync($"{command}\r\n", ct); =>await SendAsync($"{指令}\r\n", ct);
#endregion #endregion
@ -98,7 +98,7 @@ namespace DeviceCommand.Device
/// <summary> /// <summary>
/// 查询仪器标识,返回制造商、型号、序列号、固件版本 /// 查询仪器标识,返回制造商、型号、序列号、固件版本
/// </summary> /// </summary>
public virtual async Task<string> QueryIDAsync(CancellationToken ct = default) public virtual async Task<string> (CancellationToken ct = default)
{ {
await SendAsync("*IDN?\r\n", ct); await SendAsync("*IDN?\r\n", ct);
return await ReadAsync(ct: ct); return await ReadAsync(ct: ct);
@ -107,7 +107,7 @@ namespace DeviceCommand.Device
/// <summary> /// <summary>
/// 查询过流保护是否触发,返回 true 表示触发 /// 查询过流保护是否触发,返回 true 表示触发
/// </summary> /// </summary>
public virtual async Task<bool> QueryOCPTrippedAsync(CancellationToken ct = default) public virtual async Task<bool> (CancellationToken ct = default)
{ {
await SendAsync("CURRent:PROTection:TRIPed?\r\n", ct); await SendAsync("CURRent:PROTection:TRIPed?\r\n", ct);
var result = await ReadAsync(ct: ct); var result = await ReadAsync(ct: ct);
@ -117,7 +117,7 @@ namespace DeviceCommand.Device
/// <summary> /// <summary>
/// 查询过压保护是否触发,返回 true 表示触发 /// 查询过压保护是否触发,返回 true 表示触发
/// </summary> /// </summary>
public virtual async Task<bool> QueryOVPTrippedAsync(CancellationToken ct = default) public virtual async Task<bool> (CancellationToken ct = default)
{ {
await SendAsync("VOLT:PROTection:TRIPed?\r\n", ct); await SendAsync("VOLT:PROTection:TRIPed?\r\n", ct);
var result = await ReadAsync(ct: ct); var result = await ReadAsync(ct: ct);
@ -127,7 +127,7 @@ namespace DeviceCommand.Device
/// <summary> /// <summary>
/// 查询实际输出电流单位A /// 查询实际输出电流单位A
/// </summary> /// </summary>
public virtual async Task<double> QueryCurrentAsync(CancellationToken ct = default) public virtual async Task<double> (CancellationToken ct = default)
{ {
await SendAsync("MEASure:CURRent?\r\n", ct); await SendAsync("MEASure:CURRent?\r\n", ct);
var result = await ReadAsync(ct: ct); var result = await ReadAsync(ct: ct);
@ -137,7 +137,7 @@ namespace DeviceCommand.Device
/// <summary> /// <summary>
/// 查询实际输出电压单位V /// 查询实际输出电压单位V
/// </summary> /// </summary>
public virtual async Task<double> QueryVoltageAsync(CancellationToken ct = default) public virtual async Task<double> (CancellationToken ct = default)
{ {
await SendAsync("MEASure:VOLTage?\r\n", ct); await SendAsync("MEASure:VOLTage?\r\n", ct);
var result = await ReadAsync(ct: ct); var result = await ReadAsync(ct: ct);
@ -147,7 +147,7 @@ namespace DeviceCommand.Device
/// <summary> /// <summary>
/// 查询实际输出功率单位W /// 查询实际输出功率单位W
/// </summary> /// </summary>
public virtual async Task<double> QueryPowerAsync(CancellationToken ct = default) public virtual async Task<double> (CancellationToken ct = default)
{ {
await SendAsync("MEASure:POWer?\r\n", ct); await SendAsync("MEASure:POWer?\r\n", ct);
var result = await ReadAsync(ct: ct); var result = await ReadAsync(ct: ct);

View File

@ -19,86 +19,50 @@ namespace DeviceCommand.Device
#region #region
public virtual Task SetOutputAsync(bool state, CancellationToken ct = default) public virtual async Task (CancellationToken ct = default)
=> SendAsync($"OUTPut {(state ? 1 : 0)}\r\n", ct); =>await SendAsync("PROTection:CLEar\r\n", ct);
public virtual Task SetRemoteModeAsync(CancellationToken ct = default) public virtual async Task (bool , CancellationToken ct = default)
=> SendAsync("SYSTem:REMote\r\n", ct); =>await SendAsync($"OUTPut {(开关 ? 1 : 0)}\r\n", ct);
public virtual Task BeeperTestAsync(CancellationToken ct = default) public virtual async Task (CancellationToken ct = default)
=> SendAsync("SYSTem:BEEPer\r\n", ct); =>await SendAsync("SYSTem:REMote\r\n", ct);
public virtual Task SetCurrentAsync(double current, CancellationToken ct = default) public virtual async Task (CancellationToken ct = default)
=> SendAsync($"CURRent {current}\r\n", ct); =>await SendAsync("SYSTem:BEEPer\r\n", ct);
public virtual Task SetOCPCurrentAsync(double current, CancellationToken ct = default) public virtual async Task (double , CancellationToken ct = default)
=> SendAsync($"CURRent:PROTection {current}\r\n", ct); =>await SendAsync($"CURRent {电流}\r\n", ct);
public virtual Task SetOCPStateAsync(bool state, CancellationToken ct = default) public virtual async Task OCP电流(double , CancellationToken ct = default)
=> SendAsync($"CURRent:PROTection:STATe {(state ? 1 : 0)}\r\n", ct); =>await SendAsync($"CURRent:PROTection {电流}\r\n", ct);
public virtual Task ClearOCPAsync(CancellationToken ct = default) public virtual async Task OCP开关(bool , CancellationToken ct = default)
=> SendAsync("CURRent:PROTection:CLEar\r\n", ct); =>await SendAsync($"CURRent:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct);
public virtual Task SetVoltageAsync(double voltage, CancellationToken ct = default) public virtual async Task (double , CancellationToken ct = default)
=> SendAsync($"VOLTage {voltage}\r\n", ct); =>await SendAsync($"VOLTage {电压}\r\n", ct);
public virtual Task SetOVPVoltageAsync(double voltage, CancellationToken ct = default) public virtual async Task OVP电压(double , CancellationToken ct = default)
=> SendAsync($"VOLT:PROTection {voltage}\r\n", ct); =>await SendAsync($"VOLT:PROTection {电压}\r\n", ct);
public virtual Task SetOVPStateAsync(bool state, CancellationToken ct = default) public virtual async Task OVP开关(bool , CancellationToken ct = default)
=> SendAsync($"VOLT:PROTection:STATe {(state ? 1 : 0)}\r\n", ct); =>await SendAsync($"VOLT:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct);
public virtual Task ClearOVPAsync(CancellationToken ct = default) public virtual async Task (double , CancellationToken ct = default)
=> SendAsync("VOLT:PROTection:CLEar\r\n", ct); =>await SendAsync($"POWer:PROTection {电压}\r\n", ct);
public virtual Task SendCustomCommandAsync(string command, CancellationToken ct = default) public virtual async Task (bool , CancellationToken ct = default)
=> SendAsync($"{command}\r\n", ct); =>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<string> QueryIDAsync(CancellationToken ct = default) public virtual async Task (string , CancellationToken ct = default)
{ =>await SendAsync($"{指令}\r\n", ct);
await SendAsync("*IDN?\r\n", ct);
return await ReadAsync(ct: ct);
}
public virtual async Task<bool> 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<bool> 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<double> 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<double> 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<double> QueryPowerAsync(CancellationToken ct = default)
{
await SendAsync("MEASure:POWer?\r\n", ct);
var result = await ReadAsync(ct: ct);
return Convert.ToDouble(result);
}
#endregion #endregion
} }

View File

@ -11,138 +11,138 @@ namespace DeviceCommand.Device
[BOBCommand] [BOBCommand]
public class LQ7500_D : ModbusRtu 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 LQ7500_D() { }
public byte SlaveAddress { get; set; } = 1; // default slave address public byte { get; set; } = 1; // 默认从站地址
private static readonly int[] Pow10Table = private static readonly int[] Pow10Table =
{ 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
private async Task EnsureConnectionAsync() private async Task ()
{ {
if (SerialPort == null || !SerialPort.IsOpen) if (SerialPort == null || !SerialPort.IsOpen)
await ConnectAsync(); await ConnectAsync();
} }
#region Read Methods #region
public virtual async Task<ushort> ReadTemperatureResolutionAsync() public virtual async Task<ushort> ()
{ {
await EnsureConnectionAsync(); await ();
return (await ReadHoldingRegistersAsync(SlaveAddress, 35, 1))[0]; return (await ReadHoldingRegistersAsync(, 35, 1))[0];
} }
public virtual async Task<double> ReadInternalSensorTemperatureAsync(ushort resolution = 0) public virtual async Task<double> (ushort = 0)
{ {
await EnsureConnectionAsync(); await ();
if (resolution == 0) resolution = await ReadTemperatureResolutionAsync(); if ( == 0) = await ();
var data = await ReadHoldingRegistersAsync(SlaveAddress, 0, 1); var data = await ReadHoldingRegistersAsync(, 0, 1);
return Math.Round(data[0] / (double)Pow10Table[resolution], resolution + 1); return Math.Round(data[0] / (double)Pow10Table[], + 1);
} }
public virtual async Task<double> ReadExternalSensorTemperatureAsync(ushort resolution = 0) public virtual async Task<double> (ushort = 0)
{ {
await EnsureConnectionAsync(); await ();
if (resolution == 0) resolution = await ReadTemperatureResolutionAsync(); if ( == 0) = await ();
var data = await ReadHoldingRegistersAsync(SlaveAddress, 1, 1); var data = await ReadHoldingRegistersAsync(, 1, 1);
return data[0] / (double)Pow10Table[resolution]; return data[0] / (double)Pow10Table[];
} }
public virtual async Task<ushort> ReadFaultCodeAsync() public virtual async Task<ushort> ()
{ {
await EnsureConnectionAsync(); await ();
return (await ReadHoldingRegistersAsync(SlaveAddress, 1, 1))[0]; return (await ReadHoldingRegistersAsync(, 1, 1))[0];
} }
public virtual async Task<ushort> ReadDeviceStatusAsync() public virtual async Task<ushort> ()
{ {
await EnsureConnectionAsync(); await ();
return (await ReadHoldingRegistersAsync(SlaveAddress, 3, 1))[0]; return (await ReadHoldingRegistersAsync(, 3, 1))[0];
} }
public virtual async Task<ushort> ReadPumpStatusAsync() public virtual async Task<ushort> ()
{ {
await EnsureConnectionAsync(); await ();
return (await ReadHoldingRegistersAsync(SlaveAddress, 4, 1))[0]; return (await ReadHoldingRegistersAsync(, 4, 1))[0];
} }
public virtual async Task<double> ReadFlowAsync() public virtual async Task<double> ()
{ {
await EnsureConnectionAsync(); await ();
var data = await ReadHoldingRegistersAsync(SlaveAddress, 17, 1); var data = await ReadHoldingRegistersAsync(, 17, 1);
return Math.Round(data[0] / 10.0, 1); return Math.Round(data[0] / 10.0, 1);
} }
#endregion #endregion
#region Write Methods #region
public virtual async Task WriteFlowSettingAsync(double value) public virtual async Task (double )
{ {
await EnsureConnectionAsync(); await ();
short temp = (short)(value * 10); short temp = (short)( * 10);
ushort result = Unsafe.As<short, ushort>(ref temp); ushort result = Unsafe.As<short, ushort>(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 ();
await WriteSingleRegisterAsync(SlaveAddress, 3, (ushort)(on ? 1 : 0)); await WriteSingleRegisterAsync(, 3, (ushort)( ? 1 : 0));
} }
public virtual async Task WritePumpStatusAsync(bool on) public virtual async Task (bool )
{ {
await EnsureConnectionAsync(); await ();
await WriteSingleRegisterAsync(SlaveAddress, 4, (ushort)(on ? 1 : 0)); await WriteSingleRegisterAsync(, 4, (ushort)( ? 1 : 0));
} }
public virtual async Task SetTemperatureAsync(double temperature) public virtual async Task (double )
{ {
await EnsureConnectionAsync(); await ();
short temp = (short)(temperature * 100); short temp = (short)( * 100);
ushort result = Unsafe.As<short, ushort>(ref temp); ushort result = Unsafe.As<short, ushort>(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(); await ();
short temp = (short)(temperature * 100); short temp = (short)( * 100);
ushort result = Unsafe.As<short, ushort>(ref temp); ushort result = Unsafe.As<short, ushort>(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(); await ();
short temp = (short)(temperature * 100); short temp = (short)( * 100);
ushort result = Unsafe.As<short, ushort>(ref temp); ushort result = Unsafe.As<short, ushort>(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(); await ();
short v1 = (short)(upper * 100); short v1 = (short)( * 100);
short v2 = (short)(lower * 100); short v2 = (short)( * 100);
ushort[] data = MemoryMarshal.Cast<short, ushort>(new short[] { v1, v2 }).ToArray(); ushort[] data = MemoryMarshal.Cast<short, ushort>(new short[] { v1, v2 }).ToArray();
await WriteSingleRegisterAsync(SlaveAddress, 30, data[0]); await WriteSingleRegisterAsync(, 30, data[0]);
await Task.Delay(5); 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(); await ();
short v1 = (short)(upper * 100); short v1 = (short)( * 100);
ushort result = Unsafe.As<short, ushort>(ref v1); ushort result = Unsafe.As<short, ushort>(ref v1);
await WriteSingleRegisterAsync(SlaveAddress, 32, result); await WriteSingleRegisterAsync(, 32, result);
} }
#endregion #endregion

View File

@ -18,48 +18,42 @@ namespace DeviceCommand.Device
#region #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)( ? 0xFF00 : 0x0000);
{ await WriteSingleRegisterAsync(slaveAddress, 402, value, ct);
ushort value = (ushort)(activate ? 0xFF00 : 0x0000);
await WriteSingleRegisterAsync(slaveAddress, 402, value, ct);
}
catch (Exception) {
}
} }
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); 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); 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); await WriteSingleRegisterAsync(slaveAddress, 411, 0xFF00, ct);
} }
public virtual async Task<bool> QueryRemoteControlAsync(byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<bool> (byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] result = await ReadHoldingRegistersAsync(slaveAddress, 402, 1, ct); ushort[] result = await ReadHoldingRegistersAsync(slaveAddress, 402, 1, ct);
return result[0] == 0xFF00; return result[0] == 0xFF00;
} }
public virtual async Task<bool> QueryOutputAsync(byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<bool> (byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] result = await ReadHoldingRegistersAsync(slaveAddress, 405, 1, ct); ushort[] result = await ReadHoldingRegistersAsync(slaveAddress, 405, 1, ct);
return result[0] == 0xFF00; return result[0] == 0xFF00;
} }
public virtual async Task<bool> QueryWorkModeAsync(byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<bool> (byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] result = await ReadHoldingRegistersAsync(slaveAddress, 409, 1, ct); ushort[] result = await ReadHoldingRegistersAsync(slaveAddress, 409, 1, ct);
return result[0] == 0xFF00; return result[0] == 0xFF00;
@ -69,70 +63,70 @@ namespace DeviceCommand.Device
#region /// #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); await WriteSingleRegisterAsync(slaveAddress, 500, value, ct);
} }
public virtual async Task<double> GetVoltageAsync(double ratedVoltage, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> (double , byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 500, 1, ct); 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); await WriteSingleRegisterAsync(slaveAddress, 501, value, ct);
} }
public virtual async Task<double> GetCurrentAsync(double ratedCurrent, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> (double , byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 501, 1, ct); 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); await WriteSingleRegisterAsync(slaveAddress, 502, value, ct);
} }
public virtual async Task<double> GetPowerAsync(double ratedPower, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> (double , byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 502, 1, ct); 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); await WriteSingleRegisterAsync(slaveAddress, 503, value, ct);
} }
public virtual async Task<double> GetResistanceAsync(double maxResistance, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> (double , byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 503, 1, ct); ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 503, 1, ct);
return reg[0] / (double)0xCCCC * maxResistance; return reg[0] / (double)0xCCCC * ;
} }
#endregion #endregion
// 后面三、四、五区域的所有方法也同样加上 virtual #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); await WriteSingleRegisterAsync(slaveAddress, 498, value, ct);
} }
public virtual async Task<double> GetSinkPowerAsync(double sinkRatedPower, byte slaveAddress = 1, CancellationToken ct = default) public virtual async Task<double> Sink功率(double Sink额定功率, byte slaveAddress = 1, CancellationToken ct = default)
{ {
ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 498, 1, ct); ushort[] reg = await ReadHoldingRegistersAsync(slaveAddress, 498, 1, ct);
return reg[0] / (double)0xCCCC * sinkRatedPower; return reg[0] / (double)0xCCCC * Sink额定功率;
} }
// 类似方法全部加上 virtual保证代理可以拦截 #endregion
} }
} }

View File

@ -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); => SendAsync(cmd + "\n", ct);
protected virtual async Task<string> QueryAsync(string cmd, CancellationToken ct = default) protected virtual async Task<string> (string cmd, CancellationToken ct = default)
{ {
await SendAsync(cmd + "\n", ct); await SendAsync(cmd + "\n", ct);
return await ReadAsync(ct: ct); return await ReadAsync(ct: ct);
@ -29,85 +29,84 @@ namespace DeviceCommand.Device
// ===================== 1. 基础控制 ===================== // ===================== 1. 基础控制 =====================
public virtual Task<string> GetIdAsync(CancellationToken ct = default) public async Task ()
=> QueryAsync("*IDN?", ct); {
await ($"OUTPut 1\n");
}
public virtual Task ResetAsync(CancellationToken ct = default) public async Task ()
=> WriteAsync("*RST", ct); {
await ($"OUTPut 0\n");
}
public virtual Task SetRemoteAsync(CancellationToken ct = default) public virtual Task<string> ID(CancellationToken ct = default)
=> WriteAsync("SYST:REM", ct); => ("*IDN?", ct);
public virtual Task SetLocalAsync(CancellationToken ct = default) public virtual Task (CancellationToken ct = default)
=> WriteAsync("SYST:LOC", ct); => ("*RST", ct);
public virtual Task SetOutputAsync(bool on, CancellationToken ct = default) public virtual Task (CancellationToken ct = default)
=> WriteAsync($"OUTP:STAT {(on ? "ON" : "OFF")}", ct); => ("SYST:REM", ct);
public virtual async Task<bool> GetOutputAsync(CancellationToken ct = default) public virtual Task (CancellationToken ct = default)
=> (await QueryAsync("OUTP:STAT?", ct)).Trim().ToUpper() == "ON"; => ("SYST:LOC", ct);
public virtual Task (bool on, CancellationToken ct = default)
=> ($"OUTP:STAT {(on ? "ON" : "OFF")}", ct);
public virtual async Task<bool> (CancellationToken ct = default)
=> (await ("OUTP:STAT?", ct)).Trim().ToUpper() == "ON";
// ===================== 2. 电压/频率控制 ===================== // ===================== 2. 电压/频率控制 =====================
public virtual Task SetVoltageAsync(double value, CancellationToken ct = default) public virtual Task (double value, CancellationToken ct = default)
=> WriteAsync($"VOLT {value}", ct); => ($"VOLT {value}", ct);
public virtual async Task<double> GetVoltageAsync(CancellationToken ct = default) public virtual async Task<double> (CancellationToken ct = default)
=> double.Parse((await QueryAsync("VOLT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture); => double.Parse((await ("VOLT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture);
public virtual Task SetFrequencyAsync(double freq, CancellationToken ct = default) public virtual Task (double freq, CancellationToken ct = default)
=> WriteAsync($"FREQ {freq}", ct); => ($"FREQ {freq}", ct);
public virtual async Task<double> GetFrequencyAsync(CancellationToken ct = default) public virtual async Task<double> (CancellationToken ct = default)
=> double.Parse((await QueryAsync("FREQ?", ct)).Replace("Hz", ""), CultureInfo.InvariantCulture); => double.Parse((await ("FREQ?", ct)).Replace("Hz", ""), CultureInfo.InvariantCulture);
// ===================== 3. 保护 ===================== // ===================== 3. 保护 =====================
public virtual Task SetOVPAsync(double v, CancellationToken ct = default) public virtual Task (double v, CancellationToken ct = default)
=> WriteAsync($"VOLT:PROT {v}", ct); => ($"VOLT:PROT {v}", ct);
public virtual async Task<double> GetOVPAsync(CancellationToken ct = default) public virtual async Task<double> (CancellationToken ct = default)
=> double.Parse((await QueryAsync("VOLT:PROT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture); => double.Parse((await ("VOLT:PROT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture);
public virtual Task SetOCPAsync(double a, CancellationToken ct = default) public virtual Task (double a, CancellationToken ct = default)
=> WriteAsync($"CURR:PROT {a}", ct); => ($"CURR:PROT {a}", ct);
public virtual async Task<double> GetOCPAsync(CancellationToken ct = default) public virtual async Task<double> (CancellationToken ct = default)
=> double.Parse((await QueryAsync("CURR:PROT?", ct)).Replace("A", ""), CultureInfo.InvariantCulture); => double.Parse((await ("CURR:PROT?", ct)).Replace("A", ""), CultureInfo.InvariantCulture);
public virtual Task ClearProtectionAsync(CancellationToken ct = default) public virtual Task (CancellationToken ct = default)
=> WriteAsync("SYST:PROT:CLE", ct); => ("SYST:PROT:CLE", ct);
// ===================== 4. 测量 ===================== // ===================== 4. 测量 =====================
public virtual async Task<double> MeasureVoltageAsync(CancellationToken ct = default) public virtual async Task<double> (CancellationToken ct = default)
=> double.Parse((await QueryAsync("MEAS:VOLT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture); => double.Parse((await ("MEAS:VOLT?", ct)).Replace("V", ""), CultureInfo.InvariantCulture);
public virtual async Task<double> MeasureCurrentAsync(CancellationToken ct = default) public virtual async Task<double> (CancellationToken ct = default)
=> double.Parse((await QueryAsync("MEAS:CURR?", ct)).Replace("A", ""), CultureInfo.InvariantCulture); => double.Parse((await ("MEAS:CURR?", ct)).Replace("A", ""), CultureInfo.InvariantCulture);
public virtual async Task<double> MeasurePowerAsync(CancellationToken ct = default) public virtual async Task<double> (CancellationToken ct = default)
=> double.Parse((await QueryAsync("MEAS:POW?", ct)).Replace("W", ""), CultureInfo.InvariantCulture); => 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 ( return (
double.Parse(parts[0].Replace("V", ""), CultureInfo.InvariantCulture), double.Parse(parts[0].Replace("V", ""), CultureInfo.InvariantCulture),
double.Parse(parts[1].Replace("A", ""), CultureInfo.InvariantCulture), double.Parse(parts[1].Replace("A", ""), CultureInfo.InvariantCulture),
double.Parse(parts[2].Replace("W", ""), 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);
} }
} }

View File

@ -18,72 +18,32 @@ namespace DeviceCommand.Device
private const byte SlaveAddress = 1; private const byte SlaveAddress = 1;
#region
public virtual async Task Set_Mode_LocalAuto(CancellationToken ct = default)
=> await WriteSingleRegisterAsync(SlaveAddress, 51, 0, ct);
public virtual async Task Set_Mode_LocalManual(CancellationToken ct = default) public async Task _KW(float , CancellationToken ct = default)
=> await WriteSingleRegisterAsync(SlaveAddress, 51, 1, ct); {
var send = ConvertFromFloat();
await WriteMultipleRegistersAsync(1, 58, send).WaitAsync(ct);
}
public virtual async Task Set_Mode_ATEControl(CancellationToken ct = default) public async Task (CancellationToken ct = default)
=> await WriteSingleRegisterAsync(SlaveAddress, 51, 2, ct); {
var tmp = new ushort();
ushort send = (ushort)(tmp | (1 << 8));
await WriteSingleRegisterAsync(1, 70, send).WaitAsync(ct);
}
public virtual async Task Set_AlarmMute(CancellationToken ct = default) public async Task (CancellationToken ct = default)
=> await WriteSingleRegisterAsync(SlaveAddress, 52, 1, ct); {
await WriteSingleRegisterAsync(1, 70, new()).WaitAsync(ct);
}
public virtual async Task Set_FaultReset(CancellationToken ct = default) public async Task<float> _KW(CancellationToken ct = default)
=> await WriteSingleRegisterAsync(SlaveAddress, 53, 1, ct); {
var re = await ReadHoldingRegistersAsync(1, 58, 2).WaitAsync(ct);
return ConvertToFloat(re);
}
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<float> Get_A_Voltage(CancellationToken ct = default)
=> ConvertToFloat(await ReadHoldingRegistersAsync(SlaveAddress, 150, 2, ct));
public virtual async Task<float> Get_A_Current(CancellationToken ct = default)
=> ConvertToFloat(await ReadHoldingRegistersAsync(SlaveAddress, 152, 2, ct));
public virtual async Task<float> Get_A_ActivePower(CancellationToken ct = default)
=> ConvertToFloat(await ReadHoldingRegistersAsync(SlaveAddress, 154, 2, ct));
// …同理其他相和三相测量,直接调用基类 ReadHoldingRegistersAsync
#endregion
#region #region
protected virtual float ConvertToFloat(ushort[] values) protected virtual float ConvertToFloat(ushort[] values)

View File

@ -14,6 +14,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" /> <ProjectReference Include="..\Common\Common.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -100,26 +100,11 @@ public class LoggingInterceptor : IInterceptor
} }
catch (Exception ex) catch (Exception ex)
{ {
// 如果出现异常,记录异常并设置默认返回值
sw.Stop(); sw.Stop();
LoggerHelper.ErrorWithNotify($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 异常:{ex.Message}", ex.StackTrace); LoggerHelper.ErrorWithNotify($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 异常:{ex.Message}", ex.StackTrace);
// 根据返回类型设置默认值 var exception = new Exception($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 时发生异常:{ex.Message}");
if (resultType == typeof(bool)) invocation.ReturnValue = Task.FromException<object>(exception);
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);
return; return;
} }
} }