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

41 lines
1.2 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using InnovEnergy.Lib.Protocols.Modbus.Conversions;
using InnovEnergy.Lib.Utils;
namespace InnovEnergy.Lib.Devices.EmuMeter;
public static class Conversions
{
// TODO: integrate into ModbusRegisters
public static IReadOnlyList<Single> ToSingles(this ModbusRegisters regs)
{
return regs
.Chunk(2)
.Select(c => c.Reverse().SelectMany(BitConverter.GetBytes).ToArray())
.Select(d => BitConverter.ToSingle(d))
.ToList();
}
public static IReadOnlyList<Decimal> ToDecimals(this ModbusRegisters regs)
{
return regs
.Chunk(2)
.Select(c => c.Reverse().SelectMany(BitConverter.GetBytes).ToArray())
.Select(d => BitConverter.ToSingle(d))
.Select(d => d.ConvertTo<Decimal>())
.ToList();
}
// ReSharper disable once InconsistentNaming
public static IReadOnlyList<UInt64> ToUInt64s(this ModbusRegisters regs)
{
return regs
.SelectMany(d => BitConverter.GetBytes(d).Reverse())
.Chunk(8)
.Select(c => c.Reverse().ToArray())
.Select(d => BitConverter.ToUInt64(d))
.ToList();
}
}