add sanity check on uint values

This commit is contained in:
Josh Deprez 2021-01-11 10:21:53 +11:00
parent c16b5f973e
commit fac81285da
3 changed files with 31 additions and 14 deletions

View file

@ -106,7 +106,10 @@ func readRegs(client modbus.Client, start, qty uint16) {
if addr <= start || addr > start+qty { if addr <= start || addr > start+qty {
continue continue
} }
val := reg.read(data[(addr-start-1)*2:]) val, err := reg.read(data[(addr-start-1)*2:])
if err != nil {
log.Fatalf("Couldn't parse input register data: %v", err)
}
//fmt.Printf("%s: %v %s\n", reg.name, val, reg.unit) //fmt.Printf("%s: %v %s\n", reg.name, val, reg.unit)
registerGauges[addr].Set(val) registerGauges[addr].Set(val)
} }

View file

@ -1,23 +1,36 @@
package main package main
import "encoding/binary" import (
"encoding/binary"
"fmt"
)
// Helpers for converting straight from modbus data to floats. // Helpers for converting straight from modbus data to floats.
func u16(data []byte) float64 { func u16(data []byte) (float64, error) {
return float64(binary.BigEndian.Uint16(data)) n := binary.BigEndian.Uint16(data)
if n == 0xFFFF {
// probably a misread
return 0, fmt.Errorf("likely misread: u16 read %v", n)
}
return float64(n), nil
} }
func s16(data []byte) float64 { func s16(data []byte) (float64, error) {
return float64(int16(binary.BigEndian.Uint16(data))) return float64(int16(binary.BigEndian.Uint16(data))), nil
} }
func u32(data []byte) float64 { func u32(data []byte) (float64, error) {
// Little-endian big-endian :# // Little-endian big-endian :#
return float64(uint32(binary.BigEndian.Uint16(data)) + uint32(binary.BigEndian.Uint16(data[2:]))<<16) n := uint32(binary.BigEndian.Uint16(data)) + uint32(binary.BigEndian.Uint16(data[2:]))<<16
if n == 0xFFFFFFFF {
// probably a misread
return 0, fmt.Errorf("likely misread: u32 read %v", n)
}
return float64(n), nil
} }
func s32(data []byte) float64 { func s32(data []byte) (float64, error) {
// Little-endian big-endian :# // Little-endian big-endian :#
return float64(int32(binary.BigEndian.Uint16(data)) + int32(binary.BigEndian.Uint16(data[2:]))<<16) return float64(int32(binary.BigEndian.Uint16(data)) + int32(binary.BigEndian.Uint16(data[2:]))<<16), nil
} }

View file

@ -2,13 +2,14 @@ package main
type register struct { type register struct {
name string name string
conv func([]byte) float64 conv func([]byte) (float64, error)
mult float64 mult float64
unit string unit string
} }
func (r *register) read(data []byte) float64 { func (r *register) read(data []byte) (float64, error) {
return r.conv(data) * r.mult n, err := r.conv(data)
return n * r.mult, err
} }
var sungrowInputRegs = map[uint16]*register{ var sungrowInputRegs = map[uint16]*register{
@ -32,7 +33,7 @@ var sungrowInputRegs = map[uint16]*register{
//5023: {"phase_b_current", u16, 0.1, "A"}, //5023: {"phase_b_current", u16, 0.1, "A"},
//5024: {"phase_c_current", u16, 0.1, "A"}, //5024: {"phase_c_current", u16, 0.1, "A"},
5031: {"output_real_power", u32, 1, "VA"}, 5031: {"output_real_power", u32, 1, "VA"},
5033: {"output_reactive_power", s32, 1, "VA"}, 5033: {"output_reactive_power", s32, 1, "VAr"},
5035: {"power_factor", s16, 0.001, ""}, 5035: {"power_factor", s16, 0.001, ""},
5036: {"frequency", u16, 0.1, "Hz"}, 5036: {"frequency", u16, 0.1, "Hz"},
5049: {"nominal_reactive_power", s16, 0.1, "kVA"}, 5049: {"nominal_reactive_power", s16, 0.1, "kVA"},