-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AP_Scripting: example showing how to use readstring method
a fairly common requirement for scripts
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--[[ | ||
example demonstrating how to read from a serial port into a lua string | ||
--]] | ||
|
||
local baud_rate = 57600 | ||
|
||
local port = assert(serial:find_serial(0), "Could not find Scripting Serial Port") | ||
|
||
port:begin(baud_rate) | ||
port:set_flow_control(0) | ||
|
||
--[[ | ||
get a string by concatenating bytes from the serial port, | ||
suitable for ArduPilot 4.4.x | ||
--]] | ||
local function get_string_44(n) | ||
local ret = "" | ||
for _ = 1, n do | ||
local b = port:read() | ||
ret = ret .. string.char(b) | ||
end | ||
return ret | ||
end | ||
|
||
--[[ | ||
get a string directly uisng readstring | ||
suitable for ArduPilot 4.5.x and later | ||
--]] | ||
local function get_string_45(n) | ||
return port:readstring(n) | ||
end | ||
|
||
function update() | ||
-- test using 4.5 method | ||
local n = port:available():toint() | ||
if n > 0 then | ||
local str = get_string_45(n) | ||
gcs:send_text(0, string.format("Received: '%s'", str)) | ||
end | ||
|
||
-- test using 4.4 method (just so we don't have an unused function in lua check) | ||
n = port:available():toint() | ||
if n > 0 then | ||
local str = get_string_44(n) | ||
gcs:send_text(0, string.format("Received: '%s'", str)) | ||
end | ||
return update, 100 | ||
end | ||
|
||
return update, 100 |