diff --git a/DeviceCommand/Devices/SDE710FH-A.cs b/DeviceCommand/Devices/SDE710FH-A.cs index 7f21f8d..dc60f86 100644 --- a/DeviceCommand/Devices/SDE710FH-A.cs +++ b/DeviceCommand/Devices/SDE710FH-A.cs @@ -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; + + /// + /// 构造函数 + /// + /// Modbus 从站地址(1~247) + /// 设备 IP 地址 + /// 端口,默认 502 + /// 发送超时(毫秒) + /// 接收超时(毫秒) + public SDE710FH_A(byte slaveId, string ip, int port = 502, + int sendTimeoutMs = 3000, int receiveTimeoutMs = 3000) + : base() + { + _slaveId = slaveId; + ConfigureDevice(ip, port, sendTimeoutMs, receiveTimeoutMs); + } + + // ==================== 读取单个值 ==================== + + /// 读取温度 PV(工程值) + public async Task ReadTemperaturePVAsync(CancellationToken ct = default) + { + ushort[] raw = await ReadInputRegistersAsync(_slaveId, ADDR_TEMP_PV, 1, ct); + return raw[0] * SCALE; + } + + /// 读取湿度 PV(工程值) + public async Task ReadHumidityPVAsync(CancellationToken ct = default) + { + ushort[] raw = await ReadInputRegistersAsync(_slaveId, ADDR_HUMID_PV, 1, ct); + return raw[0] * SCALE; + } + + /// 读取温度 MV(工程值) + public async Task ReadTemperatureMVAsync(CancellationToken ct = default) + { + ushort[] raw = await ReadHoldingRegistersAsync(_slaveId, ADDR_TEMP_MV, 1, ct); + return raw[0] * SCALE; + } + + /// 读取湿度 MV(工程值) + public async Task ReadHumidityMVAsync(CancellationToken ct = default) + { + ushort[] raw = await ReadHoldingRegistersAsync(_slaveId, ADDR_HUMID_MV, 1, ct); + return raw[0] * SCALE; + } + + // ==================== 批量读取 ==================== + + /// + /// 一次性读取温度 PV、湿度 PV、温度 MV、湿度 MV(效率更高) + /// + /// (tempPV, humidPV, tempMV, humidMV) + 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 + ); + } + + // ==================== 写入 ==================== + + /// 设定温度 MV(工程值) + public async Task WriteTemperatureMVAsync(float value, CancellationToken ct = default) + { + ushort raw = (ushort)(value / SCALE); + await WriteSingleRegisterAsync(_slaveId, ADDR_TEMP_MV, raw, ct); + } + + /// 设定湿度 MV(工程值) + public async Task WriteHumidityMVAsync(float value, CancellationToken ct = default) + { + ushort raw = (ushort)(value / SCALE); + await WriteSingleRegisterAsync(_slaveId, ADDR_HUMID_MV, raw, ct); + } } -} +} \ No newline at end of file