Chapter 5: Scripting
Complete example of a Lua script 53
55555
Scripting
The mini-C response includes an echo of the command itself as well as a
trailing prompt for the next command. When reading from the mini-C
device there is no built-in mechanism to separate the command and the
prompt from the actual response.
Furthermore there is no guarantee that the full response is received in a
single read. The script must therefore be able to combine the response from
a number of reads until the succeeding prompt has been received.
Template for basic mini-C communication
The script below includes these functions and may serve as a template for
basic mini-C communication.
function read_timeout(stream, timeout_ms)
local res = ""
repeat
local rd
rd = stream:read(16)
if rd == nil then
timeout_ms = timeout_ms - 100
else
res = res .. rd
end
until timeout_ms <= 0
return res
end
function make_pattern(pattern)
-- Escape all "magic" characters to make it usable as search pattern
return string.gsub(pattern, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
end
function detect_prompt(stream)
1
local last_prompt = "unknown prompt"
local eq_count = 0
local resp = ""
local retries = 6
repeat
stream:write("\r")
stream:flush()
resp = read_timeout(stream, 500)
if string.len(resp) > 1 then
local new_prompt
local i
1. Detecting the mini-C prompt