fix(inputs.modbus): fix default value of transmission mode (#12367)

This commit is contained in:
Sven Rebhan 2022-12-09 19:23:51 +01:00 committed by GitHub
parent 95bdcbb7d5
commit 7df97486d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 82 additions and 20 deletions

View File

@ -50,10 +50,13 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
# parity = "N" # parity = "N"
# stop_bits = 1 # stop_bits = 1
## Transmission mode for Modbus packets depending on the controller type.
## For Modbus over TCP you can choose between "TCP" , "RTUoverTCP" and ## For Modbus over TCP you can choose between "TCP" , "RTUoverTCP" and
## "ASCIIoverTCP". The default behaviour is "TCP" for ModbusTCP controllers. ## "ASCIIoverTCP".
## For Serial controllers you can choose between "RTU" and "ASCII". ## For Serial controllers you can choose between "RTU" and "ASCII".
# transmission_mode = "RTU" ## By default this is set to "auto" selecting "TCP" for ModbusTCP connections
## and "RTU" for serial connections.
# transmission_mode = "auto"
## Trace the connection to the modbus device as debug messages ## Trace the connection to the modbus device as debug messages
## Note: You have to enable telegraf's debug mode to see those messages! ## Note: You have to enable telegraf's debug mode to see those messages!

View File

@ -235,6 +235,13 @@ func (m *Modbus) initClient() error {
return err return err
} }
switch m.TransmissionMode { switch m.TransmissionMode {
case "", "auto", "TCP":
handler := mb.NewTCPClientHandler(host + ":" + port)
handler.Timeout = time.Duration(m.Timeout)
if m.DebugConnection {
handler.Logger = m
}
m.handler = handler
case "RTUoverTCP": case "RTUoverTCP":
handler := mb.NewRTUOverTCPClientHandler(host + ":" + port) handler := mb.NewRTUOverTCPClientHandler(host + ":" + port)
handler.Timeout = time.Duration(m.Timeout) handler.Timeout = time.Duration(m.Timeout)
@ -250,12 +257,7 @@ func (m *Modbus) initClient() error {
} }
m.handler = handler m.handler = handler
default: default:
handler := mb.NewTCPClientHandler(host + ":" + port) return fmt.Errorf("invalid transmission mode %q for %q", m.TransmissionMode, u.Scheme)
handler.Timeout = time.Duration(m.Timeout)
if m.DebugConnection {
handler.Logger = m
}
m.handler = handler
} }
case "", "file": case "", "file":
path := filepath.Join(u.Host, u.Path) path := filepath.Join(u.Host, u.Path)
@ -263,7 +265,7 @@ func (m *Modbus) initClient() error {
return fmt.Errorf("invalid path for controller %q", m.Controller) return fmt.Errorf("invalid path for controller %q", m.Controller)
} }
switch m.TransmissionMode { switch m.TransmissionMode {
case "RTU": case "", "auto", "RTU":
handler := mb.NewRTUClientHandler(path) handler := mb.NewRTUClientHandler(path)
handler.Timeout = time.Duration(m.Timeout) handler.Timeout = time.Duration(m.Timeout)
handler.BaudRate = m.BaudRate handler.BaudRate = m.BaudRate
@ -286,7 +288,7 @@ func (m *Modbus) initClient() error {
} }
m.handler = handler m.handler = handler
default: default:
return fmt.Errorf("invalid protocol '%s' - '%s' ", u.Scheme, m.TransmissionMode) return fmt.Errorf("invalid transmission mode %q for %q", m.TransmissionMode, u.Scheme)
} }
default: default:
return fmt.Errorf("invalid controller %q", m.Controller) return fmt.Errorf("invalid controller %q", m.Controller)

View File

@ -25,6 +25,7 @@ func TestControllers(t *testing.T) {
var tests = []struct { var tests = []struct {
name string name string
controller string controller string
mode string
errmsg string errmsg string
}{ }{
{ {
@ -32,9 +33,41 @@ func TestControllers(t *testing.T) {
controller: "tcp://localhost:502", controller: "tcp://localhost:502",
}, },
{ {
name: "invalid TCP host", name: "TCP mode auto",
controller: "tcp://localhost:502",
mode: "auto",
},
{
name: "TCP mode TCP",
controller: "tcp://localhost:502",
mode: "TCP",
},
{
name: "TCP mode RTUoverTCP",
controller: "tcp://localhost:502",
mode: "RTUoverTCP",
},
{
name: "TCP mode ASCIIoverTCP",
controller: "tcp://localhost:502",
mode: "ASCIIoverTCP",
},
{
name: "TCP invalid host",
controller: "tcp://localhost", controller: "tcp://localhost",
errmsg: "initializing client failed: address localhost: missing port in address", errmsg: "address localhost: missing port in address",
},
{
name: "TCP invalid mode RTU",
controller: "tcp://localhost:502",
mode: "RTU",
errmsg: "invalid transmission mode",
},
{
name: "TCP invalid mode ASCII",
controller: "tcp://localhost:502",
mode: "ASCII",
errmsg: "invalid transmission mode",
}, },
{ {
name: "absolute file path", name: "absolute file path",
@ -56,20 +89,41 @@ func TestControllers(t *testing.T) {
name: "Windows COM-port file path", name: "Windows COM-port file path",
controller: "file://com2", controller: "file://com2",
}, },
{
name: "serial mode auto",
controller: "file:///dev/ttyUSB0",
mode: "auto",
},
{
name: "serial mode RTU",
controller: "file:///dev/ttyUSB0",
mode: "RTU",
},
{
name: "serial mode ASCII",
controller: "file:///dev/ttyUSB0",
mode: "ASCII",
},
{ {
name: "empty file path", name: "empty file path",
controller: "file://", controller: "file://",
errmsg: "initializing client failed: invalid path for controller", errmsg: "invalid path for controller",
}, },
{ {
name: "empty controller", name: "empty controller",
controller: "", controller: "",
errmsg: "initializing client failed: invalid path for controller", errmsg: "invalid path for controller",
}, },
{ {
name: "invalid scheme", name: "invalid scheme",
controller: "foo://bar", controller: "foo://bar",
errmsg: "initializing client failed: invalid controller", errmsg: "invalid controller",
},
{
name: "serial invalid mode TCP",
controller: "file:///dev/ttyUSB0",
mode: "TCP",
errmsg: "invalid transmission mode",
}, },
} }
@ -78,7 +132,7 @@ func TestControllers(t *testing.T) {
plugin := Modbus{ plugin := Modbus{
Name: "dummy", Name: "dummy",
Controller: tt.controller, Controller: tt.controller,
TransmissionMode: "RTU", TransmissionMode: tt.mode,
Log: testutil.Logger{}, Log: testutil.Logger{},
} }
err := plugin.Init() err := plugin.Init()

View File

@ -33,10 +33,13 @@
# parity = "N" # parity = "N"
# stop_bits = 1 # stop_bits = 1
## Transmission mode for Modbus packets depending on the controller type.
## For Modbus over TCP you can choose between "TCP" , "RTUoverTCP" and ## For Modbus over TCP you can choose between "TCP" , "RTUoverTCP" and
## "ASCIIoverTCP". The default behaviour is "TCP" for ModbusTCP controllers. ## "ASCIIoverTCP".
## For Serial controllers you can choose between "RTU" and "ASCII". ## For Serial controllers you can choose between "RTU" and "ASCII".
# transmission_mode = "RTU" ## By default this is set to "auto" selecting "TCP" for ModbusTCP connections
## and "RTU" for serial connections.
# transmission_mode = "auto"
## Trace the connection to the modbus device as debug messages ## Trace the connection to the modbus device as debug messages
## Note: You have to enable telegraf's debug mode to see those messages! ## Note: You have to enable telegraf's debug mode to see those messages!