Lua Command Manual-Draft Version-20211124
Lua Command Manual-Draft Version-20211124
ft
function, call function, require return (flow control) ······································27
logic: xor and or not lshift rshift (logical operations) ·····································29
Internal memory - $ ····················································································31
Static memory - $M ····················································································36
ra
4.4 External link (external memory) ····································································41
4.5 File (read/write/export/delete/print files) ··························································53
4.6 FileSlot (file access) ···················································································65
4.7 FTP Client (FTP transfer function) ·································································73
4.8 Math (mathematical operations) ····································································78
4.9 Recipe ····································································································85
4.10 Screen (screen control) ··············································································110
D
4.11 String (string operations) ············································································114
4.12 System library (system parameters) ····························································· 120
4.13 Serial port communication (COM communication) ··········································· 124
4.14 TCP communication ················································································· 131
4.15 UDP communication ················································································· 139
4.16 Text encoding (encoding format change) ······················································· 150
4.17 Utility (CRC calculation) ············································································ 153
4.18 Convert (Floating-point number conversion) ·················································· 155
4.19 Account (permissions and password setup) ··················································· 157
4.20 Mail ······································································································ 163
4.21 Draw (drawing function) ············································································ 170
1
Lua Command Manual
1. Introduction to Lua
Lua is a lightweight programming language created by the Computer Graphics Technology
Group (Tecgraf) in Brazil. Its scripting language is simple and easy to learn and use, helping
programmers to quickly complete their programming tasks. With Delta HMI’s support of the Lua
programming language, you can have more flexibility in designing screens.
ft
ra
Figure 1.1 The Lua programming interface
D
2
Lua Command Manual
You can also go to the Action field in the Lua window and click Add to quickly add simple Lua
example commands to the program.
Chunk or block
ft Description
A statement in the Lua program is called a chunk, which can refer to a
command or a group of commands; the execution results are the same.
For example:
====================================================
ra
a=1
mem.inter.WriteBit(1,0,a)
Or
a=1 mem.inter.WriteBit(1,0,a)
There are two types of comments in Lua :
Single-line comment: starts with two dashes (--) and continues until the
end of the line.
Example:
-- Lua
Multi-line comment: starts with two dashes and two upper brackets (--[[)
Program comments
D
and ends with two lower brackets. The contents within these brackets are
skipped and not executed.
Example:
--[[This is
a multi-line
comment]]
In Lua, variables and function names are represented by identifiers with
combinations of English letters, numbers, and underscores. However, the
first character of the string cannot be a number. Note that when setting
variables, do not use the following 22 reserved words.
local nil not or
Identifiers and and if then else
reserved words elseif for end in
do while until repeat
break true false function
return goto - -
Note: Lua is case sensitive, so while goto is a reserved word, GOTO can be a variable.
3
Lua Command Manual
Item Description
All variables are global variables unless they are declared as local.
Example:
====================================================
A=100
Local variables are variables declared as local. Their scope is limited to
Global and local the block in which they are located, such as in functions, branch control
variables instructions, loops, or do end structures.
Example:
====================================================
if a=1 then
local A=100
end
You can assign values to multiple variables at the same time in a single
statement and use a comma to separate each variable.
Example:
Multiple
assignments ====================================================
a,b,c=1,2,3
mem.inter.Write(1,b)
Variables are typeless, so there is no need to specify the data type when
declaring variables, but the variable values have types, namely nil,
Data type
ft
booleans, numbers, and strings.
Basic types
nil
boolean
number
Table 2.1 Lua data types
Missing value
False and true
Description
4
Lua Command Manual
Internal memory - $
ft
mem.inter.ReadDouble
mem.inter.Write
mem.inter.WriteDW
(unit: double-precision floating-point
numbers; 64 bits)
Write the value to the internal memory
(unit: Word)
Write the value to the internal memory
(unit: Double Word)
Write the value to the internal memory
ra
mem.inter.WriteFloat
(unit: Float)
mem.inter.WriteBit Write the bit to the internal memory
Write the value to the internal memory
mem.inter.WriteDouble (unit: double-precision floating-point
numbers; 64 bits)
mem.inter.ReadAscii Read the sting of the internal memory
mem.inter.WriteAscii Write the string to the internal memory
Read the value of the static memory
mem.static.Read
(unit: Word)
D
Read the value of the static memory
mem.static.ReadDW
(unit: Double Word)
Read the value of the static memory
mem.static.ReadFloat
(unit: Float)
mem.static.ReadBit Read the bit of the static memory
Read the value of the static memory
mem.static.ReadDouble (unit: double-precision floating-point
numbers; 64 bits)
Write the value to the static memory
Static memory - $M mem.static.Write
(unit: Word)
Write the value to the static memory
mem.static.WriteDW
(unit: Double Word)
Write the value to the static memory
mem.static.WriteFloat
(unit: Float)
mem.static.WriteBit Write the bit to the static memory
Write the value to the static memory
mem.static.WriteDouble (unit: double-precision floating-point
numbers; 64 bits)
mem.static.ReadAscii Read the string of the static memory
mem.static.WriteAscii Write the string to the static memory
5
Lua Command Manual
External link
(external memory)
ft
link.WriteAscii
link.WriteDouble
link.CopyFromInter
Write the string to the external memory
Write the value to the external memory
(unit: double-precision floating-point
numbers; 64 bits)
Copy data from the HMI internal
memory to the external memory
ra
Copy data from the external memory to
link.CopyToInter
the HMI internal memory
Copy data from the HMI
link.CopyArray internal/external memory to the HMI
internal/external memory
Use the COM communication to
link.DownloadPLC download the isp or dvp program to the
PLC through HMI
Use the network communication to
link.DownloadEthPLC download the isp or dvp program to the
PLC through HMI
D
Use the COM communication to write
link.WritePasswordPLC
the system password to the PLC
Set the default PLC Station number for
link.SetDefaultStationNo
the HMI to communicate with
Set the HMI Slave station number
link.SetHMIStationNo
(Modbus Slave)
Use the network communication to
link.CODESYSAppDownload download the CODESYS program to
the PLC through HMI
Use the network communication to
upload the CODESYS program to the
link.CODESYSAppUpload
USB storage device connected to the
HMI
file.Open Create/open file
file.Read Read file data
File file.ReadLine Read file (unit: one line)
(read/write/export/
delete/print files) file.Write Write to file
file.Length Read the file length
file.GetLineCount Read the total line count in the file
6
Lua Command Manual
FileSlot
(file access)
ft
fileslot.Write
fileslot.ReadValue
fileslot.WriteValue
fileslot.GetLength
fileslot.Remove
fileslot.Import
Write the fileslot file
Read the value of the fileslot
Write the value to the fileslot
Get the content length of the fileslot
Remove the fileslot
Import the fileslot file
ra
fileslot.Export Export the fileslot file
fileslot.SetName Set the fileslot filename
fileslot.GetName Get the fileslot filename
fileslot.GetID Get the fileslot file ID
FTP Client (FTP ftpc.Download FTP download
transfer function) ftpc.Upload FTP upload
math.abs Get the absolute value of the number
Get the value of the exponential
math.exp
D
function with the base e
math.log Get the value of the logarithmic function
math.sin Get the sine value
math.sinh Get the hyperbolic sine value
math.cos Get the cosine value
math.cosh Get the hyperbolic cosine value
math.tan Get the tangent value
Math math.tanh Get the hyperbolic tangent value
(mathematical math.asin Get the arcsine value
operations)
math.acos Get the arccosine value
math.atan Get the arctangent value
Get the arctangent value
math.atan2
(two parameters)
Get the angle corresponding to the
math.deg
radians
Get the radians corresponding to the
math.rad
angle
math.min Get the minimum value
math.max Get the maximum value
7
Lua Command Manual
ft
recipe.GetCurEnRcpNoIndex
recipe.GetCurEnRcpGIndex
recipe.GetEnRcpWord
Get the index of the current enhanced
recipe number
Get the index of the current enhanced
recipe group
Get the value of the specified enhanced
recipe address (Word)
ra
Get the value of the specified enhanced
recipe.GetEnRcpDWord
recipe address (Double Word)
Get the value of the specified enhanced
recipe.GetEnRcpFloat
recipe address (Float)
Get the string of the specified enhanced
recipe.GetEnRcpAscii
recipe address
Recipe
Set parameters to the recipe address
recipe.SetRcpWord
(Word)
Set parameters to the recipe address
recipe.SetRcpDWord
(Double Word)
D
Set parameters to the recipe address
recipe.SetRcpFloat
(Float)
Set the name of the enhanced recipe
recipe.SetCurEnRcpNoName
number
Set the name of the enhanced recipe
recipe.SetCurEnRcpGName
group
Set parameters to the enhanced recipe
recipe.SetEnRcpWord
address (Word)
Set parameters to the enhanced recipe
recipe.SetEnRcpDWord
address (Double Word)
Set parameters to the enhanced recipe
recipe.SetEnRcpFloat
address (Float)
Set the string to the enhanced recipe
recipe.SetEnRcpAscii
address
recipe.ChangeRcpNoIndex Change the index of the recipe number
recipe.ChangeRcpGIndex Change the index of the recipe group
Change the index of the enhanced
recipe.ChangeEnRcpNoIndex
recipe number
Change the index of the enhanced
recipe.ChangeEnRcpGIndex
recipe group
8
Lua Command Manual
String
ft
string.lower
string.upper
string.reverse
string.byte
after the string
Convert the string to lowercase
Convert the string to uppercase
Reverse the string
Convert the string to decimal value
ra
(string operations) string.char Convert the decimal value to string
Replace the specified string with
string.gsub
another string
Find the part in the string that matches
the pattern string, and then return the
string.gmatch matching parameters
Note: must be used with for loop.
Find the part in the string that matches
the pattern string, and then return the
matching parameters
D
string.match Note: the difference between string.match
and string.gmatch is that string.gmatch
returns all matching strings, while
string.match only returns the first set of
matching strings.
sys.Sleep System delay
sys.GetTick Get the total up time of the HMI so far
sys.GetInterParam Get the internal parameters of the HMI
sys.BuzzerOn Turn on the buzzer
sys.GetDate Get current time
sys.GetDateString Get current time (in string)
System library Get the number of days from
sys.GetDays
(system parameters) 1970/01/01 to the set date
Get the number of seconds elapsed
sys.GetSecs
from 00:00:00 to the set time
sys.GetTime Get system time
Get the date after the set number of
sys.ToDate
days from 1970/01/01
Get the time after the set number of
sys.ToTime
seconds from 00:00:00
9
Lua Command Manual
TCP communication
ft
tcp.Read
tcp.Write
tcp.Close
tcp.GetMaxCount
Read characters (TCP)
Write characters (TCP)
Close the connection (TCP)
Get the maximum number of
connections (TCP)
Get the number of running sockets
ra
tcp.GetRunCount
(TCP)
Check the communication status of the
tcp.GetStatus
socket (TCP)
udp.Open Open the UDP network communication
udp.Read Read characters (UDP)
udp.Write Write characters (UDP)
udp.Close Close the connection (UDP)
UDP communication Get the maximum number of
udp.GetMaxCount
connections (UDP)
D
Get the number of running sockets
udp.GeRunCount
(UDP)
Check the communication status of the
udp.GetStatus
socket (UDP)
Text encoding
(encoding format text.GbkToUtf8 Convert GBK to UTF-8
change)
Utility
util.Crc16Modbus Calculate the CRC value
(CRC calculation)
Convert the integer to a floating-point
Convert convert.IntToFloat
number
(Floating-point
number conversion) Convert the string to a 64-bit
convert.ToNum
floating-point number
account.Add Add permissions account
account.Delete Delete permissions account
Account account.ChangeName Change permissions account name
(permissions and account.ChangePassword Change permissions password
password setup)
Change permissions level of the
account.ChangeLevel
account
account.GetPassword Get user password
10
Lua Command Manual
ft
draw.Ellipse
draw.Clear
draw.SetAntialiasing
Draw (ellipse)
Clear the drawing
Enable/disable anti-aliasing
ra
D
11
Lua Command Manual
ft
Serial port communication (COM communication)
12
Lua Command Manual
Basic syntax commandshelp you perform matrix operations, comparisons, for loops, while
loops, basic mathematical operations, and subprogram production and logical operations.
The commands include:
Command Command expression Description
Type: bool number string nil Data type
Type: table, array Matrix operations
if then else elseif end, and or not Comparison
for var=1,3 do … end for loop
Basic syntax
while break, repeat until while, repeat loop
+-*/%^ Mathematical operations
function, call function, require return Flow control
logic: xor and or not lshift rshift Logical operations
ft
ra
D
13
Lua Command Manual
Variables do not have types, so there is no need to specify the data type when declaring
variables, but the variable values have types. The types of variables provided by Lua are
Boolean expressions, integers, decimals, and strings. Examples of their use are as follows:
Example
Click [Project] [Program] [Main] to display the Lua editor window.
Open Lua
ft
value_string = "abcd_123".
When value_bool is true, write value_integer (integer) to $10 with value_integer
as length, write value_double (decimal) to $12 with value_double as length, and
write value_string (string) to $14 with value_string as length.
while true do
ra
value_bool=true
Build value_integer=11
Lua program value_double = 13.6
value_string = "abcd_123"
if value_bool==true then
mem.inter.Write(10,value_integer,string.len(value_integer))
mem.inter.WriteFloat(12,value_double,string.len(value_double))
mem.inter.WriteAscii(14,value_string,string.len(value_string))
end
end
D
14
Lua Command Manual
Example
Create 2 Numeric Entry elements and set the Write Addresses to $10 and $12.
The Data Types are Word and Double Word, and the Data Formats are
Unsigned Decimal and Floating. The parameter settings are as follows:
Create
Numeric Entry
and Character
Entry elements
ft
ra
Create a Character Entry element, set the Write Address to $14 and String
Length to 10. The settings are as follows.
D
After building the Lua program and creating the elements, compile and
download the project to the HMI.
The elements which memory addresses are $10, $12, and $14 respectively
display the corresponding results:
Execution
results
15
Lua Command Manual
Except for the above defined variables (boolean expressions, integers, decimals, strings), the
undefined variables are not allowed in Lua. Using undefined variables causes the Lua program
to terminate. The following is a simple example.
Example
Specify the uppercase expression of Delta as v1 and write the v1 string to $100, shown as
follows.
==========================================================
while true do
v1=string.upper(Delta)
mem.inter.WriteAscii(100,v1,string.len(v1))
end
Since Delta is an undefined variable (not a string or constant), the Lua program terminates at the
second line, displays an error message "Lua runtime error" on the screen, and does not execute
the write action.
The correct syntax is:
end
ft
===========================================================
while true do
v1=string.upper("Delta")
mem.inter.WriteAscii(100,v1,string.len(v1))
ra
After adding " " to Delta, Delta becomes a string, and v1 is the uppercase expression of Delta.
Then, write the v1 string to $100.
D
16
Lua Command Manual
These commands help you calculate and design the matrix. The following sections will explain
each in detail.
{ }: define array
Command
t= { }
name
Command
t= { var1, var2, var3, var4, var5}
expression
Parameter
var1, var2,...: elements
definition
t= { 11, 22, 33, "s1", "s2" }
t [1] = 111;
Example
t [2] = 222;
t [3] = 333;
Example After creating the array t= {11, 22, 33, "s1", "s2”}, change t [1] from 11 to 111, t [2]
description from 22 to 222, and t [3] from 33 to 333.
Return value
Command
name
Command
No return value
table.count
ft
table.count: get the number of elements in the array
ra
Count = table.count(myTable)
expression
Parameter
myTable: table
definition
t = {11, 22, 33}
count = table.count(t)
Example
t ["a1"] = 10
count = table.count(t)
Create an array t={ 11, 22, 33 }; the t array contains 3 elements.
Example Count = 3.
description
D
Add t ["a1"] = 10; now, the t array contains 4 elements.
Count = 4.
Return value Count = integer; the number of items in the table
17
Lua Command Manual
ft
table.remove(myTable, [pos, ])
pos: the position of the parameter to be removed (this parameter is not mandatory)
t = {1, 4, "three"}
ra
table.remove(t, 2)
Example
t = {1, 4, "three"}
table.remove(t)
Remove the parameter of the second position specified in the t array.
Example t = {1, "three"}
description If no position is specified, remove the last parameter.
t2 = {1, 4}
Return value No return value
D
table.concat: combine arrays into a string
Command
table.concat
name
Command
valueString = table.concat(myTable, sepChar)
expression
Parameter myTable: table
definition sepChar: string; combine the parameters in the table with this string
t = {1, 2, "three", 4, "five"}
Example
str = table.concat(t, ",")
Example
Combine arrays into a string with ",", str = 1,2,three,4,five.
description
Return value str = string; the string after combination
18
Lua Command Manual
ft
ra
D
19
Lua Command Manual
These commands help you design loops of if conditional expressions in Lua. The following
sections will explain each in detail.
The following table is the list for if...else...end commands.
Basic
Command Expression Description
syntax
If Var1 == Var2 then
If Var1 is equal to Var2,
If…then…end -- Do Something A
run action A.
end
If Var1 == Var2 then
--Do Something A If Var1 is equal to Var2,
If…then…else…end else then run action A,
--Do Something B otherwise run action B.
Comparison end
If Var1 > Var2 then
--Do Something A If Var1 is greater than
elseIf Var1 < Var3 Var2, run action A;
If…then…elseif…else…end --Do Something B if Var1 is less than Var3,
run action B, otherwise run
ft else
--Do Something C
end
Example (if…then…elseif…else…end)
action C.
Var1 represents the temparture and its read address is $10. If the
ra
temperature (Var1) is over 100, enable the cooler (address 1.0) and turn
off the heater. If the temperature (Var1) is lower than 20, turn on the
heater (address 2.0) and turn off the cooler, otherwise turn off the cooler
(address 1.0) and heater (address 2.0).
while true do
Var1 = mem.inter.Read(10)
if (Var1 > 100) then
Build Lua program mem.inter.WriteBit(1, 0, 1)
(temperature mem.inter.WriteBit(2, 0, 0)
response measure) elseif (Var1 < 20) then
D
mem.inter.WriteBit(2, 0, 1)
mem.inter.WriteBit(1, 0, 0)
else
mem.inter.WriteBit(1, 0, 0)
mem.inter.WriteBit(2, 0, 0)
end
end
Create Numeric Create a Numeric Display element and set the Read Address to $10.
Display and
Maintained Button Create 2 Maintained Button elements and set the Write Addresses to $1.0
elements and $2.0.
20
Lua Command Manual
Example (if…then…elseif…else…end)
After building the Lua program and creating the elements, compile and
download the project to the HMI.
When the temperature is over 100, trigger $1.0 to turn on the cooler;
when the temperature is lower than 20, turn off $1.0 and trigger $2.0 to
enable the heater. When the temperature is between 20 and 110, turn off
$1.0 and $2.0.
Scenario 1 Scenario 2 Scenario 3
$100 > 0 $100 < 20 20 < $100 < 110
Execution
results
4.1.4
ft
for var=1, 3 do… end (for loop)
These commands help you design the for loops in Lua. The following sections will explain each
in detail.
The following table is the list of for commands.
ra
for: execute the loop
Command
for
name
for [condition1] do
Command
--[code block 1]
expression
end
for t= 1, 6 do
Example mem.inter.Write(t, 123)
D
end
Example Run the loop for 6 times and write the value 123 respectively to the internal memory
description of $1 to $6.
Return value No return value
21
Lua Command Manual
Example 1
Set the initial value v as 0 and read the address $1 as the value v1. Enter
the for loop, execute v = v + 1 every 1000 ms and write v to the memory
address $10, and execute v1 times in total. Lastly, set v to 0, then repeat
the above steps.
v=0
while true do
Build Lua v1=mem.inter.Read(1)
program for i = 1,v1 do
v=v+1
mem.inter.Write(10, v)
sys.Sleep(1000)
end
v=0
end
Create Numeric Create a Numeric Entry element and set the Write Address to $1.
Entry elements Create a Numeric Entry element and set the Write Address to $10.
After building the Lua program and creating the elements, compile and
download the project to the HMI.
Enter 3 to memory address $1, execute v = v + 1 every 1000 ms and write
the result to the memory address $10. Execute 3 times in total, then repeat
1, 2, and 3.
ft Write 3
$1 $10
ra
3 seconds
$1 $10
Execution
results
D
Repeat
22
Lua Command Manual
Example 2
Create a t array. Execute the loop every 1500 ms, each time it searches for
the parameters of the t array, and sequentially pairs the parameters to the
key and value variables, and writes to the result $10 and $1. During the
search, the parameters that are paired already will be skipped.
Build Lua while true do
program t = {v1=123, v2="abc", v3=567}
(Move once for key,value in pairs(t) do
every 1.5
seconds) mem.inter.WriteAscii(10,key,string.len(key))
mem.inter.WriteAscii(1,value,string.len(value))
sys.Sleep(1500)
end
end
Create
Create 2 Character Entry elements with the Write Addresses as $1 and
Character Entry
$10.
elements
After building the Lua program and creating the elements, compile and
download the project to the HMI.
Execute the loop every 1500 ms, write the key to $1 and the value to $10
consecutively.
ft
ra
1500 ms
Execution
results
1500 ms
1500 ms
D
23
Lua Command Manual
These commands help you design the while and repeat loops in Lua. The following sections will
explain each in detail.
Example
description
Return value
Command
ft
When i is not equal to 5, execute the loop i = i+1. If i > 100, break out of the loop
with the break command.
No return value
repeat…until
ra
name
Repeat
Command
-- [code block 1]
expression
Until [condition1]
Parameter
No parameters
definition
repeat
Example i=i+1
until( i > 15 )
D
Example
Repeat executing i = i + 1 until i > 15.
description
Return value No return value
24
Lua Command Manual
Build Lua
program
end
end
ft
mem.inter.WriteBit(100,0,0)
If $10.0 is triggered, read $20 as v1. When v1 is not equal to 10, execute v1
= v1 + 1 and write the result to $20. If the result (v1) is greater than or equal
to 10, stop the execution.
if (mem.inter.ReadBit(10,0)==1) then
ra
v1 = mem.inter.Read(20)
while v1~=10 do
v1 = v1 + 1
sys.Sleep(100)
mem.inter.Write(20, v1)
if v1>=10 then
mem.inter.WriteBit(10,0,0)
break
end
end
D
end
If $100.0 is triggered, repeat v1 = v1-1 and write the result to $20 until v1 =
0, then close $100.0.
if (mem.inter.Read(100,0)==1) then
repeat
v1 = v1 - 1
sys.Sleep(100)
mem.inter.Write(20, v1)
until( v1 ==0 )
mem.inter.WriteBit(100,0,0)
end
Create Numeric Create a Numeric Display element and set the Write Address to $20.
Display and
Maintained Create 2 Maintained Button elements and set the Write Addresses to $10.0
Button elements and $100.0.
25
Lua Command Manual
Execution
results
ft
+-*/%^ (mathematical operations)
On
ra
4.1.6
These commands help you design mathematical operations in Lua. The following sections will
explain each in detail.
The following table is the list for +-*/%^ commands.
Basic syntax
Command Expression Description
types
+ Var1=Var2+Var3 Addition
D
- Var1=Var2-Var3 Subtraction
26
Lua Command Manual
A function can wrap and name the Lua code blocks, and it is called in a program for execution,
which makes the program planning simpler. The following sections will explain each in detail.
ft
require [program name 1]
ra
Parameter
No parameters
definition
-- in Prog001 program
function Add (a, b)
return a+b, a*b
Example end
-- in main program
require "Prog001"
v1, v2 = Add (10, 20)
D
Create function Add(a, b) in the Prog001 program, load the Prog001 program in the
Example
main program with the require command, then use the Add(a, b) function to make
description
v1=10+20, v2=10*20, V1=30, and v2=200.
Return value No return value
27
Lua Command Manual
=============================================
function F(c)
return c*9/5+32
Build Lua subprogram end
In the subprogram interface, create a Lua subprogram.
K(c)=c+273.15 means converting Celsius to Kelvin.
ft
=============================================
function K(c)
end
return c+273.15
Execution results
28
Lua Command Manual
This command helps you to perform logical operations. The following sections will explain each
in detail.
The following are the lists for logical operation commands.
Basic syntax Command Expression examples Description
Var1 performs xor
operation on 0x01 and
math.bxor Var1 = math.bxor(0x01, 0x03) 0x03.
-- output: Var1 = 2
Var1 performs and
operation on 0x01 and
math.band Var1 = math.band(0x01, 0x03) 0x03.
-- output: Var1 = 1
Var1 performs or
operation on 0x01 and
math.bor Var1 = math.bor(0x01, 0x03) 0x03.
-- output: Var1 = 3
Logical Var1 performs not
operations operation on 0x01.
math.bnot Var1 = math.bnot(0x01)
ft
math.lshift Var1 = math.lshift(0x01, 2)
-- output: Var1 =
0xFFFFFFFE
Convert 1 from
hexadecimal to binary and
shift it to the left by two
bits.
-- output: Var1 = 4
ra
Convert 4 from
hexadecimal to binary and
math.rshift Var1 = math.rshift(0x04, 2) shift it to the right by two
bits.
-- output: Var1 = 1
29
Lua Command Manual
Example (logic)
Read $10 as c, perform the and logical operation on c and 1 (it is
known that in the and operation, only 1 and 1 yield 1. Which means
that with the and operation, odd numbers and 1 yield 1; even
numbers and 1 yield 0). So, if the variable g is 1, judge is the string
“odd number”, and when g is 0, judge is the string “even number”.
Lastly, write judge to $100.
===============================================
Build Lua program while true do
(determining c = mem.inter.Read(10)
odd/even number) g = math.band(1, c)
if g==1 then
judge="odd number"
else
judge="even number"
end
mem.inter.WriteAscii(100, judge, string.len(judge))
end
Create Numeric Entry Create a Numeric Entry element and set the Write Address to $10.
and Character Display Create a Character Display element and set the Read Address to
elements $100.
ftAfter building the Lua program and creating the elements, compile
and download the project to the HMI.
Enter 1 for $10, then $100 displays odd number.
Enter 2 for $10, then $100 displays even number.
$10 $100
ra
Execution results
$10 $100
D
30
Lua Command Manual
These commands help you read and write to the internal memory address. The commands
include:
Command Command expression Description
Read the value of the internal memory
mem.inter.Read
(unit: Word)
Read the value of the internal memory
mem.inter.ReadDW
(unit: Double Word)
Read the value of the internal memory (unit:
mem.inter.ReadFloat
Float)
ft
mem.inter.WriteBit
mem.inter.ReadAscii
mem.inter.WriteAscii
mem.inter.WriteDouble
(unit: Float)
Write the bit to the internal memory
Read the string of the internal memory
Write the string to the internal memory
Write the value to the internal memory
(unit: double-precision floating-point numbers)
ra
The following sections will explain each in detail.
mem.inter.ReadDW: read the value of the internal memory (unit: Double Word)
Command
mem.inter.ReadDW
name
Command
Value = mem.inter.ReadDW(index, [value_format])
expression
index: integer; memory index with the range of 0 to 199998
Parameter
definition value_format: format string. Fill in "signed" for signed numbers; this parameter is not
mandatory.
Example dw = mem.inter.ReadDW(0)
Example
Read the value of $0 in units of Double Word.
description
Return value v1: 0x00000000 to 0xFFFFFFFF; return 0 on failure
31
Lua Command Manual
Example
description
Return value
Read $0.15 bit.
ft
b1: integer; 1 or 0; return 0 on failure
32
Lua Command Manual
mem.inter.WriteDW: write the value to the internal memory (unit: Double Word)
Command
mem.inter.WriteDW
name
Command
Result = mem.inter.WriteDW(index, value)
expression
Parameter index: integer; memory index with the range of 0 to 199998
definition value: decimal integer
Example result = mem.inter.WriteDW(0, 65536)
Example
Write the value 65536 to $0 in units of Double Word.
description
Return value Result: return 1 on success; return 0 on failure
ft
Write the value 1.23 to $0 in units of Float.
33
Lua Command Manual
ft
Result = mem.inter.WriteAscii(0, "posheng", string.len("posheng"))
Write posheng to $0 with the string length of "posheng".
Note: the characters "\0" will be added to the end of the written data.
Result: integer; the number of the written ascii bytes
ra
Command Example Execution results
while true do
v1 = mem.inter.Read(100)
v1 = v1 + 100 $100
mem.inter.Write(100, v1)
mem.inter.Read v1=v1+100
end
mem.inter.Write
Description of command
$100
Read the internal memory address $100
as v1. After adding 100 to v1, write v1 to
the internal memory address $100.
D
Command Example Execution results
while true do
d1 = mem.inter.ReadDW(100)
d1 = d1 + 1
mem.inter.WriteDW(100, d1) $100
mem.inter.ReadDW end
d1=d1+1
mem.inter.WriteDW Description of command
After reading the internal memory $100
address $100 as d1, add 1 to d1 and
write the result to the internal memory
address $100. The unit of reading and
writing is Double Word.
34
Lua Command Manual
while true do
mem.inter.WriteFloat(100,1.1)
d1 = mem.inter.ReadFloat(100)
d1 = d1 *2.5
mem.inter.WriteFloat(100, d1)
$100
end
mem.inter.ReadFloat f1=f1*2.5
Description of command
mem.inter.WriteFloat
Write the floating-point number 1.1 to the
internal memory address $100 and read $100
the internal memory address $100
floating-point number as f1. After
multiplying f1 by 2.5, write f1 to the
internal memory address $100. The unit
of reading and writing is floating-point
number.
Command Example Execution results
while true do
b1 = mem.inter.ReadBit(1,15)
b1=b1+1 $1.15
mem.inter.ReadBit
mem.inter.WriteBit
ft
end
mem.inter.WriteBit(1,15,b1)
Description of command
Read the internal memory address $1.15
as b1. After adding 1 to b1, write the
b1=b1+1
$1.15
ra
resultto the internal memory address
$1.15.
Command Example Execution results
while true do
q1 = mem.inter.ReadDouble (0)
mem.inter.WriteDouble (10, q1)
end $0
mem.inter.ReadDouble Description of command
mem.inter.Write Double
After reading the internal memory
address $0 as q1, write q1 to the internal
D
memory address $10. The unit of reading $10
and writing is double-precision
floating-point number.
Command Example Execution results
while true do
str2 = mem.inter.ReadAscii(2000, 5)
mem.inter.WriteAscii(3000, str2,
string.len(str2)) $2000
end
mem.inter.ReadAscii
mem.inter.WriteAscii Description of command
Set the string length to 5, read the string
from the internal memory address $2000 $3000
as str2, and write the string str2 to the
internal memory address $3000.
Note: the unit of string length is byte.
35
Lua Command Manual
These commands help you read and write to the internal memory in the HMI when the power is
off. The commands include:
Command Command expression Description
Read the value of the static memory
mem.static.Read
(unit: Word)
Read the value of the static memory
mem.static.ReadDW
(unit: Double Word)
Read the value of the static memory
mem.static.ReadFloat
(unit: Float)
mem.static.ReadBit Read the bit of the static memory
Read the value of the static memory
mem.static.ReadDouble
(unit: double-precision floating-point numbers)
Write the value to the static memory
Static memory - $M mem.static.Write
(unit: Word)
Write the value to the static memory
mem.static.WriteDW
(unit: Double Word)
Write the value to the static memory
mem.static.WriteFloat
(unit: Float)
ft
mem.static.WriteBit
mem.static.WriteDouble
mem.static.ReadAscii
mem.static.WriteAscii
Write the bit to the static memory
Write the value to the static memory
(unit: double-precision floating-point numbers)
Read the string of the static memory
Write the string to the static memory
ra
The following will explain each in detail.
mem.static.ReadDW: read the value of the static memory (unit: Double Word)
Command
mem.static.ReadDW
name
Command
Value = mem.static.ReadDW(index, [value_format])
expression
index: integer; memory index with the range of 0 to 1022
Parameter
definition value_format: format string. Fill in "signed" for signed numbers; this parameter is not
mandatory.
Example dw = mem.static.ReadDW(0)
Example
Read the value of $M0 in units of Double Word.
description
Return value dw: 0x00000000 to 0xFFFFFFFF; return 0 on failure
36
Lua Command Manual
ft
Read the bit of the static memory $M0.15.
b1: integer; 1 or 0
37
Lua Command Manual
mem.static.WriteDW: write the value to the static memory (unit: Double Word)
Command
mem.static.WriteDW
name
Command
Result = mem.static.WriteDW(index, value)
expression
Parameter index: integer; memory index with the range of 0 to 1023
definition value: integer
Example result = mem.static.WriteDW(0, 65535)
Example
description
Return value
ft
Write the value 65535 to $M0 in units of Double Word.
38
Lua Command Manual
Example
Example
description
Return value
ft
string_len: integer; number of ascii bytes
result = mem.static.WriteAscii(0, "posheng", string.len("posheng"))
Write posheng to $M0 with the string length of "posheng".
Note: the characters "\0" will be added to the end of the written data
Result: return 1 on success; return 0 on failure
ra
Command Example Execution results
v1 = mem.static.Read(100)
v1 = v1 + 100
$M100
mem.static.Write(100, v1)
mem.static.Read Description of command v1=v1+100
mem.static.Write
Read the static memory address $M100
as v1. After adding 100 to v1, write the $M100
result to the static memory address
D
$M100.
Command Example Execution results
d1 = mem.static.ReadDW(100)
d1 = d1 + 1
mem.static.WriteDW(100, d1) $M100
39
Lua Command Manual
mem.static.WriteFloat(100,1.1)
f1 = mem.static.ReadFloat(100)
f1 = f1 *2.5
mem.static.WriteFloat(100, f1)
$M100
Description of command
mem.static.ReadFloat f1=f1*2.5
mem.static.WriteFloat Write the floating-point number 1.1 to the
static memory address $M100, and read
$M100
the floating-point number of the static
memory address $M100 as f1. After
multiplying f1 by 2.5, write the result to
the static memory address $M100. The
unit of reading and writing is floating-point
number.
Command Example Execution results
b1 = mem.static.ReadBit(1,15)
b1=b1+1 $M1.15
mem.static.WriteBit(1,15,b1)
mem.static.ReadBit b1=b1+1
mem.static.WriteBit Description of command
Command
ft
Read the static memory address $M1.15
as b1. After adding 1 to b1, write the
result to the static memory address
$M1.15.
Example
$M1.15
Execution results
ra
str2 = mem.static.ReadAscii(200, 5)
mem.static.WriteAscii(300, str2, 5)
$M200
40
Lua Command Manual
These commands help you read and write the memory address of the external device in the
HMI. The commands include:
Command Command expression Description
Read the value of the external memory
link.Read
(unit: Word)
Read the value of the external memory
link.ReadDW
(unit: Double Word)
Read the value of the external memory
link.ReadFloat
(unit: Float)
link.ReadBit Read the bit of the external memory address
link.ReadAscii Read the string of the external memory address
Read the value of the external memory (unit:
link.ReadDouble double-precision floating-point numbers;
64 bits)
Write the value to the external memory
link.Write
(unit: Word)
Write the value to the external memory
link.WriteDW
(unit: Double Word)
External link
(external
ft
link.WriteFloat
link.WriteBit
link.WriteAscii
link.WriteDouble
Write the value to the external memory
(unit: Float)
Write the bit to the external memory
Write the string to the external memory
Write the value to the external memory (unit:
double-precision floating-point numbers; 64 bits)
ra
memory)
Copy data from the HMI internal memory to the
link.CopyFromInter
external memory
Copy data from the external memory to the HMI
link.CopyToInter
internal memory
Copy data from the HMI internal/external memory
link.CopyArray
to the HMI internal/external memory
Use the COM communication to download the isp
link.DownloadPLC
or dvp program to the PLC through HMI
Use the network communication to download the
link.DownloadEthPLC
D
isp or dvp program to the PLC through HMI
Use the COM communication to write the system
link.WritePasswordPLC
password to the PLC
Set the PLC default station number for the HMI to
link.SetDefaultStationNo
communicate with
link.SetHMIStationNo Set the HMI Slave station number (Modbus Slave)
Use the network communication to download the
link.CODESYSAppDownload
CODESYS program to the PLC through HMI
Use the network communication to upload the
link.CODESYSAppUpload CODESYS program to the the USB storage
device connected to the HMI
The following sections will explain each in detail.
41
Lua Command Manual
link.ReadDW: read the value of the external memory (unit: Double Word)
Command
link.ReadDW
name
Command
Result = link.ReadDW(addr, [value_format])
expression
addr: string; memory address, such as "{Link2}1@D1"
Parameter
definition
Example
Example
description
mandatory.
ft
value_format: format string. Fill in "signed" for signed numbers; this parameter is not
dw = link.ReadDW("{Link2}1@D1")
Read the values of the following external memory in units of Double Word:
the controller memory which link number is 2, the station number is 1, and the
communication address is D1.
ra
Return value dw: integer
42
Lua Command Manual
ft
ra
D
43
Lua Command Manual
Example
ft
Result = link.ReadDouble(addr)
A1 = link.ReadDouble("{Link2}1@D1")
Read the value of the following external memory in double-precision floating-point
format: the controller memory which link number is 2, the station number is 1, and the
ra
description
communication address is D1.
Return value A1: double-precision floating-point numbers
link.WriteDW: write the value to the external memory (unit: Double Word)
Command
link.WriteDW
name
Command
Result = link.WriteDW(addr, value_dword)
expression
Parameter addr: string; memory address, such as "{Link2}1@D0"
definition value_dword: integer
Example Result = link.WriteDW("{Link2}1@D1", 65536)
Write the value 65536 in units of Double Word to the following external memory: the
Example
controller memory which link number is 2, the station number is 1, and the
description
communication address is D1.
Return value Result: return 1 on success; return 0 on failure
44
Lua Command Manual
Example
description
Return value
ft
Result = link.WriteBit("{Link2}1@M1", 1)
Set the following external memory bit to On:
the controller memory which link number is 2, the station number is 1, and the
communication address is D1.
Result: return 1 on success; return 0 on failure
ra
link.WriteAscii: write the string to the external memory
Command
link.WriteAscii
name
Command
Result = link.WriteAscii(addr, ascii, asci_len)
expression
addr: string; memory address, such as "{Link2}1@D0"
Parameter
ascii: string, UTF-8 encoding (bytes)
definition
asci_len: integer, ascii number (bytes)
Example Result = link.WriteAscii("{Link2}1@D1", "posheng", string.len("posheng"))
D
Write posheng to the following external memory with the string length of "posheng":
Example
the controller memory which link number is 2, the station number is 1, and the
description
communication address is D1.
Return value Result: return 1 on success; return 0 on failure
45
Lua Command Manual
link.CopyFromInter: copy data from the HMI internal memory to the external memory
Command
link.CopyFromInter
name
Command
result = link.CopyFromInter(addr, interMemIndex, wordLen)
expression
addr: string; memory address, such as "{Link2}1@D1"
Parameter
interMemIndex: integer; starting address of the internal memory
definition
wordLen: integer; number of Word
Example Result = link.CopyFromInter("{Link2}1@D1", 100, 6)
Move the data with the length of 6 words from HMI address $100 to the following
Example external memory:
description the controller memory which link number is 2, the station number is 1, and the
communication address is D1.
Return value Result: return 1 on success; return 0 on failure
link.CopyToInter: copy data from the external memory to the HMI internal memory
Command
link.CopyToInter
name
Command
result = link.CopyToInter(addr, interMemIndex, wordLen)
expression
Parameter
definition
Example
Example
description
ft
addr: string; memory address, such as "{Link2}1@D1"
interMemIndex: integer; starting address of the internal memory
wordLen: integer; number of Word
Result = link.CopyToInter("{Link2}1@D1", 100, 6)
Move the data with the length of 6 words from the controller memory (Link number 2,
station number 1, communication address D1) to $100 of the HMI.
ra
Return value Result: return 1 on success; return 0 on failure
link.CopyArray: copy data from the HMI internal/external memory to the HMI
internal/external memory
Command
link.CopyArray
name
Command
result = link.CopyArray(dst_addr, dst_offset, src_addr, src_offset, wordLen)
expression
dst_addr: string or integer; target address
D
dst_offset: integer; target offset length
Parameter
src_addr: string or integer; source address
definition
src_offset: integer; source offset length
wordLen: integer; copy length
Example 1: result = link.CopyArray(95, 0, 190, 3, 6)
Example
Example 2: result = link.CopyArray(95, 0,"{Link2}1@D0", 0, 6)
Example 1: move the data with the length of 6 words from addresses $193 to $198 to
Example addresses $95 to $100.
description Example 2: move the data with the length of 6 words from {Link2}1@D0 to
{Link2}1@D5 to $95 to $100.
Return value Result: return 1 on success; return 0 on failure; return -1 on parameter error
46
Lua Command Manual
link.DownloadPLC: use the COM communication to download the isp or dvp program to
the PLC through HMI
Command
link.DownloadPLC
name
Command ret, errDesc = link.DownloadPLC(comNo, stationNo, diskID, fileName, projectPwd,
expression plcSystemSecurityPwd)
comNo: integer; communication serial port number, 1: COM1; 2: COM2, ...
stationNo: integer: 1 to 255
Parameter diskID: integer; disk ID. 2: USB drive; 3: SD card
definition fileName: string; filename, such as ss.dvp and ss.isp
projectPwd: string; project password
plcSystemSecurityPwd: string; PLC system security password
Example ret, errDesc = link.DownloadPLC(2, 1, 2, "EH.dvp", "1234", "5678")
Use COM2 as the interface to download the program EH.dvp stored in the USB drive
to the PLC with the project password 1234 and system password 5678.
Example Note:
description 1. This command only supports the COM communication, but not the network communication
2. You must first ensure the communication between the HMI and the PLC.
3. Currently available on Delta DVP, AS, and AH series PLC.
ret: return 1 on success; return 0 on failure
Return value
errDesc: string; error message
ft
link.DownloadEthPLC: use the network communication to download the isp or dvp
program to the PLC through HMI
Command
name
link.DownloadEthPLC
ra
Command ret, errDesc = link.DownloadEthPLC(ip, port, stationNo, diskID, fileName,
expression projectPwd, plcSystemSecurityPwd)
ip: string; "192.168.0.1",...
port: integer
stationNo: integer; station number: 1 to 255
Parameter
diskID: integer; disk ID; 2: USB drive; 3: SD card
definition
fileName: string; filename, such as delta.dvp and delta.isp
projectPwd: string; project password
plcSystemSecurityPwd: string; PLC system security password
ret, errDesc = link.DownloadEthPLC("192.168.123.205", 502, 1, 2, "delta.isp", "1234",
D
Example
"5678")
Using the network as the interface, download the program delta.isp stored in the USB
to the PLC with the project password 1234 and system password 5678.
Example Note:
description 1. This command only supports the network communication, but not the COM
communication.
2. You must confirm that the HMI and PLC are in the same network domain.
3. Currentlyavailale on Delta DVP, AS, and AH series PLC.
ret: return 1 on success; return 0 on failure
Return value
errDesc: string; error message
47
Lua Command Manual
link.WritePasswordPLC: use the COM communication to write the system password to the
PLC
Command
link.WritePasswordPLC
name
Command ret, errDesc = link.WritePasswordPLC(comNo, stationNo, oldPlcSystemPwd,
expression newPlcSystemPwd)
comNo: integer; communication serial port number. 1:COM1; 2: COM2, and so on.
Parameter stationNo: integer: 1 to 255
definition oldPlcSystemPwd: string; the old PLC system security password
newPlcSystemPwd: string; the new PLC system security password
Example ret, errDesc = link.WritePasswordPLC(2, 1, "1234", "2222")
Use COM2 as the interface to change the PLC password to 2222. If the PLC has a
system password, it compares to see whether the password matches "1234".
Example Note:
description 1. This command only supports the COM communication, but not the network
communication.
2. You must first ensure the communication between the HMI and the PLC.
3. Currently available on Delta DVP, AS, AH series PLC.
ret: return 1 on success; return 0 on failure
Return value
errDesc: string; error message
with
Command
name
Command
expression
ft
link.SetDefaultStationNo: set the default PLC Station number for the HMI to communicate
link.SetDefaultStationNo
48
Lua Command Manual
Example
description
ft
ra
Return value ret: return 1 on success; return -1 on parameter error
D
49
Lua Command Manual
Example
username = ""
password = ""
ft
ret, errDesc = link.CODESYSAppDownload(connMethodID, address, diskID, appFile,
showMsgBox, username, password)
Download the "AddressOffset.app" program in the USB storage device connected to
the HMI to the CODESYS PLC. An error window and error message appear if the
ra
description download fails.
Note: the download folder must contain a .CRC file, such as AddressOffset.CRC.
ret: return 1 on success; return 0 on failure
Return value
errDesc: string; error message
D
50
Lua Command Manual
Example
description
username = ""
password = ""
ft
ret, errDesc = link.CODESYSAppUpload(connMethodID, address, diskID, appFile,
showMsgBox, username, password)
Upload the CODESYS PLC program to the USB storage device connected to the
HMI. An error window and error message appear if the upload fails.
ra
ret: return 1 on success; return 0 on failure
Return value
errDesc: string; error message
v1 = link.Read("{Link2}1@D1")
v1 = v1 + 100
link.Write("{Link2}1@D2",v1) {Link2}1@D1
D
link.Read Description of command v1=v1+100
link.Write Read the external memory address
{Link2}1@D1 as v1. After adding 100 to v1,
{Link2}1@D2
write the result to the external memory
address {Link2}1@D2. The unit of reading
and writing is Word.
d1 = link.ReadDW("{Link2}1@D1")
d1 = d1 + 1
link.WriteDW("{Link2}1@D3",d1) {Link2}1@D1
link.ReadDW d1=d1+1
Description of command
link.WriteDW
Read the external memory address
{Link2}1@D1 as d1. After adding 100 to d1, {Link2}1@D3
write the result to the external memory
address {Link2}1@D3. The unit of reading
and writing is Double Word.
51
Lua Command Manual
link.WriteFloat("{Link2}1@D1", 1.1)
f1 = link.ReadFloat("{Link2}1@D1")
f1 = f1 * 2.5
link.WriteFloat("{Link2}1@D3",f1)
{Link2}1@D1
b1 = link.ReadBit("{Link2}1@M0")
b1 = b1 + 1 {Link2}1@M0
link.WriteBit("{Link2}1@M1",b1)
link.ReadBit Description of command b1=b1+1
link.WriteBit
Command
ft
Read the external memory address
{Link2}1@M0 as b1, add 1 to b1, and write
the result to the register address
{Link2}1@M1.
Example
{Link2}1@M1
Execution results
ra
link.WriteAscii("{Link2}1@D0", "posheng",7)
ascii, ret, errMsg =
link.ReadAscii("{Link2}1@D0", 20)
link.WriteAscii("{Link2}1@D10", 20) {Link2}1@D0
link.ReadAscii
Description of command
link.WriteAscii
Write the string “posheng” with the string
length of 7 to the external memory address
{Link2}1@D0. Read the memory address {Link2}1@D10
{Link2}1@D0 in units of 20 bytes as the ascii
string, and write the ascii string in units of 20
D
bytes to {Link2 }1@D10.
Command Example Execution results
result = link.CopyFromInter("{Link2}1@D1",
1, 6) $1~$6
mem.inter.Write(100,result)
result = link.CopyToInter("{Link2}1@D1", 10,
link.CopyFromInter return
6)
link.CopyFromInter {Link2}1@D1~{Link2}1@D6
mem.inter.Write(200,result)
link.CopyToInter
Description of command link.CopyToInter return
52
Lua Command Manual
These commands help you read, write, export, print files, and create pdf files from the files.
The commands include:
Command Command expression Description
file.Open Create/open file
file.Read Read file data
file.ReadLine Read file (unit: one line)
file.Write Write to file
file.Length Read the file length
file.GetLineCount Read the total line count in the file
file.Seek Set the pointer
file.GetPos Get the current pointer position
file.GetError Check file
file.Close Close file
File
file.List Get a list of the files stored in the HMI
(read/write/export/
delete/print files) file.Export Export file
file.Delete Delete file
ft file.DeleteDir
file.ToPDF
file.ToPrinter
file.ListExternal
Delete directory
Convert the file to PDF
Print file
Get a list of the files stored in the external
device
ra
file.Exist Check if the file exists
file.PDFToPrinter Print PDF file
file.Copy Copy file
file.Move Move file
The following sections will explain each in detail.
53
Lua Command Manual
Example
ft
ret, data = file.ReadLine(fileHandle)
Return value ret: integer; success: the length of the written string; failure: -1
54
Lua Command Manual
Example
Example
description
ft
fileHandle: integer; file pointer
55
Lua Command Manual
Example
Example
description
ft
fileHandle: integer; file pointer
ret = file.GetError(fileHandle)
Note: the fileHandle parameter is generated by the file.Open command.
Check file.
56
Lua Command Manual
ft
ret, errCode = file.Export(“myFile.txt”, 2)
Export the file myFile.txt created by file.Open to an external USB device.
Note: only supports txt files.
ret: integer. Success: 1; failure: 0
errCode: error code
ra
Return value Description
-1 Parameter setting error
Return value
-106 External storage device not ready
-110 File does not exist
-111 Export failed
-136 Invalid path
57
Lua Command Manual
landscape)
ft
ret, errCode = file.ToPDF(disk_id, srcFileName, pdfFileName, paperSize, fontSize,
58
Lua Command Manual
Return value
ft
ret, err, errText = file.Exist(0, "myFile001")
59
Lua Command Manual
ft -110
-111
-136
The specified filename does not exist
Unable to copy file
Invalid file path
ra
Command
file.Move
name
Command
ret, errCode = file.Move(src_name, src_disk_id, dest_name, dest_disk_id)
expression
src_name: string; source filename
Parameter src_disk_id: integer; 0: HMI; 2: USB; 3: SD card
definition dest_name: string; destination filename
dest_disk_id: integer; 0: HMI; 2: USB; 3: SD card
Example ret = file.Move("myFile001.txt", 0, "myFile001Out.txt", 2)
Move the file myFile001.txt stored from the HMI to the USB storage device, and
D
Example name the file as myFile001Out.txt.
description Note: if the destination address contains a file with the same filename, the system overwrites
the file.
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description
0 No errors
60
Lua Command Manual
Example (handshake the data of two HMIs through the file command)
The concept of handshaking is as follows. Use the file command on HMI #1 to
write the internal memory address to a txt file and export it to the USB. Then
insert the USB into HMI #2. HMI #2 reads the relevant parameters through the
file command, and writes them to the internal memory.
disk_id = 0
file_name = " DELTA.txt"
ret, fileHandle = file.Open(disk_id, file_name)
while true do
end
ft
mem.inter.WriteAscii(1,"posheng",7)
mem.inter.WriteBit(0,0,0)
if mem.inter.ReadBit(0,1)==1 then
str_100w=mem.inter.ReadAscii(1,7)
file.Write(fileHandle,str_100w,string.len(str_100w))
ra
file.Close(fileHandle)
mem.inter.WriteBit(0,1,0)
end
if mem.inter.ReadBit(0,2)==1 then
SrcDiskNo = 0
DestDiskNo = 2
ret = file.Move("DELTA.txt", SrcDiskNo, " DELTA.txt", DestDiskNo)
mem.inter.Write(100,ret)
mem.inter.WriteBit(0,2,0)
D
end
end
61
Lua Command Manual
Example (handshake the data of two HMIs through the file command)
HMI #2 Lua program:
while true do
if mem.inter.ReadBit(0,3)==1 then
disk_id = 2
file_name = " DELTA.txt"
ret, fileHandle = file.Open(disk_id, file_name)
mem.inter.WriteBit(0,3,0)
Build Lua
end
program
if mem.inter.ReadBit(0,4)==1 then
ret, len = file.Length(fileHandle)
ret, data = file.Read(fileHandle, len)
mem.inter.WriteAscii(1000,data,string.len(data))
mem.inter.WriteBit(0,4,0)
end
end
Create 3 Maintained Buttons on HMI #1 with the Write Addresses as $0.0,
ft
$0.1, $0.2, and a Numeric Entry element with the Write Address as $100.
ra
Create
elements
Create 2 Maintained Buttons on HMI #2 with the Write Addresses as $0.3,
$0.4, and a Character Entry element with the Write Address as $1000.
D
After building the Lua program and creating the elements, compile and load the
project respectively to the HMIs.
When HMI #1 is booted, the HMI creates the "DELTA.txt" file internally.
disk_id = 0
file_name = " DELTA.txt"
ret, fileHandle = file.Open(disk_id, file_name)
After pressing $0.0, the system writes the string "posheng" to the internal
memory $1.
if mem.inter.ReadBit(0,0)==1 then
mem.inter.WriteAscii(1,"posheng",7)
Execution mem.inter.WriteBit(0,0,0)
results
end
62
Lua Command Manual
Example (handshake the data of two HMIs through the file command)
Press $0.1 to read the data of $1 with the string length of 7 bytes, then write the
data to the DELTA.txt file.
if mem.inter.ReadBit(0,1)==1 then
str_100w=mem.inter.ReadAscii(1,7)
file.Write(fileHandle,str_100w,string.len(str_100w))
file.Close(fileHandle)
mem.inter.WriteBit(0,1,0)
end
Press $0.2 to move the DELTA.txt file stored in the HMI to the USB.
if mem.inter.ReadBit(0,2)==1 then
Execution
results end
ft
SrcDiskNo = 0
DestDiskNo = 2
ret = file.Move("DELTA.txt", SrcDiskNo, " DELTA.txt", DestDiskNo)
mem.inter.Write(100,ret)
mem.inter.WriteBit(0,2,0)
ra
At this time, the data has been stored on the USB. After the storage is
D
complete, insert the USB into HMI #2.
On HMI #2, press $0.3 to open the "DELTA.txt" file.
if mem.inter.ReadBit(0,3)==1 then
disk_id = 2
file_name = " DELTA.txt"
ret, fileHandle = file.Open(disk_id, file_name)
mem.inter.WriteBit(0,3,0)
end
63
Lua Command Manual
Example (handshake the data of two HMIs through the file command)
Press $0.4 to read "DELTA.txt" and write the data to $1000. Then, you get the
data as "posheng".
if mem.inter.ReadBit(0,4)==1 then
ret, len = file.Length(fileHandle)
ret, data = file.Read(fileHandle, len)
mem.inter.WriteAscii(1000,data,string.len(data))
Execution mem.inter.WriteBit(0,4,0)
results end
ft
ra
D
64
Lua Command Manual
These commands help you read, write, export files, and create pdf files from the files.
The commands include:
Command Command expression Description
fileslot.Read Read the fileslot file
fileslot.Write Write the fileslot file
fileslot.ReadValue Read the value of the fileslot
fileslot.WriteValue Write the value to the fileslot
fileslot.GetLength Get the content length of the fileslot
FileSlot
fileslot.Remove Remove the fileslot
(file access)
fileslot.Import Import the fileslot file
fileslot.Export Export the fileslot file
fileslot.SetName Set the fileslot filename
fileslot.GetName Get the fileslot filename
fileslot.GetID Get the fileslot file ID
The following sections will explain each in detail.
ft
ra
D
65
Lua Command Manual
Return value
ft -24
-51
-52
-53
-54
Memory error
FileSlot ID error
FileSlot processing error
FileSlot read error
FileSlot search error
ra
-55 FileSlot writing error
-56 FileSlot deletion error
-57 FileSlot data length error
-62 FileSlot operation error
-63 External file operation error
-84 FileSlot name acquisition error
-86 FileSlot name setting error
-87 FileSlot ID acquisition error
D
-88 FileSlot write position error
66
Lua Command Manual
Return value
ft -24
-51
-52
-53
-54
Memory error
FileSlot ID error
FileSlot processing error
FileSlot read error
FileSlot search error
ra
-55 FileSlot writing error
-56 FileSlot deletion error
-57 FileSlot data length error
-62 FileSlot operation error
-63 External file operation error
-84 FileSlot name acquisition error
-86 FileSlot name setting error
-87 FileSlot ID acquisition error
D
-88 FileSlot write position error
67
Lua Command Manual
Return value
ft -24
-51
-52
-53
-54
Memory error
FileSlot ID error
FileSlot processing error
FileSlot read error
FileSlot search error
ra
-55 FileSlot writing error
-56 FileSlot deletion error
-57 FileSlot data length error
-62 FileSlot operation error
-63 External file operation error
-84 FileSlot name acquisition error
-86 FileSlot name setting error
-87 FileSlot ID acquisition error
D
-88 FileSlot write position error
68
Lua Command Manual
Return value
ft -24
-51
-52
-53
-54
-55
Memory error
FileSlot ID error
FileSlot processing error
FileSlot read error
FileSlot search error
FileSlot writing error
ra
-56 FileSlot deletion error
-57 FileSlot data length error
-62 FileSlot operation error
-63 External file operation error
-84 FileSlot name aquisition error
-86 FileSlot name setting error
-87 FileSlot ID aquisition error
-88 FileSlot write position error
D
fileslot.GetLength: get the content length of the fileslot
Command
fileslot.GetLength
name
Command
ret, err = fileslot.GetLength(FS_ID)
expression
Parameter
FS_ID: unsigned integer; FileSlot ID
definition
Example ret, err = fileslot.GetLength(1)
Example
Get the content length of FileSlot ID 1.
description
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description
69
Lua Command Manual
Parameter
definition
fileslot.Import
ft
ret, err = fileslot.Import(FS_ID, DiskID, ExtFileName)
70
Lua Command Manual
ft -63
-64
-106
-136
71
Lua Command Manual
Command
name
Command
expression
Parameter
fileslot.GetID
ft
fileslot.GetID: get the fileslot file ID
72
Lua Command Manual
These commands help you upload and download with FTP. The commands include:
Command Command expression Description
ftpc.Download FTP download
FTP Client
ftpc.Upload FTP upload
The following sections will explain each in detail.
ft
ra
D
73
Lua Command Manual
Example
description
OverWrite = 0
ft
ret, err = ftpc.Download(IPAddress, Port, UserName, Password, LocalFileName,
RemoteFileName, OverWrite)
Log into the FTP with the username "chen" and password "123" through port 21 of
the IP address 192.168.123.144. Download the file "delta.txt", and write it to FileSlot
ID 1.
ra
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description
0 No errors
-10 Invalid parameter (IP address)
-11 Invalid parameter (Port)
-12 Invalid parameter (login account)
-13 Invalid parameter (login password)
-14 Invalid parameter (local filename)
D
-15 Invalid parameter (remote filename)
-16 Invalid parameter (overwrite parameter)
-20 Login failure
Return value
-21 Logout failure
-22 Failed to set directory
-23 Failed to obtain remote directory information
-24 Local file does not exist
-25 Local file exists
-26 Remote file does not exist
-27 Remote file already exists
-28 Failed to download file
-29 Upload failure
-41 Duplicate FileSlot name
-42 FileSlot ID error
-43 FileSlot does not have enough space
74
Lua Command Manual
Example
description
OverWrite = 0
ft
ret, err = ftpc.Upload(IPAddress, Port, UserName, Password, LocalFileName,
RemoteFileName, OverWrite)
Log in to FTP with the account "chen" and password "123" through port 21 of the IP
address 192.168.123.144. Upload the contents of FileSlot ID 1 to the FTP Server
directory, and name it "delta.txt".
ra
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description
0 No errors
-10 Invalid parameter (IP address)
-11 Invalid parameter (Port)
-12 Invalid parameter (login account)
-13 Invalid parameter (login password)
-14 Invalid parameter (local filename)
D
-15 Invalid parameter (remote filename)
-16 Invalid parameter (overwrite parameter)
-20 Login failure
Return value
-21 Logout failure
-22 Failed to set directory
-23 Failed to obtain remote directory information
-24 Local file does not exist
-25 Local file already exists
-26 Remote file does not exist
-27 Remote file already exists
-28 Failed to download file
-29 Upload failure
-41 Duplicate FileSlot name
-42 FileSlot ID error
-43 FileSlot does not have enough space
75
Lua Command Manual
ft
ra
Create
elements
D
76
Lua Command Manual
ft
ra
D
77
Lua Command Manual
These commands help you perform mathematical operations. The commands include:
Command Command expression Description
math.abs Get the absolute value of the number
Get the value of the exponential function
math.exp
with the base e
math.log Get the value of the logarithmic function
math.sin Get the sine value
math.sinh Get the hyperbolic sine value
math.cos Get the cosine value
math.cosh Get the hyperbolic cosine value
math.tan Get the tangent value
math.tanh Get the hyperbolic tangent value
math.asin Get the arcsine value
Math math.acos Get the arccosine value
(mathematical
math.atan Get the arctangent value
operations)
math.atan2 Get the arctangent value (two parameters)
ft math.deg
math.rad
math.min
math.max
math.modf
Get the angle corresponding to the radians
Get the radians corresponding to the angle
Get the minimum value
Get the maximum value
Split the value into an integer and a decimal
ra
math.pi Pi (π)
math.pow Get the power value
math.randomseed Random seed
math.random Get a random value
math.sqrt Get the square root value
The following sections will explain each in detail.
D
78
Lua Command Manual
math.exp: get the value of the exponential function with the base e
Command
math.exp
name
Command
value = math.exp(x)
expression
Parameter
x: power
definition
Example v = math.exp(1)
Example
description
Return value
2.718281828459.
ft
Get the value of the natural exponential e raised to power 1, then v =
79
Lua Command Manual
Example
description
Return value
ft
Get the value of cos30°, then v = 0.866025.
v: floating-point number
80
Lua Command Manual
ft
Get the value of arcsin 30°, then v = 0.551069.
v: floating-point number
81
Lua Command Manual
Example
description
Return value
ft
Convert π to degrees, then v = 180.
82
Lua Command Manual
Return value
ft
Split 3.6 into an integer and a decimal, then v1 = 3, and v2 = 0.6.
math.pi: Pi (π)
ra
Command
math.pi
name
Command
value = math.pi
expression
Parameter
No parameters
definition
Example v = math.pi
Example
v = 3.1415926535898.
description
D
Return value v: Pi (π)
83
Lua Command Manual
Example
ft
Lower: upper limit
Upper: lower limit
math.randomseed(sys.GetTick())
v = math.random(0, 1000)
When the random function is used, it generates the same numbers with the
same order. To generate random numbers with different orders, you can
ra
Example specify a random seed with the randomSeed function before using the random
description function.
v can be any integer randomly generated from 0 to 1000.
Return value v: integer
84
Lua Command Manual
4.9 Recipe
These commands help you read and write the recipes. The recipes include 16-bit recipes, 32-bit
recipes, and enhanced recipes. If you need to specify a 16-bit recipe, the recipe group is 0. If
you need a 32-bit recipe, the recipe group is from 1 to 255. The recipe syntax of Lua includes:
Command Command expression Description
recipe.GetCurRcpNoIndex Get the current recipe number index
recipe.GetCurRcpGIndex Get the current recipe group index
Get the value of the specified recipe address
recipe.GetRcpWord
(Word)
Get the value of the specified recipe address
recipe.GetRcpDWord
(Double Word)
Get the value of the specified recipe address
recipe.GetRcpFloat
(Float)
Get the index name of the current enhanced
recipe.GetCurEnRcpNoName
recipe number
Get the index name of the current enhanced
recipe.GetCurEnRcpGName
recipe group
Get the index of the current enhanced recipe
recipe.GetCurEnRcpNoIndex
ft
recipe.GetCurEnRcpGIndex
recipe.GetEnRcpWord
recipe.GetEnRcpDWord
number
Get the index of the current enhanced recipe
group
Get the value of the specified enhanced recipe
address (Word)
Get the value of the specified enhanced recipe
ra
address (Double Word)
Get the value of the specified enhanced recipe
recipe.GetEnRcpFloat
address (Float)
Get the string of the specified enhanced recipe
recipe.GetEnRcpAscii
address
Recipe
recipe.SetRcpWord Set parameters to the recipe address (Word)
Set parameters to the recipe address
recipe.SetRcpDWord
(Double Word)
recipe.SetRcpFloat Set parameters to the recipe address (Float)
D
recipe.SetCurEnRcpNoName Set the name of the enhanced recipe number
recipe.SetCurEnRcpGName Set the name of the enhanced recipe group
Set parameters to the enhanced recipe address
recipe.SetEnRcpWord
(Word)
Set parameters to the enhanced recipe address
recipe.SetEnRcpDWord
(Double Word)
Set parameters to the enhanced recipe address
recipe.SetEnRcpFloat
(Float)
recipe.SetEnRcpAscii Set the string to the enhanced recipe address
recipe.ChangeRcpNoIndex Change the index of the recipe number
recipe.ChangeRcpGIndex Change the index of the recipe group
Change the index of the enhanced recipe
recipe.ChangeEnRcpNoIndex
number
recipe.ChangeEnRcpGIndex Change the index of the enhanced recipe group
Set parameters to the enhanced recipe address
recipe.SetEnRcpDouble
(double-precision floating-point number)
Get the value of the specified enhanced recipe
recipe.GetEnRcpDouble address (double-precision floating-point
number)
The following sections will explain each in detail.
85
Lua Command Manual
Return value
ft
ret, gIdx = recipe.GetCurRcpGIndex()
recipe.GetRcpDWord: get the value of the specified recipe address (Double Word)
Command
recipe.GetRcpDWord
name
Command
ret, value = recipe.GetRcpDWord(index, [value_format])
expression
Index: integer; recipe index
Parameter
definition value_format: formatted string.Fill in "signed" for signed numbers; this parameter is
not mandatory.
Example ret, value = recipe.GetRcpDWord(1)
Example
Get the value of the specified recipe address (RCP1) in units of Double Word.
description
ret: return 1 on success; return 0 on failure
Return value
value: integer; recipe value in units of Double Word
86
Lua Command Manual
recipe.GetCurEnRcpNoName: get the index name of the current enhanced recipe number
Command
recipe.GetCurEnRcpNoName
name
Command
ret, noName = recipe.GetCurEnRcpNoName()
expression
Parameter
No parameters
definition
Example
Example
description
Return value
ft
ret, noName = recipe.GetCurEnRcpNoName()
87
Lua Command Manual
recipe.GetEnRcpWord: get the value of the specified enhanced recipe address (Word)
Command
recipe.GetEnRcpWord
name
Command
ret, value = recipe.GetEnRcpWord(index)
expression
Parameter
Index: integer; enhanced recipe index
definition
Example
Example
description
Return value
unsigned Word.
ft
ret, value = recipe.GetEnRcpWord(2)
Get the value of the specified enhanced recipe address (ENRCP2) in units of
recipe.GetEnRcpFloat: get the value of the specified enhanced recipe address (Float)
Command
recipe.GetEnRcpFloat
name
Command
ret, value = recipe.GetEnRcpFloat(index)
expression
Parameter
Index: Integer, enhanced recipe index
definition
Example ret, value = recipe.GetEnRcpFloat(2)
Example
Get the value of the specified enhanced recipe address (ENRCP2) in units of Float.
description
ret: return 1 on success; return 0 on failure
Return value value: floating-point number; the enhanced recipe value is a 32-bit floating-point
number
88
Lua Command Manual
Example
Example
description
Return value
ft
ret = recipe.SetRcpWord(1, 5)
89
Lua Command Manual
Example
description
Return value
ft
Set POSHENG as the name of the currently specified enhanced recipe group.
90
Lua Command Manual
Example
Example
description
Return value
ft
len: integer; string length
ret = recipe.SetEnRcpAscii(4, "POSHENG", 6)
Set POSHENG to the enhanced recipe address ENRCP4 with the length of 6 bytes.
91
Lua Command Manual
Example
description
Return value
ft
Change the enhanced recipe group index to 2.
92
Lua Command Manual
Group 2:
ft
ra
Enter the Lua command as follows.
index=1
while true do
if mem.inter.ReadBit(0,0)==1 then
D
ret, noIdx = recipe.GetCurRcpNoIndex()
mem.inter.Write(1, ret)
mem.inter.Write(2,noIdx)
end
if mem.inter.ReadBit(0,1)==1 then
ret, gIdx = recipe.GetCurRcpGIndex()
mem.inter.Write(3, ret)
Build Lua
mem.inter.Write(4,gIdx)
program
end
if mem.inter.ReadBit(0,2)==1 then
ret, value = recipe.GetRcpWord(index)
mem.inter.Write(5, ret)
mem.inter.Write(6,value)
end
if mem.inter.ReadBit(0,3)==1 then
ret, value = recipe.GetRcpDWord(index)
mem.inter.Write(7, ret)
mem.inter.WriteDW(8,value)
93
Lua Command Manual
if mem.inter.ReadBit(0,4)==1 then
ret, value = recipe.GetRcpFloat(index)
mem.inter.Write(10, ret)
mem.inter.WriteFloat(11,value)
end
end
Create 5 Maintained buttons with the Write Addresses as $0.0, $0.1, $0.2,
$0.3, and $0.4.
Create 8 Numeric Entry elements with the Data Type as Word and the Data
Format as Unsigned Decimal, and then set the Write Addresses to $1, $2,
$3, $4, $5, $6, $7, and $10.
Create a Numeric Entry element with the Data Type as Double Word and
the Data Format as Floating, and then set the Write Address to $11.
Create a Numeric Entry element with the Data Type as Double Word and
the Data Format as Unsigned Decimal, and then set the Write Address to
$8.
Create 2 Numeric Entry elements, and set the Write Addresses to RCPG
and RCPNO.
Create elements
ft
ra
D
After building the Lua program and creating the elements, compile and
download the project to the HMI.
Execution
results
94
Lua Command Manual
Press $0.1 to write the return value to $3 and write the current group index
RCPG to $4.
if mem.inter.ReadBit(0,1)==1 then
ret, gIdx = recipe.GetCurRcpGIndex()
mem.inter.Write(3, ret)
mem.inter.Write(4,gIdx)
end
ft
ra
Execution
results Press $0.2. Since the index is 1, the HMI gets the value of RCP1 through
recipe.GetRcpWord, and writes the result to $5 and $6.
if mem.inter.ReadBit(0,2)==1 then
ret, value = recipe.GetRcpWord(index)
mem.inter.Write(5, ret)
mem.inter.Write(6,value)
end
D
95
Lua Command Manual
Create
Enhanced
Recipe
ft
ra
Enter the Lua command as follows.
while true do
if mem.inter.ReadBit(0,6)==1 then
ret, noName = recipe.GetCurEnRcpNoName()
mem.inter.Write(15,ret)
mem.inter.WriteAscii(16,noName,string.len(noName))
end
if mem.inter.ReadBit(0,7)==1 then
D
ret, gName = recipe.GetCurEnRcpGName()
mem.inter.Write(20,ret)
mem.inter.WriteAscii(21,gName,string.len(gName))
end
if mem.inter.ReadBit(0,9)==1 then
ret, gIdx = recipe.GetCurEnRcpGIndex()
mem.inter.Write(27,ret)
mem.inter.Write(28,gIdx)
end
if mem.inter.ReadBit(0,10)==1 then
ret, value = recipe.GetEnRcpWord(0)
mem.inter.Write(29,ret)
mem.inter.Write(30,value)
96
Lua Command Manual
if mem.inter.ReadBit(0,11)==1 then
ret, value = recipe.GetEnRcpDWord(3)
mem.inter.Write(31,ret)
mem.inter.WriteFloat(32,value)
end
if mem.inter.ReadBit(0,12)==1 then
ret, value = recipe.GetEnRcpFloat(1)
mem.inter.Write(34,ret)
mem.inter.WriteFloat(35,value)
end
if mem.inter.ReadBit(0,13)==1 then
ret, str = recipe.GetEnRcpAscii(2)
mem.inter.Write(37,ret)
mem.inter.WriteAscii(38,str,string.len(str))
end
end
ft
Create 8 Maintained buttons with the Write Addresses as $0.6, $0.7, $0.8,
$0.9, $0.10, $0.11, $0.12, and $0.13.
Create 11 Numeric Entry elements with the Data Type as Word and the
Data Format as Unsigned Decimal, and set the Write Addresses to $15,
$20, $25, $26, $27, $28, $29, $30, $31, $34, and $37.
ra
Create 3 Character Entry elements with the Write Addresses as $16, $21,
and $38, and the String Lengths as 4, 10, and 10 respectively.
Create 2 Numeric Entry elements with the Data Type as Double Word and
the Data Format as Float, and set the Write Addresses to $32 and $35.
Create 2 Numeric Entry elements with the Write Addresses as ENRCPG
and ENRCPNO.
Create elements
D
After building the Lua program and creating the elements, compile and
download the project to the HMI.
Execution
results
97
Lua Command Manual
Press $0.7 to write the return value to $20 and write the name of the current
enhanced recipe group to $21.
if mem.inter.ReadBit(0,7)==1 then
ret, gName = recipe.GetCurEnRcpGName()
mem.inter.Write(20,ret)
mem.inter.WriteAscii(21,gName,string.len(gName))
end
Execution
results
ft
Press $0.8 to write the return value to $25 and write the index of the current
enhanced recipe number to $26.
ra
if mem.inter.ReadBit(0,8)==1 then
ret, noIdx = recipe.GetCurEnRcpNoIndex()
mem.inter.Write(25,ret)
mem.inter.Write(26,noIdx)
end
Press $0.9 to write the return value to $27 and write the index of the current
D
enhanced recipe group to $28.
if mem.inter.ReadBit(0,9)==1 then
ret, gIdx = recipe.GetCurEnRcpGIndex()
mem.inter.Write(27,ret)
mem.inter.Write(28,gIdx)
end
98
Lua Command Manual
Press $0.11 to write the return value to $31 and write the value of ENRCP3
to $32 in Double Word.
if mem.inter.ReadBit(0,11)==1 then
ret, value = recipe.GetEnRcpDWord(3)
mem.inter.Write(31,ret)
mem.inter.WriteFloat(32,value)
end
Execution
results
ft
Press $0.12 to write the return value to $34 and write ENRCP1 to $35 in
floating-point number.
ra
if mem.inter.ReadBit(0,12)==1 then
ret, value = recipe.GetEnRcpFloat(1)
mem.inter.Write(34,ret)
mem.inter.WriteFloat(35,value)
end
D
Press $0.13 to write the return value to $37 and write ENRCP2 to $38 in
string.
if mem.inter.ReadBit(0,13)==1 then
ret, str = recipe.GetEnRcpAscii(2)
mem.inter.Write(37,ret)
mem.inter.WriteAscii(38,str,string.len(str))
end
99
Lua Command Manual
ft
ra
Create recipes Group 2:
Group 2:
100
Lua Command Manual
if mem.inter.ReadBit(100,1)==1 then
index=3
dword=70000
ret = recipe.SetRcpDWord(index, dword)
end
if mem.inter.ReadBit(100,2)==1 then
index=3
floatValue=10.10
ret = recipe.SetRcpFloat(index, floatValue)
ft
end
if mem.inter.ReadBit(100,3)==1 then
end
newName="recipe_NoName"
ret = recipe.SetCurEnRcpNoName(newName)
ra
if mem.inter.ReadBit(100,4)==1 then
Build Lua newName="recipe_GName"
program ret = recipe.SetCurEnRcpGName(newName)
end
if mem.inter.ReadBit(100,5)==1 then
index=4
word=88
ret = recipe.SetEnRcpWord(index, word)
D
end
if mem.inter.ReadBit(100,6)==1 then
index=7
dword=70000
ret = recipe.SetEnRcpDWord(index, dword)
end
if mem.inter.ReadBit(100,7)==1 then
index=5
dword=99.99
ret = recipe.SetEnRcpFloat(index, floatValue)
end
if mem.inter.ReadBit(100,8)==1 then
index=6
str="POSHEN"
len=6
ret = recipe.SetEnRcpAscii(index, str, len)
end
101
Lua Command Manual
if mem.inter.ReadBit(100,9)==1 then
noIdx=2
ret = recipe.ChangeRcpNoIndex(noIdx)
end
if mem.inter.ReadBit(100,10)==1 then
gIdx=2
ret = recipe.ChangeRcpGIndex(gIdx)
end
if mem.inter.ReadBit(100,11)==1 then
noIdx=3
ret = recipe.ChangeEnRcpNoIndex(noIdx)
end
if mem.inter.ReadBit(100,12)==1 then
gIdx=2
end
ft
end
ret = recipe.ChangeEnRcpGIndex(gIdx)
102
Lua Command Manual
Create elements
ft
After building the Lua program and creating the elements, compile and
download the project to the HMI.
ra
Execution
results
D
103
Lua Command Manual
Execution
results
ft
Switch RCPG to 1, and press $100.1 to write the value 70000 to the
address RCP3 of the recipe in units of DWord.
if mem.inter.ReadBit(100,1)==1 then
index=3
ra
dword=70000
ret = recipe.SetRcpDWord(index, dword)
end
D
104
Lua Command Manual
Execution
results
ft
Press $100.3 to set the recipe number index name with the string
“recipe_NoName”.
if mem.inter.ReadBit(100,3)==1 then
newName="recipe_NoName"
ra
ret = recipe.SetCurEnRcpNoName(newName)
end
D
105
Lua Command Manual
Execution
results
ft
Press $100.5 to write the value 88 to the address ENRCP4 in units of
Word.
if mem.inter.ReadBit(100,5)==1 then
index=4
word=88
ra
ret = recipe.SetEnRcpWord(index, word)
end
D
106
Lua Command Manual
Execution
results
ft
Press $100.7 to write the value 99.99 to the address ENRCP5 in units of
Float.
if mem.inter.ReadBit(100,7)==1 then
index=5
dword=99.99
ra
ret = recipe.SetEnRcpFloat(index, floatValue)
end
D
107
Lua Command Manual
Execution
results
ft
Press $100.9 to switch RCPNO to 2.
if mem.inter.ReadBit(100,9)==1 then
noIdx=2
ra
ret = recipe.ChangeRcpNoIndex(noIdx)
end
D
108
Lua Command Manual
ft
Press $100.11 to switch ENRCPNO to 3.
if mem.inter.ReadBit(100,11)==1 then
end
noIdx=3
ret = recipe.ChangeEnRcpNoIndex(noIdx)
ra
Execution
results
D
Press $100.12 to switch ENRCPG to 2.
if mem.inter.ReadBit(100,12)==1 then
gIdx=2
ret = recipe.ChangeEnRcpGIndex(gIdx)
end
109
Lua Command Manual
These commands helps you control the screen. This command includes:
Command Command expression Description
screen.Open Open the specified screen
screen.CloseSub Close the specified screen
Screen Check whether the specified screen is
(screen control) screen.IsOpened
open
Capture screenshot and save it to the
screen.Capture
external storage device
The following sections will explain each in detail.
Example
Example
description
Return value
ft
ret=screen.Open(2)
110
Lua Command Manual
Example (Screen)
Build the Lua program.
When $10.0 is triggered, the HMI opens Screen_2 and writes the return value to
$1000, and then closes $10.0.
When $10.1 is triggered, the HMI closes Sub Screen_2 and writes the return
value to $1000, and then closes $10.1.
When $10.2 is triggered, the HMI checks whether Screen_2 is opened, and
then closes $10.2.
ft
while true do
if (mem.inter.ReadBit(10,0)==1) then
ret=screen.Open(2)
mem.inter.Write(1000,ret)
mem.inter.WriteBit(10, 0, 0)
ra
program if (mem.inter.ReadBit(10,1)==1) then
screenID = 2
ret = screen.CloseSub(screenID)
mem.inter.Write(1000,ret)
mem.inter.WriteBit(10, 1, 0)
end
if (mem.inter.ReadBit(10,2)==1) then
diskID = 2
ret = screen.IsOpened(diskID)
mem.inter.Write(1000,ret)
D
mem.inter.WriteBit(10, 2, 0)
end
end
Create 3 Maintained buttons with the Write Addresses as $10.0, $10.1, and
$10.2.
Create
Maintained
buttons
Create
Screen_2
111
Lua Command Manual
Example (Screen)
After building the Lua program and creating the elements, compile and
download the project to the HMI as shown in the following screen:
Execution
When $10.0 is triggered, Screen_2 is opened, and a success value is returned
results
to $1000.
ft
ra
D
112
Lua Command Manual
Example (Screen)
When $10.1 is triggered, Screen_2 is closed, and a success value is written to
$1000.
Execution Trigger $10.2 to check whether Screen_2 is opened. If Screen_2 is closed, the
results HMI returns 0 to $1000.
ft
ra
D
113
Lua Command Manual
These commands help you perform string operations. The commands include:
Command Command expression Description
string.len Calculate the string length
string.format String formatting
string.split Split the string
string.find Locate the string
string.sub Find the string
string.rep Repeat the string
Remove the blank spaces before and
string.trim
after the string
string.lower Convert the string to lowercase
string.upper Convert the string to uppercase
string.reverse Reverse the string
String string.byte Convert the string to decimal value
(string operations) string.char Convert the decimal value to string
Replace the specified string with another
string.gsub
ft string.gmatch
string
Find the part in the string that matches
the pattern string, and then return the
matching parameters
Note: must be used with for loop.
Find the part in the string that matches
the pattern string, and then return the
ra
matching parameters
string.match Note: the difference between string.match
and string.gmatch is that string.gmatch
returns all matching strings, while
string.match only returns the first set of
matching strings.
114
Lua Command Manual
ft Example
v1 = string.format("%d", 10)
v1 = string.format("%x", 15)
v1 = string.format("%s, "posheng")
v1 = string.format("%c, 0x30)
v1 = string.format("%o, 9)
Execution results
v1=10
v1=F/f
v1= posheng
v1=0
v1=11
ra
Example %u v1 = string.format("%u, 3.14) v1=3
v1=1.000000E+03
%E/%e v1 = string.format("%e", 1000)
/1.000000E+03
%f v1 = string.format("%3.3f", 1.12345) v1=1.123
v1 = string.format("%g", 1000) v1=1000
%G/%g
v1 = string.format("%g", 1000000) v1= 1e+006
%q v1 = string.format("%q", "posheng") v1="posheng"
String formatting is to convert the string to be output (such as var1) with the
string_fmt (such as %d).
D
% operator Function
%d Output the string in decimal integer.
%X/%x Output the string as a hexadecimal integer.
%s Output the string as a string.
%c Output the string in characters.
Example
description %o Output the string in octal integer.
%u Output the string as an unsigned integer.
%E/%e Output the string in scientific notation.
%f Output the string in floating-point number.
Output the string in scientific notation (%e)
%G/%g or floating-point number (%f),
whichever has the shorter output.
%q Output the string as a complete string.
Return value v1: the string after formatting
115
Lua Command Manual
Example
Example
description
ft
start_index: integer; the starting index of the string with the index base as 1. This
parameter is not mandatory.
src="ABCDE"
v1, v2= string.find(src, "BCD")
Find the string “BCD” in the variable src, and thus v1 is 2 and v2 is 4.
ra
v1: integer; the starting index of the found string with the index base as 1
Return value
v2: integer; the ending index of the found string with the index base as 1
116
Lua Command Manual
string.trim: remove the blank spaces before and after the string
Command
string.trim
name
Command
trimStr = string.trim(string_src)
expression
Parameter
string_src: ASCII string
definition
src=" ABCDE "
Example
v1 = string.trim(src)
Example Remove the blank spaces before and after the string " ABCDE ", and thus v1=
description "ABCDE".
Return value v1: string; the string without the blank spaces
Example
Example
description
Return value
ft
name = string.lower(“Posheng”)
117
Lua Command Manual
ft
i2: integer; the decimal value to be converted to the corresponding ASCII character
v1 = string.char(65,66,67)
Convert the decimal values 65, 66, 67 into a string, and thus v1="ABC".
118
Lua Command Manual
string.gmatch: find the part in the string that matches the pattern string, and then return the
matching parameters
Command
string.gmatch
name
Command
findingIterator = string.gmatch(srcString, pattern)
expression
Parameter srcString: string
definition pattern: string; pattern string
for word in string.gmatch("Hello world", "%a+") do
w = word
Example
end
Note: this command must be used with the for loop.
Output the result content corresponding to the srcString (such as Hello world) using
the pattern string (such as %a+).
Example
The loop is executed twice with the variables w respectively being "Hello" and
description
"world".
Note: %a is to find the string and %a+ is to find the string to the end of content.
Return value findingIterator: string; the result string
string.match: find the part in the string that matches the pattern string, and then return the
matching parameters
ft
(The difference between string.match and string.gmatch is that string.gmatch returns all
matching strings, while string.match only returns the first set of matching strings)
Command
name
string.match
ra
Command
destString = string.match(srcString, pattern)
expression
Parameter srcString: string
definition pattern: string; pattern string
Example 1: word =string.match("Hello world", "%a")
Example
Example 2: s1, s2, s3 = string.match("1/22/333","(%d)/(%d+)/(%d)")
Example 1 result: word="Hello".
Example
Example 2 result: s1=1, s2=22, s3=3.
description
Note: %d is to find the number and %d+ is to find the number to the end of content.
D
Return value destString: string; the result string
119
Lua Command Manual
The system library commands help you read and write the system parameters. The commands
include:
Command Command expression Description
sys.Sleep System delay
sys.GetTick Get the total uptime of the HMI so far
sys.GetInterParam Get the internal parameters of the HMI
sys.BuzzerOn Turn on the buzzer
sys.GetDate Get current time
sys.GetDateString Get current time (in string)
System library Get the number of days from 1970/01/01 to the
sys.GetDays
(system set date
parameter) Get the number of seconds elapsed from
sys.GetSecs
00:00:00 to the set time
sys.GetTime Get system time
Get the date after the set number of days from
sys.ToDate
1970/01/01
Get the time after the set number of seconds from
ft
sys.ToTime
sys.GetDiskSpace
00:00:00
Get the disk space of the external storage
The following sections will explain each in detail.
ra
D
120
Lua Command Manual
ft
Get the total uptime of the HMI so far. Unit: milliseconds.
121
Lua Command Manual
Return value
ft
dateStr = sys.GetDateString()
dateStr is the system time (in string) represented in the form of year/month/day.
If the system time is January 31, 2019, the variable dateStr is "2019/01/31".
ra
sys.GetDays: get the number of days from 1970/01/01 to the set date
Command
sys.GetDays
name
Command
days = sys.GetDays(year, month, day)
expression
year: integer; the year
Parameter
month: integer; the month
definition
day: integer; the date
Example days = sys.GetDays(1970, 1, 2)
D
Example
days: the number of days from 1970/01/01 to 1970/01/02, and thus days=1.
description
Return value days: return the number of days elapsed on success; return nil on failure
sys.GetSecs: get the number of seconds elapsed from 00:00:00 to the set time
Command
sys.GetSecs
name
Command
Result = sys.GetSecs(hour, minute, second)
expression
hour: integer; the hour
Parameter
minute: integer; the minute
definition
second: integer; the second
Example secs = sys.GetSecs(0, 0, 59)
Example
secs: the number of seconds elapsed from 00:00:00 to 00:00:59, and thus secs=59.
description
Return value secs: return the number of seconds elapsed on success; return nil on failure
122
Lua Command Manual
sys.ToDate: get the date after the set number of days from 1970/01/01
Command
sys.ToDate
name
Command
year, month, day = sys.ToDate(days)
expression
Parameter
days: number of days elapsed
definition
Example year, month, day = sys.ToDate(5)
Example
description
Return value
ft
The year, month, and day represent the date 5 days after 1970/01/01, where
year=1970, month=1, and day=6.
The year, month, and day are the year, month, and date after the set number of days
from 1970/01/01.
sys.ToTime: get the time after the set number of seconds from 00:00:00
ra
Command
sys.ToTime
name
Command
hour, minute, second = sys.ToTime(seconds)
expression
Parameter
seconds: number of seconds elapsed
definition
Example hour, minute, second = sys.ToTime(61)
Example The hour, minute, and second represent the time 61 seconds after 00:00:00, where
description hour=0, minute=1, and second=1.
The hour, minute, and second are the hour, minute, and second after the set number
D
Return value
of seconds from 00:00:00.
123
Lua Command Manual
When two devices do not use the same communication protocol, the Delta HMIs use COM
(serial port), TCP, and UDP communication for data exchange.
The following introduces the commands for COM port communication and how to establish
connection through them. The commands include:
Command Command expression Description
com.Open Open the COM port communication
Read characters from the specified
com.ReadChars
communication port (COM)
Write characters to the specified
com.WriteChars
communication port (COM)
com.ClearBuffer Clear buffer data
Serial port Select the communication port and station
communication com.StationCheck number to check whether the
(COM communication is successful
communication) com.Close Close the communication port
Select the communication parameters to
com.CheckAlive check whether the communication is
ft com.StationOn
com.StationOff
com.GetStatus
The following sections will explain each in detail.
successful
Station On
Station Off
Get the COM port status
ra
D
124
Lua Command Manual
Parameter
definition
com.ReadChars
ft
bytes_read, buffer = com.ReadChars(com_num, len, timeout)
125
Lua Command Manual
Command
name
Command
expression
Parameter
com.ClearBuffer
ft
com.ClearBuffer: clear buffer data
126
Lua Command Manual
Parameter
definition
ft
interface: string; "RS232", "RS422", "RS485"
databits: integer; 7, 8
parity: string; "NONE", "ODD", "EVEN", "MARK", "SPACE"
stopbits: integer; 1, 2
baudrate: integer; 9600, ...
flowcontrol: string; "OFF", "CTS_RTS"
ra
station: integer; station number: 0 to 255
timeout: integer; timeout value (ms): 0 to 15000
ret = com.CheckAlive("MODBUS_ASCII", 1, "RS485", 8, "EVEN", 1, 19200, "OFF", 1,
Example
1000)
In the communication port COM1, set the communication protocol to
MODBUS_ASCII, the communication interface to RS485, the data bits to 8, the parity
Example bits to "EVEN", the stop bit to 1, the baud rate to 19200, and the flow control to OFF.
description Then, send the command to check whether the communication exists. The waiting
time for reply is 1000 ms.
Note: this command only supports Delta PLC.
D
ret: return 1 on success; return the following values on failure
Return value Description
Return value 0 Failed
-1 Invalid parameter
-101 Failed to start COM
com.StationOn: station On
Command
com.StationOn
name
Command
ret = com.StationOn(com_num, station)
expression
Parameter com_num: integer; serial communication port number: 1: COM1, 2: COM2, and so on
definition station: integer; station number: 0 to 255
Example ret = com.StationOn(1, 1)
Turn on the station number 1 controller of the communication port COM1, and then
Example
the HMI can communicate with the controller.
description
Note: this command is not related to the communication opened by com.Open.
127
Lua Command Manual
Return value
ft
ret = com.GetStatus(1)
128
Lua Command Manual
ft
Communication Time 0 ~ 15000 (ms)
RS232
0 ~ 15000 (ms)
ra
Build hardware RS485
connection HMI HMI
129
Lua Command Manual
When $70.0 is triggered, the HMI closes the communication port COM2
and writes the return value to $14.
if (mem.inter.ReadBit(70,0)==1) then
Create Maintained
buttons,
Numeric Entry and
ft
end
ret = com.Close(2)
mem.inter.Write(14, ret)
On the second HMI, trigger $60.0 to read the buffer data and write it to
the memory address $4.
130
Lua Command Manual
When two devices do not use the same communication protocol, the Delta HMIs use COM
(serial port), TCP, and UDP communication for data exchange.
The following introduces the commands for TCP communication and how to establish
connection through them. The commands include:
Command Command expression Description
Open the TCP network
tcp.Open
communication
tcp.Read Read characters (TCP)
tcp.Write Write characters (TCP)
tcp.Close Close the connection (TCP)
TCP communication Get the maximum number of
tcp.GetMaxCount
connections (TCP)
Get the number of running sockets
tcp.GetRunCount
(TCP)
Check the communication status of
tcp.GetStatus
the socket (TCP)
ft
The following sections will explain each in detail.
ra
D
131
Lua Command Manual
Parameter
definition
ft
bytes_read, buffer = tcp.Read(socket, len, timeout)
socket: integer; 1 to 8
len: integer; string length
timeout: integer; delay time (unit: ms)
ra
Example bytes_read, buffer = tcp.Read(1, 10, 1000)
Example Select socket 1 and read the characters with the set communication delay time of
description 1000 ms and the data length of 10 bytes.
bytes_read: return the length of the read data (in bytes) on success; return a
negative number on failure
Return value Description
>1 Successful
Return value
-1 Invalid parameter
-100 Socket is not open
D
-102 Failed to read
buffer: the string read back
132
Lua Command Manual
ft
No parameters
count = tcp.GetMaxCount()
133
Lua Command Manual
ft
if (mem.inter.ReadBit(50,0)==1) then
len = 5
timeout = 3000
bytes_read, buffer = tcp.Read(socket, len, timeout)
mem.inter.Write(3,bytes_read)
ra
mem.inter.WriteAscii(200,buffer,string.len(buffer))
end
if (mem.inter.ReadBit(60,0)==1) then
Build Lua buffer = mem.inter.ReadAscii(110,10)
commands for
ret = tcp.Write(1, buffer, string.len(buffer), 1000 )
TCP
communication mem.inter.Write(5, ret,string.len(ret))
end
if (mem.inter.ReadBit(70,0)==1) then
socket = 1
ret = tcp.Close(socket)
D
mem.inter.Write(7,ret)
end
if (mem.inter.ReadBit(80,0)==1) then
count = tcp.GetMaxCount()
mem.inter.Write(8,count)
end
if (mem.inter.ReadBit(90,0)==1) then
count = tcp.RunCount(socket)
mem.inter.Write(9,count)
end
if (mem.inter.ReadBit(100,0)==1) then
status = tcp.GetStatus(socket)
mem.inter.Write(10,status)
end
end
134
Lua Command Manual
TCP communication
When $40.0 is triggered, the network is turned on and the return value is
written to the memory address $1. When the first connection is established,
$40.0 is turned off.
if (mem.inter.ReadBit(40,0)==1) then
ip = "192.168.123.45"
port = 503
socket = tcp.Open(ip, port)
mem.inter.Write(1,Socket)
if socket==1 then
mem.inter.WriteBit(40,0,0)
end
end
When $50.0 is triggered, the HMI reads the data as buffer with the length of
5 bytes and the communication time of 3000 ms. Then, the return value is
written to the memory address $3, and the data buffer is written to $200.
if (mem.inter.ReadBit(50,0)==1) then
len = 5
timeout = 3000
bytes_read, buffer = tcp.Read(socket, len, timeout)
mem.inter.Write(3,bytes_read)
mem.inter.WriteAscii(200,buffer,string.len(buffer))
Build Lua
end
ft
When $60.0 is triggered, the HMI reads the data of memory address $110
with the length of 10 bytes. The data read is the string “buffer”. Then, the
read data is written to the first (socket) connection, and the return value is
written to $5.
if (mem.inter.ReadBit(60,0)==1) then
buffer = mem.inter.ReadAscii(110,10)
ra
commands for
TCP ret = tcp.Write(1, buffer, string.len(buffer), 1000 )
communication mem.inter.Write(5, ret,string.len(ret))
end
When $70.0 is triggered, the first (socket) communication is closed, and the
return value is written to $7.
if (mem.inter.ReadBit(70,0)==1) then
socket = 1
ret = tcp.Close(socket)
mem.inter.Write(7,ret)
end
D
When $80.0 is triggered, information about the maximum number of
connections is obtained.
if (mem.inter.ReadBit(80,0)==1) then
count = tcp.GetMaxCount()
mem.inter.Write(8,count)
end
When $90.0 is triggered, whether the specified socket communication is in
use is checked, and the return value is written to $9.
if (mem.inter.ReadBit(90,0)==1) then
count = tcp.RunCount(socket)
mem.inter.Write(9,count)
end
When $100.0 is triggered, the connection status of the specified socket is
checked.
if (mem.inter.ReadBit(100,0)==1) then
status = tcp.GetStatus(socket)
mem.inter.Write(10,status)
end
135
Lua Command Manual
TCP communication
Create Create 7 Maintained buttons and set the Write Addresses to $40.0, $50.0,
Maintained $60.0, $70.0, $80.0, $90.0, and $100.0.
buttons, Create 7 Numeric Entry elements and set the Write Addresses to $1, $3,
Numeric Entry $5, $7, $8, $9, and $10.
and Character Create 2 Character Entry elements and set the Write Addresses to $200
Entry elements and $110.
After building the Lua program and creating the elements, compile and
download the project to the HMI Screen (two HMIs display the same
screen).
Open the TCP Test Tool. With the PC as the Server, set the listening port
and then click Bind to wait for the Client to connect.
ft
ra
Execution
results
Trigger $40.0 to establish a connection. Then, the HMI writes the return
value to $1, and closes $40.0. You can see the connection message in the
TCP Test Tool interface.
D
Write a string to $100 to trigger $60.0. Then, the HMI read the string buffer
from $110 and writes it to the communication of the specified socket, and
then writes the return value to $5.
136
Lua Command Manual
TCP communication
Trigger $90.0 and $100.0 to write the return values to $9 and $10
respectively. Because data is written through the first (socket), $9 and $10
are both 1, which means that the first (socket) communication is in use.
Send the string “delta” to the buffer with the TCP Test Tool. When $50.0 is
triggered, the HMI reads the buffer data, writes the data to $100, and writes
the return value to $3.
Execution
results
ft
ra
D
137
Lua Command Manual
TCP communication
Trigger $80.0, and the maximum number of supported connections of 8 is
obtained.
Trigger $70.0 to close the first (socket) connection. $9 and $10 display 0,
which indicates that this connection is closed. You can also see from the
TCP Test Tool interface that the connection message disappears.
Execution
results
ft
ra
D
138
Lua Command Manual
When two devices do not use the same communication protocol, the Delta HMIs use COM
(serial port), TCP, and UDP communication for data exchange.
The following introduces the commands for UDP communication and how to establish
connection through them. The commands include:
Command Command expression Description
udp.Open Open the UDP network communication
udp.Read Read characters (UDP)
udp.Write Write characters (UDP)
udp.Close Close the connection (UDP)
UDP communication Get the maximum number of connections
udp.GetMaxCount
(UDP)
udp.GeRunCount Get the number of running sockets (UDP)
Check the communication status of the
udp.GetStatus
socket (UDP)
The following sections will explain each in detail.
ft
ra
D
139
Lua Command Manual
Parameter
udp.Read
ft
bytes_read, buffer = udp.Read(socket, len, timeout)
socket: integer; 1 to 8
len: integer; string length
ra
definition
timeout: integer; delay time (unit: ms)
Example bytes_read, buffer = udp.Read(1, 15, 1000)
Example Select socket 1, and read the characters with the set communication delay time of
description 1000 ms and the data length of 15 bytes.
bytes_read: return the length of the read data (in bytes) on success; return a
negative value on failure
Return value Description
>1 Successful
Return value
-1 Invalid parameter
D
-100 Socket is not open
-102 Failed to read
buffer: the string read back
140
Lua Command Manual
ft
ret = udp.Close(socket)
socket: integer; 1 to 8
ret = udp.Close(1)
ra
Example
Close the socket1 connection of UDP communication.
description
ret: return 1 on success; return 0 or a negative value on failure
Return value Description
Return value
-1 Invalid parameter
-100 Socket is not open
141
Lua Command Manual
ft
Check the communication status of socket 1 of the UDP communication.
Status: return 1 when the socket is on; return 0 when the socket is off
ra
D
142
Lua Command Manual
Build hardware
connection Ethernet
PC
HMI (Sokit)
end
ft
mem.inter.WriteBit(0,0,0)
if mem.inter.ReadBit(0,1)==1 then
socket = 1
buffer = "DELTA"
len = 5
ra
timeout = 1000
ret = udp.Write(socket, buffer, len, timeout)
mem.inter.Write(5, ret)
mem.inter.WriteBit(0,1,0)
end
Build Lua
commands for if mem.inter.ReadBit(0,2)==1 then
UDP socket = 1
communication len = 15
D
timeout = 1000
bytes_read, buffer = udp.Read(socket, len, timeout)
mem.inter.WriteAscii(10, buffer,string.len(buffer))
mem.inter.Write(20,bytes_read)
mem.inter.WriteBit(0,2,0)
end
if mem.inter.ReadBit(0,3)==1 then
socket = 1
ret = udp.Close(socket)
mem.inter.Write(30,ret)
mem.inter.WriteBit(0,3,0)
end
if mem.inter.ReadBit(0,4)==1 then
count = udp.GetMaxCount()
mem.inter.Write(40,count)
mem.inter.WriteBit(0,4,0)
end
143
Lua Command Manual
UDP communication
if mem.inter.ReadBit(0,5)==1 then
count = udp.GetRunCount()
mem.inter.Write(50,count)
mem.inter.WriteBit(0,5,0)
end
if mem.inter.ReadBit(0,6)==1 then
status = udp.GetStatus(1)
mem.inter.Write(60,count)
mem.inter.WriteBit(0,6,0)
end
end
When $0.0 is triggered, the network settings are established, the HMI Port
is set to 602, the destination IP is set to 192.168.123.144, the port of the
sender is set to 552, the return value is written to the memory address $1,
and then $0.0 is closed.
if mem.inter.ReadBit(0,0)==1 then
ip = "192.168.123.144"
port = 552
local_port = 602
Socket = udp.Open(ip, port, local_port)
end
ft
mem.inter.Write(1, Socket)
mem.inter.WriteBit(0,0,0)
When $0.1 is triggered, the string "DELTA" is written to the first (socket)
connection with the length of 5 bytes, the return value is written to to $5,
and then $0.1 is closed.
ra
Build Lua if mem.inter.ReadBit(0,1)==1 then
commands for socket = 1
UDP
communication buffer = "DELTA"
len =5
timeout = 1000
ret = udp.Write(socket, buffer, len, timeout)
mem.inter.Write(5, ret)
mem.inter.WriteBit(0,1,0)
end
When $0.2 is triggered, the data buffer is read with the length of 5 bytes
D
and the communication time of 1000 ms, then the data buffer is written to
$10, the return value is written to the memory address $20, and then $0.2 is
closed.
if mem.inter.ReadBit(0,2)==1 then
socket = 1
len = 15
timeout = 1000
bytes_read, buffer = udp.Read(socket, len, timeout)
mem.inter.WriteAscii(10, buffer,string.len(buffer))
mem.inter.Write(20,bytes_read)
mem.inter.WriteBit(0,2,0)
end
When $0.3 is triggered, the first (socket) communication is closed, the
return value is written to $30, and then $0.2 is closed.
if mem.inter.ReadBit(0,3)==1 then
socket = 1
ret = udp.Close(socket)
mem.inter.Write(30,ret)
mem.inter.WriteBit(0,3,0)
end
144
Lua Command Manual
UDP communication
When $0.4 is triggered, information about the maximum number of
connections supported by the HMI is obtained.
if mem.inter.ReadBit(0,4)==1 then
count = udp.GetMaxCount()
mem.inter.Write(40,count)
mem.inter.WriteBit(0,4,0)
end
When $0.5 is triggered, the number of currently running sockets is checked,
the value is written to $50, and $0.5 is closed.
Build Lua if mem.inter.ReadBit(0,5)==1 then
commands for count = udp.GetRunCount()
UDP
communication mem.inter.Write(50,count)
mem.inter.WriteBit(0,5,0)
end
When $0.6 is triggered, the connection status of the specified socket is
checked.
if mem.inter.ReadBit(0,6)==1 then
status = udp.GetStatus(1)
mem.inter.Write(60,count)
mem.inter.WriteBit(0,6,0)
end
ft
Create 7 Maintained buttons and set the Write Addresses to $0.0, $0.1,
$0.2, $0.3, $0.4, $0.5, and $0.6.
Create 7 Numeric Entry elements and set the Write Addresses to $1, $5,
$20, $30, $40, $50, and $60.
Create a Character Entry element and set the Write Address to $10.
ra
Create
Maintained
buttons,
Numeric Entry
and Character
Entry elements
D
145
Lua Command Manual
UDP communication
After building the Lua program and creating the elements, compile and
download the project to the HMI.
Use the third-party software sokit and set the computer as the Server. Set
the listening port and press UDP Listen to wait for the Client to connect.
Execution
results
Trigger $0.0 to establish connection settings, and the HMI writes the return
value to $1, and then closes $0.1.
ft
ra
D
146
Lua Command Manual
UDP communication
Trigger $0.1 to write the string “DELTA” to the specified socket
communication, and the HMI writes the return value to $5. You can see the
written content in the third-party software sokit.
Execution
results
ft
ra
Set the PC as the Client, set the Server IP address and Port, and establish
a connection.
D
147
Lua Command Manual
UDP communication
Write the string ”delta_0810” to the buffer through sokit. Trigger $0.2, and
the HMI reads the data in the buffer, writes the data to $10, and writes the
return value to $20.
ft
ra
Execution
results
D
Trigger $0.4, and the maximum number of supported connections of 8 is
obtained.
148
Lua Command Manual
UDP communication
Trigger $0.5 to get the number of running sockets, and the result is written
to $10.
Trigger $0.6 to obtain the status of the specified Socket. 1 means opened.
ft
ra
Execution
results
D
Trigger $0.3 to close the Socket.
149
Lua Command Manual
This command helps you convert the encoding format from GBK to UTF-8. The basic syntax
includes:
Command Command expression Description
Text encoding
text.GbkToUtf8 Convert GBK to UTF-8
(encoding format change)
The following section will explain the command in detail.
Example Use string.len("简体") as the string length to convert the "简体" string to a string in
description
Return value
UTF-8 format.
ft
utf8_len: if an integer is returned, it is the length of the UTF-8 string after
conversion (in bytes); if the returned value is less than or equal to 0, it indicates an
exception
utf8_string: if a string is returned, it is the UTF-8 string after conversion; if nil is
returned, it indicates an exception
ra
Example (text.GbkToUtf8)
Build a Lua program to convert the data formats.
When $102.1 is triggered, the HMI sets the string “buffer” with the length of 4 and
the Read Address of $100, converts the string “buffer” to UTF-8 encoding format,
and lastly writes the result string to $600.
while true do
Build Lua if (mem.inter.ReadBit(10, 1) == 1) then
program
buffer = mem.inter.ReadAscii(100, 4)
str_bytes, utf_str = text.GbkToUtf8(buffer, 4)
D
mem.inter.WriteAscii(600, utf_str, str_bytes)
end
end
150
Lua Command Manual
Example (text.GbkToUtf8)
Create a Maintained button and set the Write Address to $10.1.
Create
Maintained
button,
Numeric
Entry and
Multi-
language
Input
elements
ft
Create a Numeric Entry element, and set the Write Address to $100 and the Data
Format to Hexadecimal.
ra
D
151
Lua Command Manual
Example (text.GbkToUtf8)
Create a Multi-language Input element. Set the Write Address to $600, the
Encoding to UTF8, and the String Length to 16.
Note: select at least one language for using the Multi-language Input element.
Create
Maintained
button,
Numeric
Entry and
Multi-
language
Input
element
ft
ra
D
After building the Lua program and creating the elements, compile and download
the project to the HMI.
Enter EFB4A8CC to $100, trigger $10.1, and $600 displays the corresponding
data.
Execution
results GBK UTF-8
152
Lua Command Manual
This command helps you calculate the CRC value. CRC (Cyclic redundancy check) is used to
verify whether an error has occurred during data transmission. This manual does not cover
further information on CRC. Refer to the data available on the Internet for more information.
The basic syntax includes:
Command Command expression Description
Utility (CRC calculation) util.Crc16Modbus Calculate the CRC value
The following section will explain the command in detail.
Example
description
Return value
ft
crc16 = util.Crc16Modbus("abc123", 6, 0xFFFF)
Create the initial value 0xFFFF in hexadecimal. Calculate the string "abc123" (6
bytes in length) with the initial value to get the CRC value.
For the detailed calculation, refer to the information available on the Internet.
crc16: integer; the result of crc16 calculation
ra
D
153
Lua Command Manual
Example (Utility)
Build the Lua program to conver the data formats, and the value is written to the
memory address $1.
while true do
Convert the str = "abc123"
format with strLen = string.len(str)
Lua initValue = 0xFFFF
crc16 = util.Crc16Modbus(str, strLen, initValue)
mem.inter.Write(1,crc16)
end
Create a Numeric Entry element and set the Write Address to $1.
Create
Numeric
Entry
element
ft
ra
After building the Lua program and creating the elements, compile and download
the project to the HMI.
The Numeric Entry element displays the CRC result calculated from the string
"abc123" and the initial value.
The string for CRC calculation CRC value
Execution
results
D
abc123
154
Lua Command Manual
These commands help you convert the data formats. The commands include:
Command Command expression Description
Convert the integer to a floating-point
Convert convert.IntToFloat
number
(Floating-point number
conversion) Convert the string to a 64-bit
convert.ToNum
floating-point number
The following sections will explain each in detail.
Return value
ft
ret: return 1 on success; return 0 on failure
fVal: the single-precision floating-point number after conversion
155
Lua Command Manual
convert
Build the Lua program to convert the data formats, and the value is written to the
memory address $1.
Convert the while true do
format with
fVal = convert.IntToFloat(0x42F6E666)
Lua
mem.inter.WriteFloat(1,fVal)
end
Create a Numeric Entry element, and set the Write Address to $1 and the Data
Format to Floating.
Create
Numeric
Entry
element
ft
ra
After building the Lua program and creating the elements, compile and download
the project to the HMI.
The Numeric Entry element displays the result of converting 0x42F6E666 to a
floating-point number.
Execution Integer Float
results
0x42F6E666
D
156
Lua Command Manual
These commands help you manage permissions and passwords. The commands include:
Command Command expression Description
account.Add Add permissions account
account.Delete Delete permissions account
account.ChangeName Change permissions account name
account.ChangePassword Change permissions password
Change permissions level of the
account.ChangeLevel
account
account.GetPassword Get user password
Account account.GetLevel Get permissions level
(permissions and
account.GetCurrentLogin Get current login account
password setup)
account.IsExist Check whether the account exists
account.Login Log in to permissions account
account.ResetLockStatus Unlock a locked account
account.ChangeUserExpiredDays Change account expiration
account.ChangePwdExpiredDays Change password expiration
ft account.GetStatus
account.GetLockedList
The following sections will explain each in detail.
157
Lua Command Manual
Example
description
Return value
ft
Change the password of the user “DELTA” to 0101.
158
Lua Command Manual
Return value
ft
ret, name, level = account.GetCurrentLogin()
159
Lua Command Manual
Example
description
Return value
ft
Change the account expiration of the account name "11" to 2 days.
Note: when you log in to this account, the system shows that the account has expired.
ret: return 1 on success; return 0 on failure; return -1 on parameter setting error
160
Lua Command Manual
Example (account)
Create a Numeric Entry element, and set the Write Address to $1 and the User
Security Level to 5.
ft
ra
Create
Numeric
Entry
D
element
161
Lua Command Manual
Example (account)
Use the Lua program to create permissions accounts. Create a user permissions
account when the memory address $10.0 is triggered. The username is
POSHENG, the password is DELTA, and the permission level is 5.
Use Lua if (mem.inter.ReadBit(10,0)==1) then
to create strName = "POSHENG"
permissions
strPassword = "DELTA"
accounts
intLevel = 5
ret = account.Add(strName, strPassword, intLevel)
end
Enter the Lua command shown as follows.
Log in to if (mem.inter.ReadBit(10,1)==1) then
permissions strName = "POSHENG"
account strPassword = "DELTA"
through Lua ret = account.Login(strName, strPassword)
end
Create 2 Maintained Buttons and set the Write Addresses to $10.0 and $10.1.
Create
Maintained
buttons
ft
After building the Lua program and creating the elements, compile and download
the project to the HMI.
You can also log in to the permissions account by triggering $10.1, and then you
can directly use the Numeric Entry elements.
162
Lua Command Manual
4.20 Mail
You need to complete the SMTP related settings before using these commands to call the Mail
Server to send emails or files through the HMI. The commands include:
Command Command expression Description
mail.Status Mail function status
mail.Send Send email
Mail mail.SendFile Send email (including files)
mail.SendAlarm Send email (including alarms)
mail.SendHistory Send email (including history data)
The following sections will explain each in detail.
ft
status = mail.Status()
Description
The email has been successfully delivered
ra
Initial value; no email is being delivered, or the delivery task has just
0
started
Return value -100 Host connection failed
-101 Disconnected
-102 Authentication is required
-103 Authentication failed
-999 Unknown error
163
Lua Command Manual
Return value
-1
-106
-107
-110
-121
-122
ft Invalid parameter
The specified disk is not ready
Cannot open the specified file
The specified file path does not exist
SMTP is not set
An error occurred while the file is being compressed
ra
mail.SendAlarm: send email (including alarms)
Command
mail.SendAlarm
name
Command
result, error = mail.SendAlarm(receiver, subject, content, password)
expression
receiver: string; email recipient
Parameter subject: string; the subject of the email. If you don’t need a subject, set it to nil
definition content: string; email content. If you don’t need any content, set it to nil
password: string; file password. If no password is required, set it to nil
D
result, error = mail.SendAlarm("[email protected]", "test mail subject", "test mail
Example
content", "1234")
Compress the alarm CSV file and set the password to "1234", and then send the
Example
file through email to [email protected] with the subject as “test mail subject” and the
description
content as “test mail content”.
result: integer; 0: email delivery has not yet started; 1: email delivery has started
error: integer
Return value Description
0 No errors
-1 Invalid parameter
Return value
-107 Cannot open the specified file
-121 SMTP is not set
-122 An error occurred while the file is being compressed
-126 No alarm enabled
-127 Failed to export CSV file
164
Lua Command Manual
Return value
0
-1
-107
-121
-128
-129
ft No errors
Invalid parameter
Cannot open the specified file
SMTP is not set
No history buffer enabled
Incorrect history buffer ID
ra
-130 Incorrect range of days
-131 Failed to export history CSV file
-132 Failed to copy history CSV file
-133 No matching files
D
165
Lua Command Manual
Example (mail)
Go to [Main] in the project tree on the left side and create the Lua command
mail.SendFile.
if mem.inter.ReadBit(1000,0)==1 then
disk_id = 2
file_name = "posheng.txt"
ret, fileHandle = file.Open(disk_id, file_name)
sys.Sleep(1000)
result, error = mail.SendFile("[email protected]", "mail subject", "mail
content", 2, "posheng.txt", "1234")
mem.inter.Write(10,result)
if result ~=1 then
mem.inter.WriteAscii(20,error,string.len(error))
end
Write Lua
program mem.inter.WriteBit(1000,0,0)
status = mail.Status()
mem.inter.Write(1,status)
end
Program description:
When $1000.0 is triggered, a file named "posheng.txt" is created. After the file is created
for 1000 ms, the HMI encrypts the file with the password 1234, and then compresses it
into a zip file, and sends it to [email protected] with the email subject as "mail
ft
subject" and the email content as "mail content". Lastly, the HMI writes the result of the
return value to $10. If the return value is not equal to 1 (that is, the delivery failed), the
error code (such as the specified file path does not exist, cannot open the specified file,
or the disk is not ready) is written; to $20. Then, the HMI sets $1000.0 to off, checks the
SMTP connection status information through mail.Status (such as whether the
connection to the SMTP Server is successful, or whether the authentication is
successful), and returns the data to $1.
ra
Go to [Main] in the project tree on the left side and create the Lua command
mail.SendAlarm.
if mem.inter.ReadBit(1000,1)==1 then
result, error = mail.SendAlarm("[email protected]", "mail subject", "mail
content", "1234")
mem.inter.Write(10,result)
if result ~=1 then
mem.inter.WriteAscii(20,error,string.len(error))
end
Build Lua status = mail.Status()
D
program mem.inter.Write(1,status)
mem.inter.WriteBit(1000,1,0)
end
Program description:
When $1000.1 is triggered, the HMI encrypts the alarm data (.CSV) file with the
password 1234, then compresses it into a zip file, and sends it to
[email protected] with the email subject as "mail subject" and the email content
as "mail content". Then, the result of the return value is written to $10. If the return value
is not equal to 1 (that is, the delivery failed), the HMI writes the error code to $20. Lastly,
sets $1000.1 to off, checks the SMTP connection status information through mail.Status,
and returns the data to $1.
166
Lua Command Manual
Example (mail)
Create 3 Maintained buttons and set the Write Addresses to $1000.0, $1000.1,
and $2000.0.
Create 2 Numeric Entry elements and set the Write Addresses to $1 and $10.
Create a Character Entry element and set the Write Address to $20.
Create
elements
Set alarm
ft
Go to [Options] > [Alarm Settings], and set the alarm message and set the trigger
address to $2000.0.
ra
Note: alarm1 is triggered when $2000.0 is triggered.
Configure
SMTP Set the SMTP function. For details, see CH27 of the DOPSoft User Manual.
function
D
167
Lua Command Manual
Example (mail)
After building the Lua program and creating the elements, compile and download
the project to the HMI.
Click Sendfile, and you can receive the sent file.
Execution
results
ft
ra
D
168
Lua Command Manual
Example (mail)
Click $2000.0 to trigger the alarm, and click Sendalarm to send an email with "mail
subject" as the subject. Then, you can receive the CSV data of the alarm in the
mailbox.
Execution
results
ft
ra
D
169
Lua Command Manual
These commands help you draw on the HMI. The commands include:
Command Command expression Description
draw.Point Draw (point)
draw.Line Draw (line)
Draw draw.Rect Draw (rectangle)
(drawing function) draw.Ellipse Draw (ellipse)
draw.Clear Clear the drawing
draw.SetAntialiasing Enable/disable anti-aliasing
The following sections will explain each in detail.
Example
Example
description
Return value
ft
color: integer; the RGB565 value is an integer ranges from 0 to 65535
ret = draw.Point(1, 1, 100)
Draw a point on the coordinate (1, 1) of the HMI with the color of RGB100 (black).
170
Lua Command Manual
Parameter
definition
Example
ft
x: integer; the x coordinate of the center of the ellipse
y: integer; the y coordinate of the center of the ellipse
w: integer; the width of the ellipse
h: integer; the height of the ellipse
color: integer; the RGB565 value is an integer ranges from 0 to 65535
ret = draw.Ellipse(50, 50, 100, 100, 53388)
ra
Example Draw an ellipse on the coordinate (50, 50) of the HMI with the width of 100, the
description height of 100, and the color of RGB53388 (pink).
Return value ret: return 1 on success; return 0 on failure
171
Lua Command Manual
ft
ra
D
172