Wiki source code of Lua Script
Last modified by Jett on 2025/06/07 17:31
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | = Reference = | ||
2 | |||
3 | Before using the Lua script function, you should understand the basic grammar, and master how to use the script. | ||
4 | |||
5 | For the description of Lua syntax and functions, please refer to [[Lua Reference Manual>>url:http://www.lua.org/manual/5.3/]]. | ||
6 | |||
7 | == Common function == | ||
8 | |||
9 | Here is a list of commonly used functions to facilitate quick search | ||
10 | |||
11 | * basic Basic functions: English | ||
12 | * string String processing: English | ||
13 | * table Table processing: English | ||
14 | * math Mathematical calculation: English | ||
15 | |||
16 | == Built-In library == | ||
17 | |||
18 | Removed the Lua built-in functions (Including libraries), the details are as follows | ||
19 | |||
20 | * Completely remove: debug | ||
21 | * Partial removal: | ||
22 | |||
23 | |=io|=os|=basic | ||
24 | |io.close|os.clock|--dofile-- | ||
25 | |--io.flush--|os.date|--loadfile-- | ||
26 | |--io.input--|os.difftime| | ||
27 | |--io.lines--|--os.execute--| | ||
28 | |io.open|--os.exit--| | ||
29 | |--io.output--|--os.getenv--| | ||
30 | |--io.popen--|os.remove| | ||
31 | |--io.read--|os.rename| | ||
32 | |--io.stderr--|--os.setlocale--| | ||
33 | |--io.stdin--|os.time| | ||
34 | |--io.stdout--|--os.tmpname--| | ||
35 | |--io.tmpfile--| | | ||
36 | |io.type| | | ||
37 | |--io.write--| | | ||
38 | |||
39 | (% class="box infomessage" %) | ||
40 | ((( | ||
41 | **✎Note:** The io/os library cannot write to the internal Flash | ||
42 | ))) | ||
43 | |||
44 | = Lua extension = | ||
45 | |||
46 | On the basis of Lua, combined with the characteristics of HMI, some functional expansions have been made to assist in completing some more complex business requirements | ||
47 | |||
48 | == General class == | ||
49 | |||
50 | |=(% style="width: 264px;" %)Function|=(% style="width: 318px;" %)Overview|=(% style="width: 123px;" %)i/HMI+ series|=(% style="width: 122px;" %)ie series|=(% style="width: 101px;" %)ig series|=(% style="width: 103px;" %)HMI V2.0 | ||
51 | |(% style="width:264px" %)1.1 Basic Functions|(% style="width:318px" %)Address Read&Write, screen operations, etc.|(% style="width:123px" %)√|(% style="width:122px" %)√|(% style="width:101px" %)√|(% style="width:103px" %)√ | ||
52 | |(% style="width:264px" %)1.2 Lua Object Operation|(% style="width:318px" %)Object status and property control|(% style="width:123px" %)√|(% style="width:122px" %)√|(% style="width:101px" %)√|(% style="width:103px" %)√ | ||
53 | |(% style="width:264px" %)1.3 Timer|(% style="width:318px" %)Cycle execution function|(% style="width:123px" %)×|(% style="width:122px" %)√|(% style="width:101px" %)√|(% style="width:103px" %)√ | ||
54 | |(% style="width:264px" %)1.4 Trigger|(% style="width:318px" %)Trigger execution function|(% style="width:123px" %)×|(% style="width:122px" %)√|(% style="width:101px" %)√|(% style="width:103px" %)√ | ||
55 | |(% style="width:264px" %)1.5 Flash Read&Write|(% style="width:318px" %)Custom files Read&Write|(% style="width:123px" %)×|(% style="width:122px" %)√|(% style="width:101px" %)√|(% style="width:103px" %)√ | ||
56 | |(% style="width:264px" %)1.6 Other functions|(% style="width:318px" %)-|(% style="width:123px" %)√|(% style="width:122px" %)√|(% style="width:101px" %)√|(% style="width:103px" %)√ | ||
57 | |(% style="width:264px" %)4.2 LuaSqlite|(% style="width:318px" %)Lua sqlite database operation|(% style="width:123px" %)√|(% style="width:122px" %)√|(% style="width:101px" %)√|(% style="width:103px" %)√ | ||
58 | |||
59 | == Custom object class == | ||
60 | |||
61 | Realize the drawing and display of charts through **custom parts + functions** | ||
62 | |||
63 | |||
64 | |=Function|=Overview|=i/HMI+ series|=ie series|=ig series|=HMI V2.0 | ||
65 | |2.1 Table Drawing|-|×|√|√|√ | ||
66 | |2.2 Curve Drawing|-|×|√|√|√ | ||
67 | |||
68 | == Network communication == | ||
69 | |||
70 | |=Function|=Overview|=i/HMI+ series|=ie series|=ig series|=HMI V2.0 | ||
71 | |3.1 MQTT|MQTT Client|×|×|√|√ | ||
72 | |3.2 Cloud Interface|Get the data of tags from Cloud Function|×|×|√|√ | ||
73 | |3.3 json|Conversion between json string and lua data|×|×|√|√ | ||
74 | |3.4 wlan|WLAN configuration|×|×|√|√ | ||
75 | |3.5 ftpserver|Relevant functions for FTP server|×|×|√|√ | ||
76 | |3.6 sim|SIM card information|×|×|√|√ | ||
77 | |3.7 sms|Relevant functions for SIM card sending SMS|×|×|√|√ | ||
78 | |4.1 LuaSocket|Network modules programming|√|√|√|√ | ||
79 | |||
80 | = Debug = | ||
81 | |||
82 | At present, it can only be debugged by the printing function print. There are two situations for debugging. | ||
83 | |||
84 | |=Category|=Overview|=i/HMI+ series|=ie series|=ig series|=HMI V2.0 | ||
85 | |Simulation debugging|View the print information through the black window created by the simulation operation|√|√|√|√ | ||
86 | |HMI debugging|Create a small independent window at the top of the screen to view the print information|×|√|√|√ | ||
87 | |||
88 | (% class="wikigeneratedid" id="HHowtouseHMIdebugging" %) | ||
89 | **How to use HMI debugging** | ||
90 | |||
91 | * Open the debug window, add the print_debug(1) in falling script to new Lua object | ||
92 | * Close the debug window, add the print_debug(0) in falling script to new Lua object | ||
93 | |||
94 | (% class="box infomessage" %) | ||
95 | ((( | ||
96 | **✎Note:** | ||
97 | |||
98 | * When using HMI debugging, if the printing is too frequent and the quantity is large, it may cause the screen to stuck or freeze | ||
99 | * If you have completed the debugging phase of development, you can close the debugging window to reduce the performance consumption of HMI debugging | ||
100 | ))) | ||
101 | |||
102 | = Coding environment = | ||
103 | |||
104 | Like the Lua standard, the encoding environment of the simulator and the HMI is unified with the encoding of UTF8, including the definition and processing of strings | ||
105 | |||
106 | If you need to print strings in Chinese or other languages into the debugging window, please use we_u8ta for transcoding output | ||
107 | |||
108 | = Appendix: editor shortcut hotkeys = | ||
109 | |||
110 | |=Modifier Key|=Function Key|=Function | ||
111 | |Ctrl|A|Select all | ||
112 | |Ctrl|B|Insert bit address | ||
113 | |Ctrl|C|Copy | ||
114 | |Ctrl|D|Copy cursor line | ||
115 | |Ctrl|E|When the cursor is inside (outside) a pair of brackets, the cursor switches position inside (outside) | ||
116 | |Shift+Ctrl|E|When the cursor is inside (outside) a pair of brackets, select the text inside (outside) the brackets | ||
117 | |Ctrl|F|Find | ||
118 | |Ctrl|H|Replace | ||
119 | |Ctrl|L|Delete cursor line | ||
120 | |Shift+Ctrl|L|Lowercase the selected content | ||
121 | |Ctrl|S|Save | ||
122 | |Ctrl|T|Swap the cursor line and the previous line position | ||
123 | |Shift+Ctrl|U|Uppercase the selected content | ||
124 | |Ctrl|V|Paste | ||
125 | |Ctrl|W|Insert word address | ||
126 | |Ctrl|X|Cut | ||
127 | |Ctrl|Y|Redo | ||
128 | |Ctrl|Z|Undo | ||
129 | |None|Arrow key up ↑|Move cursor up | ||
130 | |Shift|Arrow key up ↑|Up area selection | ||
131 | |Shift+Alt|Arrow key up ↑|Up vertical area selection | ||
132 | |Ctrl|Arrow keys up ↑|Window slide up | ||
133 | |Shift+Ctrl|Arrow key up ↑|Move selected line up | ||
134 | |None|Arrow key down ↓|Move cursor down | ||
135 | |Shift|Arrow key down ↓|Down area selection | ||
136 | |Shift+Alt|Arrow key down ↓|Down vertical area selection | ||
137 | |Ctrl|Arrow key down ↓|Window down | ||
138 | |Shift+Ctrl|Arrow key down ↓|Move selected line down | ||
139 | |None|Left arrow key ←|Move cursor to the left | ||
140 | |Shift|Left arrow key ←|Select a symbol to the left | ||
141 | |Shift+Alt|Left Arrow ←|Select a symbol to the left | ||
142 | |Ctrl|Left Arrow ←|Cursor cross over a word to the left | ||
143 | |Shift+Ctrl|Left Arrow ←|Select a word to the left | ||
144 | |None|Right arrow key →|Move the cursor to the right | ||
145 | |Shift|Right arrow key →|Select a symbol to the right | ||
146 | |Shift+Alt|Right Arrow →|Select a symbol to the right | ||
147 | |Ctrl|Right Arrow →|Cursor cross over a word to the right | ||
148 | |Shift+Ctrl|Right Arrow →|Select a word to the right | ||
149 | |Ctrl|Numeric keyboard plus +|Zoom in the display | ||
150 | |Ctrl|Numeric keyboard minus-|Zoom out the display | ||
151 | |Ctrl|Slash'?/'|Select content comment | ||
152 | |None|Tab|Indent 4 spaces | ||
153 | |Shift|Tab|Reduce 4 spaces | ||
154 | |None|Home|Move the cursor to the beginning of the line | ||
155 | |Shift|Home|Select the cursor position to the beginning of the line | ||
156 | |None|End|Move the cursor to the end of the line | ||
157 | |Shift|End|Select the cursor position to the end of the line | ||
158 | |None|PageUp|The cursor moves up one page according to the size of the window | ||
159 | |Shift|PageUp|Select the cursor position to the previous page | ||
160 | |None|PageDown|The cursor moves down one page according to the size of the window | ||
161 | |Shift|PageDown|Select the cursor position to the next page | ||
162 | |None|Insert|Switch input mode | ||
163 | | | |The following delete operation | ||
164 | |None|Delete|Delete one symbol backward | ||
165 | |Ctrl|Backspace|Delete the content of the word range to the left of the cursor | ||
166 | |Ctrl|Delete|Delete the content of the word range to the right of the cursor | ||
167 | |Ctrl+Shift|Backspace|Delete all content to the left of the cursor | ||
168 | |Ctrl+Shift|Delete|Delete all content to the right of the cursor | ||
169 | | | |The following paragraph operations, span: paragraph length | ||
170 | |Ctrl|[|Move cursor up | ||
171 | |Ctrl+Shift|[|Move cursor up and select area | ||
172 | |Ctrl|]|Move cursor down | ||
173 | |Ctrl+Shift|]|Move cursor down and select area | ||
174 | |||
175 | = Basic function = | ||
176 | |||
177 | == Address class == | ||
178 | |||
179 | How to read or write address | ||
180 | |||
181 | |=Function|=Description | ||
182 | |[[we_bas_getshort/addr_getshort>>doc:||anchor="Hwe_bas_getshort"]]|Read single-word(16bits) signed Decimal | ||
183 | |[[we_bas_setshort/addr_setshort>>||anchor="Hwe_bas_setshort"]]|Write single-word(16bits) signed Decimal | ||
184 | |[[we_bas_getword/addr_getword>>||anchor="Hwe_bas_getword" ancord="Hwe_bas_getword"]]|Read single-word(16bits) unsigned Decimal | ||
185 | |[[we_bas_setword/addr_setword>>||anchor="Hwe_bas_setword" ancord="Hwe_bas_setword"]]|Write single-word(16bits) unsigned Decimal | ||
186 | |[[we_bas_getint/addr_getint>>||anchor="Hwe_bas_getint"]]|Read double-word(32bits) signed Decimal | ||
187 | |[[we_bas_setint/addr_setint>>||anchor="Hwe_bas_getint"]]|Write double-word(32bits) signed Decimal | ||
188 | |[[we_bas_getdword/addr_getdword>>||anchor="Hwe_bas_getint" ancwor="Hwe_bas_getdword"]]|Read double-word(32bits) unsigned Decimal | ||
189 | |[[we_bas_setdword/addr_setdword>>||anchor="Hwe_bas_setdword" ancwor="Hwe_bas_setdword"]]|Write double-word(32bits) unsigned Decimal | ||
190 | |[[we_bas_getfloat/addr_getfloat>>||anchor="Hwe_bas_getfloat" ancloa="Hwe_bas_getfloat"]]|Read 32bits Floating | ||
191 | |[[we_bas_setfloat/addr_setfloat>>||anchor="Hwe_bas_setfloat" ancloa="Hwe_bas_setfloat"]]|Write 32bits Floating | ||
192 | |[[we_bas_getdouble/addr_getdouble>>||anchor="Hwe_bas_getdouble" ancoub="Hwe_bas_getdouble"]]|Read 64bits Floating | ||
193 | |[[we_bas_setdouble/addr_setdouble>>||anchor="Hwe_bas_setdouble" ancoub="Hwe_bas_setdouble"]]|Write 64bits Floating | ||
194 | |[[we_bas_getbit/addr_getbit>>||anchor="Hwe_bas_getbit"]]|Read one bit | ||
195 | |[[we_bas_setbit/addr_setbit>>||anchor="Hwe_bas_setbit"]]|Write one bit | ||
196 | |[[we_bas_getstring/addr_getstring>>||anchor="Hwe_bas_setbit" anctri="Hwe_bas_getstring"]]|Read string | ||
197 | |[[we_bas_setstring/addr_setstring>>||anchor="Hwe_bas_setbit" anctri="Hwe_bas_setstring"]]|Write string | ||
198 | |[[we_bas_bmov/addr_bmov>>||anchor="Hwe_bas_bmov"]]|Continuous address copy | ||
199 | |[[we_bas_fill/addr_fill>>||anchor="Hwe_bas_fill"]]|Consecutive address assignment | ||
200 | |[[we_bas_newnoaddr/addr_newnoaddr>>||anchor="Hwe_bas_newnoaddr" ancoad="Hwe_bas_newnoaddr"]]|External address offset | ||
201 | |[[we_bas_newstataddr/addr_newstataddr>>||anchor="Hwe_bas_newstataddr" anctat="Hwe_bas_newstataddr"]]|External station number offset | ||
202 | |[[we_bas_getdatas>>||anchor="Hwe_bas_getdatas" ancoad="Hwe_bas_newnoaddr"]]|continous read address | ||
203 | |[[we_bas_setdatas>>||anchor="Hwe_bas_setdatas" anctat="Hwe_bas_newstataddr"]]|continous write address | ||
204 | |||
205 | == Path class == | ||
206 | |||
207 | Usage Notice | ||
208 | |||
209 | |=Function|=Description | ||
210 | |[[we_bas_getscriptpath>>||anchor="Hwe_bas_getscriptpath"]](HMI)|Get project script path | ||
211 | |[[we_bas_getcustompath>>||anchor="Hwe_bas_getcustompath"]](HMI)|Get the available internal flash path | ||
212 | |[[we_bas_getusbpath>>||anchor="Hwe_bas_getusbpath"]](HMI)|Detect USB | ||
213 | |[[we_bas_getsdpath>>||anchor="Hwe_bas_getsdpath"]](HMI)|Detect SD card | ||
214 | |[[we_bas_file_exist>>||anchor="Hwe_bas_file_exist"]](HMI)|Does the file exist | ||
215 | |[[we_bas_path_exist>>||anchor="Hwe_bas_path_exist"]](HMI)|Does the folder exist | ||
216 | |||
217 | == Screen class == | ||
218 | |||
219 | |=Function|=Description | ||
220 | |[[we_bas_jumpscreen>>||anchor="Hwe_bas_jumpscreen"]](HMI)|Main screen jump | ||
221 | |[[we_bas_popscreen>>||anchor="Hwe_bas_popscreen"]](HMI)|Pop up sub-screen | ||
222 | |[[we_bas_closescreen>>||anchor="Hwe_bas_closescreen"]](HMI)|Close sub-screen | ||
223 | |||
224 | == Others == | ||
225 | |||
226 | |=Function|=Description | ||
227 | |[[we_bas_beep>>||anchor="Hwe_bas_beep"]]|Beep | ||
228 | |[[we_bas_gettickcount>>||anchor="Hwe_bas_gettickcount"]]|Get milliseconds that have passed since the operating system was started | ||
229 | |[[we_bas_sleep>>||anchor="Hwe_bas_sleep"]]|Sleep | ||
230 | |[[we_atu8>>||anchor="Hwe_atu8"]]|Character set conversion, windows to utf8 | ||
231 | |[[we_u8ta>>||anchor="Hwe_u8ta"]]|Character set conversion, utf8 to windows | ||
232 | |[[we_bas_getmachineinfo>>||anchor="Hwe_bas_getmachineinfo"]]|Get HMI machine information | ||
233 | |||
234 | = Address class = | ||
235 | |||
236 | (% class="wikigeneratedid" id="HHowtoreadorwriteaddress" %) | ||
237 | **How to read or write address** | ||
238 | |||
239 | Let HDW0 store a 32-bit integer value, the macro instruction reads/writes the address like following: | ||
240 | |||
241 | {{code language="Basic"}} | ||
242 | @W_HDW0 = 1 'Write 1 to address HDW0 | ||
243 | Dim a as integer | ||
244 | a = @W_HDW0 'Read address HDW0 to variable a | ||
245 | {{/code}} | ||
246 | |||
247 | How to do in Lua: | ||
248 | |||
249 | {{code language="Lua"}} | ||
250 | -- The following is the wrong way to write | ||
251 | -- Mistake 1: Attempt to assign a value to a string | ||
252 | "@W_HDW0" = 1 -- The address here is a string, and an error will be reported when compiling the project | ||
253 | -- Mistake 2: Attempt to assign a value to a variable | ||
254 | W_HDW0 = 1 -- The address here is a variable, so the assignment will not be written to the real address | ||
255 | |||
256 | -- The following is the correct way to write | ||
257 | we_bas_setint("@W_HDW0", 1) - write 1 to the address HDW0 | ||
258 | local a = we_bas_getint("@W_HDW0") - read address HDW0 to variable a | ||
259 | {{/code}} | ||
260 | |||
261 | * Same point: The address is expressed in the same form, but Lua needs to be enclosed in double quotation marks. The address read and write functions, its address parameters are the same as VB, or use the combination key ctrl+W in the editor to insert the word address, ctrl+BInsert bit address | ||
262 | * Different point: In Lua, you need to **call the interface function** to complete the operation address, there is no **equal sign assignment** method like macro instruction | ||
263 | |||
264 | You can find all the interface functions available for reading /writing address operations from this list>>path:#listaddress]] | ||
265 | |||
266 | == [[we_bas_getshort>>||anchor="HAddressclass"]] == | ||
267 | |||
268 | Prototype: we_bas_getshort(address [, type]) | ||
269 | |||
270 | Function: Read single-word(16bits) signed Decimal | ||
271 | |||
272 | Parameters: | ||
273 | |||
274 | * address(string):address | ||
275 | * type(number):type, type=1 is for read-through | ||
276 | |||
277 | Return: | ||
278 | |||
279 | * Success: Single word signed decimal value | ||
280 | * Failure: nil | ||
281 | |||
282 | Example: | ||
283 | |||
284 | {{code language="Lua"}} | ||
285 | print(we_bas_getshort("@W_HDW0")) | ||
286 | {{/code}} | ||
287 | |||
288 | == [[we_bas_setshort>>||anchor="HAddressclass"]] == | ||
289 | |||
290 | Prototype: we_bas_setshort(address, value [, type]) | ||
291 | |||
292 | Function: Write single-word(16bits) signed Decimal | ||
293 | |||
294 | Parameters: | ||
295 | |||
296 | * address(string):address | ||
297 | * value(number):value | ||
298 | * type(number):type,type=1 is for write-through | ||
299 | |||
300 | Return: | ||
301 | |||
302 | * Success: true | ||
303 | * Failure: nil | ||
304 | |||
305 | Example: | ||
306 | |||
307 | {{code language="Lua"}} | ||
308 | we_bas_setshort("@W_HDW0", 123) | ||
309 | {{/code}} | ||
310 | |||
311 | == [[we_bas_getword>>||anchor="HAddressclass"]] == | ||
312 | |||
313 | Prototype: we_bas_getword(address [, type]) | ||
314 | |||
315 | Function: Read single-word(16bits) unsigned Decimal | ||
316 | |||
317 | Parameters: | ||
318 | |||
319 | * address(string):address | ||
320 | * type(number):type,type=1 is for read-through | ||
321 | |||
322 | Return: | ||
323 | |||
324 | * Success: single-word(16bits) unsigned Decimal value | ||
325 | * Failure: nil | ||
326 | |||
327 | Example: | ||
328 | |||
329 | {{code language="Lua"}} | ||
330 | print(we_bas_getword("@W_HDW0")) | ||
331 | {{/code}} | ||
332 | |||
333 | == [[we_bas_setword>>||anchor="HAddressclass"]] == | ||
334 | |||
335 | Prototype: we_bas_setword(address, value [, type]) | ||
336 | |||
337 | Function: Write single-word(16bits) unsigned Decimal | ||
338 | |||
339 | Parameters: | ||
340 | |||
341 | * address(string):address | ||
342 | * value(number):value | ||
343 | * type(number):type,type = 1 is for write-through | ||
344 | |||
345 | Return: | ||
346 | |||
347 | * Success: true | ||
348 | * Failure: nil | ||
349 | |||
350 | Example: | ||
351 | |||
352 | {{code language="Lua"}} | ||
353 | we_bas_setword("@W_HDW0", 123) | ||
354 | {{/code}} | ||
355 | |||
356 | == [[we_bas_getint>>||anchor="HAddressclass"]] == | ||
357 | |||
358 | Prototype: we_bas_getint(address [, type]) | ||
359 | |||
360 | Function: Read double-word(32bits) signed Decimal | ||
361 | |||
362 | Parameters: | ||
363 | |||
364 | * address(string):address | ||
365 | * type(number):type,type=1 is for read-through | ||
366 | |||
367 | Return: | ||
368 | |||
369 | * Success: Read double-word(32bits) signed Decimal value | ||
370 | * Failure: nil | ||
371 | |||
372 | Example: | ||
373 | |||
374 | {{code language="Lua"}} | ||
375 | print(we_bas_getint("@W_HDW0")) | ||
376 | {{/code}} | ||
377 | |||
378 | == [[we_bas_setint>>||anchor="HAddressclass"]] == | ||
379 | |||
380 | Prototype: we_bas_setint(address, value [, type]) | ||
381 | |||
382 | Function: Write double-word(32bits) signed Decimal | ||
383 | |||
384 | Parameters: | ||
385 | |||
386 | * address(string):address | ||
387 | * value(number):value | ||
388 | * type(number):type,type=1 is for write-through | ||
389 | |||
390 | Return: | ||
391 | |||
392 | * Success: true | ||
393 | * Failure: nil | ||
394 | |||
395 | Example: | ||
396 | |||
397 | {{code language="Lua"}} | ||
398 | we_bas_setint("@W_HDW0", -111) | ||
399 | {{/code}} | ||
400 | |||
401 | == [[we_bas_getdword>>||anchor="HAddressclass"]] == | ||
402 | |||
403 | Prototype: we_bas_getdword(address [, type]) | ||
404 | |||
405 | Function: Read double-word(32bits) unsigned Decimal | ||
406 | |||
407 | Parameters: | ||
408 | |||
409 | * address(string):address | ||
410 | * type(number):type,type=1 is for read-through | ||
411 | |||
412 | Return: | ||
413 | |||
414 | * Success: Read double-word(32bits) unsigned Decimal value | ||
415 | * Failure: nil | ||
416 | |||
417 | Example: | ||
418 | |||
419 | {{code language="Lua"}} | ||
420 | print(we_bas_getdword("@W_HDW0")) | ||
421 | {{/code}} | ||
422 | |||
423 | == [[we_bas_setdword>>||anchor="HAddressclass"]] == | ||
424 | |||
425 | Prototype: we_bas_setdword(address, value [, type]) | ||
426 | |||
427 | Function: Write double-word(32bits) unsigned Decimal | ||
428 | |||
429 | Parameters: | ||
430 | |||
431 | * address(string):address | ||
432 | * value(number):value | ||
433 | * type(number):type,type=1为write-through | ||
434 | |||
435 | Return: | ||
436 | |||
437 | * Success: true | ||
438 | * Failure: nil | ||
439 | |||
440 | Example: | ||
441 | |||
442 | {{code language="Lua"}} | ||
443 | we_bas_setdword("@W_HDW0", 12345678) | ||
444 | {{/code}} | ||
445 | |||
446 | == [[we_bas_getfloat>>||anchor="HAddressclass"]] == | ||
447 | |||
448 | Prototype: we_bas_getfloat(address [, type]) | ||
449 | |||
450 | Function: Read 32bits Floating | ||
451 | |||
452 | Parameters: | ||
453 | |||
454 | * address(string):address | ||
455 | * type(number):type,type=1 is for read-through | ||
456 | |||
457 | Return: | ||
458 | |||
459 | * Success: 32bits Floating value | ||
460 | * Failure: nil | ||
461 | |||
462 | Example: | ||
463 | |||
464 | {{code language="Lua"}} | ||
465 | print(we_bas_getfloat("@W_HDW0")) | ||
466 | {{/code}} | ||
467 | |||
468 | == [[we_bas_setfloat>>||anchor="HAddressclass"]] == | ||
469 | |||
470 | Prototype: we_bas_setfloat(address, value [, type]) | ||
471 | |||
472 | Function: Write 32bits Floating | ||
473 | |||
474 | Parameters: | ||
475 | |||
476 | * address(string):address | ||
477 | * value(number):value | ||
478 | * type(number):type,type=1 is for write-through | ||
479 | |||
480 | Return: | ||
481 | |||
482 | * Success: true | ||
483 | * Failure: nil | ||
484 | |||
485 | Example: | ||
486 | |||
487 | {{code language="Lua"}} | ||
488 | we_bas_setfloat("@W_HDW0", 123.456) | ||
489 | {{/code}} | ||
490 | |||
491 | == [[we_bas_getdouble>>||anchor="HAddressclass"]] == | ||
492 | |||
493 | Prototype: we_bas_getdouble(address [, type]) | ||
494 | |||
495 | Function: Read 64bits Floating | ||
496 | |||
497 | Parameters: | ||
498 | |||
499 | * address(string):address | ||
500 | * type(number):type,type=1is for read-through | ||
501 | |||
502 | Return: | ||
503 | |||
504 | * Success: Read 64bits Floating value | ||
505 | * Failure: nil | ||
506 | |||
507 | Example: | ||
508 | |||
509 | {{code language="Lua"}} | ||
510 | print(we_bas_getdouble("@W_HDW0")) | ||
511 | {{/code}} | ||
512 | |||
513 | == [[we_bas_setdouble>>||anchor="HAddressclass"]] == | ||
514 | |||
515 | Prototype: we_bas_setdouble(address, value [, type]) | ||
516 | |||
517 | Function: Write 64bits Floating | ||
518 | |||
519 | Parameters: | ||
520 | |||
521 | * address(string):address | ||
522 | * value(number):value | ||
523 | * type(number):type,type=1为write-through | ||
524 | |||
525 | Return: | ||
526 | |||
527 | * Success: true | ||
528 | * Failure: nil | ||
529 | |||
530 | Example: | ||
531 | |||
532 | {{code language="Lua"}} | ||
533 | we_bas_setdouble("@W_HDW0", 1.23456) | ||
534 | {{/code}} | ||
535 | |||
536 | == [[we_bas_getbit>>||anchor="HAddressclass"]] == | ||
537 | |||
538 | Prototype: we_bas_getbit(address [, type]) | ||
539 | |||
540 | Function: Read one bit | ||
541 | |||
542 | Parameters: | ||
543 | |||
544 | * address(string):address | ||
545 | * type(number):type,type=1is for read-through | ||
546 | |||
547 | Return: | ||
548 | |||
549 | * Success: Single-word signed decimal value | ||
550 | * Failure: nil | ||
551 | |||
552 | Example: | ||
553 | |||
554 | {{code language="Lua"}} | ||
555 | print(we_bas_getbit("@B_HDX0.0")) | ||
556 | {{/code}} | ||
557 | |||
558 | == [[we_bas_setbit>>||anchor="HAddressclass"]] == | ||
559 | |||
560 | Prototype: we_bas_setbit(address, value [, type]) | ||
561 | |||
562 | Function: Write one bit | ||
563 | |||
564 | Parameters: | ||
565 | |||
566 | * address(string):address | ||
567 | * value(number):value,value=0 is off,value=1 is on | ||
568 | * type(number):type,type=1 is for write-through | ||
569 | |||
570 | Return: | ||
571 | |||
572 | * Success: true | ||
573 | * Failure: nil | ||
574 | |||
575 | Example: | ||
576 | |||
577 | {{code language="Lua"}} | ||
578 | we_bas_setbit("@B_HDX0.0", 1) | ||
579 | {{/code}} | ||
580 | |||
581 | == [[we_bas_getstring>>||anchor="HAddressclass"]] == | ||
582 | |||
583 | Prototype: we_bas_getstring(address, len) | ||
584 | |||
585 | Function: Read string | ||
586 | |||
587 | Parameters: | ||
588 | |||
589 | * address(string):address | ||
590 | * len(number):length,Unit: byte | ||
591 | |||
592 | Return: | ||
593 | |||
594 | * Success: the designated length of the string | ||
595 | * Failure: nil | ||
596 | |||
597 | Example: | ||
598 | |||
599 | {{code language="Lua"}} | ||
600 | print(we_bas_getstring("@W_HDW0", 10)) | ||
601 | {{/code}} | ||
602 | |||
603 | == [[we_bas_setstring>>||anchor="HAddressclass"]] == | ||
604 | |||
605 | Prototype: we_bas_setstring(address, str [, len]) | ||
606 | |||
607 | Function: Write string | ||
608 | |||
609 | Parameters: | ||
610 | |||
611 | * address(string):address | ||
612 | * str(string):string | ||
613 | * len(number):length, if not set, it will be the bytes of the string | ||
614 | |||
615 | Return: | ||
616 | |||
617 | * Success: true | ||
618 | * Failure: nil | ||
619 | |||
620 | Example: | ||
621 | |||
622 | {{code language="Lua"}} | ||
623 | local str = "Hello" | ||
624 | we_bas_setstring("@W_HDW0", str) | ||
625 | {{/code}} | ||
626 | |||
627 | == [[we_bas_bmov>>||anchor="HAddressclass"]] == | ||
628 | |||
629 | Prototype: we_bas_bmov(dstAddress, srcAddress, len[, type]) | ||
630 | |||
631 | Function: Copy data of the designated length from the source address to the target address | ||
632 | |||
633 | Parameters: | ||
634 | |||
635 | * dstAddress(string): Target address | ||
636 | * srcAddress(string): Source address | ||
637 | * len(number): designated length | ||
638 | * type(number): read-write type, if type=1, it means write through | ||
639 | |||
640 | Return: | ||
641 | |||
642 | * Success: true | ||
643 | * Failure: nil | ||
644 | |||
645 | Example: | ||
646 | |||
647 | {{code language="Lua"}} | ||
648 | we_bas_bmov("@W_HDW100", "@W_HDW0", 10) | ||
649 | {{/code}} | ||
650 | |||
651 | == [[we_bas_fill>>||anchor="HAddressclass"]] == | ||
652 | |||
653 | Prototype: we_bas_fill(address, value, len) | ||
654 | |||
655 | Function: Assign the same value to continuous addresses | ||
656 | |||
657 | Parameters: | ||
658 | |||
659 | * address(string):address | ||
660 | * value(number):value | ||
661 | * len(number):continuous length(Unit:word) | ||
662 | |||
663 | Return: | ||
664 | |||
665 | * Success: true | ||
666 | * Failure: nil | ||
667 | |||
668 | Example: | ||
669 | |||
670 | {{code language="Lua"}} | ||
671 | we_bas_fill("@W_HDW0", 0, 10) | ||
672 | {{/code}} | ||
673 | |||
674 | == [[we_bas_newnoaddr>>||anchor="HAddressclass"]] == | ||
675 | |||
676 | Prototype: we_bas_newnoaddr(address, len) | ||
677 | |||
678 | Function: External address offset | ||
679 | |||
680 | Parameters: | ||
681 | |||
682 | * address(string):address | ||
683 | * len(number):length | ||
684 | |||
685 | Return: | ||
686 | |||
687 | * Success: offset address(string) | ||
688 | * Failure: nil | ||
689 | |||
690 | Example: | ||
691 | |||
692 | {{code language="Lua"}} | ||
693 | local x0 = "@W_X0" | ||
694 | for i=1, 10 do | ||
695 | -- address offset x1 ~ x10 | ||
696 | local x_i = we_bas_newnoaddr(x0, i) | ||
697 | -- todo | ||
698 | end | ||
699 | {{/code}} | ||
700 | |||
701 | == [[we_bas_newstataddr>>||anchor="HAddressclass"]] == | ||
702 | |||
703 | Prototype: we_bas_newstataddr(address, len) | ||
704 | |||
705 | Function: External station number offset | ||
706 | |||
707 | Parameters: | ||
708 | |||
709 | * address(string):address | ||
710 | * len(number):length | ||
711 | |||
712 | Return: | ||
713 | |||
714 | * Success: offset address(string) | ||
715 | * Failure: nil | ||
716 | |||
717 | Example: | ||
718 | |||
719 | {{code language="Lua"}} | ||
720 | -- The address 10 of station No.1 is offset by 2 station numbers | ||
721 | local a = we_bas_newstataddr("@W_1:10", 2) | ||
722 | -- Write 20 to address 10 of station No.3 | ||
723 | we_bas_setword(a, 20) | ||
724 | {{/code}} | ||
725 | |||
726 | |||
727 | |||
728 | == [[we_bas_getdatas>>||anchor="HAddressclass"]] == | ||
729 | |||
730 | Prototype: we_bas_getdatas(address,quantity,type[,rwtype,hilorever]) | ||
731 | |||
732 | Function: Read continous address | ||
733 | |||
734 | (% class="wikigeneratedid" %) | ||
735 | **Data type table** | ||
736 | |||
737 | |=Value|=Description | ||
738 | |0|16-bit signed decimal | ||
739 | |1|16-bit unsigned decimal | ||
740 | |2|32-bit signed decimal | ||
741 | |3|32-bit unsigned decimal | ||
742 | |4|32-bit floating point number | ||
743 | |5|64-bit floating point number | ||
744 | |6|bit | ||
745 | |||
746 | Parameters: | ||
747 | |||
748 | * address(string): address | ||
749 | * quantity(number): number | ||
750 | * type(number): data type, please refer to the data type table above | ||
751 | * rwtype(number): Read values directly from the device. When type=1, read values directly from the device. | ||
752 | * hilorever (number): high and low words. When type=1, high words are in front. | ||
753 | |||
754 | Return: | ||
755 | |||
756 | * Success: Return data table | ||
757 | * Failure: nil | ||
758 | |||
759 | Example: | ||
760 | |||
761 | {{code language="lua"}} | ||
762 | a = we_bas_getdatas("@W_HDW0",10,0) --Take @W_HDW0 as the start address, read ten consecutive words in a signed decimal. | ||
763 | for i,v in pairs(a) | ||
764 | print(i,v) | ||
765 | end | ||
766 | b = we_bas_getdatas("@B_HDX10.0",10,6) --Take B_HDX10.0 as the starting address and read ten consecutive bits. | ||
767 | for i,v in pairs(b) | ||
768 | print(i,v) | ||
769 | end | ||
770 | {{/code}} | ||
771 | |||
772 | == [[we_bas_setdatas>>||anchor="HAddressclass"]] == | ||
773 | |||
774 | Prototype: we_bas_setdatas(address,data,quantity,type[,rwtype,hilorever]) | ||
775 | |||
776 | Function: Read continous address | ||
777 | |||
778 | Parameters: | ||
779 | |||
780 | * address(string): address | ||
781 | * quantity(number): Data table name | ||
782 | * type(number): data type, please refer to the following data type table | ||
783 | * rwtype(number): Read-write type, if set type=1, enable write through. | ||
784 | * hilorever (number): high and low words. When type=1, high words are in front. | ||
785 | |||
786 | **Data type table** | ||
787 | |||
788 | |=Value|=Description | ||
789 | |0|16-bit signed decimal | ||
790 | |1|16-bit unsigned decimal | ||
791 | |2|32-bit signed decimal | ||
792 | |3|32-bit unsigned decimal | ||
793 | |4|32-bit floating point number | ||
794 | |5|64-bit floating point number | ||
795 | |6|bit | ||
796 | |||
797 | Return: | ||
798 | |||
799 | * Success: 1 | ||
800 | * Failure: nil | ||
801 | |||
802 | Example: | ||
803 | |||
804 | {{code language="lua"}} | ||
805 | tlb = {1,2,3,4,5,6,7,8,9,10} | ||
806 | we_bas_setdatas("@W_HDW0","tlb",10,0) --Take @W_HDW0 as the start address, write ten consecutive words. | ||
807 | tlbbit = {1,0,1,0,0,1,1,0,1,1} | ||
808 | we_bas_getdatas("@B_HDX10.0","tlbbit",10,0) --Take B_HDX10.0 as the starting address and read ten consecutive bits. | ||
809 | {{/code}} | ||
810 | |||
811 | = Path class = | ||
812 | |||
813 | (% class="wikigeneratedid" id="HUsageNotice" %) | ||
814 | **Usage Notice** | ||
815 | |||
816 | Before using this type of interface,you need to understand the structure of the File System inside the HMI. | ||
817 | |||
818 | == [[we_bas_getscriptpath>>||anchor="HPathclass"]](HMI) == | ||
819 | |||
820 | Prototype: we_bas_getscriptpath() | ||
821 | |||
822 | Function: Get project script path | ||
823 | |||
824 | Parameters: None | ||
825 | |||
826 | Return: | ||
827 | |||
828 | * Success: The path of script(string) | ||
829 | * Failure: nil | ||
830 | |||
831 | Example: | ||
832 | |||
833 | {{code language="Lua"}} | ||
834 | local path = we_bas_getscriptpath() | ||
835 | if path then | ||
836 | -- todo | ||
837 | end | ||
838 | {{/code}} | ||
839 | |||
840 | == [[we_bas_getcustompath>>||anchor="HPathclass"]](HMI) == | ||
841 | |||
842 | Prototype: we_bas_getcustompath() | ||
843 | |||
844 | Function: Get the available internal flash path | ||
845 | |||
846 | Parameters:None | ||
847 | |||
848 | Return: | ||
849 | |||
850 | * Success: Internal Flash path(string) | ||
851 | * Failure: nil | ||
852 | |||
853 | Example: | ||
854 | |||
855 | {{code language="Lua"}} | ||
856 | local path = we_bas_getcustompath() | ||
857 | if path then | ||
858 | -- todo | ||
859 | end | ||
860 | {{/code}} | ||
861 | |||
862 | == [[we_bas_getusbpath>>||anchor="HPathclass"]](HMI) == | ||
863 | |||
864 | Prototype: we_bas_getusbpath() | ||
865 | |||
866 | Function: Detect USB, if detected, Return USB path (premise: support U disk Function) | ||
867 | |||
868 | Parameters: None | ||
869 | |||
870 | Return: | ||
871 | |||
872 | * Success: The path of USB | ||
873 | * Failure: nil | ||
874 | |||
875 | Example: | ||
876 | |||
877 | {{code language="Lua"}} | ||
878 | local path = we_bas_getusbpath() | ||
879 | if path then | ||
880 | print("udisk found") | ||
881 | -- todo | ||
882 | else | ||
883 | print("udisk not found") | ||
884 | end | ||
885 | {{/code}} | ||
886 | |||
887 | == [[we_bas_getsdpath>>||anchor="HPathclass"]](HMI) == | ||
888 | |||
889 | Prototype: we_bas_getsdpath() | ||
890 | |||
891 | Function: Detect SD card, if detected, return SD card path (premise: support SD card Function) | ||
892 | |||
893 | Parameters: None | ||
894 | |||
895 | Return: | ||
896 | |||
897 | * Success: The path of SD card | ||
898 | * Failure: nil | ||
899 | |||
900 | Example: | ||
901 | |||
902 | {{code language="Lua"}} | ||
903 | local path = we_bas_getsdpath() | ||
904 | if path then | ||
905 | print("sd found") | ||
906 | -- todo | ||
907 | else | ||
908 | print("sd not found") | ||
909 | end | ||
910 | {{/code}} | ||
911 | |||
912 | == [[we_bas_file_exist>>||anchor="HPathclass"]](HMI) == | ||
913 | |||
914 | Prototype: we_bas_file_exist(filepath) | ||
915 | |||
916 | Function: Does the file exist | ||
917 | |||
918 | Parameters: | ||
919 | |||
920 | * filepath(string):File path | ||
921 | |||
922 | Return: | ||
923 | |||
924 | * Success: true ~-~-exist | ||
925 | * Failure: false ~-~-not exist or error | ||
926 | |||
927 | Example: | ||
928 | |||
929 | {{code language="Lua"}} | ||
930 | -- Judge whether there is a file 123.txt in the root directory of the U disk | ||
931 | print(we_bas_file_exist("udisk:123.txt")) | ||
932 | {{/code}} | ||
933 | |||
934 | == [[we_bas_path_exist>>||anchor="HPathclass"]](HMI) == | ||
935 | |||
936 | Prototype: we_bas_path_exist(dirpath) | ||
937 | |||
938 | Function: Does the folder exist | ||
939 | |||
940 | Parameters: | ||
941 | |||
942 | * dirpath(string): Folder path | ||
943 | |||
944 | Return: | ||
945 | |||
946 | * Success: true ~-~-exist | ||
947 | * Failure: false ~-~-not exist or error | ||
948 | |||
949 | Example: | ||
950 | |||
951 | {{code language="Lua"}} | ||
952 | -- Judge whether there is a folder dir in the root directory of the U disk | ||
953 | print(we_bas_path_exist("udisk:dir")) | ||
954 | {{/code}} | ||
955 | |||
956 | = Screen class = | ||
957 | |||
958 | == [[we_bas_jumpscreen>>||anchor="HScreenclass"]](HMI) == | ||
959 | |||
960 | Prototype: we_bas_jumpscreen(screenid) | ||
961 | |||
962 | Function: Main screen jump | ||
963 | |||
964 | Parameters: | ||
965 | |||
966 | * screenid(number): Main screen No. | ||
967 | |||
968 | Return: | ||
969 | |||
970 | * Success: true | ||
971 | * Failure: false | ||
972 | |||
973 | Example: | ||
974 | |||
975 | {{code language="Lua"}} | ||
976 | -- Jump to Screen 0 | ||
977 | we_bas_jumpscreen(0) | ||
978 | {{/code}} | ||
979 | |||
980 | {{info}} | ||
981 | **✎Note: **Do not recommend to use this function in poll script, otherwise frequent calls may cause the HMI get stuck. | ||
982 | {{/info}} | ||
983 | |||
984 | == [[we_bas_popscreen>>||anchor="HScreenclass"]](HMI) == | ||
985 | |||
986 | Prototype: we_bas_popscreen(subscreenid, x, y) | ||
987 | |||
988 | Function: Pop up a sub-screen, if a built-in screen like keyboard pops up, the screen cannot be used | ||
989 | |||
990 | Parameters: | ||
991 | |||
992 | * subscreenid(number): Sub-screen No. | ||
993 | * x(number):The X coordinate of upper left corner of the screen | ||
994 | * y(number):The Y coordinate of upper left corner of the screen | ||
995 | |||
996 | Return: | ||
997 | |||
998 | * Success: true | ||
999 | * Failure: false | ||
1000 | |||
1001 | Example: | ||
1002 | |||
1003 | {{code language="Lua"}} | ||
1004 | -- Pop up a sub-screen, coordinates (100, 100) | ||
1005 | we_bas_popscreen(100, 100, 100) | ||
1006 | {{/code}} | ||
1007 | |||
1008 | {{info}} | ||
1009 | **✎Note: **Do not recommend to use this function in poll script, otherwise frequent calls may cause the HMI get stuck. | ||
1010 | {{/info}} | ||
1011 | |||
1012 | == [[we_bas_closescreen>>||anchor="HScreenclass"]](HMI) == | ||
1013 | |||
1014 | Prototype: we_bas_closescreen(subscreenid) | ||
1015 | |||
1016 | Function: Close sub-screen | ||
1017 | |||
1018 | Parameters: | ||
1019 | |||
1020 | * subscreenid(number): Sub-screen No. | ||
1021 | |||
1022 | Return: | ||
1023 | |||
1024 | * Success: true | ||
1025 | * Failure: false | ||
1026 | |||
1027 | Example: | ||
1028 | |||
1029 | {{code language="Lua"}} | ||
1030 | -- Close sub-screen 100 | ||
1031 | we_bas_closescreen(100) | ||
1032 | {{/code}} | ||
1033 | |||
1034 | {{info}} | ||
1035 | **✎Note: **Do not recommend to use this function in poll script, otherwise frequent calls may cause the HMI get stuck. | ||
1036 | {{/info}} | ||
1037 | |||
1038 | = Others = | ||
1039 | |||
1040 | == [[we_bas_gettickcount>>||anchor="HOthers"]] == | ||
1041 | |||
1042 | Prototype: we_bas_gettickcount() | ||
1043 | |||
1044 | Function: Get milliseconds that have passed since the operating system was started | ||
1045 | |||
1046 | Parameters: None | ||
1047 | |||
1048 | Return: Time | ||
1049 | |||
1050 | Example: | ||
1051 | |||
1052 | {{code language="Lua"}} | ||
1053 | print(we_bas_gettickcount()) | ||
1054 | {{/code}} | ||
1055 | |||
1056 | == [[we_bas_sleep>>||anchor="HOthers"]] == | ||
1057 | |||
1058 | Prototype: we_bas_sleep(millisecond) | ||
1059 | |||
1060 | Function: Sleep,**would block the running of the script** | ||
1061 | |||
1062 | Parameters: | ||
1063 | |||
1064 | * millisecond(number): Sleep milliseconds | ||
1065 | |||
1066 | Return: None; | ||
1067 | |||
1068 | Example: | ||
1069 | |||
1070 | {{code language="Lua"}} | ||
1071 | we_bas_sleep() | ||
1072 | {{/code}} | ||
1073 | |||
1074 | == [[we_bas_beep>>||anchor="HOthers"]] == | ||
1075 | |||
1076 | Prototype: we_bas_beep() | ||
1077 | |||
1078 | Function: Beep | ||
1079 | |||
1080 | Parameters: None | ||
1081 | |||
1082 | Return: None | ||
1083 | |||
1084 | Example: | ||
1085 | |||
1086 | {{code language="Lua"}} | ||
1087 | we_bas_beep() | ||
1088 | {{/code}} | ||
1089 | |||
1090 | == [[we_atu8>>||anchor="HOthers"]] == | ||
1091 | |||
1092 | Prototype: we_atu8(str) | ||
1093 | |||
1094 | Function: Character set conversion, windows to utf8 | ||
1095 | |||
1096 | Note: Only effective for the simulator, no conversion in the real HMI | ||
1097 | |||
1098 | Parameters: | ||
1099 | |||
1100 | * str(string): string of windows character set | ||
1101 | |||
1102 | Return: | ||
1103 | |||
1104 | * Success: string of utf8 character set | ||
1105 | * Failure: nil | ||
1106 | |||
1107 | Example:[[Reference>>path:../input/General.odt/1.6lfs.html#func2]] | ||
1108 | |||
1109 | == [[we_u8ta>>||anchor="HOthers"]] == | ||
1110 | |||
1111 | Prototype: we_u8ta(str) | ||
1112 | |||
1113 | Function: Character set conversion, utf8 to windows | ||
1114 | |||
1115 | Note: Only effective for the simulator, no conversion in the real HMI | ||
1116 | |||
1117 | Parameters: | ||
1118 | |||
1119 | * str(string): string of utf8 character set | ||
1120 | |||
1121 | Return: | ||
1122 | |||
1123 | * Success: string of windows character set | ||
1124 | * Failure: nil | ||
1125 | |||
1126 | Example:[[Reference>>path:../input/General.odt/1.6lfs.html#func2]] | ||
1127 | |||
1128 | == [[we_bas_getmachineinfo>>||anchor="HOthers"]] == | ||
1129 | |||
1130 | Prototype: we_bas_getmachineinfo() | ||
1131 | |||
1132 | Function: Get HMI machine information | ||
1133 | |||
1134 | Parameters: None | ||
1135 | |||
1136 | Return: | ||
1137 | |||
1138 | * Success: Model, Machine code, Image list | ||
1139 | * Failure: nil | ||
1140 | |||
1141 | Example: | ||
1142 | |||
1143 | {{code language="Lua"}} | ||
1144 | local sBoardType, sMachineID, sImageList = we_bas_getmachineinfo() | ||
1145 | local sSendData = "" | ||
1146 | |||
1147 | sSendData = sBoardType .. "\n" .. sMachineID .. "\n" .. sImageList | ||
1148 | print(sSendData) | ||
1149 | {{/code}} | ||
1150 | |||
1151 | = Lua object operation = | ||
1152 | |||
1153 | (% class="wikigeneratedid" id="HDescription" %) | ||
1154 | **Description** | ||
1155 | |||
1156 | The library is mainly added for **Lua Object **, used to control various states of Lua Object | ||
1157 | |||
1158 | The prefix name of the library is: **we_part_xxx** | ||
1159 | |||
1160 | (% class="box infomessage" %) | ||
1161 | ((( | ||
1162 | (% class="wikigeneratedid" id="H270ENote:" %) | ||
1163 | **✎Note:** | ||
1164 | |||
1165 | * Do not use the functions of this type of interface for other types of components, may get unexpected results | ||
1166 | * The state of Lua object will be cleared after leaving the screen, and no state will be retained or recorded | ||
1167 | ))) | ||
1168 | |||
1169 | |=Function|=Description | ||
1170 | |[[we_part_setstate>>||anchor="Hwe_part_setstate"]](HMI)|Set the state of the object | ||
1171 | |[[we_part_getstate>>||anchor="Hwe_part_getstate"]](HMI)|Get the state value of the object | ||
1172 | |[[we_part_settchenable>>||anchor="Hwe_part_settchenable"]](HMI)|Set object allowed to click | ||
1173 | |[[we_part_settchunable>>||anchor="Hwe_part_settchunable"]](HMI)|Set object not allowed to click | ||
1174 | |[[we_part_gettchstate>>||anchor="Hwe_part_gettchstate"]](HMI)|Get whether the object is allowed to be clicked | ||
1175 | |[[we_part_sethidestate>>||anchor="Hwe_part_sethidestate"]](HMI)|Set object hidden value | ||
1176 | |[[we_part_gethidestate>>||anchor="Hwe_part_gethidestate"]](HMI)|Get object hidden value | ||
1177 | |[[we_part_clickdown>>||anchor="Hwe_part_clickdown"]](HMI)|Send a click and press message to the specified object, and execute the press script | ||
1178 | |[[we_part_clickup>>||anchor="Hwe_part_clickup"]](HMI)|Send a click~-~- up message to the specified part and execute the pop~-~- up script | ||
1179 | |[[we_part_setcurrc>>||anchor="Hwe_part_setcurrc"]](HMI)|Set current outline information of object | ||
1180 | |[[we_part_getcurrc>>||anchor="Hwe_part_getcurrc"]](HMI)|Get current outline information of object | ||
1181 | |[[we_part_getoriginalrc>>||anchor="Hwe_part_getoriginalrc"]](HMI)|Get the original outline information of the object | ||
1182 | |||
1183 | == [[we_part_setstate>>||anchor="HLuaobjectoperation"]](HMI) == | ||
1184 | |||
1185 | Prototype: we_part_setstate(partname, value) | ||
1186 | |||
1187 | Function: Set the status of object | ||
1188 | |||
1189 | Parameter: | ||
1190 | |||
1191 | * partname(string): object name | ||
1192 | * value(number): the status value of the object | ||
1193 | |||
1194 | Return: | ||
1195 | |||
1196 | * Success: true | ||
1197 | * Failure: false | ||
1198 | |||
1199 | Example: | ||
1200 | |||
1201 | {{code language="Lua"}} | ||
1202 | -- Set the state of part 0_Lua_0 to 1 | ||
1203 | we_part_setstate("0_Lua_0", 1) | ||
1204 | {{/code}} | ||
1205 | |||
1206 | == [[we_part_getstate>>||anchor="HLuaobjectoperation"]](HMI) == | ||
1207 | |||
1208 | Prototype: we_part_getstate(partname) | ||
1209 | |||
1210 | Function: Get the status value of the object | ||
1211 | |||
1212 | Parameter: | ||
1213 | |||
1214 | * partname(string): object name | ||
1215 | |||
1216 | Return: | ||
1217 | |||
1218 | * Success: Status value (number) | ||
1219 | * Failure: nil | ||
1220 | |||
1221 | Example: | ||
1222 | |||
1223 | {{code language="Lua"}} | ||
1224 | print(we_part_getstate("0_Lua_0")) | ||
1225 | {{/code}} | ||
1226 | |||
1227 | == [[we_part_settchenable>>||anchor="HLuaobjectoperation"]](HMI) == | ||
1228 | |||
1229 | Prototype: we_part_settchenable(partname) | ||
1230 | |||
1231 | Function: Set the object allowed to click | ||
1232 | |||
1233 | Parameter: | ||
1234 | |||
1235 | * partname(string): object name | ||
1236 | |||
1237 | Return: | ||
1238 | |||
1239 | * Success: true | ||
1240 | * Failure: false | ||
1241 | |||
1242 | Example: | ||
1243 | |||
1244 | {{code language="Lua"}} | ||
1245 | print(we_part_settchenable("0_Lua_0")) | ||
1246 | {{/code}} | ||
1247 | |||
1248 | == [[we_part_settchunable>>||anchor="HLuaobjectoperation"]](HMI) == | ||
1249 | |||
1250 | Prototype: we_part_settchunable(partname) | ||
1251 | |||
1252 | Function: Set object not allowed to click | ||
1253 | |||
1254 | Parameter: | ||
1255 | |||
1256 | * partname(string): object name | ||
1257 | |||
1258 | Return: | ||
1259 | |||
1260 | * Success: true | ||
1261 | * Failure: false | ||
1262 | |||
1263 | Example: | ||
1264 | |||
1265 | {{code language="Lua"}} | ||
1266 | print(we_part_settchunable("0_Lua_0")) | ||
1267 | {{/code}} | ||
1268 | |||
1269 | == [[we_part_gettchstate>>||anchor="HLuaobjectoperation"]](HMI) == | ||
1270 | |||
1271 | Prototype: we_part_gettchstate(partname) | ||
1272 | |||
1273 | Function: Get whether the object is allowed to be clicked | ||
1274 | |||
1275 | Parameter: | ||
1276 | |||
1277 | * partname(string): object name | ||
1278 | |||
1279 | Return: | ||
1280 | |||
1281 | * Success: true allowed to click, false not allowed to click | ||
1282 | * Failure: nil | ||
1283 | |||
1284 | Example: | ||
1285 | |||
1286 | {{code language="Lua"}} | ||
1287 | print(we_part_gettchstate("0_Lua_0")) | ||
1288 | {{/code}} | ||
1289 | |||
1290 | == [[we_part_sethidestate>>||anchor="HLuaobjectoperation"]](HMI) == | ||
1291 | |||
1292 | Prototype: we_part_sethidestate(partname, value) | ||
1293 | |||
1294 | Function: set the hidden value of the object | ||
1295 | |||
1296 | Parameter: | ||
1297 | |||
1298 | * partname(string): object name | ||
1299 | * value(number): set value | ||
1300 | 0: No special processing is performed, according to the Hide function controlled by the object’s control address | ||
1301 | 1: Display and not be affected by the Hide function of the object's control address | ||
1302 | 2: Hidden and not affected by the Hide function of the object's control address | ||
1303 | |||
1304 | Return: | ||
1305 | |||
1306 | * Success: true | ||
1307 | * Failure: false | ||
1308 | |||
1309 | Example: | ||
1310 | |||
1311 | {{code language="Lua"}} | ||
1312 | -- hide | ||
1313 | we_part_sethidestate("0_Lua_0", 2) | ||
1314 | {{/code}} | ||
1315 | |||
1316 | == [[we_part_gethidestate>>||anchor="HLuaobjectoperation"]](HMI) == | ||
1317 | |||
1318 | Prototype: we_part_gethidestate(partname) | ||
1319 | |||
1320 | Function: Get the hidden value of the object | ||
1321 | |||
1322 | Parameter: | ||
1323 | |||
1324 | * partname(string): object name | ||
1325 | |||
1326 | Return: | ||
1327 | |||
1328 | * Success: true object is hidden, false object is displayed | ||
1329 | * Failure: nil | ||
1330 | |||
1331 | == [[we_part_clickdown>>||anchor="HLuaobjectoperation"]](HMI) == | ||
1332 | |||
1333 | Prototype: we_part_clickdown(partname) | ||
1334 | |||
1335 | Function: Send a click~-~- down message to the specified component, and execute the click~-~- down script | ||
1336 | |||
1337 | Parameter: | ||
1338 | |||
1339 | * partname(string): object name | ||
1340 | |||
1341 | Return: | ||
1342 | |||
1343 | * Success: true | ||
1344 | * Failure: false | ||
1345 | |||
1346 | == [[we_part_clickup>>||anchor="HLuaobjectoperation"]](HMI) == | ||
1347 | |||
1348 | Prototype: we_part_clickup(partname) | ||
1349 | |||
1350 | Function: Send a click~-~- up message to the specified component, and execute the click~-~- up script | ||
1351 | |||
1352 | Parameter: | ||
1353 | |||
1354 | * partname(string): object name | ||
1355 | |||
1356 | Return: | ||
1357 | |||
1358 | * Success: true | ||
1359 | * Failure: false | ||
1360 | |||
1361 | == [[we_part_setcurrc>>||anchor="HLuaobjectoperation"]](HMI) == | ||
1362 | |||
1363 | Prototype: we_part_setcurrc(partname, left, top, right, bottom) | ||
1364 | |||
1365 | Function: Set the current contour info of the object | ||
1366 | |||
1367 | Parameter: | ||
1368 | |||
1369 | * partname(string): object name | ||
1370 | * left(number): coordinate value of the left side of the object | ||
1371 | * top(number): coordinate value of the left side of the object | ||
1372 | * right(number): coordinate value of the right side of the object | ||
1373 | * bottom(number): coordinate value of the bottom of the object | ||
1374 | |||
1375 | Return: | ||
1376 | |||
1377 | * Success: true | ||
1378 | * Failure: false | ||
1379 | |||
1380 | Example: | ||
1381 | |||
1382 | {{code language="Lua"}} | ||
1383 | -- Move the object to the upper left corner, and the size is 100x100 | ||
1384 | we_part_setcurrc("0_Lua_0", 0, 0, 100, 100) | ||
1385 | {{/code}} | ||
1386 | |||
1387 | == [[we_part_getcurrc>>||anchor="HLuaobjectoperation"]](HMI) == | ||
1388 | |||
1389 | Prototype: we_part_getcurrc(partname) | ||
1390 | |||
1391 | Function: Get the current contour info of the object | ||
1392 | |||
1393 | Parameter: | ||
1394 | |||
1395 | * partname(string): object name | ||
1396 | |||
1397 | Return: | ||
1398 | |||
1399 | * Success: left, top, right, bottom (number), four values | ||
1400 | * Failure: nil | ||
1401 | |||
1402 | Example: | ||
1403 | |||
1404 | {{code language="Lua"}} | ||
1405 | local left, top, right, bottom = we_part_getcurrc("0_Lua_0") | ||
1406 | print(left, top, right, bottom) | ||
1407 | {{/code}} | ||
1408 | |||
1409 | == [[we_part_getoriginalrc>>||anchor="HLuaobjectoperation"]](HMI) == | ||
1410 | |||
1411 | Prototype: we_part_getoriginalrc(partname) | ||
1412 | |||
1413 | Function: Get the original contour info of the object | ||
1414 | |||
1415 | Parameter: | ||
1416 | |||
1417 | * partname(string): object name | ||
1418 | |||
1419 | Return: | ||
1420 | |||
1421 | * Success: left, top, right, bottom (number), four values | ||
1422 | * Failure: nil | ||
1423 | |||
1424 | Example: | ||
1425 | |||
1426 | {{code language="Lua"}} | ||
1427 | local left, top, right, bottom = we_part_getoriginalrc("0_Lua_0") | ||
1428 | print(left, top, right, bottom) | ||
1429 | {{/code}} | ||
1430 | |||
1431 | = Timer = | ||
1432 | |||
1433 | (% class="wikigeneratedid" id="HDescription" %) | ||
1434 | **Description** | ||
1435 | |||
1436 | The timer function is similar to the timing script of the Macro, which has the effect of timing execution. The difference is that Macro executes one script, while Lua executes one function. | ||
1437 | |||
1438 | The name of the library is: **timer** | ||
1439 | |||
1440 | (% class="wikigeneratedid" id="HFeatures" %) | ||
1441 | **Features** | ||
1442 | |||
1443 | * Trigger execution when timing arrives | ||
1444 | * Dynamic switching enable, can disable or enable during operation | ||
1445 | * The timing interval is variable, and the interval value can be modified during operation | ||
1446 | |||
1447 | (% class="wikigeneratedid" id="H270ENote:-1" %) | ||
1448 | **✎Note:** | ||
1449 | |||
1450 | * If multiple timers are registered in one screen, they will be executed out of order, and timing arrives will be executed first | ||
1451 | * The function registered by the timer cannot be written in an endless loop, otherwise it will be stuck in all operations, including the execution of screen polling **we//scr//poll_x** | ||
1452 | |||
1453 | (% class="wikigeneratedid" id="HList-1" %) | ||
1454 | List | ||
1455 | |||
1456 | |||
1457 | |=Function|=Description | ||
1458 | |[[timer.create>>||anchor="Htimer.create"]]|Create timer | ||
1459 | |[[timer.set_status>>||anchor="Htimer.set_status"]]|Enable timer | ||
1460 | |[[timer.get_status>>||anchor="Htimer.get_status"]]|Get timer status | ||
1461 | |[[timer.set_interval>>||anchor="Htimer.set_interval"]]|Modify timer interval value | ||
1462 | |[[timer.get_interval>>||anchor="Htimer.get_interval"]]|Get timer interval value | ||
1463 | |[[timer.get_func>>||anchor="Htimer.get_func"]]|Get the function associated with the timer | ||
1464 | |||
1465 | (% class="wikigeneratedid" id="HParameterRequirements:" %) | ||
1466 | **Parameter Requirements:** | ||
1467 | |||
1468 | The value range of **screen number** in the function parameters is: | ||
1469 | |||
1470 | * -1: Global background | ||
1471 | * [0,99999]: the corresponding screen number | ||
1472 | |||
1473 | The **ID** in the function parameters must be unique in every screen, and also unique in the global, but each screen is independent of each other. For example, a timer with ID=[1,2,3] is created on screen 0, screen 1 can also have a timer with ID=[1,2,3], and the global background can also have ID=[1,2, 3] these timers are independent and unique | ||
1474 | |||
1475 | == [[timer.create>>||anchor="HTimer"]] == | ||
1476 | |||
1477 | Prototype: timer.create(screenid, id, interval, f) | ||
1478 | |||
1479 | Function: Create a timer, execute the registered function when the timer arrives, and automatically start to run after it is created by default | ||
1480 | |||
1481 | Parameter: | ||
1482 | |||
1483 | * screenid(number): screen number | ||
1484 | * id(number): ID | ||
1485 | * interval(number): interval | ||
1486 | * f(function): registered execution function | ||
1487 | |||
1488 | Return: | ||
1489 | |||
1490 | * Success: true | ||
1491 | * Failure: nil | ||
1492 | |||
1493 | Example: | ||
1494 | |||
1495 | {{code language="Lua"}} | ||
1496 | -- Method 1: On screen 0, create a timer with ID=1 and an interval of 1000ms | ||
1497 | timer.create(0, 1, 1000, function() print("Hello world") end) | ||
1498 | |||
1499 | -- Method 2: Write the function first, then register it | ||
1500 | function callback() print("Timer called") end | ||
1501 | -- Similarly, in screen 0, pay attention to ID=2 at this time, it cannot be repeated, and the function name can be used directly for the registration function | ||
1502 | timer.create(0, 2, 1000, callback) | ||
1503 | |||
1504 | -- Create a timer for global execution, which should follow the unique ID | ||
1505 | timer.create(-- 1, 1, 1000, function() print("This is global timer test.") end) | ||
1506 | {{/code}} | ||
1507 | |||
1508 | == [[timer.set_status>>||anchor="HTimer"]] == | ||
1509 | |||
1510 | Prototype: timer.set_status(screenid, id, value) | ||
1511 | |||
1512 | Function: enable timer | ||
1513 | |||
1514 | Parameter: | ||
1515 | |||
1516 | * screenid(number): screen number | ||
1517 | * id(number): ID | ||
1518 | * value(number): value, 0: off, 1: on | ||
1519 | |||
1520 | Return: | ||
1521 | |||
1522 | * Success: true | ||
1523 | * Failure: nil | ||
1524 | |||
1525 | Example: | ||
1526 | |||
1527 | {{code language="Lua"}} | ||
1528 | -- Turn off the timer | ||
1529 | timer.set_status(0, 1, 0) | ||
1530 | -- Turn on the timer | ||
1531 | timer.set_status(0, 1, 1) | ||
1532 | {{/code}} | ||
1533 | |||
1534 | == [[timer.get_status>>||anchor="HTimer"]] == | ||
1535 | |||
1536 | Prototype: timer.get_status(screenid, id) | ||
1537 | |||
1538 | Function: Get timer status | ||
1539 | |||
1540 | Parameter: | ||
1541 | |||
1542 | * screenid(number): screen number | ||
1543 | * id(number): ID | ||
1544 | |||
1545 | Return: | ||
1546 | |||
1547 | * Success: 0: off, 1: on (number) | ||
1548 | * Failure: nil | ||
1549 | |||
1550 | Example: | ||
1551 | |||
1552 | {{code language="Lua"}} | ||
1553 | -- Get the status of the timer | ||
1554 | local status = timer.get_status(0, 1) | ||
1555 | print(status) | ||
1556 | {{/code}} | ||
1557 | |||
1558 | (% id="Htimer.set_interval" %) | ||
1559 | == [[timer.set_interval>>||anchor="HTimer"]] == | ||
1560 | |||
1561 | Prototype: timer.set_interval(screenid, id, num) | ||
1562 | |||
1563 | Function: modify the interval value of the timer | ||
1564 | |||
1565 | Parameter: | ||
1566 | |||
1567 | * screenid(number): screen number | ||
1568 | * id(number): ID | ||
1569 | * num(number): value, unit: ms | ||
1570 | |||
1571 | Return: | ||
1572 | |||
1573 | * Success: true | ||
1574 | * Failure: nil | ||
1575 | |||
1576 | Example: | ||
1577 | |||
1578 | {{code language="Lua"}} | ||
1579 | -- Change the interval value of screen 0, ID=1 to 2000ms | ||
1580 | timer.set_interval(0, 1, 2000) | ||
1581 | {{/code}} | ||
1582 | |||
1583 | == [[timer.get_interval>>||anchor="HTimer"]] == | ||
1584 | |||
1585 | Prototype: timer.get_interval(screenid, id) | ||
1586 | |||
1587 | Function: Get the interval value of the timer | ||
1588 | |||
1589 | Parameter: | ||
1590 | |||
1591 | * screenid(number): screen number | ||
1592 | * id(number): ID | ||
1593 | |||
1594 | Return: | ||
1595 | |||
1596 | * Success: Interval value (number) | ||
1597 | * Failure: nil | ||
1598 | |||
1599 | Example: | ||
1600 | |||
1601 | {{code language="Lua"}} | ||
1602 | local val = timer.get_interval(0, 1) | ||
1603 | print(val) | ||
1604 | {{/code}} | ||
1605 | |||
1606 | == [[timer.get_func>>||anchor="HTimer"]] == | ||
1607 | |||
1608 | Prototype: timer.get_func(screenid, id) | ||
1609 | |||
1610 | Function: Get the execution function associated with the timer | ||
1611 | |||
1612 | Parameter: | ||
1613 | |||
1614 | * screenid(number): screen number | ||
1615 | * id(number): ID | ||
1616 | |||
1617 | Return: | ||
1618 | |||
1619 | * Success: function | ||
1620 | * Failure: nil | ||
1621 | |||
1622 | Example: | ||
1623 | |||
1624 | {{code language="Lua"}} | ||
1625 | local f = timer.get_func(0, 1) | ||
1626 | -- Print function address | ||
1627 | print(f) | ||
1628 | -- or execute function | ||
1629 | f() | ||
1630 | {{/code}} | ||
1631 | |||
1632 | = Trigger = | ||
1633 | |||
1634 | (% class="wikigeneratedid" id="HDescription-2" %) | ||
1635 | **Description** | ||
1636 | |||
1637 | The trigger function is similar to the trigger script of Macro, which has the effect of triggering execution. The difference is that Macro executes a script, while Lua executes a function. | ||
1638 | |||
1639 | The name of the library is: **trigger** | ||
1640 | \\Support 5 trigger types: | ||
1641 | |||
1642 | 1. TRUE: trigger.type_true | ||
1643 | 1. FALSE: trigger.type_false | ||
1644 | 1. Bit change: trigger.type_change | ||
1645 | 1. Rising edge: trigger.type_raise | ||
1646 | 1. Falling edge: trigger.type_fall | ||
1647 | |||
1648 | (% class="wikigeneratedid" id="HFeatures-1" %) | ||
1649 | **Features** | ||
1650 | |||
1651 | * Bit address condition triggers execution | ||
1652 | * Dynamic switching enable, which can enable or disable during operation | ||
1653 | * Variable trigger conditions, trigger conditions can be modified during operation | ||
1654 | |||
1655 | (% class="wikigeneratedid" id="H270ENote:-1" %) | ||
1656 | **✎Note:** | ||
1657 | |||
1658 | * If multiple triggers are registered in one screen, the address monitoring will be executed out of order | ||
1659 | * The function registered by the trigger cannot be written in an endless loop, otherwise it will be stuck in all operations, including the execution of screen polling **we//scr//poll_x** | ||
1660 | |||
1661 | |=Function|=Description | ||
1662 | |[[trigger.create>>||anchor="Htrigger.create"]]|Create Trigger | ||
1663 | |[[trigger.set_status>>||anchor="Htrigger.set_status"]]|Enable trigger | ||
1664 | |[[trigger.get_status>>||anchor="Htrigger.get_status"]]|Get trigger status | ||
1665 | |[[trigger.set_type>>||anchor="Htrigger.set_type"]]|Modify the trigger type of the trigger | ||
1666 | |[[trigger.get_type>>||anchor="Htrigger.get_type"]]|Get the trigger type of the trigger | ||
1667 | |[[trigger.get_func>>||anchor="Htrigger.get_func"]]|Get the execution function associated with the trigger | ||
1668 | |||
1669 | (% class="wikigeneratedid" id="HParameterRequirements:-1" %) | ||
1670 | **Parameter Requirements:** | ||
1671 | |||
1672 | The value range of **screen number** in the function parameters is: | ||
1673 | |||
1674 | * -1: Global background | ||
1675 | * [0,99999]: the corresponding screen number | ||
1676 | |||
1677 | The **ID** in the function parameters must be unique in every screen, and also unique in the global, but each screen is independent of each other. For example, a trigger with ID=[1,2,3] is created on screen 0, screen 1 can also have a trigger with ID=[1,2,3], and the global background can also have ID=[1,2, 3] , these triggers are independent and unique | ||
1678 | |||
1679 | == [[trigger.create>>||anchor="HTrigger"]] == | ||
1680 | |||
1681 | Prototype: trigger.create(screenid, id, triggertype, address, f) | ||
1682 | |||
1683 | Function: Create a trigger, execute the registered function when the trigger condition is met, and automatically start monitoring after it is created by default | ||
1684 | |||
1685 | Parameter: | ||
1686 | |||
1687 | * screenid(number): screen number | ||
1688 | * id(number): ID | ||
1689 | * triggertype(number): trigger condition type | ||
1690 | * address(string): the bit address to be monitored and triggered | ||
1691 | * f(function): registered execution function | ||
1692 | |||
1693 | Return: | ||
1694 | |||
1695 | * Success: true | ||
1696 | * Failure: nil | ||
1697 | |||
1698 | Example: | ||
1699 | |||
1700 | {{code language="Lua"}} | ||
1701 | -- Method 1: On screen 0, create a trigger with ID=1, type TRUE, and address HDX0.0 | ||
1702 | trigger.create(0, 1, trigger.type_true, "@B_HDX0.0", function() print("Hello world") end) | ||
1703 | |||
1704 | -- Method 2: Write the function first, then register | ||
1705 | function callback() print("trigger called") end | ||
1706 | -- Similarly, in screen 0, pay attention to ID=2 at this time, it cannot be repeated, and the function name can be used directly for the registration function | ||
1707 | trigger.create(0, 2, trigger.type_true, "@B_HDX0.1", callback) | ||
1708 | |||
1709 | -- Create a global execution bit change trigger, which should follow the unique ID | ||
1710 | trigger.create(-- 1, 1, trigger.type_change, "@B_HDX0.2", function() print("This is global trigger test.") end) | ||
1711 | {{/code}} | ||
1712 | |||
1713 | == [[trigger.set_status>>||anchor="HTrigger"]] == | ||
1714 | |||
1715 | Prototype: trigger.set_status(screenid, id, value) | ||
1716 | |||
1717 | Function: Enable trigger | ||
1718 | |||
1719 | Parameter: | ||
1720 | |||
1721 | * screenid(number): screen number | ||
1722 | * id(number): ID | ||
1723 | * value(number): value, 0: off, 1: on | ||
1724 | |||
1725 | Return: | ||
1726 | |||
1727 | * Success: true | ||
1728 | * Failure: nil | ||
1729 | |||
1730 | Example: | ||
1731 | |||
1732 | {{code language="Lua"}} | ||
1733 | -- Enable the trigger | ||
1734 | trigger.set_status(0, 1, 0) | ||
1735 | -- Disable the trigger | ||
1736 | trigger.set_status(0, 1, 1) | ||
1737 | {{/code}} | ||
1738 | |||
1739 | == [[trigger.get_status>>||anchor="HTrigger"]] == | ||
1740 | |||
1741 | Prototype: trigger.get_status(screenid, id) | ||
1742 | |||
1743 | Function: Get trigger status | ||
1744 | |||
1745 | Parameter: | ||
1746 | |||
1747 | * screenid(number): screen number | ||
1748 | * id(number): ID | ||
1749 | |||
1750 | Return: | ||
1751 | |||
1752 | * Success: 0: off, 1: on(number) | ||
1753 | * Failure: nil | ||
1754 | |||
1755 | Example: | ||
1756 | |||
1757 | {{code language="Lua"}} | ||
1758 | -- Get the status of the trigger | ||
1759 | local status = trigger.get_status(0, 1) | ||
1760 | print(status) | ||
1761 | {{/code}} | ||
1762 | |||
1763 | == [[trigger.set_type>>||anchor="HTrigger"]] == | ||
1764 | |||
1765 | Prototype: trigger.set_type(screenid, id, num) | ||
1766 | |||
1767 | Function: modify the trigger type of the trigger | ||
1768 | |||
1769 | Parameter: | ||
1770 | |||
1771 | * screenid(number): screen number | ||
1772 | * id(number): ID | ||
1773 | * num(number): type value | ||
1774 | |||
1775 | Return: | ||
1776 | |||
1777 | * Success: true | ||
1778 | * Failure: nil | ||
1779 | |||
1780 | Example: | ||
1781 | |||
1782 | {{code language="Lua"}} | ||
1783 | -- Change the trigger type of screen 0, ID=1 to false | ||
1784 | trigger.set_type(0, 1, trigger.type_false) | ||
1785 | {{/code}} | ||
1786 | |||
1787 | == [[trigger.get_type>>||anchor="HTrigger"]] == | ||
1788 | |||
1789 | Prototype: trigger.get_type(screenid, id) | ||
1790 | |||
1791 | Function: Get the trigger type of the trigger | ||
1792 | |||
1793 | Parameter: | ||
1794 | |||
1795 | * screenid(number): screen number | ||
1796 | * id(number): ID | ||
1797 | |||
1798 | Return: | ||
1799 | |||
1800 | * Success: trigger type value (number) | ||
1801 | * Failure: nil | ||
1802 | |||
1803 | Example: | ||
1804 | |||
1805 | {{code language="Lua"}} | ||
1806 | local val = trigger.get_type(0, 1) | ||
1807 | print(val) | ||
1808 | {{/code}} | ||
1809 | |||
1810 | == [[trigger.get_func>>||anchor="HTrigger"]] == | ||
1811 | |||
1812 | Prototype: trigger.get_func(screenid, id) | ||
1813 | |||
1814 | Function: Get the execution function associated with the trigger | ||
1815 | |||
1816 | Parameter: | ||
1817 | |||
1818 | * screenid(number): screen number | ||
1819 | * id(number): ID | ||
1820 | |||
1821 | Return: | ||
1822 | |||
1823 | * Success: function | ||
1824 | * Failure: nil | ||
1825 | |||
1826 | Example: | ||
1827 | |||
1828 | {{code language="Lua"}} | ||
1829 | local f = trigger.get_func(0, 1) | ||
1830 | -- Print function address | ||
1831 | print(f) | ||
1832 | -- or execute function | ||
1833 | f() | ||
1834 | {{/code}} | ||
1835 | |||
1836 | = Flash Read&Write = | ||
1837 | |||
1838 | (% class="wikigeneratedid" id="HDescription-3" %) | ||
1839 | **Description** | ||
1840 | |||
1841 | Lua's io/os library doesn’t support operating the internal Flash of the HMI. There is another interface for Flash that can implement common operations for files and folders. | ||
1842 | |||
1843 | The name of the library is: **flash** | ||
1844 | |||
1845 | The path format of the system is drive letter: location, and the available drive letters have the following 4 types: | ||
1846 | |||
1847 | * User directory: "user:" | ||
1848 | * User script: "script:" | ||
1849 | * U Disk: "udisk:" | ||
1850 | * SD card: "sdcard:" | ||
1851 | |||
1852 | The following shows how to express the path: | ||
1853 | |||
1854 | * Example 1: If a file named 1.txt exists in the root directory of the U disk, the path expression is: udisk:1.txt | ||
1855 | * Example 2: If a folder named dir exists in the root directory of the U disk, the path expression is: udisk:dir | ||
1856 | |||
1857 | (% class="wikigeneratedid" id="H270ENote:-2" %) | ||
1858 | **✎Note:** | ||
1859 | |||
1860 | * In order to prolong the service life, there is a limited control for writing, and only 1M is allowed to be written within 1 minute. If it exceeds, an error message will be prompted | ||
1861 | * The storage space is limited. This part of the space for reading and writing in Lua is shared with Data recording, Historical data and other functions. If the recording data is full, it may be impossible to use Lua scripts to write files, and vice versa | ||
1862 | |||
1863 | |=Function|=Description | ||
1864 | |[[flash.file_write>>||anchor="Hflash.file_write"]]|Write file | ||
1865 | |[[flash.file_copy>>||anchor="Hflash.file_copy"]]|Copy file | ||
1866 | |[[flash.file_rename>>||anchor="Hflash.file_rename"]]|Rename file | ||
1867 | |[[flash.file_remove>>||anchor="Hflash.file_remove"]]|Delete file | ||
1868 | |[[flash.file_filter>>||anchor="Hflash.file_filter"]]|Traverse folders | ||
1869 | |[[flash.dir_make>>||anchor="Hflash.dir_make"]]|Recursively create folders | ||
1870 | |[[flash.dir_remove>>||anchor="Hflash.dir_remove"]]|Recursively delete folders | ||
1871 | |[[flash.file_u8ta>>||anchor="Hflash.file_u8ta"]]|File character set conversion, UTF8 to gb2312 | ||
1872 | |[[flash.file_atu8>>||anchor="Hflash.file_atu8"]]|File character set conversion, gb2312 to UTF8 | ||
1873 | |[[flash.file_save>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#Hflash.file_save]]|Save files immediately | ||
1874 | |||
1875 | == [[flash.file_write>>||anchor="HFlashRead26Write"]] == | ||
1876 | |||
1877 | Prototype: flash.file_write(filepath, mode, data) | ||
1878 | |||
1879 | Function: Write data to a file, and you can write a file to the internal Flash through this interface | ||
1880 | |||
1881 | Parameter: | ||
1882 | |||
1883 | * filepath(string): file path | ||
1884 | * mode(string): open mode, it can be non~-~- r mode, | ||
1885 | * data(string): the data to be written | ||
1886 | |||
1887 | Return: | ||
1888 | |||
1889 | * Success: true | ||
1890 | * Failure: nil | ||
1891 | |||
1892 | Example: "w" is written once and "a" is written multiple times. | ||
1893 | |||
1894 | {{code language="Lua"}} | ||
1895 | -- Write to internal Flash | ||
1896 | flash.file_write("user:demo.txt", "w", "Hello world") | ||
1897 | flash.file_write("user:demo.txt", "a", "Hello world") | ||
1898 | -- Write to U disk | ||
1899 | flash.file_wrtie("udisk:export.txt", "w", "This is a demo file") | ||
1900 | flash.file_wrtie("udisk:export.txt", "a", "This is a demo file") | ||
1901 | {{/code}} | ||
1902 | |||
1903 | == [[flash.file_copy>>||anchor="HFlashRead26Write"]] == | ||
1904 | |||
1905 | Prototype: flash.file_copy(srcpath, dstpath, failifexists) | ||
1906 | |||
1907 | Function: Copy files, you can copy files from the peripheral to the internal Flash through this interface, and can also be used to export | ||
1908 | |||
1909 | Parameter: | ||
1910 | |||
1911 | * srcpath(string): source file path | ||
1912 | * dstpath(string): target file path | ||
1913 | * failifexists(boolean): whether it fails if the target file already exists | ||
1914 | ** true if the target file exists, copy fails | ||
1915 | ** false If the target file exists, overwrite copy | ||
1916 | |||
1917 | Return: | ||
1918 | |||
1919 | * Success: true | ||
1920 | * Failure: nil | ||
1921 | |||
1922 | Example: | ||
1923 | |||
1924 | {{code language="Lua"}} | ||
1925 | -- Assuming that the U disk has a demo.txt file, copy it to the HMI and perform an overwrite copy | ||
1926 | flash.file_copy("udisk:demo.txt", "user:demo.txt", false) | ||
1927 | -- The following is the export | ||
1928 | flash.file_copy("user:demo.txt", "udisk:demo2.txt", true) | ||
1929 | {{/code}} | ||
1930 | |||
1931 | == [[flash.file_rename>>||anchor="HFlashRead26Write"]] == | ||
1932 | |||
1933 | Prototype: flash.file_rename(oldname, newname) | ||
1934 | |||
1935 | Function: Rename file | ||
1936 | |||
1937 | Parameter: | ||
1938 | |||
1939 | * oldname(string): old file name path | ||
1940 | * newname(string): new file name path | ||
1941 | |||
1942 | Return: | ||
1943 | |||
1944 | * Success: true | ||
1945 | * Failure: false | ||
1946 | |||
1947 | Example: | ||
1948 | |||
1949 | {{code}} | ||
1950 | flash.file_rename("udisk:old.txt", "udisk:new.txt") | ||
1951 | {{/code}} | ||
1952 | |||
1953 | == [[flash.file_remove>>||anchor="HFlashRead26Write"]] == | ||
1954 | |||
1955 | Prototype: flash.file_remove(filepath) | ||
1956 | |||
1957 | Function: delete files | ||
1958 | |||
1959 | Parameter: | ||
1960 | |||
1961 | * filepath(string): file name path | ||
1962 | |||
1963 | Return: | ||
1964 | |||
1965 | * Success: true | ||
1966 | * Failure: false | ||
1967 | |||
1968 | Example: | ||
1969 | |||
1970 | {{code language="Lua"}} | ||
1971 | flash.file_remove("udisk:new.txt") | ||
1972 | {{/code}} | ||
1973 | |||
1974 | == [[flash.file_filter>>||anchor="HFlashRead26Write"]] == | ||
1975 | |||
1976 | Prototype: flash.file_filter(dirpath, format) | ||
1977 | |||
1978 | Function: traverse folders and return all file names that conform to the file format | ||
1979 | |||
1980 | Parameter: | ||
1981 | |||
1982 | * dirpath(string): folder path | ||
1983 | * format(string): file format | ||
1984 | |||
1985 | Return: | ||
1986 | |||
1987 | * Success: an array of file names (table) | ||
1988 | * Failure: nil | ||
1989 | |||
1990 | Example: | ||
1991 | |||
1992 | {{code language="Lua"}} | ||
1993 | -- Assume that the root directory of the U disk has the following files | ||
1994 | -- 123.txt|sheet.csv|demo.txt|zzz.txt|myword.doc | ||
1995 | -- Execute to get all files in txt format | ||
1996 | local data = flash.file_filter("udisk:", "*.txt") | ||
1997 | if data and type(data) == "table" then | ||
1998 | for i,v in ipairs(data) do | ||
1999 | print(i, v) | ||
2000 | end | ||
2001 | end | ||
2002 | {{/code}} | ||
2003 | |||
2004 | {{{Output: | ||
2005 | }}} | ||
2006 | |||
2007 | {{code language="Lua"}} | ||
2008 | 1 123.txt | ||
2009 | 2 demo.txt | ||
2010 | 3 zzz.txt | ||
2011 | {{/code}} | ||
2012 | |||
2013 | == [[flash.dir_make>>||anchor="HFlashRead26Write"]] == | ||
2014 | |||
2015 | Prototype: flash.dir_make(dirpath) | ||
2016 | |||
2017 | Function: Recursively create folders | ||
2018 | |||
2019 | Parameter: | ||
2020 | |||
2021 | * dirpath(string): folder path | ||
2022 | |||
2023 | Return: | ||
2024 | |||
2025 | * Success: true | ||
2026 | * Failure: false | ||
2027 | |||
2028 | Example: | ||
2029 | |||
2030 | {{code language="Lua"}} | ||
2031 | flash.dir_make("udisk:dir1/dir2/dir3") | ||
2032 | {{/code}} | ||
2033 | |||
2034 | == [[flash.dir_remove>>||anchor="HFlashRead26Write"]] == | ||
2035 | |||
2036 | Prototype: flash.dir_remove(dirpath) | ||
2037 | |||
2038 | Function: delete folders recursively, **including files in the folder** | ||
2039 | |||
2040 | Parameter: | ||
2041 | |||
2042 | * dirpath(string): folder path | ||
2043 | |||
2044 | Return: | ||
2045 | |||
2046 | * Success: true | ||
2047 | * Failure: false | ||
2048 | |||
2049 | Example: | ||
2050 | |||
2051 | {{code language="Lua"}} | ||
2052 | -- Assuming that there are dir1/dir2/dir3, three-- level folders in the U disk, specify dir1 to clear all | ||
2053 | flash.dir_remove("udisk:dir1") | ||
2054 | {{/code}} | ||
2055 | |||
2056 | == [[flash.file_u8ta>>||anchor="HFlashRead26Write"]] == | ||
2057 | |||
2058 | **Prototype:** flash.file_u8ta(u8file, gbfile) | ||
2059 | |||
2060 | **Function:** File character set conversion, UTF8 to GB2312 | ||
2061 | |||
2062 | **Parameter:** | ||
2063 | |||
2064 | * u8file(string): utf8 file to convert | ||
2065 | * gbfile(string): Save gb2312 file of converted data (**Only files in a Udisk or sd card supported)** | ||
2066 | |||
2067 | **Return:** | ||
2068 | |||
2069 | * = 0:Succeed | ||
2070 | * =-1:Function parameter error | ||
2071 | * =-2:File path error | ||
2072 | * =-3:Failed to read utf8 file data | ||
2073 | * =-4:utf8 file data exception | ||
2074 | * =-5:Insufficient space | ||
2075 | * =-6:Data conversion failure | ||
2076 | * =-7:Failed to write the GB2312 file | ||
2077 | |||
2078 | **Example:** | ||
2079 | |||
2080 | {{code language="LUA"}} | ||
2081 | --Convert utF8 files in the Udisk to GB2312 and save the files in the Udisk | ||
2082 | flash.file_u8ta("udisk:utf8.txt", "udisk:new_gb2312.txt") | ||
2083 | {{/code}} | ||
2084 | |||
2085 | == [[flash.file_atu8>>||anchor="HFlashRead26Write"]] == | ||
2086 | |||
2087 | **Prototype:** flash.file_atu8(gbfile, u8file) | ||
2088 | |||
2089 | **Function:** File character set conversion, UTF8 to GB2312 | ||
2090 | |||
2091 | **Parameter:** | ||
2092 | |||
2093 | * u8file(string): gb2312 file to convert | ||
2094 | * gbfile(string): Save utf8 file of converted data (**Only files in a Udisk or sd card supported)** | ||
2095 | |||
2096 | **Return:** | ||
2097 | |||
2098 | * = 0:Succeed | ||
2099 | * =-1:Function parameter error | ||
2100 | * =-2:File path error | ||
2101 | * =-3:Failed to read gb2312 file data | ||
2102 | * =-4:utf8 file data exception | ||
2103 | * =-5:Insufficient space | ||
2104 | * =-6:Data conversion failure | ||
2105 | * =-7:Failed to write the utf8 file | ||
2106 | |||
2107 | **Example:** | ||
2108 | |||
2109 | {{code language="LUA"}} | ||
2110 | --Convert GB2312 files in the Udisk to utF8 and save the files in the Udisk | ||
2111 | flash.file_atu8("udisk:gb2312.txt", "udisk:new_u8.txt") | ||
2112 | {{/code}} | ||
2113 | |||
2114 | == [[flash.file_save>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HFlashRead26Write]] == | ||
2115 | |||
2116 | **Prototype:** flash.file_save(filePath) | ||
2117 | |||
2118 | **Function:** File saved immediately. | ||
2119 | |||
2120 | **Parameter:** | ||
2121 | |||
2122 | * filePath(string): File name that needs to save immediately. | ||
2123 | |||
2124 | **Return:** | ||
2125 | |||
2126 | * = 0:Succeed | ||
2127 | |||
2128 | **Example:** | ||
2129 | |||
2130 | {{code language="LUA"}} | ||
2131 | flash.file_save("udisk:111.txt") | ||
2132 | {{/code}} | ||
2133 | |||
2134 | = Other functions = | ||
2135 | |||
2136 | (% class="wikigeneratedid" id="HDescription-1" %) | ||
2137 | **Description** | ||
2138 | |||
2139 | The library provides operations for folder traversal, creation, and deletion | ||
2140 | |||
2141 | The name of the library is: **lfs** | ||
2142 | |||
2143 | (% class="wikigeneratedid" id="HList-4" %) | ||
2144 | List | ||
2145 | |||
2146 | |=Function|=Description | ||
2147 | |[[lfs.attributes>>||anchor="Hlfs.attributes"]]|Get properties | ||
2148 | |[[lfs.dir>>||anchor="Hlfs.dir"]]|Traverse folders | ||
2149 | |[[lfs.mkdir>>||anchor="Hlfs.mkdir"]]|Create folder | ||
2150 | |[[lfs.rmdir>>||anchor="Hlfs.rmdir"]]|Delete folder | ||
2151 | |||
2152 | == [[lfs.attributes>>||anchor="HOtherfunctions"]] == | ||
2153 | |||
2154 | Prototype: lfs.attributes(filepath [, aname]) | ||
2155 | |||
2156 | Function: Get properties and return a table with file properties corresponding to the file path. If the second parameter is given, only the value of the naming property will be returned | ||
2157 | |||
2158 | Parameter: | ||
2159 | |||
2160 | * filepath(string): file path | ||
2161 | * aname(string): file attribute | ||
2162 | |||
2163 | Return: | ||
2164 | |||
2165 | * Success: table | ||
2166 | * Failure: nil | ||
2167 | |||
2168 | Example: | ||
2169 | |||
2170 | {{code language="Lua"}} | ||
2171 | -- Judge whether the format of the specified file is a text file | ||
2172 | local filemode = lfs.attributes("udisk:123.txt", "mode") | ||
2173 | if filemode == "file" then | ||
2174 | print("udisk:123.txt is a normal txt file") | ||
2175 | end | ||
2176 | {{/code}} | ||
2177 | |||
2178 | == [[lfs.dir>>||anchor="HOtherfunctions"]] == | ||
2179 | |||
2180 | Prototype: lfs.dir(dirpath) | ||
2181 | |||
2182 | Function: traverse the folder, this is an iterator function, need to use the for statement | ||
2183 | |||
2184 | Note: This interface function is planned to be deprecated in the future, it is recommended to use the [[flash.file_filter>>||anchor="Hflash.file_filter"]] function | ||
2185 | |||
2186 | Parameter: | ||
2187 | |||
2188 | * dirpath(string): folder path | ||
2189 | |||
2190 | Return: | ||
2191 | |||
2192 | * Success: file name (string) | ||
2193 | * Failure: nil | ||
2194 | |||
2195 | Example: | ||
2196 | |||
2197 | {{code language="Lua"}} | ||
2198 | -- Example of traversing folders | ||
2199 | local mydir = "udisk:" | ||
2200 | for fn in lfs.dir(mydir) do | ||
2201 | if fn ~= "." and fn ~= ".." then | ||
2202 | local filepath = mydir .. fn | ||
2203 | local attr = lfs.attributes(filepath) | ||
2204 | -- The simulator window needs the windows character set to display Chinese | ||
2205 | -- Print the full file path and its file properties | ||
2206 | print(we_u8ta(filepath), attr.mode) | ||
2207 | end | ||
2208 | end | ||
2209 | {{/code}} | ||
2210 | |||
2211 | == [[lfs.mkdir>>||anchor="HOtherfunctions"]] == | ||
2212 | |||
2213 | Prototype: lfs.mkdir(dirpath) | ||
2214 | |||
2215 | Function: Create a folder (recursion is not supported) | ||
2216 | |||
2217 | Notice: This interface function is planned to be deprecated in the future, it is recommended to use the [[flash.dir_make>>||anchor="Hflash.dir_make"]] function | ||
2218 | |||
2219 | Parameter: | ||
2220 | |||
2221 | * dirpath(string): folder path | ||
2222 | |||
2223 | Return: | ||
2224 | |||
2225 | * Success: true | ||
2226 | * Failure: nil | ||
2227 | |||
2228 | Example: | ||
2229 | |||
2230 | {{code language="Lua"}} | ||
2231 | -- Create the parent folder first, then create the sub folders | ||
2232 | if lfs.mkdir("udisk:mydir") then | ||
2233 | lfs.mkdir("udisk:mydir/subdir") | ||
2234 | end | ||
2235 | {{/code}} | ||
2236 | |||
2237 | == [[lfs.rmdir>>||anchor="HOtherfunctions"]] == | ||
2238 | |||
2239 | Prototype: lfs.rmdir(dirpath) | ||
2240 | |||
2241 | Function: Delete folder (recursion is not supported) | ||
2242 | |||
2243 | Note: This interface function is planned to be deprecated in the future. It is recommended to use the [[flash.dir_remove>>||anchor="Hflash.dir_remove"]] function | ||
2244 | |||
2245 | Parameter: | ||
2246 | |||
2247 | * dirpath(string): folder path | ||
2248 | |||
2249 | Return: | ||
2250 | |||
2251 | * Success: true | ||
2252 | * Failure: nil | ||
2253 | |||
2254 | Example: | ||
2255 | |||
2256 | {{code language="Lua"}} | ||
2257 | -- Remove the sub folder first, then remove the parent folder | ||
2258 | if lfs.rmdir("udisk:mydir/subdir") then | ||
2259 | lfs.rmdir("udisk:mydir") | ||
2260 | end | ||
2261 | {{/code}} | ||
2262 | |||
2263 | (% id="HTableDrawing28Experimental29" %) | ||
2264 | = Table drawing = | ||
2265 | |||
2266 | (% id="HDescription-2" %) | ||
2267 | Description | ||
2268 | |||
2269 | The name of the library is: cus_table | ||
2270 | |||
2271 | The following interface example assumes that there is a object named 0_CST_0 on screen 0 | ||
2272 | |||
2273 | {{info}} | ||
2274 | (% id="H270ENote:-3" %) | ||
2275 | ✎Note: | ||
2276 | |||
2277 | * The appearance of the table style cannot be modified currently | ||
2278 | * It is currently an experimental function. If there is any modification in the future, please read the interface documentation carefully | ||
2279 | * The status of the parts will be cleared after leaving the screen, and no status will be retained or recorded | ||
2280 | {{/info}} | ||
2281 | |||
2282 | (% id="HList-5" %) | ||
2283 | List | ||
2284 | |||
2285 | |=Function|=Description | ||
2286 | |[[cus_table.set_cell>>||anchor="Hcus_table.set_cell"]](HMI)|Set the number of rows and columns of the table | ||
2287 | |[[cus_table.set_head>>||anchor="Hcus_table.set_head"]](HMI)|Set table header text | ||
2288 | |[[cus_table.set_mode>>||anchor="Hcus_table.set_mode"]](HMI)|Set display mode | ||
2289 | |[[cus_table.page_up>>||anchor="Hcus_table.page_up"]](HMI)|Execute page up | ||
2290 | |[[cus_table.page_down>>||anchor="Hcus_table.page_down"]](HMI)|Execute page down | ||
2291 | |[[cus_table.get_index>>||anchor="Hcus_table.get_index"]](HMI)|Get the currently selected index | ||
2292 | |[[cus_table.get_page>>||anchor="Hcus_table.get_page"]](HMI)|Get the current page and the total number of pages | ||
2293 | |[[cus_table.refresh>>||anchor="Hcus_table.refresh"]](HMI)|Refresh object | ||
2294 | |[[cus_table.reset>>||anchor="Hcus_table.reset"]](HMI)|Reset object | ||
2295 | |[[Example>>||anchor="HExample"]]| | ||
2296 | |||
2297 | (% id="Hcus_table.set_cell" %) | ||
2298 | == [[cus_table.set_cell>>||anchor="HTableDrawing28Experimental29"]](HMI) == | ||
2299 | |||
2300 | Prototype: cus_table.set_cell(part, row, col[, ratio]) | ||
2301 | |||
2302 | Function: Set the number of rows and columns of the table | ||
2303 | |||
2304 | * part(string): object name | ||
2305 | * row(number): number of rows | ||
2306 | * col(number): number of columns | ||
2307 | * ratio(string): optional parameter (default equal width), the ratio of column width | ||
2308 | |||
2309 | Return: | ||
2310 | |||
2311 | * Success: true | ||
2312 | * Failure: nil, error message | ||
2313 | |||
2314 | Example: | ||
2315 | |||
2316 | {{code language="Lua"}} | ||
2317 | -- Set the table with 20 rows and 6 columns. The width of the first column is twice times than that of the second column, and the second, third, fourth, fifth, and sixth columns are equal in width | ||
2318 | cus_table.set_cell("0_CST_0", 20, 6, "2:1:1:1:1:1") | ||
2319 | {{/code}} | ||
2320 | |||
2321 | (% id="Hcus_table.set_head" %) | ||
2322 | == [[cus_table.set_head>>||anchor="HTableDrawing28Experimental29"]](HMI) == | ||
2323 | |||
2324 | Prototype: custable.sethead(part, head) | ||
2325 | |||
2326 | Function: set header text | ||
2327 | |||
2328 | * part(string): object name | ||
2329 | * head(table): array, head[1] position represents column 1, and so on | ||
2330 | |||
2331 | Return: | ||
2332 | |||
2333 | * Success: true | ||
2334 | * Failure: nil, error message | ||
2335 | |||
2336 | Example: | ||
2337 | |||
2338 | {{code language="Lua"}} | ||
2339 | -- Set the header text of these 6 columns | ||
2340 | local head = { | ||
2341 | [1] = "c1", | ||
2342 | [2] = "c2", | ||
2343 | [3] = "c3", | ||
2344 | [4] = "c4", | ||
2345 | [5] = "c5", | ||
2346 | [6] = "c6", | ||
2347 | } | ||
2348 | cus_table.set_head("0_CST_0", head) | ||
2349 | {{/code}} | ||
2350 | |||
2351 | (% id="Hcus_table.set_mode" %) | ||
2352 | == [[cus_table.set_mode>>||anchor="HTableDrawing28Experimental29"]](HMI) == | ||
2353 | |||
2354 | Prototype: cus_table.set_mode(part, mode) | ||
2355 | |||
2356 | Function: Set the display mode, you can display in ascending or descending order, the default is ascending order | ||
2357 | |||
2358 | * part(string): object name | ||
2359 | * mode(number): 0 ascending order, 1 descending order | ||
2360 | |||
2361 | Return: | ||
2362 | |||
2363 | * Success: true | ||
2364 | * Failure: nil, error message | ||
2365 | |||
2366 | Example: | ||
2367 | |||
2368 | {{code language="Lua"}} | ||
2369 | -- Set to display in descending order | ||
2370 | cus_table.set_mode("0_CST_0", 1) | ||
2371 | {{/code}} | ||
2372 | |||
2373 | (% id="Hcus_table.page_up" %) | ||
2374 | == [[cus_table.page_up>>||anchor="HTableDrawing28Experimental29"]](HMI) == | ||
2375 | |||
2376 | Prototype: cus_table.page_up(part) | ||
2377 | |||
2378 | Function: execute page up | ||
2379 | |||
2380 | * part(string): object name | ||
2381 | |||
2382 | Return: | ||
2383 | |||
2384 | * Success: | ||
2385 | ** true turn page up successfully | ||
2386 | ** false has reached the top page | ||
2387 | * Failure: nil, error message | ||
2388 | |||
2389 | Example: | ||
2390 | |||
2391 | {{code language="Lua"}} | ||
2392 | cus_table.page_up("0_CST_0") | ||
2393 | {{/code}} | ||
2394 | |||
2395 | (% id="Hcus_table.page_down" %) | ||
2396 | == [[cus_table.page_down>>||anchor="HTableDrawing28Experimental29"]](HMI) == | ||
2397 | |||
2398 | Prototype: cus_table.page_down(part) | ||
2399 | |||
2400 | Function: Execute page down | ||
2401 | |||
2402 | * part(string): object name | ||
2403 | |||
2404 | Return: | ||
2405 | |||
2406 | * Success: | ||
2407 | ** true turn page down successfully | ||
2408 | ** false to the bottom page | ||
2409 | * Failure: nil, error message | ||
2410 | |||
2411 | Example: | ||
2412 | |||
2413 | {{code language="Lua"}} | ||
2414 | cus_table.page_down("0_CST_0") | ||
2415 | {{/code}} | ||
2416 | |||
2417 | (% id="Hcus_table.get_index" %) | ||
2418 | == [[cus_table.get_index>>||anchor="HTableDrawing28Experimental29"]](HMI) == | ||
2419 | |||
2420 | Prototype: cus_table.get_index(part) | ||
2421 | |||
2422 | Function: Get the currently selected index | ||
2423 | |||
2424 | * part(string): object name | ||
2425 | |||
2426 | Return: | ||
2427 | |||
2428 | * Success: Index (number) | ||
2429 | * Failure: nil, error message | ||
2430 | |||
2431 | Example: | ||
2432 | |||
2433 | {{code language="Lua"}} | ||
2434 | print(cus_table.get_index("0_CST_0")) | ||
2435 | {{/code}} | ||
2436 | |||
2437 | (% id="Hcus_table.get_page" %) | ||
2438 | == [[cus_table.get_page>>||anchor="HTableDrawing28Experimental29"]](HMI) == | ||
2439 | |||
2440 | Prototype: cus_table.get_page(part) | ||
2441 | |||
2442 | Function: Get the current page and the total number of pages | ||
2443 | |||
2444 | * part(string): object name | ||
2445 | |||
2446 | Return: | ||
2447 | |||
2448 | * Success: current page (number), total page number (number) | ||
2449 | * Failure: nil, error message | ||
2450 | |||
2451 | Example: | ||
2452 | |||
2453 | {{code language="Lua"}} | ||
2454 | print(cus_table.get_page("0_CST_0")) | ||
2455 | {{/code}} | ||
2456 | |||
2457 | (% id="Hcus_table.refresh" %) | ||
2458 | == [[cus_table.refresh>>||anchor="HTableDrawing28Experimental29"]](HMI) == | ||
2459 | |||
2460 | Prototype: cus_table.refresh(part, data) | ||
2461 | |||
2462 | Function: Refresh the object, after performing page turning and modifying the data source, you need to call this function once to refresh the object display | ||
2463 | |||
2464 | * part(string): object name | ||
2465 | * data(table): data source, two~-~- dimensional array, data[1][1] position represents the content of the first row and the first column | ||
2466 | |||
2467 | Return: | ||
2468 | |||
2469 | * Success: true | ||
2470 | * Failure: nil, error message | ||
2471 | |||
2472 | Example: | ||
2473 | |||
2474 | {{code language="Lua"}} | ||
2475 | local t = { | ||
2476 | [1] = {1,2,3,4,5,6}, -- Row 1 | ||
2477 | [2] = {2,3,4,5,6,7}, -- Row 2 | ||
2478 | [3] = {3,4,5,6,7,8}, | ||
2479 | } | ||
2480 | cus_table.refresh("0_CST_0", t) | ||
2481 | {{/code}} | ||
2482 | |||
2483 | (% id="Hcus_table.reset" %) | ||
2484 | == [[cus_table.reset>>||anchor="HTableDrawing28Experimental29"]](HMI) == | ||
2485 | |||
2486 | Prototype: cus_table.reset(part) | ||
2487 | |||
2488 | Function: reset object, clear all settings | ||
2489 | |||
2490 | * part(string): object name | ||
2491 | |||
2492 | Return: | ||
2493 | |||
2494 | * Success: true | ||
2495 | * Failure: nil, error message | ||
2496 | |||
2497 | Example: | ||
2498 | |||
2499 | {{code language="Lua"}} | ||
2500 | cus_table.reset("0_CST_0") | ||
2501 | {{/code}} | ||
2502 | |||
2503 | == [[Example>>||anchor="HTableDrawing28Experimental29"]] == | ||
2504 | |||
2505 | Use Lua object, add table_test() in the press script, compile and run to see the effect | ||
2506 | |||
2507 | {{code language="Lua"}} | ||
2508 | function table_test() | ||
2509 | local head = { | ||
2510 | [1] = "order", | ||
2511 | [2] = "c2", | ||
2512 | [3] = "c3", | ||
2513 | [4] = "c4", | ||
2514 | [5] = "c5", | ||
2515 | [6] = "c6", | ||
2516 | } | ||
2517 | local t = { | ||
2518 | [1] = {1,2,3,4,5,6}, -- The first row, the table has 6 columns, so all need 6 data | ||
2519 | [2] = {2,3,4,5,6,7}, | ||
2520 | [3] = {3,4,5,6,7,8}, | ||
2521 | } | ||
2522 | local part = "0_CST_0" | ||
2523 | cus_table.set_cell(part, 10, 6, "2:1:1:1:1:1") | ||
2524 | cus_table.set_head(part, head) | ||
2525 | cus_table.refresh(part, t) | ||
2526 | print(cus_table.get_index(part)) | ||
2527 | print(cus_table.get_page(part)) | ||
2528 | end | ||
2529 | {{/code}} | ||
2530 | |||
2531 | (% id="HCurveDrawing28Experimental29" %) | ||
2532 | = Curve drawing = | ||
2533 | |||
2534 | (% id="HDescription" %) | ||
2535 | Description | ||
2536 | |||
2537 | The name of the library is: cus_curve | ||
2538 | |||
2539 | {{code language="lua"}} | ||
2540 | local style = { | ||
2541 | color = 0xff0000, -- color [0,0xffffff]: red | ||
2542 | linetype = 0, -- Line type [0,3]: 0 solid line, 1 dotted line, 2 short dotted line, 3 long dotted line | ||
2543 | width = 1, -- line width [1,9] | ||
2544 | visible = 1,-- visible attribute [0,1]: 0 invisible, 1 visible | ||
2545 | } | ||
2546 | {{/code}} | ||
2547 | |||
2548 | The following interface example assumes that there is a object named 0_CST_0 on screen 0 | ||
2549 | |||
2550 | {{info}} | ||
2551 | (% id="H270ENote:-4" %) | ||
2552 | ✎Note: | ||
2553 | |||
2554 | * It is currently an experimental function. If there is any modification in the future, please read the interface documentation carefully | ||
2555 | * The status of the object will be cleared after leaving the screen, and no status will be retained or recorded | ||
2556 | * It is able to draw up to 5 curves, 5 X-axis reference lines, and 5 Y-Axis reference lines at the same time | ||
2557 | {{/info}} | ||
2558 | |||
2559 | (% id="HList" %) | ||
2560 | List | ||
2561 | |||
2562 | |=Function|=Description | ||
2563 | |[[cus_curve.init>>||anchor="Hcus_curve.init"]](HMI)|Set the number of x and y major scale divisions | ||
2564 | |[[cus_curve.set_range>>||anchor="Hcus_curve.set_range"]](HMI)|Set the range of the specified axis | ||
2565 | |[[cus_curve.get_range>>||anchor="Hcus_curve.get_range"]](HMI)|Get the range of the specified axis | ||
2566 | |[[cus_curve.set_scale>>||anchor="Hcus_curve.set_scale"]](HMI)|Set the major scale style of the specified axis | ||
2567 | |[[cus_curve.get_scale>>||anchor="Hcus_curve.get_scale"]](HMI)|Get the major scale style of the specified axis | ||
2568 | |[[cus_curve.set_grid>>||anchor="Hcus_curve.set_grid"]](HMI)|Set the main grid style of the specified axis | ||
2569 | |[[cus_curve.get_grid>>||anchor="Hcus_curve.get_grid"]](HMI)|Get the main grid style of the specified axis | ||
2570 | |[[cus_curve.set_line>>||anchor="Hcus_curve.set_line"]](HMI)|Set the point and style of the curve | ||
2571 | |[[cus_curve.get_line>>||anchor="Hcus_curve.get_line"]](HMI)|Get the point and style of the curve | ||
2572 | |[[cus_curve.set_visible>>||anchor="Hcus_curve.set_visible"]](HMI)|Set the visibility of the curve | ||
2573 | |[[cus_curve.get_visible>>||anchor="Hcus_curve.get_visible"]](HMI)|Get the visibility of the curve | ||
2574 | |[[cus_curve.set_refer>>||anchor="Hcus_curve.set_refer"]](HMI)|Set a reference line | ||
2575 | |[[cus_curve.set_cursor>>||anchor="Hcus_curve.set_cursor"]](HMI)|Set the display of the click event cursor | ||
2576 | |[[cus_curve.draw_line>>||anchor="Hcus_curve.draw_line"]](HMI)|Draw a curve | ||
2577 | |[[cus_curve.refresh>>||anchor="Hcus_curve.refresh"]](HMI)|Refresh object | ||
2578 | |[[cus_curve.reset>>||anchor="Hcus_curve.reset"]](HMI)|Reset object | ||
2579 | |[[cus_curve.set_slider>>||anchor="Hcus_curve.set_slider"]]|Set the slider | ||
2580 | |Example| | ||
2581 | |||
2582 | (% id="Hcus_curve.init" %) | ||
2583 | == [[cus_curve.init>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2584 | |||
2585 | Prototype: cus_curve.init(part, x, y) | ||
2586 | |||
2587 | Function: Set the number of x and y major scale divisions | ||
2588 | |||
2589 | * part(string): part name | ||
2590 | * x(number): The number of major scale on the x~-~- axis [1, 10] | ||
2591 | * y(number): the number of major scale on the y~-~- axis [1, 10] | ||
2592 | |||
2593 | Return: | ||
2594 | |||
2595 | * Success: true | ||
2596 | * Failure: nil, error message | ||
2597 | |||
2598 | Example: | ||
2599 | |||
2600 | {{code language="Lua"}} | ||
2601 | -- Set 3x3 major scale | ||
2602 | cus_curve.init("0_CST_0", 3, 3) | ||
2603 | {{/code}} | ||
2604 | |||
2605 | (% id="Hcus_curve.set_range" %) | ||
2606 | == [[cus_curve.set_range>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2607 | |||
2608 | Prototype: cus_curve.set_range(part, axis, min, max) | ||
2609 | |||
2610 | Function: Set the range of the specified axis | ||
2611 | |||
2612 | * part(string): object name | ||
2613 | * axis(string): specify the axis, x or y | ||
2614 | * min(number): minimum value | ||
2615 | * max(number): maximum value | ||
2616 | |||
2617 | Return: | ||
2618 | |||
2619 | * Success: true | ||
2620 | * Failure: nil, error message | ||
2621 | |||
2622 | Example: | ||
2623 | |||
2624 | {{code language="Lua"}} | ||
2625 | -- Set the x-- axis range [1,4] | ||
2626 | cus_curve.set_range("0_CST_0", "x", 1, 4) | ||
2627 | -- Set the y-- axis range [1,4] | ||
2628 | cus_curve.set_range("0_CST_0", "y", 1, 4) | ||
2629 | {{/code}} | ||
2630 | |||
2631 | (% id="Hcus_curve.get_range" %) | ||
2632 | == [[cus_curve.get_range>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2633 | |||
2634 | Prototype: cus_curve.get_range(part, axis) | ||
2635 | |||
2636 | Function: Get the range of the specified axis | ||
2637 | |||
2638 | * part(string): object name | ||
2639 | * axis(string): specify the axis, x or y | ||
2640 | |||
2641 | Return: | ||
2642 | |||
2643 | * Success: minimum (number), maximum (number) | ||
2644 | * Failure: nil, error message | ||
2645 | |||
2646 | Example: | ||
2647 | |||
2648 | {{code language="Lua"}} | ||
2649 | local xmin, xmax = cus_curve.get_range("0_CST_0", "x") | ||
2650 | print(xmin, xmax) | ||
2651 | {{/code}} | ||
2652 | |||
2653 | (% id="Hcus_curve.set_scale" %) | ||
2654 | == [[cus_curve.set_scale>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2655 | |||
2656 | Prototype: cus_curve.set_scale(part, axis, style) | ||
2657 | |||
2658 | Function: Set the major scale style of the specified axis | ||
2659 | |||
2660 | * part(string): object name | ||
2661 | * axis(string): specify the axis, x or y | ||
2662 | * style(table): style sheet, [[format>>path:#linestyle]] | ||
2663 | |||
2664 | Return: | ||
2665 | |||
2666 | * Success: true | ||
2667 | * Failure: nil, error message | ||
2668 | |||
2669 | Example: | ||
2670 | |||
2671 | {{code language="Lua"}} | ||
2672 | local style = { | ||
2673 | color = 0x000000, | ||
2674 | linetype = 0, | ||
2675 | width = 1, | ||
2676 | visible = 1, | ||
2677 | } | ||
2678 | cus_curve.set_scale("0_CST_0", "x", style) | ||
2679 | cus_curve.set_scale("0_CST_0", "y", style) | ||
2680 | {{/code}} | ||
2681 | |||
2682 | (% id="Hcus_curve.get_scale" %) | ||
2683 | == [[cus_curve.get_scale>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2684 | |||
2685 | Prototype: cus_curve.get_scale(part, axis) | ||
2686 | |||
2687 | Function: Get the major scale style of the specified axis | ||
2688 | |||
2689 | * part(string): object name | ||
2690 | * axis(string): specify the axis, x or y | ||
2691 | |||
2692 | Return: | ||
2693 | |||
2694 | * Success: style sheet (table) | ||
2695 | * Failure: nil, error message | ||
2696 | |||
2697 | Example: | ||
2698 | |||
2699 | {{code language="Lua"}} | ||
2700 | local x_scale_style = cus_curve.get_scale("0_CST_0", "x") | ||
2701 | {{/code}} | ||
2702 | |||
2703 | (% id="Hcus_curve.set_grid" %) | ||
2704 | == [[cus_curve.set_grid>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2705 | |||
2706 | Prototype: cus_curve.set_grid(part, axis, style) | ||
2707 | |||
2708 | Function: Set the main grid style of the specified axis | ||
2709 | |||
2710 | * part(string): object name | ||
2711 | * axis(string): specify the axis, x or y | ||
2712 | * style(table): style sheet, [[format>>path:#linestyle]] | ||
2713 | |||
2714 | Return: | ||
2715 | |||
2716 | * Success: true | ||
2717 | * Failure: nil, error message | ||
2718 | |||
2719 | Example: | ||
2720 | |||
2721 | {{code language="Lua"}} | ||
2722 | local gridStyle = { | ||
2723 | color = 0xcccccc, | ||
2724 | linetype = 2, | ||
2725 | width = 1, | ||
2726 | visible = 1, | ||
2727 | } | ||
2728 | cus_curve.set_grid("0_CST_0", "x", gridStyle) | ||
2729 | cus_curve.set_grid("0_CST_0", "y", gridStyle) | ||
2730 | {{/code}} | ||
2731 | |||
2732 | (% id="Hcus_curve.get_grid" %) | ||
2733 | == [[cus_curve.get_grid>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2734 | |||
2735 | Prototype: cus_curve.get_grid(part, axis) | ||
2736 | |||
2737 | Function: Get the main grid style of the specified axis | ||
2738 | |||
2739 | * part(string): object name | ||
2740 | * axis(string): specify the axis, x or y | ||
2741 | |||
2742 | Return: | ||
2743 | |||
2744 | * Success: style sheet (table) | ||
2745 | * Failure: nil, error message | ||
2746 | |||
2747 | Example: | ||
2748 | |||
2749 | {{code language="Lua"}} | ||
2750 | local x_grid_style = cus_curve.get_grid("0_CST_0", "x") | ||
2751 | {{/code}} | ||
2752 | |||
2753 | (% id="Hcus_curve.set_line" %) | ||
2754 | == [[cus_curve.set_line>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2755 | |||
2756 | Prototype: cus_curve.set_line(part, id, num, style) | ||
2757 | |||
2758 | Function: Set the number of points and style of the curve | ||
2759 | |||
2760 | * part(string): object name | ||
2761 | * id(number): curve ID | ||
2762 | * num(number): The number of points that can be [0, 1000] for each curve | ||
2763 | * style(table): style sheet, see the beginning of this section for the format | ||
2764 | |||
2765 | Return: | ||
2766 | |||
2767 | * Success: true | ||
2768 | * Failure: nil, error message | ||
2769 | |||
2770 | Example: | ||
2771 | |||
2772 | {{code language="Lua"}} | ||
2773 | local line1Style = { | ||
2774 | color = 0x00868b, | ||
2775 | linetype = 0, | ||
2776 | width = 1, | ||
2777 | visible = 1, | ||
2778 | } | ||
2779 | -- Set the properties of line 1 | ||
2780 | cus_curve.set_line("0_CST_0", 1, 4, line1Style) | ||
2781 | {{/code}} | ||
2782 | |||
2783 | (% id="Hcus_curve.get_line" %) | ||
2784 | == [[cus_curve.get_line>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2785 | |||
2786 | Prototype: cus_curve.get_line(part, id) | ||
2787 | |||
2788 | Function: Get the number of points and style of the curve | ||
2789 | |||
2790 | * part(string): object name | ||
2791 | * id(number): curve ID | ||
2792 | |||
2793 | Return: | ||
2794 | |||
2795 | * Success: points (number), style sheet (table) | ||
2796 | * Failure: nil, error message | ||
2797 | |||
2798 | Example: | ||
2799 | |||
2800 | {{code language="Lua"}} | ||
2801 | local num, style = cus_curve.get_line("0_CST_0", 1) | ||
2802 | {{/code}} | ||
2803 | |||
2804 | (% id="Hcus_curve.set_visible" %) | ||
2805 | == [[cus_curve.set_visible>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2806 | |||
2807 | Prototype: cus_curve.set_visible(part, id, visible) | ||
2808 | |||
2809 | Function: Set the visibility of the curve | ||
2810 | |||
2811 | * part(string): object name | ||
2812 | * id(number): curve ID | ||
2813 | * visible(number):[[visibility>>path:#linestyle]] | ||
2814 | |||
2815 | Return: | ||
2816 | |||
2817 | * Success: true | ||
2818 | * Failure: nil, error message | ||
2819 | |||
2820 | Example: | ||
2821 | |||
2822 | {{code language="Lua"}} | ||
2823 | -- Hide curve 1 | ||
2824 | cus_curve.set_visible("0_CST_0", 1, 0) | ||
2825 | {{/code}} | ||
2826 | |||
2827 | (% id="Hcus_curve.get_visible" %) | ||
2828 | == [[cus_curve.get_visible>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2829 | |||
2830 | Prototype: cus_curve.get_visible(part, id) | ||
2831 | |||
2832 | Function: Get the visibility of the curve | ||
2833 | |||
2834 | * part(string): object name | ||
2835 | * id(number): curve ID | ||
2836 | |||
2837 | Return: | ||
2838 | |||
2839 | * Success: visibility (number) | ||
2840 | * Failure: nil, error message | ||
2841 | |||
2842 | Example: | ||
2843 | |||
2844 | {{code language="Lua"}} | ||
2845 | local visable = cus_curve.get_visible("0_CST_0", 1) | ||
2846 | {{/code}} | ||
2847 | |||
2848 | (% id="Hcus_curve.set_refer" %) | ||
2849 | == [[cus_curve.set_refer>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2850 | |||
2851 | Prototype: cus_curve.set_refer(part, axis, id, num, style) | ||
2852 | |||
2853 | Function: Set a reference line | ||
2854 | |||
2855 | * part(string): object name | ||
2856 | * axis(string): specify the axis, x or y | ||
2857 | * id(number): reference line ID | ||
2858 | * num(number): the value of the reference line | ||
2859 | * style(table): style sheet | ||
2860 | |||
2861 | Return: | ||
2862 | |||
2863 | * Success: true | ||
2864 | * Failure: nil, error message | ||
2865 | |||
2866 | Example: | ||
2867 | |||
2868 | {{code language="Lua"}} | ||
2869 | local referStyle = { | ||
2870 | color = 0x00468b, | ||
2871 | linetype = 0, | ||
2872 | width = 1, | ||
2873 | visible = 1, | ||
2874 | } | ||
2875 | -- Set a reference line perpendicular to the x axis and x=1.5 | ||
2876 | cus_curve.set_refer("0_CST_0", "x", 1, 1.5, referStyle) | ||
2877 | {{/code}} | ||
2878 | |||
2879 | (% id="Hcus_curve.set_cursor" %) | ||
2880 | == [[cus_curve.set_cursor>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2881 | |||
2882 | Prototype: cus_curve.set_cursor(part, mode, style) | ||
2883 | |||
2884 | Function: Set the display of the click event cursor | ||
2885 | |||
2886 | * part(string): object name | ||
2887 | * mode(number): display mode, 0 integer value, 1 floating value | ||
2888 | * style(table): style sheet, see the beginning of this section for the format | ||
2889 | |||
2890 | Return: | ||
2891 | |||
2892 | * Success: true | ||
2893 | * Failure: nil, error message | ||
2894 | |||
2895 | Example: | ||
2896 | |||
2897 | {{code language="Lua"}} | ||
2898 | local cursorStyle = { | ||
2899 | color = 0xff0000, -- color | ||
2900 | linetype = 2, -- line type | ||
2901 | width = 1, -- line width | ||
2902 | visible = 1, -- visible or not | ||
2903 | } | ||
2904 | cus_curve.set_cursor("0_CST_0", 0, cursorStyle) | ||
2905 | {{/code}} | ||
2906 | |||
2907 | (% id="Hcus_curve.draw_line" %) | ||
2908 | == [[cus_curve.draw_line>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2909 | |||
2910 | Prototype: cus_curve.draw_line(part, id, data) | ||
2911 | |||
2912 | Function: Draw a curve and connect all the coordinates into a line in the increasing order of the array | ||
2913 | |||
2914 | * part(string): object name | ||
2915 | * id(number): curve ID | ||
2916 | * data(table): a one~-~- dimensional data table, odd~-~- numbered index represents x, even~-~- numbered index represents y, such as: | ||
2917 | (data[1], data[2]) represents the first coordinate (x1, y1), | ||
2918 | (data[3], data[4]) represents the second coordinate (x2, y2), | ||
2919 | |||
2920 | Return: | ||
2921 | |||
2922 | * Success: true | ||
2923 | * Failure: nil, error message | ||
2924 | |||
2925 | Example: | ||
2926 | |||
2927 | {{code language="Lua"}} | ||
2928 | local xy = {1, 3, 2, 1, 3, 4, 4, 1} | ||
2929 | cus_curve.draw_line("0_CST_0", 1, xy) | ||
2930 | {{/code}} | ||
2931 | |||
2932 | (% id="Hcus_curve.refresh" %) | ||
2933 | == [[cus_curve.refresh>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2934 | |||
2935 | Prototype: cus_curve.refresh(part) | ||
2936 | |||
2937 | Function: Refresh the object, call this interface after setting the scale, grid, label and line drawing to refresh the curve object and display it | ||
2938 | |||
2939 | * part(string): object name | ||
2940 | |||
2941 | Return: | ||
2942 | |||
2943 | * Success: true | ||
2944 | * Failure: nil, error message | ||
2945 | |||
2946 | Example: | ||
2947 | |||
2948 | {{code language="Lua"}} | ||
2949 | cus_curve.refresh("0_CST_0") | ||
2950 | {{/code}} | ||
2951 | |||
2952 | (% id="Hcus_curve.reset" %) | ||
2953 | == [[cus_curve.reset>>||anchor="HCurveDrawing28Experimental29"]](HMI) == | ||
2954 | |||
2955 | Prototype: cus_curve.reset(part) | ||
2956 | |||
2957 | Function: reset object, clear all settings | ||
2958 | |||
2959 | * part(string): object name | ||
2960 | |||
2961 | Return: | ||
2962 | |||
2963 | * Success: true | ||
2964 | * Failure: nil, error message | ||
2965 | |||
2966 | Example: | ||
2967 | |||
2968 | {{code language="Lua"}} | ||
2969 | cus_curve.reset("0_CST_0") | ||
2970 | {{/code}} | ||
2971 | |||
2972 | == [[cus_curve.set_slider>>||anchor="HCurveDrawing28Experimental29"]] == | ||
2973 | |||
2974 | Prototype: cus_curve.set_slider(part [, config] ) | ||
2975 | |||
2976 | Function: Set curve slider and display the data selected by the slider. | ||
2977 | |||
2978 | * part(string): object name | ||
2979 | * config(table): Slider configuration option | ||
2980 | |||
2981 | Note: | ||
2982 | |||
2983 | {{code language="lua"}} | ||
2984 | --@1、Slider style configuration (the following are the default values of all parameters, select the specified configuration as needed): | ||
2985 | local config = { | ||
2986 | sliderType = 0, -- type[0,1], 0: x axis index, 1:y axis index | ||
2987 | format = { | ||
2988 | 0, -- Label data format (Data format to be retained) [0,6] | ||
2989 | 1, -- Channel 1 data format (Data format to be retained) [0,6] | ||
2990 | 2, -- Channel 2 data format (Data format to be retained) [0,6] | ||
2991 | 2, -- Channel 3 data format (Data format to be retained) [0,6] | ||
2992 | 2, -- Channel 4 data format (Data format to be retained) [0,6] | ||
2993 | 5 -- Channel 5 data format (Data format to be retained) [0,6] | ||
2994 | }, | ||
2995 | func = sliderCallback, -- Handle the label data callback function | ||
2996 | lineColor = 0xff0000, -- slider color,[0x000000,0xffffff] | ||
2997 | lineType = 0, -- slider type,[0,3] | ||
2998 | lineWidth = 3, -- slider width,[0,10] | ||
2999 | fontSize = 15, -- text font size,[5,512] | ||
3000 | bgColor = 0xff7f00, -- background color,[0x0000,0xffffff] | ||
3001 | bgOpacity = 50 -- Background transparency,[0,100] | ||
3002 | } | ||
3003 | |||
3004 | -- @2、note:Callback function parameters format is as follows (the following are the default values, change them as needed): | ||
3005 | --srcData = { | ||
3006 | -- title = {name = "index = ", data = "7.00000"} | ||
3007 | -- label[5] = { | ||
3008 | -- {name = "Channel 1 : ", data = "3.00000"}, | ||
3009 | -- {name = "Channel 2 : ", data = "4.00000"}, | ||
3010 | -- {name = "Channel 3 : ", data = "5.00000"}, | ||
3011 | -- {name = "Channel 4 : ", data = "6.00000"}, | ||
3012 | -- {name = "Channel 5 : ", data = "7.00000"}, | ||
3013 | -- } | ||
3014 | --} | ||
3015 | -- Example of callback function usage: (srcData is given internally as a parameter, and you need to pay attention to the format when processing the data) | ||
3016 | local function sliderCallback(srcData) | ||
3017 | |||
3018 | dstData = {} | ||
3019 | dstData.title = srcData.title | ||
3020 | dstData.label = srcData.label | ||
3021 | -- Change label title | ||
3022 | dstData.title.name = "date = " | ||
3023 | -- dstData.title.data = string.format("%f", tonumber(srcData.title.data)) | ||
3024 | -- Change data | ||
3025 | dstData.label[1].name = "Temperature: " | ||
3026 | dstData.label[1].data = srcData.label[1].data.."(x10 ℃)" | ||
3027 | dstData.label[2].name = "humidity: " | ||
3028 | dstData.label[2].data = srcData.label[2].data.."(x10 %)" | ||
3029 | dstData.label[3].name = "luminous intensity: " | ||
3030 | dstData.label[3].data = srcData.label[3].data.."(x100 cd/㎡)" | ||
3031 | dstData.label[4].name = "wind power: " | ||
3032 | dstData.label[4].data = srcData.label[4].data.."(grade)" | ||
3033 | dstData.label[5].name = "PM2.5 : " | ||
3034 | dstData.label[5].data = srcData.label[5].data.."( x10)" | ||
3035 | |||
3036 | return dstData | ||
3037 | end | ||
3038 | {{/code}} | ||
3039 | |||
3040 | Example (For details, see Example 2): | ||
3041 | |||
3042 | {{code language="lua"}} | ||
3043 | -- Called as the default configuration | ||
3044 | cus_curve.set_slider(partName) | ||
3045 | {{/code}} | ||
3046 | |||
3047 | == [[Example>>||anchor="HCurveDrawing28Experimental29"]] == | ||
3048 | |||
3049 | (% class="wikigeneratedid" id="Hexample1" %) | ||
3050 | **example 1** | ||
3051 | |||
3052 | Use Lua object, add curve_test() in the falling script, compile and run to see the effect | ||
3053 | |||
3054 | {{code language="Lua"}} | ||
3055 | function curve_test() | ||
3056 | local style = { | ||
3057 | color = 0x000000, | ||
3058 | linetype = 0, | ||
3059 | width = 1, | ||
3060 | visible = 1, | ||
3061 | } | ||
3062 | local gridStyle = { | ||
3063 | color = 0xcccccc, | ||
3064 | linetype = 2, | ||
3065 | width = 1, | ||
3066 | visible = 1, | ||
3067 | } | ||
3068 | local line1Style = { | ||
3069 | color = 0x00868b, | ||
3070 | linetype = 0, | ||
3071 | width = 1, | ||
3072 | visible = 1, | ||
3073 | } | ||
3074 | local referStyle = { | ||
3075 | color = 0x00468b, | ||
3076 | linetype = 0, | ||
3077 | width = 1, | ||
3078 | visible = 1, | ||
3079 | } | ||
3080 | local cursorStyle = { | ||
3081 | color = 0xff0000, | ||
3082 | linetype = 2, | ||
3083 | width = 1, | ||
3084 | visible = 1, | ||
3085 | } | ||
3086 | local xy = {1, 3, 2, 1, 3, 4, 4, 1} | ||
3087 | local part = "0_CST_0" | ||
3088 | cus_curve.init(part, 3, 3) | ||
3089 | cus_curve.set_range(part, "x", 1, 4) | ||
3090 | cus_curve.set_range(part, "y", 1, 4) | ||
3091 | cus_curve.set_scale(part, "x", style) | ||
3092 | cus_curve.set_scale(part, "y", style) | ||
3093 | cus_curve.set_grid(part, "x", gridStyle) | ||
3094 | cus_curve.set_grid(part, "y", gridStyle) | ||
3095 | cus_curve.set_line(part, 1, 4, line1Style) | ||
3096 | -- cus_curve.set_visible(part, 1, 0) | ||
3097 | cus_curve.set_refer(part, "x", 1, 1.5, referStyle) | ||
3098 | cus_curve.set_cursor(part, 0, cursorStyle) | ||
3099 | cus_curve.draw_line(part, 1, xy) | ||
3100 | cus_curve.refresh(part) | ||
3101 | |||
3102 | print(cus_curve.get_range(part, "x")) | ||
3103 | print(cus_curve.get_scale(part, "x")) | ||
3104 | print(cus_curve.get_grid(part, "x")) | ||
3105 | print(cus_curve.get_label(part, "x")) | ||
3106 | print(cus_curve.get_line(part, 1)) | ||
3107 | print(cus_curve.get_visible(part, 1)) | ||
3108 | end | ||
3109 | {{/code}} | ||
3110 | |||
3111 | (% class="wikigeneratedid" id="Hexample2" %) | ||
3112 | **example 2** | ||
3113 | |||
3114 | Use the Lua widget, add sldierRun() to the press script or init script, and compile. | ||
3115 | |||
3116 | {{code language="lua"}} | ||
3117 | --1、 Curve object configuration | ||
3118 | local c_axisX = "x" | ||
3119 | local c_axisY = "y" | ||
3120 | local config = { | ||
3121 | partName = "2_CST_0", -- object name | ||
3122 | scaleX = 6, -- The number of x-axis scales | ||
3123 | scaleY = 6, -- The number of y-axis scales | ||
3124 | minValueX = 1, -- Minimum value of x-axis | ||
3125 | maxValueX = 7, -- Maximum value of x-axis | ||
3126 | minValueY = 0, -- Minimum value of x-axis | ||
3127 | maxValueY = 6, -- Maximum value of y-axis | ||
3128 | } | ||
3129 | |||
3130 | -- Obtain style configuration | ||
3131 | local function getStyle(color,linetype,width,visible) | ||
3132 | |||
3133 | -- This is a uniform style sheet for curves, used to control the display of curves | ||
3134 | local style = { | ||
3135 | color = 0xff0000, -- color[0,0xffffff]: red | ||
3136 | linetype = 0, -- line style[0,3]:0 full line,1 Dotted dashed line,2 short dashed line,3 long dashed line | ||
3137 | width = 1, -- line width[1,9] | ||
3138 | visible = 1, -- visual property[0,1]:0 invisible, 1 visible, | ||
3139 | } | ||
3140 | |||
3141 | if color~=nil then style.color = color end | ||
3142 | if linetype~=nil then style.linetype = linetype end | ||
3143 | if width~= nil then style.width = width end | ||
3144 | if visible~=nil then style.visible = visible end | ||
3145 | |||
3146 | return style | ||
3147 | end | ||
3148 | |||
3149 | --2、Initialize the table | ||
3150 | local function initCurva() | ||
3151 | -- Initialize the numbers of X and Y scales | ||
3152 | cus_curve.init(config.partName, config.scaleX, config.scaleY); | ||
3153 | |||
3154 | -- Initialize the data ranges of X and Y scales | ||
3155 | cus_curve.set_range(config.partName, c_axisX, config.minValueX, config.maxValueX); | ||
3156 | cus_curve.set_range(config.partName, c_axisY, config.minValueY, config.maxValueY); | ||
3157 | |||
3158 | -- Initialize the styles of X and Y scales | ||
3159 | cus_curve.set_scale(config.partName, c_axisX, getStyle(0x000000)) | ||
3160 | cus_curve.set_scale(config.partName, c_axisY, getStyle(0x000000)) | ||
3161 | |||
3162 | -- Initializing the grid | ||
3163 | cus_curve.set_grid(config.partName, c_axisX, getStyle(0x000000,1)) | ||
3164 | cus_curve.set_grid(config.partName, c_axisY, getStyle(0x000000,1)) | ||
3165 | |||
3166 | -- Initialize the number of curve points and styles | ||
3167 | cus_curve.set_line(config.partName, 1, 1000, getStyle(0x000000)) | ||
3168 | cus_curve.set_line(config.partName, 2, 1000, getStyle(0xff0000)) | ||
3169 | cus_curve.set_line(config.partName, 3, 1000, getStyle(0x00ff00)) | ||
3170 | cus_curve.set_line(config.partName, 4, 1000, getStyle(0x000ff0)) | ||
3171 | cus_curve.set_line(config.partName, 5, 1000, getStyle(0xaaff00)) | ||
3172 | |||
3173 | -- Set curve visibility | ||
3174 | cus_curve.set_visible(config.partName, 1, 1) | ||
3175 | |||
3176 | -- Draw curve | ||
3177 | local line1 = { 1,3.7, 2,3.9, 3,4.0, 4,3.9, 5,3.9, 6,3.8, 7,3.6} | ||
3178 | local line2 = {1,4.5, 2,5.0, 3,5.3, 4,4.9, 5,5.2, 6,4.8, 7,5.0} | ||
3179 | local line3 = {1,3.12, 2,3.00, 3,2.85, 4,2.96, 5,2.68, 6,2.70, 7,3.00} | ||
3180 | local line4 = {1,1, 2,1, 3,2, 4,1, 5,2, 6,1, 7,2} | ||
3181 | local line5 = {1,2.5, 2,2.4, 3,2.5, 4,2.3, 5,2.6, 6,2.5, 7,2.6} | ||
3182 | cus_curve.draw_line(config.partName, 1, line1) | ||
3183 | cus_curve.draw_line(config.partName, 2, line2) | ||
3184 | cus_curve.draw_line(config.partName, 3, line3) | ||
3185 | cus_curve.draw_line(config.partName, 4, line4) | ||
3186 | cus_curve.draw_line(config.partName, 5, line5) | ||
3187 | |||
3188 | -- Refresh to display | ||
3189 | cus_curve.refresh(config.partName) | ||
3190 | end | ||
3191 | |||
3192 | --3、Create a callback function | ||
3193 | local function sliderCallback(srcData) | ||
3194 | |||
3195 | dstData = {} | ||
3196 | dstData.title = srcData.title | ||
3197 | dstData.label = srcData.label | ||
3198 | -- Change the label title | ||
3199 | dstData.title.name = "date = " | ||
3200 | -- dstData.title.data = string.format("%f", tonumber(srcData.title.data)) | ||
3201 | -- Change the data | ||
3202 | dstData.label[1].name = "Temperature: " | ||
3203 | dstData.label[1].data = srcData.label[1].data.."(x10 ℃)" | ||
3204 | dstData.label[2].name = "Huminity: " | ||
3205 | dstData.label[2].data = srcData.label[2].data.."(x10 %)" | ||
3206 | dstData.label[3].name = "luminous intensity: " | ||
3207 | dstData.label[3].data = srcData.label[3].data.."(x100 cd/㎡)" | ||
3208 | dstData.label[4].name = "wind power: " | ||
3209 | dstData.label[4].data = srcData.label[4].data.."(grade)" | ||
3210 | dstData.label[5].name = "PM2.5 : " | ||
3211 | dstData.label[5].data = srcData.label[5].data.."( x10)" | ||
3212 | |||
3213 | return dstData | ||
3214 | end | ||
3215 | |||
3216 | --4、set slider | ||
3217 | local function setSlider() | ||
3218 | |||
3219 | -- Fill in the parameters as needed | ||
3220 | local sliderCfg = { | ||
3221 | sliderType = 0, -- type[0,1], 0: x axis index, 1:y axis index | ||
3222 | format = { | ||
3223 | 0, -- Label data format (Data format to be retained) [0,6] | ||
3224 | 1, -- Channel 1 data format (Data format to be retained) [0,6] | ||
3225 | 2, -- Channel 2 data format (Data format to be retained) [0,6] | ||
3226 | 2, -- Channel 3 data format (Data format to be retained) [0,6] | ||
3227 | 2, -- Channel 4 data format (Data format to be retained) [0,6] | ||
3228 | 5 -- Channel 5 data format (Data format to be retained)[0,6] | ||
3229 | }, | ||
3230 | func = sliderCallback, -- Handle the label data callback function | ||
3231 | lineColor = 0xff0000, -- slider color,[0x000000,0xffffff] | ||
3232 | lineType = 0, -- slider type,[0,3] | ||
3233 | lineWidth = 3, -- slider width, [0,10] | ||
3234 | fontSize = 15, -- text font size,[5,512] | ||
3235 | bgColor = 0xff7f00, -- background color,[0x0000,0xffffff] | ||
3236 | bgOpacity = 50 -- background transparency,[0,100] | ||
3237 | } | ||
3238 | |||
3239 | -- create a slider | ||
3240 | cus_curve.set_slider(config.partName,sliderCfg) | ||
3241 | end | ||
3242 | |||
3243 | -- Run the function | ||
3244 | function sldierRun() | ||
3245 | -- Initializing the table | ||
3246 | initCurva() | ||
3247 | |||
3248 | -- Set the slider | ||
3249 | setSlider() | ||
3250 | end | ||
3251 | {{/code}} | ||
3252 | |||
3253 | = MQTT = | ||
3254 | |||
3255 | (% class="wikigeneratedid" id="HDescription-7" %) | ||
3256 | **Description** | ||
3257 | |||
3258 | The library provides the MQTT client function, which can let HMI connect to the MQTT server | ||
3259 | |||
3260 | The name of the library is: **mqtt** | ||
3261 | |||
3262 | (% class="wikigeneratedid" id="HSafetyAuthentication" %) | ||
3263 | **Safety Authentication** | ||
3264 | |||
3265 | Only supports the method of username/password authentication | ||
3266 | |||
3267 | (% class="box infomessage" id="H270ENote:-5" %) | ||
3268 | ((( | ||
3269 | **✎Note:** This library cannot be run on the simulator, please debug and run on HMI | ||
3270 | |||
3271 | Only ig series can support this library. | ||
3272 | ))) | ||
3273 | |||
3274 | |=Function|=Description | ||
3275 | |[[mqtt.setup_cfg>>||anchor="Hmqtt.setup_cfg"]]|Get the configuration information of mqtt from the HMI | ||
3276 | |[[mqtt.create>>||anchor="Hmqtt.create"]]|Create object of mqtt client | ||
3277 | |[[mqtt.close>>||anchor="Hmqtt.close"]]|Close object of mqtt client | ||
3278 | |[[mqtt:connect>>||anchor="Hmqtt:connect"]]|Establish connection with the server | ||
3279 | |[[mqtt:disconnect>>||anchor="Hmqtt:disconnect"]]|Disconnect from server | ||
3280 | |[[mqtt:isconnected>>||anchor="Hmqtt:isconnected"]]|Whether connect to the server | ||
3281 | |[[mqtt:close>>||anchor="Hmqtt:close"]]|Close the mqtt client object | ||
3282 | |[[mqtt:subscribe>>||anchor="Hmqtt:subscribe"]]|Subscribe topic | ||
3283 | |[[mqtt:unsubscribe>>||anchor="Hmqtt:unsubscribe"]]|Unsubscribe topic | ||
3284 | |[[mqtt:publish>>||anchor="Hmqtt:publish"]]|Publish message | ||
3285 | |[[mqtt:on>>||anchor="Hmqtt:on"]]|Register message callback function | ||
3286 | |Example 1|Standard usage example of mqtt library | ||
3287 | |Example 2|How to easily get configuration parameters | ||
3288 | |||
3289 | == [[mqtt.setup_cfg>>||anchor="HMQTT"]] == | ||
3290 | |||
3291 | Prototype: mqtt.setup_cfg() | ||
3292 | |||
3293 | Function: Get the user MQTT parameters configured in the “Cloud" in the record settings | ||
3294 | |||
3295 | Parameters: None | ||
3296 | |||
3297 | Return: | ||
3298 | |||
3299 | * Success: 4 results | ||
3300 | Server address (string) | ||
3301 | Client ID (string) | ||
3302 | Connection (table) | ||
3303 | Will message (table) | ||
3304 | * Failure: nil | ||
3305 | |||
3306 | Example: [[Example 2>>path:https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HExample2]] | ||
3307 | |||
3308 | {{code language="Lua"}} | ||
3309 | local serverurl, clientid, conn, lwt = mqtt.setup_cfg() | ||
3310 | print(serverurl, clientid, conn, lwt) | ||
3311 | {{/code}} | ||
3312 | |||
3313 | == [[mqtt.create>>||anchor="HMQTT"]] == | ||
3314 | |||
3315 | Prototype: mqtt.create(serverurl, clientid) | ||
3316 | |||
3317 | Function: Create mqtt client object | ||
3318 | |||
3319 | Parameter: | ||
3320 | |||
3321 | * serverurl(string): server address, format: "protocol:~/~/host:port" | ||
3322 | protocol: tcp | ||
3323 | host: Hostname/IP address | ||
3324 | port: such as 1883 | ||
3325 | * clientid(string): client ID | ||
3326 | |||
3327 | Return: | ||
3328 | |||
3329 | * Success: mqtt object (mqtt) | ||
3330 | * Failure: nil, err | ||
3331 | |||
3332 | Example: [[Example 1>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HExample1]] | ||
3333 | |||
3334 | {{code language="Lua"}} | ||
3335 | -- 1. Use Get method | ||
3336 | local serverurl, clientid, conn, lwt = mqtt.setup_cfg() | ||
3337 | local obj, err = mqtt.create(serverurl, clientid) | ||
3338 | if obj then | ||
3339 | print("mqtt create ok") | ||
3340 | else | ||
3341 | print("mqtt create:", err) | ||
3342 | end | ||
3343 | |||
3344 | -- 2. Use the designated IP method, if 192.168.1.123 is an mqtt server then | ||
3345 | local obj2 = mqtt.create("tcp://192.168.1.123:1883", "demo123") | ||
3346 | {{/code}} | ||
3347 | |||
3348 | == [[mqtt.close>>||anchor="HMQTT"]] == | ||
3349 | |||
3350 | Prototype: mqtt.close(obj) | ||
3351 | |||
3352 | Function: close the mqtt client object | ||
3353 | |||
3354 | Parameter: | ||
3355 | |||
3356 | * obj(mqtt): obj is the object created and returned by mqtt.create | ||
3357 | |||
3358 | Return: | ||
3359 | |||
3360 | * Success: true | ||
3361 | * Failure: nil | ||
3362 | |||
3363 | Example: | ||
3364 | |||
3365 | {{code language="Lua"}} | ||
3366 | local obj2 = mqtt.create("tcp://192.168.1.123:1883", "demo123") | ||
3367 | if obj2 then | ||
3368 | print(mqtt.close(obj2)) | ||
3369 | end | ||
3370 | {{/code}} | ||
3371 | |||
3372 | == [[mqtt:connect>>||anchor="HMQTT"]] == | ||
3373 | |||
3374 | Prototype: mqtt:connect(conn[, lwt]) | ||
3375 | |||
3376 | Function: establish connection with the server | ||
3377 | |||
3378 | Parameter: | ||
3379 | |||
3380 | * conn(table): table, require the following fields | ||
3381 | conn.username(string): username | ||
3382 | conn.password(string): password | ||
3383 | conn.keepalive(number): Keep-Alive Interval, unit: second, default 60 seconds | ||
3384 | conn.cleansession(number): Clean Session, default 1 | ||
3385 | This function is used to control the behavior when connecting & disconnecting. Both client and server retain session information. This information is used to ensure arrived "at least once" and "accurately once" , and includes the topic subscribed by the client. You can choose to reserve or ignore the session message. The settings are as follows: | ||
3386 | 1 (Clean): If the session exists previously and is 1, then all messages from session on the client and server are cleaned. | ||
3387 | 0 (Reserved): Conversely, both the client and the server use the previous session. If the previous session does not exist, start a new session. | ||
3388 | * lwt(table): Will message, also called Last Will and Testament, require the following fields | ||
3389 | lwt.topic(string): Topic | ||
3390 | lwt.message(string): Message body | ||
3391 | lwt.qos(number): QoS value, default 0 | ||
3392 | lwt.retain(number): Reserved flag bit, default 0 | ||
3393 | |||
3394 | Return: | ||
3395 | |||
3396 | * Success: true | ||
3397 | * Failure: nil, err | ||
3398 | |||
3399 | Example: [[Example 1>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HExample1]] | ||
3400 | |||
3401 | == [[mqtt:disconnect>>||anchor="HMQTT"]] == | ||
3402 | |||
3403 | Prototype: mqtt:disconnect([timeout]) | ||
3404 | |||
3405 | Function: Disconnect from the server | ||
3406 | |||
3407 | Parameter: | ||
3408 | |||
3409 | -timeout(number): Connection Timeout, unit: milliseconds, the default is 10000 milliseconds | ||
3410 | |||
3411 | Return: | ||
3412 | |||
3413 | * Success: true | ||
3414 | * Failure: nil | ||
3415 | |||
3416 | Example: [[Example 1>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HExample1]] | ||
3417 | |||
3418 | == [[mqtt:isconnected>>||anchor="HMQTT"]] == | ||
3419 | |||
3420 | Prototype: mqtt:isconnected() | ||
3421 | |||
3422 | Function: Whether connect to the server | ||
3423 | |||
3424 | Parameters: None | ||
3425 | |||
3426 | Return: | ||
3427 | |||
3428 | * Success: true Connected | ||
3429 | * Failure: false Not connected, other unknown situations also return false | ||
3430 | |||
3431 | Example: [[Example 1>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HExample1]] | ||
3432 | |||
3433 | == [[mqtt:close>>||anchor="HMQTT"]] == | ||
3434 | |||
3435 | Prototype: mqtt:close() | ||
3436 | |||
3437 | Function: Close the object of mqtt client (If it is connected to the server and not manually disconnected, it will be automatically disconnected) | ||
3438 | |||
3439 | Parameters: None | ||
3440 | |||
3441 | Return: | ||
3442 | |||
3443 | * Success: true | ||
3444 | * Failure: nil, err | ||
3445 | |||
3446 | Example: [[Example 1>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HExample1]] | ||
3447 | |||
3448 | == [[mqtt:subscribe>>||anchor="HMQTT"]] == | ||
3449 | |||
3450 | Prototype: mqtt:subscribe(topic, qos) | ||
3451 | |||
3452 | Function: Subscribe topic | ||
3453 | |||
3454 | Parameter: | ||
3455 | |||
3456 | * topic(string): Topic name | ||
3457 | * qos(number): QoS value | ||
3458 | |||
3459 | Return: | ||
3460 | |||
3461 | * Success: true | ||
3462 | * Failure: nil, err | ||
3463 | |||
3464 | Example: [[Example 1>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HExample1]] | ||
3465 | |||
3466 | == [[mqtt:unsubscribe>>||anchor="HMQTT"]] == | ||
3467 | |||
3468 | Prototype: mqtt:unsubscribe(topic) | ||
3469 | |||
3470 | Function: Unsubscribed topic | ||
3471 | |||
3472 | Parameter: | ||
3473 | |||
3474 | * topic(string): Topic name | ||
3475 | |||
3476 | Return: | ||
3477 | |||
3478 | * Success: true | ||
3479 | * Failure: nil, err | ||
3480 | |||
3481 | Example: [[Example 1>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HExample1]] | ||
3482 | |||
3483 | == [[mqtt:publish>>||anchor="HMQTT"]] == | ||
3484 | |||
3485 | Prototype: mqtt:publish(topic, message, qos, retain[, timeout]) | ||
3486 | |||
3487 | Function: Publish message | ||
3488 | |||
3489 | Parameter: | ||
3490 | |||
3491 | * topic(string): Topic name | ||
3492 | * message(string): Message body | ||
3493 | * qos(number): QoS value | ||
3494 | * retain(number): Reserved flag | ||
3495 | * timeout(number): Waiting Timeout of publish response, unit: milliseconds (only works when QoS > 0) | ||
3496 | |||
3497 | Return: | ||
3498 | |||
3499 | * Success: true | ||
3500 | * Failure: nil, err | ||
3501 | |||
3502 | Example: [[Example 1>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HExample1]] | ||
3503 | |||
3504 | == [[mqtt:on>>||anchor="HMQTT"]] == | ||
3505 | |||
3506 | Prototype: mqtt:on(method, callback) | ||
3507 | |||
3508 | Function: Register message callback function | ||
3509 | |||
3510 | Parameter: | ||
3511 | |||
3512 | * method(string): Totally 3 types of events (Message/Arrived/Offline) | ||
3513 | |||
3514 | 1.message: This function will be called when a message is received | ||
3515 | |||
3516 | * Function prototype: function(string topic, string message) | ||
3517 | * Parameters: | ||
3518 | ** topic Topic name | ||
3519 | message Message content | ||
3520 | |||
3521 | 2.arrived: This function will be called after the publication is arrived | ||
3522 | |||
3523 | * Function prototype: function() | ||
3524 | * Parameters: None | ||
3525 | |||
3526 | 3.offline: This function will be called after the connection is lost | ||
3527 | |||
3528 | * Function prototype: function(string cause) | ||
3529 | * Function: | ||
3530 | ** cause Reason of loss connection | ||
3531 | * callback(function): callback function, call this function once after the event is triggered | ||
3532 | |||
3533 | Return: | ||
3534 | |||
3535 | * Success: true | ||
3536 | * Failure: nil, err | ||
3537 | |||
3538 | Example: [[Example 1>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HExample1]] | ||
3539 | |||
3540 | == Example 1 == | ||
3541 | |||
3542 | Effect: Demonstrate that the mqtt client sends and receives print data spontaneously | ||
3543 | |||
3544 | The operation steps are as follows: | ||
3545 | |||
3546 | 1. Create a new file named mqdemo.lua, and copy the following code to the file | ||
3547 | 1. Modify the configuration parameters in the function mqtt.create and the variable local config in the script | ||
3548 | 1. Place a Lua object on screen No.0, and fill in mqtt_test() in the falling type script | ||
3549 | 1. Compile and click object, let the object execute the function mqtt_test(), observe the debug console | ||
3550 | |||
3551 | {{code language="Lua"}} | ||
3552 | mqdemo_limits = 11 | ||
3553 | local obj | ||
3554 | local config = { | ||
3555 | username = "admin",-- account | ||
3556 | password = "password",-- password | ||
3557 | keepalive = 100, -- set the Keepalive Interval to 100 seconds | ||
3558 | cleansession = 0, -- keep the session | ||
3559 | } | ||
3560 | -- mqtt execution | ||
3561 | function mqtt_loop() | ||
3562 | if not obj then | ||
3563 | local err | ||
3564 | -- Create an object, please replace it with the IP address of the server you want to connect to | ||
3565 | obj, err = mqtt.create("tcp://192.168.40.226:1883", "clientid") | ||
3566 | if obj then | ||
3567 | -- Register callback for receiving message | ||
3568 | obj:on("message", | ||
3569 | function(topic, msg) | ||
3570 | print("mqtt msg:", string.format("%s:%s", topic, msg)) | ||
3571 | end | ||
3572 | ) | ||
3573 | -- Register callback for lost connection | ||
3574 | obj:on("offline", function(cause) print("mqtt offline:", cause) end) | ||
3575 | -- Register callback for sending message | ||
3576 | obj:on("arrived", function() print("msg arrived") end) | ||
3577 | else | ||
3578 | print("mqtt create err:", err) | ||
3579 | end | ||
3580 | else | ||
3581 | if obj:isconnected() then | ||
3582 | obj:publish("tomyself", "send message to my self", 0, 0) | ||
3583 | else | ||
3584 | -- Connect | ||
3585 | local stat, err = obj:connect(config) | ||
3586 | if stat == nil then | ||
3587 | -- Connection failed | ||
3588 | print("mqtt connect err:", err) | ||
3589 | else | ||
3590 | -- Subscribe topic after the connection is successful | ||
3591 | obj:subscribe("tomyself", 0) | ||
3592 | end | ||
3593 | end | ||
3594 | -- The following comment lines are only used as interface demonstration usage, know how to call | ||
3595 | -- obj:unsubscribe("tohmi") - Unsubscribe | ||
3596 | -- obj:disconnect() - disconnect | ||
3597 | -- obj:close() - close | ||
3598 | end | ||
3599 | end | ||
3600 | |||
3601 | function mqtt_test() | ||
3602 | -- Create a timer on screen No.0 | ||
3603 | timer.create(0, 0, 1000, mqtt_loop) | ||
3604 | end | ||
3605 | {{/code}} | ||
3606 | |||
3607 | == Example 2 == | ||
3608 | |||
3609 | Goal: Learn to use the interface function to get configuration parameters | ||
3610 | |||
3611 | Effect: Demonstrate that mqtt client sends and receives print data spontaneously, same as example 1 | ||
3612 | |||
3613 | The operation steps are as follows: | ||
3614 | |||
3615 | 1. Confirm that the User MQTT parameters are configured in the “Cloud" | ||
3616 | 1. Create a new file named mqdemo.lua, copy the following code to the file | ||
3617 | 1. Modify the configuration parameters in the function mqtt.create and the variable local config in the script | ||
3618 | 1. Place a Lua object on screen No.0, and fill in mqtt_test() in the falling type script | ||
3619 | 1. Compile and click object, let it execute the function mqtt_test(), observe the debug output | ||
3620 | |||
3621 | {{code language="Lua"}} | ||
3622 | mqdemo_limits = 11 | ||
3623 | local obj | ||
3624 | |||
3625 | -- mqtt execution | ||
3626 | function mqtt_loop() | ||
3627 | if not obj then | ||
3628 | local err | ||
3629 | -- Create an object, the parameters are taken from global variables | ||
3630 | obj, err = mqtt.create(URL, CLIENDID) | ||
3631 | if obj then | ||
3632 | -- Register callback for receiving message | ||
3633 | obj:on("message", | ||
3634 | function(topic, msg) | ||
3635 | print("mqtt msg:") | ||
3636 | print(string.format("topic: %s\\n msg: %s\\n", topic, msg)) | ||
3637 | end | ||
3638 | ) | ||
3639 | -- Register callback for lost connection | ||
3640 | obj:on("offline", function(cause) print("mqtt offline:", cause) end) | ||
3641 | -- Register callback for sending message | ||
3642 | obj:on("arrived", function() print("msg arrived") end) | ||
3643 | else | ||
3644 | print("mqtt create err:", err) | ||
3645 | end | ||
3646 | else | ||
3647 | if obj:isconnected() then | ||
3648 | obj:publish("tomyself", "send message to my self", 0, 0) | ||
3649 | else | ||
3650 | -- Connection, parameters are taken from global variables | ||
3651 | local stat, err = obj:connect(CONN, LWT) | ||
3652 | if stat == nil then | ||
3653 | -- Connection failed | ||
3654 | print("mqtt connect err:", err) | ||
3655 | else | ||
3656 | -- Subscribe topic after the connection is successful | ||
3657 | obj:subscribe("tomyself", 0) | ||
3658 | end | ||
3659 | end | ||
3660 | -- The following comment lines are only used as interface demonstration usage, know how to call | ||
3661 | -- obj:unsubscribe("tohmi") - Unsubscribe | ||
3662 | -- obj:disconnect() - disconnect | ||
3663 | -- obj:close() - close | ||
3664 | end | ||
3665 | end | ||
3666 | |||
3667 | function mqtt_test() | ||
3668 | -- Get parameters from the configuration and set them as global variables | ||
3669 | URL, CLIENDID, CONN, LWT = mqtt.setup_cfg() | ||
3670 | -- Create a timer on screen No.0 | ||
3671 | timer.create(0, 0, 1000, mqtt_loop) | ||
3672 | end | ||
3673 | {{/code}} | ||
3674 | |||
3675 | = Cloud interface = | ||
3676 | |||
3677 | (% class="wikigeneratedid" id="HDescription-8" %) | ||
3678 | **Description** | ||
3679 | |||
3680 | The library provides the function of getting data collected by "Cloud". | ||
3681 | |||
3682 | In the configuration interface of the "Cloud", there is a checkbox for the tags for upload selection. If you make the following selections: | ||
3683 | |||
3684 | 1. Check upload to Cloud: the real-time data, Data records, and Alarm record of the tags will be pushed to the Cloud. | ||
3685 | 1. Check upload to User MQTT: the real-time data, Data records, and Alarm record of the tags will all be transmitted through the lua callback function, and the user can upload these data to the third-party server. | ||
3686 | |||
3687 | That is, the choice of the upload selection of the data, can only choose one from two. | ||
3688 | |||
3689 | **For example, if you choose upload to Cloud, all interfaces of this library can be considered invalid and cannot be used.** | ||
3690 | |||
3691 | The name of the library is: **cloud**, some of which are callback functions | ||
3692 | |||
3693 | (% class="box infomessage" id="H270ENote:-6" %) | ||
3694 | ((( | ||
3695 | **✎Note:** This library cannot be run on the simulator, please debug and run on HMI. | ||
3696 | ))) | ||
3697 | |||
3698 | == Data type table == | ||
3699 | |||
3700 | |=Value|=Description | ||
3701 | |100|16-bit binary | ||
3702 | |101|16-bit octal | ||
3703 | |102|16-bit hexadecimal | ||
3704 | |103|16-bit BCD | ||
3705 | |104|16-bit signed decimal | ||
3706 | |105|16-bit unsigned decimal | ||
3707 | |200|32-bit binary | ||
3708 | |201|32-bit octal | ||
3709 | |202|32-bit hexadecimal | ||
3710 | |203|32-bit BCD | ||
3711 | |204|32-bit signed decimal | ||
3712 | |205|32-bit unsigned decimal | ||
3713 | |206|32-bit (single precision) floating | ||
3714 | |400|64-bit binary | ||
3715 | |401|64-bit octal | ||
3716 | |402|64-bit hexadecimal | ||
3717 | |403|64-bit BCD | ||
3718 | |404|64-bit signed decimal | ||
3719 | |405|64-bit unsigned decimal | ||
3720 | |406|64-bit (double precision) floating | ||
3721 | |1000|String format | ||
3722 | |||
3723 | == Function table == | ||
3724 | |||
3725 | |=Function|=Description | ||
3726 | |[[cloud.get_history_def>>||anchor="Hcloud.get_history_def"]](HMI)|Get the configuration of Data record from HMI | ||
3727 | |[[cloud_history_data>>||anchor="Hcloud_history_data"]](HMI)|Data recording callback function | ||
3728 | |[[cloud.get_alarm_def>>||anchor="Hcloud.get_alarm_def"]](HMI)|Get the configuration of the alarm record on the configuration software | ||
3729 | |[[cloud_alarm_data>>||anchor="Hcloud_alarm_data"]](HMI)|Callback function of alarm record | ||
3730 | |[[cloud.get_groupdesc/bns_get_groupdesc>>||anchor="Hcloud.get_groupdesc"]]|Get the configurations of all groups (Only valid in "OpenCloud mode") | ||
3731 | |[[cloud.get_datadesc/bns_get_datadesc>>||anchor="Hcloud.get_datadesc"]]|Get the configurations of all collection points (Only valid in "OpenCloud mode") | ||
3732 | |[[cloud.get_groupdata/bns_get_groupdata>>||anchor="Hcloud.get_groupdata"]]|Get the collection point information of one group (Only valid in "OpenCloud mode") | ||
3733 | |[[cloud.set_data/bns_set_data>>||anchor="Hcloud.set_data"]]|Set a collection point (Only valid in "OpenCloud mode") | ||
3734 | |[[cloud.get_data/bns_get_data>>||anchor="Hcloud.get_data"]]|Get the data of a collection point(Only valid in "OpenCloud mode") | ||
3735 | |[[cloud.get_alldata/bns_get_alldata>>||anchor="Hcloud.get_alldata"]]|Get all the collection points that meet the conditions (Only valid in "OpenCloud mode") | ||
3736 | |[[cloud.set_onecache/bns_set_onecache>>||anchor="Hcloud.set_onecache"]]|Save a piece of data to the cache (Only valid in "OpenCloud mode") | ||
3737 | |[[cloud.get_allcache/bns_get_allcache>>||anchor="Hcloud.get_allcache"]]|Get data from the cache (Only valid in "OpenCloud mode") | ||
3738 | |||
3739 | == [[cloud.get_history_def>>||anchor="HFunctiontable"]](HMI) == | ||
3740 | |||
3741 | Prototype: cloud.get_//history_//def() | ||
3742 | |||
3743 | Function: Get the configuration of Data record from HMI, only get channels with the Cloud checked | ||
3744 | |||
3745 | Parameters: None | ||
3746 | |||
3747 | Return: | ||
3748 | |||
3749 | * Success: three-dimensional array (table) | ||
3750 | * Failure: nil | ||
3751 | |||
3752 | Example: | ||
3753 | |||
3754 | {{code language="Lua"}} | ||
3755 | function get_history_config() | ||
3756 | local data = cloud.get_history_def() | ||
3757 | if data == nil then return end | ||
3758 | for i,m in ipairs(data) do | ||
3759 | print(i, m[1], m[2], m[3], m[4], m[5], m[6], m[7]) | ||
3760 | -- [1] The group ID of the tag | ||
3761 | -- [2] The group name of the tag | ||
3762 | -- [3] Collection ID of the tag | ||
3763 | -- [4] The channel name of the tag | ||
3764 | -- [5] The address of the tag | ||
3765 | -- [6] The data format of the tag, see the beginning of this section | ||
3766 | -- [7] Data length of the tag | ||
3767 | end | ||
3768 | end | ||
3769 | {{/code}} | ||
3770 | |||
3771 | == [[cloud_history_data>>||anchor="HFunctiontable"]](HMI) == | ||
3772 | |||
3773 | Prototype: cloud_//history_//data(gid, ts, data) | ||
3774 | |||
3775 | Function: Data Record collection callback function, when the collection conditions are met, the collection is triggered and this interface function is called | ||
3776 | |||
3777 | Parameter: | ||
3778 | |||
3779 | * gid(number): group ID | ||
3780 | * ts(string): time format string, for example: "2021-03-01 14:35:38" | ||
3781 | * data(table): two-dimensional array, collected data | ||
3782 | The first layer is the number of data | ||
3783 | The second layer is the specific information of the data | ||
3784 | |||
3785 | Return: None | ||
3786 | |||
3787 | Example: | ||
3788 | |||
3789 | {{code language="Lua"}} | ||
3790 | function cloud_history_data(gid, t, tb) | ||
3791 | if tb == nil then return end | ||
3792 | print("group id=", gid) | ||
3793 | print("collect time=", t) | ||
3794 | for i,v in pairs(tb) do | ||
3795 | -- i represents the i-th tag, v is the second layer table, which contains the specific information of the data | ||
3796 | print(i, v[1], v[2], v[3]) | ||
3797 | -- [1] Tag ID | ||
3798 | -- [2] Current value | ||
3799 | -- [3] Tag status, 1 success, 0 failure | ||
3800 | end | ||
3801 | end | ||
3802 | {{/code}} | ||
3803 | |||
3804 | == **[[cloud.get_alarm_def>>||anchor="HFunctiontable"]]**(HMI)[[||anchor="HFunctiontable"]] == | ||
3805 | |||
3806 | Prototype: cloud.get_//alarm_//def() | ||
3807 | |||
3808 | Function: Get the configuration of the Alarm record from HMI, only get channels with the Cloud checked | ||
3809 | |||
3810 | Parameters: None | ||
3811 | |||
3812 | Return: | ||
3813 | |||
3814 | * Success: two-dimensional array (table) | ||
3815 | * Failure: nil | ||
3816 | |||
3817 | Example: | ||
3818 | |||
3819 | {{code language="Lua"}} | ||
3820 | function get_alarm_config() | ||
3821 | local data = cloud.get_alarm_def() | ||
3822 | if data == nil then return end | ||
3823 | for i,m in ipairs(data) do | ||
3824 | print(i, m[1], m[2], m[3], m[4], m[5]) | ||
3825 | -- [1] Tag ID | ||
3826 | -- [2] Alarm type, 0 bit, 1 word | ||
3827 | -- [3] Alarm address, for example: "HDX100.0" or "HDW19" | ||
3828 | -- [4] Alarm condition, for example: "On" or "100≤X≤300" | ||
3829 | -- [5] Alarm text | ||
3830 | end | ||
3831 | end | ||
3832 | {{/code}} | ||
3833 | |||
3834 | == [[**cloud_//alarm_//data**>>||anchor="HFunctiontable"]](HMI)[[||anchor="HFunctiontable"]] == | ||
3835 | |||
3836 | Prototype: cloud_//alarm_//data(data) | ||
3837 | |||
3838 | Function: Alarm data callback function, call this interface function when an alarm occurs | ||
3839 | |||
3840 | Parameter: | ||
3841 | |||
3842 | * data(table): two-dimensional array, Alarm data | ||
3843 | The first level is the number of Alarm | ||
3844 | The second layer is the specific information of the Alarm | ||
3845 | |||
3846 | Return: None | ||
3847 | |||
3848 | Example: | ||
3849 | |||
3850 | {{code language="Lua"}} | ||
3851 | function cloud_alarm_data(tb) | ||
3852 | if tb == nil then return end | ||
3853 | for i,v in pairs(tb) do | ||
3854 | -- i means the i-th Alarm, v is the second layer table, which contains the specific information of the alarm | ||
3855 | print(i, v[1], v[2], v[3], v[4]) | ||
3856 | -- [1] Tag ID | ||
3857 | -- [2] Current value | ||
3858 | -- [3] Tag status, 1 success, 0 failure | ||
3859 | -- [4] String time, for example: "2021-03-01 14:35:38" | ||
3860 | end | ||
3861 | end | ||
3862 | {{/code}} | ||
3863 | |||
3864 | == [[**cloud.get_groupdesc**>>||anchor="HFunctiontable"]] == | ||
3865 | |||
3866 | Prototype: cloud.//get_groupdesc()// | ||
3867 | |||
3868 | Function: Get the configurations of all groups. | ||
3869 | |||
3870 | Parameter: None | ||
3871 | |||
3872 | Return: | ||
3873 | |||
3874 | * ((( | ||
3875 | Success: table two-dimensional array | ||
3876 | |||
3877 | { | ||
3878 | |||
3879 | [1] = {[1] = Trigger type, [2] = Group name, [3] = Cycle}, | ||
3880 | |||
3881 | ... | ||
3882 | |||
3883 | [n] = {[1] = xx, [2] = xx, [3] = xx}, | ||
3884 | |||
3885 | } | ||
3886 | Trigger type: | ||
3887 | |||
3888 | 0: Change triggered | ||
3889 | |||
3890 | 1: Word triggered | ||
3891 | |||
3892 | 2: No trigger (sampling by time) | ||
3893 | |||
3894 | 3: Bit triggers record (sampling by time) | ||
3895 | |||
3896 | 4: Bit triggers a record and resets | ||
3897 | |||
3898 | 5:Bit triggers a record and does not reset | ||
3899 | |||
3900 | Cycle: Some tags type have no cycle (Unit: ms),then the cycle is -1. | ||
3901 | ))) | ||
3902 | * Failure: table empty | ||
3903 | |||
3904 | Example: | ||
3905 | |||
3906 | {{code language="LUA"}} | ||
3907 | local tableGroupDef = cloud.get_groupdesc() | ||
3908 | local sSenddData = "" | ||
3909 | |||
3910 | for i, j in pairs(tableGroupDef) do | ||
3911 | local sData = "" | ||
3912 | for k, v in pairs(j) do | ||
3913 | sData = sData .. v | ||
3914 | sData = sData .. " " | ||
3915 | end | ||
3916 | sSenddData = sSenddData .. sData .. "\n" | ||
3917 | end | ||
3918 | print(sSenddData) | ||
3919 | {{/code}} | ||
3920 | |||
3921 | == [[**cloud.get_datadesc**>>||anchor="HFunctiontable"]] == | ||
3922 | |||
3923 | Prototype: cloud.//get_datadesc()// | ||
3924 | |||
3925 | Function: Get the configurations of all collection points | ||
3926 | |||
3927 | Parameter: None | ||
3928 | |||
3929 | Return: | ||
3930 | |||
3931 | * ((( | ||
3932 | Success: table three-dimensional array | ||
3933 | |||
3934 | { | ||
3935 | |||
3936 | [1]={~-~-~-~-~-~-First group | ||
3937 | |||
3938 | [1]={~-~-~-~-~-~-The tag array of first group | ||
3939 | |||
3940 | ?[1] = {[1] = Collection pointID, [2] = Collection point name, [3] = Read-write property, [4] = Type}, | ||
3941 | |||
3942 | ... | ||
3943 | |||
3944 | [n] = {[1] = xx, [2] =xx [3] = xx, [4] = xx} | ||
3945 | |||
3946 | }, | ||
3947 | |||
3948 | [2] = Group ID, | ||
3949 | |||
3950 | [3] = Group name | ||
3951 | |||
3952 | }, | ||
3953 | |||
3954 | ......n groups and so on | ||
3955 | |||
3956 | } | ||
3957 | |||
3958 | Read-write property: 0: read-only, 1: write-only, 2: read and write | ||
3959 | |||
3960 | Type: 1: switch, 2: numerical, 3: strings | ||
3961 | ))) | ||
3962 | * Failure: table empty | ||
3963 | |||
3964 | Example: | ||
3965 | |||
3966 | {{code language="LUA"}} | ||
3967 | local tablePointDef = cloud.get_datadesc() | ||
3968 | local sSendData = "" | ||
3969 | |||
3970 | for i, j in pairs(tablePointDef) do | ||
3971 | local sData = "" | ||
3972 | for x, y in pairs(j) do | ||
3973 | if type(y) == "table" then | ||
3974 | for k, v in pairs(y) do | ||
3975 | for a, b in pairs(v) do | ||
3976 | sData = sData .. b | ||
3977 | sData = sData .. "," | ||
3978 | end | ||
3979 | sData = sData .. " " | ||
3980 | end | ||
3981 | else | ||
3982 | sData = sData .. y | ||
3983 | sData = sData .. " " | ||
3984 | end | ||
3985 | end | ||
3986 | sSendData = sSendData .. sData .. "\n" | ||
3987 | end | ||
3988 | print(sSendData) | ||
3989 | {{/code}} | ||
3990 | |||
3991 | == [[**cloud.get_groupdata**>>||anchor="HFunctiontable"]] == | ||
3992 | |||
3993 | Prototype: cloud.//get_groupdata(name)// | ||
3994 | |||
3995 | Function: Get the collection point information of one group | ||
3996 | |||
3997 | Parameter: | ||
3998 | |||
3999 | * name: group name | ||
4000 | |||
4001 | Return: | ||
4002 | |||
4003 | * ((( | ||
4004 | Success: table two-dimensional array | ||
4005 | |||
4006 | { | ||
4007 | |||
4008 | [1]= {[1]=Collection point ID, [2]=status, [3]=Collect pointion name, [4]=Numeric value, [5]={Cunstom content}}, | ||
4009 | |||
4010 | ... | ||
4011 | |||
4012 | [n] = {[1] = xx, [2] = xx, [3] = xx, [4] = xx, [5] = {xx}} | ||
4013 | |||
4014 | } | ||
4015 | |||
4016 | Status: 0: Online, -1: Write-only, -2: Offline, | ||
4017 | ))) | ||
4018 | * Failure: table empty | ||
4019 | |||
4020 | Example: | ||
4021 | |||
4022 | {{code language="LUA"}} | ||
4023 | local sGroup = cloud.get_groupdata("Group 3") | ||
4024 | local sSendData = "" | ||
4025 | |||
4026 | for i, j in pairs(sGroup) do | ||
4027 | local sData = "" | ||
4028 | for k, v in pairs(j) do | ||
4029 | if type(v) == "table" then | ||
4030 | sData = sData .. "Custom:{" | ||
4031 | for x, y in pairs(v) do | ||
4032 | sData = sData .. x | ||
4033 | sData = sData .. ":" | ||
4034 | sData = sData .. y | ||
4035 | sData = sData .. "," | ||
4036 | end | ||
4037 | sData = sData .. "}" | ||
4038 | sData = sData .. " " | ||
4039 | else | ||
4040 | sData = sData .. v | ||
4041 | sData = sData .. " " | ||
4042 | end | ||
4043 | end | ||
4044 | sSendData = sSendData .. sData .. "\n" | ||
4045 | end | ||
4046 | print(sSendData) | ||
4047 | {{/code}} | ||
4048 | |||
4049 | == [[**cloud.set_data**>>||anchor="HFunctiontable"]] == | ||
4050 | |||
4051 | Prototype: cloud.s//et_data(name, data)// | ||
4052 | |||
4053 | Function: Set a collection point | ||
4054 | |||
4055 | Parameter: | ||
4056 | |||
4057 | * name: collection point name | ||
4058 | * data: value (only be string) | ||
4059 | |||
4060 | Return: | ||
4061 | |||
4062 | * ((( | ||
4063 | Success: The set value | ||
4064 | ))) | ||
4065 | * Failure: nil | ||
4066 | |||
4067 | Example: | ||
4068 | |||
4069 | {{code language="LUA"}} | ||
4070 | local sValue = cloud.set_data("Channel 1", "100") | ||
4071 | local sSendData = "" | ||
4072 | |||
4073 | sSendData = sSendData .. "Set channel 1 Value : " .. sValue | ||
4074 | print(sSendData) | ||
4075 | {{/code}} | ||
4076 | |||
4077 | == [[**cloud.get_data**>>||anchor="HFunctiontable"]] == | ||
4078 | |||
4079 | Prototype: cloud.g//et_data(name)// | ||
4080 | |||
4081 | Function: Get the configurations of a collection point | ||
4082 | |||
4083 | Parameter: | ||
4084 | |||
4085 | * name: collection point name | ||
4086 | |||
4087 | Return: | ||
4088 | |||
4089 | * ((( | ||
4090 | Success: The value of collection point, the custom content of collection point | ||
4091 | ))) | ||
4092 | * Failure: nil | ||
4093 | |||
4094 | Example: | ||
4095 | |||
4096 | {{code language="LUA"}} | ||
4097 | local sValue, tableCustome = cloud.get_data("Channel 1") | ||
4098 | local sSendData = "" | ||
4099 | |||
4100 | sSendData = sSendData .. sValue .. "\n" | ||
4101 | for i, j in pairs(tableCustome) do | ||
4102 | sSendData = sSendData .. i .. j .. "\n" | ||
4103 | end | ||
4104 | print(sSendData) | ||
4105 | {{/code}} | ||
4106 | |||
4107 | == [[**cloud.get_alldata**>>||anchor="HFunctiontable"]] == | ||
4108 | |||
4109 | Prototype: cloud.//get_alldata()// | ||
4110 | |||
4111 | Function: Get all the collection points that meet the conditions | ||
4112 | |||
4113 | Parameter: None | ||
4114 | |||
4115 | Return: | ||
4116 | |||
4117 | * ((( | ||
4118 | Success: table two-dimensional array | ||
4119 | |||
4120 | { | ||
4121 | |||
4122 | [1]= {[1]=Collection point ID, [2]=status, [3]=Collect pointion name, [4]=Numeric value, [5]={Cunstom content}}, | ||
4123 | |||
4124 | ... | ||
4125 | |||
4126 | [n] = {[1] = xx, [2] = xx, [3] = xx, [4] = xx, [5] = {xx}} | ||
4127 | |||
4128 | } | ||
4129 | |||
4130 | Status: 0: Online, -1: Write-only, -2: Offline, | ||
4131 | ))) | ||
4132 | * Failure: table empty | ||
4133 | |||
4134 | Example: | ||
4135 | |||
4136 | {{code language="LUA"}} | ||
4137 | local tablePointsData = cloud.get_alldata() | ||
4138 | local sSendData = "" | ||
4139 | |||
4140 | for i, j in pairs(tablePointsData) do | ||
4141 | local sData = "" | ||
4142 | for k, v in pairs(j) do | ||
4143 | if type(v) == "table" then | ||
4144 | sData = sData .. "Custom:{" | ||
4145 | for x, y in pairs(v) do | ||
4146 | sData = sData .. x | ||
4147 | sData = sData .. ":" | ||
4148 | sData = sData .. y | ||
4149 | sData = sData .. "," | ||
4150 | end | ||
4151 | sData = sData .. "}" | ||
4152 | sData = sData .. " " | ||
4153 | else | ||
4154 | sData = sData .. v | ||
4155 | sData = sData .. " " | ||
4156 | end | ||
4157 | end | ||
4158 | sSendData = sSendData .. sData .. "\n" | ||
4159 | end | ||
4160 | print(sSendData) | ||
4161 | {{/code}} | ||
4162 | |||
4163 | == [[**cloud.set_onecache**>>||anchor="HFunctiontable"]] == | ||
4164 | |||
4165 | Prototype: cloud.set//_onecache(data)// | ||
4166 | |||
4167 | Function: Save a piece of data to the cache (30 seconds to write cached data to flash) | ||
4168 | |||
4169 | Parameter: | ||
4170 | |||
4171 | * data: the data to the cache | ||
4172 | |||
4173 | Return: | ||
4174 | |||
4175 | * ((( | ||
4176 | Success: TRUE | ||
4177 | ))) | ||
4178 | * Failure: nil | ||
4179 | |||
4180 | Example: | ||
4181 | |||
4182 | {{code language="LUA"}} | ||
4183 | local sSaveData = we_bas_getstring("@W_HDW5000", 10) | ||
4184 | local sSendData = "" | ||
4185 | |||
4186 | cloud.set_onecache(sSaveData) | ||
4187 | sSendData = sSendData .. "Save Data : " .. sSaveData | ||
4188 | print(sSendData) | ||
4189 | {{/code}} | ||
4190 | |||
4191 | == [[**cloud.get_allcache**>>||anchor="HFunctiontable"]] == | ||
4192 | |||
4193 | Prototype: cloud.//get_allcache()// | ||
4194 | |||
4195 | Function: Getdata from the cache (The cache data and flash data will be cleared after fetching)) | ||
4196 | |||
4197 | Parameter: None | ||
4198 | |||
4199 | Return: | ||
4200 | |||
4201 | * ((( | ||
4202 | Success: table two-dimensional array | ||
4203 | |||
4204 | { | ||
4205 | |||
4206 | [1]="This is the oldest message", ~-~-~-~-~-~- The first one is the oldest message | ||
4207 | |||
4208 | ...... | ||
4209 | |||
4210 | [n]="This is the latest message", ~-~-~-~-~-~- The last one is the latest message | ||
4211 | |||
4212 | } | ||
4213 | ))) | ||
4214 | * Failure: nil | ||
4215 | |||
4216 | Example: | ||
4217 | |||
4218 | {{code language="LUA"}} | ||
4219 | local tableCacheDatas = cloud.get_allcache() | ||
4220 | local sSendData = "" | ||
4221 | |||
4222 | if nil == tableCacheDatas then | ||
4223 | return | ||
4224 | end | ||
4225 | for k, v in pairs(tableCacheDatas) do | ||
4226 | sSendData = sSendData .. v .. "\n" | ||
4227 | end | ||
4228 | print(sSendData) | ||
4229 | {{/code}} | ||
4230 | |||
4231 | = json = | ||
4232 | |||
4233 | (% class="wikigeneratedid" id="HDescription-9" %) | ||
4234 | **Description** | ||
4235 | |||
4236 | The library provides the conversion function from json string data to lua table data | ||
4237 | |||
4238 | The name of the library is: **json** | ||
4239 | |||
4240 | |=Function|=Description | ||
4241 | |[[json.encode>>||anchor="Hjson.encode"]]|Lua data type to json string | ||
4242 | |[[json.decode>>||anchor="Hjson.decode"]]|json string to lua data type | ||
4243 | |[[json.null>>||anchor="Hjson.null"]]|null | ||
4244 | |[[Example>>path:http://13.229.109.52:8080/wiki/pistudio/view/Helps/10.Lua/#HExample-2]]| | ||
4245 | |||
4246 | == [[json.encode>>||anchor="Hjson"]] == | ||
4247 | |||
4248 | Prototype: json.encode(object) | ||
4249 | |||
4250 | Function: convert lua data type to json string | ||
4251 | |||
4252 | Parameter: | ||
4253 | |||
4254 | * object(boolean, number, string, table): lua data | ||
4255 | |||
4256 | Return: | ||
4257 | |||
4258 | * Success: json format string (string) | ||
4259 | * Failure: nil | ||
4260 | |||
4261 | == [[json.decode>>||anchor="Hjson"]] == | ||
4262 | |||
4263 | Prototype: json.decode(json_string) | ||
4264 | |||
4265 | Function: json string to lua data type | ||
4266 | |||
4267 | Parameter: | ||
4268 | |||
4269 | * json_string(string): string on json data structure | ||
4270 | |||
4271 | Return: | ||
4272 | |||
4273 | * Success: lua data type | ||
4274 | * Failure: nil | ||
4275 | |||
4276 | == [[json.null>>||anchor="Hjson"]] == | ||
4277 | |||
4278 | Prototype: json.null() | ||
4279 | |||
4280 | Function: This function is used when encapsulating json data, which is equivalent to null in json. If you use json.null() directly, it will directly return the address of the function, please use it with encode function together | ||
4281 | |||
4282 | Parameters: None | ||
4283 | |||
4284 | Return: None | ||
4285 | |||
4286 | == Example == | ||
4287 | |||
4288 | {{code language="Lua"}} | ||
4289 | local json = require("json") | ||
4290 | local js_string = json.encode({1, 2,'fred', {username ='foo', password=json.null}}) | ||
4291 | print("json encode:", js_string) -- convert to json data | ||
4292 | local data = json.decode(js_string) -- converted to script data type | ||
4293 | if type(data) =='table' then | ||
4294 | print("json decode:") | ||
4295 | for k,v in pairs(data) do | ||
4296 | print(k,v) | ||
4297 | end | ||
4298 | end | ||
4299 | {{/code}} | ||
4300 | |||
4301 | Output: | ||
4302 | |||
4303 | {{code language="Lua"}} | ||
4304 | json encode: [1,2,"fred",{"username":"foo","password":null}] | ||
4305 | json decode: | ||
4306 | 1 1 | ||
4307 | 2 2 | ||
4308 | 3 fred | ||
4309 | 4 table: 05B30068 | ||
4310 | {{/code}} | ||
4311 | |||
4312 | = WLAN = | ||
4313 | |||
4314 | The library provides WLAN configuration function. | ||
4315 | |||
4316 | The name of the library is: **hmiwifi** | ||
4317 | |||
4318 | **List** | ||
4319 | |||
4320 | |=Function|=Description | ||
4321 | |[[hmiwifi.open>>||anchor="Hhmiwifi.open"]]|Enable | ||
4322 | |[[hmiwifi.close>>||anchor="Hhmiwifi.close"]]|Disable | ||
4323 | |[[hmiwifi.config>>||anchor="Hhmiwifi.config"]]|Configure | ||
4324 | |||
4325 | == [[hmiwifi.open>>||anchor="HWLAN"]] == | ||
4326 | |||
4327 | Prototype: hmiwif.open() | ||
4328 | |||
4329 | Function: Enable wifi function | ||
4330 | |||
4331 | Parameters: None | ||
4332 | |||
4333 | Return: | ||
4334 | |||
4335 | * Success: ture | ||
4336 | * Failure: nil, err | ||
4337 | |||
4338 | == [[hmiwifi.close>>||anchor="HWLAN"]] == | ||
4339 | |||
4340 | Prototype: hmiwif.close() | ||
4341 | |||
4342 | Function: Disnable wifi function | ||
4343 | |||
4344 | Parameters: None | ||
4345 | |||
4346 | Return: | ||
4347 | |||
4348 | * Success: ture | ||
4349 | * Failure: nil, err | ||
4350 | |||
4351 | == [[hmiwifi.config>>||anchor="HWLAN"]] == | ||
4352 | |||
4353 | Prototype: hmiwif.close(ssid, passwd) | ||
4354 | |||
4355 | Function: Configure wifi | ||
4356 | |||
4357 | Parameters: | ||
4358 | |||
4359 | * ssid(string) hotspot name format: length 1~~32 bytes | ||
4360 | * Passwd(streing) hotspot password format: length is 0 or greater 8 | ||
4361 | |||
4362 | Return: | ||
4363 | |||
4364 | * Success: ture | ||
4365 | * Failure: nil, err | ||
4366 | |||
4367 | == Example == | ||
4368 | |||
4369 | {{code language="LUA"}} | ||
4370 | local ret,err = hmiwifi.open() | ||
4371 | if ret then | ||
4372 | print("Wlan open ok") | ||
4373 | else | ||
4374 | print("Wlan open :", err) | ||
4375 | end | ||
4376 | |||
4377 | local ret,err = hmiwifi.config("aaa","12345678") | ||
4378 | if ret then | ||
4379 | print("Wlan config ok") | ||
4380 | else | ||
4381 | print("Wlan config:", err) | ||
4382 | End | ||
4383 | |||
4384 | //open hotspot | ||
4385 | local ret,err = hmiwifi.config("aaa","") | ||
4386 | if ret then | ||
4387 | print("Wlan config ok") | ||
4388 | else | ||
4389 | print("Wlan config:", err) | ||
4390 | end | ||
4391 | |||
4392 | local ret,err = hmiwifi.close() | ||
4393 | if ret then | ||
4394 | print("Wlan close ok") | ||
4395 | else | ||
4396 | print("Wlan close :", err) | ||
4397 | end | ||
4398 | {{/code}} | ||
4399 | |||
4400 | (% class="wikigeneratedid" %) | ||
4401 | Output: | ||
4402 | |||
4403 | {{code language="LUA"}} | ||
4404 | Wlan open ok | ||
4405 | Wlan config ok | ||
4406 | Wlan config ok | ||
4407 | Wlan close ok | ||
4408 | {{/code}} | ||
4409 | |||
4410 | = FtpServer = | ||
4411 | |||
4412 | The library provides the FtpServer password change function. | ||
4413 | |||
4414 | Library name: ftpserver | ||
4415 | |||
4416 | |=Function|=Fearture | ||
4417 | |[[ftpserver.changePsd>>||anchor="Hftpserver.changePsd(oldPsd,newPsd)"]]|Change the server password | ||
4418 | |||
4419 | == [[ftpserver.changePsd(oldPsd,newPsd)>>||anchor="HFtpServer"]] == | ||
4420 | |||
4421 | Prototype: ftpserver.changePsd(oldPsd,newPsd) | ||
4422 | |||
4423 | Function: change the server password | ||
4424 | |||
4425 | Parameters: | ||
4426 | |||
4427 | * oldPsd(string) old password format: length in 1 to 32 bytes | ||
4428 | * newPsd(string) new password format: Only letters, numbers, and underscores are allowed,and the length must be 6 to 8 bits. | ||
4429 | |||
4430 | Return: | ||
4431 | |||
4432 | * Success: true | ||
4433 | * Failure: nil,err | ||
4434 | |||
4435 | == Example == | ||
4436 | |||
4437 | {{code language="lua"}} | ||
4438 | local ret,err =ftpserver.changePsd("123456","888888") | ||
4439 | if ret then | ||
4440 | print("change_Password ok") | ||
4441 | else | ||
4442 | print("change_Password:", err) | ||
4443 | end | ||
4444 | {{/code}} | ||
4445 | |||
4446 | Output: | ||
4447 | |||
4448 | (% class="box" %) | ||
4449 | ((( | ||
4450 | change_Password ok | ||
4451 | ))) | ||
4452 | |||
4453 | = SIM = | ||
4454 | |||
4455 | (% class="wikigeneratedid" id="HInstruction" %) | ||
4456 | **Instruction:** | ||
4457 | |||
4458 | This library provides SIM card configuration information. | ||
4459 | |||
4460 | The name of the library is: **sim**. | ||
4461 | |||
4462 | {{info}} | ||
4463 | **✎Note: **Need to wait for about 15s after HMI powered on, this module can only run normally after the sim starts. | ||
4464 | {{/info}} | ||
4465 | |||
4466 | |=Function|=Description | ||
4467 | |[[sim.getCCID>>doc:||anchor="Hsim.getCCID"]]|Get CCID | ||
4468 | |[[sim.getIMEI>>doc:||anchor="Hsim.getIMEI"]]|Get IMEI | ||
4469 | |||
4470 | == [[sim.getCCID>>doc:||anchor="HSIM"]] == | ||
4471 | |||
4472 | Prototype: sim.getCCID() | ||
4473 | |||
4474 | Function: Get CCID string | ||
4475 | |||
4476 | Parameters: | ||
4477 | |||
4478 | * None | ||
4479 | |||
4480 | Returns: | ||
4481 | |||
4482 | * Success: CCID string | ||
4483 | * Failure: nil | ||
4484 | |||
4485 | == [[sim.getIMEI>>doc:||anchor="HSIM"]] == | ||
4486 | |||
4487 | Prototype: sim.getIMEI() | ||
4488 | |||
4489 | Function: Get IMEI string | ||
4490 | |||
4491 | Parameters: | ||
4492 | |||
4493 | * None | ||
4494 | |||
4495 | Returns: | ||
4496 | |||
4497 | * Success: IMEI string | ||
4498 | * Failure: nil | ||
4499 | |||
4500 | Example: | ||
4501 | |||
4502 | {{code language="LUA"}} | ||
4503 | local CCID = sim.getCCID(); | ||
4504 | local IMEI = sim.getIMEI(); | ||
4505 | if CCID then | ||
4506 | print("CCID = ",CCID) | ||
4507 | we_bas_setstring("@W_HDW0",CCID,256) | ||
4508 | end | ||
4509 | if IMEI then | ||
4510 | print("IMEI = ",IMEI) | ||
4511 | we_bas_setstring("@W_HDW500",IMEI,256) | ||
4512 | end | ||
4513 | {{/code}} | ||
4514 | |||
4515 | Output: | ||
4516 | |||
4517 | {{code language="LUA"}} | ||
4518 | CCID = 89860321482093086463 | ||
4519 | IMEI = 866340051749169 | ||
4520 | {{/code}} | ||
4521 | |||
4522 | = SMS = | ||
4523 | |||
4524 | **Instruction:** | ||
4525 | |||
4526 | This library provides relevant functions for sending SMS. | ||
4527 | |||
4528 | The name of the library is: **sms**. | ||
4529 | |||
4530 | {{info}} | ||
4531 | **✎Note: ** | ||
4532 | |||
4533 | * Need to wait for about 15s after HMI powered on, this module only run normally after the sim starts. | ||
4534 | * Call the sending SMS interface, it will not be sent immediately, the program will only send when the polling time is about 2s. | ||
4535 | * Each mobile phone number is corresponding to one message ID in the same method of sending SMS. User can use the message ID to confirm that the set SMS of the current mobile phone number is successfully sent before sending the next new message, otherwise the new SMS will overwrite the old SMS. | ||
4536 | * It supports up to set to send to 20 mobile phone numbers at the same time, **if one mobile phone number uses two different SMS sending methods, it is equivalent to occupying two mobile phone numbers.** | ||
4537 | * To use this module function, need to declare its namespace first like following: | ||
4538 | {{/info}} | ||
4539 | |||
4540 | {{code language="lua"}} | ||
4541 | local sms = require("sms") -- Load SMS module | ||
4542 | {{/code}} | ||
4543 | |||
4544 | |=(% style="width: 255px;" %)Function|=(% style="width: 759px;" %)Description | ||
4545 | |(% style="width:255px" %)[[sms.send_sms_ira>>||anchor="Hsms.send_sms_ira"]]|(% style="width:759px" %)Send English SMS using the IRA character set | ||
4546 | |(% style="width:255px" %)[[sms.send_sms_ucs2>>doc:||anchor="Hsms.send_sms_ucs2"]]|(% style="width:759px" %)Send other languages' SMS using the UCS2 character set, such as Chinese, Japanese, Korean, etc. | ||
4547 | |(% style="width:255px" %)[[sms.sms_get_state>>doc:||anchor="Hsms.sms_get_state"]]|(% style="width:759px" %)Obtain corresponding SMS status | ||
4548 | |||
4549 | == [[sms.send_sms_ira>>doc:||anchor="HSMS"]] == | ||
4550 | |||
4551 | Prototype: sms.send_sms_ira(number, message) | ||
4552 | |||
4553 | Function: Send English SMS using the IRA character set | ||
4554 | |||
4555 | Parameter: | ||
4556 | |||
4557 | * number(string): number(Only 32 characters at most, the exceed character will be discarded) | ||
4558 | * message(string): SMS content(Only 160 English characters at most, including special sign, the exceed character will be discarded, do not use characters from other languages in the content) | ||
4559 | |||
4560 | Return | ||
4561 | |||
4562 | * Success: Message id, is used for obtain corresponding SMS status | ||
4563 | * Failure: nil | ||
4564 | |||
4565 | Example: | ||
4566 | |||
4567 | {{code language="lua"}} | ||
4568 | local sms = require("sms") -- Load SMS module | ||
4569 | local num = we_bas_getstring("@W_HDW0",20) | ||
4570 | local msg = we_bas_getstring("@W_HDW1000",256) | ||
4571 | local id = sms.send_sms_ira(num,msg)--Sending SMS as IRA format | ||
4572 | --print("id:", id) | ||
4573 | we_bas_setword("@W_HDW202",id)--Save message id into HMI internal register | ||
4574 | {{/code}} | ||
4575 | |||
4576 | == [[sms.send_sms_ucs2>>doc:||anchor="HSMS"]] == | ||
4577 | |||
4578 | Prototype: sms.send_sms_ucs2(number, message) | ||
4579 | |||
4580 | Function: Send other languages' SMS using the UCS2 character set, such as Chinese, Japanese, Korean, etc. | ||
4581 | |||
4582 | Parameter: | ||
4583 | |||
4584 | * number(string): number(Only 32 characters at most, the exceed character will be discarded) | ||
4585 | * message(string): SMS content(Only 70 characters for other languages at most, the exceed character will be discarded) | ||
4586 | |||
4587 | Return | ||
4588 | |||
4589 | * Success: Message id, is used for obtain corresponding SMS status | ||
4590 | * Failure: nil | ||
4591 | |||
4592 | Example: | ||
4593 | |||
4594 | {{code language="lua"}} | ||
4595 | local sms = require("sms") -- Load SMS module | ||
4596 | local num = we_bas_getstring("@W_HDW0",20) | ||
4597 | local msg = we_bas_getstring("@W_HDW1000",256) | ||
4598 | local id = sms.send_sms_ucs2(num,msg)--Sending SMS as UCS2 format | ||
4599 | --print("id:", id) | ||
4600 | we_bas_setword("@W_HDW202",id)--Save message id into HMI internal register | ||
4601 | {{/code}} | ||
4602 | |||
4603 | == [[sms.sms_get_state>>doc:||anchor="HSMS"]] == | ||
4604 | |||
4605 | Prototype: sms.sms_get_state(id) | ||
4606 | |||
4607 | Function: Obtain corresponding SMS status | ||
4608 | |||
4609 | Parameter: | ||
4610 | |||
4611 | * id(number): Message id returned from function sms.send_sms_ira or sms.send_sms_ucs2 | ||
4612 | |||
4613 | Return: | ||
4614 | |||
4615 | * Success: SMS status(1: Not sent, 2: Successfully sent, 3: Failed to send) | ||
4616 | * Failure: nil | ||
4617 | |||
4618 | Example: | ||
4619 | |||
4620 | {{code language="lua"}} | ||
4621 | local sms = require("sms") -- Load SMS module | ||
4622 | local id = we_bas_getword("@W_HDW202")--Get message id | ||
4623 | local state = sms.sms_get_state(id)--Obtain SMS sending status according to message id | ||
4624 | --print("state:", state) | ||
4625 | we_bas_setword("@W_HDW200",state) | ||
4626 | {{/code}} | ||
4627 | |||
4628 | = LuaSocket = | ||
4629 | |||
4630 | It is the network programming module of Lua, and can be used for secondary development according to requirements. | ||
4631 | |||
4632 | It include the following contents: | ||
4633 | |||
4634 | 1. socket: Network basic module: basic parameter and baisic fuction | ||
4635 | 1. [[dns>>||anchor="Hdnsmodule"]]: DNS module | ||
4636 | 1. [[TCP:>>||anchor="HTCPmodule"]] TCP protocol object | ||
4637 | 1. [[UDP>>||anchor="HUDPmodule"]]: UDP protocol object | ||
4638 | 1. [[ftp:>>||anchor="HFTPmodule"]] ftp protocal API: Used for file transfer. This module only supports FTP client API, and does not support server API. | ||
4639 | 1. [[http>>||anchor="Hhttpmodule"]]: http protocol API | ||
4640 | 1. [[https:>>||anchor="Hhttpsmodule"]] https protocol API | ||
4641 | 1. [[smtp: >>||anchor="HSMTPmodule"]]smtp protocol API: Used for third-party email services | ||
4642 | 1. [[Other auxiliary modules>>||anchor="HAuxiliaryModule"]]: It is the extented function of module including ltn12 auxiliary module and mime auxiliary module. | ||
4643 | 1. [[Appendix>>||anchor="HAppendix"]]: ftp parameter list | ||
4644 | |||
4645 | (% class="wikigeneratedid" id="H270ENote:-1" %) | ||
4646 | **✎Note:** | ||
4647 | |||
4648 | {{code language="LUA"}} | ||
4649 | local socket = require("socket") -- Load the socket module and everything it needs | ||
4650 | local http = require("socket.http") -- Load the http module and everything it needs | ||
4651 | local smtp = require("socket.smtp") -- Load the smtp module and everything it needs | ||
4652 | local ftp = require("socket.ftp") -- Load the ftp module and everything it needs | ||
4653 | {{/code}} | ||
4654 | |||
4655 | = Modules = | ||
4656 | |||
4657 | == Socket module parameter == | ||
4658 | |||
4659 | |||
4660 | |=Parameter|=Description|=Value|=Type | ||
4661 | |_DATAGRAMSIZE|Call the default datagram size used to send and receive usage|8192|number | ||
4662 | |_SETSIZE|The maximum number of sockets that the select function can process|64|number | ||
4663 | |_SOCKETINVALID|invalid socket system operation value|4294967295|number | ||
4664 | |_VERSION|luasocket version|“LuaSocket 3.0-rc1”|string | ||
4665 | |BLOCKSIZE|socket receiving block size|2048|number | ||
4666 | |||
4667 | == Socket basic function == | ||
4668 | |||
4669 | |||
4670 | |=Function|=Features | ||
4671 | |[[socket.bind()>>||anchor="Hsocket.bind2829"]]|Bind IP address and port, and return a TCP server object to the server | ||
4672 | |[[socket.connect4()>>||anchor="Hsocket.connect42829"]]|Connect the object to the IP address and port and create a TCP object.(IPV4) | ||
4673 | |[[socket.connect6()>>||anchor="Hsocket.connect62829"]]|Connect the object to the IP address and port and create a TCP object.(IPV6) | ||
4674 | |[[socket.gettime()>>||anchor="Hsocket.gettime2829"]]|Return the system time. Unit: scond. | ||
4675 | |[[socket.newtry()>>||anchor="Hsocket.newtry2829"]]|Create and return a function that throws an exception | ||
4676 | |[[socket.protect()>>||anchor="Hsocket.protect2829"]]|Convert the throw exception function to a safety function | ||
4677 | |[[socket.select()>>||anchor="Hsocket.select2829"]]|Wait for multiple sockets to change state | ||
4678 | |[[socket.sink()>>||anchor="Hsocket.sink2829"]]|Create an LTN12 sink from a stream socket object. | ||
4679 | |[[socket.sleep()>>||anchor="Hsocket.sleep2829"]]|Dormancy | ||
4680 | |[[socket.source()>>||anchor="Hsocket.source2829"]]|Create an LTN12 source from a stream socket object | ||
4681 | |[[socket.tcp()>>||anchor="Hsocket.tcp2829"]]|Create and return a Tcp object | ||
4682 | |[[socket.udp()>>||anchor="Hsocket.udp2829"]]|Creates an empty (disconnected) UDP object | ||
4683 | |||
4684 | == dns module == | ||
4685 | |||
4686 | **✎Note:** | ||
4687 | |||
4688 | * DNS resolution in HMI is not supported due to internal changes in HMI. Therefore, only offline simulation is supported. | ||
4689 | * dns is a part of data socket basic module. To use the contents of this module, you need to quote the followings. | ||
4690 | |||
4691 | {{code language="LUA"}} | ||
4692 | local socket = require("socket") -- References the socket basic module | ||
4693 | local dns = socket.dns -- Get DNS module content | ||
4694 | {{/code}} | ||
4695 | |||
4696 | |=Function|=Features | ||
4697 | |[[dns.toip()>>||anchor="Hdns.toip2829"]]|Transfer from host name to IPv4 address | ||
4698 | |[[dns.getaddrinfo()>>||anchor="Hdns.getaddrinfo2829"]]|Transfer from host name to address | ||
4699 | |[[dns.tohostname()>>||anchor="Hdns.tohostname2829"]]|Transfer from IPv4 address to host name | ||
4700 | |[[dns.getnameinfo()>>||anchor="Hdns.getnameinfo2829"]]|Given an address (hostname or IP address) and port, and returns all the information provided by the DNS resolver | ||
4701 | |[[dns.gethostname()>>||anchor="Hdns.gethostname2829"]]|Returns the standard hostname of the machine as a string | ||
4702 | |||
4703 | == TCP module == | ||
4704 | |||
4705 | This module is a part of socket. It is the object created by socket.tcp() 、socket.tcp4() 、socket.tcp6(). | ||
4706 | |||
4707 | This module divides Tcp objects into three types of objects: masterTcp、clientTcp、serverTcp. | ||
4708 | |||
4709 | Different Tcp objects have corresponding methods. | ||
4710 | |||
4711 | * masterTcp: close | ||
4712 | * clientTcp: send, receive, getsockname, getpeername, settimeout and close | ||
4713 | * serverTcp: accept, getsockname, setoption, settimeout and close | ||
4714 | |||
4715 | |=Function|=Features | ||
4716 | |[[tcp:bind()>>||anchor="Htcp:bind2829"]]|Bind the object to the address and port of the localhost | ||
4717 | |[[tcp:connect()>>||anchor="Htcp:connect2829"]]|Attempt to connect the primary object to the remote host | ||
4718 | |[[tcp:listen()>>||anchor="Htcp:listen2829"]]|Socket lisntening | ||
4719 | |[[tcp:accept()>>||anchor="Htcp:accept2829"]]|Wait for a remote connection and return the server object representing the link | ||
4720 | |[[tcp:send()>>||anchor="Htcp:send2829"]]|Send data | ||
4721 | |[[tcp:receive()>>||anchor="Htcp:receive2829"]]|Reads data from client object according to the specified read mode. | ||
4722 | |[[tcp:close()>>||anchor="Htcp:close2829"]]|Close a TCP object | ||
4723 | |[[tcp:getoption()>>||anchor="Htcp:getoption2829"]]|Gets the options for the TCP object | ||
4724 | |[[tcp:getpeername()>>||anchor="Htcp:getpeername2829"]]|Return information about the remote end of the connected client object | ||
4725 | |[[tcp:getsockname>>||anchor="Htcp:getsockname"]]|Return information about the local address associated with the object | ||
4726 | |[[tcp:getstats()>>||anchor="Htcp:getstats2829"]]|Return accounting information about a socket that can be used to limit bandwidth | ||
4727 | |[[tcp:gettimeout()>>||anchor="Htcp:gettimeout2829"]]|Get timeout | ||
4728 | |[[tcp:setoption()>>||anchor="Htcp:setoption2829"]]|Set the options for the TCP object | ||
4729 | |[[tcp:setstats()>>||anchor="Htcp:setstats2829"]]|Reset accounting information on sockets (useful for bandwidth limit) | ||
4730 | |[[tcp:settimeout()>>||anchor="Htcp:settimeout2829"]]|Change the timeout value of tcp object | ||
4731 | |[[tcp:shutdown()>>||anchor="Htcp:shutdown2829"]]|Disable some full-duplex connections. | ||
4732 | |[[tcp:getfamily()>>||anchor="Htcp:getfamily2829"]]|Get the TCP protocol family | ||
4733 | |[[tcp:setpeername()>>||anchor="Htcp:setpeername2829"]]|Change the main object to connect to remote host | ||
4734 | |[[tcp:setsockname()>>||anchor="Htcp:setsockname"]]|Change the address and port number of the object binding | ||
4735 | |||
4736 | == UDP module == | ||
4737 | |||
4738 | This module is a part of socket. It is the object created by socket.udp(), socket.udp4(), socket.udp6(). | ||
4739 | |||
4740 | This module divides UDP objects into twotypes: connect and connected. The difference is whether remote ports and addresses are bound to the generated object. Please refer to the following table. | ||
4741 | |||
4742 | |||
4743 | |=Function|=Features | ||
4744 | |[[udp:send()>>||anchor="Hudp:send2829"]]|Sends datagrams to connected UDP objects | ||
4745 | |[[udp:sendto()>>||anchor="Hudp:sendto2829"]]|Sends a datagram to the specified IP address and port number. | ||
4746 | |[[udp:receive()>>||anchor="Hudp:receive2829"]]|Receives datagrams from UDP objects | ||
4747 | |[[udp:receivefrom()>>||anchor="Hudp:receivefrom2829"]]|Receives datagrams and addresses and ports from UDP objects | ||
4748 | |[[udp:close()>>||anchor="Hudp:close2829"]]|Close udp object | ||
4749 | |[[udp:gettimeout()>>||anchor="Hudp:gettimeout2829"]]|Get timeout value | ||
4750 | |[[udp:settimeout()>>||anchor="Hudp:settimeout2829"]]|Change timeout value of udp object | ||
4751 | |[[udp:getoption()>>||anchor="Hudp:getoption2829"]]|Gets the option value from the UDP object | ||
4752 | |[[udp:getpeername()>>||anchor="Hudp:getpeername2829"]]|Search information about the peer associated with the connected UDP object | ||
4753 | |[[udp:getsockname()>>||anchor="Hudp:getsockname2829"]]|Return information about the local address associated with the object | ||
4754 | |[[udp:setoption()>>||anchor="Hudp:setoption2829"]]|Set the options for the UDP object | ||
4755 | |[[udp:setpeername()>>||anchor="Hudp:setpeername2829"]]|Change the remote information of UDP object | ||
4756 | |[[udp:setsockname()>>||anchor="Hudp:setsockname2829"]]|Bind UDP object to local address | ||
4757 | |[[udp:getfamily()>>||anchor="Hudp:getfamily2829"]]|Return a tcp protocol family | ||
4758 | |||
4759 | == FTP module == | ||
4760 | |||
4761 | * This is submodule of socket. To use the contents of this module, you need to quote the followings. | ||
4762 | |||
4763 | {{{local ftp = require("socket.ftp") | ||
4764 | }}} | ||
4765 | |||
4766 | For details about FTP commands, see "appendix". | ||
4767 | |||
4768 | |||
4769 | |=Parameter|=Type|=Value|=Description | ||
4770 | |ftp.USER|string|"ftp"|Anonymous user by default | ||
4771 | |ftp.PASSWORD|string|"anonymous@anonymous.org"|This is the default anonymous password, and used when no password is provided in the URL | ||
4772 | |ftp.TIMEOUT|number|60|Set the timeout of all I/O operations. Unit: second | ||
4773 | |||
4774 | |=Function |=Features | ||
4775 | |[[ftp.command()>>||anchor="Hftp.command2829"]]|Send ftp command | ||
4776 | |[[ftp.genericform()>>||anchor="Hftp.genericform2829"]]|Request parameter conversion, the simple URL request mode convert to a general table parameter form | ||
4777 | |[[ftp.put()>>||anchor="Hftp.put2829"]]|Upload data | ||
4778 | |[[ftp.get()>>||anchor="Hftp.get2829"]]|Get data | ||
4779 | |||
4780 | == http module == | ||
4781 | |||
4782 | This is submodule of socket. To use the contents of this module, you need to quote the followings. | ||
4783 | |||
4784 | {{{local http = require("socket.http") | ||
4785 | }}} | ||
4786 | |||
4787 | |||
4788 | |=Parameter|=Type|=Value|=Description | ||
4789 | |http.TIMEOUT|number|60|Time out unit: second | ||
4790 | |||
4791 | |=Function |=Features | ||
4792 | |[[http.request()>>||anchor="Hhttp.request2829"]]|HTTP request function | ||
4793 | |[[http genericform()>>||anchor="Hhttp genericform2829"]]|Convert a simple URL to a generic table parameter | ||
4794 | |||
4795 | == https module == | ||
4796 | |||
4797 | This is submodule of socket. To use the contents of this module, you need to quote the followings. | ||
4798 | |||
4799 | {{{local https = require("socket.https") | ||
4800 | }}} | ||
4801 | |||
4802 | |||
4803 | |=Function |=Features | ||
4804 | |[[https.request()>>||anchor="Hhttps.request2829"]]|HTTP request function | ||
4805 | |||
4806 | == SMTP module == | ||
4807 | |||
4808 | This is submodule of socket. To use the contents of this module, you need to quote the followings. | ||
4809 | |||
4810 | {{{local smtp = require("socket.smtp") | ||
4811 | }}} | ||
4812 | |||
4813 | |||
4814 | |=Parameter|=Type|=Value|=Description | ||
4815 | |smtp.TIMEOUT|number|60|Connection timeout period | ||
4816 | |smtp.SERVER|string|“localhost”|Default server used for sending E-mail | ||
4817 | |smtp.DOMAIN|string|“localhost”|Domain used to access the server | ||
4818 | |smtp.POST|number|25|Default port for connecting | ||
4819 | |smtp.ZONE|string|"-0000"|default time zone | ||
4820 | |||
4821 | |=Function |=Features | ||
4822 | |[[smtp.message()>>||anchor="Hsmtp.message2829"]]|Generate mail format messages | ||
4823 | |[[smtp.send()>>||anchor="Hsmtp.send2829"]]|Send email | ||
4824 | |||
4825 | == Auxiliary Module == | ||
4826 | |||
4827 | === LTN12 module === | ||
4828 | |||
4829 | This is auxiliary Module of socket. To use the contents of this module, you need to quote the followings. | ||
4830 | |||
4831 | {{{local LTN12 = require("socket.ltn12") | ||
4832 | }}} | ||
4833 | |||
4834 | **filter** | ||
4835 | |||
4836 | |=Function |=Features | ||
4837 | |[[LTN12.filter.chain()>>||anchor="HLTN12.filter.chain2829"]]|Generates a filter that connects a series of filters | ||
4838 | |[[LTN12.filter.cycle()>>||anchor="HLTN12.filter.cycle2829"]]|Generate a high-level filter consisting of low-level filter loops | ||
4839 | |||
4840 | ((( | ||
4841 | **pump (Data pump)** | ||
4842 | ))) | ||
4843 | |||
4844 | |=Function |=Features | ||
4845 | |[[LTN12.pump.all()>>||anchor="HLTN12.pump.all2829"]]|Extract all data from one source to one sink. | ||
4846 | |[[LTN12.pump.step()>>||anchor="HLTN12.pump.step2829"]]|Extract a chunk of data from the source to the acceptor | ||
4847 | |||
4848 | **sink (acceptor)** | ||
4849 | |||
4850 | |=Function |=Features | ||
4851 | |[[LTN12.sink.chain()>>||anchor="HLTN12.sink.chain2829"]]|Create a new sink that transmit data through a filter before sending it to the given sink. | ||
4852 | |[[LTN12.sink.error()>>||anchor="HLTN12.sink.error2829"]]|Create and return a new sink that aborts the transmission with an error message | ||
4853 | |[[LTN12.sink.file()>>||anchor="HLTN12.sink.file2829"]]|Create a sink that send data to file | ||
4854 | |[[LTN12.sink.null()>>||anchor="HLTN12.sink.null2829"]]|Returns a sink that ignores all data received | ||
4855 | |[[LTN12.sink.simplify()>>||anchor="HLTN12.sink.simplify2829"]]|Given a selected sink, create and return a simple sink | ||
4856 | |[[LTN12.sink.table()>>||anchor="HLTN12.sink.table2829"]]|Create a sink that stores all the blocks in a table. | ||
4857 | |||
4858 | ((( | ||
4859 | **source** | ||
4860 | ))) | ||
4861 | |||
4862 | |=Function |=Features | ||
4863 | |[[LTN12.source.cat()>>||anchor="HLTN12.source.cat2829"]]|Create a new source that generates concatenations of data generated by multiple sources | ||
4864 | |[[LTN12.source.chain()>>||anchor="HLTN12.source.chain2829"]]|Create a new source that transmit data through a filter before returning it. | ||
4865 | |[[LTN12.source.empty()>>||anchor="HLTN12.source.empty2829"]]|Create and return a new source | ||
4866 | |[[LTN12.source.error()>>||anchor="HLTN12.source.error2829"]]|Create and return a source that aborts the transfer and display an error message. | ||
4867 | |[[LTN12.source.file()>>||anchor="HLTN12.source.file2829"]]|Create a source to connect file contents. | ||
4868 | |[[LTN12.source.simplify()>>||anchor="HLTN12.source.simplify2829"]]|Given a source, create and return a simple source. | ||
4869 | |[[LTN12.source.string()>>||anchor="HLTN12.source.string2829"]]|Creates and returns a source that generates string content | ||
4870 | |[[LTN12.source.table()>>||anchor="HLTN12.source.table2829"]]|Create and return a table data source | ||
4871 | |||
4872 | === MIME module === | ||
4873 | |||
4874 | This is auxiliary Module of socket. To use the contents of this module, you need to quote the followings. | ||
4875 | |||
4876 | {{{local mime = require("socket.mime")}}} | ||
4877 | |||
4878 | |||
4879 | |=Function |=Features | ||
4880 | |[[mime.decode()>>||anchor="Hmime.decode2829"]]|Return a filter used to decode data from the given transmission content encoding | ||
4881 | |[[mime.encode()>>||anchor="Hmime.encode2829"]]|Returns a filter that encodes data based on the given transmission content encoding. | ||
4882 | |[[mime.normalize()>>||anchor="Hmime.normalize2829"]]|Return a filter that converts the most common end-of-line tags to a specific given tag | ||
4883 | |[[mime.stuff()>>||anchor="Hmime.stuff2829"]]|Create and return a filter that performs SMTP message filling. | ||
4884 | |[[mime.wrap()>>||anchor="Hmime.wrap2829"]]|Return a filter that divides data into several lines. | ||
4885 | |[[mime.b64()>>||anchor="Hmime.b642829"]]|Create a low-level filter to perform end-of-line tag translation | ||
4886 | |[[mime.dot()>>||anchor="Hmime.dot2829"]]|Create a low-level filter that performs SMTP filling and enables the transmission of messages containing the sequence "crlf.crLF". | ||
4887 | |[[mime.eol()>>||anchor="Hmime.eol2829"]]|Create a low-level filter to perform end-of-line tag translation | ||
4888 | |[[mime.qp()>>||anchor="Hmime.qp2829"]]|Create a low-level filter to perform Quoted-Printable decoding | ||
4889 | |[[mime.qpwrp()>>||anchor="Hmime.qpwrp2829"]]|Create a low-level filter that divides Quoted-Printable text into several rows. | ||
4890 | |[[mime.unb64()>>||anchor="Hmime.unb642829"]]|Create a low-level filter to perform Base64 decoding | ||
4891 | |[[mime.unqp()>>||anchor="Hmime.unqp2829"]]|Create a low-level filter that delete quoted-printable transfer content encoding from data | ||
4892 | |[[mime.wrp()>>||anchor="Hmime.wrp2829"]]|Create a low-level filter that uses CRLF tags to split text into several lines. | ||
4893 | |||
4894 | = Socket basic function = | ||
4895 | |||
4896 | == [[socket.bind()>>||anchor="HSocketbasicfunction"]] == | ||
4897 | |||
4898 | **Prototype:** socket.bind(address, port [, backlog]) | ||
4899 | |||
4900 | **Function: **A shortcut function that encapsulates tcp's bind, accept, and listen calling processes. Creates and returns a TCP server object bound to a local address and port. | ||
4901 | |||
4902 | **Parameters: ** | ||
4903 | |||
4904 | * address — local address | ||
4905 | * port — Port | ||
4906 | * backlog — Specifies the number of client connections that can be queued for service. If the queue is full and another client tries to connect, the connection will be rejected. (This parameter is optional. The default value is 32) | ||
4907 | |||
4908 | **Return: ** | ||
4909 | |||
4910 | * Success: return tcp server object | ||
4911 | * Failure: return nil and error | ||
4912 | |||
4913 | **Example** | ||
4914 | |||
4915 | {{code language="LUA"}} | ||
4916 | local socket = require("socket") | ||
4917 | |||
4918 | local ip = "192.168.88.246" | ||
4919 | local port = 8899 | ||
4920 | |||
4921 | local fd_server = socket.bind(ip,port) | ||
4922 | print(fd_server,type(fd_server)) | ||
4923 | --Output:tcp{server}: 06F5ED60 userdata | ||
4924 | {{/code}} | ||
4925 | |||
4926 | == [[socket.connect4()>>||anchor="HSocketbasicfunction"]] == | ||
4927 | |||
4928 | **Prototype: **socket.connect4(address, port [, locaddr] [, locport] ) | ||
4929 | |||
4930 | **Function:** This function is a shortcut that creates and returns a TCP client object connected to the remote (ipv4) address of a given port. | ||
4931 | |||
4932 | **Parameters: ** | ||
4933 | |||
4934 | * address(string) : Remote server address | ||
4935 | * port(number) : Remote server port | ||
4936 | * locaddr(string) : Local address | ||
4937 | * locport(number) : Local port | ||
4938 | |||
4939 | **Return: ** | ||
4940 | |||
4941 | * Success: return tcp client object | ||
4942 | * Failure: nil, error | ||
4943 | |||
4944 | **Example** | ||
4945 | |||
4946 | {{code language="LUA"}} | ||
4947 | local socket = require("socket") | ||
4948 | local ip = "192.168.88.246" | ||
4949 | local port = "9966" | ||
4950 | local locIp = "192.168.88.246" | ||
4951 | local locPort = "8888" | ||
4952 | |||
4953 | local fd_client = socket.connect4(ip,port,locIp,locPort) | ||
4954 | print(fd_client,type(fd_client)) | ||
4955 | --Output: tcp{client}: 07F627D8 userdata | ||
4956 | {{/code}} | ||
4957 | |||
4958 | == [[socket.connect6()>>||anchor="HSocketbasicfunction"]] == | ||
4959 | |||
4960 | **Prototype: **socket.connect6(address, port [, locaddr] [, locport]) | ||
4961 | |||
4962 | **Function:** This function is a shortcut that creates and returns a TCP client object connected to the remote (ipv6) address of a given port. | ||
4963 | |||
4964 | **Parameters: ** | ||
4965 | |||
4966 | * address(string) : Remote server address | ||
4967 | * port(number) : Remote server port | ||
4968 | * locaddr(string) : Local address | ||
4969 | * locport(number) : Local port | ||
4970 | |||
4971 | **Return: ** | ||
4972 | |||
4973 | * Success: return tcp client object | ||
4974 | * Failure: nil, error | ||
4975 | |||
4976 | **Example** | ||
4977 | |||
4978 | {{code language="LUA"}} | ||
4979 | local socket = require("socket") | ||
4980 | local ip = "192.168.88.246" | ||
4981 | local port = "9966" | ||
4982 | local locIp = "192.168.88.246" | ||
4983 | local locPort = "8888" | ||
4984 | |||
4985 | local fd_client = socket.connect6(ip,port,locIp,locPort) -- This parameter is used when the server address is ipv6 | ||
4986 | print(fd_client,type(fd_client)) | ||
4987 | |||
4988 | --Output (test results in IPv4 addresses): nil nil | ||
4989 | {{/code}} | ||
4990 | |||
4991 | == [[socket.gettime()>>||anchor="HSocketbasicfunction"]] == | ||
4992 | |||
4993 | **Prototype: **socket.gettime() | ||
4994 | |||
4995 | **Function:** Get the system time in seconds, from January 1970. | ||
4996 | |||
4997 | **Parameters: None** | ||
4998 | |||
4999 | **Return: ** | ||
5000 | |||
5001 | * systemtime(number): system time: second | ||
5002 | |||
5003 | **Example** | ||
5004 | |||
5005 | {{code language="LUA"}} | ||
5006 | local socket = require("socket") | ||
5007 | |||
5008 | local t1 = socket.gettime() -- Get the current time | ||
5009 | socket.sleep(5) -- Sleep for 5 seconds | ||
5010 | local t2 = socket.gettime() -- Get2 the current time 2 | ||
5011 | |||
5012 | print("t1 =",t1) -- Print time 1 | ||
5013 | print("t2 =",t2) -- Print time 2 | ||
5014 | print("t2-t1=",t2-t1) -- Print time difference | ||
5015 | --Output: | ||
5016 | -- t1 = 1636878024.5057 | ||
5017 | -- t2 = 1636878029.5201 | ||
5018 | -- t2-t1= 5.0144500732422 | ||
5019 | {{/code}} | ||
5020 | |||
5021 | == [[socket.newtry()>>||anchor="HSocketbasicfunction"]] == | ||
5022 | |||
5023 | **Prototype: **socket.newtry(finalizer) | ||
5024 | |||
5025 | **Function:** Creates and returns a try function that allows the exception to be cleaned up before it is thrown. | ||
5026 | |||
5027 | **Parameters: ** | ||
5028 | |||
5029 | * finalizer(function): Finalizer is a function that will be called before newtry throws an exception. | ||
5030 | |||
5031 | **Return: ** | ||
5032 | |||
5033 | * Returns a newtry function that throws an exception block | ||
5034 | |||
5035 | **Note:** | ||
5036 | |||
5037 | * This function is usually used with the socket.protect() function; Try throws an exception and Protect catches it. | ||
5038 | |||
5039 | **Example** | ||
5040 | |||
5041 | {{code language="LUA"}} | ||
5042 | local socket = require("socket") | ||
5043 | |||
5044 | foo = socket.protect(function() | ||
5045 | print("--------------") | ||
5046 | local c = socket.try(socket.connect("192.168.88.241", 9090)) | ||
5047 | print(c,type(c)) | ||
5048 | -- create a try function that closes 'c' on error | ||
5049 | local try = socket.newtry(function() c:close() end)-- Call the TCP object closure function if an exception occurs | ||
5050 | -- do everything reassured c will be closed | ||
5051 | try(c:send("hello there?\r\n")) -- formal call c of try:send() | ||
5052 | local answer = try(c:receive()) -- formal call c of try:receive() | ||
5053 | |||
5054 | print(answer) | ||
5055 | try(c:send("good bye\r\n")) | ||
5056 | c:close()formal call c of try | ||
5057 | end) | ||
5058 | foo() | ||
5059 | -- Analysis:Functions or more important blocks of code that may have exceptions are called in the form of try, and protection functions are called before exceptions occur. | ||
5060 | Prevent major problems caused by exceptions. | ||
5061 | {{/code}} | ||
5062 | |||
5063 | == [[socket.protect()>>||anchor="HSocketbasicfunction"]] == | ||
5064 | |||
5065 | **Prototype: **socket.protect(func) | ||
5066 | |||
5067 | **Function:** Convert the throw exception function to safety function. This function only catches exceptions thrown by the try and newtry functions, and would not catch normal Lua errors. | ||
5068 | |||
5069 | **Parameters: ** | ||
5070 | |||
5071 | * func(function): Is a function that calls try (or Assert or error) to throw an exception. | ||
5072 | |||
5073 | **Return: ** | ||
5074 | |||
5075 | * Returns a equivalent func function that no longer throws an exception when a try call fails but returns nil and an error. | ||
5076 | |||
5077 | **Example** | ||
5078 | |||
5079 | {{code language="LUA"}} | ||
5080 | local socket = require("socket") | ||
5081 | |||
5082 | foo = socket.protect(function() | ||
5083 | print("--------------") | ||
5084 | local c = socket.try(socket.connect("192.168.88.241", 9090)) | ||
5085 | print(c,type(c)) | ||
5086 | -- create a try function that closes 'c' on error | ||
5087 | local try = socket.newtry(function() c:close() end)-- Call the TCP object closure function if an exception occurs | ||
5088 | -- do everything reassured c will be closed | ||
5089 | try(c:send("hello there?\r\n")) -- formal call c of try:send() | ||
5090 | local answer = try(c:receive()) -- formal call c of try:receive() | ||
5091 | |||
5092 | print(answer) | ||
5093 | try(c:send("good bye\r\n")) | ||
5094 | c:close() | ||
5095 | end) | ||
5096 | foo() | ||
5097 | {{/code}} | ||
5098 | |||
5099 | == [[socket.select()>>||anchor="HSocketbasicfunction"]] == | ||
5100 | |||
5101 | **Prototype: **socket.select(recvt, sendt [, timeout]) | ||
5102 | |||
5103 | **Function:** Wait for multiple sockets to change status. | ||
5104 | |||
5105 | **Parameters: ** | ||
5106 | |||
5107 | * recvt(arrary):An array with sockets that tests readable characters. | ||
5108 | * send(arrary):sendt array. Monitor the sockets of sendt array to see if they can be written immediately. | ||
5109 | * timeout(bunber):Timeout period,nil or a negative number is an indefinite timeout. | ||
5110 | |||
5111 | **Return: ** | ||
5112 | |||
5113 | * Returns a equivalent func function that no longer throws an exception when a try call fails but returns nil and an error. | ||
5114 | |||
5115 | **Note:** | ||
5116 | |||
5117 | * Select could monitor a limited number of sockets, as defined by the constant socket._setsize. By default, this number could be as high as 1024 or as low as 64, depending on the system. You could usually change it when compiling. Calling select with a large number of sockets, an error would occur. | ||
5118 | * Call select using the server socket in the receive parameter before calling accept does not guarantee that accept will return immediately. Using the settimeout method or accept may cause block forever. | ||
5119 | * If a closed socket is passed to select, it will be ignored. | ||
5120 | * Use select with non-socket objects: Any objects that implement getfd and dirty could be used with select. This allows objects from other libraries to be used in the loops of socket.select-driven. | ||
5121 | |||
5122 | **Example** | ||
5123 | |||
5124 | {{code language="LUA"}} | ||
5125 | -- Implement timeout waits | ||
5126 | function timeoutWait() | ||
5127 | local tabFd = {} | ||
5128 | table.insert(tabFd,fdServer) -- Add the server socket to the Select socket monitoring table | ||
5129 | for i,v in pairs(tabClient) do -- Loop traversal client | ||
5130 | table.insert(tabFd,tabClient[i].fd) -- Add the client socket to the Select socket monitoring table | ||
5131 | end | ||
5132 | socket.select(tabFd) -- select monitor socket.If the socket does not receive a connection, it remains blocked | ||
5133 | end | ||
5134 | {{/code}} | ||
5135 | |||
5136 | == [[socket.sink()>>||anchor="HSocketbasicfunction"]] == | ||
5137 | |||
5138 | **Prototype: **socket.sink(mode, socket) | ||
5139 | |||
5140 | **Function:** Create an LTN12 sink from a stream socket object. | ||
5141 | |||
5142 | **Parameters: ** | ||
5143 | |||
5144 | * mode —— Receiver mode | ||
5145 | * socket —— A stream socket object used to receive data | ||
5146 | |||
5147 | **Return: ** | ||
5148 | |||
5149 | * Return an LTN12 receiver | ||
5150 | |||
5151 | **Note:** | ||
5152 | |||
5153 | LTN12 receiver mode: | ||
5154 | |||
5155 | * "http-chunked": After using chunked transmission encoding, send data through socket, and close socket when completed. | ||
5156 | * "close-when-done": Send all received data through socket, and then close the socket. | ||
5157 | * "keep-open": Send all received data through socket, Leave it open when it is done. | ||
5158 | |||
5159 | == [[socket.sleep()>>||anchor="HSocketbasicfunction"]] == | ||
5160 | |||
5161 | **Prototype: **sleep(s) | ||
5162 | |||
5163 | **Function:** Sleep s seconds | ||
5164 | |||
5165 | **Parameters: ** | ||
5166 | |||
5167 | * n(number):The number of seconds to sleep | ||
5168 | |||
5169 | **Return: None** | ||
5170 | |||
5171 | **Example** | ||
5172 | |||
5173 | {{code language="LUA"}} | ||
5174 | local socket = require("socket") print("start . . .") | ||
5175 | local time1 = socket.gettime() socket.sleep(5) | ||
5176 | -- Print the difference between the two acquisition times after sleeping for 5 seconds | ||
5177 | print("socket time=",socket.gettime() - time1) | ||
5178 | --Output: | ||
5179 | -- start . . . -- socket time= 5.0082302093506 | ||
5180 | {{/code}} | ||
5181 | |||
5182 | == [[socket.source()>>||anchor="HSocketbasicfunction"]] == | ||
5183 | |||
5184 | **Prototype: **socket.source(mode, socket [, length]) | ||
5185 | |||
5186 | **Function:** Create LTN12 source from stream socket object. | ||
5187 | |||
5188 | **Parameters: ** | ||
5189 | |||
5190 | * mode(string):The mode of source | ||
5191 | * socket(userdata):A stream socket object used to receive data. | ||
5192 | * ength(number):Length | ||
5193 | |||
5194 | **Return: ** | ||
5195 | |||
5196 | * Return a created LTN12 source | ||
5197 | |||
5198 | **Note:** | ||
5199 | |||
5200 | LTN12 source mode. The following options are available. | ||
5201 | |||
5202 | * "http-chunked": Receives data from the socket, and remove the chunked transmission encoding before returning data. | ||
5203 | * "by-length": Receives a fixed number of bytes from the socket. This mode requires additional parameter lengths. | ||
5204 | * "until-closed": Receives data from the socket until the other end closes the connection. | ||
5205 | |||
5206 | == [[socket.tcp()>>||anchor="HSocketbasicfunction"]] == | ||
5207 | |||
5208 | **Prototype: **socket.tcp()、socket.tcp4()、socket.tcp6() | ||
5209 | |||
5210 | **Function:** Create and return a Tcp main object. | ||
5211 | |||
5212 | **Parameters: **None | ||
5213 | |||
5214 | **Return: ** | ||
5215 | |||
5216 | * Success: Return a tcp object | ||
5217 | * Failure: nil, error | ||
5218 | |||
5219 | **Note:** | ||
5220 | |||
5221 | * The difference between socket.tcp4() and socket.tcp6() is whether the selected IP is ipv4 or ipv6. | ||
5222 | * Socket. TCP divides objects into three types: main object, client object and server object. Socket.tcp creates the main object. The main object could be converted to a server object through the listen method (after calling bind), or it could be converted to a clientobject through the connect method. The only method supported by the main object is the close method. | ||
5223 | * To choose IPv4 or IPv6 during the calling of bind or connection, it isdepending on the address family obtained from the parser. | ||
5224 | * If the internal socket object is invalid before choosing IPv4 or IPv6, setoption will fail. | ||
5225 | |||
5226 | **Example:** | ||
5227 | |||
5228 | {{code language="LUA"}} | ||
5229 | local socket = require("socket") | ||
5230 | local tcp = socket.tcp() | ||
5231 | print("tcp=",tcp,type(tcp)) -- Obtain TCP objects. For details about how to use TCP objects, see TCP Module | ||
5232 | -------------------------------------------- | ||
5233 | --Output: | ||
5234 | -- tcp= tcp{master}: 0B3074F8 userdata | ||
5235 | {{/code}} | ||
5236 | |||
5237 | == [[socket.udp()>>||anchor="HSocketbasicfunction"]] == | ||
5238 | |||
5239 | **Prototype: **socket.udp()、socket.udp4()、socket.udp6() | ||
5240 | |||
5241 | **Function:** Create an empty (disconnected) udp object. | ||
5242 | |||
5243 | **Parameters: **None | ||
5244 | |||
5245 | **Return: ** | ||
5246 | |||
5247 | * Success: Return a new disconnected udp object. | ||
5248 | * Failure: nil, error | ||
5249 | |||
5250 | **Note:** | ||
5251 | |||
5252 | * The difference between socket.tcp4() and socket.tcp6() is whether the selected IP is ipv4 or ipv6. | ||
5253 | * To choose IPv4 or IPv6 during the calling of udp:sendto, udp:setpeeername or udp:sockname, it is depending on the address family obtained from the parser. | ||
5254 | * If the internal socket object is invalid before choosing IPv4 or IPv6, udp: setoption will fail. | ||
5255 | |||
5256 | **Example:** | ||
5257 | |||
5258 | {{code language="LUA"}} | ||
5259 | local socket = require("socket") | ||
5260 | local udp = socket.udp() | ||
5261 | print("udp=",udp,type(udp)) -- Obtain udp objects. For details about how to use udp objects, see udp Module | ||
5262 | -------------------------------------------- | ||
5263 | --Ouptput | ||
5264 | -- udp= udp{unconnected}: 0B808B98 userdata | ||
5265 | {{/code}} | ||
5266 | |||
5267 | = dns module = | ||
5268 | |||
5269 | == [[dns.toip()>>||anchor="Hdnsmodule"]] == | ||
5270 | |||
5271 | **Prototype: **dns.toip(address) | ||
5272 | |||
5273 | **Function:** Convert from hostname to IPv4 address | ||
5274 | |||
5275 | **Parameters: ** | ||
5276 | |||
5277 | * address(string): Address. It could be an IP name or a host name. | ||
5278 | |||
5279 | **Return: ** | ||
5280 | |||
5281 | Success: . | ||
5282 | |||
5283 | * address(string): IP address | ||
5284 | * info(table): address information table | ||
5285 | |||
5286 | Failure: return nil and error | ||
5287 | |||
5288 | **Example:** | ||
5289 | |||
5290 | {{code language="LUA"}} | ||
5291 | local socket = require("socket") | ||
5292 | local dns = socket.dns | ||
5293 | local hostname = dns.gethostname() | ||
5294 | local ip,msg = dns.toip(hostname) | ||
5295 | if ip then | ||
5296 | print("ip=",ip) | ||
5297 | else | ||
5298 | print(msg) | ||
5299 | end | ||
5300 | |||
5301 | |||
5302 | --Output | ||
5303 | -- ip= 192.168.33.149 | ||
5304 | {{/code}} | ||
5305 | |||
5306 | == [[dns.getaddrinfo()>>||anchor="Hdnsmodule"]] == | ||
5307 | |||
5308 | **Prototype: **dns.getaddrinfo(address) | ||
5309 | |||
5310 | **Function:** Convert from hostname to address | ||
5311 | |||
5312 | **Parameters: ** | ||
5313 | |||
5314 | * address(string): Address. It could be an IP name or a host name. | ||
5315 | |||
5316 | **Return: ** | ||
5317 | |||
5318 | * Success: info(table): address information table | ||
5319 | * Failure: return nil and error | ||
5320 | |||
5321 | **Example:** | ||
5322 | |||
5323 | {{code language="LUA"}} | ||
5324 | local socket = require("socket") | ||
5325 | local dns = socket.dns | ||
5326 | local host = dns.gethostname() | ||
5327 | local addrinfo = dns.getaddrinfo(host) | ||
5328 | for i,v in pairs(addrinfo) do | ||
5329 | print(i,v,"--------------") | ||
5330 | for i2,v2 in pairs(v) do | ||
5331 | print(i2,v2) | ||
5332 | end | ||
5333 | end | ||
5334 | |||
5335 | --Output: | ||
5336 | -- 1 table: 0AB71660 -------------- | ||
5337 | -- addr fe80::9930:10db:5b7f:1249%4 | ||
5338 | -- family inet6 | ||
5339 | -- 2 table: 0AB715C0 -------------- | ||
5340 | -- addr 192.168.33.149 | ||
5341 | -- family inet | ||
5342 | {{/code}} | ||
5343 | |||
5344 | == [[dns.tohostname()>>||anchor="Hdnsmodule"]] == | ||
5345 | |||
5346 | **Prototype: **dns.tohostname(address) | ||
5347 | |||
5348 | **Function:** Convert from IP4v address to hostname | ||
5349 | |||
5350 | **Parameters: ** | ||
5351 | |||
5352 | * address(string): Address. It could be an IP name or a host name. | ||
5353 | |||
5354 | **Return: ** | ||
5355 | |||
5356 | * Success: Return a host name string and an address information table | ||
5357 | * Failure: return nil and error | ||
5358 | |||
5359 | **Example:** | ||
5360 | |||
5361 | {{code language="LUA"}} | ||
5362 | local host = dns.gethostname() | ||
5363 | local ip = dns.toip(host) | ||
5364 | local hostname,msg = dns.tohostname(ip) | ||
5365 | if hostname then | ||
5366 | print("hostname=",hostname) | ||
5367 | else | ||
5368 | print(msg) | ||
5369 | end | ||
5370 | |||
5371 | --Output: | ||
5372 | -- hostname= DESKTOP-LB0DAGV | ||
5373 | {{/code}} | ||
5374 | |||
5375 | == [[dns.getnameinfo()>>||anchor="Hdnsmodule"]] == | ||
5376 | |||
5377 | **Prototype: **dns.getnameinfo(address, port) | ||
5378 | |||
5379 | **Function:** Given address (host name or IP address) and port, and return all the information provided by dns parser. | ||
5380 | |||
5381 | **Parameters: ** | ||
5382 | |||
5383 | * address(string): Address. It could be an IP name or a host name. | ||
5384 | * port(number): Service port number | ||
5385 | |||
5386 | **Return: ** | ||
5387 | |||
5388 | * Success: Return an address information table and port informantion string | ||
5389 | * Failure: return nil and error | ||
5390 | |||
5391 | **Example:** | ||
5392 | |||
5393 | {{code language="LUA"}} | ||
5394 | local socket = require("socket") | ||
5395 | local json = require("json") | ||
5396 | |||
5397 | local host = "DESKTOP-P46LTTK" | ||
5398 | local port = 21 | ||
5399 | local hostinfo,servinfo=socket.dns.getnameinfo(host, port) | ||
5400 | print("hostinfo =",json.encode(hostinfo)) | ||
5401 | print("portinfo =",servinfo) | ||
5402 | |||
5403 | --Output: | ||
5404 | -- hostinfo = ["DESKTOP-P46LTTK","DESKTOP-P46LTTK"] | ||
5405 | -- portinfo = ftp | ||
5406 | {{/code}} | ||
5407 | |||
5408 | == [[dns.gethostname()>>||anchor="Hdnsmodule"]] == | ||
5409 | |||
5410 | **Prototype: **dns.gethostname() | ||
5411 | |||
5412 | **Function:** Return the standard hostname of the machine as a string | ||
5413 | |||
5414 | **Parameters: **None | ||
5415 | |||
5416 | **Return: ** | ||
5417 | |||
5418 | * Success: Return the standard hostname of the machine as a string | ||
5419 | * Failure: return nil and error | ||
5420 | |||
5421 | **Example:** | ||
5422 | |||
5423 | {{code language="LUA"}} | ||
5424 | local socket = require("socket") | ||
5425 | print("host name =",socket.dns.gethostname()) | ||
5426 | --Output: | ||
5427 | -- host name = DESKTOP-P46LTTK | ||
5428 | {{/code}} | ||
5429 | |||
5430 | = TCP module = | ||
5431 | |||
5432 | == [[tcp:bind()>>||anchor="HTCPmodule"]] == | ||
5433 | |||
5434 | **Prototype: **master:bind(address, port) | ||
5435 | |||
5436 | **Function: **Bind the object to the address and port of local host. | ||
5437 | |||
5438 | **Parameters: ** | ||
5439 | |||
5440 | * address(string):IP address or host name | ||
5441 | * port(number):port number | ||
5442 | |||
5443 | **Return: ** | ||
5444 | |||
5445 | * Success: Return 1. | ||
5446 | * Failure: return nil and error. | ||
5447 | |||
5448 | **Note:** | ||
5449 | |||
5450 | * If the address is "*", the system binds all local interfaces with the INADDR_ANYc constant or IN6ADDR_ANY_INIT. | ||
5451 | * If the port is 0, the system automatically selects a temporary port. | ||
5452 | * The socket.bind function is a shortcut to create a server socket. | ||
5453 | |||
5454 | == [[tcp:connect()>>||anchor="HTCPmodule"]] == | ||
5455 | |||
5456 | **Prototype: **master:connect(address, port) | ||
5457 | |||
5458 | **Function: **Attempt to connect the primary object to the remote host. | ||
5459 | |||
5460 | **Parameters: ** | ||
5461 | |||
5462 | * address(string):IP address or host name | ||
5463 | * port(number):port number | ||
5464 | |||
5465 | **Return: ** | ||
5466 | |||
5467 | * Success: Return 1. | ||
5468 | * Failure: return nil and error. | ||
5469 | |||
5470 | **Note:** | ||
5471 | |||
5472 | * Convert main object to client object after using the connect function. | ||
5473 | * The socket.connect function is available and is a shortcut to create a client socket. | ||
5474 | * Starting with LuaSocket 2.0, the settimeout method woudl affect the action of connect, causing it to return an error in the case of a timeout. If this happens, you could still use the socket in the Sendt table to call socket.select. AFfter the connection, the socket will be writable. | ||
5475 | * Starting with LuaSocket 3.0, hostname resolution depends on whether a socket is created by socket.tcp, socket.tcp4, or socket.tcp6. Try the addresses from the appropriate series (or both) according to the order returned by the parser until the first success or the last failure. If timeout is set to zero, only the first address is tried. | ||
5476 | |||
5477 | == [[tcp:listen()>>||anchor="HTCPmodule"]] == | ||
5478 | |||
5479 | **Prototype: **master:listen(backlog) | ||
5480 | |||
5481 | **Function: **Listening socket, convert the specified master to server object. | ||
5482 | |||
5483 | **Parameters: ** | ||
5484 | |||
5485 | * backlog(number):Specify the number of client connections that can be queued for service. If the queue is full and another client tries to connect, the connection will be rejected. | ||
5486 | |||
5487 | **Return: ** | ||
5488 | |||
5489 | * Success: Return 1. | ||
5490 | * Failure: return nil and error. | ||
5491 | |||
5492 | == [[tcp:accept()>>||anchor="HTCPmodule"]] == | ||
5493 | |||
5494 | **Prototype: **server:accept( ) | ||
5495 | |||
5496 | **Function: **Wait for a remote connection and return the server object representing the connection. | ||
5497 | |||
5498 | **Parameters: **None | ||
5499 | |||
5500 | **Return: ** | ||
5501 | |||
5502 | * Success: Return a client object. | ||
5503 | * Failure: return nil and error. (If the timeout condition is met, return nil and error string "timeout") | ||
5504 | |||
5505 | **Note:** | ||
5506 | |||
5507 | * Call select using the server socket in the receive parameter before calling accept does not guarantee that accept will return immediately. Using the settimeout method or accept may cause block forever until another client appears. | ||
5508 | |||
5509 | == [[tcp:send()>>||anchor="HTCPmodule"]] == | ||
5510 | |||
5511 | **Prototype: **client:send(data [, i [, j]]) | ||
5512 | |||
5513 | **Function: **Send data. | ||
5514 | |||
5515 | **Parameters: ** | ||
5516 | |||
5517 | * data(string): The string to be sent | ||
5518 | * i,j(number): The parameter fucntion is like string.sub. You could select a substring to send. | ||
5519 | |||
5520 | **Return: ** | ||
5521 | |||
5522 | * Success: Return the index of the last byte in the sent [i,j]. If i is 1 or does not exist, then it is the actual total | ||
5523 | * Failure: return nil and error. | ||
5524 | |||
5525 | **Note:** | ||
5526 | |||
5527 | * The output is not buffered. For small strings, it is better to connect them in Lua and send the result in one call rather than calling the method multiple times. | ||
5528 | |||
5529 | == [[tcp:receive()>>||anchor="HTCPmodule"]] == | ||
5530 | |||
5531 | **Prototype: **client:receive([pattern [, prefix]]) | ||
5532 | |||
5533 | **Function: **Read data from client object according to the specified read mode. The mode follows the Lua file I/O format, and the performance differences between all modes could be ignored. | ||
5534 | |||
5535 | **Parameters: ** | ||
5536 | |||
5537 | * pattern(string): | ||
5538 | ** '*a': Read from socket until the connection is closed. End-of-line translation is not performed. | ||
5539 | ** '*l': Read a line of text from socket. The line is ends with the LF character (ASCII 10), and optionally preceded by the CR character (ASCII 13). CR and LF characters are not included in the returned line. In fact, the mode ignores all CR characters. This is the default mode. | ||
5540 | ** number: Read the specified number of bytes from socket. | ||
5541 | * prefix(string): Prefix is an optional string, and connect to the beginning of any received data before returning. | ||
5542 | |||
5543 | **Return: ** | ||
5544 | |||
5545 | * Success: Return the received data. | ||
5546 | * Failure: return nil and error. | ||
5547 | |||
5548 | == [[tcp:close()>>||anchor="HTCPmodule"]] == | ||
5549 | |||
5550 | **Prototype: ** | ||
5551 | |||
5552 | * master:close() | ||
5553 | * clien:close() | ||
5554 | * server:close() | ||
5555 | |||
5556 | **Function: **Close a TCP object | ||
5557 | |||
5558 | **Parameters: **None | ||
5559 | |||
5560 | **Return: **None | ||
5561 | |||
5562 | **Note:** | ||
5563 | |||
5564 | * It is important to close all the sockets when not needed. Because in many systems, every socket uses a file descriptor. This is a limited system resource. However, the garbage collection object is automatically closed before being destroyed. | ||
5565 | |||
5566 | == [[tcp:getoption()>>||anchor="HTCPmodule"]] == | ||
5567 | |||
5568 | **Prototype: ** | ||
5569 | |||
5570 | * client:getoption(option) | ||
5571 | * server:getoption(option) | ||
5572 | |||
5573 | **Function: **Get the options of TCP object. Option is a string with option name. | ||
5574 | |||
5575 | **Parameters: ** | ||
5576 | |||
5577 | * option(string): Option is a string with option name. ("keepalive", "linger", "reuseaddr", "tcp-nodelay") | ||
5578 | |||
5579 | **Return: ** | ||
5580 | |||
5581 | * Success: Return option value. | ||
5582 | * Failure: return nil and error. | ||
5583 | |||
5584 | == [[tcp:getpeername()>>||anchor="HTCPmodule"]] == | ||
5585 | |||
5586 | **Prototype: **client:getpeername() | ||
5587 | |||
5588 | **Function: **Return remote information of the connected client object. | ||
5589 | |||
5590 | **Parameters: **None | ||
5591 | |||
5592 | **Return: ** | ||
5593 | |||
5594 | * return a string, including the IP address of the peer, the port number used by the peer to connect and a string with series ("inet" or "inet6"). If an error occurs, return nil. | ||
5595 | |||
5596 | == [[tcp:getsockname()>>||anchor="HTCPmodule"]] == | ||
5597 | |||
5598 | **Prototype: ** | ||
5599 | |||
5600 | * master:getsockname() | ||
5601 | * client:getsockname() | ||
5602 | * server:getsockname() | ||
5603 | |||
5604 | **Function: **Return information about the local address associated with the object. | ||
5605 | |||
5606 | **Parameters: **None | ||
5607 | |||
5608 | **Return: ** | ||
5609 | |||
5610 | * Success: Return a string with local IP address, a number with local port and a string with a series "inet" or "inet6". | ||
5611 | * Failure: return nil and error. | ||
5612 | |||
5613 | == [[tcp:getstats()>>||anchor="HTCPmodule"]] == | ||
5614 | |||
5615 | **Prototype: ** | ||
5616 | |||
5617 | * master:getstats() | ||
5618 | * client:getstats() | ||
5619 | * server:getstats() | ||
5620 | |||
5621 | **Function: **Return accounting information about socket. It could be used to limit bandwidth. | ||
5622 | |||
5623 | **Parameters: **None | ||
5624 | |||
5625 | **Return: ** | ||
5626 | |||
5627 | * Success: Return the number of the bytes received, he number of the bytes sent and the time of socket object. (Unit: second) | ||
5628 | * Failure: return nil and error. | ||
5629 | |||
5630 | == [[tcp:gettimeout()>>||anchor="HTCPmodule"]] == | ||
5631 | |||
5632 | **Prototype: ** | ||
5633 | |||
5634 | * master:settimeout*value [, mode]) | ||
5635 | * client:settimeout(value [, mode])server:settimeout(value [, mode]) | ||
5636 | |||
5637 | **Function: **Change the timeout value of tcp object. | ||
5638 | |||
5639 | **Parameters: ** | ||
5640 | |||
5641 | * value(number): The amount of time to wait is specified as the value parameter in seconds. nil and negative value allow operations to block indefinitely. | ||
5642 | * mod(string):timeout mode | ||
5643 | |||
5644 | **Return: **None | ||
5645 | |||
5646 | **Note:** | ||
5647 | |||
5648 | 1. By default, all the I/O operations are blocked. That is, any call to the method send, receive and accept will indefinitely block until the operation is complete. The method settimeout define a limit on the amount of time that n I/O method can block. When timeout is set and after a specified amount of time, the affected method aborts and fails with an error code. | ||
5649 | 1. There are two timeout modes: | ||
5650 | |||
5651 | * 'B': block timeout. It specifies the maximum time limit that the operating system can block LuaSocket while waiting for any single I/O operation to complete. This is the default mode. | ||
5652 | * 't' : total timeout. It specifies the maximum time limit that LuaSocket can block Lua scripts before the call returns | ||
5653 | |||
5654 | (% start="3" %) | ||
5655 | 1. Although the timeout value in LuaSocket has millisecond precision, large blocks can cause I/O functions not to respect timeout values due to the time the library takes to transfer blocks to and from the OS and to and from the Lua interpreter. Also, function that accept host names and perform automatic name resolution might be blocked by the resolver for longer than the specified timeout value. | ||
5656 | |||
5657 | == [[tcp:setoption()>>||anchor="HTCPmodule"]] == | ||
5658 | |||
5659 | **Prototype: ** | ||
5660 | |||
5661 | * client:setoption(option [, value]) | ||
5662 | * server:setoption(option [, value]) | ||
5663 | |||
5664 | **Function: **Set the option of TCP object. Only low-level or time-critical applications require options. If you are sure that you need it, you should only modify one option. | ||
5665 | |||
5666 | **Parameters: ** | ||
5667 | |||
5668 | * option(string): option name | ||
5669 | * valu(number): option value | ||
5670 | |||
5671 | **Return: ** | ||
5672 | |||
5673 | * Success: Return 1. | ||
5674 | * Failure: return nil and error. | ||
5675 | |||
5676 | **Note:** | ||
5677 | |||
5678 | * 'keepalive': Set this option to true to transmit messages regularly on the connected socket. If the connection fails to respond to these messages, then it is considered disconnected and the process using the socket is notified. | ||
5679 | * 'linbger': Controls the action taken when unsent data is queued on the socket and the action taken when it is closed. The value is a table of numeric entries with a Boolean entry "on" and a time interval "timeout" (in seconds). If the "on" field is set to true, the system blocks the process on a shutdown attempt until it can transfer data or until the "timeout" passes. If "on" is false and a shutdown is issued, the system will process it in a way that allows the process to continue as soon as possible. It is not recommended to set it to anything value other than 0. | ||
5680 | Shut down. I do not recommend that you set it to anything other than zero | ||
5681 | * 'reuseaddr': Set this option indicates that the rule used to validate the address provided in the binding call should allow the reuse of the local address. | ||
5682 | * 'tcp-nodelay': Set this option to true would disable the Nagle algorithm for connection. | ||
5683 | * 'tcp-keepidle': Only applicable to the value of TCP_KEEPIDLE Linux. (Unit: second) | ||
5684 | * 'tcp-keepcnt': Only applicable to the value of TCP_KEEPCNT Linux. | ||
5685 | * 'tcp-keepintv1': Only applicable to the value of TCP_KEEPINTVL Linux. | ||
5686 | * 'ipv6-v6only': Set this option to true would restrict the inET6 socket to only sending and receiving IPv6 packets. | ||
5687 | |||
5688 | == [[tcp:setstats()>>||anchor="HTCPmodule"]] == | ||
5689 | |||
5690 | **Prototype: ** | ||
5691 | |||
5692 | * master:setstats(received, sent, age) | ||
5693 | * client:setstats(received, sent, age) | ||
5694 | * server:setstats(received, sent, age) | ||
5695 | |||
5696 | **Function: **Reset accounting information on sockets. It is useful for bandwidth limiting. | ||
5697 | |||
5698 | **Parameters: ** | ||
5699 | |||
5700 | * received: Received is a number with the number of new received bytes | ||
5701 | * shend: Sent is a number with the number of new Sent bytes. | ||
5702 | * age: Age is the new Age in seconds. | ||
5703 | |||
5704 | **Return: ** | ||
5705 | |||
5706 | * Success: Return 1. | ||
5707 | * Failure: return nil | ||
5708 | |||
5709 | == [[tcp:settimeout()>>||anchor="HTCPmodule"]] == | ||
5710 | |||
5711 | **Prototype:** | ||
5712 | |||
5713 | * master:settimeout*value [, mode]) | ||
5714 | * client:settimeout(value [, mode])server:settimeout(value [, mode]) | ||
5715 | |||
5716 | **Function: **Change the timeout value of tcp object. | ||
5717 | |||
5718 | **Parameters: ** | ||
5719 | |||
5720 | * value(number):The amount of time to wait. nil and negative value are indefinite block. (Unit: second) | ||
5721 | * mod(string):timeout mode | ||
5722 | |||
5723 | **Return: **None | ||
5724 | |||
5725 | **Note:** | ||
5726 | |||
5727 | 1. By default, all the I/O operations are blocked. That is, any call to the method send, receive and accept will indefinitely block until the operation is complete. The method settimeout define a limit on the amount of time that n I/O method can block. When timeout is set and after a specified amount of time, the affected method aborts and fails with an error code. | ||
5728 | 1. There are two timeout modes: | ||
5729 | |||
5730 | * 'B': block timeout. It specifies the maximum time limit that the operating system can block LuaSocket while waiting for any single I/O operation to complete. This is the default mode. | ||
5731 | * 't' : total timeout. It specifies the maximum time limit that LuaSocket can block Lua scripts before the call returns | ||
5732 | |||
5733 | 1. Although the timeout value in LuaSocket has millisecond precision | ||
5734 | |||
5735 | == [[tcp:shutdown()>>||anchor="HTCPmodule"]] == | ||
5736 | |||
5737 | **Prototype: **client:shutdown(mode) | ||
5738 | |||
5739 | **Function: **Disable some full-duplex connections | ||
5740 | |||
5741 | **Parameters: ** | ||
5742 | |||
5743 | * mode: Close connection mode | ||
5744 | ** "both": Close send and receive connection (default mode) | ||
5745 | ** "send": Close send connection | ||
5746 | ** "receive": Close receive connection | ||
5747 | |||
5748 | **Return: ** | ||
5749 | |||
5750 | * Success: Return 1. | ||
5751 | * Failure: Return nil and error. | ||
5752 | |||
5753 | == [[tcp:getfamily()>>||anchor="HTCPmodule"]] == | ||
5754 | |||
5755 | **Prototype: ** | ||
5756 | |||
5757 | * master:getfamily() | ||
5758 | * client:getfamily() | ||
5759 | * server:getfamily() | ||
5760 | |||
5761 | **Function: **Obtain tcp protocol family | ||
5762 | |||
5763 | **Parameters: **None | ||
5764 | |||
5765 | **Return: ** | ||
5766 | |||
5767 | * Return a protocol family string | ||
5768 | |||
5769 | == [[tcp:setpeername()>>||anchor="HTCPmodule"]] == | ||
5770 | |||
5771 | **Prototype: **client:setpeername(address, port) | ||
5772 | |||
5773 | **Function: **Change the host object to connect to a remote host. | ||
5774 | |||
5775 | **Parameters: ** | ||
5776 | |||
5777 | * address(string): IP address or host name | ||
5778 | * port(number): port number | ||
5779 | |||
5780 | **Return: ** | ||
5781 | |||
5782 | * Success: Return 1. | ||
5783 | * Failure: Return nil and error. | ||
5784 | |||
5785 | == [[tcp:setsockname>>||anchor="HTCPmodule"]] == | ||
5786 | |||
5787 | **Prototype: **server:setsockname(address, port) | ||
5788 | |||
5789 | **Function: **Change the address and port number of the objectified binding. | ||
5790 | |||
5791 | **Parameters: ** | ||
5792 | |||
5793 | * address(string): IP address or host name | ||
5794 | * port(number): port number | ||
5795 | |||
5796 | **Return: ** | ||
5797 | |||
5798 | * Success: Return 1. | ||
5799 | * Failure: Return nil and error. | ||
5800 | |||
5801 | = UDP module = | ||
5802 | |||
5803 | == [[udp:send()>>||anchor="HUDPmodule"]] == | ||
5804 | |||
5805 | **Prototype: **connected:send(datagram) | ||
5806 | |||
5807 | **Function: **Sends datagrams to connected udp objects. | ||
5808 | |||
5809 | **Parameters: ** | ||
5810 | |||
5811 | * datagram(string): Datagram is a string with the datagram contents | ||
5812 | |||
5813 | **Return: ** | ||
5814 | |||
5815 | * Success: Returns the number of bytes sent. | ||
5816 | * Failure: return nil and error. | ||
5817 | |||
5818 | **Note:** | ||
5819 | |||
5820 | * ((( | ||
5821 | The maximum datagram size for UDP is 64K minus IP layer overhead. However datagrams larger than the link layer packet size will be fragmented, which may deteriorate performance and reliability. | ||
5822 | ))) | ||
5823 | * ((( | ||
5824 | In UDP, the send method never blocks and the only way it can fail is if the underlying transport layer refuses to send a message to the specified address (i.e. no interface accepts the address). | ||
5825 | ))) | ||
5826 | * ((( | ||
5827 | If the destination does not exist, the packet is discarded and no error is returned. | ||
5828 | ))) | ||
5829 | |||
5830 | == [[udp:sendto()>>||anchor="HUDPmodule"]] == | ||
5831 | |||
5832 | **Prototype: **unconnected:sendto(datagram, ip, port) | ||
5833 | |||
5834 | **Function: **Sends datagrams to connected udp objects. | ||
5835 | |||
5836 | **Parameters: ** | ||
5837 | |||
5838 | * datagram(string): Datagram is a string with the datagram contents | ||
5839 | * ip(string): Destination IP. Host names are not allowed due to performance. | ||
5840 | * port(number): Destination port | ||
5841 | |||
5842 | **Return: ** | ||
5843 | |||
5844 | * Success: Returns 1. | ||
5845 | * Failure: return nil and error. | ||
5846 | |||
5847 | **Note:** | ||
5848 | |||
5849 | * ((( | ||
5850 | The maximum datagram size for UDP is 64K minus IP layer overhead. However datagrams larger than the link layer packet size will be fragmented, which may deteriorate performance and reliability. | ||
5851 | ))) | ||
5852 | * ((( | ||
5853 | In UDP, the send method never blocks and the only way it can fail is if the underlying transport layer refuses to send a message to the specified address (i.e. no interface accepts the address). | ||
5854 | ))) | ||
5855 | |||
5856 | == [[udp:receive()>>||anchor="HUDPmodule"]] == | ||
5857 | |||
5858 | **Prototype:** | ||
5859 | |||
5860 | * connected:receive([size]) | ||
5861 | * unconnected:receive([size]) | ||
5862 | |||
5863 | **Function: **Receives a datagram from the UDP object. If the UDP object is connected, only datagrams coming from the peer are accepted. Otherwise, the returned datagram can come from any host. | ||
5864 | |||
5865 | **Parameters: ** | ||
5866 | |||
5867 | * size(number): Specifies the maximum size of the datagram to be retrieved | ||
5868 | |||
5869 | **Return: ** | ||
5870 | |||
5871 | * Success: Returns the datagram received. | ||
5872 | * Failure: return nil and error. | ||
5873 | |||
5874 | **Note:** | ||
5875 | |||
5876 | * ((( | ||
5877 | If there are more than size bytes available in the datagram, the excess bytes are discarded. If there are less then size bytes available in the current datagram, the available bytes are returned. If size is omitted, the compile-time constant ##socket._DATAGRAMSIZE## is used (it defaults to 8192 bytes). Larger sizes will cause a temporary buffer to be allocated for the operation. | ||
5878 | ))) | ||
5879 | |||
5880 | == [[udp:receivefrom()>>||anchor="HUDPmodule"]] == | ||
5881 | |||
5882 | **Prototype:** unconnected:receivefrom([size]) | ||
5883 | |||
5884 | **Function: **It works exactly as the receive method, except it returns the IP address and port as extra return values, and is therefore slightly less efficient. | ||
5885 | |||
5886 | **Parameters: ** | ||
5887 | |||
5888 | * size(number): Specifies the maximum size of the datagram to be retrieved | ||
5889 | |||
5890 | **Return: ** | ||
5891 | |||
5892 | * dgram(string):received data | ||
5893 | * adderss(string):IP address | ||
5894 | * port(number):Port | ||
5895 | |||
5896 | == [[udp:close()>>||anchor="HUDPmodule"]] == | ||
5897 | |||
5898 | **Prototype: ** | ||
5899 | |||
5900 | * connected:close() | ||
5901 | * unconnected:close() | ||
5902 | |||
5903 | **Function: **Closes a UDP object. The internal socket used by the object is closed and the local address to which the object was bound is made available to other applications. No further operations (except for further calls to the ##close## method) are allowed on a closed socket. | ||
5904 | |||
5905 | **Parameters: **None | ||
5906 | |||
5907 | **Return: **None | ||
5908 | |||
5909 | **Note:** | ||
5910 | |||
5911 | * ((( | ||
5912 | It is important to close all used sockets once they are not needed, since, in many systems, each socket uses a file descriptor, which are limited system resources. Garbage-collected objects are automatically closed before destruction, though. | ||
5913 | ))) | ||
5914 | |||
5915 | == [[udp:gettimeout()>>||anchor="HUDPmodule"]] == | ||
5916 | |||
5917 | **Prototype: ** | ||
5918 | |||
5919 | * connected:settimeout( ) | ||
5920 | * unconnected:settimeout( ) | ||
5921 | |||
5922 | **Function: **Obtains the current timeout value. | ||
5923 | |||
5924 | **Parameters: **None | ||
5925 | |||
5926 | **Return: ** | ||
5927 | |||
5928 | * Returns the current timeout value. | ||
5929 | |||
5930 | == [[udp:settimeout()>>||anchor="HUDPmodule"]] == | ||
5931 | |||
5932 | **Prototype: ** | ||
5933 | |||
5934 | * connected:settimeout(value) | ||
5935 | * unconnected:settimeout(value) | ||
5936 | |||
5937 | **Function: **Change the timeout value of UDP object. | ||
5938 | |||
5939 | **Parameters:** | ||
5940 | |||
5941 | * value(number): The amount of time to wait is specified as the value parameter in seconds. nil and negative value allow operations to block indefinitely. | ||
5942 | |||
5943 | **Return: ** | ||
5944 | |||
5945 | * Returns 1 | ||
5946 | |||
5947 | **Note:** | ||
5948 | |||
5949 | * ((( | ||
5950 | By default, the operations of receive and receiveform are blocked. | ||
5951 | ))) | ||
5952 | * ((( | ||
5953 | In UDP, the send and sendto methods never block (the datagram is just passed to the OS and the call returns immediately). Therefore, the settimeout method has no effect on them. | ||
5954 | ))) | ||
5955 | |||
5956 | == [[udp:getoption()>>||anchor="HUDPmodule"]] == | ||
5957 | |||
5958 | **Prototype: ** | ||
5959 | |||
5960 | * connected:getoption() | ||
5961 | * unconnected:getoption (optionName) | ||
5962 | |||
5963 | **Function: **Gets an option value from the UDP object | ||
5964 | |||
5965 | **Parameters:** | ||
5966 | |||
5967 | * optionName: option name. | ||
5968 | |||
5969 | **Return: ** | ||
5970 | |||
5971 | * Success: Returns the option value. | ||
5972 | * Failure: Returns nil and error. | ||
5973 | |||
5974 | **Note:** | ||
5975 | |||
5976 | Option names are as follows: | ||
5977 | |||
5978 | * 'dontroute': Indicates that outgoing messages should bypass standard routing facilities. | ||
5979 | * 'broadcast': Requests permission to send broadcast datagrams on sockets. | ||
5980 | * 'reuseaddr': Rules that represent the validation of addresses provided in a bind() call should allow the reuse of local addresses. | ||
5981 | * 'reuseport': If multiple processes have set 'reusePort' before binding the port, they are allowed to double bind completely. | ||
5982 | * 'ip-multicast-loop': Specifies whether a copy of an outgoing multicast datagram is sent to the sending host as long as it is a member of a multicast group. | ||
5983 | * 'ipv6-v6only': Specifies whether inet6 sockets are restricted to sending and receiving only IPv6 packets. | ||
5984 | * 'ip-multicast-if': Sets the interface for sending multicast datagrams. | ||
5985 | * 'ip-multicast-ttl': Sets the lifetime in the IP header for outgoing multicast datagrams. | ||
5986 | * 'ip-add-membership': Added to the specified multicast group. | ||
5987 | * 'ip-drop-membership': Leaves the specified multicast group. | ||
5988 | |||
5989 | == [[udp:getpeername()>>||anchor="HUDPmodule"]] == | ||
5990 | |||
5991 | **Prototype: ** | ||
5992 | |||
5993 | * connected:getpeername() | ||
5994 | * unconnected:getpeername() | ||
5995 | |||
5996 | **Function: **Retrieves information about the peer associated with a connected UDP object. | ||
5997 | |||
5998 | **Parameters: **None | ||
5999 | |||
6000 | **Return: ** | ||
6001 | |||
6002 | * Success: Returns a string with the IP address of the peer, the port number that peer is using for the connection, and a string with the family ("inet4" or "inet6"). | ||
6003 | * Failure: return nil and error. | ||
6004 | |||
6005 | **Note:** | ||
6006 | |||
6007 | * It makes no sense to call this method on unconnected objects. | ||
6008 | |||
6009 | == [[udp:getsockname()>>||anchor="HUDPmodule"]] == | ||
6010 | |||
6011 | **Prototype: ** | ||
6012 | |||
6013 | * connected:getsockname() | ||
6014 | * unconnected:getsockname() | ||
6015 | |||
6016 | **Function: **Returns the local address information associated to the object. | ||
6017 | |||
6018 | **Parameters: **None | ||
6019 | |||
6020 | **Return: ** | ||
6021 | |||
6022 | * Success: Returns a string with local IP address, a number with the local port, and a string with the family ("inet4" or "inet6"). | ||
6023 | * Failure: return nil and error. | ||
6024 | |||
6025 | **Note:** | ||
6026 | |||
6027 | * UDP sockets are not bound to any address until the setsockname or the sendto method is called for the first time (in which case it is bound to an ephemeral port and the wild-card address). | ||
6028 | |||
6029 | == [[udp:setoption()>>||anchor="HUDPmodule"]] == | ||
6030 | |||
6031 | **Prototype: ** | ||
6032 | |||
6033 | * connected:setoption(option [, value]) | ||
6034 | * unconnected:setoption(option [, value]) | ||
6035 | |||
6036 | **Function: **Sets options for the UDP object. | ||
6037 | |||
6038 | **Parameters: ** | ||
6039 | |||
6040 | * option(string): Option name | ||
6041 | * value: Option value and it depends on the option being set. | ||
6042 | |||
6043 | **Return: ** | ||
6044 | |||
6045 | * Success: Returns 1 | ||
6046 | * Failure: return nil and error. | ||
6047 | |||
6048 | **Note:** | ||
6049 | |||
6050 | Options are only needed by low-level or time-critical applications. You should only modify an option if you are sure you need it. | ||
6051 | |||
6052 | Option name are as follows: | ||
6053 | |||
6054 | * 'dontroute': Indicates that outgoing messages should bypass the standard routing facilities. Receives a boolean value. | ||
6055 | * 'broadcast': Requests permission to send broadcast datagrams on the socket. Receives a boolean value. | ||
6056 | * 'reuseaddr': Indicates that the rules used in validating addresses supplied in a ##bind()## call should allow reuse of local addresses. Receives a boolean value. | ||
6057 | * 'reuseport': Allows completely duplicate bindings by multiple processes if they all set '##reuseport##' before binding the port. Receives a boolean value. | ||
6058 | * 'ip-multicast-loop': Specifies whether or not a copy of an outgoing multicast datagram is delivered to the sending host as long as it is a member of the multicast group. Receives a boolean value. | ||
6059 | * 'ipv6-v6only': Specifies whether to restrict ##inet6## sockets to sending and receiving only IPv6 packets. Receive a boolean value. | ||
6060 | * 'ip-multicast-if': Sets the interface over which outgoing multicast datagrams are sent. Receives an IP address. | ||
6061 | * 'ip-multicast-ttl': Sets the Time To Live in the IP header for outgoing multicast datagrams. Receives a number. | ||
6062 | * 'ip-add-membership': Joins the multicast group specified. Receives a table with fields multiaddr and interface, each containing an IP address. | ||
6063 | * 'ip-drop-membership': Leaves the multicast group specified. Receives a table with fields multiaddr and interface, each containing an IP address. | ||
6064 | |||
6065 | == [[udp:setpeername()>>||anchor="HUDPmodule"]] == | ||
6066 | |||
6067 | **Prototype: ** | ||
6068 | |||
6069 | * connected:setpeername('*') | ||
6070 | * unconnected:setpeername(address, port) | ||
6071 | |||
6072 | **Function: **Changes the peer of a UDP object. | ||
6073 | |||
6074 | **Parameters:** | ||
6075 | |||
6076 | * "* "(string): If address is '##*##' and the object is connected, the peer association is removed and the object becomes an unconnected object again. | ||
6077 | * address(string), port(number) —— address could be IP address or hostname. Port is the port number. | ||
6078 | |||
6079 | **Return: ** | ||
6080 | |||
6081 | * Success: Returns 1. | ||
6082 | * Failure: Returns nil and error. | ||
6083 | |||
6084 | **Note:** | ||
6085 | |||
6086 | * This method converts an unconnected UDP object into a connected UDP object or vice versa. | ||
6087 | * For connected objects, the outgoing datagrams will be sent to the specified peer, and datagrams received from other peers will be discarded by the OS. The connected UDP objects must use the sand and receive methods instead of sendto and receiveform. | ||
6088 | * Since the address of the peer does not have to be passed to and from the OS, the use of connected UDP objects is recommended when the same peer is used for several transmissions and can result in up to 30% performance gains. | ||
6089 | * Starting with LuaSocket 3.0, the host name resolution depends on whether the socket was created by socket.udp and socket.udp6. Addresses from the appropriate family are tried in succession until the first success or until the last failure. | ||
6090 | |||
6091 | == [[udp:setsockname()>>||anchor="HUDPmodule"]] == | ||
6092 | |||
6093 | **Prototype: **unconnected:setsockname(address, port) | ||
6094 | |||
6095 | **Function: **Binds the UDP object to a local address. | ||
6096 | |||
6097 | **Parameters:** | ||
6098 | |||
6099 | * address(string): Bind address. The address can be an IP address or a host name. If address is '*', the system will bind to all local interfaces using the constant INADDR_ANY. | ||
6100 | * port(number): Port number. It the port is 0, the system chooses an ephemeral port. | ||
6101 | |||
6102 | **Return: ** | ||
6103 | |||
6104 | * Success: Returns 1. | ||
6105 | * Failure: Returns nil and error. | ||
6106 | |||
6107 | **Note:** | ||
6108 | |||
6109 | * This method could only be called before any datagram is sent through the UDP object, and only once. Otherwise, the system automatically binds the object to all local interfaces and chooses an ephemeral port as soon as the first datagram is sent. After the local address is set, either automatically by the system or explicitly by socketname, it cannot be changed. | ||
6110 | |||
6111 | == [[udp:getfamily()>>||anchor="HUDPmodule"]] == | ||
6112 | |||
6113 | **Prototype: ** | ||
6114 | |||
6115 | * connect:getfamily | ||
6116 | * unconnect:getfamily | ||
6117 | |||
6118 | **Function: **Returns a TCP family | ||
6119 | |||
6120 | **Parameters: **None | ||
6121 | |||
6122 | **Return: ** | ||
6123 | |||
6124 | * Success: Returns a protocol family string | ||
6125 | * Failure: Returns nil and error. | ||
6126 | |||
6127 | = ftp module = | ||
6128 | |||
6129 | == [[ftp.command()>>||anchor="HFTPmodule"]] == | ||
6130 | |||
6131 | **Prototype: **ftp.command(cmdt) | ||
6132 | |||
6133 | **Function: **Send ** **ftp command | ||
6134 | |||
6135 | **Parameters:** | ||
6136 | |||
6137 | * ((( | ||
6138 | cmdt(table):ftp command list | ||
6139 | |||
6140 | { | ||
6141 | |||
6142 | host = string, ~-~- address | ||
6143 | |||
6144 | command = string, ~-~- ftp command | ||
6145 | |||
6146 | port = number, ~-~- port | ||
6147 | |||
6148 | user = string, ~-~- username | ||
6149 | |||
6150 | password = string or table , ~-~- password | ||
6151 | |||
6152 | argument = string or table, ~-~- upload parameter | ||
6153 | |||
6154 | check = function, ~-~- Check function | ||
6155 | |||
6156 | create = function, ~-~- Create a connection function | ||
6157 | |||
6158 | } | ||
6159 | ))) | ||
6160 | |||
6161 | **Return: ** | ||
6162 | |||
6163 | * Success: Returns 1. | ||
6164 | * Failure: Returns nil and error. | ||
6165 | |||
6166 | **Note:** | ||
6167 | |||
6168 | * This function only sends commands and does not receive data. To receive data, you need to create another channel. | ||
6169 | |||
6170 | **Example** | ||
6171 | |||
6172 | {{code language="LUA"}} | ||
6173 | -- 1. Introduce modules | ||
6174 | -- local ftp = require("socket.ftp") | ||
6175 | -- local LTN12 = require("ltn12") | ||
6176 | |||
6177 | -- 2. Send command | ||
6178 | local tabRecv = {} -- Create a table to receive data | ||
6179 | local state,err = ftp.command({ | ||
6180 | host = "192.168.50.225", -- Set ftp server address to "192.168.50.225" | ||
6181 | port = 21, -- Set ftp server port to 21 | ||
6182 | -- user = ftp.USER, -- The password is set to the default anonymous account, so it can be omitted | ||
6183 | -- password = ftp.PASSWORD, | ||
6184 | command = "DELE", -- Set to send ftp command "DELE". For details about the command, see the appendix | ||
6185 | argument = "/test.png", -- Set the Delete command parameter "/test.png", (delete "/test.png" file) | ||
6186 | sink = LTN12.sink.table(tabRecv) -- Configure the receiver | ||
6187 | }) | ||
6188 | -- 3. Print result | ||
6189 | if state and err==nil then | ||
6190 | print("dele '/test.png' success ! ") | ||
6191 | else | ||
6192 | print("faild !",err) | ||
6193 | end | ||
6194 | {{/code}} | ||
6195 | |||
6196 | == [[ftp.genericform()>>||anchor="HFTPmodule"]] == | ||
6197 | |||
6198 | **Prototype: **ftp.genericform(u, b) | ||
6199 | |||
6200 | **Function: **Request parameter conversion. The simple URL request mode convert to a general table table parameter form. | ||
6201 | |||
6202 | **Parameters:** | ||
6203 | |||
6204 | * u(string): The requested URL field | ||
6205 | * b(string): The requested body field | ||
6206 | |||
6207 | **Return: ** | ||
6208 | |||
6209 | * General request table parameter | ||
6210 | |||
6211 | **Example** | ||
6212 | |||
6213 | {{code language="LUA"}} | ||
6214 | local ftp = require("socket.ftp") | ||
6215 | local json = require("json") | ||
6216 | |||
6217 | local server_ip = "192.168.50.225" | ||
6218 | local server_file1 = "/chenGH/ftpput.txt" | ||
6219 | |||
6220 | local url = "ftp://"..server_ip..server_file1 | ||
6221 | |||
6222 | local t = assert(ftp.genericform(url)) | ||
6223 | print(type(t),json.encode(t)) | ||
6224 | --------------------------------------------------------------------------------Output: | ||
6225 | -- table {"path":"/chenGH/ftpput.txt","scheme":"ftp","host":"192.168.50.225","authority": | ||
6226 | --"192.168.50.225"} | ||
6227 | {{/code}} | ||
6228 | |||
6229 | == [[ftp.put()>>||anchor="HFTPmodule"]] == | ||
6230 | |||
6231 | **Prototype 1: **ftp.**put(** url, content **)** | ||
6232 | |||
6233 | **Function: **Upload url data | ||
6234 | |||
6235 | **Parameters:** | ||
6236 | |||
6237 | * url: Resource identification locator | ||
6238 | * content: Data to be uploaded | ||
6239 | |||
6240 | **Return: ** | ||
6241 | |||
6242 | * Success: Return 1. | ||
6243 | * Failure: Return nil and error. | ||
6244 | |||
6245 | **Prototype 2: ** | ||
6246 | |||
6247 | ftp.**put{** | ||
6248 | host = //string//, | ||
6249 | source = //LTN12 sink//, | ||
6250 | argument //or// path = //string//, | ||
6251 | [user = //string//,] | ||
6252 | [password = //string//] | ||
6253 | [command = //string//,] | ||
6254 | [port = //number//,] | ||
6255 | [type = //string//,] | ||
6256 | [step = //LTN12 pump step//,] | ||
6257 | [create = //function//] | ||
6258 | **}** | ||
6259 | |||
6260 | **Function: **Data upload is controlled by fields | ||
6261 | |||
6262 | **Parameters:** | ||
6263 | |||
6264 | * host: houstname, IP address (Required parameters) | ||
6265 | * source: LTN12 object (Required parameters) | ||
6266 | * argument or path: Upload parameter/request path (Required parameters) | ||
6267 | * user: username | ||
6268 | * password: password | ||
6269 | * command: Used to send data. Defaults to ##stor##. | ||
6270 | * port: Used for the control connection. Defaults to 21 | ||
6271 | * type: The transfer mode. Can take values "##i##" or "##a##" (Binary or ASCII). The default value is the server default value. | ||
6272 | * step: Used to tranmist data from the server to the sink. Defaults to the LTN12 pump.step function. | ||
6273 | * create: An optional function to be used instead of socket.tcp when the communications socket is created. | ||
6274 | |||
6275 | **Return: ** | ||
6276 | |||
6277 | * Success: Return 1. | ||
6278 | * Failure: Return nil and error. | ||
6279 | |||
6280 | **Example** | ||
6281 | |||
6282 | {{code language="LUA"}} | ||
6283 | local ftp = require("socket.ftp") | ||
6284 | local ltn12 = require("ltn12") | ||
6285 | |||
6286 | local server_ip = "192.168.50.225" | ||
6287 | -- Create a file if the file does not exist, overwrites the original data if the file exists, | ||
6288 | and reports an error if the folder does not exist | ||
6289 | local server_file1 = "/chenGH/ftpput.txt" | ||
6290 | local server_file2 = "/chenGH/ftpput.dll" | ||
6291 | |||
6292 | -- ftp.put Data push format 1, simple mode, push simple data | ||
6293 | local url = "ftp://"..server_ip..server_file1 | ||
6294 | local ret,msg = ftp.put(url,"hello server ! this is put data !") | ||
6295 | if ret then | ||
6296 | print("put data success !") | ||
6297 | else | ||
6298 | print(msg) | ||
6299 | end | ||
6300 | |||
6301 | -- ftp.put Data push format 2, You can configure parameters to push complex data or files | ||
6302 | local put_param = {} -- Creatt a parameter table | ||
6303 | put_param.host = server_ip | ||
6304 | put_param.path = server_file2 | ||
6305 | put_param.source = ltn12.source.file(io.open("user:ftpTest.dll","rb"),"connot open file !") | ||
6306 | local ret = assert(ftp.put(put_param)) | ||
6307 | if ret then | ||
6308 | print("put data success ! ") | ||
6309 | end | ||
6310 | {{/code}} | ||
6311 | |||
6312 | == [[ftp.get()>>||anchor="HFTPmodule"]] == | ||
6313 | |||
6314 | **Prototype 1: **ftp.**get(** url, content **)** | ||
6315 | |||
6316 | **Function: **Download the contents of a URL and returns it as a string. | ||
6317 | |||
6318 | **Parameters:** | ||
6319 | |||
6320 | * 【string】 url —— url resource identifier | ||
6321 | |||
6322 | **Return: ** | ||
6323 | |||
6324 | * Success: Return the string content that the url points to. | ||
6325 | * Failure: Return nil and error. | ||
6326 | |||
6327 | **Note:** | ||
6328 | |||
6329 | * The cached string size cannot exceed 1 MB. If the string size exceeds 1 MB, data processing is performed throuhg LTN2. | ||
6330 | |||
6331 | **Prototype 2: ** | ||
6332 | |||
6333 | ftp.**get{** | ||
6334 | host = //string//, | ||
6335 | sink = //LTN12 sink//, | ||
6336 | argument //or// path = //string//, | ||
6337 | [user = //string//,] | ||
6338 | [password = //string//] | ||
6339 | [command = //string//,] | ||
6340 | [port = //number//,] | ||
6341 | [type = //string//,] | ||
6342 | [step = //LTN12 pump step//,] | ||
6343 | [create = //function//] | ||
6344 | **}** | ||
6345 | |||
6346 | **Function: **Download is controlled by parameter fields. | ||
6347 | |||
6348 | **Parameters:** | ||
6349 | |||
6350 | * host: houstname, IP address | ||
6351 | * source: LTN12 object | ||
6352 | * argument or path: Request parameter/request path | ||
6353 | * user: username. Default to "ftp". | ||
6354 | * password: password. Default to "anonymous@anonymous.org". | ||
6355 | * command: Used to get ftp command of data. Defaults to retr. | ||
6356 | * port: Used for the control connection. Defaults to 21. | ||
6357 | * type: The transfer mode. Can take values "i" or "a". The default value is the server default value. | ||
6358 | * step: Used to tranmist data from the server to the sink. Defaults to the LTN12 pump.step() | ||
6359 | * create: An optional function to be used instead of socket.tcp when the communications socket is created. | ||
6360 | |||
6361 | **Return: ** | ||
6362 | |||
6363 | * Success: Return 1. | ||
6364 | * Failure: Return nil and error. | ||
6365 | |||
6366 | **Note:** | ||
6367 | |||
6368 | * If the size of the retrieved file exceeds the size of the cache (1M), the receiver (prototype 2) is used to block the data or the retrieved data will fail. | ||
6369 | * The IO module of Lua is required to use the file receiver. Currently, the IO module of Hmi is not open, so files exceeding 1M cannot be obtained temporarily. | ||
6370 | |||
6371 | **Example** | ||
6372 | |||
6373 | {{code language="LUA"}} | ||
6374 | local ftp = require("socket.ftp") | ||
6375 | local ltn12 = require("ltn12") | ||
6376 | |||
6377 | local server_ip = "192.168.50.225" -- server address | ||
6378 | local server_port = "21" -- default to 21,If there is special, it can be changed. | ||
6379 | local server_user = "ftp" -- server name, default to "ftp" | ||
6380 | -- server passpword, default to "anonymous@anonymous.org" | ||
6381 | local server_password = "anonymous@anonymous.org" | ||
6382 | local server_file1 = "/chenGH/ftpTest.txt" -- Server destination file(File 1 you want to download) | ||
6383 | local server_file2 = "/chenGH/ftpTest.dll" -- Server destination file(File 2 you want to download) | ||
6384 | |||
6385 | |||
6386 | -- ftp.get get format 1,Enter the URL of the file directly | ||
6387 | -- Note: The size of the file cannot exceed the buffer size (1M), and exceeding will result in acquisition failure. | ||
6388 | local ftpUrl = "ftp://"..server_ip..server_file1 | ||
6389 | local data,msg = ftp.get(ftpUrl) | ||
6390 | if not data then | ||
6391 | print("ftp_get",msg) | ||
6392 | return nil | ||
6393 | else | ||
6394 | print(data) | ||
6395 | assert(flash.file_write("user:ftpTest.txt", "w+", data)) -- Create a local ftptest.txt file and write data to it | ||
6396 | end | ||
6397 | |||
6398 | -- ftp.get format 2,configure ftp.get paramter | ||
6399 | local dataTab = {} | ||
6400 | local getRequest = {} | ||
6401 | getRequest.host = server_ip | ||
6402 | getRequest.path = server_file2 | ||
6403 | -- io operation temporarily blocks I/O file operations in HMI. Therefore, files that exceed 1 MB cannot be written. | ||
6404 | getRequest.sink = ltn12.sink.file(io.open("user:ftpTest.dll", "wb")) | ||
6405 | assert(ftp.get(getRequest)) | ||
6406 | {{/code}} | ||
6407 | |||
6408 | = http module = | ||
6409 | |||
6410 | == [[http.request()>>||anchor="Hhttpmodule"]] == | ||
6411 | |||
6412 | **Prototype 1: **http.request(url [, body]) | ||
6413 | |||
6414 | **Function: **http request function | ||
6415 | |||
6416 | **Parameters:** | ||
6417 | |||
6418 | * url(string): Website address, uniform resource locator. | ||
6419 | * body: The requested data body | ||
6420 | |||
6421 | **Return: ** | ||
6422 | |||
6423 | * The simple form returns the response body as a string, followed by a response status code, a response header, and a response status line. | ||
6424 | |||
6425 | **Example** | ||
6426 | |||
6427 | {{code language="LUA"}} | ||
6428 | -- 1. Introduce modules | ||
6429 | local http = require("socket.http") | ||
6430 | local json = require("json") | ||
6431 | |||
6432 | -- 2. Constant and variable definitions | ||
6433 | local weather_url = "http://t.weather.itboy.net/api/weather/city" | ||
6434 | local city_code = "101010100" -- Beijing | ||
6435 | |||
6436 | -- 3. Get weather information | ||
6437 | local url = weather_url.."/"..city_code | ||
6438 | print(url) | ||
6439 | str_weatherInfo = http.request(url) | ||
6440 | -- print(str_weatherInfo) | ||
6441 | tab_weatherInfo = json.decode(str_weatherInfo) -- Convert the weather information in JSON format to a table | ||
6442 | print("wendu =",tab_weatherInfo["data"]["wendu"]) -- Get the temperature information in the table | ||
6443 | ----------------------------------------------------Output: | ||
6444 | --http://t.weather.itboy.net/api/weather/city/101010100 | ||
6445 | --wendu = 7 | ||
6446 | {{/code}} | ||
6447 | |||
6448 | **Prototype 2: **http.**request{** | ||
6449 | url = //string//, | ||
6450 | [sink = //LTN12 sink//,] | ||
6451 | [method = //string//,] | ||
6452 | [headers = //header-table//,] | ||
6453 | [source = //LTN12 source//], | ||
6454 | [step = //LTN12 pump step//,] | ||
6455 | [proxy = //string//,] | ||
6456 | [redirect = //boolean//,] | ||
6457 | [create = //function//] | ||
6458 | **}** | ||
6459 | |||
6460 | **Function: **http request function | ||
6461 | |||
6462 | **Parameters:** | ||
6463 | |||
6464 | * url: website address, uniform resource locator. (required parameter) | ||
6465 | * sink: LTN12 reveiver (optional parameter) | ||
6466 | * method: http request method, default to "get". (optional parameter) | ||
6467 | * headers: Any other HTTP headers sent with the request. (optional parameter) | ||
6468 | * source: //simple// LTN12 source to provide the request body. If there is a body, you need to provide an appropriate "content-length" request header field, or the function will attempt to send the body as "chunked" (something few servers support). Defaults to the empty source. (optional parameter) | ||
6469 | * step: Used to tranmist data from the server to the sink. Defaults to the LTN12 pump.step function. (optional parameter) | ||
6470 | * proXY: The URL of the proxy server to use. The default is no proxy. (optional parameter) | ||
6471 | * tredirect: Set to false to prevent this feature from automatically following 301 or 302 server redirection messages. (optional parameter) | ||
6472 | * create: An optional function to be used instead of socket.tcp when the communications socket is created.(optional parameter) | ||
6473 | |||
6474 | **Return: ** | ||
6475 | |||
6476 | * Success: Return 1, and stores the data to the receiver. | ||
6477 | * Failure: Return nil and error. | ||
6478 | |||
6479 | **Example** | ||
6480 | |||
6481 | {{code language="LUA"}} | ||
6482 | -- 1. Introduce modules | ||
6483 | local http = require("socket.http") | ||
6484 | local json = require("json") | ||
6485 | local LTN12= require("ltn12") | ||
6486 | |||
6487 | -- 2. Constant and variable definitions | ||
6488 | local weather_url = "http://t.weather.itboy.net/api/weather/city" | ||
6489 | local city_code = "101010100" -- Beijing | ||
6490 | local tabWeatherInfo = {} | ||
6491 | |||
6492 | -- 3. Get weather information | ||
6493 | local url = weather_url.."/"..city_code | ||
6494 | print(url) | ||
6495 | |||
6496 | local tabRequest = {} | ||
6497 | tabRequest.url = url | ||
6498 | tabRequest.sink = LTN12.sink.table(tabWeatherInfo) | ||
6499 | |||
6500 | status,str_weatherInfo = http.request(tabRequest) | ||
6501 | print(type(tabWeatherInfo[1])) | ||
6502 | print("str_weatherInfo =",tabWeatherInfo[1]) | ||
6503 | |||
6504 | -- Convert the weather information in JSON format to a table. Since the LTN12.sink.table receiver is used to receive, | ||
6505 | the accepted result is present in the tabWeatherInfo table.The receive occurs only once, and it is stored at the tabWeatherInfo[1]. | ||
6506 | local tab_weatherInfo = json.decode(tabWeatherInfo[1]) | ||
6507 | print("wendu =",tab_weatherInfo["data"]["wendu"]) -- Get the temperature information in the table. | ||
6508 | {{/code}} | ||
6509 | |||
6510 | == [[http.genericform()>>||anchor="Hhttpmodule"]] == | ||
6511 | |||
6512 | **Prototype : **http.genericform(u, b) | ||
6513 | |||
6514 | **Function: **Convert a simple URL to a generic table parameter | ||
6515 | |||
6516 | **Parameters:** | ||
6517 | |||
6518 | * u(string):The URL field that http requested | ||
6519 | * b(string):The body field that http requested | ||
6520 | |||
6521 | **Return: ** | ||
6522 | |||
6523 | * Generic table parameter | ||
6524 | |||
6525 | **Example** | ||
6526 | |||
6527 | {{code language="LUA"}} | ||
6528 | -- 1. Introduce modules | ||
6529 | local http = require("socket.http") | ||
6530 | |||
6531 | -- 2. Constant and variable definitions | ||
6532 | local url = "http://t.weather.itboy.net/api/weather/city/101010100" | ||
6533 | |||
6534 | ta = http.genericform(url) | ||
6535 | print(type(ta)) | ||
6536 | print("ta.utl =",ta.url) | ||
6537 | print("ta.sink =",ta.sink) | ||
6538 | print("ta.method =",ta.method) | ||
6539 | print("ta.heards =",ta.heards) | ||
6540 | print("ta.source =",ta.source) | ||
6541 | print("ta.step =",ta.step) | ||
6542 | print("ta.proxy =",ta.proxy) | ||
6543 | print("ta.redirect=",ta.redirect) | ||
6544 | print("ta.create =",ta.create) | ||
6545 | ------------------------------Output: | ||
6546 | --table | ||
6547 | --ta.utl = http://t.weather.itboy.net/api/weather/city/101010100 | ||
6548 | --ta.sink = function: 060405E0 | ||
6549 | --ta.method = nil | ||
6550 | --ta.heards = nil | ||
6551 | --ta.source = nil | ||
6552 | --ta.step = nil | ||
6553 | --ta.proxy = nil | ||
6554 | --ta.redirect= nil | ||
6555 | --ta.create = nil | ||
6556 | {{/code}} | ||
6557 | |||
6558 | = https module = | ||
6559 | |||
6560 | == [[https.request()>>||anchor="Hhttpsmodule"]] == | ||
6561 | |||
6562 | **Prototype 1: **https.request(url [, body]) | ||
6563 | |||
6564 | **Function: **https request fucntion | ||
6565 | |||
6566 | **Parameters:** | ||
6567 | |||
6568 | * url(string): Website address, uniform resource locator. | ||
6569 | * body: The requested data body | ||
6570 | |||
6571 | **Return: ** | ||
6572 | |||
6573 | |=Return value position|=Meaning | ||
6574 | |1|The returned data result | ||
6575 | |2|The access result code. 200 indicates that the connection is successful | ||
6576 | |3|The returned response header line | ||
6577 | |4|The returned response status | ||
6578 | |||
6579 | **~ Example** | ||
6580 | |||
6581 | {{code language="LUA"}} | ||
6582 | -- 1. Declare modules | ||
6583 | local https = require("socket.https") | ||
6584 | local LTN12= require("ltn12") | ||
6585 | |||
6586 | function test() | ||
6587 | print_debug(1) | ||
6588 | print("wait request . . . ") | ||
6589 | local res, code, headers, status = https.request{ | ||
6590 | url = "https://www.we-con.com.cn/" | ||
6591 | } | ||
6592 | |||
6593 | print("res=",res)--The returned data result | ||
6594 | print("code=",code)--The access result code. 200 indicates that the connection is successful | ||
6595 | for i,v in pairs(headers) do --The returned response header line | ||
6596 | print("i,v=",i,v) | ||
6597 | end | ||
6598 | print("state=",status)--The returned response status | ||
6599 | |||
6600 | we_bas_setstring("@W_HDW4600", status) | ||
6601 | end | ||
6602 | {{/code}} | ||
6603 | |||
6604 | **Prototype 2: ** http.**request{**url = //string//, | ||
6605 | [sink = //LTN12 sink//,] | ||
6606 | [method = //string//,] | ||
6607 | [headers = //header-table//,] | ||
6608 | [source = //LTN12 source//], | ||
6609 | [step = //LTN12 pump step//,] | ||
6610 | [proxy = //string//,] | ||
6611 | [redirect = //boolean//,] | ||
6612 | [create = //function//] | ||
6613 | **}** | ||
6614 | |||
6615 | **Function: **https request fucntion | ||
6616 | |||
6617 | **Parameters:** | ||
6618 | |||
6619 | * url ~-~- web address, uniform resource locator [mandatory parameter | ||
6620 | * sink ~-~- LTN12 receiver [optional parameter | ||
6621 | * method ~-~- the HTTP request method, default is "GET" [optional parameter | ||
6622 | * headers ~-~- any other HTTP headers sent with the request; [optional] [optional | ||
6623 | * source ~-~- A simple LTN12 source that provides the body of the request. If there is a body, the appropriate "content length" request header field needs to be provided, otherwise the function will attempt to send the body as a "chunk" (rarely supported by servers). Default is empty source; [optional parameter | ||
6624 | * step ~-~- Used to move the data, default is LTN12.pump.step function. [optional parameters] | ||
6625 | * proXY ~-~- the URL of the proxy server to use. Default is no proxy; [optional parameter | ||
6626 | * redirect ~-~- set to false to prevent the function from automatically following 301 or 302 server redirect messages; [optional parameter | ||
6627 | * create ~-~- optional function to use when creating communication sockets instead of socket.tcp. [optional parameter | ||
6628 | |||
6629 | **Return: ** | ||
6630 | |||
6631 | |=Return value position|=Meaning | ||
6632 | |1|The returned data result | ||
6633 | |2|The access result code. 200 indicates that the connection is successful | ||
6634 | |3|The returned response header line | ||
6635 | |4|The returned response status | ||
6636 | |||
6637 | **~ Example** | ||
6638 | |||
6639 | {{code language="LUA"}} | ||
6640 | -- 1、Declare modules | ||
6641 | local http = require("socket.https") | ||
6642 | local json = require("json") | ||
6643 | local LTN12= require("ltn12") | ||
6644 | |||
6645 | -- Case 2 | ||
6646 | function test2() | ||
6647 | print_debug(1) | ||
6648 | local url="https://open.ys7.com/api/lapp/device/info?accessToken=at.dunwhxt2azk02hcn7phqygsybbw0wv6p&deviceSerial=427734168" | ||
6649 | local Result = {} | ||
6650 | |||
6651 | local tabRequest = {} | ||
6652 | tabRequest.url = url | ||
6653 | tabRequest.method = "POST" | ||
6654 | tabRequest.sink = LTN12.sink.table(Result) | ||
6655 | |||
6656 | print("wait request . . . ") | ||
6657 | local res, code, headers, status = https.request(tabRequest) | ||
6658 | |||
6659 | print("Result =",table.concat(Result))--Returned data results | ||
6660 | print("res",res)--Returned response result | ||
6661 | print("code =", code)--Returned status code, 200 is OK | ||
6662 | for i,v in pairs(headers) do --Returned response message header | ||
6663 | print(i,v) | ||
6664 | end | ||
6665 | print("status=",status)--Returned response status | ||
6666 | end | ||
6667 | --Case 3 | ||
6668 | function test3() | ||
6669 | print_debug(1) | ||
6670 | print("wait request . . . ") | ||
6671 | local Result = {} | ||
6672 | |||
6673 | local res, code, headers, status = https.request{ | ||
6674 | url = "https://open.ys7.com/api/lapp/device/info?accessToken=at.dunwhxt2azk02hcn7phqygsybbw0wv6p&deviceSerial=427734168", | ||
6675 | method = "POST", | ||
6676 | sink = LTN12.sink.table(Result), | ||
6677 | } | ||
6678 | |||
6679 | print("Result =",table.concat(Result))--Returned data results | ||
6680 | print("res",res)--Returned response result | ||
6681 | print("code=",code)--Returned status code, 200 is OK | ||
6682 | for i,v in pairs(headers) do --Returned response message header | ||
6683 | print("i,v=",i,v) | ||
6684 | end | ||
6685 | print("state=",status)--Returned response status | ||
6686 | end | ||
6687 | {{/code}} | ||
6688 | |||
6689 | = SMTP module = | ||
6690 | |||
6691 | == [[smtp.message()>>||anchor="HSMTPmodule"]] == | ||
6692 | |||
6693 | **Prototype: **smtp.message(mesgt) | ||
6694 | |||
6695 | **Function: **Generate mail format messages | ||
6696 | |||
6697 | **Parameters: ** | ||
6698 | |||
6699 | * mesgt(table): Message format | ||
6700 | |||
6701 | {{code language="LUA"}} | ||
6702 | -- Message format: | ||
6703 | mesgt = { | ||
6704 | headers = header-table, -- message header | ||
6705 | body = LTN12 source or string or multipart-mesgt -- message text | ||
6706 | } | ||
6707 | |||
6708 | multipart-mesgt = { | ||
6709 | [preamble = string,] -- preface | ||
6710 | [1] = mesgt, -- text content 1 | ||
6711 | [2] = mesgt, -- text content 2 | ||
6712 | ... | ||
6713 | [n] = mesgt, -- text content n | ||
6714 | [epilogue = string,] -- Conclusion | ||
6715 | } | ||
6716 | {{/code}} | ||
6717 | |||
6718 | **Return: ** | ||
6719 | |||
6720 | * Returns a mail message source used to send messages | ||
6721 | |||
6722 | {{code language="LUA"}} | ||
6723 | source = smtp.message | ||
6724 | { | ||
6725 | headers = -- mail header | ||
6726 | { | ||
6727 | -- Note: smtp.send ignores headers | ||
6728 | from = "chenGH <chen__GH@163.com>", -- sender | ||
6729 | to = "chenGH <chengh@we-con.com.cn>", -- recipient | ||
6730 | cc = "<3388545257@qq.com>", -- cc | ||
6731 | subject = "Test the mail sending function of the LUa SMTP module!" -- title | ||
6732 | }, | ||
6733 | body = -- mail content | ||
6734 | { | ||
6735 | preamble = "Thanks for participating in the Lua SMTP mail test!", -- preface | ||
6736 | [1] = -- text 1(text) | ||
6737 | { | ||
6738 | -- The first part has no header, which means it is plain text, encoded in ASCII. | ||
6739 | -- mime.eol is an underlying function, and used to normalize trailing line break marks | ||
6740 | body = mime.eol(0, [[ | ||
6741 | Lines in the message body should always end in CRLF (carriage return line feed) | ||
6742 | The Smtp module does not process the message content。 The execution process is as follows | ||
6743 | A: Package the message content via smtp.message | ||
6744 | B: Package the message content via smtp.send and send the message.(Contains attachment data fill sections) | ||
6745 | ]]) | ||
6746 | }, | ||
6747 | [2] = -- text 2 (picture) | ||
6748 | { | ||
6749 | -- Part two: The title describes the content as a png image. | ||
6750 | -- Transfer content based on base64 encoding format | ||
6751 | -- Note: Nothing happens until the message is actually sent, and the data is assembled just after it is sent. | ||
6752 | headers = | ||
6753 | { | ||
6754 | -- Declare the type and name of the content of the second part of the message(type:image/png,name:image.png") | ||
6755 | ["content-type"] = 'image/png; name="image.png"', | ||
6756 | ["content-disposition"] = 'attachment; filename="image.png"', -- content description | ||
6757 | ["content-description"] = 'a beautiful image', -- mail description | ||
6758 | ["content-transfer-encoding"] = "BASE64" -- Mail encoding format | ||
6759 | }, | ||
6760 | body = LTN12.source.chain( -- Data source connection | ||
6761 | LTN12.source.file(io.open("user:smtp.png","rb")), -- Open a file as the data source | ||
6762 | LTN12.filter.chain( -- Encoding format is base64 | ||
6763 | mime.encode("base64"), | ||
6764 | mime.wrap() | ||
6765 | )) | ||
6766 | }, | ||
6767 | epilogue = "This might also show up, but after the attachments" -- Conclusion | ||
6768 | } | ||
6769 | } | ||
6770 | {{/code}} | ||
6771 | |||
6772 | == [[smtp.send()>>||anchor="HSMTPmodule"]] == | ||
6773 | |||
6774 | **Prototype : **smtp.**send{** | ||
6775 | from = //string//, | ||
6776 | rcpt = //string// or //string-table//, | ||
6777 | source = //LTN12 source//, | ||
6778 | [user = //string//,] | ||
6779 | [password = //string//,] | ||
6780 | [server = //string//,] | ||
6781 | [port = //number//,] | ||
6782 | [domain = //string//,] | ||
6783 | [step = //LTN12 pump step//,] | ||
6784 | [create = //function//] | ||
6785 | **}** | ||
6786 | |||
6787 | **Function: **send email | ||
6788 | |||
6789 | **Parameters:** | ||
6790 | |||
6791 | * from: sender | ||
6792 | * rcpt: recipient list | ||
6793 | * source: message source. The message after packaging | ||
6794 | * user: account. It is for mailbox authentication | ||
6795 | * password: password. It is for mailbox authenticatio. (base64 format) | ||
6796 | * server: smtp server address. e.g., the server of 163 mail is smtp.163.com; the server of QQ mail is smtp.qq.com. | ||
6797 | * port: smtp server port, default to 25. 163 Mailbox smtp port is 25, qq mailbox smtp server port number is 587/465. | ||
6798 | * domain: domain information. Default to "localhost". | ||
6799 | * step: Used to pass data from the source to the server. Defaults to the LTN12 ##pump.step## function. | ||
6800 | * create: Used to create socket connection. Default to socket.tcp(). | ||
6801 | |||
6802 | **Return: ** | ||
6803 | |||
6804 | * Success: Return 1. | ||
6805 | * Failure: Return nil and error. | ||
6806 | |||
6807 | **Note:** | ||
6808 | |||
6809 | 1. SMTP servers can be very picky with the format of e-mail addresses. To be safe, use only addresses of the form "##<fulano@example.com>##" in the from and rcpt arguments of the send function. In headers, e-mail addresses can take whatever form you like. | ||
6810 | 1. Only recipients specified in the rcpt list will receive a copy of the message. Each recipient of an SMTP mail message receives a copy of the message body along with the headers, and nothing more. The headers //are// part of the message and should be produced by the LTN12 source function. The rcpt list is //not// part of the message and will not be sent to anyone. | ||
6811 | |||
6812 | * To: Contains the address(es) of the primary recipient(s) of the message. | ||
6813 | * Cc: Contains the addresses of others who are to receive the message, though the content of the message may not be directed at them (where the "Cc" means "Carbon Copy" in the sense of making a copy on a typewriter using carbon paper). | ||
6814 | * Bcc: Contains addresses of recipients of the message whose addresses are not to be revealed to other recipients of the message (where the "Bcc" means "Blind Carbon Copy") | ||
6815 | |||
6816 | (% start="3" %) | ||
6817 | 1. ((( | ||
6818 | The LuaSocket send function does not care or interpret the headers you send, but it gives you full control over what is sent and to whom it is sent: | ||
6819 | ))) | ||
6820 | |||
6821 | * If someone is to receive the message, the e-mail address //has// to be in the recipient list. This is the only parameter that controls who gets a copy of the message. | ||
6822 | * If there are multiple recipients, none of them will automatically know that someone else got that message. That is, the default behavior is similar to the ##Bcc## field of popular e-mail clients; | ||
6823 | * It is up to you to add the To header with the list of primary recipients so that other recipients can see it. | ||
6824 | * It is also up to you to add the Cc header with the list of additional recipients so that everyone else sees it. | ||
6825 | * Adding a header Bcc is nonsense, unless it is empty. Otherwise, everyone receiving the message will see it and that is exactly what you //don't// want to happen. | ||
6826 | |||
6827 | **Example** | ||
6828 | |||
6829 | {{code language="LUA"}} | ||
6830 | local smtp = require("socket.smtp") | ||
6831 | local LTN12= require("ltn12") | ||
6832 | local mime = require("mime") | ||
6833 | from = "<chen__GH@163.com>" -- sender(Angle brackets must be added or some errors may occur) | ||
6834 | rcpt = { -- send list(Angle brackets must be added or some errors may occur) | ||
6835 | "<chengh@we-con.com.cn>", | ||
6836 | "<1125920709@qq.com>", | ||
6837 | "<3388545257@qq.com>" | ||
6838 | } | ||
6839 | |||
6840 | -- Create a sending source that is divided into two parts, one for plain text and the other for the PNG image。 | ||
6841 | source = smtp.message | ||
6842 | { | ||
6843 | headers = -- mail header | ||
6844 | { | ||
6845 | -- Note: smtp.send ignores headers | ||
6846 | from = "chenGH <chen__GH@163.com>", -- sender | ||
6847 | to = "chenGH <chengh@we-con.com.cn>", -- recipient | ||
6848 | cc = "<3388545257@qq.com>", -- CC | ||
6849 | subject = "Test the mail sending function of the LUa SMTP module!!" -- title | ||
6850 | }, | ||
6851 | body = -- mail text | ||
6852 | { | ||
6853 | preamble = "Thanks for participating in the Lua SMTP mail test!", -- preface | ||
6854 | [1] = -- text 1(plain text) | ||
6855 | { | ||
6856 | -- The first part has no header, which means it is plain text, encoded in ASCII. | ||
6857 | -- mime.eol is an underlying function, and used to normalize trailing line break marks | ||
6858 | body = mime.eol(0, [[ | ||
6859 | Lines in the message body should always end in CRLF (carriage return line feed) | ||
6860 | The Smtp module does not process the message content。 The execution process is as follows | ||
6861 | A: Package the message content via smtp.message | ||
6862 | B: Package the message content via smtp.send and send the message.(Contains attachment data fill sections) | ||
6863 | ]]) | ||
6864 | }, | ||
6865 | [2] = -- text 2 (picture) | ||
6866 | { | ||
6867 | -- Part two: The title describes the content as a png image. | ||
6868 | -- Transfer content based on base64 encoding format | ||
6869 | -- Note: Nothing happens until the message is actually sent, and the data is assembled just after it is sent. | ||
6870 | headers = | ||
6871 | { | ||
6872 | -- Declare the type and name of the content of the second part of the message(type:image/png,name:image.png") | ||
6873 | ["content-type"] = 'image/png; name="image.png"', | ||
6874 | -- content description | ||
6875 | ["content-disposition"] = 'attachment; filename="image.png"', | ||
6876 | -- mail description | ||
6877 | ["content-description"] = 'a beautiful image', | ||
6878 | -- Message encoding format | ||
6879 | ["content-transfer-encoding"] = "BASE64" | ||
6880 | }, | ||
6881 | body = LTN12.source.chain( -- Data source connection | ||
6882 | LTN12.source.file(io.open("user:smtp.png","rb")), -- Open a file as the data source | ||
6883 | LTN12.filter.chain( | ||
6884 | mime.encode("base64"), -- Encoding format is base64 | ||
6885 | mime.wrap() | ||
6886 | )) | ||
6887 | }, | ||
6888 | epilogue = "This might also show up, but after the attachments" -- Conclusion | ||
6889 | } | ||
6890 | } | ||
6891 | |||
6892 | -- send email | ||
6893 | r, e = assert(smtp.send{ | ||
6894 | from = from, -- mail sender | ||
6895 | rcpt = rcpt, -- Mail receiving list | ||
6896 | -- smtp server. e.g.,qq mail is smtp.qq.com; enterprise mail is smtp.qiye.com。 | ||
6897 | server = "smtp.163.com", | ||
6898 | user = "chen__GH@163.com", -- account | ||
6899 | password = "HUCOYKAUIKLSGZFJ", -- password(the password after conversion to base64) | ||
6900 | |||
6901 | |||
6902 | -- smtp server port,You need to look it up yourself;qq mailbox open 587/465, 163 mailbox port 25 | ||
6903 | --(25 is the smtp default port) | ||
6904 | -- port = 587, | ||
6905 | source = source, -- message source } ) | ||
6906 | print("end success!! ") | ||
6907 | {{/code}} | ||
6908 | |||
6909 | = LTN12 module = | ||
6910 | |||
6911 | == [[LTN12.filter.chain()>>||anchor="HLTN12module"]] == | ||
6912 | |||
6913 | **Prototype : **ltn12.filter.chain(filter1, filter2 [, ... filterN]) | ||
6914 | |||
6915 | **Function: **Generates a set of connected filters, and the nesting of filters can be arbitrary | ||
6916 | |||
6917 | **Parameters:** | ||
6918 | |||
6919 | * filter1 to filter N are simple filters | ||
6920 | |||
6921 | **Return: ** | ||
6922 | |||
6923 | * Returns the connected filter | ||
6924 | |||
6925 | == [[LTN12.filter.cycle()>>||anchor="HLTN12module"]] == | ||
6926 | |||
6927 | **Prototype : **ltn12.filter.cycle(low [, ctx, extra]) | ||
6928 | |||
6929 | **Function: **Returns a high-level filter that cycles though a low-level filter by passing it each chunk and updating a context between calls. | ||
6930 | |||
6931 | **Parameters:** | ||
6932 | |||
6933 | * low:The low-level filter to be cycled. | ||
6934 | * ctx:The initial context. | ||
6935 | * extra:Any extra argument the low-level filter might take. | ||
6936 | |||
6937 | **Return: ** | ||
6938 | |||
6939 | * Returns the high-level filter. | ||
6940 | |||
6941 | == [[LTN12.pump.all()>>||anchor="HLTN12module"]] == | ||
6942 | |||
6943 | **Prototype : **ltn12.pump.all(source, sink) | ||
6944 | |||
6945 | **Function: **Pumps all data from a source to a sink##.## | ||
6946 | |||
6947 | **Parameters:** | ||
6948 | |||
6949 | * source:data source | ||
6950 | * sink:sink | ||
6951 | |||
6952 | **Return: ** | ||
6953 | |||
6954 | * Success: Return true. | ||
6955 | * Failure: Return false and error. | ||
6956 | |||
6957 | == [[LTN12.pump.step()>>||anchor="HLTN12module"]] == | ||
6958 | |||
6959 | **Prototype : **ltn12.pump.step(source, sink) | ||
6960 | |||
6961 | **Function: **Pumps a large chunk of data from the source to the sink. | ||
6962 | |||
6963 | **Parameters:** | ||
6964 | |||
6965 | * source:source | ||
6966 | * sink:sink | ||
6967 | |||
6968 | **Return: ** | ||
6969 | |||
6970 | * Success: Return true. | ||
6971 | * Failure: Return false and error. | ||
6972 | |||
6973 | == [[LTN12.sink.chain()>>||anchor="HLTN12module"]] == | ||
6974 | |||
6975 | **Prototype : **ltn12.sink.chain(filter, sink) | ||
6976 | |||
6977 | **Function: **Creates a new sink that passes data through a filter before sending it to a given sink. | ||
6978 | |||
6979 | **Parameters:** | ||
6980 | |||
6981 | * source: filter | ||
6982 | * sink: sink | ||
6983 | |||
6984 | **Return: ** | ||
6985 | |||
6986 | * Return the created sink. | ||
6987 | |||
6988 | == [[LTN12.sink.error()>>||anchor="HLTN12module"]] == | ||
6989 | |||
6990 | **Prototype : **ltn12.sink.chain(filter, sink) | ||
6991 | |||
6992 | **Function: **Creates a new sink that passes data through a filter before sending it to a given sink. | ||
6993 | |||
6994 | **Parameters:** | ||
6995 | |||
6996 | * filter:filter | ||
6997 | * sink:sink | ||
6998 | |||
6999 | **Return: ** | ||
7000 | |||
7001 | * Returns the new sink that was created. | ||
7002 | |||
7003 | == [[LTN12.sink.file()>>||anchor="HLTN12module"]] == | ||
7004 | |||
7005 | **Prototype : **ltn12.sink.file(handle, message) | ||
7006 | |||
7007 | **Function: **Create a sink that sends data to a file | ||
7008 | |||
7009 | **Parameters:** | ||
7010 | |||
7011 | * handle: file handle | ||
7012 | * message: If ##handle## is ##**nil**##, ##message## should give the reason for failure | ||
7013 | |||
7014 | **Return: ** | ||
7015 | |||
7016 | * Returns the new sink that was created. | ||
7017 | |||
7018 | **Note:** | ||
7019 | |||
7020 | * The function returns a sink that sends all data to the given handleThe fu and closes the file when done, or a sink that aborts the transmission with the error message. | ||
7021 | * This function is used with io.open(): ltn12.sink.file(io.open("copy.png", "wb")) | ||
7022 | |||
7023 | == [[LTN12.sink.null()>>||anchor="HLTN12module"]] == | ||
7024 | |||
7025 | **Prototype : **ltn12.sink.null() | ||
7026 | |||
7027 | **Function: **Returns a sink that ignores all data it receives. | ||
7028 | |||
7029 | **Parameters: **None | ||
7030 | |||
7031 | **Return: ** | ||
7032 | |||
7033 | * Returns the new sink that was created. | ||
7034 | |||
7035 | == [[LTN12.sink.simplify()>>||anchor="HLTN12module"]] == | ||
7036 | |||
7037 | **Prototype : **ltn12.sink.simplify(sink) | ||
7038 | |||
7039 | **Function: **Creates and returns a simple sink given a fancy sink. | ||
7040 | |||
7041 | **Parameters:** | ||
7042 | |||
7043 | * sink:sink | ||
7044 | |||
7045 | **Return: ** | ||
7046 | |||
7047 | * Returns the new sink that was created. | ||
7048 | |||
7049 | == [[LTN12.sink.table()>>||anchor="HLTN12module"]] == | ||
7050 | |||
7051 | **Prototype : **ltn12.sink.table([table]) | ||
7052 | |||
7053 | **Function: **Creates a sink that stores all chunks in a table. The chunks can later be efficiently concatenated into a single string. | ||
7054 | |||
7055 | **Parameters:** | ||
7056 | |||
7057 | * table: Used to hold the chunks. If it is nil, the function creates its own table. | ||
7058 | |||
7059 | **Return: ** | ||
7060 | |||
7061 | * Returns the sink and the table used to store the chunks. | ||
7062 | |||
7063 | == [[LTN12.source.cat()>>||anchor="HLTN12module"]] == | ||
7064 | |||
7065 | **Prototype : **ltn12.source.cat(source1 [, source2, ..., sourceN]) | ||
7066 | |||
7067 | **Function: **Creates a new source that produces the concatenation of the data produced by a number of sources. | ||
7068 | |||
7069 | **Parameters:** | ||
7070 | |||
7071 | * source 1 to source n are the original sources. | ||
7072 | |||
7073 | **Return: ** | ||
7074 | |||
7075 | * Returns the new source created. | ||
7076 | |||
7077 | == [[LTN12.source.chain()>>||anchor="HLTN12module"]] == | ||
7078 | |||
7079 | **Prototype : **ltn12.source.chain(source, filter) | ||
7080 | |||
7081 | **Function: **Creates a new source that passes data through a filter before returning it. | ||
7082 | |||
7083 | **Parameters:** | ||
7084 | |||
7085 | * source: original source | ||
7086 | * filter: filter | ||
7087 | |||
7088 | **Return: ** | ||
7089 | |||
7090 | * Returns the new source created. | ||
7091 | |||
7092 | == [[LTN12.source.empty()>>||anchor="HLTN12module"]] == | ||
7093 | |||
7094 | **Prototype : **ltn12.source.empty() | ||
7095 | |||
7096 | **Function: **Creates and return a new source. | ||
7097 | |||
7098 | **Parameters: **None | ||
7099 | |||
7100 | **Return: ** | ||
7101 | |||
7102 | * Returns the new source created. | ||
7103 | |||
7104 | == [[LTN12.source.error()>>||anchor="HLTN12module"]] == | ||
7105 | |||
7106 | **Prototype : **ltn12.source.**error(message**) | ||
7107 | |||
7108 | **Function: **Creates and returns a source that aborts transmission with the error message. | ||
7109 | |||
7110 | **Parameters:** | ||
7111 | |||
7112 | * message: error message | ||
7113 | |||
7114 | **Return: ** | ||
7115 | |||
7116 | * Returns the new source created. | ||
7117 | |||
7118 | == [[LTN12.source.file()>>||anchor="HLTN12module"]] == | ||
7119 | |||
7120 | **Prototype : **ltn12.source.file(handle, message) | ||
7121 | |||
7122 | **Function: **Creates a source that produces the contents of a file. | ||
7123 | |||
7124 | **Parameters:** | ||
7125 | |||
7126 | * handle: file handle | ||
7127 | * message: If ##handle## is ##**nil**##, ##message## should give the reason for failure | ||
7128 | |||
7129 | **Return: ** | ||
7130 | |||
7131 | * Returns the new source created. | ||
7132 | |||
7133 | **Note:** | ||
7134 | |||
7135 | * The function returns a source that reads chunks of data from given handle and returns it to the user, closing the file when done, or a source that aborts the transmission with the error message. | ||
7136 | |||
7137 | == [[LTN12.source.simplify()>>||anchor="HLTN12module"]] == | ||
7138 | |||
7139 | **Prototype : **ltn12.source.simplify(source) | ||
7140 | |||
7141 | **Function: **Given a source, create and return a simple source. | ||
7142 | |||
7143 | **Parameters:** | ||
7144 | |||
7145 | * source: the given initial source | ||
7146 | |||
7147 | **Return: ** | ||
7148 | |||
7149 | * Returns the new source created. | ||
7150 | |||
7151 | == [[LTN12.source.string()>>||anchor="HLTN12module"]] == | ||
7152 | |||
7153 | **Prototype : **ltn12.source.string(string) | ||
7154 | |||
7155 | **Function: **Creates and returns a source that produces the contents of a string, chunk by chunk. | ||
7156 | |||
7157 | **Parameters:** | ||
7158 | |||
7159 | * string: string source | ||
7160 | |||
7161 | **Return: ** | ||
7162 | |||
7163 | * Returns the new source created. | ||
7164 | |||
7165 | == [[LTN12.source.table()>>||anchor="HLTN12module"]] == | ||
7166 | |||
7167 | **Prototype : **ltn12.source.table(table) | ||
7168 | |||
7169 | **Function: **Creates and returns a source that produces the numerically-indexed values of a ##table## successively beginning at 1. The source returns nil (end-of-stream) whenever a nil value is produced by the current index, which proceeds forward regardless. | ||
7170 | |||
7171 | **Parameters:** | ||
7172 | |||
7173 | * table: table data | ||
7174 | |||
7175 | **Return: ** | ||
7176 | |||
7177 | * Returns the new source created. | ||
7178 | |||
7179 | = MIME module = | ||
7180 | |||
7181 | == [[mime.decode()>>||anchor="HMIMEmodule"]] == | ||
7182 | |||
7183 | **Prototype : ** | ||
7184 | |||
7185 | * mime.decode("base64") | ||
7186 | * mime.decode("quoted-printable") | ||
7187 | |||
7188 | **Function: **Returns a filter that decodes data from a given transfer content encoding. | ||
7189 | |||
7190 | **Parameters:** | ||
7191 | |||
7192 | * The two encoding formats above. | ||
7193 | |||
7194 | **Return: ** | ||
7195 | |||
7196 | * Returns decoder filter. | ||
7197 | |||
7198 | == [[mime.encode()>>||anchor="HMIMEmodule"]] == | ||
7199 | |||
7200 | **Prototype : ** | ||
7201 | |||
7202 | * mime.encode("base64") | ||
7203 | * mime.encode("quoted-printable" [, mode]) | ||
7204 | |||
7205 | **Function: **Returns a filter that encodes data according to a given transfer content encoding. | ||
7206 | |||
7207 | **Parameters:** | ||
7208 | |||
7209 | * mode(string): User can specify whether the data is textual or binary, by passing the mode strings "text" or "binary". Mode defaults to "text". | ||
7210 | |||
7211 | **Return: ** | ||
7212 | |||
7213 | * Returns code filter. | ||
7214 | |||
7215 | **Note:** | ||
7216 | |||
7217 | * Although both transfer content encodings specify a limit for the line length, the encoding filters do not break text into lines (for added flexibility). Below is a filter that converts binary data to the Base64 transfer content encoding and breaks it into lines of the correct size. | ||
7218 | |||
7219 | == [[mime.normalize()>>||anchor="HMIMEmodule"]] == | ||
7220 | |||
7221 | **Prototype : **mime.normalize([marker]) | ||
7222 | |||
7223 | **Function: **Converts most common end-of-line markers to a specific given marker. | ||
7224 | |||
7225 | **Parameters:** | ||
7226 | |||
7227 | * marker(string): The new marker. It defaults to CRLF. This is the standard end-of-line tag defined by MIME standards. | ||
7228 | |||
7229 | **Return: ** | ||
7230 | |||
7231 | * returns a filter that performs the conversion | ||
7232 | |||
7233 | == [[mime.stuff()>>||anchor="HMIMEmodule"]] == | ||
7234 | |||
7235 | **Prototype : **mime.stuff() | ||
7236 | |||
7237 | **Function: **Creates and returns a filter that performs stuffing of SMTP messages. | ||
7238 | |||
7239 | **Parameters**: None | ||
7240 | |||
7241 | **Return: ** | ||
7242 | |||
7243 | * Returns a filter that performs stuffing of SMTP messages. | ||
7244 | |||
7245 | **Note: ** | ||
7246 | |||
7247 | * The smtp.send function uses this filter automatically. You don't need to chain it with your source, or apply it to your message body. | ||
7248 | |||
7249 | == [[mime.wrap()>>||anchor="HMIMEmodule"]] == | ||
7250 | |||
7251 | **Prototype : ** | ||
7252 | |||
7253 | * mime.wrap("text" [, length]) | ||
7254 | * mime.wrap("base64") | ||
7255 | * mime.wrap("quoted-printable") | ||
7256 | |||
7257 | **Function: **Returns a filter that breaks data into lines. | ||
7258 | |||
7259 | **Parameters**: | ||
7260 | |||
7261 | * length(number): | ||
7262 | |||
7263 | **Return: ** | ||
7264 | |||
7265 | * Returns a filter that performs stuffing of SMTP messages. | ||
7266 | |||
7267 | **Note: ** | ||
7268 | |||
7269 | * The smtp.send function uses this filter automatically. You don't need to chain it with your source, or apply it to your message body. | ||
7270 | |||
7271 | == [[mime.b64()>>||anchor="HMIMEmodule"]] == | ||
7272 | |||
7273 | **Prototype : ** A, B = mime.b64(C [, D]) | ||
7274 | |||
7275 | **Function: **Low-level filter to perform Base64 encoding. | ||
7276 | |||
7277 | **Parameters**: | ||
7278 | |||
7279 | * C: String that needs to be encoded. | ||
7280 | * D: String 2 that needs to be encoded. | ||
7281 | |||
7282 | **Return: ** | ||
7283 | |||
7284 | * ##A## is the encoded version of the largest prefix of ##C..D## that can be encoded unambiguously. B has the remaining bytes of ##C..D##, //before// encoding. If ##D## is ##**nil**##, ##A## is padded with the encoding of the remaining bytes of ##C##. | ||
7285 | |||
7286 | **Note: ** | ||
7287 | |||
7288 | * The simplest use of this function is to encode a string into it's Base64 transfer content encoding. Notice the extra parenthesis around the call to ##mime.b64 ##to discard the second return value. | ||
7289 | |||
7290 | **Example** | ||
7291 | |||
7292 | {{code language="LUA"}} | ||
7293 | print((mime.b64("diego:password"))) | ||
7294 | --> ZGllZ286cGFzc3dvcmQ= | ||
7295 | {{/code}} | ||
7296 | |||
7297 | == [[mime.dot()>>||anchor="HMIMEmodule"]] == | ||
7298 | |||
7299 | **Prototype : **A, n = mime.dot(m [, B]) | ||
7300 | |||
7301 | **Function: **Low-level filter to perform SMTP stuffing and enable transmission of messages containing the sequence "CRLF.CRLF". | ||
7302 | |||
7303 | **Parameters**: | ||
7304 | |||
7305 | * m: The data fast value used to determine whether the text concatenation belongs to the same block. | ||
7306 | * B: Data that needs to be populated. | ||
7307 | |||
7308 | **Return: ** | ||
7309 | |||
7310 | * ##A## is the stuffed version of ##B##. '##n##' gives the number of characters from the sequence CRLF seen in the end of ##B##. '##m##' should tell the same, but for the previous chunk. | ||
7311 | |||
7312 | **Note: ** | ||
7313 | |||
7314 | * The message body is defined to begin with an implicit CRLF. Therefore, to stuff a message correctly, the first ##m## should have the value 2. | ||
7315 | |||
7316 | **Example** | ||
7317 | |||
7318 | {{code language="LUA"}} | ||
7319 | print((string.gsub(mime.dot(2, ".\r\nStuffing the message.\r\n.\r\n."), "\r\n", "\\n"))) | ||
7320 | --> ..\nStuffing the message.\n..\n.. | ||
7321 | {{/code}} | ||
7322 | |||
7323 | == [[mime.eol()>>||anchor="HMIMEmodule"]] == | ||
7324 | |||
7325 | **Prototype : **A, B = mime.eol(C [, D, marker]) | ||
7326 | |||
7327 | **Function: **Low-level filter to perform end-of-line marker translation. | ||
7328 | |||
7329 | **Parameters**: | ||
7330 | |||
7331 | * C: C is the ASCII value of the last character of the previous chunk, if it was a candidate for line break, or 0 otherwise. | ||
7332 | * D: D Is the block data that needs to be translated | ||
7333 | * marker: the new end-of-line marker and defaults to CRLF. | ||
7334 | |||
7335 | **Return: ** | ||
7336 | |||
7337 | * ##A:T##he translated version of ##D##. | ||
7338 | * ##B## is the same as ##C##, but for the current chunk. | ||
7339 | |||
7340 | **Example** | ||
7341 | |||
7342 | {{code language="LUA"}} | ||
7343 | -- translates the end-of-line marker to UNIX | ||
7344 | unix = mime.eol(0, dos, "\n") | ||
7345 | {{/code}} | ||
7346 | |||
7347 | == [[mime.qp()>>||anchor="HMIMEmodule"]] == | ||
7348 | |||
7349 | **Prototype : **A, B = mime.qp(C [, D, marker]) | ||
7350 | |||
7351 | **Function: **Low-level filter to perform Quoted-Printable encoding. | ||
7352 | |||
7353 | **Parameters**: | ||
7354 | |||
7355 | * ##A## is the encoded version of the largest prefix of ##C..D## that can be encoded unambiguously. ##B## has the remaining bytes of ##C..D##, //before// encoding. If ##D## is ##**nil**##, ##A## is padded with the encoding of the remaining bytes of ##C##. Throughout encoding, occurrences of CRLF are replaced by the ##marker##, which itself defaults to CRLF. | ||
7356 | |||
7357 | **Return: None** | ||
7358 | |||
7359 | **Note: ** | ||
7360 | |||
7361 | * The simplest use of this function is to encode a string into it's Quoted-Printable transfer content encoding. Notice the extra parenthesis around the call to ##mime.qp##, to discard the second return value. | ||
7362 | |||
7363 | == [[mime.qpwrp()>>||anchor="HMIMEmodule"]] == | ||
7364 | |||
7365 | **Prototype : **A, m = mime.qpwrp(n [, B, length]) | ||
7366 | |||
7367 | **Function: **Low-level filter to break Quoted-Printable text into lines. | ||
7368 | |||
7369 | **Parameters**: | ||
7370 | |||
7371 | * n: The number of bytes remaining in the first line | ||
7372 | * B: Data block | ||
7373 | * length: The length of the line, defaults to 76. | ||
7374 | |||
7375 | **Return: ** | ||
7376 | |||
7377 | * ##A:## a copy of ##B.## | ||
7378 | * ##m: ##Returns the number of bytes left in the last line of ##A##. | ||
7379 | |||
7380 | **Note: ** | ||
7381 | |||
7382 | * Besides breaking text into lines, this function makes sure the line breaks don't fall in the middle of an escaped character combination. Also, this function only breaks lines that are bigger than ##length## bytes. | ||
7383 | |||
7384 | == [[mime.unb64()>>||anchor="HMIMEmodule"]] == | ||
7385 | |||
7386 | **Prototype : **A, B = mime.unb64(C [, D]) | ||
7387 | |||
7388 | **Function: **Low-level filter to perform Base64 decoding. | ||
7389 | |||
7390 | **Parameters**: | ||
7391 | |||
7392 | * C: Data block 1 | ||
7393 | * D: Data block 2 | ||
7394 | |||
7395 | **Return: ** | ||
7396 | |||
7397 | * ##A## is the decoded version of the largest prefix of ##C..D## that can be decoded unambiguously. ##B## has the remaining bytes of ##C..D##, //before// decoding. If ##D## is ##**nil**##, ##A## is the empty string and ##B## returns whatever couldn't be decoded. | ||
7398 | |||
7399 | **Note: ** | ||
7400 | |||
7401 | * The simplest use of this function is to decode a string from it's Base64 transfer content encoding. Notice the extra parenthesis around the call to ##mime.unqp##, to discard the second return value. | ||
7402 | |||
7403 | **Example** | ||
7404 | |||
7405 | {{code language="LUA"}} | ||
7406 | print((mime.unb64("ZGllZ286cGFzc3dvcmQ="))) | ||
7407 | --> diego:password | ||
7408 | {{/code}} | ||
7409 | |||
7410 | == [[mime.unqp()>>||anchor="HMIMEmodule"]] == | ||
7411 | |||
7412 | **Prototype : **A, B = mime.unqp(C [, D]) | ||
7413 | |||
7414 | **Function: **Low-level filter to remove the Quoted-Printable transfer content encoding from data. | ||
7415 | |||
7416 | **Parameters**: | ||
7417 | |||
7418 | * C: Code block 1 | ||
7419 | * D: Code block 2 | ||
7420 | |||
7421 | **Return: ** | ||
7422 | |||
7423 | * ##A## is the decoded version of the largest prefix of ##C..D## that can be decoded unambiguously. ##B## has the remaining bytes of ##C..D##, //before// decoding. If ##D## is ##**nil**##, ##A## is augmented with the encoding of the remaining bytes of ##C##. | ||
7424 | |||
7425 | **Note: ** | ||
7426 | |||
7427 | * The simplest use of this function is to decode a string from it's Quoted-Printable transfer content encoding. Notice the extra parenthesis around the call to ##mime.unqp##, to discard the second return value. | ||
7428 | |||
7429 | **Example** | ||
7430 | |||
7431 | {{code language="LUA"}} | ||
7432 | print((mime.qp("ma=E7=E3="))) | ||
7433 | --> ma?? | ||
7434 | {{/code}} | ||
7435 | |||
7436 | == [[mime.wrp()>>||anchor="HMIMEmodule"]] == | ||
7437 | |||
7438 | **Prototype : **A, m = mime.wrp(n [, B, length]) | ||
7439 | |||
7440 | **Function: **Create a low-level filter that uses CRLF tags to split text into several lines. | ||
7441 | |||
7442 | **Parameters**: | ||
7443 | |||
7444 | * n: The number of bytes remaining in the first line | ||
7445 | * B: Code block 2 | ||
7446 | * length: The length of the line, defaults to 76. | ||
7447 | |||
7448 | **Return: ** | ||
7449 | |||
7450 | * ##A## is a copy of ##B##, broken into lines of at most ##length## bytes (defaults to 76). '##n##' should tell how many bytes are left for the first line of ##B## and '##m##' returns the number of bytes left in the last line of ##A##. | ||
7451 | |||
7452 | **Note: ** | ||
7453 | |||
7454 | * This function only breaks lines that are bigger than ##length## bytes. The resulting line length does not include the CRLF marker. | ||
7455 | |||
7456 | = Appendix = | ||
7457 | |||
7458 | == ftp command list == | ||
7459 | |||
7460 | **Access commands** | ||
7461 | |||
7462 | |=Command|=Format|=Instruction | ||
7463 | |USER|USER|Specifies the login user name for authentication. | ||
7464 | |PASS|PASS|Specifies the user password, which must be followed by the login user name command | ||
7465 | |REIN|REIN|Reinitialize the user information. This command terminates the transfer of the current USER and the data being transferred, and then resets all parameters and enables the control connection so that the USER command occurs again on the client. | ||
7466 | |QUIT|QUIT|Disable the connection to the server | ||
7467 | |||
7468 | **Mode setting command** | ||
7469 | |||
7470 | |=Command|=Format|=Instruction | ||
7471 | |PASV|PASV|This command tells the FTP server to listen on the specified data port and passively accept requests from clients. If no mode is specified, the FTP server uses PASV mode by default. | ||
7472 | |PORT|PORT|This command tells the FTP server that the port number monitored by the client is address, and enables the FTP server to connect to the client in active mode. | ||
7473 | |TYPE|TYPE|This command specifies the data type to be transferred: ASCII and BINARY. | ||
7474 | |MODE|MODE|The command specifies the transmission mode. S denotes stream, B denotes block, and C denotes compression. | ||
7475 | |||
7476 | **File manegement command** | ||
7477 | |||
7478 | |=Command|=Format|=Instruction | ||
7479 | |CWD|CWD|Change the working directory; This command allows users to work in different directories or data sets without changing their login information. Directory is usually a directory name or a collection of files related to the system. | ||
7480 | |PWD|PDW|Returns the current working directory. | ||
7481 | |MKD|MKD|Creates a new directory in the specified path. Directory is a string for a specific directory. | ||
7482 | |CDUP|CDUP|Returns to the upper-layer directory. | ||
7483 | |RMD|RMD|Deletes a specified directory. Directory is a string representing a specific directory. | ||
7484 | |LIST|LIST|Returns a list of subdirectories and files under the specified path. Name is the path. If the path is omitted, the list of files in the current path is returned. | ||
7485 | |NLST|NLST|Returns a list of directories under the specified path. If the path is omitted, the current directory is returned. | ||
7486 | |RNFR|RNFR|Renames the file, and the next command in this command specifies the new file name with RNTO. | ||
7487 | |RNTO|RNTO|This command works with the RNFR command to rename a file. | ||
7488 | |DELE|DELE|Used to delete files in the specified path. | ||
7489 | |||
7490 | **File tranfer command** | ||
7491 | |||
7492 | |=Command|=Format|=Instruction | ||
7493 | |RETR|RETR|Downloads the file in the specified path | ||
7494 | |STOR|STOR|Uploads a specified file and stores it in the specified location. If the file already exists, the original file will be overwritten. If the file does not exist, a new file will be created. | ||
7495 | |SYST|SYST|Returns to the operating system used by the server | ||
7496 | |||
7497 | == ftp common response code == | ||
7498 | |||
7499 | |=((( | ||
7500 | Response Code | ||
7501 | )))|=Meaning | ||
7502 | |110|Restart the token repl. In this case the text is deterministic, it must be MARK yyyy=mmmm. YYYY is the user process data flow marker and MMMM is the server marker. | ||
7503 | |120|Service is ready in N minutes | ||
7504 | |125|Data connection open, ready for transfer | ||
7505 | |150|The file is in good condition and will open a data connection | ||
7506 | |200|Command successful | ||
7507 | |202|Command not executed | ||
7508 | |211|System status or system help response | ||
7509 | |212|Directory status reply | ||
7510 | |213|File status reply | ||
7511 | |214|Help message Reply | ||
7512 | |215|System type reply | ||
7513 | |220|Service is ready | ||
7514 | |221|The service closes the control connection and can log out | ||
7515 | |225|Data connection open, no transfer in progress | ||
7516 | |226|Closed data connection. Requested file operation succeeded | ||
7517 | |227|In passive mode | ||
7518 | |230|User logged in | ||
7519 | |250|Requested file action completed | ||
7520 | |257|Create pathname | ||
7521 | |331|Correct user name, password required | ||
7522 | |332|Account information is required for login | ||
7523 | |350|The requested file operation requires further command | ||
7524 | |421|Unable to provide service, close control connection | ||
7525 | |425|Unable to open data link | ||
7526 | |426|Close the connection and terminate the transmission | ||
7527 | |450|Unavailable file | ||
7528 | |451|Requested action aborted: local error | ||
7529 | |452|Requested action not performed: Insufficient system storage space | ||
7530 | |500|Invalid command | ||
7531 | |501|syntax error | ||
7532 | |502|The command was not executed | ||
7533 | |503|Command order error | ||
7534 | |504|Invalid command parameter | ||
7535 | |530|Not logged in | ||
7536 | |532|Account information is required to store files | ||
7537 | |550|Requested action not performed | ||
7538 | |551|Request action aborted: unknown page type | ||
7539 | |552|Requested file action aborted: storage allocation overflowed | ||
7540 | |553|Requested action not performed: filename is invalid | ||
7541 | |||
7542 | = LuaSqlite module = | ||
7543 | |||
7544 | **Introduction** | ||
7545 | |||
7546 | * Luasqlite is a third-party library of Lua that provides lua operations on sqLite databases. | ||
7547 | * Luasqlite is a built-in module that can be used without reference. Module name is luasql_sqlite3. | ||
7548 | * It is roughly divided into three parts: SQLite environment Settings, database file operations, database file cursor operations. | ||
7549 | * Since sqLite's extensive read and write operations shorten Flash life, database files can only be created on Udisk or SD cards. | ||
7550 | |||
7551 | **Get sqlite environment** | ||
7552 | |||
7553 | {{{env = luasql_sqlite3.sqlite3() | ||
7554 | }}} | ||
7555 | |||
7556 | **sqlite environment operation** | ||
7557 | |||
7558 | {{code language="LUA"}} | ||
7559 | -- sqlite environment | ||
7560 | env = luasql_sqlite3.sqlite3() | ||
7561 | {{/code}} | ||
7562 | |||
7563 | == **db file operation** == | ||
7564 | |||
7565 | {{code language="LUA"}} | ||
7566 | -- Get objects in database files | ||
7567 | |||
7568 | env = luasql_sqlite3.sqlite3()-- Initialize the environment before obtaining the db file object | ||
7569 | |||
7570 | db = env:connect("udisk:test.db")-- Connect a db file | ||
7571 | {{/code}} | ||
7572 | |||
7573 | |=Function|=Introduction | ||
7574 | |[[db:close()>>||anchor="Hdb:close2829"]]|Close the database file | ||
7575 | |[[db:escape()>>||anchor="Hdb:escape2829"]]|Escape encoding of string data | ||
7576 | |[[db:execute()>>||anchor="Hdb:execute2829"]]|Execute the sql statement | ||
7577 | |[[db:commit()>>||anchor="Hdb:commit2829"]]|Commit the data to the database | ||
7578 | |[[db:rollback()>>||anchor="Hdb:rollback2829"]]|Roll back operations on the database before committing | ||
7579 | |[[db:setautocommit()>>||anchor="Hdb:setautocommit2829"]]|Set the automatic submission function | ||
7580 | |[[db:getlastautoid()>>||anchor="Hdb:getlastautoid2829"]]|Get the total number of data records for the current database +1 | ||
7581 | |||
7582 | == **Cursor operation** == | ||
7583 | |||
7584 | {{code language="LUA"}} | ||
7585 | -- Get cursor object | ||
7586 | env = luasql_sqlite3.sqlite3() -- Initialize the environment before obtaining the db file object | ||
7587 | db = env:connect("udisk:test.db") -- Connect a db file | ||
7588 | |||
7589 | cursor = db:execute([[select * from students]]) | ||
7590 | {{/code}} | ||
7591 | |||
7592 | |=Function|=Introduction | ||
7593 | |[[cursor:close()>>||anchor="Hcursor:close2829"]]|Close the cursor | ||
7594 | |[[cursor:getcolnames()>>||anchor="Hcursor:getcolnames2829"]]|Get the name of the header | ||
7595 | |[[cursor:getcoltypes()>>||anchor="Hcursor:getcoltypes2829"]]|Get the type of the header | ||
7596 | |[[cursor:fetch()>>||anchor="Hcursor:fetch2829"]]|Similar to iterators, facilitate row nodes | ||
7597 | |||
7598 | = LuaSecure = | ||
7599 | |||
7600 | **Description** | ||
7601 | |||
7602 | This module provides common data verification algorithm function. | ||
7603 | |||
7604 | The name of the library is: secure, crc | ||
7605 | |||
7606 | **List** | ||
7607 | |||
7608 | |**Function**|**Features** | ||
7609 | |[[secure.md5string>>path:mk:@MSITStore:D:\WV%20Studio20230801\HmiHelp.chm::/leviⅡ/6/6.4/4.3LuaSecure.html#func1]]|Verify the string with md5 algorithm | ||
7610 | |[[secure.md5file>>path:mk:@MSITStore:D:\WV%20Studio20230801\HmiHelp.chm::/leviⅡ/6/6.4/4.3LuaSecure.html#func2]]|Verify the file with md5 algorithm | ||
7611 | |[[secure.sha>>path:mk:@MSITStore:D:\WV%20Studio20230801\HmiHelp.chm::/leviⅡ/6/6.4/4.3LuaSecure.html#func3]]|Verify the string with sha algorithm | ||
7612 | |[[secure.checksum>>path:mk:@MSITStore:D:\WV%20Studio20230801\HmiHelp.chm::/leviⅡ/6/6.4/4.3LuaSecure.html#func4]]|verify string with 8-bit checksum algorithm | ||
7613 | |[[secure.xor>>path:mk:@MSITStore:D:\WV%20Studio20230801\HmiHelp.chm::/leviⅡ/6/6.4/4.3LuaSecure.html#func5]]|verify string with 8-bit XOR checksum algorithm | ||
7614 | |[[crc.init>>path:mk:@MSITStore:D:\WV%20Studio20230801\HmiHelp.chm::/leviⅡ/6/6.4/4.3LuaSecure.html#func6]]|Initialization of luacrc checksum algorithm | ||
7615 | |[[crc.calc>>path:mk:@MSITStore:D:\WV%20Studio20230801\HmiHelp.chm::/leviⅡ/6/6.4/4.3LuaSecure.html#func7]]|Verify the string with crc algorithm | ||
7616 | |||
7617 | === **secure.md5string** === | ||
7618 | |||
7619 | Prototype: md5string (data) | ||
7620 | |||
7621 | Function: Verify the string with md5 algorithm | ||
7622 | |||
7623 | Parameter: | ||
7624 | |||
7625 | * data (string): Data | ||
7626 | |||
7627 | Return: | ||
7628 | |||
7629 | * Success: Return 32-bit md5 encrypted data | ||
7630 | * Failure: nil | ||
7631 | |||
7632 | Example: | ||
7633 | |||
7634 | {{code}} | ||
7635 | print (secure.md5string ("test")) | ||
7636 | {{/code}} | ||
7637 | |||
7638 | **✎Note: When using MD5 encryption, the letters in the string to be encrypted need to be case-sensitive, but the contents of the ciphertext need not be case-sensitive.** | ||
7639 | |||
7640 | === **secure.md5file** === | ||
7641 | |||
7642 | Prototype: md5file (path) | ||
7643 | |||
7644 | Function: Verify the file with md5 algorithm | ||
7645 | |||
7646 | Parameter: | ||
7647 | |||
7648 | * Path (string): File path | ||
7649 | |||
7650 | Return: | ||
7651 | |||
7652 | * Success: Return 32-bit file encrypted data | ||
7653 | * Failure: nil | ||
7654 | |||
7655 | Example: | ||
7656 | |||
7657 | {{code}} | ||
7658 | print(secure.md5file("udisk:test.txt")) | ||
7659 | {{/code}} | ||
7660 | |||
7661 | === **secure.sha** === | ||
7662 | |||
7663 | Prototype: sha (data, length) | ||
7664 | |||
7665 | * data (string): Data | ||
7666 | |||
7667 | Function: Encrypt strings with sha algorithm | ||
7668 | |||
7669 | Parameter: | ||
7670 | |||
7671 | * Length (number): Output data length | ||
7672 | * Supported length: 160,256,384,512 (only these 4 fixed lengths are supported, and cannot be customized) | ||
7673 | |||
7674 | Return: | ||
7675 | |||
7676 | * Success: Corresponding length sha encrypted data | ||
7677 | * Failure: nil | ||
7678 | |||
7679 | Example: | ||
7680 | |||
7681 | {{code}} | ||
7682 | print (secure.sha ("test", 160)) | ||
7683 | {{/code}} | ||
7684 | |||
7685 | === **secure.checksum** === | ||
7686 | |||
7687 | Prototype: checksum (data, length) | ||
7688 | |||
7689 | Function: 16-bit checksum algorithm for string | ||
7690 | |||
7691 | Parameter: | ||
7692 | |||
7693 | * data (string): Data | ||
7694 | * Length (number): Data length | ||
7695 | |||
7696 | Return: | ||
7697 | |||
7698 | * Success: Return 16-bit checksum encrypted data | ||
7699 | * Failure: nil | ||
7700 | |||
7701 | Example: | ||
7702 | |||
7703 | {{code}} | ||
7704 | print (secure.checksum ("test", 4)) | ||
7705 | {{/code}} | ||
7706 | |||
7707 | === **secure.xor** === | ||
7708 | |||
7709 | Prototype: xor (data, length) | ||
7710 | |||
7711 | Function: 16-bit XOR checksum algorithm for string | ||
7712 | |||
7713 | Parameter: | ||
7714 | |||
7715 | * data (string): Data | ||
7716 | * Length (number): Data length | ||
7717 | |||
7718 | Return: | ||
7719 | |||
7720 | * Success: Return 16-bit xor encrypted data | ||
7721 | * Failure: nil | ||
7722 | |||
7723 | Example: | ||
7724 | |||
7725 | {{code}} | ||
7726 | print (secure.xor ("test", 4)) | ||
7727 | {{/code}} | ||
7728 | |||
7729 | === **crc.init** === | ||
7730 | |||
7731 | Prototype: crc.init (parameter) | ||
7732 | |||
7733 | Function: Initialize crc check algorithm | ||
7734 | |||
7735 | Parameter: | ||
7736 | |||
7737 | * parameter (table): Parameter list, the required parameters are as follows: | ||
7738 | ** name (string): Parameter model name (Example: crc8, crc16_arc, crc32, etc.) | ||
7739 | ** width (number): Width, that is, the number of CRC bits. | ||
7740 | ** poly (number) (Hexadecimal): Abbreviations for generated items, expressed in hexadecimal. | ||
7741 | ** init (number) (Hexadecimal): This is the initialization preset value of the register (crc) when the algorithm starts, which is expressed in hexadecimal. | ||
7742 | ** xorout (number) (Hexadecimal): The final CRC value is obtained after the calculation result is exclusive or with this parameter. | ||
7743 | ** refin (number): Whether each byte of the data under test is inverted bitwise. 1 is true and 0 is false. | ||
7744 | ** refout (number): Whether the whole data is inverted bitwise after calculation and before XOR output, 1 is true and 0 is false. | ||
7745 | |||
7746 | Return: | ||
7747 | |||
7748 | * Success: crc object | ||
7749 | * Failure: nil | ||
7750 | |||
7751 | === **crc.calc** === | ||
7752 | |||
7753 | Prototype: crc.calc (lcrc, data) | ||
7754 | |||
7755 | Function: Calculate the results after crc , which should be used in conjunction with crc.init. | ||
7756 | |||
7757 | Parameter: | ||
7758 | |||
7759 | * lcrc: crc object | ||
7760 | * data (string): Data | ||
7761 | |||
7762 | Return: | ||
7763 | |||
7764 | * Success: Return crc encrypted data | ||
7765 | * Failure: nil | ||
7766 | |||
7767 | Example: | ||
7768 | |||
7769 | {{code}} | ||
7770 | local crc = require("crc")local parameter={name="crc32",width=32,poly=0x04C11DB7,init=FFFFFFFF,xorout=FFFFFFFF,refin=1,refout=1} | ||
7771 | lcrc = crc.init(parameter)if lcrc ~= nil | ||
7772 | result,err= lcrc.calc(lcrc,"123456789")print(result) | ||
7773 | |||
7774 | {{/code}} | ||
7775 | |||
7776 | **Appendix** | ||
7777 | |||
7778 | Some common crc initialization parameters and their output results. | ||
7779 | |||
7780 | |**Algorithm**|**Poly**|**Init**|**XorOut**|**RefIn**|**RefOut**|**Input**|**Result** | ||
7781 | |crc8|0x07|0x00|0x00|false|false|123456789|0xF4 | ||
7782 | |crc16_arc|0x8005|0x0000|0x0000|true|true|123456789|0xBB3D | ||
7783 | |crc32|0x04C11DB7|0xFFFFFFF|0xFFFFFFF|true|true|123456789|0xCBF43926 | ||
7784 | |||
7785 | = Environment operation = | ||
7786 | |||
7787 | (% class="wikigeneratedid" id="HGetsqliteenvironment" %) | ||
7788 | **Get sqlite environment** | ||
7789 | |||
7790 | {{code language="LUA"}} | ||
7791 | sql = require "Luasqlite" -- Introducing the lua libraries | ||
7792 | env = sql.sqlite3() -- Get the environment class | ||
7793 | {{/code}} | ||
7794 | |||
7795 | == env:connect() == | ||
7796 | |||
7797 | **Prototype : **env:connect(dbFilePath) | ||
7798 | |||
7799 | **Function: **Connect to a DB file.(If the file does not exist) | ||
7800 | |||
7801 | **Parameters**: | ||
7802 | |||
7803 | * dbFilePath (string): Full path to the DB file to connect to (create) | ||
7804 | |||
7805 | **Return: ** | ||
7806 | |||
7807 | * Success: database object | ||
7808 | * Failure: Returns nil and error. | ||
7809 | |||
7810 | **Example** | ||
7811 | |||
7812 | {{code language="LUA"}} | ||
7813 | env = luasql_sqlite3.sqlite3()-- Get the environment class | ||
7814 | db = env:connect("udisk:test.db") -- Connect to a DB file | ||
7815 | db:close()-- Close the db Connection | ||
7816 | env:close()-- Close the database environment | ||
7817 | {{/code}} | ||
7818 | |||
7819 | == env:close() == | ||
7820 | |||
7821 | **Prototype : **env:close() | ||
7822 | |||
7823 | **Function: **Close the environment | ||
7824 | |||
7825 | **Parameters**: None | ||
7826 | |||
7827 | **Return: **None | ||
7828 | |||
7829 | **Example** | ||
7830 | |||
7831 | {{code language="LUA"}} | ||
7832 | env = luasql_sqlite3.sqlite3()-- Get the environment class | ||
7833 | db = env:connect("udisk:test.db") -- Connect to a DB file | ||
7834 | db:close()-- Close the db Connection | ||
7835 | env:close()-- Close the database environment | ||
7836 | {{/code}} | ||
7837 | |||
7838 | = db file operation = | ||
7839 | |||
7840 | == [[db:close()>>||anchor="Hdbfileoperation"]] == | ||
7841 | |||
7842 | **Prototype : **db:close() | ||
7843 | |||
7844 | **Function: **Close connection | ||
7845 | |||
7846 | **Parameters**: None | ||
7847 | |||
7848 | **Return: **None | ||
7849 | |||
7850 | **Example** | ||
7851 | |||
7852 | {{code language="LUA"}} | ||
7853 | env = luasql_sqlite3.sqlite3()-- Get the environment class | ||
7854 | db = env:connect("udisk:test.db") -- Connect to a DB file | ||
7855 | db:close()-- Close the db Connection | ||
7856 | env:close()-- Close the database environment | ||
7857 | {{/code}} | ||
7858 | |||
7859 | == [[db:escape()>>||anchor="Hdbfileoperation"]] == | ||
7860 | |||
7861 | **Prototype : **db:escape(szValue) | ||
7862 | |||
7863 | **Function: **Connect to a DB file.(If the file does not exist) | ||
7864 | |||
7865 | **Parameters**: | ||
7866 | |||
7867 | * szValue(string): The string that needs to be encoded | ||
7868 | |||
7869 | **Return: ** | ||
7870 | |||
7871 | * string: Converted string | ||
7872 | |||
7873 | **Example** | ||
7874 | |||
7875 | {{code language="LUA"}} | ||
7876 | local env = luasql_sqlite3.sqlite3() -- Get the environment class | ||
7877 | local db = env:connect("udisk:test.db") -- Connect to a DB file | ||
7878 | |||
7879 | strSql=db:escape("INSERT INTO student values(1,'lili')") | ||
7880 | |||
7881 | db:close() -- Close the db Connection | ||
7882 | env:close() -- Close the database environment | ||
7883 | -- printout: INSERT INTO student values(1,"lili") | ||
7884 | {{/code}} | ||
7885 | |||
7886 | == [[db:execute()>>||anchor="Hdbfileoperation"]] == | ||
7887 | |||
7888 | **Prototype : **db:execute(sql) | ||
7889 | |||
7890 | **Function: **execute a SQL statement | ||
7891 | |||
7892 | **Parameters**: | ||
7893 | |||
7894 | * SQL statement executed | ||
7895 | |||
7896 | **Return: **It depends on the type of SQL | ||
7897 | |||
7898 | * If the information is retrieved from a database, the return value is a user-defined data object. (Extracted target data) | ||
7899 | * If the statement is a non-fetch data type, return a status to verify that whether the SQL statement was successfully executed | ||
7900 | |||
7901 | **Example** | ||
7902 | |||
7903 | {{code language="LUA"}} | ||
7904 | local env = luasql_sqlite3.sqlite3() -- Get the environment class | ||
7905 | local db = env:connect("udisk:test.db") -- Connect to a DB file. | ||
7906 | |||
7907 | local status=db:execute("CREATE TABLE students('id' TEXT,'name' TEXT)") -- Create a table "student" | ||
7908 | status=db:execute("INSERT INTO students values('1','AA')") -- Insert information 1 AA | ||
7909 | status=db:execute("INSERT INTO students values('2','BB')") -- Insert information 2 BB | ||
7910 | status=db:execute("INSERT INTO students values('3','CC')") -- Insert information 3 CC | ||
7911 | status=db:execute([[UPDATE students SET name = 'DD' WHERE id = '3']]) -- Update information | ||
7912 | status=db:execute([[DELETE FROM students WHERE id='2']]) -- Delete statement | ||
7913 | local cursor=db:execute("SELECT * FROM students") --Query the database | ||
7914 | |||
7915 | local row = cursor:fetch({},"a") | ||
7916 | while row do | ||
7917 | print(row.id,row.name) | ||
7918 | row = cursor:fetch({},"a") | ||
7919 | end | ||
7920 | cursor:close() | ||
7921 | db:close() | ||
7922 | env:close() | ||
7923 | ----------Output----------: | ||
7924 | {{/code}} | ||
7925 | |||
7926 | == [[db:commit()>>||anchor="Hdbfileoperation"]] == | ||
7927 | |||
7928 | **Prototype : **db:commit() | ||
7929 | |||
7930 | **Function: **Commit data to the database (executing the execture () function simply stores the data into memory, automatically commit by default (can be set)) | ||
7931 | |||
7932 | **Parameters**: None | ||
7933 | |||
7934 | **Return:** None | ||
7935 | |||
7936 | **Example** | ||
7937 | |||
7938 | {{code language="LUA"}} | ||
7939 | local env = luasql_sqlite3.sqlite3() -- Get the environment class | ||
7940 | local db = env:connect("udisk:test.db") -- Connect a db file | ||
7941 | |||
7942 | local status=db:execute("CREATE TABLE students('id' TEXT,'name' TEXT)") -- Create a table "student" | ||
7943 | status=assert(db:setautocommit(false)) -- Set non-auto commit | ||
7944 | status=assert(db:execute("INSERT INTO students values('4','AA')")) -- Insert information 4 AA | ||
7945 | status=assert(db:execute("INSERT INTO students values('5','BB')")) -- Insert information 5 BB | ||
7946 | status=assert(db:execute("INSERT INTO students values('6','CC')")) -- Insert information 6 CC | ||
7947 | |||
7948 | --statue=assert(db:commit()) -- Manually commit the database | ||
7949 | --while 1 do | ||
7950 | --At this point, cut out and view the database content, you will find that the data is not commited to the database | ||
7951 | --Put the commit() function in front of the dead loop and re-verify that the data has been updated into the database | ||
7952 | --end | ||
7953 | statue=assert(db:commit()) -- Manually commit the database | ||
7954 | |||
7955 | db:close() | ||
7956 | {{/code}} | ||
7957 | |||
7958 | == [[db:rollback()>>||anchor="Hdbfileoperation"]] == | ||
7959 | |||
7960 | **Prototype : **db:rollback() | ||
7961 | |||
7962 | **Function: **Roll back operations on the database until before commit | ||
7963 | |||
7964 | **Parameters**: None | ||
7965 | |||
7966 | **Return:** None | ||
7967 | |||
7968 | **Exmaple:** | ||
7969 | |||
7970 | {{code language="LUA"}} | ||
7971 | local env = luasql_sqlite3.sqlite3() -- Get the environment class | ||
7972 | local db = env:connect("udisk:test.db") -- Connect a db file | ||
7973 | |||
7974 | local status=db:execute("CREATE TABLE students('id' TEXT,'name' TEXT)") -- Create a table "student" | ||
7975 | local status=assert(db:setautocommit(false)) -- Manually commit the database | ||
7976 | status=assert(db:execute("INSERT INTO students values('7','AA')")) -- Insert information 7 AA | ||
7977 | statue=assert(db:commit()) -- Commit | ||
7978 | status=assert(db:execute("INSERT INTO students values('8','BB')")) -- Insert information 8 BB | ||
7979 | statue=assert(db:rollback()) -- Rollback | ||
7980 | status=assert(db:execute("INSERT INTO students values('9','CC')")) -- Insert information 9 CC | ||
7981 | statue=assert(db:commit()) -- Commit | ||
7982 | |||
7983 | db:close() | ||
7984 | env:close() | ||
7985 | ------------------------------------------------------------ | ||
7986 | -- Only 7 and 9 are inserted in the final result because after 8 we roll back the operation to before commit. | ||
7987 | {{/code}} | ||
7988 | |||
7989 | == [[db:setautocommit()>>||anchor="Hdbfileoperation"]] == | ||
7990 | |||
7991 | **Prototype : **db:setautocommit( flag ) | ||
7992 | |||
7993 | **Function: **Set the automatic commit function | ||
7994 | |||
7995 | **Parameters**: | ||
7996 | |||
7997 | * flag(boolean) : Whether to set auto-commit | ||
7998 | ** true —— auto-commit | ||
7999 | ** false —— Commit mannully | ||
8000 | |||
8001 | **Return:** None | ||
8002 | |||
8003 | **Exmaple:** | ||
8004 | |||
8005 | {{code language="LUA"}} | ||
8006 | env = luasql_sqlite3.sqlite3() -- Get the environment class | ||
8007 | db = env:connect("udisk:test.db") -- Connect a db file | ||
8008 | |||
8009 | status=assert(db:setautocommit(false)) -- Set non-auto commit | ||
8010 | status=assert(db:execute("INSERT INTO students values('10','AA')")) -- Insert information 10 AA | ||
8011 | statue=assert(db:commit()) -- Commit | ||
8012 | status=assert(db:execute("INSERT INTO students values('11','BB')")) -- Insert information 11 BB | ||
8013 | statue=assert(db:rollback()) -- Rollback | ||
8014 | |||
8015 | status=assert(db:setautocommit(true)) -- Set non-auto commit | ||
8016 | status=assert(db:execute("INSERT INTO students values('12','CC')")) -- Insert information 12 CC | ||
8017 | statue=assert(db:rollback()) -- Rollback | ||
8018 | statue=assert(db:commit()) -- Commit | ||
8019 | |||
8020 | cursor:close() | ||
8021 | db:close() | ||
8022 | env:close() | ||
8023 | ------------------------------------------ | ||
8024 | --By comparing the two rollbacks and combining the information actually written to the database (11 is not written to the database, 12 is written to the database) | ||
8025 | --You can see that setting automatic commit as it literally means, commits to the database after the SQL statement is executed. | ||
8026 | --(ps: The flash of embedded machine has a life limit. To increase the service life, the operation frequency of flash should be reduced as much as possible) | ||
8027 | {{/code}} | ||
8028 | |||
8029 | == [[db:getlastautoid()>>||anchor="Hdbfileoperation"]] == | ||
8030 | |||
8031 | **Prototype : **db:getlastautoid() | ||
8032 | |||
8033 | **Function: **Gets the total number of data records for the current database | ||
8034 | |||
8035 | **Parameters**: None | ||
8036 | |||
8037 | **Return:** Total number of data records (number) | ||
8038 | |||
8039 | **Exmaple:** | ||
8040 | |||
8041 | {{code language="LUA"}} | ||
8042 | local env = luasql_sqlite3.sqlite3() -- Get the environment class | ||
8043 | local db = env:connect("udisk:test.db") -- Connect a db file | ||
8044 | |||
8045 | local status=db:execute("CREATE TABLE students('id' TEXT,'name' TEXT)") -- Create a table "student" | ||
8046 | status=assert(db:execute("INSERT INTO students values('1','AA')")) -- Insert information 1 AA | ||
8047 | status=assert(db:execute("INSERT INTO students values('2','BB')")) -- Insert information 2 BB | ||
8048 | status=assert(db:execute("INSERT INTO students values('3','CC')")) -- Insert information 3 CC | ||
8049 | status=assert(db:getlastautoid()) | ||
8050 | print(type(status),status) -- number 4.0 | ||
8051 | |||
8052 | db:close() | ||
8053 | env:close() | ||
8054 | {{/code}} | ||
8055 | |||
8056 | = Cursor operation = | ||
8057 | |||
8058 | == [[cursor:close()>>||anchor="HCursoroperation"]] == | ||
8059 | |||
8060 | **Prototype : **cursor:close() | ||
8061 | |||
8062 | **Function: **Close the cursor | ||
8063 | |||
8064 | **Parameters**: None | ||
8065 | |||
8066 | **Return:** None | ||
8067 | |||
8068 | **Exmaple: ** | ||
8069 | |||
8070 | {{{cursor:close() | ||
8071 | }}} | ||
8072 | |||
8073 | == [[cursor:getcolnames()>>||anchor="HCursoroperation"]] == | ||
8074 | |||
8075 | **Prototype : **cursor:getcolnames() | ||
8076 | |||
8077 | **Function: **Gets the name of the header | ||
8078 | |||
8079 | **Parameters**: None | ||
8080 | |||
8081 | **Return:** Header (table): the database table header | ||
8082 | |||
8083 | **Exmaple: ** | ||
8084 | |||
8085 | {{code language="LUA"}} | ||
8086 | local env = luasql_sqlite3.sqlite3() -- Get the environment class | ||
8087 | local db = env:connect("udisk:test.db") -- Connect a db file | ||
8088 | |||
8089 | local status=db:execute("CREATE TABLE students('id' TEXT,'name' TEXT)") | ||
8090 | local cursor,errorString = db:execute([[select * from students]]) | ||
8091 | local colNmaes=cursor:getcolnames() | ||
8092 | for i,v in ipairs(colNmaes) do | ||
8093 | print(v) | ||
8094 | end | ||
8095 | |||
8096 | cursor:close() | ||
8097 | db:close() | ||
8098 | env:close() | ||
8099 | --------------Output | ||
8100 | --id | ||
8101 | --name | ||
8102 | {{/code}} | ||
8103 | |||
8104 | == [[cursor:getcoltypes()>>||anchor="HCursoroperation"]] == | ||
8105 | |||
8106 | **Prototype : **cursor:getcoltypes() | ||
8107 | |||
8108 | **Function: **Gets the type of the header | ||
8109 | |||
8110 | **Parameters**: None | ||
8111 | |||
8112 | **Return:** | ||
8113 | |||
8114 | * headerType(table): Returns a table containing header type information | ||
8115 | |||
8116 | **Exmaple: ** | ||
8117 | |||
8118 | {{code language="LUA"}} | ||
8119 | local env = luasql_sqlite3.sqlite3() --Get the environment class | ||
8120 | local db = env:connect("udisk:test.db") --Connect a db file | ||
8121 | |||
8122 | local status=db:execute("CREATE TABLE students('id' TEXT,'name' TEXT)") | ||
8123 | status=db:execute("INSERT INTO students values('1','AA')") --Insert information 1 AA | ||
8124 | status=db:execute("INSERT INTO students values('2','BB')") --Insert information 2 BB | ||
8125 | status=db:execute("INSERT INTO students values('3','CC')") --Insert information 3 CC | ||
8126 | local cursor,errorString = assert(db:execute([[select * from students]])) | ||
8127 | |||
8128 | local colTypes = cursor:getcoltypes() | ||
8129 | for i,v in ipairs(colTypes) do | ||
8130 | print(i,v) | ||
8131 | end | ||
8132 | |||
8133 | cursor:close() | ||
8134 | db:close() | ||
8135 | env:close() | ||
8136 | ----------Output | ||
8137 | --1 TEXT | ||
8138 | --2 TEXT | ||
8139 | {{/code}} | ||
8140 | |||
8141 | == [[cursor:fetch()>>||anchor="HCursoroperation"]] == | ||
8142 | |||
8143 | **Prototype : **cursor:fetch(tab, opt) | ||
8144 | |||
8145 | **Function: **Facilitate row nodes (Similar to iterators) | ||
8146 | |||
8147 | **Parameters**: | ||
8148 | |||
8149 | * tab(table) : Row node | ||
8150 | * ((( | ||
8151 | opt(string): | ||
8152 | |||
8153 | "n" —— The index of the table is the header of the table, column id | ||
8154 | |||
8155 | "a" —— The index of the table is the header of the table, column value | ||
8156 | ))) | ||
8157 | |||
8158 | **Return:** | ||
8159 | |||
8160 | * row(table) : Get the extracted data | ||
8161 | |||
8162 | **Exmaple:** | ||
8163 | |||
8164 | {{code language="LUA"}} | ||
8165 | local env = luasql_sqlite3.sqlite3() -- Get the environment class | ||
8166 | local db = env:connect("udisk:test.db") -- Connect a db file | ||
8167 | |||
8168 | local status = db:execute("CREATE TABLE students('id' TEXT,'name' TEXT)") | ||
8169 | status = assert(db:execute([[INSERT INTO students values('1','AA')]])) | ||
8170 | status = assert(db:execute([[INSERT INTO students values('2','BB')]])) | ||
8171 | status = assert(db:execute([[INSERT INTO students values('3','CC')]])) | ||
8172 | |||
8173 | local cursor = assert(db:execute([[select * from students]])) | ||
8174 | |||
8175 | local row = cursor:fetch({},"a") | ||
8176 | print(string.format("id = %s name = %s ",row.id,row.name)) -- Index by table header name | ||
8177 | |||
8178 | row = cursor:fetch({},"n") | ||
8179 | print(string.format("id = %s name = %s ",row[1], row[2])) -- Index by numerical number | ||
8180 | |||
8181 | |||
8182 | cursor:close() | ||
8183 | db:close() | ||
8184 | env:close() | ||
8185 | ----------Output: | ||
8186 | --id = 1 , name = AA | ||
8187 | --id = 2 , name = BB | ||
8188 | {{/code}} |