Modbus: treat nan and inf responses as zero (#4968)

This commit is contained in:
andig 2022-10-30 16:23:40 +01:00 • committed by GitHub
parent c83e10996d
commit f2f2edfcdf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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
}
}