QP Script
QP Script
Mathematical expressions
Conditional expressions
In some cases, you can use Constant values or Variable References in place
of expressions.
Warning: Script execution is not as fast as other parts of QuickPanel applications, especially
scripts that access and change large amounts of variable data. NEVER use QPScript scripts for
the control of machines or actions that can result in either operator injury or damage to equipment.
Examples:
MyINT1 = 3
MyINTSum = MyINT1 + MyINT2
MyBOOL = 1
MyBOOLResult = MyBOOL1 XOR MyBOOL2
Branching statements let you change the execution flow of a script. Normally,
statements in a script are executed sequentially starting from the beginning.
Branching statements let you change this, to a limited extent.
There are two methods of program branching in QPScript: IF clauses and
LOOP clauses.
IF clauses
An IF Clause lets you execute certain statements only when some condition is
met. An IF Clause uses the if, else, and endif keywords. The syntax for an
IF Clause is as follows:
if ([Conditional Expression])
[True Statement block]
else
[False Statement block]
endif
[Conditional Expression] is a conditional expression, which evaluates to True
or False.
[True Statement block] is a block of statements to execute if the condition
evaluates to True. When the else keyword is reached, execution continues
after the matching endif keyword.
[False Statement block] is a block of statements to execute if the condition
evaluates to False. The "else" and "[False Statements]" portions are optional.
Regardless, after statements in [True Statements] or [False Statements] are
executed, execution continues after "endif".
For more complicated cases, you can nest additional IF clauses within other
IF clauses. For example:
if (MyDINT > 5)
[Statement block A]
if (MyDINT == 9)
[Statement block B]
endif
else
[Statement block C]
endif
In the above example:
LOOP clauses
Note: LOOP clauses are not supported by QuickPanel 2 models.
+<< >>
AND OR XOR
Last < > <= >= == <>
Notes
In QScript, The NOT, AND, OR, and XOR operators are treated as mathematical bitwise
operators between integer values. They are not treated as boolean operators.
In QPScript, a conditional expression can have only one conditional operator (<, <, <=, >=.
==, <>).
For example:
(3 > 1) OR (8 < Invalid. In QPScript, OR is used only as a bitwise operator
7)
and there can be only one conditional operator in a
conditional expression.
3 > 1 OR 8
"1 OR 8" evaluates to 9 (0001 OR 1000 = 1001); the whole
expression therefore evaluates to 3 > 9 or 0.
When using the subtraction operator, make sure you add a space
between the "-" and the second operand. Otherwise, the dash will be
treated as a negation operator for the second operand:
MyINT1 = MyINT2 - Interpreted as "MyINT2" and "negative 3",
3
resulting in a syntax error.
MyINT1 = MyINT2-3
MyINT1 = MyINT2- Interpreted as "MyINT2 minus 3", a valid
3
expression.
When using the AND, OR, XOR, and NOT operators, the indicated
operation is performed in a bitwise mannerthat is, on each bit of the
operands' values. For example, 6 AND 3 (0110 AND 0011 in binary
notation) evaluates to 2 (0010 in binary).
More complex expressions with more than one operator are evaluated in a
specific order. This order is called precedence. See precedence rules below.
Notes
The order of expressions for the - (subtraction), / (division) and % (modulo division)
operators is important. "8 - 5" evaluates to a different value than "5 - 8".
The results of all expressions are 16-bit integer values. In the case of the division operator
(/), the fractional portion is discarded.
You cannot use conditional operators (such as "<" or "==") in a mathematical expression.
See Precedence Rules below. To calculate the desired results, you may need
to enclose bit shift expressions within parentheses:
MyINT1 = (MyINT2 << 3) + (MyINT3 >> 2)
MyINT1 = (MyINT2 >> 8) * 2
If [exprValue] is the name of an integer variable and the expression appears
as a separate statement (that is, it is not part of an assignment statement or
conditional expression), the result of the shifting operation is stored in the
same variable value that it was applied to. For example, the following two
statements are functionally identical:
MyINT = MyINT << 3
MyINT << 3
Note: The above syntax is valid only if [exprValue] is an integer variable. Any other kind of
expression results in a validation error.
NOT
*/%
+<< >>
AND OR XOR
Last (lowest
precedence):
You can override this order by enclosing sub-expressions in parentheses, "("
and ")".
For example:
2+5*6
(2 + 5) * 6
6 AND 3 OR
8
Evaluates to 2 + 30 = 32.
Evaluates to 7 * 6 = 42.
Evaluates first to 2 (0010, the result of 0110 AND 0011) OR 8
(1000), which itself evaluates to 10 in decimal (1010 in binary)
QPScript Language
else
endif
loop
QPScript Language
Mathematical Operators
Mathematical operators are used in mathematical expressions. Supported
mathematical operators are as follows:
+
*
/
%
AND
OR
XOR
NOT
Addition.
Subtraction.
Multiplication.
Division. All results are truncated to the nearest integer.
Modulo division. That is, the remainder of a division operation.
AND (boolean or bitwise).
OR (boolean or bitwise).
Exclusive-OR (boolean or bitwise).
NOT (boolean or bitwise).
Conditional Operators
Conditional operators are used in conditional expressions, in IF statements.
Supported conditional operators are as follows.
<
>
<>
==
Less than.
Greater than.
Not equal to.
Equal to.
<=
>=
QPScript Language
Set
Neg
break
Immediately exits a loop, before the iterator variable reaches 0 and before
the endloop statement is reached.
Supported by: QuickPanel targets; not supported by QuickPanel 2 models
QPScript syntax
break
See Also: loop, endloop
Comments
The break statement is typically used to exit a loop early due to some error
message, or because a particular value in a series of values are found. It is
used in the middle of a LOOP clause. You can use as many break statements
within a LOOP clause as you want.
LOOP Clauses are not supported by QuickPanel 2 models. For more
information, see LOOP Clauses.
Example
The following example searches through the bits of the variable ErrorStatus,
exiting as soon as it finds the first bit that is set to 1. When the loop ends,
the variable BitErrorNumber either indicates which bit was set, or is -1 if no
set bits were found. All variables in this example are INT variables.
BitErrorNumber = -1
MyIndex = 16
loop (MyIndex)
BitIndex = 16 - MyIndex
MyTemp1 = 0
Set(MyTemp1, BitIndex)
MyTemp2 = MyTemp1 AND ErrorStatus
if ((MyTemp1 AND ErrorStatus) > 0)
BitErrorNumber = BitIndex
break
endif
endloop
else
Indicates the beginning of the else section of an IF Clause, which is executed
when the if statement's conditional expression evaluates to 0 (False).
Supported by: QuickPanel targets
QPScript syntax
else
See Also: if, endif
Parameters
CondExpression: Conditional Expression that evaluates to 1 (True) or 0
(False).
Comments
For the complete syntax for if, else, and endif statements, see IF Clause.
In summary:
Each if statement must have a matching endif statement, indicating the end
of the IF clause. You can nest up to three levels of IF clauses within one
another. Using "else" is optional, but each if statement can have only one
matching else statement.
Example
The following example tests the value of MyINT. If MyINT is greater than 100,
MyBOOL is set to 1. Otherwise, MyBOOL is set to 0.
endif
Indicates the end of an IF Clause. Execution continues after the endif
statement, regardless of the results of the if statement's conditional
expression.
Supported by: QuickPanel targets
QPScript syntax
if(exprCondition)
See Also: else, if
Parameters
exprCondition: Conditional Expression that evaluates to 1 (True) or 0 (False).
Comments
For the complete syntax of if, else, and endif statements, see IF Clause. In
summary:
MyBOOL = 1
else
MyBOOL = 0
endif
endloop
Indicates the end of a LOOP Clause.
Supported by: QuickPanel targets; not supported by QuickPanel 2 models
QPScript syntax
endloop
See Also: break, loop
Comments
When execution reaches the endloop statement, the value of the iterator
variable (as specified in the initial loop statement) is decremented by 1. If the
resulting value is 0 or less, the loop ends and execution continues with the
next statement after endloop. Otherwise, execution returns to the beginning
of the loop.
Each loop statement must have a matching endloop statement, indicating
the end of the LOOP Clause. You cannot nest a loop inside another.
A loop can exit early with the break statement.
LOOP Clauses are not supported by QuickPanel 2 models. For more
information, see LOOP Clauses.
Example
The following example searches through the bits of the variable ErrorStatus,
exiting as soon as it finds the first bit that is set to 1. When the loop ends,
the variable BitErrorNumber either indicates which bit was set, or is -1 if no
set bits were found. All variables in this example are INT variables.
BitErrorNumber = -1
MyIndex = 16
loop (MyIndex)
BitIndex = 16 - MyIndex
MyTemp1 = 0
Set(MyTemp1, BitIndex)
MyTemp2 = MyTemp1 AND ErrorStatus
if ((MyTemp1 AND ErrorStatus) > 0)
BitErrorNumber = BitIndex
break
endif
endloop
if
Indicates the beginning of an IF Clause, which performs a test or comparison,
branching execution based on the result.
Supported by: QuickPanel targets
QPScript syntax
if (exprCondition)
See Also: else, endif
Parameters
exprCondition: Conditional Expression that evaluates to 1 (True) or 0 (False).
Comments
For the complete syntax of if, else, and endif statements, see IF Clause. In
summary:
loop
Indicates the beginning of a LOOP Clause, which repeats a set of statements
a specified number of times.
Supported by: QuickPanel targets; not supported by QuickPanel 2 models
QPScript syntax
loop(varIterator)
See Also: break, endloop
Parameters
varIterator: Integer variable to use as the Iterator variable. Before the loop
statement, this variable should be set to the number of times you
want to execute the LOOP Clause. If 0 or less, statements within
the LOOP Clause are not executed at all.
Note: varIterator must be an integer variable with an Internal data source. It cannot
be a constant value or the result of an expression.
Comments
Each loop statement must have a matching endloop statement, indicating
the end of the loop clause. You cannot nest a loop inside another.
You can exit a loop early with the break statement.
Upon each iteration, the value of the iterator variable is decreased by 1.
Assuming that a break statement was not used, when the loop exits, the
value of the iterator variable will be 0.
LOOP Clauses are not supported by QuickPanel 2 models. For more
information, see LOOP Clauses.
Example
The following example searches through the bits of the variable ErrorStatus,
exiting as soon as it finds the first bit that is set to 1. When the loop ends,
the variable BitErrorNumber either indicates which bit was set, or is -1 if no
set bits were found. All variables in this example are INT variables.
BitErrorNumber = -1
MyIndex = 16
loop (MyIndex)
BitIndex = 16 - MyIndex
MyTemp1 = 0
Set(MyTemp1, BitIndex)
MyTemp2 = MyTemp1 AND ErrorStatus
if ((MyTemp1 AND ErrorStatus) > 0)
BitErrorNumber = BitIndex
break
endif
endloop
+ (Addition)
Indicates an addition operation in a mathematical or conditional expression.
Supported by: QuickPanel targets
See Also: - (Subtraction operator)
Comments
The + and - operators have the same precedence within expressions. For a
complete description of the syntax for mathematical expressions, see
Mathematical expressions.
Warning: In QPScript, operands and expression results are 16-bit integers. If the result of the
expression is greater than 65535, the high word is discarded.
Example
The following example sets the value of MyINT1 to 3 more than the value of
MyINT2.
MyINT1 = MyINT2 + 3
AND
Performs a bitwise AND operation on the bits of two integer values.
Supported by: QuickPanel targets
See Also: OR, NOT, XOR
Comments
The results of an AND operation for each bit position are as follows:
1 (True) AND 1
=1
(True)
1 (True) AND 0
=0
(False)
0 (False) AND 1
=0
(True)
0 (False) AND 0
=0
(False)
The AND, OR, and XOR operators have the same precedence within
expressions. For a complete description of the syntax for mathematical
expressions, see Mathematical expressions.
Example
The following example sets the value of MyINT1 to the result of 5 (0101) AND
12 (1100), that is, 4 (0100).
MyINT = 5 AND 12
Example
The following shifts the variable MyINT to the left by 2 binary places. If its
original value were 3 (11 in binary), its value would be changed to 12 (1100
in binary).
MyINT << 2
/ (Division)
Performs a division operation within a mathematical or conditional
expression.
Supported by: QuickPanel targets
See Also: * (Multiplication operator), % (Modulo Division operator)
Comments
In QuickPanel scripts, all mathematical operations are performed on 16-bit
integer values. This means that any fractional results of a division operation
will be lost. You can determine the remainder of a division operation with the
% (modulo division) operator. For example:
17 / 7 resolves to 2
17 % 7 resolves to 17 - (7*2), that is, 3
The *, /, and % operators have the same precedence within expressions. For
a complete description of the syntax for mathematical expressions, see
Mathematical expressions.
Warnings: Before performing the division operation, ensure that the second operand is not 0. If
a divide-by-zero error occurs in a script, the QuickPanel application will stop working and must be
restarted; no indication of the divide-by-zero error will be provided. It is the responsibility of the
application developer to ensure that a divide-by-zero condition does not occur.
Example
The following example sets the value of MyINT1 to the value of MyINT2
divided by 2, discarding any fractional remainder.
MyINT1 = MyINT2 / 2
== (Equal to)
Performs an equality comparison between the results of two mathematical
expressions.
Supported by: QuickPanel targets
See Also: < (less than), > (greater than), <= (less than or equal to), >=
(greater than or equal to), <> (not equal to)
Comments
The == operator is used only in conditional expressions. It compares two
values, the results of two mathematical expressions. If the values are equal,
the result of the conditional expression is 1 (True); otherwise, the result is 0
(False).
The operands are not affected by the comparison.
Example
The result of the following if statement is True only if MyINT is equal to 5.
if (MyINT == 5)
% (Modulo division)
Indicates an integral division (modulus) operation within a mathematical or
conditional expression.
Supported by: QuickPanel targets
See Also: * (Multiplication operator), / (Division operator)
Comments
The result of a modulo division operation is the integer remainder of the
division result. Since regular division operations are performed only on
integers, the modulo operator can be used to determine the true results of a
division. For example:
17 / 7 resolves to 2
17 % 7 resolves to 17 - (7*2), that is, 3
The *, /, and % operators have the same precedence within expressions. For
a complete description of the syntax for mathematical expressions, see
Mathematical expressions.
Example
The following example sets the value of MyINT1 to the remainder of MyINT2
divided by 7.
MyINT1 = MyINT2 % 7
* (Multiplication)
Performs a multiplication operation within a mathematical or conditional
expression.
Supported by: QuickPanel targets
See Also: / (Division operator), % (Modulo Division operator)
Comments
The *, /, and % operators have the same precedence within expressions. For
a complete description of the syntax for mathematical expressions, see
Mathematical expressions.
Warning: In QPScript, operands and expression results are 16-bit integers. If the result of the
expression is greater than 65535, the high word is discarded.
Example
The following example sets the value of MyINT1 to 6 times than the value of
MyINT2.
MyINT1 = MyINT2 * 6
NOT
Performs a bitwise NOT operation (an "inversion") on the bits of an integer
value.
Supported by: QuickPanel targets
See Also: AND, XOR, OR
Comments
The results of a NOT operation on each bit position are as follows:
NOT 1 (True) = 0
NOT 0
=1
(False)
Note that, unlike AND, XOR, and OR, the NOT operator works on a single
value or result. For a complete description of the syntax for mathematical
expressions, see Mathematical expressions.
Example
The following example sets the value of the 16-bit INT variable MyINT1 to the
result of NOT 5 (0000 0000 0000 0101), or 65530 (1111 1111 1111 1010).
MyINT = NOT 5
OR
Performs a bitwise OR operation on the bits of two integer values.
Supported by: QuickPanel targets
See Also: AND, NOT, XOR
Comments
The results of an OR operation for each bit position are as follows:
1 (True) OR 1 (True) = 1
1 (True) OR 0 (False) = 1
0 (False) OR 1 (True) = 1
0 (False) OR 0 (False) = 0
The AND, OR, and XOR operators have the same precedence within
expressions. For a complete description of the syntax for mathematical
expressions, see Mathematical expressions.
Example
The following example sets the value of MyINT1 to the result of 5 (0101) OR
12 (1100), that is, 13 (1101).
MyINT = 5 OR 12
- (Subtraction)
Performs a subtraction operation in a mathematical or conditional expression.
Supported by: QuickPanel targets
See Also: + (Addition operator)
Comments
The + and - operators have the same precedence within expressions. For a
complete description of the syntax for mathematical expressions, see
Mathematical expressions.
When using the subtraction operator, make sure you add a space between
the "-" and the second operand. Otherwise, the dash will be treated as a
negation operator for the second operand:
MyINT1 = MyINT2 -3
MyINT1 = MyINT2-3
MyINT1 = MyINT2- 3
Example
The following example sets the value of MyINT1 to 3 less than the value of
MyINT2.
MyINT1 = MyINT2 - 3
XOR
Performs a bitwise XOR (exclusive-OR) operation on the bits of two integer
values.
Supported by: QuickPanel targets
See Also: AND, NOT, OR
Comments
The results of an XOR operation on each bit position are as follows:
1 (True) XOR 1 (True) = 0
1 (True) XOR 0 (False) = 1
0 (False) XOR 1 (True) = 1
0 (False) XOR 0 (False) = 0
The AND, OR, and XOR operators have the same precedence within
expressions. For a complete description of the syntax for mathematical
expressions, see Mathematical expressions.
Example
The following example sets the value of MyINT1 to the result of 5 (0101) XOR
12 (1100), that is, 9 (1001).
MyINT = 5 XOR 12
DrawCircle
Draws a circle on the QuickPanel display.
Supported by: QuickPanel targets; not supported by QuickPanel 2 models
QPScript syntax
DrawCircle(intHorPos, intVerPos, intRadius, intColor, intAttributes)
See Also: DrawDot, DrawLine, DrawRect
Parameters
intHorPos:
Black
1:
Blue
2:
Green
3:
Cyan
4:
Red
5:
Magenta
6:
Yellow
7:
White
Example
The following example draws a solid white 10-pixel wide circle at coordinates
(30,30).
DrawCircle(30, 30, 5, 7, 1)
DrawDot
Draws a circle on the QuickPanel display.
Supported by: QuickPanel targets; not supported by QuickPanel 2 models
QPScript syntax
DrawDot(IntHorPos, IntVerPos, intColor)
See Also: DrawCircle, DrawLine, DrawRect
Parameters
IntHorPos: Integer constant or variable, specifying the horizontal pixel
position of the dot.
IntVerPos: Integer constant or variable, specifying the vertical pixel position
of the dot.
intColor: Integer constant or variable, specifying the color of the dot. This
can be from 0 through 7. For a list of available colors, see
Comments below.
Comments
This function cannot be used in Application scripts.
Pixel positions are numbered starting from 0. Pixel (0,0) is the top-left
corner. The dot's coordinates must fall within the panel's pixel dimensions.
For details, see QuickPanel pixel coordinate system.
Available colors for intColor are as follows:
0:
Black
1:
Blue
2:
Green
3:
Cyan
4:
Red
5:
Magenta
6:
Yellow
7:
White
Example
The following example draws a red dot at coordinates (30,30).
DrawDot(30, 30, 4)
DrawLine
Draws a straight line on the QuickPanel display.
Supported by: QuickPanel targets; not supported by QuickPanel 2 models
QPScript syntax
DrawLine(intHor1Pos, intVer1Pos, intHor2Pos, intVer2Pos, intColor,
intStyle)
See Also: DrawDot, DrawCircle, DrawRect
Parameters
IntHor1Pos: Integer constant or variable, specifying the horizontal pixel
position of the line's starting position.
IntVer1Pos: Integer constant or variable, specifying the vertical pixel position
of the line's starting position.
IntHor2Pos: Integer constant or variable, specifying the horizontal pixel
position of the line's ending position.
IntVer2Pos: Integer constant or variable, specifying the vertical pixel position
of the line's ending position.
intColor:
Integer constant or variable, specifying the color of the line. This
can be from 0 through 7. For a list of available colors, see
Comments below.
intStyle:
Integer constant or variable, specifying the style of the line. This
can be one of the following:
0: a solid line.
Comments
This function cannot be used in Application scripts.
Pixel positions are numbered starting from 0. Pixel (0,0) is the top left corner.
Both the line's starting coordinates and ending coordinates must fall within
the panel's pixel dimensions. For details, see QuickPanel pixel coordinate
system.
Available colors for intColor are as follows:
0:
Black
1:
Blue
2:
Green
3:
Cyan
4:
Red
5:
Magenta
6:
Yellow
7:
White
Example
The following example draws a red diagonal arrow from the top left corner to
coordinates (30,30).
DrawLine(0, 0, 30, 30, 4, 1)
DrawRect
Draws a rectangle on the QuickPanel display.
Supported by: QuickPanel targets; not supported by QuickPanel 2 models
QPScript syntax
DrawRect(intHor1Pos, intVer1Pos, intHor2Pos, intVer2Pos, intColor,
intAttributes)
See Also: DrawDot, DrawLine, DrawCircle
Parameters
intHor1Pos: Integer constant or variable, specifying the horizontal pixel
position of the top-left corner of the rectangle.
intVer1Pos: Integer constant or variable, specifying the vertical pixel
position of the top-left corner of the rectangle.
intHor2Pos: Integer constant or variable, specifying the horizontal pixel
position of the bottom-right corner of the rectangle.
intVer2Pos: Integer constant or variable, specifying the vertical pixel
position of the bottom-right corner of the rectangle.
intColor:
Integer constant or variable, specifying the color of the line.
This can be from 0 through 7. For a list of available colors, see
Comments below.
intAttributes: Integer constant or variable. If 0, the rectangle is drawn as an
outline. If 1, the rectangle is drawn as a solid rectangular block.
Comments
This function cannot be used in Application scripts.
Pixel positions are numbered starting from 0. Pixel (0,0) is the top left corner.
The intHor2Pos parameter must be greater than intHor1Pos; similarly, the
intVer2Pos parameter must be greater than intVer1Pos. Both sets of
coordinates must fall within the panel's pixel dimensions. For details, see
QuickPanel pixel coordinate system.
Available colors for intColor are as follows:
0:
Black
1:
Blue
2:
Green
3:
Cyan
4:
Red
5:
Magenta
6:
Yellow
7:
White
Example
The following example draws a red rectangle from the top left corner to
coordinates (30,30).
DrawRect(0, 0, 30, 30, 4, 1)
Neg
Toggles (inverts) a bit of an integer variable. If the bit is 0, it is changed to 1;
if the bit is 1, it is changed to 0.
Supported by: QuickPanel targets
QPScript syntax
Neg(varVarName, intBitNumber)
See Also: Reset, Set
Parameters
varVarName: The name of the integer variable whose bit you want to invert.
intBitNumber: Integer constant or variable, indicating the specific bit to invert
This can be from 0 to one less than the number of bits in the
variable value.
Example
The following example toggles the fourth bit of the variable MyStatus.
Neg(MyStatus, 3)
Reset
Resets the value of a bit of an INT variable to 0.
Supported by: QuickPanel targets
QPScript syntax
Reset(varVarName, intBitNumber)
See Also: Neg, Set
Parameters
varVarName: The name of the INT variable whose bit you want to reset to 0.
Note: varVarName must indicate a variable with an INT data type.
Set
Sets the value of a bit of an INT variable to 1.
Supported by: QuickPanel targets
QPScript syntax
Set(varVarName, intBitNumber)
See Also: Neg, Reset
Parameters
varVarName: The name of the INT variable whose bit you want to set to 1.
Note: varVarName must indicate a variable with an INT data type.