Skip to content
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ err := handler.Connect()
defer handler.Close()

client := modbus.NewClient(handler)
client.ChangeSlaveId(0x02) //you can change slaveId before use
results, err := client.ReadDiscreteInputs(15, 2)
```

References
----------
- [Modbus Specifications and Implementation Guides](http://www.modbus.org/specs.php)


Update log:
2017-07-12:add ChangeSlaveId api
1 change: 1 addition & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package modbus

type Client interface {
ChangeSlaveId(newSlaveId byte) //add 2017-7-11
// Bit access

// ReadCoils reads from 1 to 2000 contiguous status of coils in a
Expand Down
4 changes: 4 additions & 0 deletions asciiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type asciiPackager struct {
SlaveId byte
}

func (mb *asciiPackager) ChangeSlaveId(newSlaveId byte) {
mb.SlaveId = newSlaveId
}

// Encode encodes PDU in a ASCII frame:
// Start : 1 char
// Address : 2 chars
Expand Down
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ type client struct {
transporter Transporter
}

//add 2017-7-11
func (mb *client) ChangeSlaveId(newSlaveId byte) {
mb.packager.ChangeSlaveId(newSlaveId)
}

// NewClient creates a new modbus client with given backend handler.
func NewClient(handler ClientHandler) Client {
return &client{packager: handler, transporter: handler}
Expand Down
1 change: 1 addition & 0 deletions modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type Packager interface {
Encode(pdu *ProtocolDataUnit) (adu []byte, err error)
Decode(adu []byte) (pdu *ProtocolDataUnit, err error)
Verify(aduRequest []byte, aduResponse []byte) (err error)
ChangeSlaveId(newSlaveId byte)//add 2017-7-11
}

// Transporter specifies the transport layer.
Expand Down
4 changes: 4 additions & 0 deletions rtuclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type rtuPackager struct {
SlaveId byte
}

//add 2017-7-11
func (mb *rtuPackager) ChangeSlaveId(newSlaveId byte) {
mb.SlaveId = newSlaveId
}
// Encode encodes PDU in a RTU frame:
// Slave Address : 1 byte
// Function : 1 byte
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
}

//add 2017-7-11
func (mb *tcpPackager) ChangeSlaveId(newSlaveId byte) {
mb.SlaveId = newSlaveId
}

// Encode adds modbus application protocol header:
// Transaction identifier: 2 bytes
// Protocol identifier: 2 bytes
Expand Down