Innovenergy_trunk/csharp/Lib/Protocols/Modbus/Slaves/ModbusDevice.cs

51 lines
1.3 KiB
C#
Raw Normal View History

2023-06-13 11:03:36 +00:00
using System.Diagnostics.CodeAnalysis;
2023-05-04 07:32:35 +00:00
using InnovEnergy.Lib.Protocols.Modbus.Clients;
using InnovEnergy.Lib.Protocols.Modbus.Reflection;
2023-06-13 11:03:36 +00:00
using static System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes;
2023-05-04 07:32:35 +00:00
namespace InnovEnergy.Lib.Protocols.Modbus.Slaves;
public class ModbusDevice<R> where R : notnull
{
private readonly IReadOnlyList<Batch<R>> _Batches;
public ModbusDevice(ModbusClient modbusClient, Int32 addressOffset = 0)
{
2023-06-13 11:03:36 +00:00
_Batches = modbusClient.MakeBatchesFor<R>(addressOffset);
2023-05-04 07:32:35 +00:00
}
public R Read()
{
R r;
try
{
r = Activator.CreateInstance<R>();
}
catch (Exception e)
{
throw new Exception
(
$"type {typeof(R).Name} seems to lack a parameterless constructor. " +
$"Either create one or use the other overload of{nameof(Read)} instead.",
e
);
}
return Read(r);
}
2023-06-13 11:03:36 +00:00
public R Read([DynamicallyAccessedMembers(All)] R record)
2023-05-04 07:32:35 +00:00
{
foreach (var batch in _Batches)
batch.Read(record);
return record;
}
2023-06-13 11:03:36 +00:00
public void Write([DynamicallyAccessedMembers(All)] R record)
2023-05-04 07:32:35 +00:00
{
foreach (var batch in _Batches)
batch.Write(record);
}
}