Alfen: fix NaN values in 1p connection (#4578)

This commit is contained in:
andig 2022-09-25 11:53:10 +02:00 • committed by GitHub
parent 63b16915fc
commit 60cde1ccbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -43,6 +43,7 @@ type Alfen struct {
}
const (
alfenRegCurrents = 320 // 3 registers
alfenRegPower = 344
alfenRegEnergy = 374 // 390
alfenRegStatus = 1201 // 5 registers
@ -50,8 +51,6 @@ const (
alfenRegPhases = 1215
)
var alfenRegCurrents = []uint16{320, 322, 324}
func init() {
registry.Add("alfen", NewAlfenFromConfig)
}
@ -215,17 +214,22 @@ var _ api.MeterCurrent = (*Alfen)(nil)
// Currents implements the api.MeterCurrent interface
func (wb *Alfen) Currents() (float64, float64, float64, error) {
var currents []float64
for _, reg := range alfenRegCurrents {
b, err := wb.conn.ReadHoldingRegisters(reg, 2)
if err != nil {
return 0, 0, 0, err
}
currents = append(currents, rs485.RTUIeee754ToFloat64(b))
b, err := wb.conn.ReadHoldingRegisters(alfenRegCurrents, 6)
if err != nil {
return 0, 0, 0, err
}
return currents[0], currents[1], currents[2], nil
var res [3]float64
for i := 0; i < 3; i++ {
f := rs485.RTUIeee754ToFloat64(b[4*i:])
if math.IsNaN(f) {
f = 0
}
res[i] = f
}
return res[0], res[1], res[2], nil
}
var _ api.PhaseSwitcher = (*Alfen)(nil)