From 7fdaa04607e9a966279bac75280a9b3960bdc8f1 Mon Sep 17 00:00:00 2001 From: andig Date: Wed, 19 Sep 2018 09:37:05 +0200 Subject: [PATCH 1/3] Consistently format serial logs --- rtuclient.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtuclient.go b/rtuclient.go index 4f52dac..ad6a31a 100644 --- a/rtuclient.go +++ b/rtuclient.go @@ -120,7 +120,7 @@ func (mb *rtuSerialTransporter) Send(aduRequest []byte) (aduResponse []byte, err mb.serialPort.startCloseTimer() // Send the request - mb.serialPort.logf("modbus: sending %q\n", aduRequest) + mb.serialPort.logf("modbus: sending % x\n", aduRequest) if _, err = mb.port.Write(aduRequest); err != nil { return } From 5dd0834cded69438fde6b7d0d7eb801db4f48c17 Mon Sep 17 00:00:00 2001 From: andig Date: Tue, 9 Oct 2018 09:05:14 +0200 Subject: [PATCH 2/3] Declare SetSlave api --- asciiclient.go | 5 +++++ modbus.go | 1 + rtuclient.go | 5 +++++ tcpclient.go | 5 +++++ 4 files changed, 16 insertions(+) diff --git a/asciiclient.go b/asciiclient.go index 98d9c60..44dd806 100644 --- a/asciiclient.go +++ b/asciiclient.go @@ -46,6 +46,11 @@ type asciiPackager struct { SlaveId byte } +// SetSlave sets modbus slave id for the next client operations +func (mb *asciiPackager) SetSlave(slaveId byte) { + mb.SlaveId = slaveId +} + // Encode encodes PDU in a ASCII frame: // Start : 1 char // Address : 2 chars diff --git a/modbus.go b/modbus.go index 869473c..070e48e 100644 --- a/modbus.go +++ b/modbus.go @@ -82,6 +82,7 @@ type ProtocolDataUnit struct { // Packager specifies the communication layer. type Packager interface { + SetSlave(slaveId byte) Encode(pdu *ProtocolDataUnit) (adu []byte, err error) Decode(adu []byte) (pdu *ProtocolDataUnit, err error) Verify(aduRequest []byte, aduResponse []byte) (err error) diff --git a/rtuclient.go b/rtuclient.go index ad6a31a..def4772 100644 --- a/rtuclient.go +++ b/rtuclient.go @@ -44,6 +44,11 @@ type rtuPackager struct { SlaveId byte } +// SetSlave sets modbus slave id for the next client operations +func (mb *rtuPackager) SetSlave(slaveId byte) { + mb.SlaveId = slaveId +} + // Encode encodes PDU in a RTU frame: // Slave Address : 1 byte // Function : 1 byte diff --git a/tcpclient.go b/tcpclient.go index 4e53c73..10acc6b 100644 --- a/tcpclient.go +++ b/tcpclient.go @@ -55,6 +55,11 @@ type tcpPackager struct { SlaveId byte } +// SetSlave sets modbus slave id for the next client operations +func (mb *tcpPackager) SetSlave(slaveId byte) { + mb.SlaveId = slaveId +} + // Encode adds modbus application protocol header: // Transaction identifier: 2 bytes // Protocol identifier: 2 bytes From df54eabf1017db4b3c07440b9d154ce9cef59e6d Mon Sep 17 00:00:00 2001 From: andig Date: Tue, 9 Oct 2018 09:05:30 +0200 Subject: [PATCH 3/3] Expose Connector api --- client.go | 1 + modbus.go | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/client.go b/client.go index ac3ee2e..f9c4c04 100644 --- a/client.go +++ b/client.go @@ -13,6 +13,7 @@ import ( type ClientHandler interface { Packager Transporter + Connector } type client struct { diff --git a/modbus.go b/modbus.go index 070e48e..d137dcc 100644 --- a/modbus.go +++ b/modbus.go @@ -92,3 +92,8 @@ type Packager interface { type Transporter interface { Send(aduRequest []byte) (aduResponse []byte, err error) } + +type Connector interface { + Connect() error + Close() error +}