Innovenergy_trunk/csharp/lib/Devices/Battery48TL/Battery48TLDevice.cs

53 lines
1.6 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using InnovEnergy.Lib.Protocols.Modbus.Clients;
using InnovEnergy.Lib.Protocols.Modbus.Connections;
namespace InnovEnergy.Lib.Devices.Battery48TL;
public class Battery48TlDevice
{
private ModbusClient Modbus { get; }
public Battery48TlDevice(String device, Byte nodeId)
{
var serialConnection = new ModbusSerialConnection(device,
Constants.BaudRate,
Constants.Parity,
Constants.DataBits,
Constants.StopBits,
Constants.Timeout);
Modbus = new ModbusRtuClient(serialConnection, nodeId);
}
private Battery48TlDevice(ModbusClient modbus) // TODO : remove nullable
{
Modbus = modbus;
}
public static Battery48TlDevice Fake() // TODO : remove nullable
{
return new Battery48TlDevice(null!);
}
public Battery48TLStatus? ReadStatus() //Already try catch is implemented
{
if (Modbus is null) // TODO : remove fake
{
return null;
}
// Console.WriteLine("Reading Battery Data");
try
{
var registers = Modbus.ReadInputRegisters(Constants.BaseAddress, Constants.NoOfRegisters);
return new Battery48TLStatus(registers);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Modbus.CloseConnection();
return null;
}
}
}