forked from tbrandon/mbserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux_test.go
83 lines (73 loc) · 1.8 KB
/
linux_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
// +build linux
package mbserver
import (
"log"
"os/exec"
"testing"
"time"
"github.com/goburrow/modbus"
"github.com/goburrow/serial"
)
// The serial read and close has a known race condition.
// https://github.com/golang/go/issues/10001
func TestModbusRTU(t *testing.T) {
// Create a pair of virutal serial devices.
cmd := exec.Command("socat",
"pty,raw,echo=0,link=ttyFOO",
"pty,raw,echo=0,link=ttyBAR")
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
// Allow the virutal serial devices to be created.
time.Sleep(10 * time.Millisecond)
// Server
s := NewServer()
err = s.ListenRTU(&serial.Config{
Address: "ttyFOO",
BaudRate: 115200,
DataBits: 8,
StopBits: 1,
Parity: "N",
Timeout: 10 * time.Second})
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.NewRTUClientHandler("ttyBAR")
handler.BaudRate = 115200
handler.DataBits = 8
handler.Parity = "N"
handler.StopBits = 1
handler.SlaveId = 1
handler.Timeout = 5 * time.Second
// 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
_, 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)
}
}