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

97 lines
2.9 KiB
C#

using InnovEnergy.Lib.Protocols.Modbus.Clients;
using InnovEnergy.Lib.Protocols.Modbus.Connections;
using InnovEnergy.Lib.Units.Composite;
using static DecimalMath.DecimalEx;
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;
}
}
//private static Decimal GetPhi(Decimal cosPhi) => cosPhi.Clamp(-1m, 1m).Apply(ACos);
private EmuMeterStatus TryReadStatus()
{
// Console.WriteLine("Reading Emu Meter Data");
// TODO: get SerialNb, depends on Little/Big Endian support in Modbus Lib
// var registers = Modbus.ReadHoldingRegisters(5001, 4);
// var id = registers.GetInt32(5001);
var powerCurrent = Modbus.ReadHoldingRegisters(9000, 108).ToDecimals(); // TODO "ModbusRegisters"
var voltageFreq = Modbus.ReadHoldingRegisters(9200, 112).ToDecimals(); // To check with Ivo
// 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];
var currentL1 = powerCurrent[51];
var currentL2 = powerCurrent[52];
var currentL3 = powerCurrent[53];
var voltageL1N = voltageFreq[0];
var voltageL2N = voltageFreq[1];
var voltageL3N = voltageFreq[2];
var frequency = voltageFreq[55];
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)
};
return new EmuMeterStatus
{
Ac = new Ac3Bus
{
Frequency = frequency,
L1 = l1,
L2 = l2,
L3 = l3
}
};
}
}