Modbus: always assume RTU for serial devices (#16134)

This commit is contained in:
andig 2024-09-15 16:18:27 +02:00 • committed by GitHub
parent bab0d67d93
commit 22db14bb4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 3 deletions

View file

@ -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()

View file

@ -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

View file

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