Changes for page 09 Lua Editor

Last modified by Nhu Nguyen on 2025/03/09 17:22

From version 1.1
edited by Leo Wei
on 2022/06/08 12:57
Change comment: Imported from XAR
To version 9.2
edited by Nhu Nguyen
on 2025/03/09 17:22
Change comment: Added comment

Summary

Details

Page properties
Title
... ... @@ -1,1 +1,1 @@
1 -Foreword
1 +09 Lua Editor
Parent
... ... @@ -1,1 +1,1 @@
1 -PIStudio.1 User Manual.10\.Lua.WebHome
1 +PIStudio.WebHome
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.admin
1 +XWiki.nhunguyen95
Content
... ... @@ -1,169 +1,132 @@
1 -**[[1 Lua Editor>>path:#_Toc42782932]]**
1 +Lua script is only supported on HMI+, i series, ie series, ig series or models whose upgrade OS version to HMI V2.0 and above;
2 2  
3 -**[[1.1 Script File Area>>path:#_Toc42782933]]**
4 -
5 -**[[1.1.1 Lua Node>>path:#_Toc42782934]]**
6 -
7 -**[[1.1.2 Lua Child Node>>path:#_Toc42782936]]**
8 -
9 -**[[1.2 Script Editor>>path:#_Toc42782937]]**
10 -
11 -[[(% class="wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink" %)__**1.2.1 Tool bar**__ 5>>path:#_Toc42782938]]
12 -
13 -[[(% class="wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink" %)__**1.2.2 Editor area**__ 5>>path:#_Toc42782939]]
14 -
15 -[[(% class="wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink" %)__**1.2.3 Output area**__ 5>>path:#_Toc42782940]]
16 -
17 -[[(% class="wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink" %)__**2 Lua object**__ 6>>path:#_Toc42782941]]
18 -
19 -[[(% class="wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink" %)__**2.1 How to use LuaScrip object**__ 6>>path:#_Toc42782942]]
20 -
21 -[[(% class="wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink" %)__**3 Platform**__ 7>>path:#_Toc42782943]]
22 -
23 -[[(% class="wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink" %)__**3.1 File header identification**__ 7>>path:#_Toc42782944]]
24 -
25 -[[(% class="wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink" %)__**3.2 Special script file**__ 8>>path:#_Toc42782945]]
26 -
27 -[[(% class="wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink wikiinternallink" %)__**3.3** **Script type (operating mechanism)**__ 9>>path:#_Toc42782946]]
28 -
29 -Lua script is currently only available in PI+ or models ending with i(like PI3070i). Only these two types of HMI can use the Lua editor and object to develop lua scripts. The following operations are based on PI3070i project.
30 -
31 -This article only introduces the use of lua object in PIStudio.
32 -
33 33  (% style="text-align:center" %)
34 -[[image:HMI Lua Editor user manual_html_90c08e4cdcf4210c.png||class="img-thumbnail" height="484" width="500"]]
4 +[[image:HMI Lua Editor user manual_html_90c08e4cdcf4210c.png||height="484" width="500" class="img-thumbnail"]]
35 35  
36 36  
37 37  (% style="text-align:center" %)
38 -[[image:HMI Lua Editor user manual_html_674bb6778b0f003.png||class="img-thumbnail" height="487" width="500"]]
8 +[[image:HMI Lua Editor user manual_html_674bb6778b0f003.png||height="487" width="500" class="img-thumbnail"]]
39 39  
40 -{{id name="_Toc42782932"/}}
10 += **Lua Editor** =
41 41  
42 -{{id name="_Toc6870"/}}{{id name="_Toc27091"/}}**1 Lua editor**
43 -
44 44  The editor is divided into two areas, one is the script file area, and the other is the script editing area.
45 45  
46 46  (% style="text-align:center" %)
47 -[[image:HMI Lua Editor user manual_html_34da990820a7a4e5.png||class="img-thumbnail" height="208" width="400"]]
15 +[[image:HMI Lua Editor user manual_html_34da990820a7a4e5.png||height="208" width="400" class="img-thumbnail"]]
48 48  
49 -{{id name="_Toc42782933"/}}{{id name="_Toc14144"/}}{{id name="_Toc22413"/}}**1.1 Script file area**
17 +== Script file area ==
50 50  
51 51  The script file area is located on the left side, named after the Lua, It will be referred to as the Lua node hereinafter.
52 52  
53 -**1.1.1 {{id name="_Toc42782934"/}}{{id name="_Toc2961"/}}Lua node**
21 +**Lua Node**
54 54  
55 -~1. Create new file: right-click the Lua node to select new file, and enter a name in pop-up window, (the name starts with an underscore or letter);
23 +* Create new file: right-click the Lua node to select new file, and enter a name in pop-up window, (the name starts with an underscore or letter);
24 +* Refresh list: the file edited (new/deleted/renamed) by the user externally, the configuration is not refreshed. In this case this function needs to be used to synchronize changes;
56 56  
57 -2.Refresh list: the file edited (new/deleted/renamed) by the user externally, the configuration is not refreshed. In this case this function needs to be used to synchronize changes;
26 +(% class="box infomessage" %)
27 +(((
28 +**✎Note: **
58 58  
59 -**✎Note: **There is no script file for the newly created project. Following two new files will automatically generate.
30 +* There is no script file for the newly created project. Following two new files will automatically generate.
31 +* Script_BG.lua: is the global background script file, which includes initialization and timing functions
32 +* Script_Screen.lua: is a screen script file, and each screen can have initialization, timing, and close functions
33 +* At any time, as long as there is a new operation, the software will judge whether the above two files exist, and which one is missing will be automatically filled.
34 +)))
60 60  
61 -Script_BG.lua: is the global background script file, which includes initialization and timing functions
36 +**Lua Child Node**
62 62  
63 -Script_Screen.lua: is a screen script file, and each screen can have initialization, timing, and close functions
64 -
65 -{{id name="_Toc5597"/}}{{id name="_Toc42782551"/}}{{id name="_Toc42782582"/}}{{id name="_Toc42782935"/}}{{id name="_GoBack"/}}At any time, as long as there is a new operation, the software will judge whether the above two files exist, and which one is missing will be automatically filled.
66 -
67 -**{{id name="_Toc42782936"/}}1.1.2 Lua child node**
68 -
69 69  After creating a new file, expand the Lua node to display several script files. The following is referred to as child nodes.
70 70  
71 71  (% style="text-align:center" %)
72 -[[image:HMI Lua Editor user manual_html_fc10128583a230da.png||class="img-thumbnail" height="143" width="200"]]
41 +[[image:HMI Lua Editor user manual_html_fc10128583a230da.png||height="143" width="200" class="img-thumbnail"]]
73 73  
74 -Child node color description:
43 +* Child node color description:
44 +* Red: encrypted file
45 +* Yellow: read-only file
46 +* Green: existed file or refresh the list to turn white to green
47 +* White: The newly created file, and the project will turn green after refreshing, protecting or reopening the project
75 75  
76 -Red: encrypted file
49 +**Delete:** right-click the child node to delete. The deleted node will be placed in the recycle bin.
77 77  
78 -Yellow: read-only file
51 +**Rename: **right-click child node to rename.
79 79  
80 -Green: existed file or refresh the list to turn white to green
53 +**Encryption:** right-click the child node, select encryption. Each file can be set with an independent password(length 1-6, only uppercase/lowercase letters and numbers are accepted).
81 81  
82 -White: The newly created file, and the project will turn green after refreshing, protecting or reopening the project
55 +* Encryption mode: When this mode is turned on, you need to verify the password to view and edit.
56 +* Read-only mode: This mode is opened without verifying the password, if you want to edit, you must first release the protection
83 83  
84 -**(1) Delete:** right-click the child node to delete. The deleted node will be placed in the recycle bin.
58 +**Unprotect:** Right-click on the child node, select Unprotect, enter the password to release.
85 85  
86 -**(2)** **Rename: **right-click child node to rename.
87 -
88 -**(3) Encryption:** right-click the child node, select encryption. Each file can be set with an independent password(length 1-6, only uppercase/lowercase letters and numbers are accepted).
89 -
90 -Encryption mode: When this mode is turned on, you need to verify the password to view and edit.
91 -
92 -Read-only mode: This mode is opened without verifying the password, if you want to edit, you must first release the protection
93 -
94 -**(4) Unprotect:** Right-click on the child node, select Unprotect, enter the password to release.
95 -
60 +(% class="box infomessage" %)
61 +(((
96 96  **✎Note: **If user forgets the password, the script file can never be recovered, the software will not storage any passwords, developers need to keep the password by themselves carefully.
63 +)))
97 97  
98 -{{id name="_Toc42782937"/}}{{id name="_Toc5485"/}}{{id name="_Toc31584"/}}**1.2 Script editor**
65 +== Script editor ==
99 99  
100 100  The script editor is divided into three blocks: the upper part is the toolbar, the middle part is the editing area, and the lower part is the output area.
101 101  
102 -{{id name="_Toc42782938"/}}{{id name="_Toc24279"/}}**1.2.1 Tool bar**
69 +**Tool Bar**
103 103  
104 104  Open any script file, the toolbar is as follows:
105 105  
106 106  (% style="text-align:center" %)
107 -[[image:HMI Lua Editor user manual_html_59f8bacea4239441.png||class="img-thumbnail" height="29" width="279"]]
74 +[[image:HMI Lua Editor user manual_html_59f8bacea4239441.png||height="29" width="279" class="img-thumbnail"]]
108 108  
109 109  Save, cut, copy, paste, word address, bit address, zoom in, zoom out, display spaces and tabs, grammar check.
110 110  
111 111  Find and replace, undo and redo and more functions see shortcut keys manual.
112 112  
113 -{{id name="_Toc42782939"/}}{{id name="_Toc32765"/}}**1.2.2 Editor area**
80 +**Editor Area**
114 114  
115 -**(1) Highlight color:** Lua's built-in keywords has a different color with the Lua interface functions. The functions written by users will not be highlighted
82 +* **Highlight color:** Lua's built-in keywords has a different color with the Lua interface functions. The functions written by users will not be highlighted
83 +* **Auto-complete:** Lua function auto-complete prompts, Lua chunk auto-complete (such as if xx then end), also supports the completion of all the words in this script file. Use the Enter key to select candidates words, use the Tab key to turn off automatic completion.
84 +* **Automatic indentation: **The tab key can add 4 spaces
85 +* **Calling prompt:** only available for Lua interface functions
86 +* **Hovering hint:** for example the mouse stays near the o letter position of demo() , it will display the prototype of the function
87 +* **(6)** **Shortcut keys:** In order to facilitate users, various shortcut keys have been added. Detailed description: Configure Lua shortcut keys.doc
88 +* **Interface document: **The scripts listed in the interface document will be distinguished by the highlighted color in the editing area. You can check whether the interface is called incorrectly by judging the color change. Detailed description: Lua interface manual.
116 116  
117 -**(2)** **Auto-complete:** Lua function auto-complete prompts, Lua chunk auto-complete (such as if xx then end), also supports the completion of all the words in this script file. Use the Enter key to select candidates words, use the Tab key to turn off automatic completion.
90 +**Output Area**
118 118  
119 -**(3) Automatic indentation: **The tab key can add 4 spaces
120 -
121 -**(4) Calling prompt:** only available for Lua interface functions
122 -
123 -**(5) Hovering hint:** for example the mouse stays near the o letter position of demo() , it will display the prototype of the function
124 -
125 -**(6)** **Shortcut keys:** In order to facilitate users, various shortcut keys have been added. Detailed description: Configure Lua shortcut keys.doc
126 -
127 -**(7)** **Interface document: **The scripts listed in the interface document will be distinguished by the highlighted color in the editing area. You can check whether the interface is called incorrectly by judging the color change. Detailed description: Lua interface manual.
128 -
129 -{{id name="_Toc42782940"/}}{{id name="_Toc25425"/}}**1.2.3 Output area**
130 -
131 131  Click the syntax button in the toolbar to perform a grammar check. If the script is written incorrectly, it will be displayed in the window. Double-click the prompt to locate the "near" line of the error.
132 132  
133 -{{id name="_Toc42782941"/}}{{id name="_Toc27350"/}}{{id name="_Toc26670"/}}**2 Lua object**
94 += **Lua Object** =
134 134  
135 135  Lua objects are mainly used to call functions and complete user interface interactions. Normally, functions should not be written in objects. Next, we will explain how to use the objects.
136 136  
137 137  (% style="text-align:center" %)
138 -[[image:HMI Lua Editor user manual_html_a9a41fb25f413fec.png||class="img-thumbnail" height="317" width="150"]]
99 +[[image:HMI Lua Editor user manual_html_a9a41fb25f413fec.png||height="317" width="150" class="img-thumbnail"]]
139 139  
140 -{{id name="_Toc42782942"/}}{{id name="_Toc888"/}}{{id name="_Toc26065"/}}**2.1 How to use LuaScrip object**
101 +== How to use Lua button ==
141 141  
142 142  Find the custom part->LuaScript on the right side, drag and draw to generate a part. Double-click the part to pop up a dialog box.
143 143  
144 144  (% style="text-align:center" %)
145 -[[image:HMI Lua Editor user manual_html_14bab1899c9ccc62.png||class="img-thumbnail" height="267" width="400"]]
106 +[[image:HMI Lua Editor user manual_html_14bab1899c9ccc62.png||height="267" width="400" class="img-thumbnail"]]
146 146  
147 147  Click Lua object will trigger two actions, that is, rising and falling scripts;
148 148  
149 -**(1)** **Falling script**: after the button is pressed, the code chunk of the area is executed;
110 +* **Falling script**: after the button is pressed, the code chunk of the area is executed;
111 +* **Rising script**: after the button is raised, the code chunk in the area is executed;
112 +* **Timing script**: periodically execute the code.
150 150  
151 -**(2) Rising script**: after the button is raised, the code chunk in the area is executed;
114 +(% class="box infomessage" %)
115 +(((
116 +**✎Note: **
152 152  
153 -**(3)** **Timing script**: periodically execute the code.
118 +* The timing script is not commonly used because it is similar to the screen background script function. It is not recommended to write and call function here. It should be written in Script_Screen.lua, which is convenient for editing, searching, and replacing operations.
119 +* It is not allowed to write endless loops or call functions that will loop enlessly in the widget. Once used, the entire screen will be stuck, and any buttons will no longer be used. We have to restart the screen to restore.
120 +)))
154 154  
155 -**✎Note: **The timing script is not commonly used because it is similar to the screen background script function. It is not recommended to write and call function here. It should be written in Script_Screen.lua, which is convenient for editing, searching, and replacing operations.
122 += **Platform** =
156 156  
157 -**Warning:** It is not allowed to write endless loops or call functions that will loop enlessly in the widget. Once used, the entire screen will be stuck, and any buttons will no longer be used. We have to restart the screen to restore.
124 +== File header identification ==
158 158  
159 -{{id name="_Toc42782943"/}}{{id name="_Toc16937"/}}{{id name="_Toc18999"/}}**3 Platform**
160 -
161 -{{id name="_Toc17954"/}}{{id name="_Toc18609"/}}{{id name="_Toc42782944"/}}**3.1 File header identification**
162 -
163 163  Any new file will automatically generate a line of code, as shown below:
164 164  
165 165  (% style="text-align:center" %)
166 -[[image:HMI Lua Editor user manual_html_40558d3555455933.png||class="img-thumbnail" height="116" width="270"]]
129 +[[image:HMI Lua Editor user manual_html_40558d3555455933.png||height="116" width="270" class="img-thumbnail"]]
167 167  
168 168  The format of this variable: file name_limits = number
169 169  
... ... @@ -170,64 +170,60 @@
170 170  The file name comes from the newly created file name (without suffix):
171 171  
172 172  (% style="text-align:center" %)
173 -[[image:HMI Lua Editor user manual_html_52fb935bcc76d7ad.png||class="img-thumbnail" height="113" width="203"]]
136 +[[image:HMI Lua Editor user manual_html_52fb935bcc76d7ad.png||height="113" width="203" class="img-thumbnail"]]
174 174  
175 -**_limits**:Fixed format, cannot be modified
138 +* **_limits: **Fixed format, cannot be modified
139 +* **Description**:
140 +* **Tens digit=1**: indicates that the function or global variable defined in this file can be used in the background code file (Script_BG.lua). If it is 0, it is not available.
141 +* **Ones digit=1**: indicates that the functions or global variables defined in this file can be used in the screen code file (Script_Screen.lua) or LuaScript components. If it is 0, it is not available.
176 176  
177 -**Description**:
178 -
179 -**Tens digit=1**:indicates that the function or global variable defined in this file can be used in the background code file (Script_BG.lua). If it is 0, it is not available.
180 -
181 -**Ones digit=1**:indicates that the functions or global variables defined in this file can be used in the screen code file (Script_Screen.lua) or LuaScript components. If it is 0, it is not available.
182 -
143 +(% class="box infomessage" %)
144 +(((
183 183  **✎Note: **If you rename the file manually, you must modify the limits in the file accordingly, otherwise the script file will not take effect.
146 +)))
184 184  
185 -**{{id name="_Toc42782945"/}}3.2 Special script file**
148 +== Built-In script file ==
186 186  
187 187  As mentioned above, the first new build will automatically generate two script files. These two files cannot be deleted and renamed, which will be explained in detail here.
188 188  
189 -~1. Script_BG.lua is a global background script file, which includes initialization and polling functions
152 +**Script_BG.lua** is a global background script file, which includes initialization and polling functions
190 190  
191 191  (% style="text-align:center" %)
192 -[[image:HMI Lua Editor user manual_html_5eb1ad5776f18955.png||class="img-thumbnail" height="148" width="200"]]
155 +[[image:HMI Lua Editor user manual_html_5eb1ad5776f18955.png||height="148" width="200" class="img-thumbnail"]]
193 193  
194 -we_bg_init(): global initialization, that is, execute once when power on.
157 +* we_bg_init(): global initialization, that is, execute once when power on.
158 +* we_bg_poll(): Global polling, that is, the cycle is repeated after power on.
195 195  
196 -we_bg_poll():Global polling, that is, the cycle is repeated after power on.
160 +Script_Screen.lua is a screen script file, and each screen has an initialization, polling, and closing functions
197 197  
198 -1. Script_Screen.lua is a screen script file, and each screen has an initialization, polling, and closing functions
199 -
200 200  (% style="text-align:center" %)
201 -[[image:HMI Lua Editor user manual_html_e6807eb0da0eedd4.png||class="img-thumbnail" height="231" width="300"]]
163 +[[image:HMI Lua Editor user manual_html_e6807eb0da0eedd4.png||height="231" width="300" class="img-thumbnail"]]
202 202  
203 -we_scr_init_0(): execute this function when screen 0 is initialized
165 +* we_scr_init_0(): execute this function when screen 0 is initialized
166 +* we_scr_poll_0(): when in picture 0, the function is executed repeatedly
167 +* we_scr_close_0(): execute this function when screen 0 is closed
204 204  
205 -we_scr_poll_0(): when in picture 0, the function is executed repeatedly
206 -
207 -we_scr_close_0(): execute this function when screen 0 is closed
208 -
209 209  The above is the initialization function of screen 0. For screen 100, you can replace 0 with 100. You can add your own function: function we_scr_init_100() end
210 210  
171 +(% class="box infomessage" %)
172 +(((
211 211  **✎Note: **The above function has a fixed name. If it is modified, it will not cause crash, but the function can be found.
174 +)))
212 212  
213 -Example: After the screen 0 initialization function is changed to we_scR_init_0(), the script will not be executed when screen 0 is initialized, and then no script will be executed, and nothing will be done because the "we_scr_init_0" function cannot be found.
176 +**Example:** After the screen 0 initialization function is changed to we_scR_init_0(), the script will not be executed when screen 0 is initialized, and then no script will be executed, and nothing will be done because the "we_scr_init_0" function cannot be found.
214 214  
215 -{{id name="_Toc42782946"/}}{{id name="_Toc21777"/}}{{id name="_Toc1844"/}}**3.2** **Script type (operating mechanism)**
178 +== Script type (operating mechanism) ==
216 216  
217 217  There are two types of scripts:
218 218  
219 -Active: script automatically executed by HMI
182 +* Active: script automatically executed by HMI
183 +* Passive: Script executed only after user trigger
220 220  
221 -Passive: Script executed only after user trigger
222 -
223 223  The active types includes:
224 224  
225 -Global _bg_ related functions in Script_BG.lua
187 +* Global _bg_ related functions in Script_BG.lua
188 +* Screen _scr_ related functions in Script_Screen.lua
226 226  
227 -Screen _scr_ related functions in Script_Screen.lua
228 -
229 229  Polling script in Lua object
230 230  
231 -The passive type scripts include:
232 -
233 -The rising and falling script in Lua object.
192 +The passive type scripts include: The rising and falling script in Lua object.
XWiki.XWikiComments[0]
Date
... ... @@ -1,0 +1,1 @@
1 +2025-03-09 17:22:36.258
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.nhunguyen95
Comment
... ... @@ -1,0 +1,1 @@
1 +hello, i am using wecon pi3070ig monitor product and i want to use it to control thermal monitoring device. my thermal monitoring device will send thermal information via rs485 communication when receiving thermal reading command with syntax: "STX001,RRD,01,0100", the returned information will be in the form of "STX001,RRD,OK,(thermal parameter)". i want to ask if i can use lua script to send the above data reading command?