0% found this document useful (0 votes)
261 views

Lua Command Manual-Draft Version-20211124

The document provides an overview of the Lua programming language and its basic syntax used for writing Lua scripts. It covers topics such as Lua commands, comments, identifiers, variables, data types, assignments, and control structures. The manual serves as a reference for users to learn about the various Lua commands and functions available to program Delta HMI devices.

Uploaded by

Aydın Zeki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
261 views

Lua Command Manual-Draft Version-20211124

The document provides an overview of the Lua programming language and its basic syntax used for writing Lua scripts. It covers topics such as Lua commands, comments, identifiers, variables, data types, assignments, and control structures. The manual serves as a reference for users to learn about the various Lua commands and functions available to program Delta HMI devices.

Uploaded by

Aydın Zeki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 172

Lua Command Manual

1. Introduction to Lua ····························································································2


2. Basic Lua programming syntax ············································································3
3. Lua command list ······························································································5
4. Detailed explanation of Lua commands ································································12
4.1 Basic syntax ·····························································································13
4.1.1 Type: bool, number, string, nil (data type) ·················································14
4.1.2 Type: table, array (matrix operations) ·······················································17
4.1.3 if…then…else…elseif…end, and or not (comparison) ·································20
4.1.4 for var=1, 3 do… end (for loop) ······························································21
4.1.5 while break, repeat until (while, repeat loop) ··············································24
4.1.6 +-*/%^ (mathematical operations) ···························································26
4.1.7
4.1.8
4.2
4.3

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.

 Lua editor window


Click [Project]  [Program]  [Main], then you can start writing Lua programs, as shown in
Figure 1.1.
When writing a Lua program, you can use the program example assistant to quickly get familiar
with commands, or move the mouse pointer to the commands to see its description. Refer to
Figure 1.2.

ft
ra
Figure 1.1 The Lua programming interface
D

Figure 1.2 The Lua program example assistant and description

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.

Figure 1.3 Quick input of Lua commands

2. Basic Lua programming syntax


The following is a summary of the basic Lua programming syntax.
Item

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

Double-precision real floating-point value


ra
Character string; represented by a pair of double quotation
string
marks or single quotation marks
D

4
Lua Command Manual

3. Lua command list


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
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
mem.inter.ReadFloat
(unit: Float)
mem.inter.ReadBit Read the bit of the internal memory
Read the value of the internal memory

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

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)
Read the bit of the external memory
link.ReadBit
address
Read the string of the external memory
link.ReadAscii
address
Read the value of the external memory
link.ReadDouble (unit: 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)
Write the value to the external memory
link.WriteFloat
(unit: Float)
link.WriteBit Write the bit to the external memory

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

Command Command expression Description


file.Seek Set the pointer
file.GetPos Get the current pointer position
file.GetError Check file
file.Close Close file
file.List Get a list of the files stored in the HMI
file.Export Export file
file.Delete Delete file
File
file.DeleteDir Delete directory
(read/write/export/
delete/print files) file.ToPDF Convert the file to PDF
file.ToPrinter Print file
Get a list of the files stored in the
file.ListExternal
external device
file.Exist Check if the file exists
file.PDFToPrinter Print PDF file
file.Copy Copy file
file.Move Move file
fileslot.Read Read the fileslot file

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

Command Command expression Description


Split the value into an integer and a
math.modf
decimal
math.pi Pi (π)
Math
(mathematical math.pow Get the power value
operations) math.randomseed Random seed
math.random Get a random value
math.sqrt Get the square root value
recipe.GetCurRcpNoIndex Get the current recipe number index
recipe.GetCurRcpGIndex Get the current recipe group index
Get the value of the specified recipe
recipe.GetRcpWord
address (Word)
Get the value of the specified recipe
recipe.GetRcpDWord
address (Double Word)
Get the value of the specified recipe
recipe.GetRcpFloat
address (Float)
Get the index name of the current
recipe.GetCurEnRcpNoName
enhanced recipe number
Get the index name of the current
recipe.GetCurEnRcpGName
enhanced recipe group

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

Command Command expression Description


Set parameters to the enhanced recipe
recipe.SetEnRcpDouble address (double-precision floating-point
number)
Recipe
Get the value of the specified enhanced
recipe.GetEnRcpDouble recipe address (double-precision
floating-point number)
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
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

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

Command Command expression Description


System library Get the disk space of the external
sys.GetDiskSpace
(system parameters) storage
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
communication com.StationCheck station number to check whether the
(COM communication is successful
communication) com.Close Close the communication port
Select the communication parameters
com.CheckAlive to check whether the communication is
successful
com.StationOn Station On
com.StationOff Station Off
com.GetStatus Get the COM port status
tcp.Open Open the TCP network communication

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

Command Command expression Description


account.GetLevel Get permissions level
account.GetCurrentLogin Get current login account
account.IsExist Check whether the account exists

Account account.Login Log in to permissions account


(permissions and account.ResetLockStatus Unlock a locked account
password setup) account.ChangeUserExpiredDays Change account expiration
account.ChangePwdExpiredDays Change password expiration
account.GetStatus Get account status
account.GetLockedList Get a list of locked accounts
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)
draw.Point Draw (point)
draw.Line Draw (line)
Draw draw.Rect Draw (rectangle)
(drawing function)

ft
draw.Ellipse
draw.Clear
draw.SetAntialiasing
Draw (ellipse)
Clear the drawing
Enable/disable anti-aliasing
ra
D

11
Lua Command Manual

4. Detailed explanation of Lua commands


Lua commands include:
 Basic syntax
 Internal memory - $
 Static memory - $M
 External link (external memory)
 File (read/write/export/delete/print files)
 FileSlot (file access)
 FTP Client (FTP transfer function)
 Math (mathematical operations)
 Recipe
 Screen (screen control)
 String (string operations)
 System library (system parameters)





TCP communication
UDP communication

ft
Serial port communication (COM communication)

Text encoding (encoding format change)


Utility (CRC calculation)
ra
 Convert (Floating-point number conversion)
 Account (permissions and password setup)
 Mail
 Draw (drawing function)

The following chapters will introduce each type of Lua command.


D

12
Lua Command Manual

4.1 Basic syntax

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

The following sections will explain each in detail.

ft
ra
D

13
Lua Command Manual

4.1.1 Type: bool, number, string, nil (data type)

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

 Set value_bool = true, value_integer = 11, value_double = 13.6,

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

4.1.2 Type: table, array (matrix operations)

These commands help you calculate and design the matrix. The following sections will explain
each in detail.

The following tables are the lists for array commands.

 { }: 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

 table.insert: insert elements into the array


Command
table.insert
name
Command
table.insert(myTable, [pos, ]value)
expression
myTable: table
Parameter
pos: the position for the element to be inserted (this parameter is not mandatory)
definition
value: basic type
t1 = {1, 3, "four"}
table.insert(t1, 2, "two")
Example
t2 = {1, 3, "four"}
table.insert(t2, "five")
Insert the string two into the second position specified in the t array.
Example t1 = {1, "two", 3, "four"}
description If no position is specified, add the string five to the end of the t array.
t2 = {1, 3, "four", "five"}
Return value No return value

 table.remove: remove elements


Command
table.remove
name
Command
expression
Parameter
definition
myTable: table

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

 table.sort: sort the array


Command
table.sort
name
Command
table.sort(myTable, compareFunc)
expression
Parameter myTable: table
definition compareFunc: function
t = {3, 2, 5, 1, 4}
Example
table.sort (t, function(a,b) return a>b end)
Example Sort values in the array from large to small, and put the values into the array.
description t = {5 ,4 ,3 ,2 ,1}
Return value No return value

ft
ra
D

19
Lua Command Manual

4.1.3 if…then…else…elseif…end, and or not (comparison)

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 Condition

Bit on $1.0 Bit on $2.0 Bit off $1.0 and $2.0

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

4.1.5 while break, repeat until (while, repeat loop)

These commands help you design the while and repeat loops in Lua. The following sections will
explain each in detail.

 while...end: execute the loop when...


Command
while…end
name
while [condition1] do
Command
-- [code block 1]
expression
end
Parameter
No parameters
definition
while i ~= 5 do
i = i+1
If i > 100 then
Example
break
end
end


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: repeat executing the loop until...

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

Example (while, repeat)


 Build the Lua program as follows:
while true do
if (mem.inter.ReadBit(10,0)==1) then
v1 = mem.inter.Read(20)
while v1~=10 do
v1 = v1 + 1
sys.Sleep(100)
mem.inter.Write(20, v1)
if v1>=10 then
em.inter.WriteBit(10,0,0)
break
end
end
end
if (mem.inter.Read(100,0)==1) then
repeat
v1 = v1 - 1
sys.Sleep(100)
mem.inter.Write(20, v1)
until( v1 ==0 )

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

Example (while, repeat)


 After building the Lua program and creating the elements, compile and
download the project to the HMI.
 Trigger $10.0 and execute v1 = v1 + 1. Stop executing the loop when v1 is
greater than or equal to 10, then close $10.0.
On

Execution
results

 Trigger $100.0, execute v1 = v1-1 until v1 = 0, and close $100.0.

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

Mathematical * Var1=Var2*Var3 Multiplication


operations / Var1=Var2/Var3 Division

% Var1=Var2%Var3 Remainder operation

^ Var1=Var2^Var3 Involution (power) operation

26
Lua Command Manual

4.1.7 function, call function, require return (flow control)

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.

 function: create a function


Command
function
name
function [function name 1]
Command -- [code block 1]
expression return [result 1]
end
Parameter
No parameters
definition
function Add (a, b)
Example return a+b, a*b
end
Example
Create an Add(a, b) command and return the results of a+b and a*b.
description
Return value No return value

 require: load the module


Command
name
Command
expression
require

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

Example (function, call function)


 In the subprogram interface, create a Lua subprogram.
F(c)=c*9/5+32 means converting Celsius to Fahrenheit.

=============================================
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

In the main program, use the require command to call the


subprogram filename. Now, you can directly use F(c) and K(c) to
execute F(c)=c*9/5+32 and K(c)=c+273.15, and write the results to
ra
$200 and $300 respectively.
while true do
require"Fahrenheit"
Build Lua main require"Kelvin"
program c= mem.inter.Read(100)
K_temperature=K(c)
F_temperature=F(c)
mem.inter.Write(200, K_temperature)
mem.inter.Write(300, F_temperature)
end
D
Create Numeric Entry  Create 3 Numeric Entry elements with the Write Addresses as $100,
elements $200, and $300.
 After the building the Lua program and creating the elements,
compile and download the project to the HMI.
 Enter 30 degrees Celsius for $100, then $200 and $300 each
displays the temperature in Kelvin and Fahrenheit.

Execution results

Celsius Kelvin Temperature Fahrenheit

28
Lua Command Manual

4.1.8 logic: xor and or not lshift rshift (logical operations)

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

Logical operation list


Operand 1 0 1 0 1
D
Operand 2 0 0 1 1
XOR 0 1 1 0
AND 0 0 0 1
OR 0 1 1 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

4.2 Internal memory - $

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)

mem.inter.ReadBit Read the bit of the internal memory

Read the value of the internal memory


mem.inter.ReadDouble
(unit: double-precision floating-point numbers)

Internal memory - $ Write the value to the internal memory


mem.inter.Write
(unit: Word)
Write the value to the internal memory
mem.inter.WriteDW
(unit: Double Word)
Write the value to the internal memory
mem.inter.WriteFloat

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.Read: read the value of the internal memory (unit: Word)


Command
mem.inter.Read
name
Command
Value = mem.inter.Read(index, [value_format])
expression
index: integer; memory index with the range of 0 to 199999
Parameter
value_format: format string. Fill in "signed" for signed numbers; this parameter is not
D
definition
mandatory.
Example v1 = mem.inter.Read(0)
Example
Read the value of $0 in units of Word.
description
Return value v1: 0x0000 to 0xFFFF; return 0 on failure

 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

 mem.inter.ReadFloat: read the value of the internal memory (unit: Float)


Command
mem.inter.ReadFloat
name
Command
Value = mem.inter.ReadFloat(index)
expression
Parameter
index: integer; memory index with the range of 0 to 199998
definition
Example f1 = mem.inter.ReadFloat(0)
Example
Read the value of $0 in units of Float.
description
Return value f1: single-precision floating-point value

 mem.inter.ReadBit: read the bit of the internal memory


Command
mem.inter.ReadBit
name
Command
Result = mem.inter.ReadBit(index, bit)
expression
Parameter index: integer; memory index with the range of 0 to 199999
definition bit: integer; BIT index with the range of 0 to 15
Example b1 = mem.inter.ReadBit(0, 15)


Example
description
Return value
Read $0.15 bit.

ft
b1: integer; 1 or 0; return 0 on failure

mem.inter.ReadDouble: read the value of the internal memory


(unit: double-precision floating-point numbers)
ra
Command
mem.inter.ReadDouble
name
Command
Result = mem.inter.ReadDouble(index)
expression
Parameter
index: integer; memory index with the range of 0 to 199996
definition
Example Q1 = mem.inter.ReadDouble(0)
Example
Read the value of $0 in units of double-precision floating-point numbers.
description
D
Return value Q1: double-precision floating-point numbers

 mem.inter.Write: write the value to the internal memory (unit: Word)


Command
mem.inter.Write
name
Command
Result = mem.inter.Write(index, value)
expression
Parameter index: integer; memory index with the range of 0 to 199999
definition value: decimal integer
Example result = mem.inter.Write(0, 123)
Example
Write the value 123 to $0 in units of Word.
description
Return value Result: return 1 on success; 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

 mem.inter.WriteFloat: write the value to the internal memory (unit: Float)


Command
mem.inter.WriteFloat
name
Command
Result = mem.inter.WriteFloat(index, float_value)
expression
Parameter index: integer; memory index with the range of 0 to 199998
definition float_value: single-precision floating-point value
Example result = mem.inter.WriteFloat(0, 1.23)
Example
description
Return value

ft
Write the value 1.23 to $0 in units of Float.

Result: return 1 on success; return 0 on failure

mem.inter.WriteBit: write the bit to the internal memory


ra
Command
mem.inter.WriteBit
name
Command
Result = mem.inter.WriteBit(index, bit, logic)
expression
index: integer; memory index with the range of 0 to 199999
Parameter
bit: integer; BIT index with the range range of 0 to 15
definition
logic: integer; 1 or 0
Example result = mem.inter.WriteBit(0, 0, 1)
Example
Set the $0.0 bit to On.
description
D
Return value Result: return 1 on success; return 0 on failure

 mem.inter.WriteDouble: write the value to the internal memory


(unit: double-precision floating-point numbers)
Command
mem.inter.WriteDouble
name
Command
Result = mem.inter.WriteDouble(index, Double_value)
expression
Parameter index: integer; memory index with the range of 0 to 199996
definition Double_value: double-precision floating-point value
Example result = mem.inter.WriteDouble(10, 123456789.99)
Example
Write 123456789.99 to $10 in units of double-precision floating-point numbers.
description
Return value Result: return 1 on success; return 0 on failure

33
Lua Command Manual

 mem.inter.ReadAscii: read the string of the internal memory


Command
mem.inter.ReadAscii
name
Command
ascii_string = mem.inter.ReadAscii(start_index, string_len)
expression
Parameter start_index: integer; memory index with the range of 0 to 199999
definition string_len: integer; ascii number (bytes)
Example ascii_string = mem.inter.ReadAscii(0, 4)
Example
Read the $0 string with the length of 4 bytes.
description
Return value ascii_string: ascii string

 mem.inter.WriteAscii: write the string to the internal memory


Command
mem.inter.WriteAscii
name
Command
Result = mem.inter.WriteAscii(start_index, asci_string, string_len)
expression
start_index: integer; memory index with the range of 0 to 199999
Parameter
asci_string: ascii string
definition
string_len: integer; ascii number (bytes)
Example
Example
description
Return value

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

Command Example Execution results

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

4.3 Static memory - $M

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.Read: read the value of the static memory (unit: Word)


Command
mem.static.Read
name
Command
Value = mem.static.Read(index, [value_format])
expression
index: integer; memory index with the range of 0 to 1023
Parameter
definition value_format: format string. Fill in "signed" for signed numbers; this parameter is not
D
mandatory.
Example v1 = mem.static.Read(0)
Example
Read the value of $M0 in units of Word.
description
Return value v1: 0x0000 to 0xFFFF; return 0 on failure

 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

 mem.static.ReadFloat: read the value of the static memory (unit: Float)


Command
mem.static.ReadFloat
name
Command
Value = mem.static.ReadFloat(index)
expression
Parameter
index: integer; memory index with the range of 0 to 1022
definition
Example f1 = mem.static.ReadFloat(0)
Example
Read the value of $M0 in units of Float.
description
Return value f1: single-precision floating-point value

 mem.static.ReadBit: read the bit of the static memory


Command
mem.static.ReadBit
name
Command
Result = mem.static.ReadBit(index, bit)
expression
Parameter index: integer; memory index with the range of 0 to 1023
definition bit: integer; BIT index with the range of 0 to 15
Example b1 = mem.static.ReadBit(0, 15)
Example
description
Return value

ft
Read the bit of the static memory $M0.15.

b1: integer; 1 or 0

mem.static.ReadDouble: read the value of the static memory


(unit: double-precision floating-point numbers)
ra
Command
mem.static.ReadDouble
name
Command
Result = mem.static.ReadDouble(index)
expression
Parameter
index: integer; memory index with the range of 0 to 1020
definition
Example Q1 = mem.static.ReadDouble(0)
Example
Read the value of $M0 in units of double-precision floating-point numbers.
description
D
Return value Q1: double-precision floating-point numbers

 mem.static.ReadAscii: read the string of the static memory


Command
mem.static.ReadAscii
name
Command
ascii_string = mem.static.ReadAscii(start_index, string_len)
expression
Parameter start_index: integer; memory index with the range of 0 to 1023
definition string_len: integer; ascii number (bytes)
Example ascii_string = mem.static.ReadAscii(0, 4)
Example
Read the static memory address $M0 with the length of 4 bytes (in string).
description
Return value ascii_string: ascii string

37
Lua Command Manual

 mem.static.Write: write the value to the static memory (unit: Word)


Command
mem.static.Write
name
Command
Result = mem.static.Write(index, value)
expression
Parameter index: integer; memory index with the range of 0 to 1023
definition value: integer
Example result = mem.static.Write(0, 123)
Example
Write the value 123 to $M0 in units of Word.
description
Return value Result: return 1 on success; return 0 on failure

 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.

Result: return 1 on success; return 0 on failure

mem.static.WriteFloat: write the value to the static memory (unit: Float)


ra
Command
mem.static.WriteFloat
name
Command
Result = mem.static.WriteFloat(index, value_float)
expression
Parameter index: integer; memory index with the range of 0 to 1023
definition value_float: single-precision floating-point value
Example result = mem.static.WriteFloat(0, 1.23)
Example
Write the value 1.23 to $M0 in units of Float.
description
Return value Result: return 1 on success; return 0 on failure
D
 mem.static.WriteBit: write the bit to the static memory
Command
mem.static.WriteBit
name
Command
Result = mem.static.WriteBit(index, bit, logic)
expression
index: integer; memory index with the range of 0 to 1023
Parameter
bit: integer; BIT index with the range of 0 to 15
definition
logic: integer; 1 or 0
Example result = mem.static.WriteBit(0, 0, 1)
Example
Set the $M0.0 bit of the static memory to On.
description
Return value Result: return 1 on success; return 0 on failure

38
Lua Command Manual

 mem.static.WriteDouble: write the value to the static memory


(unit: double-precision floating-point numbers)
Command
mem.static.WriteDouble
name
Command
Result = mem.static.WriteDouble(index, Double_value)
expression
Parameter index: integer; memory index with the range of 0 to 1020
definition Double_value: double-precision floating-point numbers
Example result = mem.static.WriteDouble(10, 123456789.99)
Example
Write 123456789.99 to $M10 in units of double-precision floating-point numbers.
description
Return value Result: return 1 on success; return 0 on failure

 mem.static.WriteAscii: write the string to the static memory


Command
mem.static.WriteAscii
name
Command
Result = mem.static.WriteAscii(start_index, ascii_string, string_len)
expression
start_index: integer; memory index with the range of 0 to 1023
Parameter
ascii_string: ascii string
definition

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

mem.static.ReadDW Description of command d1=d1+1


mem.static.WriteDW Read the static memory address $M100
as d1. After adding 1 to d1, write the $M100
result to the static memory address
$M100. The unit of reading and writing is
Double Word.

39
Lua Command Manual

Command Example Execution results

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

mem.static.ReadAscii Description of command


mem.static.WriteAscii Read the string of the static memory
address $M200 as str2 with the string $M300
length of 5. Then, write the string str2 to
the static memory address $M300 with
the string length of 5.
Note: the unit of string length is byte.
D
Command Example Execution results
while true do
q1 = mem.static.ReadDouble (0)
mem.static.WriteDouble (10, q1)
end
$M0
mem.static.ReadDouble Description of command
mem.static.WriteDouble
After reading the static memory address
$M0 as q1, write q1 to the static memory
$M10
address $M10. The unit of reading and
writing is double-precision floating-point
number.

40
Lua Command Manual

4.4 External link (external memory)

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.Read: read the value of the external memory (unit: Word)


Command
link.Read
name
Command
Result = link.Read(addr, [value_format])
expression
addr: string, memory address, for example: "{Link2}1@D1"
Parameter
definition value_format: format string. Fill in "signed" for signed numbers; this parameter is not
mandatory.
Example v1 = link.Read("{Link2}1@D1")
Read the values of the following external memory in units of Word:
Example the controller memory which link number is 2, the station number is 1, and the
description
communication address is D1.
Return value v1: integer

 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

 link.ReadFloat: read the value of the external memory (unit: Float)


Command
link.ReadFloat
name
Command
Result = link.ReadFloat(addr)
expression
Parameter
addr: string; memory address,such as "{Link2}1@D1"
definition
Example f1 = link.ReadFloat("{Link2}1@D1")
D
Read the values of the following external memory in units of Float:
Example
the controller memory which link number is 2, the station number is 1, and the
description
communication address is D1.
Return value f1: single-precision floating-point value

 link.ReadBit: read the bit of the external memory address


Command
link.ReadBit
name
Command
Result = link.ReadBit(addr)
expression
Parameter
addr: string; memory address, such as "{Link2}1@M100"
definition
Example b = link.ReadBit("{Link2}1@M1")
Read the bit of the following external memory address:
Example
the controller memory which link number is 2, the station number is 1, and the
description
communication address is M1.
Return value v1: integer; 1 or 0

42
Lua Command Manual

ft
ra
D

43
Lua Command Manual

 link.ReadAscii: read the string of the external memory address


Command
link.ReadAscii
name
Command
asciiString, result, errMsg = link.ReadAscii(addr, string_len)
expression
Parameter addr: string; memory address, for example: "{Link2}1@D1"
definition string_len: integer; number of ascii bytes
Example ascii, ret, errMsg = link.ReadAscii("{Link2}1@D1", 20)
Read the string of the following external memory address in units of 20 bytes:
Example
the controller memory which link number is 2, the station number is 1, and the
description
communication address is D1.
ascii: ascii string
Return value ret: 1: success; 0: failure
errMsg: string; error message

 link.ReadDouble: read the value of the external memory


(unit: double-precision floating-point numbers; 64 bits)
Command
link.ReadDouble
name
Command
expression
Parameter
definition
Example

Example

ft
Result = link.ReadDouble(addr)

addr: string; memory address, such as "{Link2}1@D1"

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.Write: write the value to the external memory (unit: Word)


Command
link.Write
name
Command
Result = link.Write(addr, value_word)
expression
Parameter addr: string; memory address, such as "{Link2}1@D0"
D
definition value_word: integer
Example Result = link.Write("{Link2}1@D1", 123)
Write the value 123 in units of Word to the following external memory:
Example
the controller memory which link number is 2, the station number is 1, and the
description
communication address is D0.
Return value Result: return 1 on success; return 0 on failure

 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

 link.WriteFloat: write the value to the external memory (unit: Float)


Command
link.WriteFloat
name
Command
Result = link.WriteFloat(addr, value_float)
expression
Parameter addr: string; memory address, such as "{Link2}1@D0"
definition value_float: single-precision floating-point number
Example Result = link.WriteFloat("{Link2}1@D1", 1.23)
Write the value 1.23 in units of Float to the following external memory:
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

 link.WriteBit: write the bit to the external memory


Command
link.WriteBit
name
Command
Result = link.WriteBit(addr, value_bit)
expression
Parameter addr: string; memory address, such as "{Link2}1@M100"
definition value_bit: integer; 1 or 0
Example

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

 link.WriteDouble: write the value to the external memory


(unit: double-precision floating-point numbers; 64 bits)
Command
link.WriteDouble
name
Command
Result = link.WriteDouble(addr, value_double)
expression
Parameter addr: string; memory address, such as "{Link2}1@D0"
definition value_double: double-precision floating-point value
Example Result = link.WriteDouble("{Link2}1@D1", 1234567.89)
Write the value 1234567.89 in units of double-precision floating-point numbers to the
Example following 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

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

ret = link.SetDefaultStationNo(link_number, station_number)


ra
link_number: integer; communication serial port number. 0: COM1; 1: COM2, and so
Parameter on.
definition
station_number: integer: 1 to 255
Example ret = link.SetDefaultStationNo(0, 1)
Change the default PLC Station number of COM1 communication to 1 dynamically.
D
Example
description

Return value ret: return 1 on success; return -1 on parameter error

48
Lua Command Manual

 link.SetHMIStationNo: set the HMI Slave station number (Modbus Slave)


Command
link.SetHMIStationNo
name
Command
ret = link.SetHMIStationNo(link_number, station_number)
expression
link_number: integer; communication serial port number. 0: COM1; 1: COM2, and so
Parameter on.
definition
station_number: integer: 1 to 255
ret = link.SetHMIStationNo(0, 1)
Example Note: you can only use this command when the HMI is a Modbus Slave, so you must set the
communication parameters to Modbus ASCII/RTU (Slave).
When you change the COM1 HMI Station number to 1, set the communication
parameters to Modbus ASCII/RTU (Slave).

Example
description

ft
ra
Return value ret: return 1 on success; return -1 on parameter error
D

49
Lua Command Manual

 link.CODESYSAppDownload: use the network communication to download the CODESYS


program to the PLC through HMI
Command
link.CODESYSAppDownload
name
Command ret, errDesc = link.CODESYSAppDownload(connMethodID, address, diskID,
expression appFile, showMsgBox, username, password)
connMethodID: integer; connection code. 0: PLC IP address; 1: PLC logical address
address: string. PLC IP address: "192.168.0.1"; PLC IP logical address: "002D"
diskID: integer; disk ID. 2: USB drive; 8: AX8 controller
appFile: string; application filename, such as "Application.app"
Parameter showMsgBox: integer. 1: an error window appears when the download fails;
definition 0: an error window will not be displayedwhen the download fails
username: string; authenticated username: "Admin"; blank string "" means no
authentication
password: string; authenticated user password: "12345"; blank string "" means no
authentication
connMethodID = 0
address = "192.168.123.23"
diskID = 2
appFile = "AddressOffset.app"
Example showMsgBox = 1

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

 link.CODESYSAppUpload: use the network communication to upload the CODESYS


program to the USB storage device connected to the HMI
Command
link.CODESYSAppUpload
name
Command ret, errDesc = link.CODESYSAppUpload(connMethodID, address, diskID, appFile,
expression showMsgBox, username, password)
connMethodID: integer; connection code. 0: PLC IP address; 1: PLC logical address
address: string. PLC IP address: "192.168.0.1"; PLC IP logical address: "002D"
diskID: integer; disk ID. 2: USB drive; 8: AX8 controller
appFile: string; application filename, such as "Application.app"
Parameter showMsgBox: integer. 1: an error window apperas when the download fails; 0: an
definition error window does not appear when the download fails
username: string; authenticated username: "Admin"; blank string "" means no
authentication
password: string; authenticated user password: "12345"; blank string "" means no
authentication
connMethodID = 0
address = "192.168.123.23"
diskID = 2
appFile = "DELTA.app"
Example showMsgBox = 1

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

Command Example Execution results

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.

Command Example Execution results

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

Command Example Execution results

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

link.ReadFloat Description of command


f1=f1*2.5
link.WriteFloat Write the floating-point number 1.1 to the
external memory address {Link2}1@D1and
{Link2}1@D3
read the external memory address
{Link2}1@D1 floating-point number as f1.
Multiply f1 by 2.5, and write the result to the
external memory address {Link2}1@D3. The
unit of reading and writing is floating-point
number.
Command Example Execution results

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

Copy the data of $1 to {Link2}1@D1 with the $10~$15

length of 6, and write the return value to


$100. Then, copy the data of {Link2}1@D1 to
$1, and write the return value to $200.

52
Lua Command Manual

4.5 File (read/write/export/delete/print files)

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.

 file.Open: create/open file


Command
file.Open
name
D
Command
ret, fileHandle = file.Open(disk_id, file_name, add_utf8_id)
expression
disk_id: integer; 0: internal memory of HMI; 2: USB drive; 3: SD card
Parameter
file_name: string; filename
definition
add_utf8_id: 1: add utf-8 identifiers; others: do not add additional identifiers
Example ret, fileHandle = file.Open(2, "myFile001.txt")
Open or create a file named "myFile001.txt" in the external USB storage device .
Example fileHandle is the file data to be used for subsequent commands.
description Note: if the storage device contains the file you specify, it is opened; if not, a new file with the
name you specify is created.
ret: return 1 on success; return 0 on failure
fileHandle: integer; file pointer
Return value Description
Return value -1 Invalid parameter
-106 The specified disk is not ready
-107 Cannot open the specified file
-136 Invalid file path

53
Lua Command Manual

 file.Read: read file data


Command
file.Read
name
Command
ret, data = file.Read(fileHandle, len)
expression
Parameter fileHandle: integer; file pointer
definition len: integer; character length
ret, data = file.Read(fileHandle, 10)
Example
Note: The fileHandle parameter is generated by the file.Open command.
With the current pointer position as the initial address, read the fileHandle file data
with the length of 10 bytes. After reading, the current pointer address is changed to
Example
[initial address + read length].
description
Note: if you need to read a specific pointer address, you can use the file.Seek command to change
the current pointer address.

ret: integer. Success: the length of the read string; failure: -1


Return value
data: character set. Success: character content; failure: nil

 file.ReadLine: read file (unit: one line)


Command
file.ReadLine
name
Command
expression
Parameter
definition

Example

ft
ret, data = file.ReadLine(fileHandle)

fileHandle: integer; file pointer

ret, data = file.ReadLine(fileHandle)


Note: the fileHandle parameter is generated by the file.Open command.
ra
With the current pointer position as the initial address, read the file fileHandle data in
the unit of one line.
Example Note:
description 1. A line ends at a carriage return symbol ("/r") or newline symbol ("/n").
2. You can read the file with the third-party software Notepad++ and check for the carriage
return and newline symbols.
ret: integer; success: the length of the string read; failure: return a negative number
Return value Description
Return value -1 Invalid parameter
-102 Read failure
D
data: character set; success: character content; failure: nil

 file.Write: write to file


Command
file.Write
name
Command
ret = file.Write(fileHandle, buffer, len)
expression
fileHandle: integer; file pointer
Parameter
buffer: Ascii character set; text content (UTF-8 format)
definition
len: integer; length of written characters
ret = file.Write(fileHandle, "posheng", 6)
Example
Note: The fileHandle parameter is generated by the file.Open command.
With the current pointer position as the initial address, write the string posheng to the
Example fileHandle file with the length of 6 bytes.
description Note: if you need to read a specific pointer address, you can use the file.Seek command to change
the current pointer address.

Return value ret: integer; success: the length of the written string; failure: -1

54
Lua Command Manual

 file.Length: read the file length


Command
file.Length
name
Command
ret, len = file.Length(fileHandle)
expression
Parameter
fileHandle: integer; file pointer
definition
ret, len = file.Length(fileHandle)
Example
Note: the fileHandle parameter is generated by the file.Open command.
Example
Read the length of the file fileHandle in Bytes.
description

ret: success: 1; failure: 0


Return value
len: success: file length; failure: less than 0

 file.GetLineCount: read the total line count in the file


Command
file.GetLineCount
name
Command
ret, lineCount = file.GetLineCount(fileHandle)
expression
Parameter
definition

Example

Example
description

ft
fileHandle: integer; file pointer

ret, lineCount = file.GetLineCount(fileHandle)


Note: the fileHandle parameter is generated by the file.Open command
Read the total number of lines in the file fileHandle.
Note:
1. A line ends at a carriage return symbol ("/r") or newline symbol ("/n").
ra
2. You can read the file with the third-party software Notepad++ and check for the carriage
return and newline symbols.
ret: success: 1; failure: 0
Return value
lineCount: Success: the total number of lines; failure: -1

 file.Seek: set the pointer


Command
file.Seek
name
Command
ret = file.Seek(fileHandle, offset, origin)
expression
D
fileHandle: integer; file pointer
Parameter offset: integer; offset number of the file
definition origin: the starting reference position of the offset; 0: the start position of the file,
1: current position, 2: the end position of the file
ret = file.Seek(fileHandle, 5, 0)
Example
Note: the fileHandle parameter is generated by the file.Open command.
Example
Set the current pointer to a position offset by 5 bytes from the beginning of the file.
description
Return value ret: success: 1; other: invalid parameter

55
Lua Command Manual

 file.GetPos: get the current pointer position


Command
file.GetPos
name
Command
ret, pos = file.GetPos(fileHandle)
expression
Parameter
fileHandle: integer; file pointer
definition
ret, pos = file.GetPos(fileHandle)
Example
Note: the fileHandle parameter is generated by the file.Open command.
Example
Get the current pointer position.
description

ret: integer; success: 1; failure: -1


Return value
pos: integer; file pointer position

 file.GetError: check file


Command
file.GetError
name
Command
ret = file.GetError(fileHandle)
expression
Parameter
definition

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.

ret: integer; success: 1; failure: negative number


ra
Return value Description
Return value -1 Invalid
-117 Failed to read data
-118 Failed to access file

 file.Close: close file


Command
file.Close
name
Command
D
file.Close(fileHandle)
expression
Parameter
fileHandle: integer; file pointer
definition
file.Close(fileHandle)
Example
Note: the fileHandle parameter is generated by the file.Open command.
Example
Close the fileHandle file.
description
Return value No return value

56
Lua Command Manual

 file.List: get a list of the files stored in the HMI


Command
file.List
name
Command
ret, nameList = file.List()
expression
Parameter
No parameters
definition
Example ret, nameList = file.List()
Example Get the list of files stored in the HMI; nameList is the list, nameList[1] is the first
description filename, nameList[2] is the second filename, and so on.
ret: success: 1; failure or no file: 0
Return value
nameList: matrix table; success: filename list; failure: nil

 file.Export: export file


Command
file.Export
name
Command
ret, errCode = file.Export(name, disk_id)
expression
Parameter name: string; filename
definition disk_id: integer; 2: USB; 3: SD card
Example
Example
description

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

 file.Delete: delete file


Command
file.Delete
D
name
Command
ret, err, errText = file.Delete(diskNo, fileName)
expression
Parameter diskNo: integer; 0: HMI; 2: USB; 3: SD card
definition filename: string; filename
Example ret, err, errText = file.Delete(0, “myFile.txt”)
Example
Delete the data of myFile.txt stored in the HMI.
description
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description
-1 Parameter setting error
Return value
-106 External storage device not ready
-114 Failed to delete
-136 Invalid path
errText: string; error description

57
Lua Command Manual

 file.DeleteDir: delete directory


Command
file.DeleteDir
name
Command
ret, err, errText = file.DeleteDir(diskNo, dirName)
expression
Parameter diskNo: integer; 0: HMI; 2: USB; 3: SD card
definition dirName: string; directory name
Example ret, err, errText = file.DeleteDir(2, "/DELTA")
Example
Delete the directorynamed DELTA saved on the USB.
description
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description
-1 Parameter setting error
Return value
-106 External storage device not ready
-114 Failed to delete
-136 Invalid path
errText: string; error description

 file.ToPDF: convert the file to PDF


Command
name
Command
expression
file.ToPDF

landscape)

ft
ret, errCode = file.ToPDF(disk_id, srcFileName, pdfFileName, paperSize, fontSize,

disk_id: integer; 0: HMI; 2: USB; 3: SD card


srcFileName: string; source filename
ra
Parameter pdfFileName: PDF filename
definition paperSize: string; "A3", "A4"
fontSize: integer; font size: 8, 10, 12...
landscape: integer; 0: portrait; 1: landscape
Example ret, errCode = file.ToPDF(2, "file001.txt", "out001.pdf", "A4", 10, 0)
Example Convert the file file001.txt saved on the USB to PDF, and set the name to out001.pdf,
description the format to A4, the layout to portrait, and font size to 10.
ret: integer. Success: 1; failure: 0
Return value
errCode: integer; error code
D
 file.ToPrinter: print file
Command
file.ToPrinter
name
Command
ret, errCode = file.ToPrinter(disk_id, srcFileName, fontSize, outputPDF)
expression
disk_id: integer; 0: HMI; 2: USB; 3: SD card
srcFileName: string; source filename (must include the file extension), must be a txt
Parameter or csv file
definition
fontSize: integer; 8, 10, 12...
outputPDF: integer; 0: do not generate PDF; 1: generate PDF
Example ret, errCode = file.ToPrinter(2, "file001.txt", 10, 1)
Print the file file001.txt and convert it to a pdf file to the USB with the font size of 10.
Example Note:
description 1. Must first set up the printer and connect it to the HMI.
2. See Chapter 26 of the DOPSoft User Manual for printer setup.
ret: integer. Success: 1; failure: 0
Return value
errCode: integer; error code

58
Lua Command Manual

 file.ListExternal: get a list of the files stored in the external device


Command
file.ListExternal
name
Command
ret, nameList = file.ListExternal(disk_id, sub_dir)
expression
Parameter disk_id: integer; 2: USB; 3: SD card
definition sub_dir: string; word directory; can be nil
Example ret, nameList = file.ListExternal(2,"file1/file2")
Example Get the file list stored under the USB path /file1/file2; nameList is the list,
description nameList[1] is the first filename, nameList[2] is the second filename, and so on.
ret: integer; success: 1; failure or no file: 0
Return value
nameList: array table; success: filename list; failure: nil

 file.Exist: check if the file exists


Command
file.Exist
name
Command
ret, err, errText = file.Exist(disk_id, file_name)
expression
Parameter disk_id: integer; 0: HMI; 2: USB; 3: SD card
definition file_name: string; filename
Example
Example
description

Return value

ft
ret, err, errText = file.Exist(0, "myFile001")

Check whether the file myFile001.txt exists in the HMI.

ret: integer. Success: 1; failure: 0


err: integer; error code
ra
errText: string; error description

 file.PDFToPrinter: print PDF file


Command
file.PDFToPrinter
name
Command
ret, errCode = file.PDFToPrinter(disk_id, srcFileName)
expression
Parameter disk_id: integer; 0: HMI; 2: USB drive; 3: SD card
definition srcFileName: string; source PDF filename
Example ret, errCode = file.PDFToPrinter(2, "file001.pdf")
D
Example Print the file file001.pdf saved on the USB device.
description Note: before printing, make sure that the HMI is connected to the printer.

ret: integer. Success: 1; failure: 0


Return value
errCode: integer; error code

59
Lua Command Manual

 file.Copy: copy file


Command
file.Copy
name
Command
ret, errCode = file.Copy(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, err = file.Copy("myFile001.txt", 0, "myFile001Out.txt", 2)
Copy the file myFile001.txt stored in the HMI to the USB, and name the file as
Example 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

Return value -1 Invalid parameter


-106 The specified disk is not ready

 file.Move: move file

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

Return value -1 Invalid parameter


-106 The specified disk is not ready
-110 The specified filename does not exist
-111 Cannot move file
-136 Invalid file path

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.

 Build Lua program, shown as follows.


HMI #1 Lua program:

disk_id = 0
file_name = " DELTA.txt"
ret, fileHandle = file.Open(disk_id, file_name)

while true do

Build Lua if mem.inter.ReadBit(0,0)==1 then


program

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

4.6 FileSlot (file access)

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

 fileslot.Read: read the fileslot file


Command
fileslot.Read
name
Command
ret, err = fileslot.Read(FS_ID, FS_Pos, Device, MemIdx, length)
expression
FS_ID: unsigned integer; FileSlot ID
FS_Pos: unsigned integer; FileSlot reading position
Parameter
Device: unsigned integer; 1: internal memory ($); 2: static memory ($M)
definition
MemIdx: unsigned integer; read or write address of the device
length: unsigned integer; data length
Example ret, err = fileslot.Read(1, 0, 1, 20, 5)
Example Read the data of 5 words starting from position 0 in the FileSlot content of ID 1 to the
description destination address $20.
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description
1 No errors
-1 Invalid parameter
-13 Data read error
-14 Data writing error

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

 fileslot.Write: write to the fileslot file


Command
fileslot.Write
name
Command
ret, err = fileslot.Write(FS_ID, FS_Pos, Device, MemIdx, length)
expression
FS_ID: unsigned integer; FileSlot ID
FS_Pos: unsigned integer; FileSlot reading position
Parameter
Device: unsigned integer; 1: internal memory ($); 2: static memory ($M)
definition
MemIdx: unsigned integer; read or write address of the device
length: unsigned integer; data length
Example ret, err = fileslot.Write(1, 0, 1, 10, 5)
Example Write thedata of 5 words starting from $10 to FileSlot with FileSlot ID 1 and address
description 0.
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description
1 No errors
-1 Invalid parameter
-13 Data read error
-14 Data writing error

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

 fileslot.ReadValue: read the value of the fileslot


Command
fileslot.ReadValue
name
Command
ret, err = fileslot.ReadValue(FS_ID, FS_Pos, Format, [value_format])
expression
FS_ID: unsigned integer; FileSlot ID
FS_Pos: unsigned integer; FileSlot reading position
Parameter
Format: unsigned integer; 2: length (WORD); 3: length (DWORD)
definition
value_format: string. Fill in "signed" for signed numbers; this parameter is not
mandatory.
Example ret, err = fileslot.ReadValue(1, 0, 2, "SIGNED")
Example
Read the FileSlot which ID is 1 and address is 0 in the unit of signed word.
description
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description
1 No errors
-1 Invalid parameter
-13 Data read error
-14 Data writing error

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

 fileslot.WriteValue: write the value to the fileslot


Command
fileslot.WriteValue
name
Command
ret, err = fileslot.WriteValue(FS_ID, FS_Pos, Format, Value)
expression
FS_ID: unsigned integer; FileSlot ID
Parameter FS_Pos: unsigned integer; FileSlot reading position
definition Format: unsigned integer; 2: length (WORD); 3: length (DWORD)
Value: unsigned integer; data content
Example ret, err = fileslot.WriteValue(1, 0, 2, 99)
Example Write the unsigned integer 99 in the unit of word to the FileSlot which ID is 1 and
description address is 0.
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description
1 No errors
-1 Invalid parameter
-13 Data read error
-14 Data writin error

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

Return value 1 No errors


-1 Invalid parameter
-51 FileSlot ID error
-57 FileSlot data length error

69
Lua Command Manual

 fileslot.Remove: remove the fileslot


Command
fileslot.Remove
name
Command
ret, err = fileslot.Remove(FS_ID)
expression
Parameter
FS_ID: unsigned integer; FileSlot ID
definition
Example ret, err = fileslot.Remove(1)
Example
Remove FileSlot ID 1.
description
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description

Return value 1 No errors


-1 Invalid parameter
-51 FileSlot ID error
-56 FileSlot deletion error

 fileslot.Import: import the fileslot file


Command
name
Command
expression

Parameter
definition
fileslot.Import

ft
ret, err = fileslot.Import(FS_ID, DiskID, ExtFileName)

FS_ID: unsigned integer; FileSlot ID


DiskID: integer; disk ID. 2: USB drive; 3: SD card
ra
ExtFileName: string, external filename
Example ret, err = fileslot.Import(1, 2, "myfile.txt")
Example
Import the text file named "myfile.txt" from the USB storage devic to FileSlot ID 1.
description
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description
1 No errors
-1 Invalid parameter
D
-59 FileSlot export error
Return value
-61 FileSlot import error
-62 FileSlot operation error
-63 External file operation error
-64 FileSlot copy error
-106 External storage device not ready
-136 External filename error

70
Lua Command Manual

 fileslot.Export: export the fileslot file


Command
fileslot.Export
name
Command
ret, err = fileslot.Export(FS_ID, DiskID, ExtFileName)
expression
FS_ID: unsigned integer; FileSlot ID
Parameter
DiskID: integer, disk ID; 2: USB drive; 3: SD card
definition
ExtFileName: string, external filename
Example ret, err = fileslot.Export(10, 2, "Newfile.txt")
Example
Export the data of FileSlot ID 10 to the USB drive and name it "Newfile.txt".
description
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description
1 No errors
-1 Invalid parameter
-59 FileSlot export error
Return value
-61 FileSlot import error
-62 FileSlot operation error


ft -63
-64
-106
-136

fileslot.SetName: set the fileslot filename


External file operation error
FileSlot copy error
External storage device not ready
External filename error
ra
Command
fileslot.SetName
name
Command
et, err = fileslot.SetName(FS_ID, FS_Name)
expression
Parameter FS_ID: unsigned integer; FileSlot ID
definition FS_Name: string, filename
ret, err = fileslot.SetName(1, "myfile.txt")
Example
ret, err = fileslot.Export(1, 2, "folder1/")
Example Name the FileSlot ID 1 file “myfile.txt”, and export the file to the folder named folder1.
D
description Note: you must first create the folder1 folder on the USB storage device.
ret: integer. Success: 1; failure: 0
err: integer; error code
Return value Description

Return value 1 No errors


-1 Invalid parameter
-51 FileSlot ID error
-66 Failed to set the FileSlot name

71
Lua Command Manual

 fileslot.GetName: get the fileslot filename


Command
fileslot.GetName
name
Command
ret, err = fileslot.GetName(FS_ID)
expression
Parameter
FS_ID: unsigned integer; FileSlot ID
definition
Example ret, err = fileslot.GetName(5)
Example
Get the name of FileSlot ID 5.
description
ret: integer. Success: filename string; failure: 0
err: integer; error code
Return value Description

Return value 1 No errors


-1 Invalid parameter
-51 FileSlot ID error
-84 Failed to obtain the FileSlot name


Command
name
Command
expression
Parameter
fileslot.GetID

ft
fileslot.GetID: get the fileslot file ID

ret, err = fileslot.GetID(FS_Name)

FS_Name: string, filename


ra
definition
ret, err = fileslot.SetName(1, "myfile")
Example
ret, err = fileslot.GetID("myfile")
Example Name the FileSlot ID 1 file “myfile”. Get the FileSlot ID of “myfile”, and the result is ID
description 1.
ret: integer. Success: the corresponding FileSlot ID; FileSlot ID does not exist or failure:
0
err: integer; error code
Return value Description
Return value
D
1 No errors
-1 Invalid parameter
-51 FileSlot ID error
-87 Failed to obtain the FileSlot ID

72
Lua Command Manual

4.7 FTP Client (FTP transfer function)

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

 ftpc.Download: FTP download


Command
ftpc.Download
name
Command ret, err = ftpc.Download(IPAddress, Port, UserName, Password, LocalFileName,
expression RemoteFileName, OverWrite)
IPAddress: string, IP address
Port: unsigned integer; port number with the range of 1 to 65535
UserName: string; login account. If no account is required, set to "" (anonymous
login).
Parameter Password: string; login password. If no password is required, set it to "".
definition LocalFileName: string; local filename. Use "/HMI/filename" or "/FILESLOT/FS_ID" to
select the file in the HMI or FileSlot.
RemoteFileName: string; external filename
OverWrite: unsigned integer. 0: do not overwrite when the file exists; 1: overwrite
when the file exists
IPAddress = "192.168.123.144"
Port = 21
UserName = "chen"
Password = "123"
Example LocalFileName = "/FILESLOT/1"
RemoteFileName = "delta.txt"

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

 ftpc.Upload: FTP upload


Command
ftpc.Upload
name
Command ret, err = ftpc.Upload(IPAddress, Port, UserName, Password, LocalFileName,
expression RemoteFileName, OverWrite)
IPAddress: string; IP address
Port: unsigned integer; port number with the range of 1 to 65535
UserName: string; login account. If no account is required, set to "" (anonymous
login).
Parameter Password: string; login password. If no password is required, set it to "".
definition LocalFileName: string; local filename. Use "/HMI/filename" or "/FILESLOT/FS_ID" to
select the file in HMI or FileSlot.
RemoteFileName: string; external filename
OverWrite: unsigned integer. 0: do not overwrite when the file exists; 1: overwrite
when the file exists
IPAddress = "192.168.123.144"
Port = 21
UserName = "chen"
Password = "123"
Example LocalFileName = "/FILESLOT/1"
RemoteFileName = "delta.txt"

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

Example (upload/download files via ftp command)


 Build Lua program:
if mem.inter.ReadBit(0,0)==1 then
IPAddress = "192.168.123.144"
Port = 21
UserName = "chen"
Password = ""
Build Lua
program LocalFileName = "/FILESLOT/1"
RemoteFileName = "delta.txt"
OverWrite = 0
ret, err = ftpc.Download(IPAddress, Port, UserName, Password,
LocalFileName, RemoteFileName, OverWrite)
mem.inter.WriteBit(0,0,0)
end
 Create two Maintained buttons with the Write Addresses as $0.0 and $500.0.
 Create a Character Entry element with the Write Address as $300.
 Create a Multi-line text input element, with its the File Name Address as $300
and the Load File Trigger Address as $500.0.

ft
ra
Create
elements
D

76
Lua Command Manual

Example (upload/download files via ftp command)


 Download a third-party FTP Server software, and create an FTP Server
directory with the software.
 Create a notepad file named "delta.txt", and place it in the above mentioned
FTP Server directory.
 Download the project to the HMI. Trigger $0.0 and download "delta.txt" to
fileslot ID 1.
if mem.inter.ReadBit(0,0)==1 then
IPAddress = "192.168.123.144"
Port = 21
UserName = "chen"
Password = ""
LocalFileName = "/FILESLOT/1"
RemoteFileName = "delta.txt"
OverWrite = 0
ret, err = ftpc.Download(IPAddress, Port, UserName, Password,
LocalFileName, RemoteFileName, OverWrite)
Execution
results mem.inter.WriteBit(0,0,0)
end
 Enter “/FILESLOT/1” to $300 to trigger $500.0 and display the contents of
FileSlot ID 1 in the Multi-line text input element.

ft
ra
D

77
Lua Command Manual

4.8 Math (mathematical operations)

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.abs: get the absolute value of the number


Command
math.abs
name
Command
value = math.abs(value_number)
expression
Parameter
value_number: signed integer
definition
Example v = math.abs(-123)
Example
Get the absolute value of -123, then v = 123.
description
Return value v: unsigned integer

 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 =

v: the value of the natural exponential e raised to power x

math.log: get the value of the logarithmic function


Command
ra
math.log
name
Command
value= math.log(x, base)
expression
Parameter x: value
definition base: the logarithm base; if left blank, the base is a natural exponent.
Example v = math.log(1000, 10)
Example
Use 10 as the base to calculate the logarithmic of 1000, then v = 3.
description
Return value v: logarithm
D
 math.sin: get the sine value
Command
math.sin
name
Command
value = math.sin(radian)
expression
Parameter
radian: floating-point number; radians
definition
Example v = math.sin(math.rad(30))
Example
v is the value of sin30°, then v = 0.499999.
description
Return value v: floating-point number

79
Lua Command Manual

 math.sinh: get the hyperbolic sine value


Command
math.sinh
name
Command
value = math.sinh(tangenhValue)
expression
Parameter
tangenhValue: number; tangens hiperboliczny values
definition
Example v = math.sinh(2)
Example
Get the value of sinh(2), then v = 3.62860.
description
Return value v: hyperbolic function value

 math.cos: get the cosine value


Command
math.cos
name
Command
value = math.cos(radian)
expression
Parameter
radian: floating-point number; radians
definition
Example v = math.cos(math.rad(30))


Example
description
Return value

ft
Get the value of cos30°, then v = 0.866025.

v: floating-point number

math.cosh: get the hyperbolic cosine value


Command
ra
math.cosh
name
Command
value = math.cosh(tangenhValue)
expression
Parameter
tangenhValue: number; tangens hiperboliczny values
definition
Example v = math.cosh(2)
Example
Get the value of cosh(2), then v = 3.7621956910.
description
Return value v: hyperbolic function value
D
 math.tan: get the tangent value
Command
math.tan
name
Command
value = math.tan(radian)
expression
Parameter
radian: floating-point number; radians
definition
Example v = math.tan(math.rad(30))
Example
Get the value of tan30°, then v = 0.599350.
description
Return value v: floating-point number

80
Lua Command Manual

 math.tanh: get the hyperbolic tangent value


Command
math.tanh
name
Command
value= math.tanh(tangenhValue)
expression
Parameter
tangenhValue: number; tangens hiperboliczny values
definition
Example v = math.tanh(2)
Example
Get the value of tanh(2), then v = 0.964027580.
description
Return value v: hyperbolic function value

 math.asin: get the arcsine value


Command
math.asin
name
Command
value= math.asin(radian)
expression
Parameter
radian: floating-point number; radians
definition
Example v = math.asin(math.rad(30))
Example
description
Return value

ft
Get the value of arcsin 30°, then v = 0.551069.

v: floating-point number

math.acos: get the arccosine value


Command
ra
math.acos
name
Command
value= math.acos(radian)
expression
Parameter
radian: floating-point number; radians
definition
Example v = math.acos(math.rad(30))
Example
Get the value of arccos 30°, then v = 1.019726.
description
Return value v: floating-point number
D
 math.atan: get the arctangent value
Command
math.atan
name
Command
value= math.atan(radian)
expression
Parameter
radian: floating-point number; radians
definition
Example v = math.atan(math.rad(30))
Example
Get the value of arctan 30°, then v = 0.482347.
description
Return value v: floating-point number

81
Lua Command Manual

 math.atan2: get the arctangent value (two parameters)


Command
math.atan2
name
Command
value= math.atan2(yRadian, xRadian)
expression
Parameter yRadian: number; length of y-axis
definition xRadian: number; length of x-axis, can be 0
Example v = math.atan2(math.rad(30), math.rad(60))
Example Get the value of atan2 (30°, 60°), then v = 0.463647609000806.
description Note: atan2 (30°, 60°) = atan(30°/60°).
Return value v: floating-point number

 math.deg: get the angle corresponding to the radians


Command
math.deg
name
Command
angle = math.deg(radian)
expression
Parameter
radian: floating-point number; radians
definition
Example v = math.deg(math.pi)


Example
description
Return value

ft
Convert π to degrees, then v = 180.

v: floating-point number, angle

math.rad: get the radians corresponding to the angle


Command
ra
math.rad
name
Command
radian = math.rad(angle)
expression
Parameter
angle: floating-point number; angle
definition
Example v = math.rad(180)
Example
Convert π to radians, then v = 3.14159265.
description
Return value v: positive number
D
 math.min: get the minimum value
Command
math.min
name
Command
min_value = math.min(v1, v2)
expression
Parameter
v1, v2: floating-point numbers; numerical values
definition
Example v = math.min(-2, 3.6)
Example
Get the minimum value between -2 and 3.6, then v = -2.
description
Return value v: floating-point number; minimum value

82
Lua Command Manual

 math.max: get the maximum value


Command
math.max
name
Command
max_value = math.max(v1, v2)
expression
Parameter
v1, v2: floating-point numbers; numerical values
definition
Example v = math.max(-2, 3.6)
Example
Get the maximum value between -2 and 3.6, then v = 3.6.
description
Return value v: floating-point number; maximum value

 math.modf: split the value into an integer and a decimal


Command
math.modf
name
Command
Integer, fraction = math.modf(v1)
expression
Parameter
v1: floating-point number
definition
Example v1, v2 = math.modf(3.6)
Example
description

Return value

ft
Split 3.6 into an integer and a decimal, then v1 = 3, and v2 = 0.6.

v1: integer value


v2: decimal value

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 (π)

 math.pow: get the power value


Command
math.pow
name
Command
value = math.pow(x, y)
expression
Parameter x: base
definition y: power
Example v = math.pow(2, 3)
Example
Get the value of 2 to the 3rd power, then v = 8.
description
Return value v: x to the power of y

83
Lua Command Manual

 math.randomseed: random seed (used with the math.random command)


Command
math.randomseed
name
Command
math.randomseed(seedValue)
expression
Parameter
seedValue: initial random seed; you can set this variable to a time
definition
math.randomseed(sys.GetTick())
Example
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
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

 math.random: get a random value


Command
math.random
name
Command
value = math.random(lower, upper)
expression
Parameter
definition

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

 math.sqrt: get the square root value


Command
math.sqrt
name
Command
sqrt = math.sqrt(nSquare)
D
expression
Parameter
NSquare: the radicand
definition
Example v = math.sqrt(100)
Example
Get the square root value of 100, then v = 10.
description
Return value v: square root

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

 recipe.GetCurRcpNoIndex: get the current recipe number index


Command
recipe.GetCurRcpNoIndex
name
Command
ret, noIdx = recipe.GetCurRcpNoIndex()
expression
Parameter
No parameters
definition
Example ret, noIdx = recipe.GetCurRcpNoIndex()
Example
Get the current recipe number index.
description
ret: return 1 on success; return 0 on failure
Return value
noldx: integer; recipe number index

 recipe.GetCurRcpGIndex: get the current recipe group index


Command
recipe.GetCurRcpGIndex
name
Command
ret, gIdx = recipe.GetCurRcpGIndex()
expression
Parameter
No parameters
definition
Example
Example
description

Return value

ft
ret, gIdx = recipe.GetCurRcpGIndex()

Get the current recipe group index.

ret: return 1 on success; return 0 on failure


gldx: integer, recipe group index
ra
 recipe.GetRcpWord: get the value of the specified recipe address (Word)
Command
recipe.GetRcpWord
name
Command
ret, value = recipe.GetRcpWord(index)
expression
Parameter
index: integer; recipe index
definition
Example ret, value = recipe.GetRcpWord(1)
Example
Get the value of the specified recipe address (RCP1) in units of unsigned Word.
description
D
ret: return 1 on success; return 0 on failure
Return value
value: integer; unsigned recipe value in units of Word

 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.GetRcpFloat: get the value of the specified recipe address (Float)


Command
recipe.GetRcpFloat
name
Command
ret, value = recipe.GetRcpFloat(index)
expression
Parameter
Index: integer; recipe index
definition
Example ret, value = recipe.GetRcpFloat(1)
Example
Get the value of the specified recipe address (RCP1) in units of Float.
description
ret: return 1 on success; return 0 on failure
Return value
value: floating-point number; the recipe value is a 32-bit floating-point number

 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()

Get the index name of the current enhanced recipe number.

ret: return 1 on success; return 0 on failure


noName: string; the name of the currently active enhanced recipe number
ra
 recipe.GetCurEnRcpGName: get the index name of the current enhanced recipe group
Command
recipe.GetCurEnRcpGName
name
Command
ret, gName = recipe.GetCurEnRcpGName()
expression
Parameter
No parameters
definition
Example ret, gName = recipe.GetCurEnRcpGName()
Example
Get the index name of the current enhanced recipe group.
description
D
ret: return 1 on success; return 0 on failure
Return value
gName: string; the name of the currently active enhanced recipe group

 recipe.GetCurEnRcpNoIndex: get the index of the current enhanced recipe number


Command
recipe.GetCurEnRcpNoIndex
name
Command
ret, noIdx = recipe.GetCurEnRcpNoIndex()
expression
Parameter
No parameters
definition
Example ret, noIdx = recipe.GetCurEnRcpNoIndex()
Example
Get the index of the current enhanced recipe number.
description
ret: return 1 on success; return 0 on failure
Return value
noIdx: integer; the index of the currently active enhanced recipe number

87
Lua Command Manual

 recipe.GetCurEnRcpGIndex: get the index of the current enhanced recipe group


Command
recipe.GetCurEnRcpGIndex
name
Command
ret, gIdx = recipe.GetCurEnRcpGIndex()
expression
Parameter
No parameters
definition
Example ret, gIdx = recipe.GetCurEnRcpGIndex()
Example
Get the index of the current enhanced recipe group.
description
ret: return 1 on success; return 0 on failure
Return value
gIdx: integer; the index of the currently active enhanced recipe group

 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

ret: return 1 on success; return 0 on failure


value: integer; the enhanced recipe value is in units of Word
ra
 recipe.GetEnRcpDWord: get the value of the specified enhanced recipe address
(Double Word)
Command
recipe.GetEnRcpDWord
name
Command
ret, value = recipe.GetEnRcpDWord(index, [value_format])
expression
Index: integer, enhanced recipe index
Parameter
value_format: formatted string. Fill in "signed" for signed numbers; this parameter is
definition
not mandatory.
D
Example ret, value = recipe.GetEnRcpDWord(2)
Example Get the value of the specified enhanced recipe address (ENRCP2) in units of Double
description Word.
Return value ret: return 1 on success; return 0 on failure

 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

 recipe.GetEnRcpAscii: get the string of the specified enhanced recipe address


Command
recipe.GetEnRcpAscii
name
Command
ret, str = recipe.GetEnRcpAscii(index)
expression
Parameter
Index: integer; enhanced recipe index
definition
Example ret, str = recipe.GetEnRcpAscii(2)
Example
Get the string of the specified enhanced recipe address (ENRCP2).
description
ret: return 1 on success; return 0 on failure
Return value
str: string; string of the enhanced recipe

 recipe.SetRcpWord: set parameters to the recipe address (Word)


Command
recipe.SetRcpWord
name
Command
ret = recipe.SetRcpWord(index, word)
expression
Parameter Index: integer; recipe index
definition Word: integer; unsigned recipe value in units of Word


Example
Example
description
Return value

ft
ret = recipe.SetRcpWord(1, 5)

Set 5 to the recipe address RCP1.

ret: return 1 on success; return 0 on failure

recipe.SetRcpDWord: set parameters to the recipe address (Double Word)


ra
Command
recipe.SetRcpDWord
name
Command
ret = recipe.SetRcpDWord(index, dword)
expression
Parameter Index: integer; recipe index
definition dword: integer; the recipe value in units of Double Word
Example ret = recipe.SetRcpDWord(1, 65536)
Example
Set 65536 to the recipe address RCP1.
description
D
Return value ret: return 1 on success; return 0 on failure

 recipe.SetRcpFloat: set parameters to the recipe address (Float)


Command
recipe.SetRcpFloat
name
Command
ret = recipe.SetRcpFloat(index, floatValue)
expression
Parameter Index: integer; recipe index
definition floatValue: floating-point number; the recipe value is a 32-bit floating-point number
Example ret = recipe.SetRcpFloat(1, 9.9)
Example
Set 9.9 to the recipe address RCP1.
description
Return value ret: return 1 on success; return 0 on failure

89
Lua Command Manual

 recipe.SetCurEnRcpNoName: set the name of the enhanced recipe number


Command
recipe.SetCurEnRcpNoName
name
Command
ret = recipe.SetCurEnRcpNoName(newName)
expression
Parameter
newName: string; the name to be set for the enhanced recipe number
definition
Example ret = recipe.SetCurEnRcpNoName("POSHENG")
Example
Set POSHENG as the name of the currently specified enhanced recipe number.
description
Return value ret: return 1 on success; return 0 on failure

 recipe.SetCurEnRcpGName: set the name of the enhanced recipe group


Command
recipe.SetCurEnRcpGName
name
Command
ret = recipe.SetCurEnRcpGName(newName)
expression
Parameter
newName: string; the name to be set for the enhanced recipe group
definition
Example ret = recipe.SetCurEnRcpGName("POSHENG")


Example
description
Return value

ft
Set POSHENG as the name of the currently specified enhanced recipe group.

ret: return 1 on success; return 0 on failure

recipe.SetEnRcpWord: set parameters to the enhanced recipe address (Word)


Command
ra
recipe.SetEnRcpWord
name
Command
ret = recipe.SetEnRcpWord(index, word)
expression
Parameter Index: integer; enhanced recipe index
definition Word: integer; the enhanced recipe value in units of Word
Example ret = recipe.SetEnRcpWord(1, 88)
Example
Set 88 to the current enhanced recipe ENRCP1.
description
Return value ret: return 1 on success; return 0 on failure
D
 recipe.SetEnRcpDWord: set parameters to the enhanced recipe address (Double Word)
Command
recipe.SetEnRcpDWord
name
Command
ret = recipe.SetEnRcpDWord(index, dword)
expression
Parameter Index: integer; enhanced recipe index
definition dword: integer; the enhanced recipe value in units of Double Word
Example ret = recipe.SetEnRcpDWord(2, 65536)
Example
Set 65536 to the current enhanced recipe ENRCP2.
description
Return value ret: return 1 on success; return 0 on failure

90
Lua Command Manual

 recipe.SetEnRcpFloat: set parameters to the enhanced recipe address (Float)


Command
recipe.SetEnRcpFloat
name
Command
ret = recipe.SetEnRcpFloat(index, floatValue)
expression
Index: integer; enhanced recipe index
Parameter
definition floatValue: floating-point number; the enhanced recipe value is a 32-bit floating-point
number
Example ret = recipe.SetEnRcpFloat(3, 99.9)
Example
Set 99.9 to the enhanced recipe address ENRCP3.
description
Return value ret: return 1 on success; return 0 on failure

 recipe.SetEnRcpAscii: set the string to the enhanced recipe address


Command
recipe.SetEnRcpAscii
name
Command
ret = recipe.SetEnRcpAscii(index, str, len)
expression
Index: integer; enhanced recipe index
Parameter
str: string; string of the enhanced recipe
definition

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.

ret: return 1 on success; return 0 on failure


ra
 recipe.ChangeRcpNoIndex: change the index of the recipe number
Command
recipe.ChangeRcpNoIndex
name
Command
ret = recipe.ChangeRcpNoIndex(noIdx)
expression
Parameter
noIdx: integer; recipe number index
definition
Example ret = recipe.ChangeRcpNoIndex(1)
Example
Change the recipe number index to 1.
D
description
Return value ret: return 1 on success; return 0 on failure

 recipe.ChangeRcpGIndex: change the index of the recipe group


Command
recipe.ChangeRcpGIndex
name
Command
ret = recipe.ChangeRcpGIndex(gIdx)
expression
Parameter
gIdx: integer; recipe number index
definition
Example ret = recipe.ChangeRcpGIndex(1)
Example
Change the recipe group index to 1.
description
Return value ret: return 1 on success; return 0 on failure

91
Lua Command Manual

 recipe.ChangeEnRcpNoIndex: change the index of the enhanced recipe number


Command
recipe.ChangeEnRcpNoIndex
name
Command
ret = recipe.ChangeEnRcpNoIndex(noIdx)
expression
Parameter
noIdx: integer; enhanced recipe number index
definition
Example ret = recipe.ChangeEnRcpNoIndex(3)
Example
Change the enhanced recipe number index to 3.
description
Return value ret: return 1 on success; return 0 on failure

 recipe.ChangeEnRcpGIndex: change the index of the enhanced recipe group


Command
recipe.ChangeEnRcpGIndex
name
Command
ret = recipe.ChangeEnRcpGIndex(gIdx)
expression
Parameter
gIdx: integer; enhanced recipe group index
definition
Example ret = recipe.ChangeEnRcpGIndex(2)


Example
description
Return value

ft
Change the enhanced recipe group index to 2.

ret: return 1 on success; return 0 on failure

recipe.SetEnRcpDouble: set parameters to the enhanced recipe address


(double-precision floating-point number)
ra
Command
recipe.SetEnRcpDouble
name
Command
ret = recipe.SetEnRcpDouble(index, doubleValue)
expression
Index: integer; enhanced recipe index
Parameter
definition doubleValue: floating-point number; the enhanced recipe value is a 64-bit floating-
point number
Example ret = recipe.SetEnRcpDouble(0, 22.22)
Example
D
Set 22.22 to the enhanced recipe address ENRCP0.
description
Return value ret: return 1 on success; return 0 on failure

 recipe.GetEnRcpDouble: get the value of the specified enhanced recipe address


(double-precision floating-point number)
Command
recipe.GetEnRcpDouble
name
Command
ret, value = recipe.GetEnRcpDouble(index)
expression
Parameter
Index: integer; enhanced recipe index
definition
Example ret, value = recipe.GetEnRcpDouble(0)
Example Get the value of the specified enhanced recipe address (ENRCP0) which is a 64-bit
description floating-point number.
Return value ret: return 1 on success; return 0 on failure

92
Lua Command Manual

Examples (recipe.GetCurRcpNoIndex, recipe.GetCurRcpGIndex, recipe.GetRcpWord,


recipe.GetRcpDWord, recipe.GetRcpFloat)
 In the DOPSoft, go to [Options] > [Recipe] > [16-Bit Recipe] >
to add a recipe with the button.

 In the DOPSoft, go to [Options] > [Recipe] > [32-Bit Recipe] >


Create 16-bit
and 32-bit to add 2 new recipes with the button.
recipes Group 1:

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

Examples (recipe.GetCurRcpNoIndex, recipe.GetCurRcpGIndex, recipe.GetRcpWord,


recipe.GetRcpDWord, recipe.GetRcpFloat)
end

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

Examples (recipe.GetCurRcpNoIndex, recipe.GetCurRcpGIndex, recipe.GetRcpWord,


recipe.GetRcpDWord, recipe.GetRcpFloat)
 Press $0.0 to write the return value to $1 and write the current number
index RCPNO to $2.
if mem.inter.ReadBit(0,0)==1 then
ret, noIdx = recipe.GetCurRcpNoIndex()
mem.inter.Write(1, ret)
mem.inter.Write(2,noIdx)
end.

 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

 Switch the recipe group RCPG to 1. Press $0.3 to write RCP to $8 in


Double Word.
if mem.inter.ReadBit(0,3)==1 then
ret, value = recipe.GetRcpDWord(index)
mem.inter.Write(7, ret)
mem.inter.WriteDW(8,value)
end

95
Lua Command Manual

Examples (recipe.GetCurRcpNoIndex, recipe.GetCurRcpGIndex, recipe.GetRcpWord,


recipe.GetRcpDWord, recipe.GetRcpFloat)
 Switch the recipe group RCPG to 2. Press $0.4 to write RCP1 to $11.
if mem.inter.ReadBit(0,4)==1 then
ret, value = recipe.GetRcpFloat(index)
mem.inter.Write(10, ret)
mem.inter.WriteFloat(11,value)
Execution end
results

Example (recipe.GetCurEnRcpNoName, recipe.GetCurEnRcpGName,


recipe.GetCurEnRcpNoIndex, recipe.GetCurEnRcpGIndex, recipe.GetEnRcpWord,
recipe.GetEnRcpDWord, recipe.GetEnRcpFloat, recipe.GetEnRcpAscii)
 In the DOPSoft, go to [Options] > [Recipe] > [Enhanced Recipe] >
to add an enhanced recipe with the button.

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

Build Lua if mem.inter.ReadBit(0,8)==1 then


program ret, noIdx = recipe.GetCurEnRcpNoIndex()
mem.inter.Write(25,ret)
mem.inter.Write(26,noIdx)
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

Example (recipe.GetCurEnRcpNoName, recipe.GetCurEnRcpGName,


recipe.GetCurEnRcpNoIndex, recipe.GetCurEnRcpGIndex, recipe.GetEnRcpWord,
recipe.GetEnRcpDWord, recipe.GetEnRcpFloat, recipe.GetEnRcpAscii)
end

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

Example (recipe.GetCurEnRcpNoName, recipe.GetCurEnRcpGName,


recipe.GetCurEnRcpNoIndex, recipe.GetCurEnRcpGIndex, recipe.GetEnRcpWord,
recipe.GetEnRcpDWord, recipe.GetEnRcpFloat, recipe.GetEnRcpAscii)
 Press $0.6 to write the return value to $15 and write the name of the current
enhanced recipe number to $16.
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

 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

Example (recipe.GetCurEnRcpNoName, recipe.GetCurEnRcpGName,


recipe.GetCurEnRcpNoIndex, recipe.GetCurEnRcpGIndex, recipe.GetEnRcpWord,
recipe.GetEnRcpDWord, recipe.GetEnRcpFloat, recipe.GetEnRcpAscii)
 Press $0.10 to write the return value to $29 and write the value of ENRCP0
to $30 in Word.
if mem.inter.ReadBit(0,10)==1 then
ret, value = recipe.GetEnRcpWord(0)
mem.inter.Write(29,ret)
mem.inter.Write(30,value)
end

 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

Examples (recipe.SetRcpWord, recipe.SetRcpDWord, recipe.SetRcpFloat,


recipe.SetCurEnRcpNoName, recipe.SetCurEnRcpGName, recipe.SetEnRcpWord,
recipe.SetEnRcpDWord, recipe.SetEnRcpFloat, recipe.SetEnRcp.
recipe.ChangeEnRcpGIndex)
 In the DOPSoft, go to [Options] > [Recipe] > [16-Bit Recipe] >
to add a recipe with the button.

 In the DOPSoft, go to [Options] > [Recipe] > [32-Bit Recipe] >


to add two recipes with the button.
Group 1:

ft
ra
Create recipes Group 2:

 In the DOPSoft, go to [Options] > [Recipe] > [Enhanced Recipe] >


D
to add two enhanced recipes with the button.
Group 1:

Group 2:

100
Lua Command Manual

Examples (recipe.SetRcpWord, recipe.SetRcpDWord, recipe.SetRcpFloat,


recipe.SetCurEnRcpNoName, recipe.SetCurEnRcpGName, recipe.SetEnRcpWord,
recipe.SetEnRcpDWord, recipe.SetEnRcpFloat, recipe.SetEnRcp.
recipe.ChangeEnRcpGIndex)
 Enter the Lua command as follows.
while true do
if mem.inter.ReadBit(100,0)==1 then
index=3
word=100
ret = recipe.SetRcpWord(index, word)
end

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

Examples (recipe.SetRcpWord, recipe.SetRcpDWord, recipe.SetRcpFloat,


recipe.SetCurEnRcpNoName, recipe.SetCurEnRcpGName, recipe.SetEnRcpWord,
recipe.SetEnRcpDWord, recipe.SetEnRcpFloat, recipe.SetEnRcp.
recipe.ChangeEnRcpGIndex)

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)

Create 12 Numeric Entry elements with the Write Addresses as RCP0,


RCP1, RCP2, ... RCP11, the Data Type as Word, and the Data Format as
Unsigned Decimal.
ra
 Create 12 Numeric Entry elements with the Write Addresses as RCP0,
RCP1, RCP2, ... RCP11, the Data Type as Double Word, and the Data
Format as Unsigned Decimal.
 Create 12 Numeric Entry elements with the Write Addresses as RCP0,
RCP1, RCP2, ... RCP11, the Data Type as Double Word, and the Data
Format as Floating.
 Create 4 Numeric Entry elements with the Write Addresses as ENRCP0,
Create elements
ENRCP4, ENRCP8, and ENRCP12, the Data Type as Word, and the Data
Format as Unsigned Decimal.
 Create 4 Numeric Entry Elements with the Write Addresses as ENRCP1,
ENRCP5, ENRCP9, and ENRCP13, the Data Type as Double Word, and
D
the Data Format as Floating.
 Create 4 Character Entry elements with the Write Addresses as ENRCP2,
ENRCP6, ENRCP10, and ENRCP14, and the String Length as 6.
 Create 4 Numeric Entry elements with the Write Addresses as ENRCP3,
ENRCP7, ENRCP11, and ENRCP15, the Data Type as Double Word, and
the Data Format as Unsigned Decimal.

102
Lua Command Manual

Examples (recipe.SetRcpWord, recipe.SetRcpDWord, recipe.SetRcpFloat,


recipe.SetCurEnRcpNoName, recipe.SetCurEnRcpGName, recipe.SetEnRcpWord,
recipe.SetEnRcpDWord, recipe.SetEnRcpFlope, recipe.SetEnRcpChangeAscii,
IndexChangeAscii, IndexChangeAscii, recipe. recipe.ChangeEnRcpGIndex)
 Create 2 Numeric Entry elements with the Write Addresses as RCPNO and
RCPG.
 Create 2 Character Entry elements with the Write Addresses as
ENRCPGNAME and ENRCPNONAME.
 Create 2 Numeric Entry elements with the Write Addresses as ENRCPNO
and ENRCPG.
 Create 13 Maintained buttons with the Write Addresses as $100.0, $100.1,
$100.2, …, $100.12.
 The following figure shows the results.

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

Examples (recipe.SetRcpWord, recipe.SetRcpDWord, recipe.SetRcpFloat,


recipe.SetCurEnRcpNoName, recipe.SetCurEnRcpGName, recipe.SetEnRcpWord,
recipe.SetEnRcpDWord, recipe.SetEnRcpFlope, recipe.SetEnRcpChangeAscii,
IndexChangeAscii, IndexChangeAscii, recipe. recipe.ChangeEnRcpGIndex)
 Press $100.0 to write the value 100 to the address RCP3 of the recipe in
units of Word.
if mem.inter.ReadBit(100,0)==1 then
index=3
word=100
ret = recipe.SetRcpWord(index, word)
end

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

Examples (recipe.SetRcpWord, recipe.SetRcpDWord, recipe.SetRcpFloat,


recipe.SetCurEnRcpNoName, recipe.SetCurEnRcpGName, recipe.SetEnRcpWord,
recipe.SetEnRcpDWord, recipe.SetEnRcpFlope, recipe.SetEnRcpChangeAscii,
IndexChangeAscii, IndexChangeAscii, recipe. recipe.ChangeEnRcpGIndex)
 Switch RCPG to 2, and press $100.2 to write the value 10.10 to the
address RCP3 of the recipe in units of Float.
if mem.inter.ReadBit(100,2)==1 then
index=3
floatValue=10.10
ret = recipe.SetRcpFloat(index, floatValue)
end

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

Examples (recipe.SetRcpWord, recipe.SetRcpDWord, recipe.SetRcpFloat,


recipe.SetCurEnRcpNoName, recipe.SetCurEnRcpGName, recipe.SetEnRcpWord,
recipe.SetEnRcpDWord, recipe.SetEnRcpFlope, recipe.SetEnRcpChangeAscii,
IndexChangeAscii, IndexChangeAscii, recipe. recipe.ChangeEnRcpGIndex)
 Press $100.4 to set the recipe group index name with the string
“recipe_GName”.
if mem.inter.ReadBit(100,4)==1 then
newName="recipe_GName"
ret = recipe.SetCurEnRcpGName(newName)
end

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

Examples (recipe.SetRcpWord, recipe.SetRcpDWord, recipe.SetRcpFloat,


recipe.SetCurEnRcpNoName, recipe.SetCurEnRcpGName, recipe.SetEnRcpWord,
recipe.SetEnRcpDWord, recipe.SetEnRcpFlope, recipe.SetEnRcpChangeAscii,
IndexChangeAscii, IndexChangeAscii, recipe. recipe.ChangeEnRcpGIndex)
 Press $100.6 to write the value 70000 to the address ENRCP7 in units of
Double Word.
if mem.inter.ReadBit(100,6)==1 then
index=7
dword=70000
ret = recipe.SetEnRcpDWord(index, dword)
end

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

Examples (recipe.SetRcpWord, recipe.SetRcpDWord, recipe.SetRcpFloat,


recipe.SetCurEnRcpNoName, recipe.SetCurEnRcpGName, recipe.SetEnRcpWord,
recipe.SetEnRcpDWord, recipe.SetEnRcpFlope, recipe.SetEnRcpChangeAscii,
IndexChangeAscii, IndexChangeAscii, recipe. recipe.ChangeEnRcpGIndex)
 Press $100.8 to write the string "POSHEN” to the address ENRCP6 with
the length of 6 bytes.
if mem.inter.ReadBit(100,8)==1 then
index=6
str="POSHEN"
len=6
ret = recipe.SetEnRcpAscii(index, str, len)
end

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

Examples (recipe.SetRcpWord, recipe.SetRcpDWord, recipe.SetRcpFloat,


recipe.SetCurEnRcpNoName, recipe.SetCurEnRcpGName, recipe.SetEnRcpWord,
recipe.SetEnRcpDWord, recipe.SetEnRcpFlope, recipe.SetEnRcpChangeAscii,
IndexChangeAscii, IndexChangeAscii, recipe. recipe.ChangeEnRcpGIndex)
 Press $100.10 to switch RCPG to 2.
if mem.inter.ReadBit(100,10)==1 then
gIdx=2
ret = recipe.ChangeRcpGIndex(gIdx)
end

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

4.10 Screen (screen control)

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.

 screen.Open: open the specified screen


Command
screen.Open
name
Command
Result= screen.Open(screen_id)
expression
Parameter
screen_id: integer; screen ID: 1 to 65535
definition


Example
Example
description
Return value

ft
ret=screen.Open(2)

Open the screen with the Screen ID as 2.

ret: return 1 on success; return 0 on failure

screen.CloseSub: close the specified screen


ra
Command
screen.CloseSub
name
Command
ret = screen.CloseSub(screen_id)
expression
Parameter
screen_id: integer; screen ID: 1 to 65535
definition
Example ret=screen.CloseSub(2)
Example
Close the screen with the Screen ID as 2.
description
Return value ret: return 1 on success; return 0 on failure
D
 screen.IsOpened: check whether the specified screen is open
Command
screen.IsOpened
name
Command
result = screen.IsOpened(screen_id)
expression
Parameter
screen_id: integer; screen ID: 1 to 65535
definition
Example result = screen.IsOpened(1)
Example
Check whether the screen with the Screen ID as 1 is open.
description
Return value ret: return 1 on open; return 0 on not open

110
Lua Command Manual

 screen.Capture: capture screenshot and save it to the external storage device


Command
screen.Capture
name
Command
Result = screen.Capture(disk_ID)
expression
Parameter
disk_ID: integer; disk ID: 2 refers to USB drive and 3 refers to SD card
definition
Example Result = screen.Capture(2)
Example
Capture screenshot and save it to the USB drive.
description
Return value ret: return 1 on success; return 0 on failure

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.

Build Lua end

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

 In the DOPSoft, go to [Screen] > [New Screen] to create a new screen.

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

4.11 String (string operations)

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.

The following sections will explain each in detail.


D

114
Lua Command Manual

 string.len: calculate the string length


Command
string.len
name
Command
length = string.len(string_src)
expression
Parameter
string_src: ASCII string
definition
Example v1 = string.len("ABCDE")
Example
Calculate the length of the string "ABCDE", and thus v1 is 5.
description
Return value v1: string length

 string.format: string formatting


Command
string.format
name
Command
string_ret= string.format(string_fmt, var1, var2, …)
expression
string_fmt: ASCII string; variables: %d, %x, %X, %s, %c, %f, and so on
Parameter
var1: the first variable
definition
var2: the second variable
% operator
%d
%X/%x
%s
%c
%o

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

 string.split: split the string


Command
string.split
name
Command
tName = string.split(src, ",")
expression
Parameter
src: ASCII string
definition
src="John,Andy,Mike"
Example
tName = string.split(src, ",")
Example Separate the strings with ",", and thus tName[1]="John", tName[2]="Andy", and
description tName[3]="Mike".
Return value tName: array

 string.find: locate the string


Command
string.find
name
Command
found_index, end_index = string.find(string, pattern, [start_index])
expression
string: ASCII raw string
Parameter pattern: target string
definition

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

 string.sub: find the string


Command
string.sub
name
Command
string_ret = string.sub(string_src, start_index, [end_index])
expression
string_src: ASCII string
D
Parameter start_index: integer; the starting index of the string with the index base as 1
definition end_index: integer; the ending index of the string with the index base as 1. This
parameter is not mandatory.
Example v1 = string.sub("ABCDE", 2, 4)
Example
Get the 2nd to 4th bytes of the string "ABCDE", and thus v1= "BCD".
description
Return value v1: string; the subset of strings

 string.rep: repeat the string


Command
string.rep
name
Command
destString = string.rep(srcString, repeatCount)
expression
Parameter srcString: string
definition repeatCount: integer; number of repetitions
Example v1 = string.rep("AB+", 2)
Example
Repeat the string "AB+", and thus v1= "AB+AB+".
description
Return value v1: string; the result string

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

 string.lower: convert the string to lowercase


Command
string.lower
name
Command
destString = string.lower(srcString)
expression
Parameter
srcString: string
definition


Example
Example
description
Return value

ft
name = string.lower(“Posheng”)

Convert the string "Posheng" to lowercase, and thus name="posheng".

name: string; the string converted to lowercase

string.upper: convert the string to uppercase


ra
Command
string.upper
name
Command
destString = string.upper(srcString)
expression
Parameter
srcString: string
definition
Example name = string.upper("Posheng”)
Example
Convert the string "Posheng" to uppercase, and thus name="POSHENG".
description
D
Return value name: string; the string converted to uppercase

 string.reverse: reverse the string


Command
string.reverse
name
Command
destString = string.reverse(srcString)
expression
Parameter
srcString: string
definition
Example v1 = string.reverse("ABCDE")
Example
Reverse the string "ABCDE", and thus v1="EDCBA".
description
Return value v1: string; the reversed string

117
Lua Command Manual

 string.byte: convert the string to decimal value


Command
string.byte
name
Command
num1, num2, … = string.byte(string, [start_index, [end_index]])
expression
string: ASCII string
Parameter start_index: integer; the starting index of the string with the index base as 1
definition end_index: integer; the ending index of the string with the index base as 1. This
parameter is not mandatory.
Example v1 = string.byte("ABCDE", 1)
Example
Convert the first byte of the string "ABCDE" to decimal, and thus v1=65.
description
Return value v1: the returned integer

 string.char: convert the decimal value to string


Command
string.char
name
Command
destString = string.char(il, i2, …)
expression
Parameter i1: integer; the decimal value to be converted to the corresponding ASCII character
definition
Example
Example
description
Return value

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".

v1: the result string


ra
 string.gsub: replace the specified string with another string
Command
string.gsub
name
Command
destString = string.gsub(srcString, patternString, replacedString)
expression
srcString: string
Parameter
patternString: the string to be changed
definition
replacedString: the string to be changed is replaced with this replacedString variable
Example s = string.gsub("ABCDE", "C", "x")
D
Example
Replace the "C" in the string "ABCDE" with "x", and thus s = "ABxDE".
description
Return value s: the string after replacement

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

4.12 System library (system parameters)

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

 sys.Sleep: system delay


Command
sys.Sleep
name
Command
sys.Sleep(time)
expression
Parameter
time: integer (ms)
definition
Example sys.Sleep(1000)
Example
The system is delayed for 1000 ms.
description
Return value No return value

 sys.GetTick: get the total uptime of the HMI so far


Command
sys.GetTick
name
Command
startTick=sys.GetTick()
expression
Parameter
No parameters
definition
Example startTick=sys.GetTick()
Example
description
Return value

ft
Get the total uptime of the HMI so far. Unit: milliseconds.

startTick: time; unit: milliseconds

sys.GetInterParam: get the internal parameters of the HMI


Command
ra
sys.GetInterParam
name
Command
value, ret =sys.GetInterParam("paraName")
expression
Parameter paraName: string; name of the internal parameter, which is the same as the internal
definition parameter name in the element address
Example value, ret = sys.GetInterParam("TP_Y")
Example
The value is the Y coordinate of the position where you pressed the HMI screen.
description
y: integer or string; depending on the internal parameters
Return value
D
ret: return 1 on success; return 0 on failure

 sys.BuzzerOn: turn on the buzzer


Command
sys.BuzzerOn
name
Command
sys.BuzzerOn(buzzerType)
expression
Parameter
buzzerType: 0: turn off the buzzer; 1: turn on the buzzer; 2: keep the buzzer on
definition
Example sys.BuzzerOn(1)
Example
Turn on the buzzer.
description
Return value No return value

121
Lua Command Manual

 sys.GetDate: get current time


Command
sys.GetDate
name
Command
year, month, day, week = sys.GetDate()
expression
Parameter
No parameters
definition
Example year, month, day, week = sys.GetDate()
Example
Get the current time (year, month, date, day of the week).
description
year, month, day are the year, month, and date; week indicates Monday to Sunday
Return value
with 0 to 6.

 sys.GetDateString: get current time (in string)


Command
sys.GetDateString
name
Command
dateStr = sys.GetDateString()
expression
Parameter
No parameters
definition
Example
Example
description

Return value

ft
dateStr = sys.GetDateString()

Get the current time (in string).

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.GetTime: get system time


Command
sys.GetTime
name
Command
h, m, s = sys.GetTime()
expression
Parameter
No parameters
definition
Example h, m, s = sys.GetTime()
Example
Get the system time.
description
Return value If the system time is 11:20:35, h=11, m=20, and s=35.

 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.

 sys.GetDiskSpace: get the disk space of the external storage


Command
sys.GetDiskSpace
name
Command
ret, total, free = sys.GetDiskSpace(disk_id)
expression
Parameter
disk_id: integer; 2 refers to USB drive and 3 refers to SD card
definition
Example ret, total, free = sys.GetDiskSpace(2)
Example
Get the total space and remaining space of the USB storage device.
description
ret: return 1 on success; return a negative number on failure
Return value Description
-1 Parameter setting error
Return value
-106 Disk is not ready
total: integer; total disk space (MB)
free: integer; remaining disk space (MB)

123
Lua Command Manual

4.13 Serial port communication (COM communication)

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

 com.Open: open the COM port communication


Command
com.Open
name
Command
ret = com.Open(com_num, interface, databits, parity, stopbits, baudrate, flowcontrol)
expression
com_num: integer; serial communication port number: 1: COM1, 2: COM2, and so on
interface: string; "RS232", "RS422", "RS485"
databits: integer; 7, 8
Parameter
parity: string; "NONE", "ODD", "EVEN", "MARK", "SPACE"
definition
stopbits: integer; 1, 2
baudrate: integer; 9600, ...
flowcontrol: string; "OFF", "CTS_RTS"
Example ret = com.Open(1, "RS232", 8, "EVEN", 1,19200, "OFF")
Open the communication port COM1, set the communication interface to RS232, the
Example
data bits to 8, the parity bits to "EVEN", the stop bit to 1, the baud rate to 19200, and
description
the flow control to OFF.
ret: return 1 on success; return -1 for invalid parameters; return -101 on COM port
Return value
open failure

 com.ReadChars: read characters from the specified communication port (COM)


Command
name
Command
expression

Parameter
definition
com.ReadChars

ft
bytes_read, buffer = com.ReadChars(com_num, len, timeout)

com_num: integer; serial communication port number: 1: COM1, 2: COM2, and so on


len: integer; ASCII string length
timeout: integer; delay time (unit: ms)
ra
Example bytes_read, buffer = com.ReadChars(1, 10, 1000)
Example Read characters from the communication port COM1 with the communication delay
description time of 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
0 Read timeout
Return value
-1 Parameter setting error
D
-100 Port not open
-102 Failed to read
buffer: the string read back

125
Lua Command Manual

 com.WriteChars: write characters to the specified communication port (COM)


Command
com.WriteChars
name
Command
ret = com.WriteChars(com_num, buffer, len, timeout)
expression
com_num: integer; serial communication port number: 1: COM1, 2: COM2, and so on
Parameter buffer: ASCII string
definition len: integer; ASCII string length
timeout: integer; delay time (unit: ms)
Example ret = com.WriteChars(1, “posheng”, 6, 1000)
Example Write the string "posheng" to the communication port COM1 with the communication
description delay time of 1000 ms and the length of 6 bytes.
bytes_read: return the length of the written data (in bytes) on success; return a
negative number on failure
Return value Description
Return value -1 Parameter setting error
-100 Port not open
-103 Failed to write
buffer: the written string


Command
name
Command
expression
Parameter
com.ClearBuffer

ft
com.ClearBuffer: clear buffer data

ret = com.ClearBuffer(com_num, clear_type)

com_num: integer; serial communication port number: 1: COM1, 2: COM2, and so on


ra
definition clear_type: integer; 1: clear read buffer; 0: clear write buffer
Example ret = com.ClearBuffer(1, 1)
Example
Clear the read buffer data of the communication port COM1.
description
ret: return 1 on success; return a negative number on failure
Return value Description
Return value -1 Parameter setting error
-100 Port not open
-104 Failed to clear buffer
D
 com.StationCheck: select the communication port and station number to check whether
the communication is successful
Command
com.StationCheck
name
Command
ret = com.StationCheck(com_num, station)
expression
Parameter com_num: integer; serial communication port number: 1: COM1, 2: COM2, and so on
definition station: integer, station number
Example ret = com.StationCheck(1, 1)
Example Check whether the communication with the station number 1 of communication port
description COM1 is successful.
Return value Description
1 Successful
Return value
0 Failed
-1 Invalid parameter

126
Lua Command Manual

 com.Close: close the communication port


Command
com.Close
name
Command
ret = com.Close(com_num)
expression
Parameter
com_num: integer; serial communication port number: 1: COM1, 2: COM2, and so on
definition
Example ret = com.Close(1)
Example
Close COM 1 connection.
description
Return value ret: return 1 on success; return -1 on invalid parameter

 com.CheckAlive: select the communication parameters to check whether the


communication is successful
Command
com.CheckAlive
name
Command ret = com.CheckAlive(modbus_mode, com_num, interface, databits, parity, stopbits,
expression baudrate, flowcontrol, station, timeout)
modbus_mode: string; modbus mode: "MODBUS_ASCII", "MODBUS_RTU"
com_num: integer; serial communication port number: 1: COM1, 2: COM2, and so on

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.

Return value ret: return 1 on success; return -1 on invalid parameter

127
Lua Command Manual

 com.StationOff: station Off


Command
com.StationOff
name
Command
ret = com.StationOff(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.StationOff(1, 1)
Turn off the station number 1 controller of the communication port COM1, and then
Example
the HMI cannot communicate with the controller.
description
Note: this command is not related to the communication opened by com.Open.

Return value ret: return 1 on success; return -1 on invalid parameter

 com.GetStatus: get the COM port status


Command
com.GetStatus
name
Command
ret = com.GetStatus(com_num)
expression
Parameter
com_num: integer; serial communication port number: 1: COM1, 2: COM2, and so on
definition
Example
Example
description

Return value
ft
ret = com.GetStatus(1)

Check the communication status of COM1

ret: return 1 on success; return a negative number on failure


Return value
-1 Invalid parameter
Description
ra
-100 Port not open
-101 Failed to open COM port

Content of parameter settings


Variable Option Option content Corresponding code
Communication MODBUS_ASCII "MODBUS_ASCII"
modbus_mode
Format MODBUS_RTU "MODBUS_RTU"
COM 1 1
D
com_num COM Port COM 2 2
COM 3 3
RS232 "RS232"
interface Interface RS422 "RS422"
RS485 "RS485"
7 Bits 7
databits Data Bits
8 Bits 8
NONE "NONE"
ODD "ODD"
parity Parity Bits EVEN "EVEN"
MARK "MARK"
SPACE "SPACE"
1 Bits 1
stopbits Stop Bits
2 Bits 2

128
Lua Command Manual

Content of parameter settings


Variable Option Option content Corresponding code
300 300
600 600
900 900
1200 1200
2400 2400
4800 4800
baudrate Baud Rate 9600 9600
14400 14400
19200 19200
28800 28800
38400 38400
57600 57600
115200 115200
OFF "OFF"
flowcontrol Flow Control
CTS_RTS "CTS_RTS"
station Station No. 1 ~ 255 1 ~ 255
timeout

ft
Communication Time 0 ~ 15000 (ms)

Example (COM communication)


The concept map of wiring.

RS232
0 ~ 15000 (ms)
ra
Build hardware RS485
connection HMI HMI

The commands are as follows:


var1 = com.Open(2, "RS485", 8, "NONE", 1, 9600, "OFF")
mem.inter.Write(11, var1)
while true do
D
if (mem.inter.ReadBit(50,0)==1) then
buffer = mem.inter.ReadAscii(1000,10)
ret = com.WriteChars(2, buffer, string.len(buffer), 3000)
mem.inter.Write(12, ret)
end
if (mem.inter.ReadBit(60,0)==1) then
Build Lua var10, buffer = com.ReadChars(2, 10, 3000)
commands for mem.inter.WriteAscii(4,buffer,string.len(buffer))
COM communication mem.inter.Write(13, var10)
end
if (mem.inter.ReadBit(70,0)==1) then
ret = com.Close(2)
mem.inter.Write(14, ret)
end
end
 The HMI opens the COM port and writes the return value to the
memory address $1.
var1 = com.Open(2, "RS485", 8, "NONE", 1, 9600, "OFF")
mem.inter.Write(11, var1)

129
Lua Command Manual

Example (COM communication)


 When $50.0 is triggered, the buffer of the $1000 memory string is read
as a variable with the length of 10 bytes. The HMI writes the variable
buffer to the buffer, and writes the return value to memory address $3.
if (mem.inter.ReadBit(50,0)==1) then
buffer = mem.inter.ReadAscii(1000,10)
ret = com.WriteChars(2, buffer, string.len(buffer), 3000)
mem.inter.Write(12, ret)
end
 When $60.0 is triggered, the buffer of the data is read as a variable
with the data length of 10 bytes in communication port 2. The HMI
writes the read variable bufferto $4, and writes the return value to the
Build Lua memory address $13.
commands for COM if (mem.inter.ReadBit(60,0)==1) then
communication var10, buffer = com.ReadChars(2, 10, 3000)
mem.inter.WriteAscii(4,buffer,string.len(buffer))
mem.inter.Write(13, var10)
end

 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)

Create 3 Maintained buttons and set the Write Addresses to $50.0,


$60.0, and $70.0.
Create 4 Numeric Entry elements and set the Write Addresses to $11,
ra
$12, $13, and $14.
Character Entry
elements  Create 2 Character Entry elements and set the Write Addresses to
$1000 and $4.
 After building the Lua program and creating the elements, compile and
download the project to the HMI Screen (two HMIs display the same
screen).
 After the project is downloaded to the HMI, the COM port is
successfully opened, and the success value is returned to $11.
 Write the string DELTA to $1000 and trigger $50.0, and the string is
written to the buffer.
D
Execution results

 On the second HMI, trigger $60.0 to read the buffer data and write it to
the memory address $4.

 Trigger $70.0 to close the communication port COM2.

130
Lua Command Manual

4.14 TCP communication

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

 tcp.Open: open the TCP network communication


Command
tcp.Open
name
Command
Socket = tcp.Open(ip, port)
expression
Parameter ip: string; "192.168.0.1", ...
definition port: integer
Example Socket = tcp.Open(“192.168.0.1”, 502)
Example
Open the network communication; set the IP to 192.168.0.1 and the port to 502.
description
Socket: return the socket number on success; return a negative value on failure
Return value Description
>0 Successful
Return value
-1 Invalid parameter
-101 Failed to open socket
-145 Too many sockets have been opened

 tcp.Read: read characters (TCP)


Command
tcp.Read
name
Command
expression

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

 tcp.Write: write characters (TCP)


Command
tcp.Write
name
Command
ret = tcp.Write(socket, buffer, len, timeout)
expression
socket: integer; 1 to 8
Parameter buffer: string
definition len: integer; string length
timeout: integer; delay time (ms)
Example ret = tcp.Write(1, “abc123”, 6, 1000)
Example
Send the string “abc123” to socket 1 with the length of 6 bytes.
description
ret: return 1 on success; return a negative value on failure
Return value Description
Return value -1 Invalid parameter
-100 Socket is not open
-103 Failed to write

132
Lua Command Manual

 tcp.Close: close the connection (TCP)


Command
tcp.Close
name
Command
ret = tcp.Close(socket)
expression
Parameter
socket: integer; 1 to 8
definition
Example ret = tcp.Close(1)
Example
Close the socket 1 connection of the TCP communication.
description
ret: return 1 on success; return a negative value or 0 on failure
Return value Description
Return value
-1 Invalid parameter
-100 Socket is not open

 tcp.GetMaxCount: get the maximum number of connections (TCP)


Command
tcp.GetMaxCount
name
Command
count = tcp.GetMaxCount()
expression
Parameter
definition
Example
Example
description
Return value

ft
No parameters

count = tcp.GetMaxCount()

Get the maximum connection number of TCP communication.

count: the maximum number of sockets in the system


ra
 tcp.GetRunCount: get the number of running sockets (TCP)
Command
tcp.GetRunCount
name
Command
count = tcp.GetRunCount()
expression
Parameter
No parameters
definition
Example count = tcp.GetRunCount()
D
Example
Get the number of running sockets of the TCP communication.
description
Return value count: the number of running sockets

 tcp.GetStatus: check the communication status of the socket (TCP)


Command
tcp.GetStatus
name
Command
status = tcp.GetStatus(socket)
expression
Parameter
socket: integer; 1 to 8
definition
Example status = tcp.GetStatus(1)
Example
Check the communication status of socket 1 of the TCP communication.
description
Return value Status: return 1 when the socket is on; return 0 when the socket is off

133
Lua Command Manual

The following is a detailed description with an example of TCP communication.


TCP communication
 In the concept map of wiring, the HMI is the Client, and the PC is the Server
which receives data from the Client.

Build hardware Ethernet


connection PC
HMI (Tcp test tool)

The commands are as follows:


while true do
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

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

4.15 UDP communication

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

 udp.Open: open the UDP network communication


Command
udp.Open
name
Command
Socket = udp.Open(ip, port, local_port)
expression
ip: string; IP address of the receiver, such as "192.168.0.1"
Parameter
port: integer; port of the receiver
definition
local_port: integer; port of the sender
Example Socket = udp.Open(“192.168.123.134”, 502, 602)
Open the UDP network communication with the IP address as 192.168.123.134,
Example
the sender communication port as 602, and the receiver communication port as
description
502.
Socket: return the socket number on success; return a negative value on failure
Return value Description
>0 Successful
Return value
-1 Invalid parameter
-101 Failed to open socket
-145 Too many sockets have been opened

 udp.Read: read characters (UDP)


Command
name
Command
expression

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

 udp.Write: write characters (UDP)


Command
udp.Write
name
Command
ret = udp.Write(socket, buffer, len, timeout)
expression
socket: integer; 1 to 8
Parameter buffer: string
definition len: integer; string length
timeout: integer; delay time (ms)
Example ret = udp.Write(1, “DELTA”, 5, 1000)
Example
Write the string “DELTA” to socket 1 with the length of 5 bytes.
description
ret: return 1 on success; return a negative value on failure
Return value Description
Return value -1 Invalid parameter
-100 Socket is not open
-103 Failed to write

 udp.Close: close the connection (UDP)


Command
name
Command
expression
Parameter
definition
Example
udp.Close

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

 udp.GetMaxCount: get the maximum number of connections (UDP)


Command
D
udp.GetMaxCount
name
Command
count = udp.GetMaxCount()
expression
Parameter
No parameters
definition
Example count = udp.GetMaxCount()
Example
Get the maximum number of connections for UDP communication.
description
Return value count: the maximum number of sockets in the system

141
Lua Command Manual

 udp.GetRunCount: get the number of running sockets (TCP)


Command
tcp.GetRunCount
name
Command
count = tcp.GetRunCount()
expression
Parameter
No parameters
definition
Example count = tcp.GetRunCount()
Example
Get the number of running sockets of the UDP communication.
description
Return value count: number of running sockets

 udp.GetStatus: check the communication status of the socket (UDP)


Command
udp.GetStatus
name
Command
status = udp.GetStatus(socket)
expression
Parameter
socket: integer; 1 to 8
definition
Example status = udp.GetStatus(1)
Example
description
Return value

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

The following is a detailed description with an example of UDP communication.


UDP communication
 The concept map for wiring.
 Use the HMI as the Client and the PC as the Server to receive data from
the Client. Or use the PC as the Client and the HMI as the Server to receive
data from the Client.

Build hardware
connection Ethernet
PC
HMI (Sokit)

The commands are as follows:


while true do
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)
mem.inter.Write(1, Socket)

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

4.16 Text encoding (encoding format change)

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.

 text.GbkToUtf8: convert GBK to UTF-8


Command
text.GbkToUtf8
name
Command
utf8_len, utf8_string = text.GbkToUtf8(ascii_string, ascii_len)
expression
Parameter ascii_string: GBK string
definition ascii_len: integer; string length (in bytes)
Example utf8_len, utf8_string = text.GbkToUtf8("简体", string.len("简体"))

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

4.17 Utility (CRC calculation)

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.

 util.Crc16Modbus: calculate the CRC value


Command
util.Crc16Modbus
name
Command
crc16 = util.Crc16Modbus(str, strLen, initValue)
expression
str: ASCII string
Parameter
strLen: integer; string length (in bytes)
definition
initValue: initial value; the default is 0xFFFF
Example

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

4.18 Convert (Floating-point number conversion)

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.

 convert.IntToFloat: convert the integer to a floating-point number


Command
convert.IntToFloat
name
Command
fVal, ret = convert.IntToFloat(iVal)
expression
Parameter
iVal: 32-bit integer
definition
Example fVal, ret = convert.IntToFloat(0x42F6E666)
Example
Convert the data format of 0x42F6E666 from integer to floating-point number.
description


Return value

ft
ret: return 1 on success; return 0 on failure
fVal: the single-precision floating-point number after conversion

convert.ToNum: convert the string to a 64-bit floating-point number


Command
convert.ToNum
ra
name
Command
dVal, ret = convert.ToNum(str)
expression
Parameter
str: string, for example: "123"
definition
Example dVal, ret = convert.ToNum("123")
Example
Convert the string "123" to a 64-bit floating-point number, and thus dVal = 123.
description
ret: return 1 on success; return 0 on failure
Return value
dVal: the 64-bit floating-point number after conversion
D

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

4.19 Account (permissions and password setup)

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.

 account.Add: add permissions account


Get account status
Get a list of locked accounts
ra
Command
account.Add
name
Command
ret = account.Add(name, password, level)
expression
name: account name
Parameter
password: account password
definition
level: integer; account permissions
Example ret = account.Add("DELTA", "1234", 5)
Example Add an account with the permission level as 5, the username as DELTA, and the
description password as 1234.
D
Return value ret: return 1 on success; return 0 on failure

 account.Delete: delete permissions account


Command
account.Delete
name
Command
ret = account.Delete(name)
expression
Parameter
name: account name
definition
Example ret = account.Delete(“posheng”)
Example
Delete the permissions account of the username “posheng”.
description
Return value ret: return 1 on success; return 0 on failure

157
Lua Command Manual

 account.ChangeName: change permissions account name


Command
account.ChangeName
name
Command
ret = account.ChangeName(srcName, newName)
expression
Parameter srcName: string; the account name to be changed
definition newName: string; new account name
Example ret = account.ChangeName("DELTA", “posheng”)
Example
Change the username "DELTA" to "posheng".
description
Return value ret: return 1 on success; return 0 on failure

 account.ChangePassword: change permissions password


Command
account.ChangePassword
name
Command
ret = account.ChangePassword(Name, newPassword)
expression
Parameter name: string; the account password to be changed
definition newPassword: string; new password
Example ret = account.ChangePassword("DELTA", "0101")


Example
description
Return value

ft
Change the password of the user “DELTA” to 0101.

ret: return 1 on success; return 0 on failure

account.ChangeLevel: change permissions level of the account


ra
Command
account.ChangeLevel
name
Command
ret = account.ChangeLevel(name, newLevel)
expression
Parameter name: string; the name of the account whose permissions is to be changed
definition newLevel: integer; new permission level
Example account.ChangeLevel("DELTA", 1)
Example
Change the permission level of the user “DELTA” to 1.
description
Return value ret: return 1 on success; return 0 on failure
D
 account.GetPassword: get user password
Command
account.GetPassword
name
Command
ret, password = account.GetPassword(name)
expression
Parameter
name: the name of the account whose password is to be obtained
definition
Example ret, password = account.GetPassword(“DELTA”)
Example
Get the password of the user “DELTA” as password.
description
ret: return 1 on success; return 0 on failure
Return value
password: string; the obtained password

158
Lua Command Manual

 account.GetLevel: get permissions level


Command
account.GetLevel
name
Command
ret, level = account.GetLevel(name)
expression
Parameter
name: string; the name of the account whose permission level is to be obtained
definition
Example ret, level = account.GetLevel("DELTA")
Example
level = the permission level of the user “DELTA”.
description
ret: return 1 on success; return 0 on failure
Return value
level: integer; the obtained permission level

 account.GetCurrentLogin: get current login account


Command
account.GetCurrentLogin
name
Command
ret, name, level = account.GetCurrentLogin()
expression
Parameter
No parameters
definition
Example
Example
description

Return value

ft
ret, name, level = account.GetCurrentLogin()

Get the current logged in account.

ret: return 1 on success; return 0 on failure


name: string; the obtained account name
level: integer; the obtained permission level
ra
 account.IsExist: check whether the account exists
Command
account.IsExist
name
Command
ret = account.IsExist(name)
expression
Parameter
name: string; account name
definition
Example ret = account.IsExist("DELTA")
D
Example
Check whether the account of the user “DELTA” exists.
description
Return value ret: return 1 if it exists; return 0 if it does not exist

 account.Login: log in to permissions account


Command
account.Login
name
Command
ret = account.Login(name, password)
expression
Parameter name: string; login account
definition password: string; login password
Example ret = account.Login("DELTA", "0101")
Example Use the username “DELTA” and the password 0101 to log in to the permissions
description account.
Return value ret: return 1 on success; return 0 on failure

159
Lua Command Manual

 account.ResetLockStatus: unlock a locked account


Command
account.ResetLockStatus
name
Command
ret = account.ResetLockStatus(name)
expression
Parameter
name: string; account name
definition
Example ret = account.ResetLockStatus("user01")
Example
Unlock the locked account “user01”.
description
Return value ret: return 1 on success; return 0 on failure

 account.ChangeUserExpiredDays: change account expiration


Command
account.ChangeUserExpiredDays
name
Command
ret = account.ChangeUserExpiredDays(name, expiredDays)
expression
Parameter name: string; the name of the account whose account expiration is to be changed
definition expiredDays: integer; 0 to 9999
Example ret = account.ChangeUserExpiredDays("11", 2)


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

account.ChangePwdExpiredDays: change password expiration


Command
ra
account.ChangePwdExpiredDays
name
Command
ret =account.ChangePwdExpiredDays(name, expiredDays)
expression
Parameter name: string; the name of the account whose password expiration is to be changed
definition expiredDays: integer; 0 to 9999
Example ret = account.ChangePwdExpiredDays("33", 2)
Example Change the password expiration of the account name "33" to 2 days.
description Note: when you log in to this account, the system shows that the password has expired.
Return value ret: return 1 on success; return 0 on failure; return -1 on parameter setting error
D
 account.GetStatus: get account status
Command
account.GetStatus
name
Command
ret = account.GetStatus(name)
expression
Parameter
name: string; the name of the account to obtain account status
definition
Example ret = account.GetStatus("33")
Example
Get the account status of the account name "33".
description
ret: return 1 if the account is locked; return 0 if the account is not locked;
Return value
return -1 on parameter setting error

160
Lua Command Manual

 account.GetLockedList: get a list of locked accounts


Command
account.GetLockedList
name
Command
ret, nameList = account.GetLockedList()
expression
Parameter
No parameters
definition
Example ret, nameList = account.GetLockedList()
Example
Get a list of locked accounts.
description
ret: return 1 on success; return 0 on failure
Return value
nameList: matrix table; return a filename list on success; return nil on failure

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.

Trigger $10.0 to create a permissions account.


ra
 Press the Numeric Entry element, and then you can directly enter the username
and password for login.
D
Execution
results

 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.

 mail.Status: mail function status


Command
mail.Status
name
Command
status = mail.Status()
expression
Parameter
No parameters
definition
Example
Example
description
Return value
1

ft
status = mail.Status()

Get the current mail function 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

 mail.Send: send email


Command
D
mail.Send
name
Command
result, error = mail.Send(receiver, subject, content)
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
Example result, error = mail.Send("[email protected]", "test mail subject", "test mail content")
Example Send an 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
Return value Initial value; no email is being delivered, or the delivery task has
0
just started
-1 Invalid parameter
-121 SMTP is not set

163
Lua Command Manual

 mail.SendFile: send email (including files)


Command
mail.SendFile
name
Command result, error = mail.SendFile(receiver, subject, content, disk_id, file_path,
expression password)
receiver: string; email recipient
subject: string; the subject of the email. If you don’t need a subject, set it to nil
Parameter content: string; email content. If you don’t need any content, set it to nil
definition disk_id: integer; 0 refers to HMI, 2 refers to USB drive, and 3 refers to SD card
file_path: string; the file path under the specified disk_id
password: string; file password. If no password is required, set it to nil
result, error = mail.SendFile("[email protected]", "test mail subject", "test mail content",
Example
2, "test/file.txt", "1234")
Compress the file "test/file.txt” stored in the USB storage device, and set the
Example
password to "1234".. Then, send the file through email to [email protected] with the
description
subject as “test mail subject” and the 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

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

 mail.SendHistory: send email (including history data)


Command
mail.SendHistory
name
Command result, error = mail.SendHistory(bufferNo, dayRange, receiver, subject, content,
expression password)
bufferNo: integer; history buffer ID
dayRange: integer; the day range of the history buffer CSV file. You have to enable
the Save As Multi function to set this parameter with the range of 0 to 7. Set it to 0
Parameter to export the contents of 7 days to the CSV file.
definition receiver: string; email recipient
subject: string; the subject of the email. If you don’t need a subject, set it to nil
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
result, error = mail.SendHistory(1, 0, "[email protected]", "test mail subject", "test mail
Example
content", "1234")
Compress the CSV file of history buffer ID 1 and set the password to "1234", and
Example
then send the file through email to [email protected] with the subject as “test mail
description
subject” and the content as “test mail content“.
result: integer; 0: email delivery has not yet started; 1: email delivery has started
error: integer
Return value Description

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

4.21 Draw (drawing function)

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.

 draw.Point: draw (point)


Command
draw.Point
name
Command
ret = draw.Point(x, y, color)
expression
x: integer; the x coordinate of the HMI
Parameter
y: integer; the y coordinate of the HMI
definition

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).

ret: return 1 on success; return 0 on failure


ra
 draw.Line: draw (line)
Command
draw.Line
name
Command
ret = draw.Line(x1, y1, x2, y2, color, penWidth)
expression
x1: integer; the x coordinate of the starting point of the line
y1: integer; the y coordinate of the starting point of the line
Parameter x2: integer; the x coordinate of the ending point of the line
D
definition y2: integer; the y coordinate of the ending point of the line
color: integer; the RGB565 value is an integer ranges from 0 to 65535
penWidth: integer; the width of the line
Example ret = draw.Line(1, 1, 100, 100, 53388, 5)
Example Draw a line from coordinates (1, 1) to (100, 100) of the HMI with the color of
description RGB53388 (pink).
Return value ret: return 1 on success; return 0 on failure

170
Lua Command Manual

 draw.Rect: draw (rectangle)


Command
draw.Rect
name
Command
ret = draw.Rect(x, y, w, h, color)
expression
x: integer; the x coordinate of the upper left of the rectangle
y: integer; the y coordinate of the upper left of the rectangle
Parameter
w: integer; the width of the rectangle
definition
h: integer; the height of the rectangle
color: integer; the RGB565 value is an integer ranges from 0 to 65535
Example ret = draw.Rect(50, 50, 100, 20, 53388)
Example Draw a rectangle on the coordinate (50, 50) of the HMI with the width of 100, the
description height of 20, and the color of RGB53388 (pink).
Return value ret: return 1 on success; return 0 on failure

 draw.Ellipse: draw (ellipse)


Command
draw.Ellipse
name
Command
ret = draw.Ellipse(x, y, w, h, color)
expression

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

 draw.Clear: clear the drawing


Command
draw.Clear
name
Command
ret = draw.Clear()
expression
D
Parameter
No parameters
definition
Example ret = draw.Clear()
Example
Clear the drawing.
description
Return value ret: return 1 on success; return 0 on failure

 draw.SetAntialiasing: enable/disable anti-aliasing


Command
draw.SetAntialiasing
name
Command
ret = draw.SetAntialiasing(flag)
expression
Parameter flag: set to 1 to enable the anti-aliasing function; set to 0 to disable the anti-aliasing
definition function
Example ret = draw.SetAntialiasing(1)
Example
Enable the anti-aliasing function to make the drawing smoother.
description
Return value ret: return 1 on success; return 0 on failure

171
Lua Command Manual

(This page is intentionally left blank.)

ft
ra
D

172

You might also like