Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SlaveId and Connector APIs #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions asciiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type ClientHandler interface {
Packager
Transporter
Connector
}

type client struct {
Expand Down
6 changes: 6 additions & 0 deletions modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -91,3 +92,8 @@ type Packager interface {
type Transporter interface {
Send(aduRequest []byte) (aduResponse []byte, err error)
}

type Connector interface {
Connect() error
Close() error
}
7 changes: 6 additions & 1 deletion rtuclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -120,7 +125,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
}
Expand Down
5 changes: 5 additions & 0 deletions tcpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down