Skip to content

Commit

Permalink
Modbus: always assume RTU for serial devices (#16134)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Sep 15, 2024
1 parent bab0d67 commit 22db14b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 3 additions & 2 deletions meter/mbmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion util/modbus/modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions util/modbus/modbus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package modbus
import (
"testing"

"github.com/samber/lo"
"github.com/stretchr/testify/require"
)

Expand All @@ -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)
}
}

0 comments on commit 22db14b

Please sign in to comment.