if a battery is not reachable, Battery48TlDevices now does not give up and reports the remaining ones instead

This commit is contained in:
ig 2023-08-30 14:50:04 +02:00
parent 28d46d6689
commit 8596fddb04
3 changed files with 21 additions and 2 deletions

View File

@ -14,6 +14,7 @@ public class Battery48TlDevice : ModbusDevice<Battery48TlRecord>
public const Int32 BaudRate = 115200; public const Int32 BaudRate = 115200;
public const Int32 DataBits = 8; public const Int32 DataBits = 8;
public Byte SlaveId { get; }
public Battery48TlDevice(String tty, Byte slaveId, SshHost host) : this public Battery48TlDevice(String tty, Byte slaveId, SshHost host) : this
( (
@ -41,6 +42,7 @@ public class Battery48TlDevice : ModbusDevice<Battery48TlRecord>
public Battery48TlDevice(ModbusClient client): base(client) public Battery48TlDevice(ModbusClient client): base(client)
{ {
SlaveId = client.SlaveId;
} }
} }

View File

@ -13,7 +13,8 @@ public class Battery48TlDevices
try try
{ {
var records = _Devices var records = _Devices
.Select(d => d.Read()) .Select(TryRead)
.NotNull()
.ToArray(_Devices.Count); .ToArray(_Devices.Count);
return new Battery48TlRecords(records); return new Battery48TlRecords(records);
@ -26,4 +27,19 @@ public class Battery48TlDevices
return Battery48TlRecords.Null; return Battery48TlRecords.Null;
} }
} }
private static Battery48TlRecord? TryRead(Battery48TlDevice d)
{
try
{
return d.Read();
}
catch (Exception e)
{
Console.WriteLine($"Failed to read Battery node {d.SlaveId}\n{e.Message}");
// TODO: log
return null;
}
}
} }

View File

@ -11,8 +11,9 @@ using UInt16s = IReadOnlyCollection<UInt16>;
public abstract class ModbusClient public abstract class ModbusClient
{ {
public Byte SlaveId { get; }
internal Channel Channel { get; } internal Channel Channel { get; }
internal Byte SlaveId { get; }
internal Endian Endian { get; } internal Endian Endian { get; }
// TODO: add additional functions: coils... // TODO: add additional functions: coils...