From f2f2edfcdfae8d4bb658dbb51817ccff1d8d3656 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 30 Oct 2022 16:23:40 +0100 Subject: [PATCH] Modbus: treat nan and inf responses as zero (#4968) --- util/modbus/modbus.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/util/modbus/modbus.go b/util/modbus/modbus.go index c9e19b1c9..a715f756d 100644 --- a/util/modbus/modbus.go +++ b/util/modbus/modbus.go @@ -3,6 +3,7 @@ package modbus import ( "errors" "fmt" + "math" "strconv" "strings" "sync" @@ -280,7 +281,11 @@ type Register struct { // asFloat64 creates a function that returns numerics vales as float64 func asFloat64[T constraints.Signed | constraints.Unsigned | constraints.Float](f func([]byte) T) func([]byte) float64 { return func(v []byte) float64 { - return float64(f(v)) + res := float64(f(v)) + if math.IsNaN(res) || math.IsInf(res, 0) { + res = 0 + } + return res } }