-
Notifications
You must be signed in to change notification settings - Fork 4
/
mb-client.go
70 lines (59 loc) · 1.47 KB
/
mb-client.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
// Example of Modbus Master device (Client)
package main
import (
"flag"
"fmt"
"os"
"github.com/soldatov-s/go-modbus"
"github.com/soldatov-s/go-modbus/modbusgrpc"
)
var (
protocol = flag.String("protocol", "tcp", "type of protocol, tcp/udp")
port = flag.String("port", "502", "port number")
grpc_port = flag.String("grpc_port", "9000", "port number")
host = flag.String("host", "localhost", "hostname or host ip")
mbprotocol = flag.String("mbprotocol", "ModbusRTUviaTCP", "type of modbus protocol: ModbusTCP or ModbusRTUviaTCP")
)
func main() {
flag.Parse()
fmt.Println("Modbus client app!")
cl, err := modbus.NewClient(*port, *host,
modbus.StringToModbusTypeProtocol(*mbprotocol), 1)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
hold_regs, err := cl.ReadHoldingRegisters(0, 10)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Result ", hold_regs)
coils, err := cl.ReadCoilStatus(0, 10)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Result ", coils)
cl.Close()
// gRPC test
fmt.Println("gRPC client app!")
clRPC, err := modbusgrpc.NewgRPCClient(*grpc_port, *host)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
hold_regs_int32, err := clRPC.ReadHoldingRegisters(0, 10)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Result ", hold_regs_int32)
coils, err = clRPC.ReadCoilStatus(0, 10)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Result ", coils)
clRPC.Close()
}