9.6 Lua Script Interfaces
Lua manual link: https://drive.google.com/drive/folders/1XbyB-j_lFq6eirMnccR1HS4TkkKsMrtD?usp=sharing
1.Data format
void //void
double //floating number
int //signed integer
uint //unsinged integer
string //string
table //table
bool //false or true
multi // Multiple return values (see the description of "Return" for details)
2.Lua built-in function library
The built-in functions mentioned below could be used, the rest cannot be used
All available:
Mathematics, string, table, file io, UTF8, etc.
Partially available:
System type os (only includes clock, date, difftime, remove, rename, time)
3.File IO
As long as the file is not closed during the use of the write function, what is written is temporarily stored in the RAM space. If the cumulative number of written bytes is about 60% of the RAM size, the program will be crashed. For example, the HMI memory size is 128M, and written byte is about 70M. The operating memory space required by the system itself, then a total of 90% of the total memory is occupied, the program will be killed (that is, the screen will freeze).
The space of the internal Flash is limited. Once the internal space is full, files cannot be written. If you need to operate large files, they must be handled separately, you can read and write files through an external U disk.
4.Table
Lua tables can implement various data structures, such as stacks, queues, and trees. The size of the table needs to be controlled during use, because the RAM size of the HMI is limited. Using a loop to create a table array with nearly endless elements results in excessive memory usage, thus the program wil be killed (that is, the screen freezes) . Below is an example.
local t = {}
for i=1, math.maxinteger do
table.insert(t, math.random(100000, 9999999))
end
5.Lua supports bit operation
&: Bitwise and
|: Bitwise or
~: Bitwise XOR
>>: right shift
<<: left shift
~: bitwise not
Chapter 1 Public function
A large number of operations to read and write external addresses will slow down the running speed of the script, such as calling we_bas_setword("xx", 1) 100 times, it may take a few seconds to run.
we_atu8
string we_atu8(string str)
/*
*Function:Convert ANSI character set string to utf8 character set string
*Parameter:
* str String of ANSI character set
*Return:String of Utf-8 character set
*/
we_u8ta
string we_u8ta(string str)
/*
*Function:Convert utf8 character set string to ANSI character set string,
*Parameter:
* str String of Utf-8 character set
*Return:String of ANSI character set
*/
we_bas_getshort
multi we_bas_getshort(string address, int type)
/*
*Function:Read single word signed decimal address
*Parameter:
* address address
* type type(no need to set,type = 1 means read thorough)
*Return:single word signed decimal value Read correctly
* multi Return values(nil and error message) read error
*/
we_bas_setshort
multi we_bas_setshort(string address, int value, int type)
/*
*Function:Write value to a single word signed decimal address
*Parameter:
* address address
* value value
* type type(no need to set,type = 1 means read thorough)
*Return:TRUE set correctly
* multi Return values(nil and error message) set error
*/
we_bas_getword
multi we_bas_getword(string address, int type)
/*
*Function:Read single word unsigned decimal address
*Parameter:
* address address
* type type(no need to set,type = means read thorough)
*Return:Word unsigned decimal value read correctly
* multi return values(nil and error message) read error
*/
we_bas_setword
multi we_bas_setword(string address, uint value, int type)
/*
*Function:Write a single word unsigned decimal address
*Parameter:
* address address
* value value
* type type(no need to set,type = means read thorough)
*Return:TRUE set correctly
* multi return values(nil and error message) set error
*/
we_bas_getint
multi we_bas_getint(string address, int type)
/*
*Function:Read double word signed decimal address
*Parameter:
* address address
* type type(no need to set,type = means read thorough)
*Return:Double word signed decimal value read correctly
* multi return values(nil and error message) read error
*/
we_bas_setint
multi we_bas_setint(string address, int value, int type)
/*
*Function:Write double-word signed decimal address
*Parameter:
* address address
* value value
* type type(no need to set,type = means read thorough)
*Return:TRUE set correctly
* multi return values(nil and error message) set error
*/
we_bas_getdword
multi we_bas_getdword(string address, int type)
/*
*Function:Read double word unsigned decimal address
*Parameter:
* address address
* type type(no need to set,type = means read thorough)
*Return:Double word unsigned decimal value read correctly
* multi return values(nil and error message) read error
*/
we_bas_setdword
multi we_bas_setdword(string address, uint value, int type)
/*
*Function:Write double word unsigned decimal address
*Parameter:
* address address
* value value
* type type(no need to set,type = means read thorough)
*Return:TRUE set correctly
* multi return values(nil and error message) set error
*/
we_bas_getbit
multi we_bas_getbit(string address, int type)
/*
*Function:Read a bit of the register address
*Parameter:
* address internal bit address
* type type(no need to set,type = means read thorough)
*Return:bit address value read correctly
* multi return values(nil and error message) read error
*/
we_bas_setbit
multi we_bas_setbit(string address, uint value, int type)
/*
*Function:Write a bit of the register address
*Parameter:
* address internal bit address
* value value
* type type(no need to set,type = means read thorough)
*Return:TRUE set correctly
* multi return values(nil and error message) set error
*/
we_bas_getfloat
multi we_bas_getfloat(string address, int type)
/*
*Function:Read 32-bit floating point address
*Parameter:
* address address
* type type(no need to set,type = means read thorough)
*Return:32-bit floating point value read correctly
* multi return values(nil and error message) read error
*/
we_bas_setfloat
multi we_bas_setfloat(string address, double value, int type)
/*
*Function:Write 32-bit floating point address
*Parameter:
* address address
* value value
* type type(no need to set,type = means read thorough)
*Return:TRUE set correctly
* multi return values(nil and error message) set error
*/
we_bas_getdouble
multi we_bas_getdouble(string address, int type)
/*
*Function:Read 64-bit floating point address
*Parameter:
* address address
* type type(no need to set,type = means read thorough)
*Return:64-bit floating point value read correctly
* multi return values(nil and error message) read error
*/
we_bas_setdouble
multi we_bas_setdouble(string address, double value, int type)
/*
*Function:Write 64-bit floating point address
*Parameter:
* address address
* value value
* type type(no need to set,type = means read thorough)
*Return:TRUE set correctly
* multi return values(nil and error message) set error
*/
we_bas_getstring
multi we_bas_getstring(string address, uint len)
/*
*Function:Read the specified length string *Parameter:
* address address
* len length(unit:byte)
*Return:Specify length string read correctly
* multi return values(nil and error message) read error
*/
we_bas_setstring
multi we_bas_setstring(string address, string str, uint len)
/*
*Function:write string
*Parameter:
* address address
* str string
* len length(no need to set,the default setting is the number of bytes in the string)
*Return:TRUE set correctly
* multi return values(nil and error message) set error
*/
we_bas_gettickcount
uint we_bas_gettickcount()
/*
*Function:Get the time(millisecond) it takes to start the OS
*Parameter:
* none
*Return:Return the time it takes to start the OS
*/
we_bas_sleep
void we_bas_sleep(uint millisecond)
/*
*Function:sleep
*Parameter:
* millisecond Sleep milliseconds
*Return:none
*/
we_bas_bmov
multi we_bas_bmov(string desAddress, string srcAddress, uint len)
/*
*Function:Copy data with specified length from source address to destination address
*Parameter:
* desAddress target address
* srcAddress source address
* len Specified length (unit: word)
*Return:TRUE copy successfully
* multi return values(nil and error message) copy error
*/
print
void print(constchar *format, [argument])
/*
*Function:Print debugging information in the debugging window
*Parameter:
* format,[argument] print Parameter format
*Return:none
*/
we_bas_fill
multi we_bas_fill(string address, int value, uint len)
/*
*Function:Assign the same value to consecutive addresses
*Parameter:
* address address
* value value
* len Continuous length (unit: words)
*Return:TRUE set correctly
* multi return values(nil and error message) set error
*/
we_bas_beep
void we_bas_beep()
/*
*Function:beep
*Parameter:
* none
*Return:none
*/
we_bas_newnoaddr
multi we_bas_newnoaddr(string address, uint len)
/*
*Function:offset address
*Parameter:
* address address
* len length
*Return:the address after offset set correctly
* multi return values(nil and error message) set error
*/
we_bas_newstataddr
multi we_bas_newstataddr(string address, uint len)
/*
*Function:offset station number
*Parameter:
* address address
* len length
*Return:the address after offset set correctly
* multi return values(nil and error message) set error
*/
we_bas_getscriptpath
multi we_bas_getscriptpath()
/*
*Function:get script path
*Parameter: none
*Return:script path get successfully
* multi return values(nil and error message) get error
*/
we_bas_getcustompath
string we_bas_getcustompath()
/*
*Function:Get user-defined folder path
*Parameter: none
*Return:data path
*/
we_bas_getusbpath
multi we_bas_getusbpath()
/*
*Function:get u disk path
*Parameter: none
*Return:U disk path get successfully
* multi return values(nil and error message) get error
*/
we_bas_getsdpath
multi we_bas_getsdpath()
/*
*Function:get SD card path
*Parameter: none
*Return:SD card path get successfully
* multi return values(nil and error message) get error
*/
we_bas_file_exist
bool we_bas_file_exist(string path)
/*
*Function: judge whether there is a file
*Parameter: path file path
*Return:true exist
* false not exist
*/
we_bas_path_exist
bool we_bas_path_exist(string path)
/*
*Function:judge whether there is a folder
*Parameter: path folder path
*Return:true exist
* false not exist
*/
we_bas_jumpscreen
bool we_bas_jumpscreen(uint id)
/*
*Function:main screen jump
*Parameter:id screen id
*Return:true jump successfully
* false jump error
*/
we_bas_popscreen
bool we_bas_popscreen (int scrno,int x, int y)
/*
*Function:Sub-screen pops up, if the built-in screen like keyboard pops up, the screen cannot be used
*Parameter:
* scrno Screen number of the sub screen
* x The x coordinate
* y The y coordinate
*Return:
* true success
* false failure
*Note:
*/
we_bas_closescreen
bool we_bas_closescreen (int scrno)
/*
*Function:close subscreen
*Parameter:
* scrno Screen number of the sub screen
*Return:
* true success
* false failure
*Note:
*/
*
Chapter 2 Object operation function
2.1 we_part_setstate
bool we_part_setstate(string partName, string value)
/*
*Function:Set the state of the object according to the object name (for bit lamp, bit switch, word switch, LUA script object)
*Parameter:
* partName object name
* value the set value
*Return:
* true success
* false failure
*Note:If object has an address, then the operation address is the monitoring address;
* If object doesn’t have an address, then the operation address is the internal address;
* The “parameter value” should be the original data
*/
2.2 we_part_getstate
string we_part_getstate(string partName)
/*
*Function:get the value of object(for bit lamp, bit switch, word switch, lua script object)
*Parameter:
* partName object name
*Return:
* objet value success
* nill failure
*Note:If object has an address, then the operation address is the monitoring address;
* If object doesn’t have an address, then the operation address is the internal address;
* The “parameter value” must be the original data
*/
2.3 we_part_settchenable
bool we_part_settchenable(string partName)
/*
*Function:Set to allow parts to be clicked (for all parts with this attribute)
*Parameter:
* partName object name
*Return:
* true success
* false failure
*Note:
*/
2.4 we_part_settchunable
bool we_part_settchunable(string partName)
/*
*Function:Set to not allow parts to be clicked (for all parts with this attribute)
*Parameter:
* partName object name
*Return:
* true success
* false failure
*Note:
*/
2.5 we_part_gettchstate
bool we_part_gettchstate(string partName)
/*
*Function:Get whether the part is allowed to be clicked (for all parts with this attribute)
*Parameter:
* partName object name
*Return:
* true allowed
* false not allowed
* nill failure
*Note:
*/
2.6 we_part_sethidestate
bool we_part_sethidestate(string partName, int value)
/*
*Function:Set part hidden attribute (for all parts with this attribute)
*Parameter:
* partName object name
* value the set value
* 0:Dispaly or hide according to part control address
* 1:Just display and not affected by the control address
* 2:Just hide and affected by the control address
*Return:
* true success
* false failure
*Note:
*/
2.7 we_part_gethidestate
bool we_part_gethidestate(string partName)
/*
*Function:Get hidden attributes of parts (for all parts with this attribute)
*Parameter:
* partName object name
*Return:
* true hidden
* false display
* nill failure
*Note:
*/
2.8 we_part_clickdown
bool we_part_clickdown(string partName)
/*
*Function:Send click-down command messages to specified parts (for all parts with this attribute)
*Parameter:
* partName object name
*Return:
* true success
* false failure
*Note:
*/
2.9 we_part_clickup
bool we_part_clickup(string partName)
/*
*Function:Send click-up command messages to specified parts (for all parts with this attribute)
*Parameter:
* partName object name
*Return:
* true success
* false failure
*Note:
*/
2.10 we_part_setcurrc
bool we_part_setcurrc(string partName, int Left, int Top, int Right, int Bottom)
/*
*Function:Set the contour information of the part (for all parts with this attribute)
*Parameter:
* partName object name
* Left coordinate of the left side of the part
* Top coordinate of the top side of the part
* Right Coordinates of the right side of the part
* Bottom coordinate of the bottom side of the part
*Return:
* true success
* false failure
*Note:
*/
2.11 we_part_getcurrc
multi we_part_getcurrc(string partName)
/*
*Function:Get the outline information of the part (for all parts have this attribute)
*Parameter:
* partName object name
*Return:
* Left nill: Execution error
* not nill(int):coordinate of the left side of the part
* Top nill: Execution error
* not nill(int):coordinate of the top side of the part
* Right nill: Execution error
* not nill(int):Coordinates of the right side of the part
* Bottom nill: Execution error
* not nill(int):coordinate of the bottom side of the part
*Note:
*/
2.12 we_part_getoriginalrc
multi we_part_getoriginalrc(string partName)
/*
*Function:Get contour information of the part (for all parts with this attribute)
*Parameter:
* partName object name
*Return:
* Left nill: Execution error
* not nill(int):Coordinates of the left side of the part
* Top nill: Execution error
* not nill(int):coordinate of the top side of the part
* Right nill: Execution error
* not nill(int):coordinate of the right side of the part
* Bottom nill: Execution error
* not nill(int):coordinate of the bottom side of the part
*Note:
*/
Chapter 3 lfs operation library functions
Declare: The following lists only some of the functions of the lfs library, and others that are not mentioned in the document are unavailable. For details, see http://math2.org/luasearch/lfs.html.
3.1 lfs.attributes
table lfs.attributes(string filePath [, aname])
/*
*Function:Get a table with file attributes corresponding to the file path, if the second parameter is given, only the value of the named attribute is returned
*Parameter:
* filePath given path
* aname attribute(optional)
*Return:Return a table get successfully
* nil get error
*/
3.2 lfs.currentdir
string lfs.currentdir()
/*
*Function:Get current working directory
*Parameter:
* none
*Return:Return path get successfully
* nil get error
*/
3.3 lfs.dir
string lfs.dir(string path)
/*
*Function:go through current directory to get file name
*Parameter:
* path Current directory path
*Return:Return file name get successfully
* nil get error
*/
3.4 lfs.mkdir
bool lfs.mkdir(string dirName)
/*
*Function:Create Folder
*Parameter:
* dirName Folder name
*Return: TRUE Operation correct
* nil Operation error
*/
3.5 lfs.rmdir
bool lfs.rmdir(string dirName)
/*
*Function:remove folder
*Parameter:
* dirName folder name
*Return: TRUE operation correct
* nil operation error
*/
Last updated