forked from hootrhino/rulex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modbus_rtu_test.go
107 lines (94 loc) · 2.1 KB
/
modbus_rtu_test.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package test
import (
"log"
"os"
"testing"
"time"
"github.com/i4de/rulex/glogger"
modbus "github.com/wwhai/gomodbus"
)
var keys = [8]uint16{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}
/*
*
* 继电器测试
*
*/
func TestRTU_relay(t *testing.T) {
handler := modbus.NewRTUClientHandler("COM4")
handler.BaudRate = 9600
handler.DataBits = 8
handler.Parity = "N"
handler.StopBits = 1
handler.SlaveId = 1
handler.Logger = log.New(os.Stdout, "rtu: ", log.LstdFlags)
err := handler.Connect()
if err != nil {
t.Fatal(err)
}
defer handler.Close()
client := modbus.NewClient(handler)
for _, key := range keys {
t.Log("------------------> ", key)
for i := 0; i < 20; i++ {
client.WriteSingleCoil(key, 0xFF00)
st1, _ := client.ReadCoils(key, 1)
t.Log("ReadCoils==> ", st1)
time.Sleep(500 * time.Microsecond)
client.WriteSingleCoil(key, 0)
st2, _ := client.ReadCoils(key, 1)
t.Log("ReadCoils==> ", st2)
time.Sleep(500 * time.Microsecond)
}
}
}
/*
*
* 温湿度传感器测试
*
*/
func TestRTU485_THer_Usage(t *testing.T) {
handler := modbus.NewRTUClientHandler("COM6")
handler.BaudRate = 4800
handler.DataBits = 8
handler.Parity = "N"
handler.StopBits = 1
handler.SlaveId = 1
handler.Logger = log.New(os.Stdout, "rtu: ", log.LstdFlags)
err := handler.Connect()
if err != nil {
t.Fatal(err)
}
defer handler.Close()
client := modbus.NewClient(handler)
results, err := client.ReadHoldingRegisters(0x00, 2)
if err != nil {
glogger.GLogger.Fatal(err)
}
glogger.GLogger.Println(results)
}
/*
*
* 空调面板测试
*
*/
func TestRTU485_Air_Usage(t *testing.T) {
handler := modbus.NewRTUClientHandler("COM4")
handler.BaudRate = 4800
handler.DataBits = 8
handler.Parity = "O"
handler.StopBits = 1
handler.SlaveId = 1
handler.Logger = log.New(os.Stdout, "rtu: ", log.LstdFlags)
err := handler.Connect()
if err != nil {
t.Fatal(err)
}
defer handler.Close()
client := modbus.NewClient(handler)
results, err := client.ReadHoldingRegisters(0x09, 1)
glogger.GLogger.Println(results)
if err != nil {
glogger.GLogger.Fatal(err)
}
glogger.GLogger.Println(results)
}