All pages
Powered by GitBook
1 of 6

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

io Library

The io library has been simplified and only a subset of functions and their functionality is available. What follows is a complete reference of io functions that are available to EdgeTX scripts

Available functions:

  • io.open()

  • io.close()

  • io.read()

  • io.write()

  • io.seek()

Examples

Read the whole file

Append data to file

-- this is a stand-alone script

local function run(event)
  print("lua io.read test")         -- print() statements are visible in Debug output window
  local f = io.open("foo.bar", "r")
  while true do
    local data = io.read(f, 10)     -- read up to 10 characters (newline char also counts!)
    if #data == 0 then break end    -- we get zero length string back when we reach end of the file
    print("data: "..data)
    end
  io.close(f)
  return 1
end

return {  run=run }
-- this is a stand-alone script

local function run(event)
  print("lua io.write test")
  local f = io.open("foo.bar", "a")        -- open file in append mode
  io.write(f, "first line\r\nsecond line\r\n")
  io.write(f, 4, "\r\n", 35.6778, "\r\n")  -- one can write multiple items at the same time
  local foo = -4.45
  io.write(f, foo, "\r\n")
  io.close(f)
  return 1    -- this will end the script execution
end

return { run=run }

io.seek()

The io.seek() function is used to move the current read/write position.

Notice: other read standard seek bases (like "cur", "end") are not supported.

Parameters

  • file object a file object that was returned by the io.open() function.

  • offset position the read/write file pointer at the specified offset from the beginning of the file. If specified offset is bigger than the file size, then the pointer is moved to the end of the file.

Return value

  • 0 success

  • <number> any other value means failure.

io.open()

The io.open() function is used to open the file on SD card for subsequent reading or writing. After the script is done with the file manipulation io.close() function should be used.

Parameters

  • filename full path to the file starting from the SD card root directory. This function can't create a new file in non-existing directory.

mode supported mode strings are:

  • "r" read access. File must exist beforehand. The read pointer is located at the beginning of file. This is the default mode if is omitted.

  • "w" write access. File is opened or created (if it didn't exist) and truncated (all existing file contents are lost).

  • "a" write access. File is opened or created (if it didn't exist) and write pointer is located at the end of the file. The existing file contents are preserved.

Return value

  • <file object> if file was successfully opened.

  • nil if file could not be opened.

io.write()

The io.write() function is used to write data to the file on SD card.

Parameters

  • file object a file object that was returned by the io.open() function. The file must be opened in write or append mode.

data any Lua type that can be converted into string. If more than one data parameter is used their contents are written to the file by one in the same order as they are specified.

Return value

  • <file object> if data was successfully opened.

  • nil, <error string>, <error number> if the data can't be written.

io.read()

The io.read() function is used to read data from the file on SD card.

Notice: other read commands (like "all", etc..) are *not supported.

Parameters

  • file object a file object that was returned by the io.open() function. The file must be opened in read mode.

  • length number of characters/bytes to read. The number of actual read/returned characters can be less if the file end is reached.

Return value

  • <string> a string with a length equal or less than

  • "" a zero length string if the end of file was reached

io.close()

The io.close() function is used to close open file.

Parameters

  • file object a file object that was returned by the io.open() function.

Return value

  • none