-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
54 lines (47 loc) · 1.05 KB
/
base.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
package _examples
import (
"github.com/shiyuecamus/gs7"
"github.com/shiyuecamus/gs7/common"
"github.com/shiyuecamus/gs7/logging"
)
func main() {
const (
host = "192.168.0.1"
port = 102
rack = 0
slot = 1
)
logger := logging.GetDefaultLogger()
c := gs7.NewClientBuilder().
PlcType(common.S1500).
Host(host).
Port(port).
Rack(rack).
Slot(slot).
Build()
_, err := c.Connect().Wait()
if err != nil {
logger.Errorf("Failed to connect PLC, host: %s, port: %d, error: %s", host, port, err)
return
}
// read
wait, err := c.ReadParsed("DB1.X0.0").Wait()
if err != nil {
logger.Errorf("Failed to read bit, error: %s", err)
return
}
logger.Infof("Read bit success with value: %s", wait)
// write
err = c.WriteRaw("DB1.X0.0", gs7.Bit(true).ToBytes()).Wait()
if err != nil {
logger.Errorf("Failed to read bit, error: %s", err)
return
}
// check
wait, err = c.ReadParsed("DB1.X0.0").Wait()
if err != nil {
logger.Errorf("Failed to read bit, error: %s", err)
return
}
logger.Infof("Read bit success with value: %s", wait)
}