Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Modbus Rtu over tcp enhancement #9570

Merged
merged 3 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions plugins/inputs/modbus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
##
Expand Down
28 changes: 21 additions & 7 deletions plugins/inputs/modbus/modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
##

Expand Down Expand Up @@ -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":
Expand Down