update Modbus Library to achieve clear buffers regularly

This commit is contained in:
atef 2026-02-13 09:36:49 +01:00
parent 4c94238188
commit 6c0271e70c
2 changed files with 5 additions and 0 deletions

View File

@ -36,4 +36,5 @@ public abstract class ModbusClient
SlaveId = slaveId; SlaveId = slaveId;
Endian = endian; Endian = endian;
} }
} }

View File

@ -8,10 +8,12 @@ namespace InnovEnergy.Lib.Protocols.Modbus.Slaves;
public class ModbusDevice<[DynamicallyAccessedMembers(All)] R> where R : notnull public class ModbusDevice<[DynamicallyAccessedMembers(All)] R> where R : notnull
{ {
private readonly IReadOnlyList<Batch<R>> _Batches; private readonly IReadOnlyList<Batch<R>> _Batches;
private readonly ModbusClient _modbusClient;
public ModbusDevice(ModbusClient modbusClient, Int32 addressOffset = 0) public ModbusDevice(ModbusClient modbusClient, Int32 addressOffset = 0)
{ {
_Batches = modbusClient.MakeBatchesFor<R>(addressOffset); _Batches = modbusClient.MakeBatchesFor<R>(addressOffset);
_modbusClient = modbusClient;
} }
public R Read() public R Read()
@ -40,6 +42,7 @@ public class ModbusDevice<[DynamicallyAccessedMembers(All)] R> where R : notnull
//Console.WriteLine($"Reading { _Batches.Count } Modbus batches."); //Console.WriteLine($"Reading { _Batches.Count } Modbus batches.");
foreach (var batch in _Batches) foreach (var batch in _Batches)
{ {
_modbusClient.Channel.ClearBuffers();
batch.Read(record); batch.Read(record);
//Thread.Sleep(30); // this added mainly for Growatt reading //Thread.Sleep(30); // this added mainly for Growatt reading
} }
@ -50,6 +53,7 @@ public class ModbusDevice<[DynamicallyAccessedMembers(All)] R> where R : notnull
{ {
foreach (var batch in _Batches) foreach (var batch in _Batches)
{ {
_modbusClient.Channel.ClearBuffers();
batch.Write(record); batch.Write(record);
//Thread.Sleep(50); // this added mainly for Growatt reading //Thread.Sleep(50); // this added mainly for Growatt reading
} }