Syntax Basics
Structured Text follows a clear, readable syntax similar to Pascal or other high-level programming languages. Understanding these fundamental rules will help you write clean, maintainable ST code.
🚀 Enhance Your Development Experience
Get syntax highlighting, IntelliSense, and debugging support for Structured Text:
Install VS Code Extension →Basic Statement Structure
Semicolon Termination
Every statement in Structured Text must end with a semicolon (;):
Basic Assignment Statements
// Variable assignments Counter := Counter + 1; Temperature := 25.5; IsRunning := TRUE; // Function calls Result := MyFunction(Input1, Input2); Timer1(IN := StartSignal, PT := T#5s);
Comments
ST supports two types of comments for documenting your code. Good commenting is essential for maintaining industrial automation systems over their long operational lifespans.
Single-line Comments
Use // for brief explanations and inline documentation:
// Motor control initialization
MotorSpeed := 0.0; // Start at zero speed
MotorDirection := FORWARD; // Default direction
MotorEnabled := FALSE; // Safety: start disabled
// Process variables
Temperature := SensorReading; // Read from analog input
Pressure := ADC_Value * 0.1; // Convert to bar (0.1 bar/unit)
// Safety interlocks
IF EmergencyStop THEN
MotorEnabled := FALSE; // Immediate stop
AlarmActive := TRUE; // Trigger alarm
END_IF;Multi-line Comments
Use (* *) for detailed documentation and code blocks:
(*
PID Controller Implementation
- Kp: Proportional gain = 2.0
- Ki: Integral gain = 0.5
- Kd: Derivative gain = 0.1
- Output range: 0-100%
*)
Error := SetPoint - ProcessValue;
Integral := Integral + Error * ScanTime;
Derivative := (Error - LastError) / ScanTime;
Output := Kp * Error + Ki * Integral + Kd * Derivative;
(*
Temporarily disabled for testing
IF Pressure > MaxPressure THEN
EmergencyShutdown := TRUE;
END_IF;
*)💡 Best Commenting Practices
✅ Good Comments:
- • Explain why, not just what
- • Document safety-critical logic
- • Include units and ranges
- • Reference documentation sources
- • Explain complex calculations
❌ Avoid:
- • Obvious comments (Counter := Counter + 1; // Increment)
- • Outdated or incorrect comments
- • Commenting every single line
- • Using comments to fix bad code
- • Leaving TODO comments in production
Code Blocks and Structure
ST uses keywords to define code blocks. Blocks are always terminated with corresponding END keywords:
Control Block Structures
// IF block structure
IF Temperature > 80.0 THEN
OverheatAlarm := TRUE;
CoolingFan := TRUE;
ELSIF Temperature > 60.0 THEN
OverheatAlarm := FALSE;
CoolingFan := TRUE;
ELSE
OverheatAlarm := FALSE;
CoolingFan := FALSE;
END_IF;
// CASE block structure
CASE OperationMode OF
1: SingleCycle();
2: ContinuousMode();
3: MaintenanceMode();
ELSE
ErrorHandler();
END_CASE;Operators and Expressions
Arithmetic Operators
| Operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| MOD | Modulo |
| ** | Exponentiation |
Comparison Operators
| Operator | Description |
|---|---|
| = | Equal to |
| <> | Not equal to |
| < | Less than |
| <= | Less than or equal |
| > | Greater than |
| >= | Greater than or equal |
Logical Operators
// Boolean operations EnableOutput := InputA AND InputB; AlarmCondition := HighTemp OR LowPressure; SafetyOK := NOT EmergencyStop; // Complex expressions with precedence Result := (A AND B) OR (C AND NOT D);
Assignment vs. Comparison
Important Distinction
ST uses different operators for assignment and comparison:
// Assignment uses :=
Counter := 10;
Temperature := SensorReading;
// Comparison uses =
IF Counter = 10 THEN
// Do something
END_IF;Case Sensitivity
Structured Text is case-insensitive for keywords, identifiers, and operators:
Case Insensitivity Examples
// These are all equivalent IF temperature > 80 THEN if Temperature > 80 then If TEMPERATURE > 80 Then // Variable names are also case-insensitive MyVariable := 10; myvariable := 10; // Same variable MYVARIABLE := 10; // Same variable
Formatting and Style
While ST is flexible with formatting, following consistent style improves readability:
❌ Poor Formatting
if a>10 then b:=20;c:=30;end_if; for i:=1 to 100 do sum:=sum+i;end_for;
✅ Good Formatting
IF A > 10 THEN
B := 20;
C := 30;
END_IF;
FOR I := 1 TO 100 DO
Sum := Sum + I;
END_FOR;Common Syntax Patterns
Typical Program Structure
Typical Program Structure
// Variable declarations (usually at the top)
VAR
Counter : INT := 0;
Temperature : REAL;
IsRunning : BOOL := FALSE;
END_VAR
// Program logic
IF StartButton AND NOT StopButton THEN
IsRunning := TRUE;
Counter := Counter + 1;
END_IF;
// Read inputs
Temperature := TemperatureSensor.Value;
// Control logic
IF Temperature > SetPoint THEN
CoolingValve := TRUE;
ELSE
CoolingValve := FALSE;
END_IF;
// Safety checks
IF EmergencyStop THEN
IsRunning := FALSE;
AllOutputs := FALSE;
END_IF;💡 Next Steps
Now that you understand the basic syntax, you're ready to learn about:
