Innovenergy_trunk/csharp/Lib/Devices/EmuMeter/EmuMeterDevice.cs

97 lines
2.9 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-03-01 09:53:34 +00:00
using InnovEnergy.Lib.Units.Composite;
using static DecimalMath.DecimalEx;
2023-02-16 12:57:06 +00:00
namespace InnovEnergy.Lib.Devices.EmuMeter;
public class EmuMeterDevice
{
private ModbusTcpClient Modbus { get; }
public EmuMeterDevice(String hostname, UInt16 port = 502, Byte slaveId = 1)
{
var connection = new ModbusTcpConnection(hostname, port);
Modbus = new ModbusTcpClient(connection, slaveId);
}
public EmuMeterStatus? ReadStatus()
{
try
{
return TryReadStatus();
}
catch (Exception)
{
Modbus.CloseConnection();
return null;
}
}
2023-03-01 12:35:33 +00:00
//private static Decimal GetPhi(Decimal cosPhi) => cosPhi.Clamp(-1m, 1m).Apply(ACos);
2023-02-16 12:57:06 +00:00
private EmuMeterStatus TryReadStatus()
{
// Console.WriteLine("Reading Emu Meter Data");
2023-03-08 13:22:57 +00:00
// TODO: get SerialNb, depends on Little/Big Endian support in Modbus Lib
// var registers = Modbus.ReadHoldingRegisters(5001, 4);
// var id = registers.GetInt32(5001);
2023-02-16 12:57:06 +00:00
var powerCurrent = Modbus.ReadHoldingRegisters(9000, 108).ToDecimals(); // TODO "ModbusRegisters"
var voltageFreq = Modbus.ReadHoldingRegisters(9200, 112).ToDecimals(); // To check with Ivo
2023-03-08 13:22:57 +00:00
// var energyPhases = Modbus.ReadHoldingRegisters(6100, 104).ToUInt64s();
var activePowerL1 = powerCurrent[1];
var activePowerL2 = powerCurrent[2];
var activePowerL3 = powerCurrent[3];
var reactivePowerL1 = powerCurrent[6];
var reactivePowerL2 = powerCurrent[7];
var reactivePowerL3 = powerCurrent[8];
2023-03-08 13:22:57 +00:00
var currentL1 = powerCurrent[51];
var currentL2 = powerCurrent[52];
var currentL3 = powerCurrent[53];
2023-03-08 13:22:57 +00:00
var voltageL1N = voltageFreq[0];
var voltageL2N = voltageFreq[1];
var voltageL3N = voltageFreq[2];
var frequency = voltageFreq[55];
2023-03-08 13:22:57 +00:00
var l1 = new AcPhase
{
Current = currentL1,
Voltage = voltageL1N,
Phi = ATan2(reactivePowerL1, activePowerL1) // TODO: check that this works
};
var l2 = new AcPhase
{
Current = currentL2,
Voltage = voltageL2N,
Phi = ATan2(reactivePowerL2, activePowerL2)
};
var l3 = new AcPhase
{
Current = currentL3,
Voltage = voltageL3N,
Phi = ATan2(reactivePowerL3, activePowerL3)
};
2023-03-01 12:35:33 +00:00
2023-02-16 12:57:06 +00:00
return new EmuMeterStatus
2023-03-01 12:35:33 +00:00
{
Ac = new Ac3Bus
{
Frequency = frequency,
2023-03-08 13:22:57 +00:00
L1 = l1,
L2 = l2,
L3 = l3
2023-03-01 12:35:33 +00:00
}
};
2023-02-16 12:57:06 +00:00
}
}