forked from tarm/serial
-
Notifications
You must be signed in to change notification settings - Fork 2
/
serial_test.go
228 lines (196 loc) · 4.76 KB
/
serial_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//go:build linux
// +build linux
package goserial_test
import (
"context"
"io"
"os"
"os/exec"
"runtime"
"testing"
"time"
serial "github.com/FObersteiner/goserial"
)
func TestConnection(t *testing.T) {
port0 := os.Getenv("PORT0")
port1 := os.Getenv("PORT1")
if port0 == "" || port1 == "" {
t.Skip("skipping test because PORT0 and/or PORT1 environment variable is not set")
}
c0 := &serial.Config{Name: port0, Baud: 115200, ReadTimeout: time.Duration(time.Second)}
c1 := &serial.Config{Name: port1, Baud: 115200, ReadTimeout: time.Duration(time.Second)}
s1, err := serial.OpenPort(c0)
if err != nil {
t.Fatal(err)
}
defer s1.Close()
s2, err := serial.OpenPort(c1)
if err != nil {
t.Fatal(err)
}
defer s2.Close()
readChan := make(chan int, 1)
errChan := make(chan error, 1)
go func() {
buf := make([]byte, 128)
var readCount int
for {
n, err := s2.Read(buf)
if err != nil {
errChan <- err
close(readChan)
return
}
readCount++
t.Logf("read %v %v bytes: % 02x %s", readCount, n, buf[:n], buf[:n])
select {
case <-readChan:
readChan <- readCount
close(readChan)
default:
}
}
}()
if _, err = s1.Write([]byte("hello")); err != nil {
t.Fatal(err)
}
if _, err = s1.Write([]byte(" ")); err != nil {
t.Fatal(err)
}
// setting a time delay here to simulate a subsequent write,
// causing a second read
time.Sleep(time.Millisecond * 10)
if _, err = s1.Write([]byte("world")); err != nil {
t.Fatal(err)
}
readChan <- 0
_, err = s1.Write([]byte(" ")) // We could be blocked by s2.Read(buf) ...
if err != nil {
t.Fatalf("error on write to serial port 1; %v", err)
}
// TODO : readChan might be closed; make use of this fact ?
c := <-readChan
exp := 5
if c >= exp {
t.Fatalf("expected less than %v read, got %v", exp, c)
}
err = <-errChan
if err != io.EOF {
t.Fatal(err)
}
}
func TestConnectionLinux(t *testing.T) {
if runtime.GOOS != "linux" {
// there is a port of socat to Windows, but it seems to rely on cygwin
// not sure about Mac
t.Skip("skipping socat test because not on Linux")
}
_, err := exec.LookPath("socat")
if err != nil {
t.Skip("skipping test because socat was not found in PATH")
}
// timeout is a fallback here, if something fails
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
wd, _ := os.Getwd()
cmd := "socat"
args := []string{"pty,raw,echo=0,link=/tmp/pty0", "pty,raw,echo=0,link=/tmp/pty1"}
p, err := startCmd(ctx, wd, cmd, args...)
if err != nil {
t.Fatal(err)
}
defer func() {
err := p.Signal(os.Interrupt)
if err != nil {
t.Fatal(err)
}
}()
// cmd.Start() / starting socat creates a race !
// (FO) on my machine, 10 ms is a safe time to wait; might be different
// on your machine.
time.Sleep(time.Millisecond * 10)
port0 := &serial.Config{
Name: "/tmp/pty0",
// Baud: 115200,
Baud: 100000,
ReadTimeout: time.Duration(time.Second),
Size: 8,
}
port1 := &serial.Config{
Name: "/tmp/pty1",
// Baud: 115200,
Baud: 100000,
ReadTimeout: time.Duration(time.Second),
Size: 8,
}
stream0, err := serial.OpenPort(port0)
if err != nil {
t.Fatal("could not setup connection", err)
}
stream1, err := serial.OpenPort(port1)
if err != nil {
t.Fatal("could not setup connection", err)
}
want := []byte("Hello, World!")
nIn, err := stream1.Write(want)
if err != nil {
t.Fatal(err)
}
// stream is buffered, so we can read in sequence:
buf := make([]byte, 1024)
nOut, err := stream0.Read(buf)
if err != nil {
t.Fatal(err)
}
if nOut != nIn {
t.Fatalf("sent %v bytes, got %v", nIn, nOut)
}
have := buf[:nOut]
if !testEq(have, want) {
t.Fatal("read data does not match written data")
}
// after flushing a serial interface, no bytes should be left:
_, err = stream1.Write(want)
if err != nil {
t.Fatal(err)
}
// this again is a data race; need to wait a bit after writing before
// we can flush the bytes...
time.Sleep(time.Millisecond * 10)
stream0.Flush()
nOut, err = stream0.Read(buf)
if err != nil && err != io.EOF {
t.Fatal(err)
}
if nOut != 0 {
t.Logf("expected zero bytes read after flush, got %v", nOut)
t.Fail()
}
cancel()
}
func TestFindSerial(t *testing.T) {
_, err := serial.FindSerial()
if err != nil {
t.Fatalf("error discovering serial ports; %v", err)
}
}
// --- HELPERS ------------------------------------------------------------------------
func startCmd(ctx context.Context, wd, cmd string, args ...string) (*os.Process, error) {
ecmd := exec.CommandContext(ctx, cmd, args...)
ecmd.Dir = wd
err := ecmd.Start()
if err != nil {
return nil, err
}
return ecmd.Process, nil
}
func testEq(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}