To get started with an overview of V2 Adapter Scripts, visit this Knowledge Base Article.
Expressions are a type of value supported in several different parts of an IO adapter script. Expressions allow a value to be calculated as a kind of mathematical or logical formula using other identifiers. Identifiers are variables or data sources defined in the adapter script.
- Variables come from the variables section of an adapter script. A variable is a named list of operations applied to other variables or data sources. For more information, see: IO Configuration - Variables.
- Data sources are defined in other sections specific to a particular integration type. Some examples of data sources would be:
- Pins for Labjack U3/T4/T7 integrations
- Registers and coils for Modbus-TCP integrations
- Tags for Ethernet/IP and OPC-UA integrations
- Declared keys for MTConnect and MTConnect Adapter integrations
- Some expressions used within variable definitions also support the special “this” identifier. The this identifier represents the result of the previous step in the variable’s operation list.
Here are a few example expressions, with the variables or data sources highlighted:
- Convert a temperature from C to F
(temp-in * 9 / 5) + 32
- Check if either is running
spindle-1 or spindle-2
- Check if a pin is in a specific range
AIN0 > 1.2 and AIN0 < 3.4
- Check if an execution key is set to STOPPED
exec == ‘STOPPED’
- Cause a counter to reset after counting up to 10
this >= 10
Variables and data sources are treated the same way in an expression. When any variable or data source referenced within an expression updates, the entire expression is immediately re-computed. This will cause cascading updates to occur where the expression is used.
For example, if an expression is used in the middle of a variable definition, an expression update will cause the entire variable’s operation list to be re-executed with the last data it received. If this causes the variable’s value to change, then any other expression that references the variable will be re-computed as well. Avoid creating circular update loops via expressions.
Expressions can also appear in data item and condition definitions, which are responsible for outputting data to the MachineMetrics cloud. If an expression update changes the value of the data item or condition, the update will immediately be sent out.
Operators and Functions
Expressions support the following operators:
+, -, *, /, % (modulus), ^ (power)
and, or, not
&, |, ~, ^|, <<, >>, >>>
>, <, >=, <=, ==, !=
- Conditional expression:
(statement) ? (yes) : (no)
Expressions support grouping statements with parentheses. Parentheses can be nested arbitrarily deep and are useful for more complex logic or forcing order of operations.
(temp-in * 9 / 5) + 32
AIN0 > 1.2 and AIN0 < 3.4
hv-relay-on ? hv-analog : (lv-analog * 10)
Expressions support many standard arithmetic functions:
abs(x): Compute absolute value of a number
ceil(x): Round value up to next integer
floor(x): Round a value down to next integer
log(x, base): Calculate the logarithm of a value
round(x, n): Round to the nearest integer or n decimal places
sqrt(x): Calculate the square root of a value
Expressions support several statistical functions. These functions can consume an arbitrary number of arguments, including variables, data sources, or constants.
max(a, b, ...): Compute the maximum value of the passed values
mean(a, b, ...): Compute the mean value of the passed values
median(a, b, ...): Compute the median value of the passed values
min(a, b, ...): Compute the minimum value of the passed values
mode(a, b, ...): Compute the mode of the passed values
prod(a, b, ...): Compute the product of the passed values
std(a, b, ...): Compute the standard deviation of the passed values
sum(a, b, ...): Compute the sum of the passed values
variance(a, b, ...): Compute the variance of the passed values
max(spindle-1, spindle-2, spindle-3) > 6000
Expressions support some functions intended to work with string values.
startsWith(x, term): Checks if the string x starts with the passed term. Matches are case-sensitive.
endsWith(x, term): Checks if the string x ends with the passed term. Matches are case-sensitive.
contains(x, term): Checks if the string x contains the passed term anywhere. Matches are case-sensitive.
Expressions support several trigonometry functions.
acos(x): Calculate the inverse cosine of a value
acot(x): Calculate the inverse cotangent of a value
acsc(x): Calculate the inverse cosecant of a value
asec(x): Calculate the inverse secant of a value
asin(x): Calculate the inverse sine of a value
atan(x): Calculate the inverse tangent of a value
cos(x): Calculate the cosine of a value
cot(x): Calculate the cotangent of a value
csc(x): Calculate the cosecant of a value
sec(x): Calculate the secant of a value
sin(x): Calculate the sine of a value
tan(x): Calculate the tangent of a value
These miscellaneous functions support other useful functionality in expressions.
flag(x, index): Read one bit as true or false from a bit flag value. The index is 0-based.
- # The following lines are equivalent
(register70 & (1 << index)) != 0
Reach out to Support@machinemetrics.com.
Please sign in to leave a comment.