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

48 lines
1.5 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;
2023-04-04 14:37:37 +00:00
using static InnovEnergy.Lib.Devices.Battery48TL.Constants;
2023-02-16 12:57:06 +00:00
namespace InnovEnergy.Lib.Devices.Battery48TL;
public class Battery48TlDevice
{
private ModbusClient Modbus { get; }
public Battery48TlDevice(String device, Byte nodeId)
{
var serialConnection = new ModbusSerialConnection(device,
2023-04-04 14:37:37 +00:00
BaudRate,
Parity,
DataBits,
StopBits,
2023-02-16 12:57:06 +00:00
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
{
try
{
2023-03-01 09:41:13 +00:00
return Modbus
2023-04-04 14:37:37 +00:00
.ReadInputRegisters(BaseAddress, NoOfRegisters)
2023-03-01 09:41:13 +00:00
.ParseBatteryStatus();
2023-02-16 12:57:06 +00:00
}
catch (Exception e)
{
Console.WriteLine(e.Message + " Battery ");
2023-02-16 12:57:06 +00:00
Modbus.CloseConnection();
return null;
}
}
}