-
Notifications
You must be signed in to change notification settings - Fork 1
/
readwritemultipleregistersresponse.go
132 lines (118 loc) · 4.35 KB
/
readwritemultipleregistersresponse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package packet
import (
"encoding/binary"
"errors"
)
// ReadWriteMultipleRegistersResponseTCP is TCP Response for Read / Write Multiple Registers request (FC=23)
//
// Example packet: 0x01 0x38 0x00 0x00 0x00 0x05 0x11 0x17 0x02 0xCD 0x6B
// 0x01 0x38 - transaction id (0,1)
// 0x00 0x00 - protocol id (2,3)
// 0x00 0x05 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5)
// 0x11 - unit id (6)
// 0x17 - function code (7)
// 0x02 - registers bytes count (8)
// 0xCD 0x6B - write registers data (1 registers) (9, 10, ...)
type ReadWriteMultipleRegistersResponseTCP struct {
MBAPHeader
ReadWriteMultipleRegistersResponse
}
// ReadWriteMultipleRegistersResponseRTU is RTU Response for Read / Write Multiple Registers request (FC=23)
//
// Example packet: 0x11 0x17 0x02 0xCD 0x6B 0x69 0x08
// 0x11 - unit id (0)
// 0x17 - function code (1)
// 0x02 - registers bytes count (2)
// 0xCD 0x6B - write registers data (1 registers) (3, 4, ...)
// 0x69 0x08 - CRC16 (n-2,n-1)
type ReadWriteMultipleRegistersResponseRTU struct {
ReadWriteMultipleRegistersResponse
}
// ReadWriteMultipleRegistersResponse is Response for Read / Write Multiple Registers request (FC=23)
type ReadWriteMultipleRegistersResponse struct {
UnitID uint8
RegisterByteLen uint8
Data []byte
}
// Bytes returns ReadWriteMultipleRegistersResponseTCP packet as bytes form
func (r ReadWriteMultipleRegistersResponseTCP) Bytes() []byte {
length := r.len()
result := make([]byte, tcpMBAPHeaderLen+length)
r.MBAPHeader.bytes(result[0:6], length)
r.ReadWriteMultipleRegistersResponse.bytes(result[6 : 6+length])
return result
}
// ParseReadWriteMultipleRegistersResponseTCP parses given bytes into ReadWriteMultipleRegistersResponseTCP
func ParseReadWriteMultipleRegistersResponseTCP(data []byte) (*ReadWriteMultipleRegistersResponseTCP, error) {
dLen := len(data)
if dLen < 11 {
return nil, errors.New("received data length too short to be valid packet")
}
byteLen := int(data[8])
if dLen != 9+byteLen {
return nil, errors.New("received data length does not match byte len in packet")
}
return &ReadWriteMultipleRegistersResponseTCP{
MBAPHeader: MBAPHeader{
TransactionID: binary.BigEndian.Uint16(data[0:2]),
ProtocolID: 0,
},
ReadWriteMultipleRegistersResponse: ReadWriteMultipleRegistersResponse{
UnitID: data[6],
// function code = data[7]
RegisterByteLen: data[8],
Data: data[9 : 9+byteLen],
},
}, nil
}
// Bytes returns ReadWriteMultipleRegistersResponseRTU packet as bytes form
func (r ReadWriteMultipleRegistersResponseRTU) Bytes() []byte {
length := r.len()
result := make([]byte, length+2)
bytes := r.ReadWriteMultipleRegistersResponse.bytes(result)
crc := CRC16(bytes[:length])
result[length] = uint8(crc)
result[length+1] = uint8(crc >> 8)
return result
}
// ParseReadWriteMultipleRegistersResponseRTU parses given bytes into ReadWriteMultipleRegistersResponseRTU
func ParseReadWriteMultipleRegistersResponseRTU(data []byte) (*ReadWriteMultipleRegistersResponseRTU, error) {
dLen := len(data)
if dLen < 7 {
return nil, errors.New("received data length too short to be valid packet")
}
byteLen := int(data[2])
if dLen != 3+byteLen+2 {
return nil, errors.New("received data length does not match byte len in packet")
}
return &ReadWriteMultipleRegistersResponseRTU{
ReadWriteMultipleRegistersResponse: ReadWriteMultipleRegistersResponse{
UnitID: data[0],
// function code = data[1]
RegisterByteLen: data[2],
Data: data[3 : 3+byteLen],
},
}, nil
}
// FunctionCode returns function code of this request
func (r ReadWriteMultipleRegistersResponse) FunctionCode() uint8 {
return FunctionReadWriteMultipleRegisters
}
func (r ReadWriteMultipleRegistersResponse) len() uint16 {
return 3 + uint16(r.RegisterByteLen)
}
// Bytes returns ReadWriteMultipleRegistersResponse packet as bytes form
func (r ReadWriteMultipleRegistersResponse) Bytes() []byte {
return r.bytes(make([]byte, r.len()))
}
func (r ReadWriteMultipleRegistersResponse) bytes(data []byte) []byte {
data[0] = r.UnitID
data[1] = FunctionReadWriteMultipleRegisters
data[2] = r.RegisterByteLen
copy(data[3:], r.Data)
return data
}
// AsRegisters returns response data as Register to more convenient access
func (r ReadWriteMultipleRegistersResponse) AsRegisters(requestStartAddress uint16) (*Registers, error) {
return NewRegisters(r.Data, requestStartAddress)
}