diff --git a/meter/modbus.go b/meter/modbus.go index 1e512bf3a..23fcd0f9b 100644 --- a/meter/modbus.go +++ b/meter/modbus.go @@ -45,6 +45,10 @@ func NewModbusFromConfig(other map[string]interface{}) (api.Meter, error) { log := util.NewLogger("modbus") conn, err := modbus.NewConnection(cc.URI, cc.Device, cc.Comset, cc.Baudrate, *cc.RTU) + if err != nil { + return nil, err + } + conn.Logger(log.TRACE) // prepare device diff --git a/util/modbus/modbus.go b/util/modbus/modbus.go index 19d0b0101..802f17636 100644 --- a/util/modbus/modbus.go +++ b/util/modbus/modbus.go @@ -26,32 +26,20 @@ type Connection struct { RTU *bool // indicates RTU over TCP if true } -var connections map[string]meters.Connection - -func registeredConnection(key string, newConn meters.Connection) meters.Connection { - if connections == nil { - connections = make(map[string]meters.Connection) - } - - if conn, ok := connections[key]; ok { - return conn - } - - connections[key] = newConn - return newConn -} - // NewConnection creates physical modbus device from config func NewConnection(uri, device, comset string, baudrate int, rtu bool) (conn meters.Connection, err error) { if device != "" { - conn = registeredConnection(device, meters.NewRTU(device, baudrate, comset)) + if baudrate == 0 || comset == "" { + return nil, errors.New("invalid modbus configuration: need baudrate and comset") + } + conn = meters.NewRTU(device, baudrate, comset) } if uri != "" { if rtu { - conn = registeredConnection(uri, meters.NewRTUOverTCP(uri)) + conn = meters.NewRTUOverTCP(uri) } else { - conn = registeredConnection(uri, meters.NewTCP(uri)) + conn = meters.NewTCP(uri) } }