-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
48 lines (43 loc) · 958 Bytes
/
main.go
File metadata and controls
48 lines (43 loc) · 958 Bytes
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
// 03_bit_operations: ビットデバイス(M, X, Y)の読み書きサンプル。
package main
import (
"fmt"
mc "github.com/moge800/gomcprotocol"
)
func main() {
c, err := mc.New3EClient("192.168.0.1", 5007, mc.ModeBinary)
if err != nil {
panic(err)
}
if err := c.Connect(); err != nil {
panic(err)
}
defer c.Close()
// X0 から 8 点読み取り(入力リレー)
inputs, err := c.ReadBits("X", 0, 8)
if err != nil {
panic(err)
}
fmt.Print("X0-X7: ")
for _, b := range inputs {
if b {
fmt.Print("1 ")
} else {
fmt.Print("0 ")
}
}
fmt.Println()
// M0 から 4 点書き込み(内部リレー)
if err := c.WriteBits("M", 0, []bool{true, false, true, false}); err != nil {
panic(err)
}
fmt.Println("M0-M3 書き込み完了")
// 書き込んだ M0-M3 を読み返し
bits, err := c.ReadBits("M", 0, 4)
if err != nil {
panic(err)
}
for i, b := range bits {
fmt.Printf("M%d = %v\n", i, b)
}
}