Appendix G: Lua Scripts
X-600M Users Manual
More information about variable scope can be found in the Lua manual. One thing to note on the X-
600M, is that global variables declared in a script can be accessed by other scripts and expressions.
script, use the local keyword when declaring the variable. Functions created in a lua script are also
global. They can be called in other scripts and expressions. The three special tables
io
,
reg
, and
event
,
are global to all Lua scripts.
Also notice in this example the function calls to enabledDebug and print. On the X-600M, print functions
will output text to the debug console when it is enabled. The debug console is disabled by default. The
function call enableDebug will enable the debug console so that the print statements will function.
To run the example, paste it into a script and commit the settings. After running this example, open up
the debug console to view the output.
Reading and Writing Files
The X-600M Lua interpreter has limited support for reading and writing files. The standard Lua library
used for reading and writing files is the 'io' library. Since the 'io' table on the X-600M is used for
accessing I/O and registers, the standard 'io' library on the X-600M is called the 'file' library. The only
other difference between Lua's standard 'io' library and the X-600M's 'file' library is that the X-600M's
'file' library only allows file access to certain locations within the internal flash.
The following functions are available in the 'file' library: open, close, flush, input, lines, read, tmpfile, type,
and write. These functions work identical to those found in the standard 'io' library and can be looked up
in the Lua documentation.
There are three places files can be placed on the X-600M. First, any file opened without any path being
specified will be placed in the X-600M's internal flash.
fh = file.open(“filename.txt”, “a”);
The storage space for these files is shared between custom web pages and internal log files. If there is
no more available space in the internal flash, a call to open a file will fail and return NIL.
The second place where files can be accessed in LUA is an external USB drive. To read and write files
on the external USB drive, open the file using the path “/usb”.
fh = file.open(“/usb/filename.txt”, “a”);
The third place where files can be access in LUA is an internal ram drive on the X-600M. This location is
different than the other two in that files created on the ram drive will not be preserved through a power
loss. Files in the ram drive can be used for temporary storage, possibly as a convenient way to share
information between LUA and custom web pages. To read and write files on the ram drive, open the file
using the path “/ram”;
fh = file.open(“/usb/filename.txt”, “a”);
There are some things to be aware of when accessing files from LUA. First, always make sure to close
files that have been opened when they aren't needed anymore, and especially at the end of any LUA
scripts. Over time, leaving files open will use up resources on the X-600M that could cause the X-600M
to not be able to function correctly. Second, try not to create really big files (tens of megabytes), or a lot
of files, especially on the internal flash and in ram. If the X-600M runs out of space on the internal flash,
logging will not function correctly. If the ram drive is filled (it's 40MB,) the X-600M will have less ram to
function. Third, always check the result of a call to file.open to make sure a NIL value is not returned. If
an attempt to read or write to a file is made using a file handle that is NIL, the Lua script will throw an
error and stop executing.
Page 126
Xytronix Research & Design, Inc.
Содержание X-600M
Страница 1: ...X 600MTitle Page Goes Here...
Страница 6: ...X 600M Users Manual Page 4 Xytronix Research Design Inc...
Страница 99: ...X 600M Users Manual Setup Pages Xytronix Research Design Inc Page 97...
Страница 116: ...Appendix B Installing New Firmware X 600M Users Manual Page 114 Xytronix Research Design Inc...