From 1a8ba418c9eb79d8c12e758728e002fd544d3bbf Mon Sep 17 00:00:00 2001 From: soerman Date: Wed, 25 Nov 2020 18:31:32 +0100 Subject: [PATCH 1/6] Add FLOAT64-IEEE support to inputs.modbus (by Nemecsek) --- plugins/inputs/modbus/modbus.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/modbus/modbus.go b/plugins/inputs/modbus/modbus.go index ec68890c5eb91..4f0d781fa2de6 100644 --- a/plugins/inputs/modbus/modbus.go +++ b/plugins/inputs/modbus/modbus.go @@ -132,7 +132,8 @@ const sampleConfig = ` ## |---BA, DCBA - Little Endian ## |---BADC - Mid-Big Endian ## |---CDAB - Mid-Little Endian - ## data_type - INT16, UINT16, INT32, UINT32, INT64, UINT64, FLOAT32-IEEE (the IEEE 754 binary representation) + ## data_type - INT16, UINT16, INT32, UINT32, INT64, UINT64, + ## FLOAT32-IEEE, FLOAT64-IEEE (the IEEE 754 binary representation) ## FLOAT32, FIXED, UFIXED (fixed-point representation on input) ## scale - the final numeric variable representation ## address - variable address @@ -355,7 +356,7 @@ func validateFieldContainers(t []fieldContainer, n string) error { // search data type switch item.DataType { - case "UINT16", "INT16", "UINT32", "INT32", "UINT64", "INT64", "FLOAT32-IEEE", "FLOAT32", "FIXED", "UFIXED": + case "UINT16", "INT16", "UINT32", "INT32", "UINT64", "INT64", "FLOAT32-IEEE", "FLOAT64-IEEE", "FLOAT32", "FIXED", "UFIXED": break default: return fmt.Errorf("invalid data type '%s' in '%s' - '%s'", item.DataType, n, item.Name) @@ -512,6 +513,10 @@ func convertDataType(t fieldContainer, bytes []byte) interface{} { e32 := convertEndianness32(t.ByteOrder, bytes) f32 := math.Float32frombits(e32) return scaleFloat32(t.Scale, f32) + case "FLOAT64-IEEE": + e64 := convertEndianness64(t.ByteOrder, bytes) + f64 := math.Float64frombits(e64) + return scaleFloat64(t.Scale, f64) case "FIXED": if len(bytes) == 2 { e16 := convertEndianness16(t.ByteOrder, bytes) @@ -662,6 +667,10 @@ func scaleFloat32(s float64, v float32) float32 { return float32(float64(v) * s) } +func scaleFloat64(s float64, v float64) float64 { + return float64(float64(v) * s) +} + func scaleUint64(s float64, v uint64) uint64 { return uint64(float64(v) * float64(s)) } From da67e9671e64329ac4e26ce3fd110338550f153c Mon Sep 17 00:00:00 2001 From: SoerMan Date: Thu, 26 Nov 2020 12:47:35 +0100 Subject: [PATCH 2/6] Update plugins/inputs/modbus/modbus.go Co-authored-by: Steven Soroka --- plugins/inputs/modbus/modbus.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inputs/modbus/modbus.go b/plugins/inputs/modbus/modbus.go index 4f0d781fa2de6..21bd8a977da7b 100644 --- a/plugins/inputs/modbus/modbus.go +++ b/plugins/inputs/modbus/modbus.go @@ -668,7 +668,7 @@ func scaleFloat32(s float64, v float32) float32 { } func scaleFloat64(s float64, v float64) float64 { - return float64(float64(v) * s) + return v * s } func scaleUint64(s float64, v uint64) uint64 { From eda66fe9b4e2116fa25f0d3c92326362e4598b83 Mon Sep 17 00:00:00 2001 From: soerman Date: Fri, 27 Nov 2020 10:24:08 +0100 Subject: [PATCH 3/6] Added FLOAT64_IEEE to README.md --- plugins/inputs/modbus/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/inputs/modbus/README.md b/plugins/inputs/modbus/README.md index 3c568b5e6e5e7..3e5d6d64e06d4 100644 --- a/plugins/inputs/modbus/README.md +++ b/plugins/inputs/modbus/README.md @@ -67,7 +67,7 @@ Registers via Modbus TCP or Modbus RTU/ASCII. ## |---BA, DCBA - Little Endian ## |---BADC - Mid-Big Endian ## |---CDAB - Mid-Little Endian - ## data_type - INT16, UINT16, INT32, UINT32, INT64, UINT64, FLOAT32-IEEE (the IEEE 754 binary representation) + ## data_type - INT16, UINT16, INT32, UINT32, INT64, UINT64, FLOAT32-IEEE, FLOAT64-IEEE (the IEEE 754 binary representation) ## FLOAT32 (deprecated), FIXED, UFIXED (fixed-point representation on input) ## scale - the final numeric variable representation ## address - variable address @@ -105,9 +105,9 @@ and cannot be configured. These types are used for integer input values. Select the one that matches your modbus data source. -#### Floating Point: `FLOAT32-IEEE` +#### Floating Point: `FLOAT32-IEEE`, `FLOAT64-IEEE` -Use this type if your modbus registers contain a value that is encoded in this format. This type +Use these types if your modbus registers contain a value that is encoded in this format. This type always includes the sign and therefore there exists no variant. #### Fixed Point: `FIXED`, `UFIXED` (`FLOAT32`) From 1e1b587b1c44b8ec4e85b1a1faccbc0905810780 Mon Sep 17 00:00:00 2001 From: soerman Date: Fri, 27 Nov 2020 10:57:03 +0100 Subject: [PATCH 4/6] Update README.md --- plugins/inputs/modbus/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inputs/modbus/README.md b/plugins/inputs/modbus/README.md index 3e5d6d64e06d4..eec4a2a2b6b16 100644 --- a/plugins/inputs/modbus/README.md +++ b/plugins/inputs/modbus/README.md @@ -107,7 +107,7 @@ These types are used for integer input values. Select the one that matches your #### Floating Point: `FLOAT32-IEEE`, `FLOAT64-IEEE` -Use these types if your modbus registers contain a value that is encoded in this format. This type +Use these types if your modbus registers contain a value that is encoded in this format. These types always includes the sign and therefore there exists no variant. #### Fixed Point: `FIXED`, `UFIXED` (`FLOAT32`) From b2d1143b686f8403cd973af0c422021abca40221 Mon Sep 17 00:00:00 2001 From: soerman Date: Fri, 27 Nov 2020 11:03:28 +0100 Subject: [PATCH 5/6] correction in README.md --- plugins/inputs/modbus/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inputs/modbus/README.md b/plugins/inputs/modbus/README.md index eec4a2a2b6b16..7fe8f8fda4205 100644 --- a/plugins/inputs/modbus/README.md +++ b/plugins/inputs/modbus/README.md @@ -108,7 +108,7 @@ These types are used for integer input values. Select the one that matches your #### Floating Point: `FLOAT32-IEEE`, `FLOAT64-IEEE` Use these types if your modbus registers contain a value that is encoded in this format. These types -always includes the sign and therefore there exists no variant. +always include the sign and therefore there exists no variant. #### Fixed Point: `FIXED`, `UFIXED` (`FLOAT32`) From 86f4308f8b762f7c6c7fdee526b5105f13e5f99b Mon Sep 17 00:00:00 2001 From: soerman Date: Fri, 27 Nov 2020 11:16:38 +0100 Subject: [PATCH 6/6] added cases for FLOAT64_IEEE in modbus_test.go --- plugins/inputs/modbus/modbus_test.go | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/plugins/inputs/modbus/modbus_test.go b/plugins/inputs/modbus/modbus_test.go index 8c5241dc2aaee..07af3369a66ec 100644 --- a/plugins/inputs/modbus/modbus_test.go +++ b/plugins/inputs/modbus/modbus_test.go @@ -549,6 +549,66 @@ func TestHoldingRegisters(t *testing.T) { write: []byte{0xF6, 0x84, 0xF9, 0x45, 0xFE, 0xBC, 0xFF, 0xFF}, read: uint64(18446742686322259968), }, + { + name: "register214_to_register217_abcdefgh_float64_ieee", + address: []uint16{214, 215, 216, 217}, + quantity: 4, + byteOrder: "ABCDEFGH", + dataType: "FLOAT64-IEEE", + scale: 1, + write: []byte{0xBF, 0x9C, 0x6A, 0x40, 0xC3, 0x47, 0x8F, 0x55}, + read: float64(-0.02774907295123737), + }, + { + name: "register214_to_register217_abcdefgh_float64_ieee_scaled", + address: []uint16{214, 215, 216, 217}, + quantity: 4, + byteOrder: "ABCDEFGH", + dataType: "FLOAT64-IEEE", + scale: 0.1, + write: []byte{0xBF, 0x9C, 0x6A, 0x40, 0xC3, 0x47, 0x8F, 0x55}, + read: float64(-0.002774907295123737), + }, + { + name: "register218_to_register221_abcdefgh_float64_ieee_pos", + address: []uint16{218, 219, 220, 221}, + quantity: 4, + byteOrder: "ABCDEFGH", + dataType: "FLOAT64-IEEE", + scale: 1, + write: []byte{0x3F, 0x9C, 0x6A, 0x40, 0xC3, 0x47, 0x8F, 0x55}, + read: float64(0.02774907295123737), + }, + { + name: "register222_to_register225_hgfecdba_float64_ieee", + address: []uint16{222, 223, 224, 225}, + quantity: 4, + byteOrder: "HGFEDCBA", + dataType: "FLOAT64-IEEE", + scale: 1, + write: []byte{0x55, 0x8F, 0x47, 0xC3, 0x40, 0x6A, 0x9C, 0xBF}, + read: float64(-0.02774907295123737), + }, + { + name: "register226_to_register229_badcfehg_float64_ieee", + address: []uint16{226, 227, 228, 229}, + quantity: 4, + byteOrder: "BADCFEHG", + dataType: "FLOAT64-IEEE", + scale: 1, + write: []byte{0x9C, 0xBF, 0x40, 0x6A, 0x47, 0xC3, 0x55, 0x8F}, + read: float64(-0.02774907295123737), + }, + { + name: "register230_to_register233_ghefcdab_float64_ieee", + address: []uint16{230, 231, 232, 233}, + quantity: 4, + byteOrder: "GHEFCDAB", + dataType: "FLOAT64-IEEE", + scale: 1, + write: []byte{0x8F, 0x55, 0xC3, 0x47, 0x6A, 0x40, 0xBF, 0x9C}, + read: float64(-0.02774907295123737), + }, } serv := mbserver.NewServer()