using InnovEnergy.Lib.Protocols.Modbus.Clients; using InnovEnergy.Lib.Protocols.Modbus.Connections; 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 EmuMeterStatus TryReadStatus() { // Console.WriteLine("Reading Emu Meter Data"); var powerCurrent = Modbus.ReadHoldingRegisters(9000, 108).ToDecimals(); // TODO "ModbusRegisters" var voltageFreq = Modbus.ReadHoldingRegisters(9200, 112).ToDecimals(); // To check with Ivo var energyTotal = Modbus.ReadHoldingRegisters(6000, 24) .ToUInt64s(); var energyPhases = Modbus.ReadHoldingRegisters(6100, 104).ToUInt64s(); return new EmuMeterStatus { ActivePowerL123 = powerCurrent[0], ActivePowerL1 = powerCurrent[1], ActivePowerL2 = powerCurrent[2], ActivePowerL3 = powerCurrent[3], ReactivePowerL123 = powerCurrent[5], ReactivePowerL1 = powerCurrent[6], ReactivePowerL2 = powerCurrent[7], ReactivePowerL3 = powerCurrent[8], ApparentPowerL123 = powerCurrent[10], ApparentPowerL1 = powerCurrent[11], ApparentPowerL2 = powerCurrent[12], ApparentPowerL3 = powerCurrent[13], CurrentL123 = powerCurrent[50], CurrentL1 = powerCurrent[51], CurrentL2 = powerCurrent[52], CurrentL3 = powerCurrent[53], VoltageL1N = voltageFreq[0], VoltageL2N = voltageFreq[1], VoltageL3N = voltageFreq[2], VoltageL1L2 = voltageFreq[3], VoltageL2L3 = voltageFreq[4], VoltageL3L1 = voltageFreq[5], PowerFactorL1 = voltageFreq[50], PowerFactorL2 = voltageFreq[51], PowerFactorL3 = voltageFreq[52], Frequency = voltageFreq[55], EnergyImportL123 = energyTotal[0 / 4] / 1000.0m, EnergyExportL123 = energyTotal[20 / 4] / 1000.0m, EnergyImportL1 = energyPhases[0 / 4] / 1000.0m, EnergyExportL1 = energyPhases[20 / 4] / 1000.0m, EnergyImportL2 = energyPhases[40 / 4] / 1000.0m, EnergyExportL2 = energyPhases[60 / 4] / 1000.0m, EnergyImportL3 = energyPhases[80 / 4] / 1000.0m, EnergyExportL3 = energyPhases[100 / 4] / 1000.0m, }; } }