Data Types
Every variable in Structured Text has a data type, which determines the values it can hold and the operations that can be performed on it. The SoftPLC supports all of the elementary data types defined by IEC 61131-3, along with user-defined (derived) types.
Contents
- Boolean and Bit Strings
- Integers
- Real Numbers
- Durations, Dates and Times
- Characters and Strings
- Generic Types
- Implicit Conversions
- Derived Types
- Enumerations
- Subranges
- Structures
- Arrays
- References
Boolean and Bit Strings
| Type | Size | Description |
|---|---|---|
BOOL | 1 bit | A boolean value, TRUE or FALSE. The default value is FALSE. |
BYTE | 8 bits | A bit string of 8 bits. |
WORD | 16 bits | A bit string of 16 bits. |
DWORD | 32 bits | A bit string of 32 bits. |
LWORD | 64 bits | A bit string of 64 bits. |
Bit strings are used for bitwise operations and for packing several boolean values into one variable. Individual bits can be accessed using partial access, for example Status.%X3 or Status.3.
Integers
| Type | Size | Range |
|---|---|---|
SINT | 8 bits | -128 to 127 |
INT | 16 bits | -32,768 to 32,767 |
DINT | 32 bits | -2,147,483,648 to 2,147,483,647 |
LINT | 64 bits | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
USINT | 8 bits | 0 to 255 |
UINT | 16 bits | 0 to 65,535 |
UDINT | 32 bits | 0 to 4,294,967,295 |
ULINT | 64 bits | 0 to 18,446,744,073,709,551,615 |
The default value of an integer is 0. An arithmetic result that is outside of the range of its type is a runtime error.
Real Numbers
| Type | Size | Description |
|---|---|---|
REAL | 32 bits | A single precision IEEE 754 floating-point number, with about 7 significant digits. |
LREAL | 64 bits | A double precision IEEE 754 floating-point number, with about 16 significant digits. |
The default value of a real number is 0.0. Prefer LREAL for positions and other values that require high precision. The motion control function blocks use LREAL for all quantities.
Durations, Dates and Times
| Type | Short Form | Description |
|---|---|---|
TIME | A duration with millisecond resolution, for example T#1s500ms. | |
LTIME | A duration with nanosecond resolution, for example LT#250us. | |
DATE | A calendar date, for example D#2024-01-31. | |
TIME_OF_DAY | TOD | A time of day, for example TOD#12:30:00. |
DATE_AND_TIME | DT | A date and time of day, for example DT#2024-01-31-12:30:00. |
LDATE | A calendar date with nanosecond resolution. | |
LTIME_OF_DAY | LTOD | A time of day with nanosecond resolution. |
LDATE_AND_TIME | LDT | A date and time of day with nanosecond resolution. |
Durations are typically used with the timers. Durations can be added, subtracted, multiplied and divided by numbers, for example T#1s * 2 is T#2s. Time in the SoftPLC is simulation time, which advances by the timestep with each simulation step.
Characters and Strings
| Type | Description |
|---|---|
STRING | A string of single-byte characters, for example 'text'. |
WSTRING | A string of double-byte characters, for example "text". |
USTRING | A string of Unicode characters, for example U#'text'. |
CHAR | A single-byte character. |
WCHAR | A double-byte character. |
UCHAR | A Unicode character. |
A string variable has a maximum length, which is 80 characters by default. You can specify the maximum length in square brackets. A string that is longer than the maximum length is truncated when it is assigned.
VAR
Name : STRING; // Up to 80 characters.
Code : STRING[8] := 'A1'; // Up to 8 characters.
Label : WSTRING[20];
END_VARA single character of a string is accessed with its position in square brackets. Positions are counted from 1, so the first character is at position 1. The character has the CHAR type for a STRING, and the WCHAR type for a WSTRING. A character can also be assigned to a string variable.
VAR
Code : STRING[8] := 'A1';
First : CHAR;
END_VAR
First := Code[1]; // 'A'
Code[2] := '7'; // Code is now 'A7'.A position outside the current length of the string is a runtime error, even when it is within the maximum length.
The string functions can be used to join, search and modify strings.
Generic Types
Generic types describe groups of types. They are used in the documentation of the standard functions, which accept any type in the group. You cannot declare variables with a generic type.
| Type | Includes |
|---|---|
ANY | Every type. |
ANY_ELEMENTARY | Every elementary type. |
ANY_MAGNITUDE | The numeric types and the durations. |
ANY_NUM | The integer and real types. |
ANY_REAL | REAL and LREAL. |
ANY_INT | The signed and unsigned integer types. |
ANY_BIT | BOOL, BYTE, WORD, DWORD and LWORD. |
ANY_STRING | STRING, WSTRING and USTRING. |
Implicit Conversions
A value is converted implicitly when no information can be lost. For example, an INT can be assigned to a DINT, an LINT or an LREAL, and a REAL can be assigned to an LREAL. Any conversion that could lose information must be explicit, using one of the conversion functions:
VAR
Count : INT := 300;
Total : DINT;
Ratio : LREAL;
Small : SINT;
END_VAR
Total := Count; // Implicit, since every INT is a valid DINT.
Ratio := Count; // Implicit, since every INT is a valid LREAL.
Small := INT_TO_SINT(Count); // Explicit. Keeps the lowest 8 bits, giving 44.
Count := LREAL_TO_INT(2.5); // Explicit. Rounds to the nearest integer, giving 2.Conversions to a smaller integer type keep the lowest bits of the value. Conversions from real numbers to integers round to the nearest integer, and halfway values round to the nearest even integer. Use TRUNC to round towards zero instead.
Derived Types
Derived types are declared between TYPE and END_TYPE. They can be declared in any Structured Text file and used throughout the project. The simplest derived type gives a new name to an existing type, optionally with a different default value:
TYPE
Millimeters : LREAL := 0.0;
Percentage : REAL := 100.0;
END_TYPEEnumerations
An enumeration is a type with a fixed set of named values. The default value is the first value, unless another default is specified.
TYPE
Color : (Red, Green, Blue); // Defaults to Red
Mode : (Off, Manual, Automatic) := Manual; // Defaults to Manual
END_TYPEThe values can be written on their own or qualified with the type name. A qualified value is required when two enumerations share the same value name.
VAR
Lamp : Color := Color#Green;
END_VAR
IF Lamp = Red THEN
Alarm := TRUE;
END_IF;A typed enumeration specifies an integer type and the value of each name. A typed enumeration can be assigned to a variable of its integer type.
TYPE
Command : INT (Stop := 0, Forward := 1, Reverse := 2);
END_TYPEEnumerations are particularly useful for the steps of a state machine in a CASE statement.
Subranges
A subrange restricts an integer type to a range of values. Assigning a value outside of the range is a runtime error.
TYPE
Percentage : INT (0..100);
END_TYPEStructures
A structure groups related variables, called fields, into a single type. Fields can have default values.
TYPE
Point : STRUCT
X : LREAL;
Y : LREAL;
END_STRUCT;
Recipe : STRUCT
Name : STRING[20];
Target : Point;
Speed : LREAL := 100.0;
Enabled : BOOL := TRUE;
END_STRUCT;
END_TYPEThe fields are accessed with the . operator. A structure can be initialized by assigning values to some or all of its fields. A whole structure can be assigned to another structure of the same type.
VAR
Home : Point := (X := 0.0, Y := 250.0);
Current : Recipe;
END_VAR
Current.Target := Home;
Current.Target.X := 50.0;Arrays
An array holds a fixed number of elements of the same type. The bounds of each dimension are specified in square brackets, and can start at any integer. Arrays can be declared directly in a variable declaration or as a derived type.
TYPE
Positions : ARRAY[1..10] OF LREAL;
END_TYPE
VAR
Speeds : ARRAY[1..3] OF REAL := [1.0, 2.5, 4.0];
Grid : ARRAY[0..2, 0..3] OF INT := [12(0)]; // A 3x4 2D array, with each of the 12 elements initialized to 0.
Lanes : ARRAY[1..4] OF Point;
Delays : ARRAY[1..8] OF TON; // An array of TON function block instances.
END_VAR
Speeds[2] := 3.0;
Grid[1, 2] := 5;
Lanes[I].X := 10.0;
Delays[I](IN := Sensors[I], PT := T#1s);In an array initializer, n(value) repeats a value n times. An index that is outside of the bounds of the array is a runtime error.
The elements of an array can themselves be arrays. Each level is indexed with its own pair of brackets.
VAR
Matrix : ARRAY[1..3] OF ARRAY[1..4] OF INT;
END_VAR
Matrix[2][3] := 9; // Row 2, column 3.Functions and function blocks can accept arrays of any length through a VAR_IN_OUT declared with ARRAY[*]. The LOWER_BOUND and UPPER_BOUND functions return the bounds of each dimension.
FUNCTION Sum : LREAL
VAR_IN_OUT
Values : ARRAY[*] OF LREAL;
END_VAR
VAR
I : DINT;
END_VAR
Sum := 0.0;
FOR I := LOWER_BOUND(Values, 1) TO UPPER_BOUND(Values, 1) DO
Sum := Sum + Values[I];
END_FOR;
END_FUNCTIONReferences
A reference holds the location of another variable. References are declared with REF_TO, created using the REF operator, and dereferenced with the ^ operator. A reference that does not refer to a variable has the value NULL, which is also the default value.
VAR
Count : INT;
Target : REF_TO INT;
Timer : TON;
TimerRef : REF_TO TON;
END_VAR
Target := REF(Count); // Target points to Count.
Target^ := Target^ + 1; // Increments Count.
TimerRef := REF(Timer);
TimerRef^(IN := TRUE, PT := T#1s);
IF Target <> NULL THEN
Target^ := 0;
END_IF;References allow a function block to operate on variables that it does not own, or to select between several variables at runtime. Dereferencing a NULL reference is a runtime error. The compiler reports an error when a reference could outlive the variable it refers to, such as a reference to a VAR_TEMP variable that is stored in a VAR variable.
References in Function Blocks
References are typically used to connect function block instances, so that one instance can read or control another. A common example is dieback (zero-pressure accumulation), where a line is made of several short conveyors, each with its own motor and a photo eye at its end. A conveyor holding a part only runs when the next conveyor can take the part, so when the end of the line is blocked, the parts stop one conveyor at a time without pushing against each other. Each conveyor therefore needs to know the state of the conveyor it feeds, which it accesses through a reference:
FUNCTION_BLOCK Conveyor
VAR_INPUT
PartPresent : BOOL; // The photo eye at the end of this conveyor.
Next : REF_TO Conveyor; // The conveyor that this one feeds, or NULL for the last conveyor.
Accept : BOOL; // Only used by the last conveyor: TRUE when the next machine accepts a part.
END_VAR
VAR_OUTPUT
Motor : BOOL;
END_VAR
IF NOT PartPresent THEN
Motor := TRUE; // Runs to receive a part.
ELSIF Next = NULL THEN
Motor := Accept; // If the last conveyor then wait for the next machine.
ELSE
Motor := NOT Next^.PartPresent OR Next^.Motor; // Runs if the next conveyor can take the part.
END_IF;
END_FUNCTION_BLOCK
PROGRAM Main
VAR
Sensor1 AT %IX0.0 : BOOL;
Sensor2 AT %IX0.1 : BOOL;
Sensor3 AT %IX0.2 : BOOL;
Accept AT %IX0.3 : BOOL; // The next machine accepts a part.
Motor1 AT %QX0.0 : BOOL;
Motor2 AT %QX0.1 : BOOL;
Motor3 AT %QX0.2 : BOOL;
Conveyor1 : Conveyor;
Conveyor2 : Conveyor;
Conveyor3 : Conveyor;
END_VAR
// The last conveyor is called first, so each conveyor sees the current state of the one it feeds.
Conveyor3(PartPresent := Sensor3, Accept := Accept);
Conveyor2(PartPresent := Sensor2, Next := REF(Conveyor3));
Conveyor1(PartPresent := Sensor1, Next := REF(Conveyor2));
Motor1 := Conveyor1.Motor;
Motor2 := Conveyor2.Motor;
Motor3 := Conveyor3.Motor;
END_PROGRAMWhen the next machine stops accepting parts and every conveyor holds a part, all three motors stop. An empty conveyor keeps running until a part reaches its photo eye. When the next machine accepts parts again, the last conveyor starts, and each conveyor before it follows.
The same Conveyor function block works for a line of any length, since each conveyor only refers to the one it feeds. Since a reference can be NULL, a function block should check it before dereferencing it, as the last conveyor does.
Assignment Attempts
A reference to a function block can also refer to an instance of any function block that extends it. For example, a REF_TO Machine can refer to a Conveyor if Conveyor extends Machine. Going the other way, from a REF_TO Machine to a REF_TO Conveyor, is only valid if the instance really is a Conveyor, which can only be known at runtime. A plain assignment is therefore a compile error.
The assignment attempt operator ?= performs this check at runtime. It assigns the reference if the instance has the required type, and assigns NULL otherwise, so the result must be checked before it is used.
FUNCTION_BLOCK Machine
VAR_OUTPUT
Running : BOOL;
END_VAR
END_FUNCTION_BLOCK
FUNCTION_BLOCK Conveyor EXTENDS Machine
VAR_OUTPUT
Speed : LREAL;
END_VAR
END_FUNCTION_BLOCK
FUNCTION_BLOCK Press EXTENDS Machine
END_FUNCTION_BLOCK
PROGRAM Main
VAR
Infeed : Conveyor;
Stamp : Press;
Current : REF_TO Machine;
AsConveyor : REF_TO Conveyor;
Speed : LREAL;
END_VAR
Current := REF(Infeed); // Valid, since every Conveyor is a Machine.
AsConveyor ?= Current; // Infeed is a Conveyor, so AsConveyor refers to Infeed.
IF AsConveyor <> NULL THEN
Speed := AsConveyor^.Speed;
END_IF;
Current := REF(Stamp);
AsConveyor ?= Current; // Stamp is not a Conveyor, so AsConveyor is NULL.
END_PROGRAMAssignment attempts are also used with interfaces, to check whether an instance implements a more specific interface.