Skip to content

Commit

Permalink
AP_Scripting: add serial loopback test script
Browse files Browse the repository at this point in the history
Tests that data can flow both ways with one end using protocol 28
(Scripting) and the other using the serial device feature.
  • Loading branch information
tpwrules committed Jun 16, 2024
1 parent 01af8b1 commit 7a9f4be
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions libraries/AP_Scripting/tests/serial_loopback.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local ser_driver = serial:find_serial(0)
local ser_device = serial:get_device_port(1)

if ser_driver == nil or ser_device == nil then
error("bad config")
end

ser_driver:begin(115200) -- baud rate does not matter

function test_driver_to_device()
local msg_send = "hello device"
local num_sent = 0
for ci = 1,#msg_send do
num_sent = num_sent + ser_driver:write(msg_send:byte(ci, ci)):toint()
end
local msg_recv = ser_device:readstring(#msg_send)
if msg_send == msg_recv and num_sent == #msg_send then
gcs:send_text(6, "driver -> device good")
end
end

function test_device_to_driver()
local msg_send = "hello driver"
local num_sent = ser_device:writestring(msg_send)
local msg_recv = ser_driver:readstring(#msg_send)
if msg_send == msg_recv and num_sent == #msg_send then
gcs:send_text(6, "device -> driver good")
end
end

function update()
test_driver_to_device()
test_device_to_driver()
end

return update()

0 comments on commit 7a9f4be

Please sign in to comment.