forked from tbrandon/mbserver
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction.go
More file actions
209 lines (167 loc) · 5.38 KB
/
Copy pathfunction.go
File metadata and controls
209 lines (167 loc) · 5.38 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package mbserver
import (
"encoding/binary"
)
type Function func(Register, Framer) ([]byte, Exception)
// readCoils function 1, reads coils from internal memory.
func readCoils(r Register, frame Framer) ([]byte, Exception) {
register, numRegs := registerAddressAndNumber(frame)
if register > 65535 || register+numRegs > 65536 {
return []byte{}, IllegalDataAddress
}
dataSize := numRegs / 8
if (numRegs % 8) != 0 {
dataSize++
}
data := make([]byte, 1+dataSize)
data[0] = byte(dataSize)
coils, exception := r.ReadCoils(register, numRegs)
if exception != Success {
return []byte{}, exception
}
for i, value := range coils {
if value {
shift := uint(i) % 8
data[1+i/8] |= byte(1 << shift)
}
}
return data, Success
}
// readDiscreteInputs function 2, reads discrete inputs from internal memory.
func readDiscreteInputs(r Register, frame Framer) ([]byte, Exception) {
register, numRegs := registerAddressAndNumber(frame)
if register > 65535 || register+numRegs > 65536 {
return []byte{}, IllegalDataAddress
}
dataSize := numRegs / 8
if (numRegs % 8) != 0 {
dataSize++
}
data := make([]byte, 1+dataSize)
data[0] = byte(dataSize)
discreteInputs, exception := r.ReadDiscreteInputs(register, numRegs)
if exception != Success {
return []byte{}, exception
}
for i, value := range discreteInputs {
if value {
shift := uint(i) % 8
data[1+i/8] |= byte(1 << shift)
}
}
return data, Success
}
// readHoldingRegisters function 3, reads holding registers from internal memory.
func readHoldingRegisters(r Register, frame Framer) ([]byte, Exception) {
register, numRegs := registerAddressAndNumber(frame)
if register > 65535 || register+numRegs > 65536 {
return []byte{}, IllegalDataAddress
}
hRegisters, exception := r.ReadHoldingRegisters(register, numRegs)
if exception != Success {
return []byte{}, exception
}
data := make([]byte, 1, 1+numRegs*2)
data[0] = byte(numRegs * 2)
data = append(data, Uint16ToBytes(hRegisters)...)
return data, Success
}
// readInputRegisters function 4, reads input registers from internal memory.
func readInputRegisters(r Register, frame Framer) ([]byte, Exception) {
register, numRegs := registerAddressAndNumber(frame)
if register > 65535 || register+numRegs > 65536 {
return []byte{}, IllegalDataAddress
}
iRegisters, exception := r.ReadInputRegisters(register, numRegs)
if exception != Success {
return []byte{}, exception
}
data := make([]byte, 1, 1+numRegs*2)
data[0] = byte(numRegs * 2)
data = append(data, Uint16ToBytes(iRegisters)...)
return data, Success
}
// writeSingleCoil function 5, write a coil to internal memory.
func writeSingleCoil(r Register, frame Framer) ([]byte, Exception) {
register, value := registerAddressAndValue(frame)
if value != 0 {
value = 1 // Modbus standard uses 0 for off and 1 for on
}
if exception := r.WriteSingleCoil(register, value != 0); exception != Success {
return []byte{}, exception
}
return frame.GetData()[0:4], Success
}
// writeSingleRegister function 6, write a holding register to internal memory.
func writeSingleRegister(r Register, frame Framer) ([]byte, Exception) {
register, value := registerAddressAndValue(frame)
if exception := r.WriteSingleRegister(register, value); exception != Success {
return []byte{}, exception
}
return frame.GetData()[0:4], Success
}
// writeMultipleCoils function 15, writes holding registers to internal memory.
func writeMultipleCoils(r Register, frame Framer) ([]byte, Exception) {
register, numRegs := registerAddressAndNumber(frame)
valueBytes := frame.GetData()[5:]
if register > 65535 || numRegs > 65535-register {
return []byte{}, IllegalDataAddress
}
expectedBytes := (numRegs + 7) / 8
if len(valueBytes) < expectedBytes {
return []byte{}, IllegalDataValue
}
bitCount := 0
bitValue := make([]bool, numRegs)
for i, value := range valueBytes {
for bitPos := uint(0); bitPos < 8; bitPos++ {
bitValue[(i*8)+int(bitPos)] = bitAtPosition(value, bitPos) != 0
bitCount++
if bitCount >= numRegs {
break
}
}
if bitCount >= numRegs {
break
}
}
if exception := r.WriteMultipleCoils(register, bitValue); exception != Success {
return []byte{}, exception
}
return frame.GetData()[0:4], Success
}
// writeMultipleRegisters function 16, writes holding registers to internal memory.
func writeMultipleRegisters(r Register, frame Framer) ([]byte, Exception) {
register, numRegs := registerAddressAndNumber(frame)
valueBytes := frame.GetData()[5:]
if register > 65535 || numRegs > 65535-register {
return []byte{}, IllegalDataAddress
}
if len(valueBytes)/2 != numRegs {
return []byte{}, IllegalDataValue
}
values := BytesToUint16(valueBytes)
if exception := r.WriteMultipleRegisters(register, values); exception != Success {
return []byte{}, exception
}
return frame.GetData()[0:4], Success
}
// BytesToUint16 converts a big endian array of bytes to an array of unit16s
func BytesToUint16(bytes []byte) []uint16 {
values := make([]uint16, len(bytes)/2)
for i := range values {
values[i] = binary.BigEndian.Uint16(bytes[i*2 : (i+1)*2])
}
return values
}
// Uint16ToBytes converts an array of uint16s to a big endian array of bytes
func Uint16ToBytes(values []uint16) []byte {
bytes := make([]byte, len(values)*2)
for i, value := range values {
binary.BigEndian.PutUint16(bytes[i*2:(i+1)*2], value)
}
return bytes
}
func bitAtPosition(value uint8, pos uint) uint8 {
return (value >> pos) & 0x01
}