-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·73 lines (59 loc) · 1.71 KB
/
test.py
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
#!/usr/bin/env python3
# assumes pyttymux is running with a loopback on serial, and three mux channels, 'a', 'b', and 'c'
import sys, os
import time
import serial
if __name__ == "__main__":
TIMEOUT=0.1
print("Opening ports")
a = serial.Serial(port='a', timeout=TIMEOUT)
b = serial.Serial(port='b', timeout=TIMEOUT)
c = serial.Serial(port='c', timeout=TIMEOUT)
print("Basic communication")
a.write(b"a")
assert a.read() == b"a"
assert b.read() == b""
assert c.read() == b""
b.write(b"b")
assert a.read() == b""
assert b.read() == b"b"
assert c.read() == b""
c.write(b"c")
assert a.read() == b""
assert b.read() == b""
assert c.read() == b"c"
print("Interleaved communication")
a.write(b"AAA")
b.write(b"BBB")
c.write(b"CCC")
assert c.read(10) == b"CCC"
assert b.read(10) == b"BBB"
time.sleep(5)
assert a.read(10) == b"AAA"
print("Escaped data")
a.write(b"ff\xffff")
assert a.read(100) == b"ff\xffff"
print("Large buffer")
a.timeout = b.timeout = c.timeout = 10
a.write(os.urandom(1024))
b.write(b"xyz")
assert b.read(3) == b"xyz"
assert len(a.read(2048)) == 1024
print("Blocking port")
for i in range(50):
a.write(os.urandom(1024))
b.write(os.urandom(4))
assert len(b.read(4)) == 4
print("Single channel throughput test")
start_time = time.time()
c.timeout = 0
rx = tx = 0
for i in range(400):
tx += c.write(os.urandom(256))
rx += len(c.read(1024))
c.timeout = 0.1
while rx < tx:
rx += len(c.read(1024))
duration = time.time() - start_time
print("{:.1f} bytes/sec".format(400*256 / duration))
print("Done")