fix(inputs.modbus): Fix Windows COM-port path (#12339)
This commit is contained in:
parent
e44129869c
commit
eea9021771
|
|
@ -41,15 +41,18 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
|||
controller = "tcp://localhost:502"
|
||||
|
||||
## Serial (RS485; RS232)
|
||||
## For unix-like operating systems use:
|
||||
# controller = "file:///dev/ttyUSB0"
|
||||
## For Windows operating systems use:
|
||||
# controller = "COM1"
|
||||
# baud_rate = 9600
|
||||
# data_bits = 8
|
||||
# parity = "N"
|
||||
# stop_bits = 1
|
||||
|
||||
## 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"
|
||||
## For Modbus over TCP you can choose between "TCP", "RTUoverTCP" and
|
||||
## "ASCIIoverTCP". The default behaviour is "TCP" for ModbusTCP controllers.
|
||||
## For Serial controllers you can choose between "RTU" and "ASCII".
|
||||
# transmission_mode = "RTU"
|
||||
|
||||
## Trace the connection to the modbus device as debug messages
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
|
|
@ -256,10 +257,14 @@ func (m *Modbus) initClient() error {
|
|||
}
|
||||
m.handler = handler
|
||||
}
|
||||
case "file":
|
||||
case "", "file":
|
||||
path := filepath.Join(u.Host, u.Path)
|
||||
if path == "" {
|
||||
return fmt.Errorf("invalid path for controller %q", m.Controller)
|
||||
}
|
||||
switch m.TransmissionMode {
|
||||
case "RTU":
|
||||
handler := mb.NewRTUClientHandler(u.Path)
|
||||
handler := mb.NewRTUClientHandler(path)
|
||||
handler.Timeout = time.Duration(m.Timeout)
|
||||
handler.BaudRate = m.BaudRate
|
||||
handler.DataBits = m.DataBits
|
||||
|
|
@ -270,7 +275,7 @@ func (m *Modbus) initClient() error {
|
|||
}
|
||||
m.handler = handler
|
||||
case "ASCII":
|
||||
handler := mb.NewASCIIClientHandler(u.Path)
|
||||
handler := mb.NewASCIIClientHandler(path)
|
||||
handler.Timeout = time.Duration(m.Timeout)
|
||||
handler.BaudRate = m.BaudRate
|
||||
handler.DataBits = m.DataBits
|
||||
|
|
|
|||
|
|
@ -21,6 +21,76 @@ import (
|
|||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
func TestControllers(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
controller string
|
||||
errmsg string
|
||||
}{
|
||||
{
|
||||
name: "TCP host",
|
||||
controller: "tcp://localhost:502",
|
||||
},
|
||||
{
|
||||
name: "invalid TCP host",
|
||||
controller: "tcp://localhost",
|
||||
errmsg: "initializing client failed: address localhost: missing port in address",
|
||||
},
|
||||
{
|
||||
name: "absolute file path",
|
||||
controller: "file:///dev/ttyUSB0",
|
||||
},
|
||||
{
|
||||
name: "relative file path",
|
||||
controller: "file://dev/ttyUSB0",
|
||||
},
|
||||
{
|
||||
name: "relative file path with dot",
|
||||
controller: "file://./dev/ttyUSB0",
|
||||
},
|
||||
{
|
||||
name: "Windows COM-port",
|
||||
controller: "COM2",
|
||||
},
|
||||
{
|
||||
name: "Windows COM-port file path",
|
||||
controller: "file://com2",
|
||||
},
|
||||
{
|
||||
name: "empty file path",
|
||||
controller: "file://",
|
||||
errmsg: "initializing client failed: invalid path for controller",
|
||||
},
|
||||
{
|
||||
name: "empty controller",
|
||||
controller: "",
|
||||
errmsg: "initializing client failed: invalid path for controller",
|
||||
},
|
||||
{
|
||||
name: "invalid scheme",
|
||||
controller: "foo://bar",
|
||||
errmsg: "initializing client failed: invalid controller",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
plugin := Modbus{
|
||||
Name: "dummy",
|
||||
Controller: tt.controller,
|
||||
TransmissionMode: "RTU",
|
||||
Log: testutil.Logger{},
|
||||
}
|
||||
err := plugin.Init()
|
||||
if tt.errmsg != "" {
|
||||
require.ErrorContains(t, err, tt.errmsg)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoils(t *testing.T) {
|
||||
var coilTests = []struct {
|
||||
name string
|
||||
|
|
|
|||
|
|
@ -24,15 +24,18 @@
|
|||
controller = "tcp://localhost:502"
|
||||
|
||||
## Serial (RS485; RS232)
|
||||
## For unix-like operating systems use:
|
||||
# controller = "file:///dev/ttyUSB0"
|
||||
## For Windows operating systems use:
|
||||
# controller = "COM1"
|
||||
# baud_rate = 9600
|
||||
# data_bits = 8
|
||||
# parity = "N"
|
||||
# stop_bits = 1
|
||||
|
||||
## 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"
|
||||
## For Modbus over TCP you can choose between "TCP", "RTUoverTCP" and
|
||||
## "ASCIIoverTCP". The default behaviour is "TCP" for ModbusTCP controllers.
|
||||
## For Serial controllers you can choose between "RTU" and "ASCII".
|
||||
# transmission_mode = "RTU"
|
||||
|
||||
## Trace the connection to the modbus device as debug messages
|
||||
|
|
|
|||
Loading…
Reference in New Issue