Twincat and Structured Text
Getting in a new programming language can be difficult somethimes.
I will try to get you an short overview, to get started.
Variables
Variables are containers for storing data values.
VAR
// Internal logic variable (no hardware link)
bState: BOOL;
// Flexible input (auto-assigned by TwinCAT)
bSensor AT %I*: BOOL; // e.g., connected to a sensor
// Flexible output (auto-assigned by TwinCAT)
nSetValue AT %Q*: UINT; // e.g., drives an actuator
// Optional: Directly addressed example
// bEmergencyStop AT %IX5.3: BOOL; // Input bit 5.3
END_VAR
Attributes
Variables can have some custom attributes,
which can help to debug code in online mode.
There are many more in the twincat documentation.
VAR
{attribute 'hide'}
bHide: BOOL; // will be hidden
{attribute 'displaymode':='hex'}
nValue AT %I*: UINT; // displayed as hex value
{attribute 'TcDisplayScale' := '0-10'}
nSetValue AT %Q*: UINT; // 0-10
END_VAR
Datatypes
Variables must have a specified data type.
Data Type | Size | Description |
---|---|---|
BOOL | 1 byte | Logical value: TRUE or FALSE |
BYTE | 1 byte | Unsigned 8-bit integer (0 to 255) |
INT | 2 bytes | Signed 16-bit integer (-32,768 to 32,767) |
UINT | 2 bytes | Unsigned 16-bit integer (0 to 65,535) |
DINT | 4 bytes | Signed 32-bit integer |
UDINT | 4 bytes | Unsigned 32-bit integer |
LINT | 8 bytes | Signed 64-bit integer |
ULINT | 8 bytes | Unsigned 64-bit integer |
REAL | 4 bytes | 32-bit floating point (single precision) |
LREAL | 8 bytes | 64-bit floating point (double precision) |
STRING | 1 bytes | ASCII string null-terminated |
WSTRING | 2 bytes | Wide-character string UTF-16 |
Structs
A STRUCT is define a group of variables
TYPE MotorData :
STRUCT
speed: REAL;
current: REAL;
isRunning: BOOL;
END_STRUCT
END_TYPE
Operators
How operators look like
Logical Operators
Operator | Name | Description |
---|---|---|
AND | AND | only if both inputs are TRUE |
OR | OR | TRUE if at least one input is TRUE |
NOT | NOT | Inverts the input |
XOR | XOR | TRUE if inputs are different |
Bitwise Operators
Operator | Name | Description |
---|---|---|
AND | AND | Performs bitwise AND |
OR | OR | Performs bitwise OR |
XOR | XOR | Performs bitwise XOR |
NOT | NOT | Inverts all bits |
SHL | Shift Left | Shifts bits left |
SHR | Shift Right | Shifts bits right |
Comparison Operators
Operator | Name | Description |
---|---|---|
= | Equal | Checks if two values are equal. |
<> | Not Equal | Checks if two values are different. |
> | Greater Than | Left operand is greater than |
< | Less Than | Left operand is smaller than |
>= | Greater Equal | Left operand is greater than or equal |
<= | Less Equal | Left operand is smaller than or equal |
If Statement
IF statement is used to test a condition and to execute the subsequent instructions
VAR
bExecute: BOOL;
bCondition: BOOL;
END_VAR
// Simple IF statement
IF bExecute THEN
// Code executes only if bExecute is TRUE
END_IF
// IF-ELSE structure
IF bExecute THEN
// Runs when bExecute is TRUE
ELSE
// Runs when bExecute is FALSE
END_IF
// IF-ELSIF-ELSE structure (multiple conditions)
IF bExecute THEN
// Executes if bExecute is TRUE
ELSIF bCondition THEN
// Executes if bCondition is TRUE (bExecute is FALSE)
ELSE
// Executes if neither bExecute nor bCondition is TRUE
END_IF
Switch Statement
CASE instruction is used to group multiple conditional instructions
VAR
nSwitch: INT;
END_VAR
CASE nSwitch OF
0:
// Code block for case x
1:
// Code block for case y
ELSE
// Default code block (when no cases match)
END_CASE
Loops
For Loop
FOR loop is used to execute instructions with a specific number of retries
VAR
nIndex: INT;
aData: ARRAY [0..9] OF REAL:= [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0];
END_VAR
// mimimal example
FOR nIndex := 0 TO 9 DO // with step width FOR nIndex := 0 TO 9 BY 1 DO
// Output the current value of nIndex
ADSLOGSTR(ADSLOG_MSGTYPE_HINT, 'Log Message: %s', LREAL_TO_FMTSTR( aData[nIndex], 2, TRUE ));
END_FOR
Exit a for loop early and return to the program flow
FOR nCounter:= 0 TO 10 DO
IF nCounter >= 5 THEN
EXIT;
END_IF
END_FOR
While Loop
WHILE loop is used to execute instructions repeatedly until the termination condition applies
VAR
nIndex : INT := 0; // Initialize index
END_VAR
WHILE nIndex < 5 DO
ADSLOGSTR(ADSLOG_MSGTYPE_HINT, 'Log Message: %s', INT_TO_STRING(nIndex));
nIndex := nIndex + 1; // Increment index
END_WHILE
Exit a while loop
WHILE nIndex < 5 DO
IF nIndex >= 1000 then
EXIT;
END_IF
nIndex := nIndex + 1; // Increment index
END_WHILE
More will follow in part 2