Variables
Variables store the state of a Structured Text program. Each variable is declared with a name and a data type, and optionally an initial value and an address. Variables are declared in sections, which determine how the variable can be accessed.
Contents
Declaring Variables
A variable declaration has the form Name : Type;. Several variables of the same type can be declared together by separating their names with commas.
VAR
Running : BOOL;
Speed : LREAL := 250.0;
Left, Right : INT;
Station : Recipe := (Name := 'Pick', Speed := 50.0);
END_VARA variable without an initial value starts with the default value of its type.
Sections
| Section | Description |
|---|---|
VAR | Local variables. In programs and function blocks, they keep their values between scans. In functions, they are reinitialized with each function call. |
VAR_INPUT | Inputs, which are assigned by the caller. Inputs cannot be assigned inside the POU. |
VAR_OUTPUT | Outputs, which are assigned inside the POU and read by the caller. |
VAR_IN_OUT | Variables that are passed by reference. The POU reads and writes the caller’s variable directly. |
VAR_TEMP | Temporary variables, which are reinitialized with each execution of the POU. |
VAR_STAT | Static variables of a function block, which are shared by all of its instances. |
VAR_GLOBAL | Global variables, which can be accessed from any POU that declares them as external. |
VAR_EXTERNAL | Declares that a POU uses a global variable. |
VAR_CONFIG | Assigns addresses to variables of program instances in a configuration. |
Qualifiers
A qualifier is written after the section keyword, and applies to every variable in the section.
- CONSTANT: The variables cannot be modified. The initial value is required.
- RETAIN: The variables keep their values through a warm restart.
- NON_RETAIN: The variables are reinitialized by a warm restart.
VAR CONSTANT
MaxItems : INT := 10;
Pi : LREAL := 3.141592653589793;
END_VARResetting the simulation restarts the SoftPLC, which reinitializes every variable.
Addresses
An address assigns a variable to a location in the memory of the PLC, using the AT keyword. A variable with an address is called a located variable. The located variables of programs and function block instances can be imported as tags, which you can then bind to signals in the model through the IO Browser. This is how a Structured Text program reads its sensors and controls its actuators.
PROGRAM Main
VAR
PartPresent AT %IX0.0 : BOOL; // Input bit 0 of byte 0.
ResetButton AT %IX0.1 : BOOL; // Input bit 1 of byte 0.
Distance AT %ID4 : REAL; // Input double word at byte 4.
Conveyor AT %QX0.0 : BOOL; // Output bit 0 of byte 0.
Speed AT %QD4 : REAL; // Output double word at byte 4.
Count AT %MW0 : INT; // Memory word at byte 0.
END_VARFor example, the PartPresent tag (named Main.PartPresent) could be bound to the state of a sensor, and the Conveyor tag could be bound to the state of transport surface’s motor. When the program assigns Conveyor := PartPresent, the conveyor runs whenever the sensor detects a part.
An address is made up of a % sign, an area, an optional size and an offset:
| Area | Description |
|---|---|
I | Inputs, which the program reads. The values are written by the model. |
Q | Outputs, which the program writes. The values are read by the model. |
M | Memory, which both the program and the model can read and write. |
| Size | Description |
|---|---|
X | A single bit, for BOOL variables. The size can be omitted for bits, for example %I0.1. |
B | A byte (8 bits), for example BYTE, SINT or USINT variables. |
W | A word (16 bits), for example WORD, INT or UINT variables. |
D | A double word (32 bits), for example DWORD, DINT, UDINT or REAL variables. |
L | A long word (64 bits), for example LWORD, LINT or LREAL variables. |
The offset of a bit address is the byte followed by the bit, from 0 to 7. The offset of every other address is a byte offset. Each area is byte-addressed, so addresses can overlap. For example, %MW0 occupies bytes 0 and 1, so it overlaps %MB0, %MB1 and %MX0.0 to %MX1.7. Values are stored in little-endian byte order. Make sure that located variables do not overlap unintentionally, by leaving enough bytes between their offsets. Each area has a capacity of 64 KiB.
An address can also be hierarchical, with several numbers separated by dots, such as %IW1.2 for word 2 of module 1, or %ID2.5.7.1. Each number after the first is at most 99. A hierarchical address names a location by its path of numbers rather than by a byte offset, so each distinct path is given its own storage, at the top of its area. Different paths never overlap each other or the ordinary addresses, so %IW1.2 and %IW1.3 are separate words, and %IB1.2 is not the same byte as %IB102. The sizes of one path share its storage from the first byte, and a bit address names a bit of that byte, so %IB1.2 is the first byte of %IW1.2, and %IX1.2.3 is bit 3 of it. The same path used in several programs refers to the same storage. The storage of each path is assigned when the project is compiled, so adding or removing a hierarchical address can move the others. An ordinary address that falls within the storage of the hierarchical addresses is an error.
A partly specified address, %I*, %Q* or %M*, leaves the location open. The location is then assigned for each program instance with a VAR_CONFIG declaration in a configuration. This allows a program to be reused for several stations of a machine.
You do not need to assign addresses to communicate with the model. Global variables without an address also become memory tags. However, addresses make the inputs and outputs of a program explicit, and allow the program to be transferred to a physical PLC with the same IO mapping.
Partial Access
The bits, bytes and words of a bit string variable can be accessed directly. .%X accesses a bit, .%B a byte, .%W a word and .%D a double word, numbered from the least significant. The %X can be omitted for bits.
VAR
Status : WORD;
Ready : BOOL;
END_VAR
Ready := Status.%X3; // Bit 3.
Ready := Status.3; // Also bit 3.
Status.%B0 := 16#FF; // The lower byte.Global Variables
Global variables are declared in a VAR_GLOBAL section outside of any POU, and can be declared in any Structured Text file. A POU must declare each global variable that it uses in a VAR_EXTERNAL section, with the same name and type.
VAR_GLOBAL
LineSpeed : LREAL := 0.5;
EmergencyStop AT %IX10.0 : BOOL;
END_VAR
VAR_GLOBAL CONSTANT
StationCount : INT := 4;
END_VAR
PROGRAM Main
VAR_EXTERNAL
LineSpeed : LREAL;
EmergencyStop : BOOL;
END_VAR
VAR_EXTERNAL CONSTANT
StationCount : INT;
END_VAR
IF EmergencyStop THEN
LineSpeed := 0.0;
END_IF;
END_PROGRAMGlobal variables that are not constant can be imported as tags named GVL.<name>, such as GVL.LineSpeed. Global variables can also be declared inside a configuration, in which case they are named by the configuration, such as Cell.LineSpeed.
Edge Detection
A boolean input of a function block can be declared with R_EDGE or F_EDGE. The input is then only TRUE in the call where the value assigned to it changes from FALSE to TRUE (R_EDGE) or from TRUE to FALSE (F_EDGE). This is equivalent to using the R_TRIG and F_TRIG function blocks.
FUNCTION_BLOCK Station
VAR_INPUT
Start : BOOL R_EDGE;
END_VAR
VAR_OUTPUT
Count : INT;
END_VAR
IF Start THEN
Count := Count + 1; // Counts each press of the button, rather than each scan when the button is pressed down.
END_IF;
END_FUNCTION_BLOCK