TwinCAT Reference
Free reference guide: TwinCAT Reference
About TwinCAT Reference
The TwinCAT Reference is a searchable programming reference for Beckhoff TwinCAT 3 PLC development using IEC 61131-3 Structured Text (ST). It covers the complete ST syntax including variable declarations (VAR/VAR_GLOBAL), control flow (IF/ELSIF/CASE/FOR/WHILE), function blocks (FUNCTION_BLOCK with METHOD, PROPERTY, INTERFACE), and data types (STRUCT, ENUM, ARRAY, POINTER/REFERENCE) with ready-to-use code examples.
Beyond basic syntax, this reference includes standard IEC function blocks (TON, TOF, TP timers, CTU/CTD/CTUD counters, R_TRIG/F_TRIG edge detection, SR/RS flip-flops) and PLCopen motion control blocks (MC_MoveAbsolute, MC_MoveRelative, MC_MoveVelocity, MC_Home, MC_Power) with complete parameter lists showing Axis, Execute, Position, Velocity, Acceleration, and Deceleration arguments.
The EtherCAT and communication section covers ADS protocol usage (FB_AdsRead with NETID, PORT, IDXGRP/IDXOFFS parameters) and CoE SDO access (FB_EcCoESdoRead/Write for reading and writing servo parameters like 0x6041 StatusWord and 0x6060 ModeOfOperation). TwinCAT-specific libraries including Tc2_Utilities (string conversion, MEMCPY) and Tc2_System (time functions, CPU usage) are also documented.
Key Features
- Complete IEC 61131-3 Structured Text syntax: VAR declarations, IF/CASE/FOR/WHILE control flow
- Function block programming with METHOD, PROPERTY, INTERFACE for object-oriented PLC design
- Standard timer blocks (TON, TOF, TP) and counter blocks (CTU, CTD, CTUD) with usage examples
- PLCopen motion control: MC_MoveAbsolute, MC_MoveRelative, MC_MoveVelocity, MC_Home, MC_Power
- EtherCAT CoE SDO read/write (FB_EcCoESdoRead/Write) for servo parameter access (0x6041, 0x6060)
- ADS communication setup with NETID, PORT, index group/offset addressing for remote PLC access
- Data type definitions: STRUCT recipes, ENUM state machines, multi-dimensional ARRAY, POINTER/REFERENCE
- Beckhoff library reference for Tc2_Utilities (string conversion, memory) and Tc2_System (time, CPU)
Frequently Asked Questions
What Structured Text syntax does this TwinCAT reference cover?
It covers variable declarations (VAR/VAR_GLOBAL with type initialization), conditional branching (IF/ELSIF/ELSE, CASE...OF for state machines), loops (FOR with BY step, WHILE, REPEAT), and complete function block definitions with VAR_INPUT, VAR_OUTPUT, METHOD, PROPERTY, and INTERFACE for polymorphism.
How are state machines implemented in TwinCAT Structured Text?
The reference shows CASE...OF syntax commonly used for state machines: CASE nStep OF with numbered states (0: Initialize, 10: Wait, 20: Run). Each state can set outputs and advance to the next step conditionally using IF statements and nStep assignments.
What PLCopen motion control blocks are included?
Five core motion blocks: MC_Power (servo enable with Enable_Positive/Negative), MC_Home (homing with position reset), MC_MoveAbsolute (absolute positioning), MC_MoveRelative (incremental moves), and MC_MoveVelocity (continuous speed control). Each includes Axis, Execute, Position/Velocity/Acceleration parameters.
How do I read and write EtherCAT servo parameters?
Use FB_EcCoESdoRead to read parameters (e.g., StatusWord at index 0x6041) and FB_EcCoESdoWrite to write parameters (e.g., ModeOfOperation at index 0x6060). Both require sNetId, nSlaveAddr, nIndex, nSubIndex, buffer pointer (ADR), buffer size (SIZEOF), and bExecute trigger.
What is ADS communication and how is it configured?
ADS (Automation Device Specification) is Beckhoff's communication protocol. FB_AdsRead requires a NETID string (e.g., '5.80.192.168.1.1'), PORT number (851 for PLC runtime), IDXGRP/IDXOFFS for variable addressing, buffer length via SIZEOF, and destination address via ADR.
What timer and counter function blocks are covered?
Timers: TON (on-delay, output after PT elapsed), TOF (off-delay, output maintained after input OFF), TP (pulse, fixed-duration output). Counters: CTU (up counter with PV preset), CTD (down counter), CTUD (bidirectional). Edge detection: R_TRIG (rising) and F_TRIG (falling). Flip-flops: SR (set priority) and RS (reset priority).
How do I define custom data types in TwinCAT?
STRUCT defines record types (TYPE ST_Recipe : STRUCT with STRING, REAL, INT, BOOL fields). ENUM creates named constants (TYPE E_MachineState with IDLE:=0, RUNNING:=1, etc.). ARRAY supports multi-dimensional arrays (ARRAY[0..9] OF REAL). POINTER TO and REFERENCE TO provide indirect access with ADR() and ^ dereference.
What Beckhoff-specific libraries are documented?
Tc2_Utilities covers string conversion (REAL_TO_STRING, STRING_TO_INT) and memory operations (MEMCPY with ADR/SIZEOF). Tc2_System provides system time access (fbGetTime with TIMESTR output) and system information (CPU usage monitoring via fbSysInfo.CPUUSAGE).