forked from simonvetter/modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtu_transport_test.go
165 lines (145 loc) · 3.58 KB
/
rtu_transport_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package modbus
import (
"testing"
"io"
"net"
"time"
)
func TestAssembleRTUFrame(t *testing.T) {
var rt *rtuTransport
var frame []byte
rt = &rtuTransport{}
frame = rt.assembleRTUFrame(&pdu{
unitId: 0x33,
functionCode: 0x11,
payload: []byte{0x22, 0x33, 0x44, 0x55},
})
// expect 1 byte of unit id, 1 byte of function code, 4 bytes of payload and
// 2 bytes of CRC
if len(frame) != 8 {
t.Errorf("expected 8 bytes, got %v", len(frame))
}
for i, b := range []byte{
0x33, 0x11, // unit id and function code
0x22, 0x33, // payload
0x44, 0x55, // payload
0xf0, 0x93, // CRC
} {
if frame[i] != b {
t.Errorf("expected 0x%02x at position %v, got 0x%02x", b, i, frame[i])
}
}
frame = rt.assembleRTUFrame(&pdu{
unitId: 0x31,
functionCode: 0x06,
payload: []byte{0x12, 0x34},
})
// expect 1 byte of unit if, 1 byte of function code, 2 bytes of payload and
// 2 bytes of CRC
if len(frame) != 6 {
t.Errorf("expected 6 bytes, got %v", len(frame))
}
for i, b := range []byte{
0x31, 0x06, // unit id and function code
0x12, 0x34, // payload
0xe3, 0xae, // CRC
} {
if frame[i] != b {
t.Errorf("expected 0x%02x at position %v, got 0x%02x", b, i, frame[i])
}
}
return
}
func TestRTUTransportReadRTUFrame(t *testing.T) {
var rt *rtuTransport
var p1, p2 net.Conn
var txchan chan []byte
var err error
var res *pdu
txchan = make(chan []byte, 2)
p1, p2 = net.Pipe()
go feedTestPipe(t, txchan, p1)
rt = newRTUTransport(p2, "", 9600, 10 * time.Millisecond)
// read a valid response (illegal data address)
txchan <- []byte{
0x31, 0x82, // unit id and response code
0x02, // exception code
0xc1, 0x6e, // CRC
}
res, err = rt.readRTUFrame()
if err != nil {
t.Errorf("readRTUFrame() should have succeeded, got %v", err)
}
if res.unitId != 0x31 {
t.Errorf("expected 0x31 as unit id, got 0x%02x", res.unitId)
}
if res.functionCode != 0x82 {
t.Errorf("expected 0x82 as function code, got 0x%02x", res.functionCode)
}
if len(res.payload) != 1 {
t.Errorf("expected a length of 1, got %v", len(res.payload))
}
if res.payload[0] != 0x02 {
t.Errorf("expected {0x02} as payload, got {0x%02x}",
res.payload[0])
}
// read a frame with a bad crc
txchan <- []byte{
0x30, 0x82, // unit id and response code
0x12, // exception code
0xc0, 0xa2, // CRC
}
res, err = rt.readRTUFrame()
if err != ErrBadCRC {
t.Errorf("readRTUFrame() should have returned ErrBadCrc, got %v", err)
}
// read a longer, valid response
txchan <- []byte{
0x31, 0x03, // unit id and response code
0x04, // length
0x11, 0x22, // register #1
0x33, 0x44, // register #2
0x7b, 0xc5, // CRC
}
res, err = rt.readRTUFrame()
if err != nil {
t.Errorf("readRTUFrame() should have succeeded, got %v", err)
}
if res.unitId != 0x31 {
t.Errorf("expected 0x31 as unit id, got 0x%02x", res.unitId)
}
if res.functionCode != 0x03 {
t.Errorf("expected 0x03 as function code, got 0x%02x", res.functionCode)
}
if len(res.payload) != 5 {
t.Errorf("expected a length of 5, got %v", len(res.payload))
}
for i, b := range []byte{
0x04,
0x11, 0x22,
0x33, 0x44,
} {
if res.payload[i] != b {
t.Errorf("expected 0x%02x at position %v, got 0x%02x",
b, i, res.payload[i])
}
}
p1.Close()
p2.Close()
return
}
func feedTestPipe(t *testing.T, in chan []byte, out io.WriteCloser) {
var err error
var txbuf []byte
for {
// grab a slice of bytes from the channel
txbuf = <-in
// write this slice to the pipe
_, err = out.Write(txbuf)
if err != nil {
t.Errorf("failed to write to test pipe: %v", err)
return
}
}
return
}