This repository was archived by the owner on Dec 18, 2018. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathtest.lua
More file actions
342 lines (292 loc) · 8.05 KB
/
Copy pathtest.lua
File metadata and controls
342 lines (292 loc) · 8.05 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
port_name = "/dev/ttyUSB0"
platform = "linux"
-- port_name = "COM1"
-- platform = "win32"
rs232 = require("luars232")
function time_stamp()
t = os.date("*t")
return string.format("[%02s.%02s.%s %02s:%02s:%02s] ",
t.day, t.month, t.year,
t.hour, t.min, t.sec)
end
oldprint = print
function myprint(text)
text = time_stamp() .. text
oldprint(text)
file = io.open("test_lua_log.txt", "a+")
file:write(text .. "\n")
file:close()
end
print = myprint
function string.lines(str)
return string.gfind(str, "([^\n]*)\n")
end
function format_test(text, err)
local ret = "\n"
local line_number = 1
local _, _, line_err, line_str = string.find(err, ".*:(%d+):(.*)")
for line in string.lines(text) do
if line_number == tonumber(line_err) then
ret = ret .. string.format("--> %d. %s (%s)\n", line_number, line, line_str)
else
ret = ret .. string.format("% 8d. %s\n", line_number, line)
end
line_number = line_number + 1
end
return ret
end
function test(text)
local chunk, err = loadstring(text)
if chunk == nil then
print(string.format([[
---------------------------------------------------------------
%s
loadstring error: '%s'
---------------------------------------------------------------
]], format_test(text, err), err))
return false
end
local ret, msg = pcall(chunk)
if ret == false then
print(string.format([[
---------------------------------------------------------------
%s
pcall error: '%s'
---------------------------------------------------------------
]], format_test(text, msg), msg))
return false
end
return true
end
baud_rates = {
"rs232.RS232_BAUD_9600",
"rs232.RS232_BAUD_19200",
"rs232.RS232_BAUD_38400",
"rs232.RS232_BAUD_57600",
"rs232.RS232_BAUD_115200",
}
-- It's such a crap, Windows seems to not like 5 data
-- bits. If you make this test to work with 5 data bits on
-- Windows, I'll buy you a beer or two...
if platform == "win32" then
data_bits = {
"rs232.RS232_DATA_6",
"rs232.RS232_DATA_7",
"rs232.RS232_DATA_8",
}
else
data_bits = {
"rs232.RS232_DATA_5",
"rs232.RS232_DATA_6",
"rs232.RS232_DATA_7",
"rs232.RS232_DATA_8",
}
end
stop_bits = {
"rs232.RS232_STOP_1",
"rs232.RS232_STOP_2",
}
parity_bits = {
"rs232.RS232_PARITY_NONE",
"rs232.RS232_PARITY_ODD",
"rs232.RS232_PARITY_EVEN",
}
flow_bits = {
"rs232.RS232_FLOW_OFF",
"rs232.RS232_FLOW_HW",
"rs232.RS232_FLOW_XON_XOFF",
}
dtr_bits = {
"rs232.RS232_DTR_ON",
"rs232.RS232_DTR_OFF",
}
rts_bits = {
"rs232.RS232_RTS_ON",
"rs232.RS232_RTS_OFF",
}
errors = {
"rs232.RS232_ERR_NOERROR",
"rs232.RS232_ERR_UNKNOWN",
"rs232.RS232_ERR_OPEN",
"rs232.RS232_ERR_CLOSE",
"rs232.RS232_ERR_FLUSH",
"rs232.RS232_ERR_CONFIG",
"rs232.RS232_ERR_READ",
"rs232.RS232_ERR_WRITE",
"rs232.RS232_ERR_SELECT",
"rs232.RS232_ERR_TIMEOUT",
"rs232.RS232_ERR_IOCTL",
"rs232.RS232_ERR_PORT_CLOSED",
}
assert(
test(string.format([[
local e, p = rs232.open("%s")
assert(e == rs232.RS232_ERR_NOERROR)
assert(p ~= nil)
assert(p:close() == rs232.RS232_ERR_NOERROR)
]], port_name))
)
assert(
test([[
local e, p = rs232.open("/dev/hell")
assert(e == rs232.RS232_ERR_OPEN)
assert(p == nil)
]])
)
for _, baud in pairs(baud_rates) do
for _, data in pairs(data_bits) do
for _, parity in pairs(parity_bits) do
for _, stop in pairs(stop_bits) do
for _, flow in pairs(flow_bits) do
for _, dtr in pairs(dtr_bits) do
for _, rts in pairs(rts_bits) do
assert(
test(
string.format([[
local e, p = rs232.open("%s")
assert(e == rs232.RS232_ERR_NOERROR)
assert(p ~= nil)
local bret = rs232.error_tostring(p:set_baud_rate(%s))
local dret = rs232.error_tostring(p:set_data_bits(%s))
local pret = rs232.error_tostring(p:set_parity(%s))
local sret = rs232.error_tostring(p:set_stop_bits(%s))
local fret = rs232.error_tostring(p:set_flow_control(%s))
local dtr_ret = rs232.error_tostring(p:set_dtr(%s))
local rts_ret = rs232.error_tostring(p:set_rts(%s))
local text = tostring(p)
assert(text ~= nil)
local errors = ""
if bret ~= "no error" then
errors = "set baud rate,"
end
if dret ~= "no error" then
errors = errors .. "set data bits,"
end
if pret ~= "no error" then
errors = errors .. "set parity,"
end
if sret ~= "no error" then
errors = errors .. "set stop bits,"
end
if fret ~= "no error" then
errors = errors .. "set flow control"
end
if dtr_ret ~= "no error" then
errors = errors .. "set dtr"
end
if rts_ret ~= "no error" then
errors = errors .. "set rts"
end
if (string.len(errors) > 0) then
print(string.format(" [!] ERROR: %%s (failed: %%s)", text, errors))
else
print(string.format(" [!] OK: %%s", text))
end
assert(p:close() == rs232.RS232_ERR_NOERROR)
]], port_name, baud, data, parity, stop, flow, dtr, rts)
)
)
end
end
end
end
end
end
end
for _, baud in pairs(baud_rates) do
for _, data in pairs(data_bits) do
for _, parity in pairs(parity_bits) do
for _, stop in pairs(stop_bits) do
for _, flow in pairs(flow_bits) do
for _, dtr in pairs(dtr_bits) do
for _, rts in pairs(rts_bits) do
assert(
test(
string.format([[
local timeout = 10
local e, p = rs232.open("%s")
assert(e == rs232.RS232_ERR_NOERROR)
assert(p ~= nil)
assert(p:set_baud_rate(%s) == rs232.RS232_ERR_NOERROR)
assert(p:set_data_bits(%s) == rs232.RS232_ERR_NOERROR)
assert(p:set_parity(%s) == rs232.RS232_ERR_NOERROR)
assert(p:set_stop_bits(%s) == rs232.RS232_ERR_NOERROR)
assert(p:set_flow_control(%s) == rs232.RS232_ERR_NOERROR)
assert(p:set_dtr(%s) == rs232.RS232_ERR_NOERROR)
assert(p:set_rts(%s) == rs232.RS232_ERR_NOERROR)
local baud = p:baud_rate()
local str_baud1 = p:baud_rate_tostring()
local str_baud2 = p:baud_rate_tostring(baud)
assert(str_baud1 == str_baud2)
local bits = p:data_bits()
local str_bits1 = p:data_bits_tostring()
local str_bits2 = p:data_bits_tostring(bits)
assert(str_bits1 == str_bits2)
local parity = p:parity()
local str_parity1 = p:parity_tostring()
local str_parity2 = p:parity_tostring(parity)
assert(str_parity1 == str_parity2)
local stop = p:stop_bits()
local str_stop1 = p:stop_bits_tostring()
local str_stop2 = p:stop_bits_tostring(stop)
assert(str_stop1 == str_stop2)
local flow = p:flow_control()
local str_flow1 = p:flow_control_tostring()
local str_flow2 = p:flow_control_tostring(flow)
assert(str_flow1 == str_flow2)
local dtr = p:dtr()
local str_dtr1 = p:dtr_tostring()
local str_dtr2 = p:dtr_tostring(dtr)
assert(str_dtr1 == str_dtr2)
local rts = p:rts()
local str_rts1 = p:rts_tostring()
local str_rts2 = p:rts_tostring(rts)
assert(str_rts1 == str_rts2)
local e = p:flush()
assert(e == rs232.RS232_ERR_NOERROR)
e, d, l = p:read(1, timeout)
assert(e == rs232.RS232_ERR_TIMEOUT)
assert(d == nil)
assert(l == 0)
-- not implemented yet...
if platform ~= "win32" then
local forced = 1
e, d, l = p:read(1, timeout, forced)
assert(e == rs232.RS232_ERR_TIMEOUT)
assert(d == nil)
assert(l == 0)
end
local forced = 0
e, d, l = p:read(1, timeout, forced)
assert(e == rs232.RS232_ERR_TIMEOUT)
assert(d == nil)
assert(l == 0)
e, l = p:write("ynezz")
assert(e == rs232.RS232_ERR_NOERROR)
-- althought the write is successful it returns 0 bytes written
-- in some baud/data/stop/flow combinations...
if platform ~= "win32" then
assert(l == 5)
end
e, l = p:write("ynezz", timeout)
assert(e == rs232.RS232_ERR_NOERROR)
-- althought the write is successful it returns 0 bytes written
-- in some baud/data/stop/flow combinations...
if platform ~= "win32" then
assert(l == 5)
end
local text = tostring(p)
assert(text ~= nil)
print("tostring(p): " .. text)
assert(p:close() == rs232.RS232_ERR_NOERROR)
]], port_name, baud, data, parity, stop, flow, dtr, rts)
)
)
end
end
end
end
end
end
end
print("[*] All tests passed succesfuly!")