-
Notifications
You must be signed in to change notification settings - Fork 1
/
external_test.go
63 lines (56 loc) · 1.98 KB
/
external_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
package modbus_test
import (
"context"
"github.com/aldas/go-modbus-client"
"github.com/aldas/go-modbus-client/modbustest"
"github.com/aldas/go-modbus-client/packet"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestReadCoilsRequestTCP(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
addr := startServer(ctx, t)
//addr := "localhost:5020"
client := modbus.NewTCPClient()
if err := client.Connect(context.Background(), addr); err != nil {
return
}
defer client.Close()
req, err := packet.NewReadCoilsRequestTCP(0, 10, 9)
//req, err := packet.NewReadDiscreteInputsRequestTCP(0, 10, 9)
//req, err := packet.NewReadHoldingRegistersRequestTCP(0, 10, 9)
//req, err := packet.NewReadInputRegistersRequestTCP(0, 10, 9)
//req, err := packet.NewWriteSingleCoilRequestTCP(0, 10, true)
//req, err := packet.NewWriteSingleRegisterRequestTCP(0, 10, []byte{0xCA, 0xFE})
//req, err := packet.NewWriteMultipleCoilsRequestTCP(0, 10, []bool{true, false, true})
//req, err := packet.NewWriteMultipleRegistersRequestTCP(0, 10, []byte{0xCA, 0xFE, 0xBA, 0xBE})
assert.NoError(t, err)
clientCtx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
resp, err := client.Do(clientCtx, req)
assert.NoError(t, err)
assert.Equal(t, packet.FunctionReadHoldingRegisters, resp.FunctionCode())
}
func startServer(ctx context.Context, t *testing.T) string {
handler := func(received []byte, bytesRead int) (response []byte, closeConnection bool) {
if bytesRead == 0 {
return nil, false
}
resp := packet.ReadHoldingRegistersResponseTCP{
MBAPHeader: packet.MBAPHeader{TransactionID: 123, ProtocolID: 0},
ReadHoldingRegistersResponse: packet.ReadHoldingRegistersResponse{
UnitID: 0,
RegisterByteLen: 2,
Data: []byte{0xca, 0xfe},
},
}
return resp.Bytes(), true
}
addr, err := modbustest.RunServerOnRandomPort(ctx, handler)
if err != nil {
t.Fatal(err)
}
return addr
}