Skip to content

Commit

Permalink
Fix clone
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed May 1, 2024
1 parent 0d34151 commit fe73629
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 32 deletions.
14 changes: 9 additions & 5 deletions meters/ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

// ASCII is an ASCII modbus connection
type ASCII struct {
device string
Client modbus.Client
Handler *modbus.ASCIIClientHandler
prevID uint8
Expand Down Expand Up @@ -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,
}
Expand All @@ -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
Expand Down Expand Up @@ -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,
}
}
14 changes: 9 additions & 5 deletions meters/asciiovertcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,7 +30,6 @@ func NewASCIIOverTCP(address string) Connection {
client := modbus.NewClient(handler)

b := &ASCIIOverTCP{
address: address,
Client: client,
Handler: handler,
}
Expand All @@ -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
Expand Down Expand Up @@ -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,
}
}
2 changes: 1 addition & 1 deletion meters/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions meters/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -54,16 +54,16 @@ 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.
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
Expand Down
14 changes: 9 additions & 5 deletions meters/rtu.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

// RTU is an RTU modbus connection
type RTU struct {
device string
Client modbus.Client
Handler *modbus.RTUClientHandler
prevID uint8
Expand Down Expand Up @@ -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,
}
Expand All @@ -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
Expand Down Expand Up @@ -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,
}
}
14 changes: 9 additions & 5 deletions meters/rtuovertcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,7 +30,6 @@ func NewRTUOverTCP(address string) Connection {
client := modbus.NewClient(handler)

b := &RTUOverTCP{
address: address,
Client: client,
Handler: handler,
}
Expand All @@ -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
Expand Down Expand Up @@ -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,
}
}
10 changes: 8 additions & 2 deletions meters/rtuoverudp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
14 changes: 9 additions & 5 deletions meters/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

// TCP is a TCP modbus connection
type TCP struct {
address string
Client modbus.Client
Handler *modbus.TCPClientHandler
}
Expand All @@ -30,7 +29,6 @@ func NewTCP(address string) Connection {
client := modbus.NewClient(handler)

b := &TCP{
address: address,
Client: client,
Handler: handler,
}
Expand All @@ -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
Expand Down Expand Up @@ -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,
}
}

0 comments on commit fe73629

Please sign in to comment.