From fe73629adf13524f226e5896598c1344faba0649 Mon Sep 17 00:00:00 2001 From: andig Date: Wed, 1 May 2024 17:36:38 +0200 Subject: [PATCH] Fix clone --- meters/ascii.go | 14 +++++++++----- meters/asciiovertcp.go | 14 +++++++++----- meters/connection.go | 2 +- meters/mock.go | 8 ++++---- meters/rtu.go | 14 +++++++++----- meters/rtuovertcp.go | 14 +++++++++----- meters/rtuoverudp.go | 10 ++++++++-- meters/tcp.go | 14 +++++++++----- 8 files changed, 58 insertions(+), 32 deletions(-) diff --git a/meters/ascii.go b/meters/ascii.go index d24efd77..91ab2a28 100644 --- a/meters/ascii.go +++ b/meters/ascii.go @@ -10,7 +10,6 @@ import ( // ASCII is an ASCII modbus connection type ASCII struct { - device string Client modbus.Client Handler *modbus.ASCIIClientHandler prevID uint8 @@ -45,7 +44,6 @@ func NewASCII(device string, baudrate int, comset string) Connection { client := modbus.NewClient(handler) b := &ASCII{ - device: device, Client: client, Handler: handler, } @@ -55,7 +53,7 @@ func NewASCII(device string, baudrate int, comset string) Connection { // String returns the bus device func (b *ASCII) String() string { - return b.device + return b.Handler.Address } // ModbusClient returns the RTU modbus client @@ -98,6 +96,12 @@ func (b *ASCII) Close() { } // Clone clones the modbus connection. -func (b *ASCII) Clone() { - b.Handler.Clone() +func (b *ASCII) Clone(deviceID byte) Connection { + handler := b.Handler.Clone() + handler.SetSlave(deviceID) + + return &ASCII{ + Client: modbus.NewClient(handler), + Handler: handler, + } } diff --git a/meters/asciiovertcp.go b/meters/asciiovertcp.go index 568801ac..643c64b3 100644 --- a/meters/asciiovertcp.go +++ b/meters/asciiovertcp.go @@ -8,7 +8,6 @@ import ( // ASCIIOverTCP is an ASCII encoder over a TCP modbus connection type ASCIIOverTCP struct { - address string Client modbus.Client Handler *modbus.ASCIIOverTCPClientHandler prevID uint8 @@ -31,7 +30,6 @@ func NewASCIIOverTCP(address string) Connection { client := modbus.NewClient(handler) b := &ASCIIOverTCP{ - address: address, Client: client, Handler: handler, } @@ -41,7 +39,7 @@ func NewASCIIOverTCP(address string) Connection { // String returns the bus connection address (TCP) func (b *ASCIIOverTCP) String() string { - return b.address + return b.Handler.Address } // ModbusClient returns the TCP modbus client @@ -84,6 +82,12 @@ func (b *ASCIIOverTCP) Close() { } // Clone clones the modbus connection. -func (b *ASCIIOverTCP) Clone() { - b.Handler.Clone() +func (b *ASCIIOverTCP) Clone(deviceID byte) Connection { + handler := b.Handler.Clone() + handler.SetSlave(deviceID) + + return &ASCIIOverTCP{ + Client: modbus.NewClient(handler), + Handler: handler, + } } diff --git a/meters/connection.go b/meters/connection.go index 84392c82..5ebce2f3 100644 --- a/meters/connection.go +++ b/meters/connection.go @@ -25,7 +25,7 @@ type Connection interface { Close() // Clone clones the modbus connection, keeping the underlying transport. - Clone() + Clone(deviceID byte) Connection // Logger sets a logging instance for physical bus operations Logger(l Logger) diff --git a/meters/mock.go b/meters/mock.go index f2bdb9b4..0c2bb6f0 100644 --- a/meters/mock.go +++ b/meters/mock.go @@ -45,7 +45,7 @@ func (b *Mock) Logger(l Logger) { } // Slave sets the modbus device id for the following operations -func (b *Mock) Slave(deviceID uint8) { +func (b *Mock) Slave(_ uint8) { } // Timeout sets the modbus timeout @@ -54,8 +54,7 @@ func (b *Mock) Timeout(timeout time.Duration) time.Duration { } // ConnectDelay sets the the initial delay after connecting before starting communication -func (b *Mock) ConnectDelay(delay time.Duration) { - // nop +func (b *Mock) ConnectDelay(_ time.Duration) { } // Close closes the modbus connection. @@ -63,7 +62,8 @@ func (b *Mock) Close() { } // Clone clones the modbus connection. -func (b *Mock) Clone() { +func (b *Mock) Clone(_ byte) Connection { + return b } // MockClient is a mock modbus client for testing that diff --git a/meters/rtu.go b/meters/rtu.go index 9dd385bf..210066df 100644 --- a/meters/rtu.go +++ b/meters/rtu.go @@ -10,7 +10,6 @@ import ( // RTU is an RTU modbus connection type RTU struct { - device string Client modbus.Client Handler *modbus.RTUClientHandler prevID uint8 @@ -45,7 +44,6 @@ func NewRTU(device string, baudrate int, comset string) Connection { client := modbus.NewClient(handler) b := &RTU{ - device: device, Client: client, Handler: handler, } @@ -55,7 +53,7 @@ func NewRTU(device string, baudrate int, comset string) Connection { // String returns the bus device func (b *RTU) String() string { - return b.device + return b.Handler.Address } // ModbusClient returns the RTU modbus client @@ -97,6 +95,12 @@ func (b *RTU) Close() { } // Clone clones the modbus connection. -func (b *RTU) Clone() { - b.Handler.Clone() +func (b *RTU) Clone(deviceID byte) Connection { + handler := b.Handler.Clone() + handler.SetSlave(deviceID) + + return &RTU{ + Client: modbus.NewClient(handler), + Handler: handler, + } } diff --git a/meters/rtuovertcp.go b/meters/rtuovertcp.go index a9668456..cc824a21 100644 --- a/meters/rtuovertcp.go +++ b/meters/rtuovertcp.go @@ -8,7 +8,6 @@ import ( // RTUOverTCP is a RTU encoder over a TCP modbus connection type RTUOverTCP struct { - address string Client modbus.Client Handler *modbus.RTUOverTCPClientHandler prevID uint8 @@ -31,7 +30,6 @@ func NewRTUOverTCP(address string) Connection { client := modbus.NewClient(handler) b := &RTUOverTCP{ - address: address, Client: client, Handler: handler, } @@ -41,7 +39,7 @@ func NewRTUOverTCP(address string) Connection { // String returns the bus connection address (TCP) func (b *RTUOverTCP) String() string { - return b.address + return b.Handler.Address } // ModbusClient returns the TCP modbus client @@ -84,6 +82,12 @@ func (b *RTUOverTCP) Close() { } // Clone clones the modbus connection. -func (b *RTUOverTCP) Clone() { - b.Handler.Clone() +func (b *RTUOverTCP) Clone(deviceID byte) Connection { + handler := b.Handler.Clone() + handler.SetSlave(deviceID) + + return &RTUOverTCP{ + Client: modbus.NewClient(handler), + Handler: handler, + } } diff --git a/meters/rtuoverudp.go b/meters/rtuoverudp.go index bfa3ec19..1826adda 100644 --- a/meters/rtuoverudp.go +++ b/meters/rtuoverudp.go @@ -75,6 +75,12 @@ func (b *RTUOverUDP) Close() { } // Clone clones the modbus connection. -func (b *RTUOverUDP) Clone() { - b.Handler.Clone() +func (b *RTUOverUDP) Clone(deviceID byte) Connection { + handler := b.Handler.Clone() + handler.SetSlave(deviceID) + + return &RTUOverUDP{ + Client: modbus.NewClient(handler), + Handler: handler, + } } diff --git a/meters/tcp.go b/meters/tcp.go index b0db3ba9..bec8f891 100644 --- a/meters/tcp.go +++ b/meters/tcp.go @@ -8,7 +8,6 @@ import ( // TCP is a TCP modbus connection type TCP struct { - address string Client modbus.Client Handler *modbus.TCPClientHandler } @@ -30,7 +29,6 @@ func NewTCP(address string) Connection { client := modbus.NewClient(handler) b := &TCP{ - address: address, Client: client, Handler: handler, } @@ -40,7 +38,7 @@ func NewTCP(address string) Connection { // String returns the bus connection address (TCP) func (b *TCP) String() string { - return b.address + return b.Handler.Address } // ModbusClient returns the TCP modbus client @@ -77,6 +75,12 @@ func (b *TCP) Close() { } // Clone clones the modbus connection. -func (b *TCP) Clone() { - b.Handler.Clone() +func (b *TCP) Clone(deviceID byte) Connection { + handler := b.Handler.Clone() + handler.SetSlave(deviceID) + + return &TCP{ + Client: modbus.NewClient(handler), + Handler: handler, + } }