diff --git a/main.go b/main.go index c10c108..ee3d800 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( + "errors" "flag" "fmt" "log" @@ -108,6 +109,10 @@ func readRegs(client modbus.Client, start, qty uint16) { } val, err := reg.read(data[(addr-start-1)*2:]) if err != nil { + if errors.Is(err, errSkippableRead) { + log.Printf("Couldn't parse input register data, skipping: %v", err) + continue + } log.Fatalf("Couldn't parse input register data: %v", err) } //fmt.Printf("%s: %v %s\n", reg.name, val, reg.unit) diff --git a/sungrow_map.go b/sungrow_map.go index cd460be..0e674b1 100644 --- a/sungrow_map.go +++ b/sungrow_map.go @@ -1,5 +1,11 @@ package main +import ( + "encoding/binary" + "errors" + "fmt" +) + type register struct { name string conv func([]byte) (float64, error) @@ -40,5 +46,20 @@ var sungrowInputRegs = map[uint16]*register{ 5113: {"daily_running_time", u16, 1, "m"}, 5144: {"total_power_yield_2", u32, 0.1, "kWh"}, - 5148: {"frequency_2", u16, 0.01, "Hz"}, + 5148: {"frequency_2", frequency2, 0.01, "Hz"}, +} + +var errSkippableRead = errors.New("skip read") + +func frequency2(data []byte) (float64, error) { + n := binary.BigEndian.Uint16(data) + if n == 0xFFFF { + // probably a misread + return 0, fmt.Errorf("likely misread: frequency_2 read %v", n) + } + if n == 100 { + // this translates as 1 Hz, which is unlikely + return 0, fmt.Errorf(" %w: frequency_2 is 1.00Hz", errSkippableRead) + } + return float64(n), nil }