Modbus: always assume RTU for serial devices (#16134)
This commit is contained in:
parent
bab0d67d93
commit
22db14bb4c
3 changed files with 22 additions and 3 deletions
|
|
@ -53,8 +53,9 @@ func NewModbusMbmdFromConfig(other map[string]interface{}) (api.Meter, error) {
|
|||
|
||||
// assume RTU if not set and this is a known RS485 meter model
|
||||
if cc.RTU == nil {
|
||||
b := modbus.IsRS485(cc.Model)
|
||||
cc.RTU = &b
|
||||
if rtu := modbus.IsRS485(cc.Model); rtu {
|
||||
cc.RTU = &rtu
|
||||
}
|
||||
}
|
||||
|
||||
modbus.Lock()
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ func (s Settings) Protocol() Protocol {
|
|||
switch {
|
||||
case s.UDP:
|
||||
return Udp
|
||||
case s.RTU != nil && *s.RTU:
|
||||
case s.Device != "" || s.RTU != nil && *s.RTU:
|
||||
return Rtu
|
||||
default:
|
||||
return Tcp
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package modbus
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
|
|
@ -23,3 +24,20 @@ func TestParsePoint(t *testing.T) {
|
|||
require.Equal(t, tc.ops, ops)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSettingsProtocol(t *testing.T) {
|
||||
tc := []struct {
|
||||
Settings
|
||||
res Protocol
|
||||
}{
|
||||
{Settings{UDP: true}, Udp},
|
||||
{Settings{RTU: lo.ToPtr(true)}, Rtu},
|
||||
{Settings{Device: "foo"}, Rtu},
|
||||
{Settings{URI: "foo"}, Tcp},
|
||||
{Settings{}, Tcp},
|
||||
}
|
||||
|
||||
for _, tc := range tc {
|
||||
require.Equal(t, tc.res, tc.Protocol(), tc)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue