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
22 changes: 11 additions & 11 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,41 @@ type Client interface {

// ReadCoils reads from 1 to 2000 contiguous status of coils in a
// remote device and returns coil status.
ReadCoils(address, quantity uint16) (results []byte, err error)
ReadCoils(slaveId uint8, address, quantity uint16) (results []byte, err error)
// ReadDiscreteInputs reads from 1 to 2000 contiguous status of
// discrete inputs in a remote device and returns input status.
ReadDiscreteInputs(address, quantity uint16) (results []byte, err error)
ReadDiscreteInputs(slaveId uint8, address, quantity uint16) (results []byte, err error)
// WriteSingleCoil write a single output to either ON or OFF in a
// remote device and returns output value.
WriteSingleCoil(address, value uint16) (results []byte, err error)
WriteSingleCoil(slaveId uint8, address, value uint16) (results []byte, err error)
// WriteMultipleCoils forces each coil in a sequence of coils to either
// ON or OFF in a remote device and returns quantity of outputs.
WriteMultipleCoils(address, quantity uint16, value []byte) (results []byte, err error)
WriteMultipleCoils(slaveId uint8, address, quantity uint16, value []byte) (results []byte, err error)

// 16-bit access

// ReadInputRegisters reads from 1 to 125 contiguous input registers in
// a remote device and returns input registers.
ReadInputRegisters(address, quantity uint16) (results []byte, err error)
ReadInputRegisters(slaveId uint8, address, quantity uint16) (results []byte, err error)
// ReadHoldingRegisters reads the contents of a contiguous block of
// holding registers in a remote device and returns register value.
ReadHoldingRegisters(address, quantity uint16) (results []byte, err error)
ReadHoldingRegisters(slaveId uint8, address, quantity uint16) (results []byte, err error)
// WriteSingleRegister writes a single holding register in a remote
// device and returns register value.
WriteSingleRegister(address, value uint16) (results []byte, err error)
WriteSingleRegister(slaveId uint8, address, value uint16) (results []byte, err error)
// WriteMultipleRegisters writes a block of contiguous registers
// (1 to 123 registers) in a remote device and returns quantity of
// registers.
WriteMultipleRegisters(address, quantity uint16, value []byte) (results []byte, err error)
WriteMultipleRegisters(slaveId uint8, address, quantity uint16, value []byte) (results []byte, err error)
// ReadWriteMultipleRegisters performs a combination of one read
// operation and one write operation. It returns read registers value.
ReadWriteMultipleRegisters(readAddress, readQuantity, writeAddress, writeQuantity uint16, value []byte) (results []byte, err error)
ReadWriteMultipleRegisters(slaveId uint8, readAddress, readQuantity, writeAddress, writeQuantity uint16, value []byte) (results []byte, err error)
// MaskWriteRegister modify the contents of a specified holding
// register using a combination of an AND mask, an OR mask, and the
// register's current contents. The function returns
// AND-mask and OR-mask.
MaskWriteRegister(address, andMask, orMask uint16) (results []byte, err error)
MaskWriteRegister(slaveId uint8, address, andMask, orMask uint16) (results []byte, err error)
//ReadFIFOQueue reads the contents of a First-In-First-Out (FIFO) queue
// of register in a remote device and returns FIFO value register.
ReadFIFOQueue(address uint16) (results []byte, err error)
ReadFIFOQueue(slaveId uint8, address uint16) (results []byte, err error)
}
19 changes: 10 additions & 9 deletions asciiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,20 @@ type asciiPackager struct {
}

// Encode encodes PDU in a ASCII frame:
// Start : 1 char
// Address : 2 chars
// Function : 2 chars
// Data : 0 up to 2x252 chars
// LRC : 2 chars
// End : 2 chars
func (mb *asciiPackager) Encode(pdu *ProtocolDataUnit) (adu []byte, err error) {
//
// Start : 1 char
// Address : 2 chars
// Function : 2 chars
// Data : 0 up to 2x252 chars
// LRC : 2 chars
// End : 2 chars
func (mb *asciiPackager) Encode(slaveId uint8, pdu *ProtocolDataUnit) (adu []byte, err error) {
var buf bytes.Buffer

if _, err = buf.WriteString(asciiStart); err != nil {
return
}
if err = writeHex(&buf, []byte{mb.SlaveId, pdu.FunctionCode}); err != nil {
if err = writeHex(&buf, []byte{slaveId, pdu.FunctionCode}); err != nil {
return
}
if err = writeHex(&buf, pdu.Data); err != nil {
Expand All @@ -68,7 +69,7 @@ func (mb *asciiPackager) Encode(pdu *ProtocolDataUnit) (adu []byte, err error) {
// Exclude the beginning colon and terminating CRLF pair characters
var lrc lrc
lrc.reset()
lrc.pushByte(mb.SlaveId).pushByte(pdu.FunctionCode).pushBytes(pdu.Data)
lrc.pushByte(slaveId).pushByte(pdu.FunctionCode).pushBytes(pdu.Data)
if err = writeHex(&buf, []byte{lrc.value()}); err != nil {
return
}
Expand Down
9 changes: 3 additions & 6 deletions asciiclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import (

func TestASCIIEncoding(t *testing.T) {
encoder := asciiPackager{}
encoder.SlaveId = 17

pdu := ProtocolDataUnit{}
pdu.FunctionCode = 3
pdu.Data = []byte{0, 107, 0, 3}

adu, err := encoder.Encode(&pdu)
adu, err := encoder.Encode(17, &pdu)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -47,15 +46,13 @@ func TestASCIIDecoding(t *testing.T) {
}

func BenchmarkASCIIEncoder(b *testing.B) {
encoder := asciiPackager{
SlaveId: 10,
}
encoder := asciiPackager{}
pdu := ProtocolDataUnit{
FunctionCode: 1,
Data: []byte{2, 3, 4, 5, 6, 7, 8, 9},
}
for i := 0; i < b.N; i++ {
_, err := encoder.Encode(&pdu)
_, err := encoder.Encode(10, &pdu)
if err != nil {
b.Fatal(err)
}
Expand Down
Loading