02 Lua Operation
Support model and version
| model | Whether to support Lua script |
| E-4G | support |
| H-00 | support |
| H-WF | support |
| H-AG | support |
| S-3N (Discontinued) | support |
| S-NG (Discontinued) | support |
| E-00 (Discontinued) | not support |
| E-2G (Discontinued) | not support |
Lua Script for V-NET
Click the V-Box menu "Lua Script". Lua operation includes: new script, edit script, delete script, batch delete, batch synchronization, import script, export script, debugging, view V-Box script and other functions.

Import:import new script from computer,and the original scripts will be cleared if import new scripts.
Export:export “V-Box.script” to computer.But user can not program it.
Sync selected:update script into V-Box
Delete selected:delete script into V-Box
New: creat new script.Up to five new scripts can be added
Debug:test script and check result.In general,user will print different text to test whether script is correct.
Create a new script
Click on the "New" button to create a new script for this V-Box. It requires two steps to complete.

Step 1: Enter script name “Test1” , choose timing mode and click “save and next”

Name: Enter the name. The name must be begin with letter, within a maximum of 32 characters , characters only support letter and number
Execution mode: select the execution conditions of this script. The execution conditions of the script include the following

| Execution conditions | Features | Script Quantity |
|---|---|---|
| Initial execution | It is executed after the V-Box is powered on, and only once. | 1. |
| Execute if connect | Execute one time after V-Box connects server. | 1. |
| Execute if disconnect | execute one time after V-Box disconnects server. | 1. |
| Timing | According to the set timing time, loop execution. | Up to 5. |
Timing: When the execution script condition is timing execution, you need to enter the time and time unit of the timing execution. The time unit can choose days, hours, minutes, seconds, milliseconds. And the timing time must be greater than 1000 milliseconds and cannot exceed 1 day.After the information is filled in correctly, click Save and enter the next step to generate a new script and enter the next step of script editing, script editing.
Step 2: Write the LUA script

After entering the step 2 to write the LUA script. At this time, the initialization code will be displayed in the script editor. As follows:
function script name.main()
--dosomething
End
Step 3:Click "Sync" to synchronize script between V-Box with server
![]()
Step 4:Click debug and set on the debug button."happy new year" will be printed

Script list:
The added script will be displayed in the script list. One line represents one script. As shown below

Description of script parameters:
| Field | Description |
| status | Refers to whether the script is synchronized. The state is divided into two states: synchronized and unsynchronized |
| name | script name |
| Execution script | The execution conditions of the script |
| Enable | ON means to run script, and OFF means to close script. When the V-Box is offline or pass-through, the button is grayed out and cannot be operated |
| Synchronize | Synchronize the script to the V-Box. When the script synchronization fails, it will prompt the synchronization failure, and click on the details to view the failure information feedback from the V-Box. When the V-Box is offline or pass-through, the button is grayed out and cannot be operated |
| V-Box script | View the script that have been running on the V-Box |
| operating | delete and edit script |
Delete
Click the delete button in the operation V-Box behind the script list to delete a single script.
Select multiple script, click the batch delete in the script list button bar, you can delete the selected script
Editor
Click the edit button in the operation V-Box in thescript list to edit the script. The second step of the default entry script: writing LUA scripts. After entering this page, click the first step to view the basic information of the script. When editing a script, in the basic information, except for the timing, other information cannot be modified.

Batch sync
Select script and click the batch synchronization button above the list to perform batch synchronization of the selected scripts. The compilation failure information after synchronization will be displayed after the synchronization button in the script list.
(1) Import and export scripts
(2) Export script: Click in the button bar above the policy list
, Can export all the script of the current V-Box as a file with a .script extension
(3) Import script: Click in the button bar above the script list
, You can import an external script file with a .script extension into the V-Box. After importing the script, the script of the current V-Box will be cleared.

Debugging
Click the debug button in the button bar above the script list to enter the script debugging window of the V-Box

The switch button in the debugging window can turn on or off the report debugging information of the current V-Box
Script editor
The script editor is an online editor for users to write LUA scripts in the edge computing module. In the second step of adding scripts and modifying script, you can switch to the script editor page to write the LUA script of the V-Box script. As shown below

The function partition on the script edit page is shown in the figure below:

List of monitoring points
This area lists all the monitoring point lists of the current V-Box and the function of searching monitoring points.
Click the line of the monitoring point in the monitoring point list, and the monitoring point will be inserted at the current cursor of the script editor. The inserted monitoring point will be added with the @ symbol in front of the monitoring point name in the editor and defined as a string. Such as”@Beijing A Steam temperature”, The monitoring point variable will follow the grammar rules of the string for grammar checking. After synchronizing the script, the monitoring point variable will be converted to an address on the server side and sent to the V-Box side for use.
Note: The above monitoring points are converted into address format as @Address type_ serial number# station number: register type main number. sub number Among them, if the station number is empty, remove the station number:, if the sub-number is empty, remove the sub-number
When the mouse passes over the line of the monitoring point, more detailed monitoring point information will be displayed through the floating V-Box, and clicking the floating V-Box can also display to the cursor of the script editor
Tool list
Located at the top of the script editor

From left to right
Save code: save the current code, the small red dot next to it means the code has been modified but not saved
Clear code: Clear the current code in the script editor. And restore to the initial structure
Insert new address: insert a new bit address or word address at the current cursor
Copy:Copy the code, shortcut key [ctrl+c]. For some low-version browsers (such as IE8 and below), you may not be able to copy the content to use outside of this editor. It is recommended to use ctrl+c to copy the key combination.
Paste:Paste, shortcut key [ctrl+v]. Except for Internet Explorer, this button is limited to pasting and copying the text in the current editor. It is recommended to use ctrl+v to paste the key combination.
Undo: Undo actions in the editor every step
Restore: restore the operation in the editor every step
Comment code: comment or uncomment the code in the current line or the selected line
Help: Script editor help document. Will be open to the technical department for the management of help files
Code editing area
The lua script editor provides user-written lua scripts in the script. It can also perform syntax verification, keyword prompts, common code snippet prompts, variable prompts, code completion, etc. for the written code. At the same time, the monitoring point management function is provided, and the address can be directly inserted as a variable in the code.
Grammar check:

When writing Lua code, you can check the validity of the syntax in real time. An icon will appear next to the line number of the syntax error line
, The error message will be prompted after the mouse is moved up. When multiple lines have errors, it will be displayed on the first line of the error. After the error in this line is resolved, it will be displayed on the next line of the error line.
Error example: [3:30]'do' expected near'doo'. Indicates that do is expected to be entered at the thirtieth character position on the third line
Grammar checking includes the following aspects:
A: Check the spelling of keywords
B: Check the definition of variables
C: Check the usage specifications of operators
D: Check the syntax of the function body structure
(1) Script prompt
When writing code, provide hints such as fuzzy matching keywords, defined variables, and code snippets. Click the corresponding prompt line to complete the code. As shown below

The provided prompt codes and complete codes include the following categories:
A: Keywords (keyword), Lua language keywords, built-in method names, built-in constants, library functions.
B: Code snippet (snippet), commonly used code snippets in Lua language.
C: Defined variables (local), the names of all defined variables.
| code segment | local x = 1 | Variable definitions |
function fname(...) - body end | Function structure | |
for i=1,10 do print(i) end | for loop structure | |
while (condition) do - body end | while loop structure | |
if (condition) then - body end | Conditional structure | |
| System keywords | break|do|else|elseif|end|for|function|if|in|local|repeat|return|then|until|while|or|and|not | |
| Project-specific keywords | status|wrap|encode|decode|mqtt|serial|json|null| open|close|write|flush|read|addr_getshort|addr_setshort|addr_getword|addr_setword|addr_getint|addr_setint|addr_getdword|addr_setdword|addr_getbit|addr_setbit|addr_getbit|addr_setbit|addraddraddraddraddraddraddraddraddr_addr_doubleaddradd connect|disconnect|isconnected|subscribe|unsubscribe|publish|on | |
| Method name | _G|_VERSION|assert|collectgarbage|dofile|error|getmetatable|ipairs|" + "load|loadfile|next|pairs|pcall|print|rawequal|" + "rawget|rawlen|rawset|require|select|setmetatable|" + "tonumber|tostring|type|xpcall|create|isyieldable|resume|running|" + "status|wrap|yield|debug|gethook|getinfo|getlocal|" + "getmetatable|getregistry|getupvalue|" + "getuservalue|sethook|setlocal|setmetatable|setupvalue|setuservalue|traceback|upvalueid|upvaluejoin|" + "abs|acos|asin|atan|ceil|cos|deg|exp|" + "floor|fmod|huge|log|max|maxinteger|min|mininteger|" + "modf|pi|rad|random|randomseed|sin|sqrt|tan|" + "tointeger|type|ult|clock|date|difftime|time|config|cpath|" + "loaded|loadlib|path|preload|searchers|searchpath|byte|char|dump|" + "find|format|gmatch|gsub|len|lower|match|pack|packsize|rep|reverse|" + "sub|unpack|upper|concat|insert|move|pack|remove|sort|unpack|charpattern|" + "codepoint|codes|offset|" + "foreachi|maxn|foreach|concat|remove| | |
| Built-in constant | true|false|nil|_G|_VERSION | |
| System library functions | basic|string|package|os|io|math|debug|table|utf8|coroutine | |
| Project-specific library functions | mqtt|serial | |
| other | setn|foreach|foreachi|gcinfo|log10|maxn |
(2) Code highlighting
| Code type | colour |
| Normal code | black |
| Key words | red |
| variable | black |
| String | blue |
| Built-in method name | green |
| Constants and built-in constants | purple |
| Library Functions | black |
| Comment, [[xx]] | gray |
Prompt area

a is located below the script editor. It is hidden by default. When you click the custom method name or mouse over the toolbar button, related prompts will be displayed.
b Double-click the prompt V-Box or click the X button in the upper right corner to close the prompt V-Box
c Click the up or down arrow to collapse or collapse the prompt V-Box
Save reminder
In the case that the script content is modified and not saved, jumping to other modules will pop up a pop-up V-Box to confirm whether to leave. The prompt content of different browsers will be slightly different, as shown in the following figure:
Chrome:

Firefox:

PC configuration tool:
