-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirectory.go
83 lines (78 loc) · 1.82 KB
/
directory.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
package s7
import (
"fmt"
)
// Copyright 2018 Trung Hieu Le. All rights reserved.
const (
// Block type byte
BlockOB = 56
BlockDB = 65
BlockSDB = 66
BlockFC = 67
BlockSFC = 68
BlockFB = 69
BlockSFB = 70
)
//S7BlocksList Block List
type S7BlocksList struct {
OBList []int
FBList []int
FCList []int
SFBList []int
SFCList []int
DBList []int
SDBList []int
}
//implement list block
func (mb *client) PGListBlocks() (list S7BlocksList, err error) {
list.OBList, err = mb.pgBlockList(BlockOB)
//debug
fmt.Printf("%v", list.DBList)
list.DBList, err = mb.pgBlockList(BlockDB)
list.FCList, err = mb.pgBlockList(BlockFC)
list.OBList, err = mb.pgBlockList(BlockOB)
list.FBList, err = mb.pgBlockList(BlockFB)
list.SDBList, err = mb.pgBlockList(BlockSDB)
list.SFBList, err = mb.pgBlockList(BlockSFB)
list.SFCList, err = mb.pgBlockList(BlockSFC)
return
}
func (mb *client) pgBlockList(blockType byte) (arr []int, err error) {
bl := make([]byte, len(s7PGBlockListTelegram))
copy(bl, s7PGBlockListTelegram)
bl = append(bl, make([]byte, 1)...)
switch blockType {
case BlockDB:
bl[len(bl)-1] = BlockDB
case BlockOB:
bl[len(bl)-1] = BlockOB
case BlockSDB:
bl[len(bl)-1] = BlockSDB
case BlockFC:
bl[len(bl)-1] = BlockFC
case BlockSFC:
bl[len(bl)-1] = BlockSFC
case BlockFB:
bl[len(bl)-1] = BlockFB
case BlockSFB:
bl[len(bl)-1] = BlockSFB
default:
return
}
request := NewProtocolDataUnit(bl)
//send
response, err := mb.send(&request)
if err == nil {
res := make([]byte, len(response.Data)-33) //remove first 26 byte function and 7 byte header
copy(res, response.Data[33:len(response.Data)])
arr = dataToBlocks(res)
}
return
}
func dataToBlocks(data []byte) []int {
arr := make([]int, len(data)/4)
for i := 0; i <= len(data)/4-1; i++ {
arr[i] = int(data[i*4])*256 + int(data[i*4+1])
}
return arr
}