From 39aa5c8d63966be050c0367faf1f24e21156a4cc Mon Sep 17 00:00:00 2001 From: Marius Bezuidenhout Date: Sun, 1 Aug 2021 10:38:20 +0200 Subject: [PATCH 1/3] Update modbus.go Add support for RTU over TCP --- plugins/inputs/modbus/modbus.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/plugins/inputs/modbus/modbus.go b/plugins/inputs/modbus/modbus.go index 8e1dc90027cf0..9fb951ae234bb 100644 --- a/plugins/inputs/modbus/modbus.go +++ b/plugins/inputs/modbus/modbus.go @@ -88,6 +88,10 @@ const sampleConfig = ` # TCP - connect via Modbus/TCP controller = "tcp://localhost:502" + + # RTU over TCP - connect via Modbus RTU over TCP + # controller = "tcp://localhost:502" + # transmission_mode = "RTU" ## Serial (RS485; RS232) # controller = "file:///dev/ttyUSB0" @@ -246,9 +250,16 @@ func (m *Modbus) initClient() error { if err != nil { return err } - handler := mb.NewTCPClientHandler(host + ":" + port) - handler.Timeout = time.Duration(m.Timeout) - m.handler = handler + switch m.TransmissionMode { + case "RTU": + handler := mb.NewRTUOverTCPClientHandler(host + ":" + port) + handler.Timeout = time.Duration(m.Timeout) + m.handler = handler + default: + handler := mb.NewTCPClientHandler(host + ":" + port) + handler.Timeout = time.Duration(m.Timeout) + m.handler = handler + } case "file": switch m.TransmissionMode { case "RTU": From 58c6ab822faaed69c458afa34309f1d1e3303954 Mon Sep 17 00:00:00 2001 From: Marius Bezuidenhout Date: Sun, 1 Aug 2021 10:41:41 +0200 Subject: [PATCH 2/3] Update README.md --- plugins/inputs/modbus/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/inputs/modbus/README.md b/plugins/inputs/modbus/README.md index 6340672b6e13d..b546fd19f8de1 100644 --- a/plugins/inputs/modbus/README.md +++ b/plugins/inputs/modbus/README.md @@ -29,6 +29,10 @@ Registers via Modbus TCP or Modbus RTU/ASCII. # TCP - connect via Modbus/TCP controller = "tcp://localhost:502" + + # RTU over TCP - connect via Modbus RTU over TCP + # controller = "tcp://localhost:502" + # transmission_mode = "RTU" ## Serial (RS485; RS232) # controller = "file:///dev/ttyUSB0" From 7b7d1b49180e9342b4e888e98519c6d781ede4ef Mon Sep 17 00:00:00 2001 From: Marius Bezuidenhout Date: Mon, 2 Aug 2021 17:59:41 +0200 Subject: [PATCH 3/3] Added ASCII over TCP --- plugins/inputs/modbus/README.md | 11 +++++------ plugins/inputs/modbus/modbus.go | 21 ++++++++++++--------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/plugins/inputs/modbus/README.md b/plugins/inputs/modbus/README.md index b546fd19f8de1..9f4cf5e37487c 100644 --- a/plugins/inputs/modbus/README.md +++ b/plugins/inputs/modbus/README.md @@ -9,7 +9,7 @@ Registers via Modbus TCP or Modbus RTU/ASCII. [[inputs.modbus]] ## Connection Configuration ## - ## The plugin supports connections to PLCs via MODBUS/TCP or + ## The plugin supports connections to PLCs via MODBUS/TCP, RTU over TCP, ASCII over TCP or ## via serial line communication in binary (RTU) or readable (ASCII) encoding ## ## Device name @@ -30,18 +30,17 @@ Registers via Modbus TCP or Modbus RTU/ASCII. # TCP - connect via Modbus/TCP controller = "tcp://localhost:502" - # RTU over TCP - connect via Modbus RTU over TCP - # controller = "tcp://localhost:502" - # transmission_mode = "RTU" - ## Serial (RS485; RS232) # controller = "file:///dev/ttyUSB0" # baud_rate = 9600 # data_bits = 8 # parity = "N" # stop_bits = 1 - # transmission_mode = "RTU" + ## For Modbus over TCP you can choose between "TCP", "RTUoverTCP" and "ASCIIoverTCP" + ## default behaviour is "TCP" if the controller is TCP + ## For Serial you can choose between "RTU" and "ASCII" + # transmission_mode = "RTU" ## Measurements ## diff --git a/plugins/inputs/modbus/modbus.go b/plugins/inputs/modbus/modbus.go index 9fb951ae234bb..18a00e990dc66 100644 --- a/plugins/inputs/modbus/modbus.go +++ b/plugins/inputs/modbus/modbus.go @@ -68,7 +68,7 @@ const description = `Retrieve data from MODBUS slave devices` const sampleConfig = ` ## Connection Configuration ## - ## The plugin supports connections to PLCs via MODBUS/TCP or + ## The plugin supports connections to PLCs via MODBUS/TCP, RTU over TCP, ASCII over TCP or ## via serial line communication in binary (RTU) or readable (ASCII) encoding ## ## Device name @@ -88,20 +88,19 @@ const sampleConfig = ` # TCP - connect via Modbus/TCP controller = "tcp://localhost:502" - - # RTU over TCP - connect via Modbus RTU over TCP - # controller = "tcp://localhost:502" - # transmission_mode = "RTU" - + ## Serial (RS485; RS232) # controller = "file:///dev/ttyUSB0" # baud_rate = 9600 # data_bits = 8 # parity = "N" # stop_bits = 1 - # transmission_mode = "RTU" - + ## For Modbus over TCP you can choose between "TCP", "RTUoverTCP" and "ASCIIoverTCP" + ## default behaviour is "TCP" if the controller is TCP + ## For Serial you can choose between "RTU" and "ASCII" + # transmission_mode = "RTU" + ## Measurements ## @@ -251,10 +250,14 @@ func (m *Modbus) initClient() error { return err } switch m.TransmissionMode { - case "RTU": + case "RTUoverTCP": handler := mb.NewRTUOverTCPClientHandler(host + ":" + port) handler.Timeout = time.Duration(m.Timeout) m.handler = handler + case "ASCIIoverTCP": + handler := mb.NewASCIIOverTCPClientHandler(host + ":" + port) + handler.Timeout = time.Duration(m.Timeout) + m.handler = handler default: handler := mb.NewTCPClientHandler(host + ":" + port) handler.Timeout = time.Duration(m.Timeout)