Академический Документы
Профессиональный Документы
Культура Документы
TABLE OF CONTENT
Actions:
■ Install Ztools Beta from USB drive
■ Start Ztools - Beta
■ Select File-New
■ Select ZM400 Family
■ Select ZM405 Model
■ Select Lua Programming Language
■ Select US for Region
■ Click the Zedit Icon
■ Manipulate your screens so Ztools and Zedit are visible simultaneously
Actions:
■ Manipulate your screen#2 so Ztools, and place Zedit on top of it
■ Select the Play button from the Zedit toolbar
■ Zsim will appear on Screen#1
■ Move it to the upper left hand corner of the display
■ Open up a serial window
■ Devices-Serial Port-Serial Port 1
■ Open up setpoint monitor
■ Make Zsim, setpoint monitor, and Serial Port window fit on one screen simultaneously
■ Move the slide bar up/down
■ Observe that the left slider is Coarse, middle is Moderate, and the right slider is Fine
adjustment.
■ Apply a weight on the scale, then click on the TARE key, observe the functionality
■ Apply some weight, press the PRINT key
Module: Exercise3
Actions:
■ Open Exercise3.ztools
■ Click on Zedit - maximize
■ Hover cursor between line number and expand/collapse tool by the word print (line 11)
■ Single click – a red dot appears
■ Click Play
■ Observe how the software stops
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Exercise6
Actions:
■ Open Ztools, Select File-Open
■ Create a project called Exercise6.ztools
■ Click on the Zedit from the ribbon
■ You will see code that demonstrates the following:
■ Print “Hello World” to the output console
■ Create a global variable to the file, name it myVar_0, make this on line 12
■ Assign it to a default value of 555
■ Add a local variable to the Start key function called myVar_0. Make this on line 18
■ Assign it to a default value of 123
■ Place a breakpoint on lines 12 and 18
■ Start the simulator, observe the variable myVar_0 in a watch window, then press play
again.
■ Press the Start key and use the step over tool.
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Exercise7a
function addValues()
local a
local b
local c
a=4
b=6
c=a+b
print ("c="..c)
end
Actions:
■ create a function (addValues) to add the value of 2 local variables and store the sum in
another local variable
■ call the function
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Exercise7b
function addValues(a)
local b
local c
b=6
c=a+b
print ("c="..c)
end
Actions:
■ modify the function to pass one of the variable to add as a parameter to the function
■ call the function passing a value as argument
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Exercise7c
function addValues(a,b)
local c
c=a+b
print ("c="..c)
end
Actions:
■ modify the function to pass both variables to add as a parameters to the function
■ call the function passing values to add as argument
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Exercise7d
function addValues(a,b)
local c
c=a+b
print ("c="..c)
return c
end
Actions:
■ modify the function to return the sum of both parameters
■ call the function passing values to add as argument
■ print the returned value
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Exercise7e
Actions:
■ modify the function to return the sum and product of both parameters to the function
■ call the function passing values to add as argument
■ print returned values
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Exercise8
function setupTable()
myTable = {15, 20, 30, 40} --assign some values to the table
print(myTable[1]) --print the first element of the table
print(myTable[2]) --print the second element of the table
print(myTable[3]) --print the third element of the table
print(myTable[4]) --print the forth element of the table
end
Actions:
■ Create a project called Exercise8.ztools
■ Click on the Zedit from the ribbon
■ Create a comment stating that a function called setupTable is being created to allocate
memory
■ Create a global table called myTable
■ Create a new function called setupTable
■ Assign my table to 4 values, 15, 20, 25, and 30
■ Print to the output console each of these values individually.
■ Call the setupTable function, which will occur on power up.
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Exercise9
function setupTable()
local i = 1
myTable = {15, 20, 30, 40}
print("For loop")
for i=1,4,1 do -- loop upto 4 times, stepping/incrementing by 1
print(myTable[i]) --print myTable
end
print("While loop")
i=1 --reinitialize index variable
while i <= 4 do
print(myTable[i])
i = i + 1 --increment index variable, since a while loop doesn't do this automatically
end
Actions:
■ Open Ztools, Select File-New
■ Create a project called Exercise9.ztools
■ Click on the Zedit from the ribbon
■ Modify the code to use a for loop instead of individual print statements
■ Then add code to use a while loop to do the same thing as the for loop
■ Then add code to only print the first and third numbers in the table using a while loop to
index through the whole table. You must use an if statement to accomplish this.
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Example10
Actions:
■ Open Ztools, Select File-New
■ Create a project called Exercise10.ztools
■ Click on the Zedit from the ribbon
■ Create the Start key event
function awtx.keypad.KEY_START_DOWN()
■ Create a local variable which is assigned by a function called findNumber
■ You will pass this function the number you wish to find inside of myTable from the
previous examples.
■ You will return 2 values from this function once the number is found.
■ Print these variables to the Output console
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Example11
Actions:
■ Open Ztools, Select File-New
■ Create a project called Exercise11.ztools
■ Click on the Zedit from the ribbon
■ Create a function to write on the screen the text passed as parameter
switch the display to user mode
write text
sleep
switch display back to previous mode
■ Create the Start key event handler
■ function awtx.keypad.KEY_START_DOWN()
■ In event handler, call the function to display “START”
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Example12
function onStart()
-- Initializes a print variable to print slot 1
-- 'enteredID' is the name of the global lua variable defined above that it references
-- 'Truck ID' is the variable name, which can be referenced as part of the print format
awtx.fmtPrint.varSet(1, 'enteredID', 'Truck ID', 13)
-- Registers a handler for the print complete event which tells us
-- if a print request was approved or not approved
awtx.weight.registerPrintCompleteEvent(onPrintComplete)
end
if (isEnterKey) then
print('The user entered ID = ' .. enteredID)
else
print('The user canceled the ID entry')
end
end
-- Handler function for the PRINT key down event
-- Called when the PRINT key is pressed down
--
function awtx.keypad.KEY_PRINT_DOWN()
-- Requests a print operation from firmware.
-- The request response will be handled in the onPrintComplete event handler below
awtx.weight.requestPrint()
end
onStart()
Actions:
■ In ZTools
Setup PrintFormat#5 to print the Name and the Value of ApplicationVariable#1.
Setup a Print protocol to send PrintFormat#5 to Port 1.
■ In ZEdit
Define a variable enteredID to hold the Truck ID entered by the user.
Create an OnStart function to create an application variable named ‘Truck ID’
using the value of the variable enteredID and register a function called
onPrintComplete as handler for the PrintComplete event.
In the KEY_F1_DOWN handler, prompt the user for an integer between 0 and
1000. Store the entered value in the variable enteredID
In the PRINT_KEY_DOWN handler, create a print request
Create the onPrintComplete function to print PrintFormat#5, if the print request
succeeds.
■ Start the Simulator
Open the Port 1 window
Press the F1 key
Enter an ID
Press the Print key.
Data should show up in the Port1 window
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Example13
require('ReqConstants')
require('ReqScaleKeys')
require('ReqVariables')
require('ReqDisplayMessages')
local MAX_RECORDS = 10
local records = { }
--
-- Function: onStart
--
-- This function sets up our application when it first runs
--
local function onStart()
end
--
Actions:
■ Include Require files for writing to the display
■ Create a table with permanent storage for up to 10 records with 4 associated fields. ID,
Tare, Target, ingName
■ Create a function to enter/edit items in the list
■ Assign the function to the KEY_SETUP_DOWN event
■ Create a function to list items on the list in the output window when the key F1 is pressed
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Example14
require('ReqConstants')
require('ReqScaleKeys')
require('ReqVariables')
require('ReqDisplayMessages')
sp1Target = 0
function onStart()
-- Create an event listener for SP1
awtx.setPoint.registerOutputEvent(1, onSp1Change)
end
function awtx.keypad.KEY_START_DOWN()
local wt = awtx.weight.getCurrent(1)
local tempGross = wt.gross
sp1Target = tempGross + 100
awtx.setPoint.outputSet(1)
end
onStart()
Actions:
■ In ZTools, set Setpoint#1 activation mode to User Control and deactivation mode to a
variable sp1Target.
■ Create and register an event handler for the setpoint change event. The function should
display Done when the setpoint turns off.
■ When the start key is pressed, set the target weight for the setpoint target to the current
gross weight + 100 and turn Setpoint#1 on.
■ Start the Simulator
Open the Setpoint Monitor window
Press START key. This will set the new target weight and Setpoint#1 is activated
Increase weight on the scale. When the target weight is reached, Setpoint#1 is
deactivated and a message is displayed on the display
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Example15
function onStart()
openPort(1)
end
function openPort(portNum)
local EOMchar = 13 -- Carriage return
local result = awtx.serial.open(portNum, 115200, 8, "none", 1, "none")
if result ~= 0 then
print(string.format("Error opening port %d Result = %d", portNum, result ))
return
end
awtx.serial.setEomChar(portNum, EOMchar)
result = awtx.serial.registerEomEvent(portNum, comMessage)
if (result == 0) then
print('Successfully registered EOM event handler on port ' .. portNum)
else
print('Failed to register EOM event handler on port '.. portNum .. ' error ' .. result)
end
end
function closePort(portNum)
local result
result = awtx.serial.unregisterEomEvent(portNum)
result = awtx.serial.close(portNum)
end
onStart()
Actions:
■ Create an event handler for serial port #1. Read received data and re-send on the same
serial port.
■ Create a function to open the serial port and set the OEM character to carriage return
and register the event handler.
■ In ZSim , enter characters in the Serial port 1 window.
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Example16
function writeGrossWeight()
local SCALE_1 = 1
--Get weight table for scale 1
local currWtTable = awtx.weight.getCurrent(SCALE_1)
local file = io.open("a:\\Gross.txt", "a") -- file opened in append mode
io.output(file)
file:write (string.format("Gross weight =%d %s\r\n", currWtTable.gross,
currWtTable.unitsStr))
file:close()
end
function awtx.keypad.KEY_START_DOWN()
awtx.display.setDisplayBusy()
writeGrossWeight()
awtx.os.sleep(2500)
awtx.display.clrDisplayBusy ()
end
Actions:
■ Display busy on the display
■ Write a function to write the gross weight to a text file
■ Assign the function to the KEY_START_DOWN event
■ Sleep 2500 ms
■ Return the display to weighing mode
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Example17
local DISPLAY_MODE_SCALE = 0 --local constant for setting the display to the weighing mode
local DISPLAY_MODE_USER = 1 --local constant for setting the display to the user mode
function displayWord(word)
local curMode = awtx.display.setMode(DISPLAY_MODE_USER)
awtx.display.writeLine(word)
awtx.os.sleep(750)
awtx.display.setMode(DISPLAY_MODE_SCALE)
end
-- Show
function awtx.keypad.KEY_START_DOWN()
awtx.display.setMode(DISPLAY_MODE_USER)
awtx.graphics.clearScreen()
scr1:show()
segLabel:setText('Hello')
end
function awtx.keypad.KEY_STOP_DOWN()
displayWord('DONE', 750)
awtx.display.setMode(DISPLAY_MODE_SCALE)
awtx.graphics.clearScreen()
end
Actions:
■ Using the graphics library, create a new screen called ‘scr1’
■ Create a label control call ‘lbl1’
■ Set the Location, Size, Font, Visibility and Text for ‘lbl1’
■ Get a reference to the segment control
■ Add ‘lbl1’ and the segment control to ‘scr1’
■ When the START key is pressed
Switch the display to ‘user’ mode
Display ‘scr1’
Set the Text for the segment label to ‘Hello’
■ When the key STOP is pressed
Write “Done” to the display
Return the display to the scale mode
Clear the screen
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Example18
local truck = { }
truck.ID = 456
truck.name = "Dan's Truck"
truck.tare = 200
function onSerialData(portNum)
local rxData, count = awtx.serial.getRx(portNum)
end
Actions:
■ Create a table with id, tare and name
■ Encode the table using json
■ Setup serial port and define a handler to process incoming data
■ Encode the table using the json library
■ Transmitting the string on the serial port
■ In the event handler for the serial port, decode the incoming string and print the content
of the table using the pairs function and using the record structure.
■ From the Simulator Serial Port 1 window, copy data transmitted on the Output Data
window.
■ Paste it in the Text box, add the terminating character and press Send
--[[
Copyright (c) 2015 Avery Weigh-Tronix
Module: Example19
DISPLAY_MODE_SCALE_OBJECT = 0
DISPLAY_MODE_USER_SCRIPT = 1
DISPLAY_MODE_USER_MENU = 2
function onStart()
displayWORD("DEMO")
awtx.os.registerFtpFileAddedEvent (onFtpFileAdded)
awtx.os.registerUsbDriveMountEvent (onUSBMounted)
end
function onFtpFileAdded(filePath)
awtx.display.doBeep()
awtx.display.doBeep()
displayWORD ("NEW")
awtx.display.doBeep()
displayWORD("FILE")
end
function onUSBMounted(newDrive)
awtx.display.doBeep()
awtx.display.doBeep()
displayWORD ("NEW")
awtx.display.doBeep()
displayWORD("USB")
end
function displayWORD(word)
local mode = awtx.display.setMode(DISPLAY_MODE_USER_SCRIPT)
awtx.display.writeLine(word)
awtx.os.sleep(1750)
awtx.display.setMode(mode)
end
onStart()
Actions:
■ Create a function displaying a message on the ZM display to show a new file has been
added.
■ Register this function as the event handler for the FtpFileAddedEvent
■ Create a function displaying a message on the ZM display to show a new file has been
added.
■ Register this function as the event handler for the UsbDriveMountEvent
■ Download the ZTools file to the ZM405
■ Insert a US stick – ZM405 should show message
■ Using an FTP CLIENT, connect to the ZM FTP SERVER.
■ (the ZM405 IP address is 192.168.1.50 )
■ Send a file to the ZM405
ZM Print Tokens
ASCII Table
Escape Sequences
used in formatting strings and ZSim Serial port windows
flags Description
Left-justify within the given field width; Right justification is the default
-
(see width sub-specifier).
Forces to preceed the result with a plus or minus sign ( + or -) even for positive
+
numbers. By default, only negative numbers are preceded with a - sign.
Left-pads the number with zeroes (0) instead of spaces when padding is specified
0
(see width sub-specifier).
width Description
Minimum number of characters to be printed. If the value to be printed is shorter
(number) than this number, the result is padded with blank spaces. The value is not
truncated even if the result is larger.
The width is not specified in the format string, but as an additional integer value
*
argument preceding the argument that has to be formatted.
.precision Description
For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of
digits to be written. If the value to be written is shorter than this number, the result
is padded with leading zeros. The value is not truncated even if the result is longer.
A precision of 0 means that no character is written for the value 0.
For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the
.number decimal point (by default, this is 6).
For g and G specifiers: This is the maximum number of significant digits to be
printed.
For s: this is the maximum number of characters to be printed. By default all
characters are printed until the ending null character is encountered.
If the period is specified without an explicit value for precision, 0 is assumed.
The precision is not specified in the format string, but as an additional integer value
.*
argument preceding the argument that has to be formatted.
LUA Operators
Type Description
Arithmetic + addition
Operate on numbers - Subtraction
• Multiplication
/ division
Relational == equal to
Results in true or false ~= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
Logical and
or
not
Concatenation ..
if .. then .. else …
if condition then
statements
else
statements
end
while loops
while condition do
statements
end
repeat .. until
repeat
statements
until condition
for loops
-- numeric
for var=fromexpr,toexpr,stepexpr do
statements
end
-- Generic
for vars in iterator do
statements
end
Resources
Links
Avery Weigh-Tronix Wiki ?
Lua site http://www.lua.org/
Lua Reference Manual 5.1 (on-line) http://www.lua.org/manual/5.1/
Lua Reference Manual 5.0 (PDF) www.lua.org/ftp/refman-5.0.pdf
Lua User’s site http://lua-users.org/wiki/LuaDirectory
contains several links
Lua Tutorial http://www.tutorialspoint.com/lua/
Books
Lua 5.1 Reference Manual ISBN 8590379833
by R. Ierusalimschy, L. H. de Figueiredo,
W. Celes, Lua.org
Programming in Lua Second Edition
by Roberto Ierusalimschy ISBN 8590379825
Beginning Lua Programming ISBN 0470069171
By Kurt Jung, Aaron Brown
Videos
■ Avery Weigh-Tronix on-demand training videos
■ Several training videos and tutorials on youtube.com
Revision History