diff --git a/plugins/inputs/modbus/README.md b/plugins/inputs/modbus/README.md index 6340672b6e13d..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 @@ -29,15 +29,18 @@ Registers via Modbus TCP or Modbus RTU/ASCII. # TCP - connect via Modbus/TCP controller = "tcp://localhost:502" - + ## 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 8e1dc90027cf0..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,16 +88,19 @@ const sampleConfig = ` # TCP - connect via Modbus/TCP controller = "tcp://localhost:502" - + ## 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 ## @@ -246,9 +249,20 @@ 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 "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) + m.handler = handler + } case "file": switch m.TransmissionMode { case "RTU":