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"
# stop_bits = 1
## For Modbus over TCP you can choose between "TCP", "RTUoverTCP" and
## "ASCIIoverTCP". The default behaviour is "TCP" for ModbusTCP controllers.
## Transmission mode for Modbus packets depending on the controller type.
## For Modbus over TCP you can choose between "TCP" , "RTUoverTCP" and
## "ASCIIoverTCP".
## 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
## 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
}
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":
handler := mb.NewRTUOverTCPClientHandler(host + ":" + port)
handler.Timeout = time.Duration(m.Timeout)
@ -250,12 +257,7 @@ func (m *Modbus) initClient() error {
}
m.handler = handler
default:
handler := mb.NewTCPClientHandler(host + ":" + port)
handler.Timeout = time.Duration(m.Timeout)
if m.DebugConnection {
handler.Logger = m
}
m.handler = handler
return fmt.Errorf("invalid transmission mode %q for %q", m.TransmissionMode, u.Scheme)
}
case "", "file":
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)
}
switch m.TransmissionMode {
case "RTU":
case "", "auto", "RTU":
handler := mb.NewRTUClientHandler(path)
handler.Timeout = time.Duration(m.Timeout)
handler.BaudRate = m.BaudRate
@ -286,7 +288,7 @@ func (m *Modbus) initClient() error {
}
m.handler = handler
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:
return fmt.Errorf("invalid controller %q", m.Controller)

View File

@ -25,6 +25,7 @@ func TestControllers(t *testing.T) {
var tests = []struct {
name string
controller string
mode string
errmsg string
}{
{
@ -32,9 +33,41 @@ func TestControllers(t *testing.T) {
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",
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",
@ -56,20 +89,41 @@ func TestControllers(t *testing.T) {
name: "Windows COM-port file path",
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",
controller: "file://",
errmsg: "initializing client failed: invalid path for controller",
errmsg: "invalid path for controller",
},
{
name: "empty controller",
controller: "",
errmsg: "initializing client failed: invalid path for controller",
errmsg: "invalid path for controller",
},
{
name: "invalid scheme",
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{
Name: "dummy",
Controller: tt.controller,
TransmissionMode: "RTU",
TransmissionMode: tt.mode,
Log: testutil.Logger{},
}
err := plugin.Init()

View File

@ -33,10 +33,13 @@
# parity = "N"
# stop_bits = 1
## For Modbus over TCP you can choose between "TCP", "RTUoverTCP" and
## "ASCIIoverTCP". The default behaviour is "TCP" for ModbusTCP controllers.
## Transmission mode for Modbus packets depending on the controller type.
## For Modbus over TCP you can choose between "TCP" , "RTUoverTCP" and
## "ASCIIoverTCP".
## 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
## Note: You have to enable telegraf's debug mode to see those messages!