forked from flosse/go-modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
46 lines (41 loc) · 794 Bytes
/
util.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
package modbus
import (
"encoding/binary"
"math"
)
func wordsToByteArray(words ...uint16) []byte {
array := make([]byte, 2*len(words))
for i, v := range words {
binary.BigEndian.PutUint16(array[i*2:], v)
}
return array
}
func bytesToWordArray(bytes ...byte) []uint16 {
l := len(bytes)
n := int(math.Ceil(float64(l) / 2))
array := make([]uint16, n)
for i := 0; i < n; i++ {
j := i * 2
if j+2 > l {
array[i] = uint16(bytes[j])
} else {
array[i] = binary.BigEndian.Uint16(bytes[j : j+2])
}
}
return array
}
func filterNullChar(a []byte) []byte {
x := filter(a, func(c byte) bool {
return c != 0
})
return x
}
func filter(s []byte, fn func(byte) bool) []byte {
var p []byte // == nil
for _, v := range s {
if fn(v) {
p = append(p, v)
}
}
return p
}