From a4c9590277d76127778f281a1d388d3c3fd651ee Mon Sep 17 00:00:00 2001 From: Ilia Choly Date: Fri, 10 Dec 2021 08:45:05 -0500 Subject: [PATCH] Allow omitting the DPT scale field (#84) --- dpt.go | 9 ++++++--- dpt_test.go | 9 +++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/dpt.go b/dpt.go index 148eda9..9c7af9f 100644 --- a/dpt.go +++ b/dpt.go @@ -18,10 +18,13 @@ type DPT struct { func newDPT(s BaseSentence) (DPT, error) { p := NewParser(s) p.AssertType(TypeDPT) - return DPT{ + dpt := DPT{ BaseSentence: s, Depth: p.Float64(0, "depth"), Offset: p.Float64(1, "offset"), - RangeScale: p.Float64(2, "range scale"), - }, p.Err() + } + if len(p.Fields) > 2 { + dpt.RangeScale = p.Float64(2, "range scale") + } + return dpt, p.Err() } diff --git a/dpt_test.go b/dpt_test.go index 0bf95c7..c09d615 100644 --- a/dpt_test.go +++ b/dpt_test.go @@ -30,6 +30,15 @@ var dpttests = []struct { RangeScale: MustParseDecimal("0.1"), }, }, + { + name: "good sentence with 2 fields", + raw: "$INDPT,2.3,0.0*46", + msg: DPT{ + Depth: MustParseDecimal("2.3"), + Offset: MustParseDecimal("0.0"), + RangeScale: MustParseDecimal("0"), + }, + }, { name: "bad validity", raw: "$SDDPT,0.5,0.5,*AA",