Standard Functions
The SoftPLC provides all of the standard functions defined by IEC 61131-3. Standard functions are always available, and do not need to be declared. Many standard functions are generic, accepting any type in a group of types such as ANY_NUM. The data types page describes each generic type.
Functions marked as extensible accept any number of inputs. For example, MAX(A, B, C) returns the largest of three values. When the inputs are named, they are numbered from IN1 up to IN64, except for MUX, which numbers its inputs from IN0 up to IN63.
Contents
- Conversion
- Numeric
- Arithmetic
- Bit Shift
- Bitwise
- Selection
- Comparison
- Strings
- Durations, Dates and Times
- Arrays
- Byte Order
- Other
Conversion
Conversion functions convert a value from one type to another. There are two forms. <source>_TO_<target> converts a value of a specific type, for example INT_TO_REAL or LREAL_TO_DINT. Conversely, TO_<target> converts a value of any elementary type, for example TO_REAL or TO_STRING.
Speed := INT_TO_LREAL(RawSpeed) * 0.01;
Count := LREAL_TO_DINT(Distance / Pitch);
Text := CONCAT('Count: ', TO_STRING(Count));
Value := STRING_TO_REAL('2.5');- Conversions from real numbers to integers round to the nearest integer, and halfway values round to the nearest even integer.
- Conversions to a smaller integer type keep the lowest bits of the value.
- A real number that is too large for
LINT,ULINTorLTIME, such as the result of an overflow, cannot be converted to these types. The conversion is a runtime error and returns 0. - Conversions between
TIMEand numbers use milliseconds, soTIME_TO_DINT(T#1.5s)is 1500 andDINT_TO_TIME(250)isT#250ms. - Conversions from
BOOLgive 1 forTRUEand 0 forFALSE. - Conversions to and from strings use the same text as literals, for example
'12.5'or'TRUE'. A date and time holds whole seconds, soSTRING_TO_DTdrops any fraction of a second. DT_TO_DATEandDT_TO_TODextract the date and the time of day from a date and time.
| Function | Inputs | Returns | Description |
|---|---|---|---|
TRUNC | IN : ANY_REAL | DINT | Rounds towards zero, so TRUNC(-2.7) is -2. |
TRUNC_<integer> | IN : ANY_REAL | The integer type | Rounds towards zero, for example TRUNC_INT. |
REAL_TRUNC_<integer> | IN : REAL | The integer type | Rounds a REAL towards zero, for example REAL_TRUNC_DINT. |
LREAL_TRUNC_<integer> | IN : LREAL | The integer type | Rounds an LREAL towards zero, for example LREAL_TRUNC_DINT. |
<integer>_TO_BCD | IN : ANY_INT | ANY_BIT | Encodes an integer as binary-coded decimal, so INT_TO_BCD(42) is 16#42. |
BCD_TO_<integer> | IN : ANY_BIT | The integer type | Decodes a binary-coded decimal value, for example BCD_TO_INT. |
TO_BCD_<bits> | IN : ANY_INT | The bit string type | Encodes an integer as binary-coded decimal, for example TO_BCD_WORD. |
Binary-coded decimal holds one decimal digit in each group of 4 bits, so a WORD holds up to 4 digits. Encoding a negative value, or a value with more digits than the result can hold, is a runtime error, and so is decoding a value with a group of 4 bits above 9, such as 16#1A. The function then returns 0. Use IS_VALID_BCD to check a value before decoding it.
Numeric
| Function | Inputs | Returns | Description |
|---|---|---|---|
ABS | IN : ANY_NUM | ANY_NUM | The absolute value. |
SQRT | IN : ANY_REAL | ANY_REAL | The square root. The input must not be negative. |
LN | IN : ANY_REAL | ANY_REAL | The natural logarithm. The input must be positive. |
LOG | IN : ANY_REAL | ANY_REAL | The base 10 logarithm. The input must be positive. |
EXP | IN : ANY_REAL | ANY_REAL | e raised to the power of the input. |
EXPT | IN1 : ANY_REAL, IN2 : ANY_NUM | ANY_REAL | IN1 raised to the power of IN2, equivalent to the ** operator. |
SIN | IN : ANY_REAL | ANY_REAL | The sine of an angle in radians. |
COS | IN : ANY_REAL | ANY_REAL | The cosine of an angle in radians. |
TAN | IN : ANY_REAL | ANY_REAL | The tangent of an angle in radians. |
ASIN | IN : ANY_REAL | ANY_REAL | The inverse sine in radians, for an input from -1 to 1. |
ACOS | IN : ANY_REAL | ANY_REAL | The inverse cosine in radians, for an input from -1 to 1. |
ATAN | IN : ANY_REAL | ANY_REAL | The inverse tangent in radians. |
ATAN2 | Y : ANY_REAL, X : ANY_REAL | ANY_REAL | The angle in radians of the point (X, Y), from -π to π. |
Arithmetic
Each arithmetic function is equivalent to an operator. A duration can be added to or subtracted from another duration, or multiplied and divided by a number.
| Function | Inputs | Returns | Description |
|---|---|---|---|
ADD | Extensible, ANY_MAGNITUDE | ANY_MAGNITUDE | The sum of the inputs, equivalent to +. |
SUB | IN1 : ANY_MAGNITUDE, IN2 : ANY_MAGNITUDE | ANY_MAGNITUDE | IN1 minus IN2, equivalent to -. |
MUL | Extensible, ANY_MAGNITUDE | ANY_MAGNITUDE | The product of the inputs, equivalent to *. |
DIV | IN1 : ANY_MAGNITUDE, IN2 : ANY_MAGNITUDE | ANY_MAGNITUDE | IN1 divided by IN2, equivalent to /. Integer division truncates towards zero. |
MOD | IN1 : ANY_INT, IN2 : ANY_INT | ANY_INT | The remainder of IN1 divided by IN2, equivalent to MOD. The result has the sign of IN1. |
MOVE | IN : ANY | ANY | Returns the input unchanged, equivalent to :=. |
Bit Shift
| Function | Inputs | Returns | Description |
|---|---|---|---|
SHL | IN : ANY_BIT, N : ANY_INT | ANY_BIT | Shifts IN left by N bits, filling with zeros. |
SHR | IN : ANY_BIT, N : ANY_INT | ANY_BIT | Shifts IN right by N bits, filling with zeros. |
ROL | IN : ANY_BIT, N : ANY_INT | ANY_BIT | Rotates IN left by N bits. |
ROR | IN : ANY_BIT, N : ANY_INT | ANY_BIT | Rotates IN right by N bits. |
Value := SHL(BYTE#2#0000_0011, 2); // 2#0000_1100
Value := SHR(BYTE#2#1111_0000, 4); // 2#0000_1111
Value := ROL(BYTE#2#1000_0001, 1); // 2#0000_0011, the highest bit moves to the lowest.
Value := ROR(BYTE#2#0000_0001, 1); // 2#1000_0000, the lowest bit moves to the highest.Bitwise
The bitwise functions operate on BOOL values, and on each bit of the bit string types. Both operands must have the same type.
| Function | Inputs | Returns | Description |
|---|---|---|---|
AND | Extensible, ANY_BIT | ANY_BIT | Bitwise AND, equivalent to AND or &. |
OR | Extensible, ANY_BIT | ANY_BIT | Bitwise OR, equivalent to OR. |
XOR | Extensible, ANY_BIT | ANY_BIT | Bitwise exclusive OR, equivalent to XOR. |
NOT | IN : ANY_BIT | ANY_BIT | Bitwise complement, equivalent to NOT. |
Low := Status AND BYTE#2#0000_1111; // Keeps the lowest 4 bits of Status.
Flags := Flags OR BYTE#2#0000_0100; // Sets the third bit.
Flags := Flags AND NOT BYTE#2#0000_0001; // Clears the first bit.
Flags := Flags XOR BYTE#2#1000_0000; // Toggles the eighth bit.
AllReady := AND(Ready1, Ready2, Ready3); // TRUE when all three are TRUE.Selection
| Function | Inputs | Returns | Description |
|---|---|---|---|
SEL | G : BOOL, IN0 : ANY, IN1 : ANY | ANY | Returns IN0 when G is FALSE, and IN1 when G is TRUE. |
MAX | Extensible, ANY_ELEMENTARY | ANY_ELEMENTARY | The largest input. |
MIN | Extensible, ANY_ELEMENTARY | ANY_ELEMENTARY | The smallest input. |
LIMIT | MN, IN, MX | ANY_ELEMENTARY | Limits IN to the range from MN to MX. |
MUX | K : ANY_INT, extensible IN0, IN1, … | ANY_ELEMENTARY | Returns the input selected by K, counting from IN0. |
Speed := SEL(Fast, 0.25, 1.0); // 1.0 when Fast is TRUE.
Target := LIMIT(0.0, Request, 500.0); // Request, limited to 0 to 500.
Speed := MUX(Mode, 0.0, 0.5, 1.0); // 0.0 when Mode is 0.Comparison
Each comparison function is equivalent to an operator. The extensible comparisons are TRUE when the condition holds between each pair of consecutive inputs. For example, LT(A, B, C) is TRUE when A < B and B < C.
| Function | Inputs | Returns | Description |
|---|---|---|---|
GT | Extensible, ANY_ELEMENTARY | BOOL | Greater than, equivalent to >. |
GE | Extensible, ANY_ELEMENTARY | BOOL | Greater than or equal to, equivalent to >=. |
EQ | Extensible, ANY_ELEMENTARY | BOOL | Equal to, equivalent to =. |
LE | Extensible, ANY_ELEMENTARY | BOOL | Less than or equal to, equivalent to <=. |
LT | Extensible, ANY_ELEMENTARY | BOOL | Less than, equivalent to <. |
NE | IN1, IN2 | BOOL | Not equal to, equivalent to <>. |
Strings
Several string functions take or return a position, which is the place of a character in the string. Positions are counted from 1, so the first character is at position 1, not 0. For example, in 'Hello World', the H is at position 1 and the W is at position 7, so FIND('Hello World', 'World') returns 7.
| Function | Inputs | Returns | Description |
|---|---|---|---|
LEN | IN : ANY_STRING | INT | The number of characters. |
LEN_MAX | IN : ANY_STRING | INT | The maximum length of a string variable. |
LEN_CODE_UNIT | IN : ANY_STRING | INT | The number of code units. |
LEFT | IN : ANY_STRING, L : ANY_INT | ANY_STRING | The first L characters. |
RIGHT | IN : ANY_STRING, L : ANY_INT | ANY_STRING | The last L characters. |
MID | IN : ANY_STRING, L : ANY_INT, P : ANY_INT | ANY_STRING | L characters, starting at position P. |
CONCAT | Extensible, ANY_STRING | ANY_STRING | Joins the strings. |
INSERT | IN1 : ANY_STRING, IN2 : ANY_STRING, P : ANY_INT | ANY_STRING | Inserts IN2 into IN1 after position P. |
DELETE | IN : ANY_STRING, L : ANY_INT, P : ANY_INT | ANY_STRING | Removes L characters, starting at position P. |
REPLACE | IN1 : ANY_STRING, IN2 : ANY_STRING, L : ANY_INT, P : ANY_INT | ANY_STRING | Replaces L characters of IN1, starting at position P, with IN2. |
FIND | IN1 : ANY_STRING, IN2 : ANY_STRING | INT | The position of the first occurrence of IN2 in IN1, or 0 if it does not occur. |
Name := 'Hello World';
Length := LEN(Name); // 11
Text := LEFT(Name, 5); // 'Hello'
Text := MID(Name, 5, 7); // 'World'
Position := FIND(Name, 'World'); // 7
Text := INSERT(Name, '!', 5); // 'Hello! World'
Text := REPLACE(Name, 'There', 5, 7); // 'Hello There'The following functions convert between strings and arrays of bytes, which is useful for building or parsing messages:
| Function | Description |
|---|---|
STRING_TO_ARRAY_OF_BYTE | Copies the bytes of the STRING IN into the array OUT. The outputs BYTES_OUT and CHARS_OUT give the number of bytes and characters written. |
WSTRING_TO_ARRAY_OF_BYTE | Copies the bytes of the WSTRING IN into the array OUT. The outputs BYTES_OUT and CHARS_OUT give the number of bytes and characters written. |
USTRING_TO_ARRAY_OF_BYTE | Copies the bytes of the USTRING IN into the array OUT. The outputs BYTES_OUT and CHARS_OUT give the number of bytes and characters written. |
ARRAY_OF_BYTE_TO_STRING | Decodes BYTES_IN bytes of the array IN into the STRING OUT. The output VALID is TRUE when the bytes were valid. |
ARRAY_OF_BYTE_TO_WSTRING | Decodes BYTES_IN bytes of the array IN into the WSTRING OUT. The output VALID is TRUE when the bytes were valid. |
ARRAY_OF_BYTE_TO_USTRING | Decodes BYTES_IN bytes of the array IN into the USTRING OUT. The output VALID is TRUE when the bytes were valid. |
Durations, Dates and Times
The arithmetic operators can be used with durations, dates and times. The following functions are the typed equivalents, which make the types of the operands explicit.
| Function | Inputs | Returns | Description |
|---|---|---|---|
ADD_TIME | TIME, TIME | TIME | Adds two durations. |
ADD_TOD_TIME | TOD, TIME | TOD | Adds a duration to a time of day. |
ADD_DT_TIME | DT, TIME | DT | Adds a duration to a date and time. |
SUB_TIME | TIME, TIME | TIME | Subtracts two durations. |
SUB_DATE_DATE | DATE, DATE | TIME | The duration between two dates. |
SUB_TOD_TIME | TOD, TIME | TOD | Subtracts a duration from a time of day. |
SUB_TOD_TOD | TOD, TOD | TIME | The duration between two times of day. |
SUB_DT_TIME | DT, TIME | DT | Subtracts a duration from a date and time. |
SUB_DT_DT | DT, DT | TIME | The duration between two dates and times. |
MUL_TIME | TIME, ANY_NUM | TIME | Multiplies a duration by a number. |
DIV_TIME | TIME, ANY_NUM | TIME | Divides a duration by a number. |
DIV_TIME_TIME | TIME, TIME | LINT | Divides a duration by a duration, truncating the result. |
DIV_TIME_LREAL | TIME, TIME | LREAL | Divides a duration by a duration. |
MOD_TIME | TIME, TIME | TIME | The remainder of dividing two durations. |
CONCAT_DATE_TOD | DATE, TOD | DT | Combines a date and a time of day. |
CONCAT_DATE | YEAR, MONTH, DAY | DATE | Creates a date from its fields. |
CONCAT_TOD | HOUR, MINUTE, SECOND, MILLISECOND | TOD | Creates a time of day from its fields. |
CONCAT_DT | YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MILLISECOND | DT | Creates a date and time from its fields. |
SPLIT_DATE | IN : DATE, YEAR, MONTH, DAY | Writes the fields of a date to the variables passed after IN. | |
SPLIT_TOD | IN : TOD, HOUR, MINUTE, SECOND, MILLISECOND | Writes the fields of a time of day to the variables passed after IN. | |
SPLIT_DT | IN : DT, YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MILLISECOND | Writes the fields of a date and time to the variables passed after IN. | |
DAY_OF_WEEK | IN : DATE | USINT | The day of the week, from 0 for Sunday to 6 for Saturday. |
The long types have equivalent functions, which replace TIME with LTIME, DATE with LDATE, TOD with LTOD and DT with LDT. The functions SPLIT_LTOD and SPLIT_LDT also write the microsecond and nanosecond fields.
SPLIT_TOD(TOD#12:34:56.789, Hour, Minute, Second, Millisecond);
Shift := SUB_TOD_TOD(TOD#14:00:00, TOD#06:00:00); // T#8hArrays
| Function | Inputs | Returns | Description |
|---|---|---|---|
LOWER_BOUND | ARR : ANY, DIM : ANY_INT | INT | The lower bound of dimension DIM of an array, counting from 1. |
UPPER_BOUND | ARR : ANY, DIM : ANY_INT | INT | The upper bound of dimension DIM of an array, counting from 1. |
VAR
Speeds : ARRAY[0..9] OF REAL;
I : INT;
END_VAR
// Sets every element to 0.0, from the lower bound (0) to the upper bound (9).
FOR I := LOWER_BOUND(Speeds, 1) TO UPPER_BOUND(Speeds, 1) DO
Speeds[I] := 0.0;
END_FOR;These functions are typically used with variable-length arrays (ARRAY[*]), as shown on the data types page.
Byte Order
The SoftPLC stores values in little-endian byte order. These functions convert values for devices or protocols that use a particular byte order. They accept elementary values, structures and arrays.
| Function | Description |
|---|---|
TO_BIG_ENDIAN | Converts a value to big-endian byte order. |
FROM_BIG_ENDIAN | Converts a value from big-endian byte order. |
TO_LITTLE_ENDIAN | Converts a value to little-endian byte order. |
FROM_LITTLE_ENDIAN | Converts a value from little-endian byte order. |
Other
| Function | Inputs | Returns | Description |
|---|---|---|---|
WRITE | IN : ANY | Writes a value to the console. Structures, arrays, enumerations and function block instances are written in the syntax of an initializer, such as (X := 1, Y := 2). | |
ASSERT | IN : BOOL | Reports a runtime error and stops the simulation when IN is FALSE. | |
SIZEOF | IN : ANY | DINT | The size of a variable in bytes. |
IS_VALID | IN : ANY_REAL | BOOL | TRUE when the value is a finite number. |
IS_VALID_BCD | IN : ANY_BIT | BOOL | TRUE when every group of 4 bits is a valid decimal digit. |
WRITE(CONCAT('Pallet complete: ', TO_STRING(PalletCount)));
ASSERT(Speed <= MaxSpeed);