Skip to content

Commit

Permalink
Merge pull request #8 from andig/connect
Browse files Browse the repository at this point in the history
feat(handler): Implement SlaveId and Connector APIs
  • Loading branch information
gq0 authored Oct 18, 2018
2 parents ca14cd1 + df54eab commit 0902a7b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
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 @@ -100,6 +100,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 @@ -109,3 +110,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 @@ -63,6 +63,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

0 comments on commit 0902a7b

Please sign in to comment.