-
Notifications
You must be signed in to change notification settings - Fork 39
/
test_serial.lua
223 lines (181 loc) · 10.1 KB
/
test_serial.lua
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
--
-- lua-periphery by vsergeev
-- https://github.com/vsergeev/lua-periphery
-- License: MIT
--
require('test')
local periphery = require('periphery')
local Serial = periphery.Serial
--------------------------------------------------------------------------------
local device = nil
--------------------------------------------------------------------------------
function test_arguments()
local serial = nil
ptest()
-- Invalid data bits
passert_periphery_error("invalid databits", function () serial = Serial{["device"]=device, baudrate=115200, databits=4} end, "SERIAL_ERROR_ARG")
passert_periphery_error("invalid databits", function () serial = Serial{["device"]=device, baudrate=115200, databits=9} end, "SERIAL_ERROR_ARG")
-- Invalid parity
passert_periphery_error("invalid parity", function () serial = Serial{["device"]=device, baudrate=115200, databits=8, parity="blah"} end, "SERIAL_ERROR_ARG")
-- Invalid stopbits
passert_periphery_error("invalid stopbits", function () serial = Serial{["device"]=device, baudrate=115200, databits=8, stopbits=0} end, "SERIAL_ERROR_ARG")
passert_periphery_error("invalid stopbits", function () serial = Serial{["device"]=device, baudrate=115200, databits=8, stopbits=3} end, "SERIAL_ERROR_ARG")
-- Everything else is fair game, although termios might not like it.
end
function test_open_config_close()
local serial = nil
ptest()
passert_periphery_success("open serial", function () serial = Serial(device, 115200) end)
-- Confirm default settings
passert("fd > 0", serial.fd > 0)
passert("baudrate is 115200", serial.baudrate == 115200)
passert("databits is 8", serial.databits == 8)
passert("parity is none", serial.parity == "none")
passert("stopbits is 1", serial.stopbits == 1)
passert("xonxoff is false", serial.xonxoff == false)
passert("rtscts is false", serial.rtscts == false)
passert("vmin is 0", serial.vmin == 0)
passert("vtime is 0", serial.vtime == 0)
io.write(string.format("serial: %s\n", serial:__tostring()))
-- Change some stuff around
passert_periphery_success("set baudrate to 4800", function () serial.baudrate = 4800 end)
passert("baudrate is 4800", serial.baudrate == 4800)
passert_periphery_success("set baudrate to 9600", function () serial.baudrate = 9600 end)
passert("baudrate is 9600", serial.baudrate == 9600)
passert_periphery_success("set databits to 7", function () serial.databits = 7 end)
passert("databits is 7", serial.databits == 7)
passert_periphery_success("set parity to odd", function () serial.parity = "odd" end)
passert("parity is odd", serial.parity == "odd")
passert_periphery_success("set stopbits to 2", function () serial.stopbits = 2 end)
passert("stopbits is 2", serial.stopbits == 2)
passert_periphery_success("set xonxoff to true", function () serial.xonxoff = true end)
passert("xonxoff is true", serial.xonxoff == true)
--[[ -- Test serial port may not support rtscts
passert_periphery_success("set rtscts to true", function () serial.rtscts = true end)
print(serial.rtscts)
passert("rtscts is true", serial.rtscts == true)
]]--
passert_periphery_success("set vmin to 50", function () serial.vmin = 50 end)
passert("vmin is 50", serial.vmin == 50)
passert_periphery_success("set vtime to 15.3", function () serial.vtime = 15.3 end)
passert("vtime is 15.3", math.abs(serial.vtime - 15.3) < 0.1)
passert_periphery_success("close serial", function () serial:close() end)
end
function test_loopback()
local serial = nil
local lorem_ipsum = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
local lorem_hugesum = nil
local buf = nil
local ret = nil
ptest()
passert_periphery_success("open serial", function () serial = Serial(device, 115200) end)
-- Test write/flush/read
passert("write lorem ipsum", serial:write(lorem_ipsum) == #lorem_ipsum)
passert_periphery_success("flush serial", function () serial:flush() end)
passert_periphery_success("read lorem ipsum", function () buf = serial:read(#lorem_ipsum) end)
passert("compare write/read lorem ipsum", buf == lorem_ipsum)
-- Test poll/write/flush/poll/input waiting/read
passert("poll timed out", serial:poll(500) == false)
passert("write lorem ipsum", serial:write(lorem_ipsum) == #lorem_ipsum)
passert_periphery_success("flush serial", function () serial:flush() end)
passert("poll succeeded", serial:poll(500) == true)
periphery.sleep_ms(500)
passert("input waiting is lorem ipsum size", serial:input_waiting() == #lorem_ipsum)
passert_periphery_success("read lorem ipsum", function () buf = serial:read(#lorem_ipsum) end)
passert("compare write/read lorem ipsum", buf == lorem_ipsum)
-- Test non-blocking poll
passert("non-blocking poll", serial:poll(0) == false)
-- Test a very large read-write (likely to exceed internal buffer size (~4096))
lorem_hugesum = string.rep("\xaa", 4096*3)
passert("write lorem hugesum", serial:write(lorem_hugesum) == #lorem_hugesum)
passert_periphery_success("flush", function () serial:flush() end)
passert_periphery_success("read lorem hugesum", function () buf = serial:read(#lorem_hugesum) end)
passert("compare write/read lorem hugesum", buf == lorem_hugesum)
-- Test read timeout
local tic = os.time()
passert("read timed out", serial:read(4096*3, 2000) == "")
local toc = os.time()
passert("time elapsed", (toc-tic) > 1)
-- Test non-blocking read
tic = os.time()
passert("read non-blocking", serial:read(4096*3, 0) == "")
toc = os.time()
-- Assuming we weren't context switched out for a second and weren't on a
-- thin time boundary ;)
passert("almost no time elapsed", (toc-tic) == 0)
-- Test blocking read with vmin=5 termios timeout
passert_periphery_success("set vmin to 5", function () serial.vmin = 5 end)
-- Write 5, read back 5 (vmin)
passert("write 5 bytes of lorem ipsum", serial:write(lorem_ipsum:sub(1, 5)) == 5)
passert_periphery_success("flush", function () serial:flush() end)
passert_periphery_success("read blocking with termios timeout", function () buf = serial:read(#lorem_ipsum) end)
passert("compare write/read partial lorem ipsum", buf == lorem_ipsum:sub(1, 5))
-- Test blocking read with vmin=5, vtime=2 termios timeout
passert_periphery_success("set vtime to 2", function () serial.vtime = 2 end)
-- Write 3, read back 3 (< vmin, but > vtime interbyte timeout)
passert("write 5 bytes of lorem ipsum", serial:write(lorem_ipsum:sub(1, 3)) == 3)
passert_periphery_success("flush", function () serial:flush() end)
local tic = os.time()
passert_periphery_success("read blocking with termios timeout", function () buf = serial:read(#lorem_ipsum) end)
local toc = os.time()
passert("compare write/read partial lorem ipsum", buf == lorem_ipsum:sub(1, 3))
passert("time elapsed", (toc-tic) > 1)
passert_periphery_success("close serial", function () serial:close() end)
end
function test_interactive()
local serial = nil
local buf = "Hello World!"
ptest()
passert_periphery_success("open serial", function () serial = Serial(device, 4800) end)
print("Starting interactive test. Get out your logic analyzer, buddy!")
print("Press enter to continue...")
io.read()
-- Check tostring
io.write(string.format("Serial description: %s\n", serial:__tostring()))
print("Serial description looks OK? y/n")
passert("interactive success", io.read() == "y")
print("Press enter to start transfer...")
io.read()
passert("serial write", serial:write(buf) == #buf)
print("Serial transfer baudrate 4800, 8n1 occurred? y/n")
passert("interactive success", io.read() == "y")
passert_periphery_success("set baudrate to 9600", function () serial.baudrate = 9600 end)
print("Press enter to start transfer...")
io.read()
passert("serial write", serial:write(buf) == #buf)
print("Serial transfer baudrate 9600, 8n1 occurred? y/n")
passert("interactive success", io.read() == "y")
passert_periphery_success("set baudrate to 115200", function () serial.baudrate = 115200 end)
print("Press enter to start transfer...")
io.read()
passert("serial write", serial:write(buf) == #buf)
print("Serial transfer baudrate 115200, 8n1 occurred? y/n")
passert("interactive success", io.read() == "y")
passert_periphery_success("close serial", function () serial:close() end)
end
if #arg < 1 then
io.stderr:write(string.format("Usage: lua %s <serial port device>\n\n", arg[0]))
io.stderr:write("[1/4] Arguments test: No requirements.\n")
io.stderr:write("[2/4] Open/close test: Serial port device should be real.\n")
io.stderr:write("[3/4] Loopback test: Serial TX and RX should be connected with a wire.\n")
io.stderr:write("[4/4] Interactive test: Serial TX should be observed with an oscilloscope or logic analyzer.\n\n")
io.stderr:write("Hint: for Raspberry Pi 3, enable UART0 with:\n")
io.stderr:write(" $ echo \"dtoverlay=pi3-disable-bt\" | sudo tee -a /boot/config.txt\n")
io.stderr:write(" $ sudo systemctl disable hciuart\n")
io.stderr:write(" $ sudo reboot\n")
io.stderr:write(" (Note that this will disable Bluetooth)\n")
io.stderr:write("Use pins UART0 TXD (header pin 8) and UART0 RXD (header pin 10),\n")
io.stderr:write("connect a loopback between TXD and RXD, and run this test with:\n")
io.stderr:write(string.format(" lua %s /dev/ttyAMA0\n\n", arg[0]))
os.exit(1)
end
device = arg[1]
test_arguments()
pokay("Arguments test passed.")
test_open_config_close()
pokay("Open/close test passed.")
test_loopback()
pokay("Loopback test passed.")
test_interactive()
pokay("Interactive test passed.")
pokay("All tests passed!")