添加SDE710FH-A驱动
This commit is contained in:
@@ -1,13 +1,102 @@
|
||||
using DeviceCommand.Base;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DeviceCommand.Devices
|
||||
{
|
||||
public class SDE710FH_A:ModbusTcp
|
||||
public class SDE710FH_A : ModbusTcp
|
||||
{
|
||||
// 从站地址(设备 ID)
|
||||
private readonly byte _slaveId;
|
||||
|
||||
// 寄存器地址常量(根据设备手册定义)
|
||||
private const ushort ADDR_TEMP_PV = 0x0000; // 温度测量值
|
||||
private const ushort ADDR_HUMID_PV = 0x0001; // 湿度测量值
|
||||
private const ushort ADDR_TEMP_MV = 0x0002; // 温度输出值
|
||||
private const ushort ADDR_HUMID_MV = 0x0003; // 湿度输出值
|
||||
|
||||
// 转换系数:寄存器原始值 × SCALE = 工程值
|
||||
private const float SCALE = 0.1f;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="slaveId">Modbus 从站地址(1~247)</param>
|
||||
/// <param name="ip">设备 IP 地址</param>
|
||||
/// <param name="port">端口,默认 502</param>
|
||||
/// <param name="sendTimeoutMs">发送超时(毫秒)</param>
|
||||
/// <param name="receiveTimeoutMs">接收超时(毫秒)</param>
|
||||
public SDE710FH_A(byte slaveId, string ip, int port = 502,
|
||||
int sendTimeoutMs = 3000, int receiveTimeoutMs = 3000)
|
||||
: base()
|
||||
{
|
||||
_slaveId = slaveId;
|
||||
ConfigureDevice(ip, port, sendTimeoutMs, receiveTimeoutMs);
|
||||
}
|
||||
|
||||
// ==================== 读取单个值 ====================
|
||||
|
||||
/// <summary>读取温度 PV(工程值)</summary>
|
||||
public async Task<float> ReadTemperaturePVAsync(CancellationToken ct = default)
|
||||
{
|
||||
ushort[] raw = await ReadInputRegistersAsync(_slaveId, ADDR_TEMP_PV, 1, ct);
|
||||
return raw[0] * SCALE;
|
||||
}
|
||||
|
||||
/// <summary>读取湿度 PV(工程值)</summary>
|
||||
public async Task<float> ReadHumidityPVAsync(CancellationToken ct = default)
|
||||
{
|
||||
ushort[] raw = await ReadInputRegistersAsync(_slaveId, ADDR_HUMID_PV, 1, ct);
|
||||
return raw[0] * SCALE;
|
||||
}
|
||||
|
||||
/// <summary>读取温度 MV(工程值)</summary>
|
||||
public async Task<float> ReadTemperatureMVAsync(CancellationToken ct = default)
|
||||
{
|
||||
ushort[] raw = await ReadHoldingRegistersAsync(_slaveId, ADDR_TEMP_MV, 1, ct);
|
||||
return raw[0] * SCALE;
|
||||
}
|
||||
|
||||
/// <summary>读取湿度 MV(工程值)</summary>
|
||||
public async Task<float> ReadHumidityMVAsync(CancellationToken ct = default)
|
||||
{
|
||||
ushort[] raw = await ReadHoldingRegistersAsync(_slaveId, ADDR_HUMID_MV, 1, ct);
|
||||
return raw[0] * SCALE;
|
||||
}
|
||||
|
||||
// ==================== 批量读取 ====================
|
||||
|
||||
/// <summary>
|
||||
/// 一次性读取温度 PV、湿度 PV、温度 MV、湿度 MV(效率更高)
|
||||
/// </summary>
|
||||
/// <returns>(tempPV, humidPV, tempMV, humidMV)</returns>
|
||||
public async Task<(float tempPV, float humidPV, float tempMV, float humidMV)> ReadAllAsync(CancellationToken ct = default)
|
||||
{
|
||||
// 从 0x0000 开始连续读取 4 个保持寄存器
|
||||
ushort[] raw = await ReadHoldingRegistersAsync(_slaveId, ADDR_TEMP_PV, 4, ct);
|
||||
return (
|
||||
raw[0] * SCALE, // 温度 PV
|
||||
raw[1] * SCALE, // 湿度 PV
|
||||
raw[2] * SCALE, // 温度 MV
|
||||
raw[3] * SCALE // 湿度 MV
|
||||
);
|
||||
}
|
||||
|
||||
// ==================== 写入 ====================
|
||||
|
||||
/// <summary>设定温度 MV(工程值)</summary>
|
||||
public async Task WriteTemperatureMVAsync(float value, CancellationToken ct = default)
|
||||
{
|
||||
ushort raw = (ushort)(value / SCALE);
|
||||
await WriteSingleRegisterAsync(_slaveId, ADDR_TEMP_MV, raw, ct);
|
||||
}
|
||||
|
||||
/// <summary>设定湿度 MV(工程值)</summary>
|
||||
public async Task WriteHumidityMVAsync(float value, CancellationToken ct = default)
|
||||
{
|
||||
ushort raw = (ushort)(value / SCALE);
|
||||
await WriteSingleRegisterAsync(_slaveId, ADDR_HUMID_MV, raw, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user