-
Notifications
You must be signed in to change notification settings - Fork 1
/
writesinglecoilresponse.go
132 lines (120 loc) · 4.2 KB
/
writesinglecoilresponse.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"
)
// WriteSingleCoilResponseTCP is TCP Response for Write Single Coil (FC=05)
//
// Data part of packet is always 4 bytes - 2 byte for address and 2 byte for coil status (FF00 = on, 0000 = off).
// For example: coil at address 1 is turned on '0x00 0x01 0xFF 0x00'
// For example: coil at address 10 is turned off '0x00 0x0A 0x00 0x00'
//
// Example packet: 0x00 0x01 0x00 0x00 0x00 0x06 0x03 0x05 0x00 0x02 0xFF 0x00
// 0x00 0x01 - transaction id (0,1)
// 0x00 0x00 - protocol id (2,3)
// 0x00 0x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5)
// 0x03 - unit id (6)
// 0x05 - function code (7)
// 0x00 0x02 - start address (8,9)
// 0xFF 0x00 - coil data (true) (10,11)
type WriteSingleCoilResponseTCP struct {
MBAPHeader
WriteSingleCoilResponse
}
// WriteSingleCoilResponseRTU is RTU Response for Write Single Coil (FC=05)
//
// Data part of packet is always 4 bytes - 2 byte for address and 2 byte for coil status (FF00 = on, 0000 = off).
// For example: coil at address 1 is turned on '0x00 0x01 0xFF 0x00'
// For example: coil at address 10 is turned off '0x00 0x0A 0x00 0x00'
//
// Example packet: 0x03 0x05 0x00 0x02 0xFF 0x00 0x2c 0x18
// 0x03 - unit id (0)
// 0x05 - function code (1)
// 0x00 0x02 - start address (2,3)
// 0xFF 0x00 - coil data (true) (4,5)
// 0x2c 0x18 - CRC16 (6,7)
type WriteSingleCoilResponseRTU struct {
WriteSingleCoilResponse
}
// WriteSingleCoilResponse is Response for Write Single Coil (FC=05)
type WriteSingleCoilResponse struct {
UnitID uint8
StartAddress uint16
CoilState bool
}
// Bytes returns WriteSingleCoilResponseTCP packet as bytes form
func (r WriteSingleCoilResponseTCP) Bytes() []byte {
length := uint16(6)
result := make([]byte, tcpMBAPHeaderLen+length)
r.MBAPHeader.bytes(result[0:6], length)
r.WriteSingleCoilResponse.bytes(result[6 : 6+length])
return result
}
// ParseWriteSingleCoilResponseTCP parses given bytes into ParseWriteSingleCoilResponseTCP
func ParseWriteSingleCoilResponseTCP(data []byte) (*WriteSingleCoilResponseTCP, error) {
dLen := len(data)
if dLen < 12 {
return nil, errors.New("received data length too short to be valid packet")
}
pduLen := binary.BigEndian.Uint16(data[4:6])
if dLen != 6+int(pduLen) {
return nil, errors.New("received data length does not match PDU len in packet")
}
return &WriteSingleCoilResponseTCP{
MBAPHeader: MBAPHeader{
TransactionID: binary.BigEndian.Uint16(data[0:2]),
ProtocolID: 0,
},
WriteSingleCoilResponse: WriteSingleCoilResponse{
UnitID: data[6],
StartAddress: binary.BigEndian.Uint16(data[8:10]),
CoilState: binary.BigEndian.Uint16(data[10:12]) == writeCoilOn,
},
}, nil
}
// Bytes returns WriteSingleCoilResponseRTU packet as bytes form
func (r WriteSingleCoilResponseRTU) Bytes() []byte {
result := make([]byte, 6+2)
bytes := r.WriteSingleCoilResponse.bytes(result)
crc := CRC16(bytes[:6])
result[6] = uint8(crc)
result[7] = uint8(crc >> 8)
return result
}
// ParseWriteSingleCoilResponseRTU parses given bytes into WriteSingleCoilResponseRTU
func ParseWriteSingleCoilResponseRTU(data []byte) (*WriteSingleCoilResponseRTU, error) {
dLen := len(data)
if dLen < 8 {
return nil, errors.New("received data length too short to be valid packet")
}
if dLen > 8 {
return nil, errors.New("received data length too long to be valid packet")
}
return &WriteSingleCoilResponseRTU{
WriteSingleCoilResponse: WriteSingleCoilResponse{
UnitID: data[0],
// data[1] function code
StartAddress: binary.BigEndian.Uint16(data[2:4]),
CoilState: binary.BigEndian.Uint16(data[4:6]) == writeCoilOn,
},
}, nil
}
// FunctionCode returns function code of this request
func (r WriteSingleCoilResponse) FunctionCode() uint8 {
return FunctionWriteSingleCoil
}
// Bytes returns WriteSingleCoilResponse packet as bytes form
func (r WriteSingleCoilResponse) Bytes() []byte {
return r.bytes(make([]byte, 6))
}
func (r WriteSingleCoilResponse) bytes(bytes []byte) []byte {
bytes[0] = r.UnitID
bytes[1] = FunctionWriteSingleCoil
binary.BigEndian.PutUint16(bytes[2:4], r.StartAddress)
coilState := writeCoilOff
if r.CoilState {
coilState = writeCoilOn
}
binary.BigEndian.PutUint16(bytes[4:6], coilState)
return bytes
}