SR-X Script Reference - E
SR-X Script Reference - E
Table of Contents
General cautions .................................................................................. 1
3-1 Calculation............................................................................................ 6
3-2 Comparison .......................................................................................... 6
3-3 Combination of condition...................................................................... 6
3-4 Character string concatenation ............................................................ 6
3-5 Pattern matching .................................................................................. 7
Related manuals
Included in DVD-ROM
• SR-X Series User's Manual
Download from KEYENCE homepage
Download the latest manual from the following web page.
BarcodeReader.com
http://www.barcodereader.com/
1 GB SR SCRIPT RM
1-1 Overview 1-3 What Script can do
Definition of Script file Examples of what the script can do
The Script explained in this manual is a simple programming language operating • Extracting arbitrary portion of read data
on the SR-X Series. • Appending arbitrary character strings to read data
Compared to using the setting software (AutoID Network Navigator), using the • Comparing data and outputting result data
script enables more flexible operations for the (1) Edit output data, (2) Edit image • Four arithmetic operations
file name, and (3) Control output terminals functions. • Changing image file names for FTP transmission
• Comparing data and generating output from the output terminals
Related software and features * "Additional information" set with AutoID Network Navigator cannot be used in
combination with the script.
AutoID Network Navigator
Software to set the SR-X Series
FileView
1-4 Script file configuration
Use this to send/receive the script file to/from the SR-X Series. This describes the configuration of the script file (FmtSet.lua).
Web Navigator
The SR-X Series has a web server feature. Settings can be configured by
Script file configuration
connecting to the server in a web browser on a computer. FmtSet.lua file, the script program handled by SR-X Series consists of the following
3 parts.
* See the User's Manual for detailed operating instructions for AutoID Network
Navigator, FileView and Web Navigator. (1) function readformatEvent()……………… Edit read data
(2) function nameformatEvent( idx ) ……… Edit image file name
The process is written in the above parts in combination with variables or functions
Steps before executing the script provided by the SR-X Series system.
FmtSet.lua file
[1] Creating the script file [2] Changing the script execution setting [3] Transferring the script file
The file version can be determined
SCPVERSION="1.00" arbitrarily.
(Can be omitted.)
function readformatEvent()
Process (1) Edit read data
end
as notepad.exe. (This manual describes writing methods for programming.) function User-defined function
[2] Changing the script execution setting User-defined function that can be called
Process
for (1) and (2) (Can be omitted.)*1
Using the AutoID Network Navigator, set the script execution setting of the end
SR-X Series to "Enable".
Putting 2 hyphens at the beginning
[3] Transferring the script file --Comment
enables writing comment.
Transfer the script file (FmtSet.lua) to the SR-X Series.
*1 Multiple user-defined functions can be written as necessary. Skip this if not
* See the User's Manual for instructions on configuring settings for AutoID Network used.
Navigator and Web Navigator. For writing procedure of user-defined functions, refer to "5-7 User-defined
function" (Page 13).
Operations when executing the script
The script is executed when a code is read.
[1] Reading a code [2] Executing the script when reading. [3] Sending the script execution result
GB SR SCRIPT RM 2
(1) Edit read data function readformatEvent() Comment description
readformatEvent() is activated if "Edit data using script" is enabled in the code
Format
reader settings. It is activated at the data transmission timing set in the code
reader.
1-line comment
Format -- Comment Character strings written after -- are treated as the comment.
function readformatEvent()
Variable declaration Multiple-line comment
Process -- [[
The value entered for setSecondData is set as Comment Character strings surrounded with --[[ ]] are treated as the
setSecondData(variable) the second output data. "5-6 Second output data
Comment comment.
setting function" (Page 13)
]]
The value entered into return is output as "Read
return(variable)
data".
end
Example
function readformatEvent()
local a Declares variable a.
a="successful reading!" Assigns "successful reading !" to variable a.
return(a) The value stored in a is output as "Read data".
end
Format
function nameformatEvent()
Variable declaration
Process
The value entered into return is output as "Image
return(variable)
file name".
end
Example
Reference • More than one "Variable" and "Process" can be written. For how to
define variables, refer to "2-1 Naming rules" (Page 4).
• A different function among functions can be called. "Calling the
function" (Page 13)
• Directories can be specified in file names.
To put a image file named "a" into the folder named [image] in the
root folder, specify the value such as image\\a in return.
Note if the applicable folder does not exist in the root folder, it does
not operate normally.
3 GB SR SCRIPT RM
2-1 Naming rules Characters and numerical values that can be assigned to variables
3 types of values can be assigned to variables: character string, numerical value
This describes naming rules for variables, etc. used in the script. and logical value
Text notation : "ABC", "keyence"
Naming methods Character code (decimal number) notation : "\013\010"
character string ([CR][LF])
Script file name Escape sequence : "\r"
(CR)
The script file name handled by SR-X Series is FmtSet.lua only. Integer number : 1, -1
Numerical value Decimal number : 2.0, -2.0
Names defined in the script program Hexadecimal number notation : 0x0a
Types of Name defined in the script program are as follows. True : true
Logical value
False : false
• Variable name, array variable name
• Function name * The condition that no value is assigned to the variable is called nil. If it is referred,
an error will occur. Be sure to assign a value before referring to the variable.
Definitions of variable name and table name
Start the name with a 1-byte alphabet character.
character string
1-byte alphanumeric characters and "_" (underscore) can be used. Text notation
Do not use reserved words. Expresses with the text surrounded with "". Numerical values, symbols and
For reserved words, "8-2 Reserved words/Language elements" (Page 20). character strings can be written in "".
Do not use "_" (underscore) or a number at the beginning of the Character code (decimal number) notation
NOTICE
name. It may not operate normally. Expresses the character string by specifying the ASCII code decimal number after
\.
Numerical value
Positive and negative integer (decimal number notation)
Definition of variable
This expresses positive integer and negative integer as decimal number.
Values such as numerical values, character strings or calculated results can be
Positive and negative decimal (decimal number notation)
assigned to variables.
This expresses positive decimal and negative decimal as decimal number.
Declaration of variable
Important Decimal uses double-precision floating-point number, which may
Format have a margin of error.
Assignment and initialization of variable a= 123 Assigns decimal number integer 123 to variable a.
b = -1.5 Assigns decimal number integer -1.5 to variable b.
Storing a value in a variable is called "assignment".
Assigning a value at the time of variable declaration is called "Initialization". c = 0x0f Assigns hexadecimal number f to variable c.
Example
Logical value
local a=0 Initializes with 0 when declaring variable a. Logical value shows True or False to processed results.
a=1 Assigns 1 to variable a.
Escape sequence
If this is referred with no value assigned to the variable, an error will occur when
executing the script. Pattern Meaning
\r [CR](0x0D)
\n [LF](0x0A)
\a [BEL](0x07)
\b [BS](0x08)
\f [CL](0x0c)
\t [HT](0x09)
\v [VT](0x1B)
\\ Backslash
\" Double quotation
\' Single quotation
\000 Specifies an arbitrary character code as decimal number.
GB SR SCRIPT RM 4
2-3 Table 2-4 Array
The table is the data configuration to collect multiple data. The array is a type of table and a data column that can store multiple values.
Similar to normal variables, reference and assignment can be made.
123 1001 “camera”
Table Product
3 4 5 6 7
Table b
Data (fields) such as LotNo, SerialNo and name are collected in the table (Product).
Numerical values 3,4,5,6,7 are stored in table b.
Example 2) Reference to data variables within the table Assignment and initialization of array variable
Assign values to array valuables using { } and values. Specify multiple values by
local a = 0 Declares variable a.
dividing "," (comma).
local Product ={ LotNo=123 , Serial No=1001 , name="camera"} Declares table Product.
Stores the name field of
Example
a = Product.name
the table Product in a.
local a={} Declares array a.
a = {3,4,5,6} Assigns the 4 values 3, 4, 5, and 6 to array a.
Declares array b and initializes it with the 3
local b ={16,51,55}
values 16, 51, 55.
local b[3] = 10 Assigns 10 to the third value of array b.
Assigns 11 to the fourth value of array b (adds 1
local b[4] = 11
element).
5 GB SR SCRIPT RM
3-1 Calculation 3-3 Combination of condition
This describes arithmetic operators used for general calculations. This describes logical operators used when combining conditions.
Format Format
On the left-hand side and the right-hand side of each operator, write an expression Example
using numerical values or variables of numerical values.
Examples of calculations and results
Example
Arithmetic expression Result
A=B+1 Assigns the result of variable B plus 1 to variable A. true and true true
true or true true
true and false false
Example
true or false true
Reference To obtain only the integer part of division result, use arithmetic function Concatenation operator
math.floor.(Page 11)
Format
3-2 Comparison
variable 1..variable 2
Expresses using 2 periods ".." between
variables.
This describes comparative operators used when comparing numerical values or logical values.
Example
Comparative operator
Declares variable a and initializes with character
Format local a = "Keyence"
string "Keyence".
Declares variable b and initializes with character
local b = "Auto-ID"
Comparative operator Meaning string "Auto-ID".
< smaller local c Declares variable c.
<= smaller or equal c = a..b Assigns values after a and b are connected.
> greater Value of c: "KeyenceAuto-ID"
>= greater or equal
== equal
~= different
On the left-hand side and the right-hand side of each operator, write numerical
values, variables of numerical values or arithmetic expression.
Example
GB SR SCRIPT RM 6
3-5 Pattern matching
This section describes how to search character strings with specific patterns
(Pattern matching) from character strings.
Pattern
Patterns used to search alphabets, numbers, symbols, etc. from character strings.
Meta-character
Pattern Meaning
. All characters (arbitrary 1 character)
%a Character
%c Control character (0x01 to 0x1F, 0x7F)
%d Number
%l Alphabet lowercase
%p Symbol (! " # $ % & ' ( ) * + , - . / : ; > = < ? @ [ \ ] ^ _ ` { | })
%s Space character (0x09,0x0a,0x0b,0x0c,0x0d,0x20)
%u Alphabet uppercase
%w Alphabet and number
%x Hexadecimal number (0 to 9, a to f, A to F)
%z null character
% character*1 Literal character
Quantifier
Pattern Meaning
+ Matches with 1 or more times repetitions of the previous character.
Matches with 0 or more times repetitions of the previous character.
∗
(Longest match)
Matches with 0 or more times repetitions of the previous character.
-
(Shortest match)
? Matches with 0 or 1 time repetition of the previous character.
Range specification
Pattern Meaning
[0-9] Number (0 to 9)
[^0-9] Not number (0 to 9)
[a-z] Alphabet lowercase (a to z)
[A-Z] Alphabet uppercase (A to Z)
[a-zA-Z] Alphabet lowercase and alphabet uppercase (a to z and A to Z)
[0-9a-zA-Z] Number and alphabet (0 to 9, a to z and A to Z)
* If ^ (caret) is added to the beginning of the pattern, the match is fixed to the
beginning of the target character string.
If $ (dollar) is added to the end of the pattern, the match is fixed to the end of the
target character string.
7 GB SR SCRIPT RM
4-1 Conditional branching of Conditional expression
else else
If both "condition 1" and "condition 2" are not fulfilled, "process If type is 20 or more, assign 30 to termID.
process 3 3" is executed. termID=30
end end
No
condition 1
Yes Reference Up to 20 nests can be made for the conditional branching.
No
condition 2
Yes
Reference The "elsif condition then process" sentence can be written multiple
times.
GB SR SCRIPT RM 8
4-2 Repeat process repeat until sentence
repeat until sentences execute process at least once. After the last condition is
This section describes processes when repeating processes. checked, whether to repeat is judged.
Process
break sentence
Use break when going out in the middle of repeat process of while sentences or of
for sentences.
Example Format
while (i<=10) do
If i is 10 or less, the value for i is increased by one. while continuation
i = i+1 condition do
No
end Conditional expression Continuation condition
Yes
Process
break
for sentence Conditional expression
end No
for sentences repeat process using the counter to count the number of repetitions. end
Process
Format
Base
* break can be written only just before end.
for Initialization expression for counter, • Sets the default value to the counter To call break in the middle of the sentence, write as below.
and final value do (variable). The process is repeated until
the value exceeds the final value. Example
Process • Each time the process ends, the counter
increases by one. do
end break
Specifying "increment of counter" end
for Initialization expression for counter, final value • Sets the default value to the counter
and increment of counter do (variable). The process is repeated until
the value exceeds the final value.
• Each time the process ends, the counter
Process increases by positive and negative integer
value specified.
end
Initialization of
No
Final value of counter >= counter
Yes
Process
Addition of counter
Example
for i=1,5 do
If i is 5 or less, the value for a is increased by two.
a=a+2
end
9 GB SR SCRIPT RM
5-1 Data acquisition function imageResult(idx)
These functions are used to acquire information related to code read by the SR-X Acquires information related to read images
Series. Argument idx : Index for image name information array*2
*1 The number of arrays can be obtained using the common function Functions used in imageResult(idx)
"readCount()".
These functions obtain information of captured images. Use as imageResult(n):***
If idx is omitted, the first code is specified. Example) imageResult(1):bankNo()
Function name Argument Return value
bankNo() None Bank number or nil
Functions used in readResult(idx)
imageFileName() None Image file name
These functions obtain information of read data. Use as readResult(n):*** Character string data of the barcode itself or nil
readDatas() None
Example) readResult(n):readData() (Concatenated when multiple data are present.)
readResultIdx() None Index table of append data (readResult(n)) or nil
Argu-
Function name Return value Applicable model
ment
readData() None Read data 2000 1000 750 700
ISO/IEC15415 verification When reading is successful with the second and third banks
i15415Verification() None 2000 1000 750 700
result*1
ISO/IEC TR 29158(AIM DPM-1- readResult(1)
aimVerification() None 2000 1000 750 700
2006) verification result*1
ISO/IEC15416 verification imageResult(1)
i15416Verification() None 2000
result*1
as9132Verification() None SAE AS9132 verification result*1 2000 1000 750 700
• Read error
SEMI T10-070 verification When reading fails
semiT10Verification() None 2000 1000 750 700
result*1
ISO/IEC16022 verification readResult(1)
i16022Verification() None 2000 1000 750 700
result*1
japanMedicalVerification Verification result of the Japan
None 2000 1000 750 700 imageResult(1) imageResult(2) imageResult(3)
() ethical drug barcode*1
imageFileNames() None Image file name*2 2000 1000 750 700
Number of cells:"Vertical/
cellNum() None 2000 1000 700
horizontal"*3
cellSize() None Cell size (mm)*3 2000 1000 700
*1 The functions of the code reader main unit must be enabled beforehand.
*2 When using the script for the edit image file name, the file name after the image
file is changed is output. When multiple image files exist, the value of the
Interdelimiter is the same value set for the SR-X Series.
*3 This function is designed for 2D codes.
When a barcode, not a 2D code, is read, the narrow bar width is stored during
the execution of the cellSize() function. When a barcode is read, cellNum() and
codeSize() cannot be executed.
• When using the advanced multi head, the append data of the slave is all obtained
with readData().
*1 When reading the GS1 composite (JAN/EAN/UPC), the 1D code and 2D code
are connected with a composite delimiter and then output.
*2 When reading the GS1 composite code, values of the 1D code and 2D code
are obtained separately, connected with a partition mark, and then output.
GB SR SCRIPT RM 10
5-2 Common functions (Basic) Character string operation
Function name Argument Return value
string.len(str) str: character string Character string (str) length
These functions can be used in common.
Converts the character string (str) to
string.lower(str) str: character string
lowercase characters.
Character string processing functions
Converts the character string (str) to
string.upper(str) str: character string
Function name Argument Return value uppercase characters.
str: character string str: character string Character string that repeated the character
Character string of m digits counted from the start of string.rep(str,n)
left (str, m) m: number of digits n: numerical value string (str) n times
the character string (str).
(1 to 9990) Reverses the alignment sequence of
string.reverse(str) str: character string
Character string of m digits counted from s digit character string (str).
position from the start of the character string (str). If str: character string
str: character string Portion of character string from ith character
m is not specified, the range from the start position string.sub(str,i,j) i: numerical value
mid(str, s, m) s: 1 to 9990 to jth character of the character string (str)
(s) to the end is specified. If the start position is j: numerical value
m: 1 to 9990
outside the range of character string data, there is no
str: character string Returns the character code for the ith
return value. string.byte(str,i)
i: numerical value character of the character string (str)
str: character string Character string of m digits counted from the end of
right(str, m) string.char(c1,c2,…) c1: numerical value Character string for the character code.
m: 1 to 9990 the character string (str)
str: character string Searches characters that match p among
str: character string
string.find(str,p) p: character string or the character string (str), and then returns
m: 1 to 9990 The mth character string after dividing the character
field(str, m, sep) regular expression the digit number at the head.
sep: character string (str) with the separator (sep)
(1 character) str: character string
Returns characters that match p among the
string.match(str,p) p: character string or
character string (str).
regular expression
Reader status acquisition functions Used for for loop.
str: character string
Each time the characters that match p
Function name Argument Return value string.gmatch(str,p) p: character string or
among the character string (str) are called,
time() None Time regular expression
the result is returned in turn.
readCount() None Total number of reading results*1 str: character string
imageCount() None Total number of saved images*1 p: character string or Replaces characters that match p among
Comparison result regular expression the character string (str) with r.
result() None string.gsub(str,p,r,n)
0: Comparison OK, 1: Comparison NG, 2: ERROR r: character string If n is specified, the first n characters in the
n: numerical value (Can be character string are replaced.
IP address (delimited by underscore)
ipAddress() None omitted)
ex)192_168_100_001
masterSlaveID()*2 None Master/Slave ID
masterSlaveGroupName() None Master/Slave group name Array operation
*1 When the Master/Slave function is used, result data cannot be obtained correctly. Function name Argument Return value
*2 Only used for multi-drop links. tbl: array name
Character strings combined after the data
sep: character (1 character)
table.concat(tbl,sep,i,j) from tbl[i] to l[j] in the array (tbl) is delimited
i: numerical value
Debug function by sep.
j: numerical value
Function name Argument Return value Inserts v into the tbl[i] position in the array
tbl: array name (tbl).
str: character Outputs character strings (str) when debug is table.insert(tbl,i,v) i: numerical value Elements following the ith position are
print(str)
string executed.*2 v: numerical value moved backward by one.
No return value.
*2 The output setting turns ON after the "SCPDBG,1" command is executed. "6-1
Returns the largest index value in the array
Debug methods" (Page 13) table.maxn(tbl) tbl: array name
(tbl).
Deletes the element at the tbl[i] position in
tbl: array name the array (tbl). Elements following the ith
table.remove(tbl,i)
math.ceil(v) v: numerical value Integer with decimals rounded up a: numerical value Exclusive disjunction (exclusive OR) of a
bitXor(a,b)
b: numerical value and b
a: numerical value
math.pow(a,n) an a: numerical value
n: numerical value bitLeftShift(a,b) Shifts a to the left by b bits
b: numerical value
math.sqrt(v) v: positive numerical value √v
a: numerical value
math.deg(rad) rad: numerical value Converts radian values to angular degrees. bitRightShift(a,b) Shifts a to the right by b bits
b: numerical value
math.rad(deg) deg: numerical value Converts angular degrees to radian values. a: numerical value
bitLeftRotate(a,b) Rotates a to the left by b bits
math.sin(rad) rad: numerical value sin b: numerical value
11 GB SR SCRIPT RM
Statistical information (functions for SR Web Monitor use) Timing diagrams
Function name Argument Return value • Normal
None
id: 0-63 Timing input
stat.setName(id,str) Sets the name of the slot with the specified
str: character string
ID (up to 16 characters) Image capture +
None decoding
id: 0-63
stat.setValue(id,v) Sets the value of the slot with the specified
v: 0-65535
ID
• When using invokeNextShot(n)
stat.getName(id) id: 0-63 Name of the slot with the specified ID
stat.getValue(id) id: 0-63 Value of the slot with the specified ID*1 Timing input
stat.incValue(id) id: 0-63 Value of the slot with the specified ID) + 1*1
Image capture +
decoding
*1 Any values that exceed 65535 are set to 65535.
invokeNextShot(n)
Others
Function name Argument Return value Trigger status
v: judgment If v is False, mes is output. Function name Argument Return value
assert( v,mes)
mes: character string The program is force-quit.
Trigger status
Errors (ScriptError) out explicitly and isTriggerActive() None 0: triggers are off
error("",1) -
terminates the program. 1: triggers are on
print(mes) mes: character string Outputs messages for debug. Number of times the script was called while the trigger
scriptExecNum() None
Converts to numerical values. was applied
tonumber(v) v: value Returns nil if value conversion is not
Timing diagrams
possible.
• isTriggerActive
tostring(v) v: value Converts to character strings.
type(v) v: value Variable type
Timing input
Timing input
When using a script to control OUT terminals, the following settings need to be
configured after setting "Enable script features". Trigger control precautions
(This is activated when "SCRIPT CONTROL" is checked only for the OUT
terminals to be controlled by the script.) Limits when performing trigger control
Example settings: When using a script to control all OUT terminals EtherNet/IP,
Out signals
Script PROFINET Saving
LED*2
processing SCRIPT Completion Read images
OK ERROR
CONTROL bit *3 error bit
When capturing
images
When reading is
judged to be Green
successful *1
When outputting
data
*1 This refers to the case in which the conditions for successful reading
specified with the SR-X Series are met. When using script triggers, reading is
not judged as resulting in an error.
*2 This is the color displayed by the OK/ERROR LED (green/red) of the SR-X
Series main unit. When using script triggers, this LED does not light in red.
*3 This indicates "Read Complete" or "Ext. Request Complete" when using
EtherNet/IP or PROFINET with the SR-X Series.
GB SR SCRIPT RM 12
5-6 Second output data setting Example 1
return(“12345”)
Field network PLC end
setSecondData
(“12345ABCDE”) Ethernet socket
SR-X Series
FTP
PC
6-1 Debug methods
This section describes debug methods when the script is executed.
When using a script to set a second output data, the following settings need to be
configured after setting "Enable script features". Operation check procedure
1 Send the FmtSet.lua file to SR-X Series using AutoID Network
Navigator.
3 Read with the code reader and check if the result is correctly
reflected.
• For editing read data
→ Check if the output data is correct.
ScriptError
Script timeout error.
The script execution took too long. *1
Other errors.
This section describes functions that can be uniquely defined by users. *1 The time for script timeout should be 3 seconds.
13 GB SR SCRIPT RM
3 Click [LON] to start reading. Other errors
The content specified as the argument of the print function is output. Error message Description
Referring to this output result, open FmtSet.lua with the text editor and repair Memory shortage has occurred.
not enough memory
the source code. Review the program.
GB SR SCRIPT RM 14
1 Editing read data 3 Conditional branching
1-1 Extracting the first 1 character and the last 2 characters of read data. 3-1 Outputting the type of read codes
Execution result
Read data : keyence
1-3 Deleting all unnecessary zeros at the beginning of barcode. 3-2 Branching according to the read results (OK/NG/ERROR)
Execution result
Read data : keyence
Number of readings : n (n=1 to 4) n=5 and more Read data : 1SR-D100 2SR-D110 3SR-D100HA
Execution result : keyence:count_data is n 5 count over
Execution result :A B Z
*1 The variable value defined here is initialized when the script file is sent again or
the SR main unit is reset.
* When the Master/Slave function is used, the count is not correct.
15 GB SR SCRIPT RM
4 Comparison 4-5 Date comparison
4-1 Comparison when specific characters are included in read data function readformatEvent()
local year=2012
local month=12
function readformatEvent() local date=22
local o_data= "" local code_Year
if string.match(readResult():readData(),"456") then -- When "456" is included in read data local code_Month
o_data = readResult():readData() .. "_OK" -- Appends "_OK" to the end of read data. local code_Date
else -- Other cases local read_data = readResult():readData()
o_data = readResult():readData() .. "_NG" -- Appends "_NG" to the end of read if string.len(read_data) < 8 then
end data. return("reading data is not correct")
return (o_data) end
end code_Year = tonumber(left(read_data ,4)) -- Extracts date information and
code_Month = tonumber(mid(read_data,5,2)) converts it to numerical values.
code_Date = tonumber(mid(read_data,7,2))
Execution result
if code_Year < year then
Read data : 123456789 return "The year is not correct"
elseif code_Month < month then
Execution result : 123456789_OK return "The month is not correct"
elseif code_Date < date then
( "3-5 Pattern matching" (Page 7)is used.) return "The date is not correct"
end
return ("OK") -- Returns OK.
4-2 Comparison when control characters are included in read data
end
function readformatEvent()
local o_data= "" Execution result
if string.match(readResult():readData(),"%c") then -- When control characters are included in read data
o_data = "OK" -- Outputs "OK".
Read data : 22001225. 20111222
else -- Other cases
o_data ="NG" -- Outputs "NG". Execution result : OK The year is not correct
end
return (o_data) This is used to control expiration dates, etc. The date data in the barcode is
end compared with the reference date and output.
Execution result
Execution result : OK
4-3 Deleting control characters when they are included in read data
function readformatEvent()
local read_data
read_data=readResult():readData()
read_data=string.gsub(read_data,"%c","") -- Deletes control characters
read_data=string.gsub(read_data,"%z","") -- Deletes NULL<0x00>
return(read_data)
end
Execution result
function readformatEvent()
local read_data
read_data=readResult():readData()
read_data=string.gsub(read_data,"\010","") -- Deletes [LF]<0x0A>(010 in
return(read_data) decimal number)
end
Execution result
GB SR SCRIPT RM 16
5 Calculations 5-2 Obtaining angular degrees of a tilted barcode
function readformatEvent()
5-1 Obtaining angular degrees of a tilted 2D code
local o_data_u
local o_data --Output data
function readformatEvent()
if result() == 2 then -- Displays the message at read if result() == 2 then --Displays the message at read
return "READ_ERROR" error. return "READ_ERROR" error.
end end
local o_data
local data = readResult():cornerCoordinates() -- Obtains the coordinates at 4 positions of code. local data = readResult():cornerCoordinates() -- Obtains the coordinates at 4 positions of code.
local x1_y1 = field(data,1,":") -- x1/y1:x2/y2:x3/y3:x4/y4 local x1_y1 = field(data,1,":") --x1/y1:x2/y2:x3/y3:x4/y4
local x2_y2 = field(data,2,":") local x2_y2 = field(data,2,":")
local x1 = tonumber(field(x1_y1,1,"/")) local x3_y3 = field(data,3,":")
local y1 = tonumber(field(x1_y1,2,"/")) local x4_y4 = field(data,4,":")
local x2 = tonumber(field(x2_y2,1,"/")) local x1 = tonumber(field(x1_y1,1,"/")) --Converts it to numerical values
local y2 = tonumber(field(x2_y2,2,"/")) local y1 = tonumber(field(x1_y1,2,"/")) and assigns the value to the
if (x1 - x2) == 0 then local x2 = tonumber(field(x2_y2,1,"/")) variable.
o_data = "90".."[deg]_"..readResult():readData() local y2 = tonumber(field(x2_y2,2,"/"))
return (o_data) local x3 = tonumber(field(x3_y3,1,"/"))
elseif (y1 - y2) == 0 then local y3 = tonumber(field(x3_y3,2,"/"))
o_data = "0".."[deg]_"..readResult():readData() local x4 = tonumber(field(x4_y4,1,"/"))
return (o_data) local y4 = tonumber(field(x4_y4,2,"/"))
else
o_data = (y1 - y2)/(x2 - x1)
o_data = math.atan(o_data) -- Obtains arctan. if (x3 - x2) == 0 then
o_data = math.deg(o_data) -- Radian --> Converted to deg(°). if (y3 - y2) > 0 then
o_data = math.floor(o_data) -- Truncates decimals. o_data_u = 0
o_data = o_data.."[deg]_"..readResult():readData() else
return (o_data) -- Appends read data to output. o_data_u = 180
end end
end elseif (y3 - y2) == 0 then
if (x3 - x2) > 0 then
o_data_u = -90
Execution result else
o_data_u = 90
end
Read Data : else
o_data_u = (y3 - y2)/(x3-x2)
o_data_u = math.atan(o_data_u)
Execution result : 45[deg]_keyence 0[deg]_keyence o_data_u = math.deg(o_data_u)
o_data_u = math.floor(o_data_u)
Angular degrees when a workpiece is read from the front are obtained.
* If it is read from angles other than the front, correct angular degrees cannot be if (x3 > x2) then
obtained. o_data_u = -90 + o_data_u
else
* Set the "partition mark" for SR to ":" (colon).
o_data_u = 90 + o_data_u
end
end
end
Execution result
Read Data
Execution result
:
*SR* *SR*
: 30[deg]_SR 0[deg]_SR
Angular degrees when a workpiece is read from the front are obtained.
* If it is read from angles other than the front, correct angular degrees cannot be
obtained.
* Set the "partition mark" for SR to ":" (colon).
function readformatEvent()
local a ={}
a[1] = bitAnd(0,0)
a[2] = bitAnd(1,0)
a[3] = bitAnd(0,1)
a[4] = bitAnd(1,1)
return(a[1]..a[2]..a[3]..a[4])
end
Execution result
17 GB SR SCRIPT RM
6 Editing read image file names 7 Controlling output terminals
When using a script to control OUT terminals, the following settings need to be
6-1 Appending read data to the read image file name configured after setting "Enable script features".
(This is activated when "SCRIPT CONTROL" is checked only for the OUT
function nameformatEvent() terminals to be controlled by the script.)
local read_data
Example settings: When using a script to control all OUT terminals
read_data = readResult():readData() -- Obtains the code reading result.
return(read_data)
end
Execution result
* The read data is specified as the image file name. If a code with exactly the same
data contents is read, the same image file name is adopted. In this case, the old
file is overwritten.
6-2 Appending date and time to the read image file name
function nameformatEvent()
return(time()) -- Obtains date information.
end
Execution result
* Sending settings from AutoID Network Navigator to the SR-X Series 7-1 Controlling output terminals (OK/NG/ERROR)
synchronizes the time of the PC to that of the SR-X Series.
function readformatEvent()
6-3 Appending date and time and read data to the read image file name local r_data = result()
local data = readResult():readData()
function nameformatEvent() if r_data == 0 then --If the result is OK, turn on OUT1.
local read_data outonEvent(1)
read_data = readResult():readData() -- Obtains the code reading result.
read_data = time().."_"..read_data -- Appends date information. elseif r_data == 2 then --If the result is ERROR, turn on
return(read_data) outonEvent(2) OUT2.
end else --In all other situations, turn on
outonEvent(1) OUT1 and OUT2.
outonEvent(2)
Execution result
end
return (data)
Read data : keyence
end
Execution result : 20121222120001_keyence.bmp
* Sending settings from AutoID Network Navigator to the SR-X Series Execution result
synchronizes the time of the PC to that of the SR-X Series.
function readformatEvent()
local r_data = result() --Branching depending on the result (OK/ERROR)
local readData = readResult():readData() --Read data
if r_data == 0 then --OK
if g_latestData == readData then
return ""
end
g_latestData = readData
outonEvent(1)
return readData
--ERROR
elseif r_data == 2 then
outonEvent(2)
return "ERROR"
end
end
Execution result
GB SR SCRIPT RM 18
8 Controlling triggers with scripts 9 Confirmation commands
To control triggers with scripts, you have to set "Enable scripts," and then configure
the settings as shown below. 9-1 MAC address confirmation command
function readformatEvent()
local output_data
output_data=execCmd("EMAC") -Execute the command.
return(output_data)
end
Execution result
8-1 Continuing reading until the character "S" is read at the start of the data Execution result: OK,EMAC,0001FC2D8206
function readformatEvent()
local i 9-2 Reader explanation confirmation command
local read_data
local output_data You can use the SR-X Series to obtain the values set below.
for i = 1, readCount() do
read_data = readResult(i):readData()
if left(read_data,1) == "S" then
return("S data is found!")
elseif result() == 0 then
output_data = "This data isn't S"
invokeNextShot() --Continue reading.
else
output_data = "Read ERROR"
invokeNextShot() --Continue reading.
end
end
return (output_data)
end
Execution result
Execution result : 1
2 10-1 Displaying the count information in SR Web Monitor
3
Trigger OFF
local i=0
function readformatEvent()
i=i+1
stat.setName(1,"Count")
stat.setValue(1,i)
return (readResult():readData())
end
Execution result
19 GB SR SCRIPT RM
8-1 ASCII code table Pattern matching
Pattern Meaning
High-order 4 bits . All characters (arbitrary 1 character)
Hexadecimal %a Character
0 1 2 3 4 5 6 7
number %c Control character (0x01 to 0x1F, 0x7F)
%d Number
Binary
0000 0001 0010 0011 0100 0101 0110 0111 %l Alphabet lowercase
number
%p Symbol (! " # $ % & ' ( ) * + , - . / : ; > = < ? @ [ \ ] ^ _ ` { | })
NUL DLE (SP) 0 @ P ` p
0 0000 %s Space character (0x09,0x0a,0x0b,0x0c,0x0d,0x20)
000 016 032 048 064 080 096 112
SOH DC1 ! 1 A Q a q %u Alphabet uppercase
1 0001
001 017 033 049 065 081 097 113 %w Alphabet and number
STX DC2 " 2 B R b r %x Hexadecimal number (0 to 9, a to f, A to F)
2 0010
002 018 034 050 066 082 098 114 %z null character
ETX DC3 # 3 C S c s % character*1 Literal character
3 0011
003 019 035 051 067 083 099 115
*1 Specify characters other than alphanumeric characters
EOT DC4 $ 4 D T d t
4 0100
004 020 036 052 068 084 100 116
5 0101
ENQ
005
ACK
NAK
021
SYN
%
021
&
5
053
6
E
069
F
U
085
V
e
101
f
u
117
v
8-3 Code type
6 0110
006 022 038 054 070 086 102 118
Low- 7 0111
BEL ETB ' 7 G W g w Code type list used for symbolType of readResult(idx)
007 023 039 055 071 087 103 119
order 4
BS CAN ( 8 H X h x
bits 8 1000 Code type Return value of symbolType
008 024 040 056 072 088 104 120
Reading error 0
HT EM ) 9 I Y i y
9 1001 QR 1
009 025 041 057 073 089 105 121
LF SUB * : J Z j z DataMatrix 2
A 1010
010 026 042 058 074 090 106 122 PDF417/MicroPDF 3
VT ESC + ; K [ k { GS1 DataBar 5
B 1011
011 027 043 059 075 091 107 123 CODE39 6
CL FS , < L \ l | ITF 7
C 1100
012 028 044 060 076 092 108 124
2of5 8
CR GS - = M ] m }
D 1101 NW-7(Codabar) 9
013 029 045 061 077 093 109 125
JAN/EAN/UPC 10
SOH RS . > N ^ n ~
E 1110 CODE128 11
014 030 046 062 078 094 110 126
SI US / ? O _ o DEL COOP2of5 12
F 1111
015 031 047 063 079 095 111 127 CODE93 13
CC-A/B(GS1 DataBar) 14
8-2
CC-A/B(JAN/EAN/UPC) 15
Priority of operator
High ^
not # - (unary)
* / %
+ -
..
< > <= >= ~= ==
and
Low or
GB SR SCRIPT RM 20
Code type list used for symbolIdentifier of readResult(idx) 8-4 Troubleshooting
Code type Detail Symbol ID
QR : Model 1 ]Q0 The script does not operate.
: Model 2, ECI not applied ]Q1
: Model 2, ECI applied ]Q2 If codes can be read normally but the script does not operate, the script execution
: Model 2, ECI not applied, FNC1 (1st) ]Q3 setting of the SR-X Series may not be set to "Enable". Using the AutoID Network
: Model 2, ECI applied, FNC1 (1st) ]Q4 Navigator, set the script execution setting to "Enable".
: Model 2, ECI not applied, FNC1 (2nd) ]Q5
* For using the AutoID Network Navigator, refer to the user's manual.
: Model 2, ECI applied, FNC1 (2nd) ]Q6
DataMatrix : ECC 200 ]d1
Expected results cannot be achieved.
: ECC 200, FNC1 (1st) ]d2
Debug according to "6-1 Debug methods" (Page 13)
: ECC 200, FNC1 (2nd) ]d3
: ECC 200, ECI applied ]d4
An error occurs when executed.
: ECC 200, ECI applied, FNC1 (1st) ]d5
: ECC 200, ECI applied, FNC1 (2nd) ]d6 See the applicable error message on "6-2 Error message list" (Page 14), and
CODE39 No check digit validation ]A0 revise the program.
Check digit is validated and transmitted. ]A1
Check digit is validated but not transmitted. ]A3 Barcodes/2D codes cannot be read.
ITF No check digit validation ]I0
Check digit is validated and transmitted. ]I1 Reading setting may not be correctly made on your code reader. Using the AutoID
Check digit is validated but not transmitted. ]I3 Network Navigator, make the reading setting for the SR-X Series.
NW-7(Codabar) ]F0 * For using the AutoID Network Navigator, refer to the user's manual.
JAN/EAN/UPC UPC-A, UPC-E, JAN/EAN13 ]E0
JAN/EAN8
UPC-A, UPC-E, JAN/EAN13
Addon 2, addon 5
]E4
]E3
8-5 Copyright indication
CODE128 FNC1 not included. ]C0 This software uses the following libraries:
FNC1 on the first digit (GS1-128). ]C1 Lua:
FNC1 on the second digit. ]C2 Copyright (c) 1994-2021 Lua.org, PUC-Rio.
GS1 Databar ]e0
PDF417, Standard ]L0
MicroPDF417 Extended channel interpretation ]L1
Basic channel interpretation ]L2
CODE93 ]G0
2of5 ]S0
COOP2of5 ]X0
Trioptic CODE39 ]A8
Postal ]X0
DotCode generic data ]J0
GS1 format (GS1 DotCode) ]J1
Application Specific ]J2
generic data + ECI ]J3
GS1 format (GS1 DotCode) + ECI ]J4
Application Specific + ECI ]J5
21 GB SR SCRIPT RM
Revision History WARRANTIES AND DISCLAIMERS
(1) KEYENCE warrants the Products to be free of defects in materials and workmanship
Date of printing Version Revision contents for a period of one (1) year from the date of shipment. If any models or samples were
January 2022 First Edition shown to Buyer, such models or samples were used merely to illustrate the general
type and quality of the Products and not to represent that the Products would
necessarily conform to said models or samples. Any Products found to be defective
must be shipped to KEYENCE with all shipping costs paid by Buyer or offered to
KEYENCE for inspection and examination. Upon examination by KEYENCE,
KEYENCE, at its sole option, will refund the purchase price of, or repair or replace at
no charge any Products found to be defective. This warranty does not apply to any
defects resulting from any action of Buyer, including but not limited to improper
installation, improper interfacing, improper repair, unauthorized modification,
misapplication and mishandling, such as exposure to excessive current, heat,
coldness, moisture, vibration or outdoors air. Components which wear are not
warranted.
(2) KEYENCE is pleased to offer suggestions on the use of its various Products. They are
only suggestions, and it is Buyer's responsibility to ascertain the fitness of the
Products for Buyer's intended use. KEYENCE will not be responsible for any damages
that may result from the use of the Products.
(3) The Products and any samples ("Products/Samples") supplied to Buyer are not to be
used internally in humans, for human transportation, as safety devices or fail-safe
systems, unless their written specifications state otherwise. Should any Products/
Samples be used in such a manner or misused in any way, KEYENCE assumes no
responsibility, and additionally Buyer will indemnify KEYENCE and hold KEYENCE
harmless from any liability or damage whatsoever arising out of any misuse of the
Products/Samples.
(4) OTHER THAN AS STATED HEREIN, THE PRODUCTS/SAMPLES ARE PROVIDED
WITH NO OTHER WARRANTIES WHATSOEVER. ALL EXPRESS, IMPLIED, AND
STATUTORY WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, AND NON-INFRINGEMENT OF PROPRIETARY RIGHTS, ARE
EXPRESSLY DISCLAIMED.
IN NO EVENT SHALL KEYENCE AND ITS AFFILIATED ENTITIES BE LIABLE TO
ANY PERSON OR ENTITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, PUNITIVE,
SPECIAL OR CONSEQUENTIAL DAMAGES (INCLUDING, WITHOUT LIMITATION,
ANY DAMAGES RESULTING FROM LOSS OF USE, BUSINESS INTERRUPTION,
LOSS OF INFORMATION, LOSS OR INACCURACY OF DATA, LOSS OF
PROFITS, LOSS OF SAVINGS, THE COST OF PROCUREMENT OF
SUBSTITUTED GOODS, SERVICES OR TECHNOLOGIES, OR FOR ANY MATTER
ARISING OUT OF OR IN CONNECTION WITH THE USE OR INABILITY TO USE
THE PRODUCTS, EVEN IF KEYENCE OR ONE OF ITS AFFILIATED ENTITIES
WAS ADVISED OF A POSSIBLE THIRD PARTY'S CLAIM FOR DAMAGES OR
ANY OTHER CLAIM AGAINST BUYER. In some jurisdictions, some of the foregoing
warranty disclaimers or damage limitations may not apply.
BarcodeReader.com
http://www.barcodereader.com/
You can download technical documents useful for BL/SR/RF introduction and
operation.
GB SR SCRIPT RM 22
23 GB SR SCRIPT RM
KEYENCE CORPORATION
1-3-14, Higashi-Nakajima, Higashi-Yodogawa-ku,
Osaka, 533-8555, Japan
PHONE: +81-6-6379-2211 www.keyence.com/glb
AUSTRIA HONG KONG NETHERLANDS TAIWAN
Ph: +43 (0)2236 378266 0 Ph: +852-3104-1010 Ph: +31 (0)40 206 6100 Ph: +886-2-2721-8080
BELGIUM HUNGARY PHILIPPINES THAILAND
Ph: +32 (0)15 281 222 Ph: +36 1 802 7360 Ph: +63-(0)2-8981-5000 Ph: +66-2-078-1090
BRAZIL INDIA POLAND UK & IRELAND
Ph: +55-11-3045-4011 Ph: +91-44-4963-0900 Ph: +48 71 368 61 60 Ph: +44 (0)1908-696-900
CANADA INDONESIA ROMANIA USA
Ph: +1-905-366-7655 Ph: +62-21-2966-0120 Ph: +40 (0)269 232 808 Ph: +1-201-930-0100
CHINA ITALY SINGAPORE VIETNAM
Ph: +86-21-3357-1001 Ph: +39-02-6688220 Ph: +65-6392-1011 Ph: +84-24-3772-5555
CZECH REPUBLIC KOREA SLOVAKIA
Ph: +420 220 184 700 Ph: +82-31-789-4300 Ph: +421 (0)2 5939 6461
FRANCE MALAYSIA SLOVENIA
Ph: +33 1 56 37 78 00 Ph: +60-3-7883-2211 Ph: +386 (0)1 4701 666
GERMANY MEXICO SWITZERLAND
Ph: +49-6102-3689-0 Ph: +52-55-8850-0100 Ph: +41 (0)43 455 77 30
Specifications are subject to change without notice. A6WW1-MAN-2121
GB SR SCRIPT RM 24
Copyright (c) 2022 KEYENCE CORPORATION. All rights reserved.
193308GB 2012-1 C88GB Printed in Japan
*C88GB-1*