-
Notifications
You must be signed in to change notification settings - Fork 139
/
server_test.go
138 lines (121 loc) · 3.29 KB
/
server_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package mbserver
import (
"testing"
"time"
"github.com/goburrow/modbus"
)
func TestAduRegisterAndNumber(t *testing.T) {
var frame TCPFrame
SetDataWithRegisterAndNumber(&frame, 0, 64)
expect := []byte{0, 0, 0, 64}
got := frame.Data
if !isEqual(expect, got) {
t.Errorf("expected %v, got %v", expect, got)
}
}
func TestAduSetDataWithRegisterAndNumberAndValues(t *testing.T) {
var frame TCPFrame
SetDataWithRegisterAndNumberAndValues(&frame, 7, 2, []uint16{3, 4})
expect := []byte{0, 7, 0, 2, 4, 0, 3, 0, 4}
got := frame.Data
if !isEqual(expect, got) {
t.Errorf("expected %v, got %v", expect, got)
}
}
func TestUnsupportedFunction(t *testing.T) {
s := NewServer()
var frame TCPFrame
frame.Function = 255
var req Request
req.frame = &frame
response := s.handle(&req)
exception := GetException(response)
if exception != IllegalFunction {
t.Errorf("expected IllegalFunction (%d), got (%v)", IllegalFunction, exception)
}
}
func TestModbus(t *testing.T) {
// Server
s := NewServer()
err := s.ListenTCP("127.0.0.1:3333")
if err != nil {
t.Fatalf("failed to listen, got %v\n", err)
}
defer s.Close()
// Allow the server to start and to avoid a connection refused on the client
time.Sleep(1 * time.Millisecond)
// Client
handler := modbus.NewTCPClientHandler("127.0.0.1:3333")
// Connect manually so that multiple requests are handled in one connection session
err = handler.Connect()
if err != nil {
t.Errorf("failed to connect, got %v\n", err)
t.FailNow()
}
defer handler.Close()
client := modbus.NewClient(handler)
// Coils
results, err := client.WriteMultipleCoils(100, 9, []byte{255, 1})
if err != nil {
t.Errorf("expected nil, got %v\n", err)
t.FailNow()
}
results, err = client.ReadCoils(100, 16)
if err != nil {
t.Errorf("expected nil, got %v\n", err)
t.FailNow()
}
expect := []byte{255, 1}
got := results
if !isEqual(expect, got) {
t.Errorf("expected %v, got %v", expect, got)
}
// Discrete inputs
results, err = client.ReadDiscreteInputs(0, 64)
if err != nil {
t.Errorf("expected nil, got %v\n", err)
t.FailNow()
}
// test: 2017/05/14 21:09:53 modbus: sending 00 01 00 00 00 06 ff 02 00 00 00 40
// test: 2017/05/14 21:09:53 modbus: received 00 01 00 00 00 0b ff 02 08 00 00 00 00 00 00 00 00
expect = []byte{0, 0, 0, 0, 0, 0, 0, 0}
got = results
if !isEqual(expect, got) {
t.Errorf("expected %v, got %v", expect, got)
}
// Holding registers
results, err = client.WriteMultipleRegisters(1, 2, []byte{0, 3, 0, 4})
if err != nil {
t.Errorf("expected nil, got %v\n", err)
t.FailNow()
}
// received: 00 01 00 00 00 06 ff 10 00 01 00 02
expect = []byte{0, 2}
got = results
if !isEqual(expect, got) {
t.Errorf("expected %v, got %v", expect, got)
}
results, err = client.ReadHoldingRegisters(1, 2)
if err != nil {
t.Errorf("expected nil, got %v\n", err)
t.FailNow()
}
expect = []byte{0, 3, 0, 4}
got = results
if !isEqual(expect, got) {
t.Errorf("expected %v, got %v", expect, got)
}
// Input registers
s.InputRegisters[65530] = 1
s.InputRegisters[65535] = 65535
results, err = client.ReadInputRegisters(65530, 6)
if err != nil {
t.Errorf("expected nil, got %v\n", err)
t.FailNow()
}
expect = []byte{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255}
got = results
if !isEqual(expect, got) {
t.Errorf("expected %v, got %v", expect, got)
}
}