Variables & Data Types
Variables are the foundation of any Structured Text program. Understanding how to declare, initialize, and use different data types is essential for effective ST programming.
๐ Enhance Your Development Experience
Get syntax highlighting, IntelliSense, and debugging support for Structured Text:
Install VS Code Extension โVariable Declaration
Variables must be declared before use within VAR blocks. The basic syntax is:
Variable Declaration Syntax
VAR
VariableName : DataType;
AnotherVariable : DataType := InitialValue;
END_VARBasic Data Types
Boolean Types
VAR
IsRunning : BOOL := FALSE;
StartButton : BOOL;
SafetyOK : BOOL := TRUE;
END_VARValues: TRUE, FALSE
Size: 1 bit
Use: Digital signals, flags, conditions
Integer Types
VAR
Counter : INT := 0;
LargeNumber : DINT := 100000;
SmallValue : SINT := -50;
PositiveOnly : UINT := 65000;
END_VARSINT: -128 to 127 (8-bit)
INT: -32,768 to 32,767 (16-bit)
DINT: -2ยณยน to 2ยณยน-1 (32-bit)
UINT: 0 to 65,535 (16-bit unsigned)
Real (Floating Point) Types
VAR
Temperature : REAL := 25.5;
PreciseValue : LREAL := 3.14159265359;
Percentage : REAL := 75.0;
END_VARREAL: 32-bit floating point
LREAL: 64-bit floating point
Range: ยฑ1.175e-38 to ยฑ3.402e+38 (REAL)
String Types
VAR
Message : STRING := 'Hello World';
PartNumber : STRING(20) := 'ABC-123';
ErrorText : STRING(100);
END_VARSTRING: Variable-length text
STRING(n): Fixed maximum length
Default length: Usually 80 characters
Time Data Types
Time Data Types
VAR
DelayTime : TIME := T#5s; // 5 seconds
CycleTime : TIME := T#100ms; // 100 milliseconds
LongDelay : TIME := T#2h30m15s; // 2 hours, 30 minutes, 15 seconds
CurrentTime : DATE_AND_TIME;
StartDate : DATE := D#2024-01-15;
StartTime : TIME_OF_DAY := TOD#08:30:00;
END_VARTime Format Examples
T#5s or TIME#5s - 5 secondsT#1m30s - 1 minute 30 secondsT#2h15m - 2 hours 15 minutesT#100ms - 100 millisecondsD#2024-12-25 - December 25, 2024TOD#14:30:45 - 2:30:45 PMVariable Initialization
โ Good Practices
VAR
// Initialize critical values
SafetyEnabled : BOOL := TRUE;
MaxTemp : REAL := 100.0;
// Clear initial states
AlarmActive : BOOL := FALSE;
ErrorCount : INT := 0;
END_VARโ Potential Issues
VAR
// Uninitialized variables may have
// unpredictable values
Temperature : REAL;
Counter : INT;
Status : BOOL;
Speed : REAL;
END_VARArrays
Arrays allow you to store multiple values of the same type:
Array Declaration and Usage
VAR
// One-dimensional arrays
Temperatures : ARRAY[1..10] OF REAL;
InputStates : ARRAY[0..15] OF BOOL;
Messages : ARRAY[1..5] OF STRING(50);
// Two-dimensional arrays
Matrix : ARRAY[1..3, 1..3] OF INT;
// Array with initialization
DefaultValues : ARRAY[1..3] OF INT := [10, 20, 30];
END_VAR
// Using arrays
Temperatures[1] := 25.5;
InputStates[0] := TRUE;
Matrix[2,3] := 42;Structures (User-Defined Types)
Structures allow you to group related data together:
Structure Definition and Usage
// Type definition (usually in a separate section)
TYPE
MotorData : STRUCT
Speed : REAL;
Current : REAL;
Temperature : REAL;
IsRunning : BOOL;
FaultCode : INT;
END_STRUCT;
END_TYPE
// Variable declaration using the structure
VAR
Motor1 : MotorData;
Motor2 : MotorData;
END_VAR
// Using structure members
Motor1.Speed := 1500.0;
Motor1.IsRunning := TRUE;
IF Motor1.Temperature > 80.0 THEN
Motor1.IsRunning := FALSE;
END_IF;Variable Scope and Storage Classes
| Declaration | Scope | Persistence |
|---|---|---|
| VAR | Local to program/function | Lost on power cycle |
| VAR_GLOBAL | Accessible from anywhere | Lost on power cycle |
| VAR_RETAIN | Local to program/function | Retained through power cycle |
| VAR_INPUT | Function/FB input parameter | Parameter passing |
| VAR_OUTPUT | Function/FB output parameter | Parameter passing |
Example of Different Variable Scopes
Variable Scopes Example
VAR_GLOBAL
SystemStatus : INT := 0; // Available everywhere
END_VAR
VAR_GLOBAL RETAIN
TotalRunTime : TIME := T#0s; // Survives power cycles
END_VAR
PROGRAM MainProgram
VAR
LocalCounter : INT := 0; // Only in this program
END_VAR
VAR_RETAIN
LastStopTime : DATE_AND_TIME; // Local but retained
END_VARType Conversion
ST provides explicit type conversion functions for safe data type changes:
Type Conversion Examples
VAR
IntValue : INT := 42;
RealValue : REAL;
StringValue : STRING;
BoolValue : BOOL;
END_VAR
// Numeric conversions
RealValue := INT_TO_REAL(IntValue); // 42 โ 42.0
IntValue := REAL_TO_INT(25.7); // 25.7 โ 25
// String conversions
StringValue := INT_TO_STRING(IntValue); // 42 โ '42'
IntValue := STRING_TO_INT('123'); // '123' โ 123
// Boolean conversions
BoolValue := INT_TO_BOOL(1); // 1 โ TRUE
IntValue := BOOL_TO_INT(TRUE); // TRUE โ 1โ ๏ธ
Type Conversion Safety
Always use explicit type conversion functions rather than relying on implicit conversions:
- โข Check for potential data loss (e.g., REAL to INT truncates)
- โข Validate string-to-number conversions for invalid input
- โข Consider range limits when converting between integer types
Common Patterns and Best Practices
โ Naming Conventions
// Clear, descriptive names MotorSpeed : REAL; IsSystemReady : BOOL; ErrorMessage : STRING; // Consistent prefixes bStartButton : BOOL; // Boolean iCounter : INT; // Integer rTemperature : REAL; // Real sPartNumber : STRING; // String
๐ก Initialization Tips
// Group related variables
VAR
// Temperature control
SetPoint : REAL := 25.0;
Hysteresis : REAL := 2.0;
// Safety limits
MaxTemp : REAL := 100.0;
MinTemp : REAL := -10.0;
END_VAR๐ Next Steps
Now that you understand variables and data types, explore:
