TwinCAT 3


TwinCAT 3 makes the development environment areas available all together, with Microsoft Visual Studio: after startup, the project folder explorer appears on the left in the general window area (see “TwinCAT System Manager” of TwinCAT 2) for communication with the electromechanical components.

After successful installation of the TwinCAT system on the PC to be used for development, TwinCAT 3 (shell) displays the following user interface after startup:

TwinCAT 3 1:
Initial TwinCAT 3 user interface

First create a new project via TwinCAT 3 2: (or under “File”→“New”→ “Project…”). In the following dialog, make the corresponding entries as required (as shown in the diagram):

TwinCAT 3 3:
Create new TwinCAT 3 project

The new project is then available in the project folder explorer:

TwinCAT 3 4:
New TwinCAT 3 project in the project folder explorer

Generally, TwinCAT can be used in local or remote mode. Once the TwinCAT system including the user interface (standard) is installed on the respective PLC (locally), TwinCAT can be used in local mode and the process can be continued with the next step, “Insert Device”.

If the intention is to address the TwinCAT runtime environment installed on a PLC remotely from another system used as a development environment, the target system must be made known first. Via the symbol in the menu bar:

TwinCAT 3 5:

expand the pull-down menu:

TwinCAT 3 6:

and open the following window:

TwinCAT 3 7:
Selection dialog: Choose the target system

Use “Search (Ethernet)...” to enter the target system. Thus another dialog opens to either:

  • enter the known computer name after “Enter Host Name / IP:” (as shown in red)
  • perform a “Broadcast Search” (if the exact computer name is not known)
  • enter the known computer – IP or AmsNetID
TwinCAT 3 8:
specify the PLC for access by the TwinCAT System Manager: selection of the target system

Once the target system has been entered, it is available for selection as follows (the correct password may have to be entered beforehand):

TwinCAT 3 9:

After confirmation with “OK” the target system can be accessed via the Visual Studio shell.

Adding devices

In the project folder explorer on the left of the Visual Studio shell user interface, select “Devices” within the element “I/O”, then right-click to open a context menu and select “Scan” or start the action via TwinCAT 3 10: in the menu bar. The TwinCAT System Manager may first have to be set to “Config mode” via TwinCAT 3 11: or via the menu “TwinCAT” → “Restart TwinCAT (Config Mode)”.

TwinCAT 3 12:
Select “Scan”

Confirm the warning message, which follows, and select the “EtherCAT” devices in the dialog:

TwinCAT 3 13:
Automatic detection of I/O devices: selection of the devices to be integrated

Confirm the message “Find new boxes”, in order to determine the terminals connected to the devices. “Free Run” enables manipulation of input and output values in “Config Mode” and should also be acknowledged.

Based on the example configuration described at the beginning of this section, the result is as follows:

TwinCAT 3 14:
Mapping of the configuration in VS shell of the TwinCAT 3 environment

The whole process consists of two stages, which can also be performed separately (first determine the devices, then determine the connected elements such as boxes, terminals, etc.). A scan (search function) can also be initiated by selecting “Device ...” from the context menu, which then only reads the elements below which are present in the configuration:

TwinCAT 3 15:
Reading of individual terminals connected to a device

This functionality is useful if the actual configuration is modified at short notice.

Programming the PLC

TwinCAT PLC Control is the development environment for generating the controller in different program environments: TwinCAT PLC Control supports all languages described in IEC 61131-3. There are two text-based languages and three graphical languages.

  • Text-based languages
    • Instruction List (IL)
    • Structured Text (ST)
  • Graphical languages
    • Function Block Diagram (FBD)
    • Ladder Diagram (LD)
    • The Continuous Function Chart Editor (CFC)
    • Sequential Function Chart (SFC)

The following section refers solely to Structured Text (ST).

In order to create a programming environment, a PLC subproject is added to the example project via the context menu of the “PLC” in the project folder explorer by selecting “Add New Item….”:

TwinCAT 3 16:
Adding the programming environment in “PLC”

In the dialog that opens, select “Standard PLC project” and enter “PLC_example” as project name, for example, and select a corresponding directory:

TwinCAT 3 17:
Specifying the name and directory for the PLC programming environment

The “Main” program, which already exists due to selecting “Standard PLC project”, can be opened by double-clicking on “PLC_example_project” in “POUs”. The following user interface is shown for an initial project:

TwinCAT 3 18:
Initial “Main” program for the standard PLC project

Now example variables and an example program have been created for the next stage of the process:

TwinCAT 3 19:
Example program with variables after a compile process (without variable integration)

The control program is now created as a project folder, followed by the compile process:

TwinCAT 3 20:
Start program compilation

The following variables, identified in the ST/PLC program with “AT%”, are then available under “Assignments” in the project folder explorer:

TwinCAT 3 21:

Assigning variables

Via the menu of an instance – variables in the “PLC” context, use the “Modify Link…” option to open a window to select a suitable process object (PDO) for linking:

TwinCAT 3 22:
Creating the links between PLC variables and process objects

In the window that opens, the process object for the “bEL1004_Ch4” BOOL-type variable can be selected from the PLC configuration tree:

TwinCAT 3 23:
Selecting BOOL-type PDO

According to the default setting, only certain PDO objects are now available for selection. In this example, the input of channel 4 of the EL1004 terminal is selected for linking. In contrast, the checkbox “All types” must be ticked to create the link for the output variables, in order to allocate a set of eight separate output bits to a byte variable in this case. The following diagram shows the whole process:

TwinCAT 3 24:
Selecting several PDOs simultaneously: activate “Continuous” and “All types”

Note that the “Continuous” checkbox was also activated. This is designed to allocate the bits contained in the byte of the “nEL2008_value” variable sequentially to all eight selected output bits of the EL2008 Terminal. It is thus possible to subsequently address all eight outputs of the terminal in the program with a byte corresponding to bit 0 for channel 1 to bit 7 for channel 8 of the PLC. A special symbol (TwinCAT 3 25:) on the yellow or red object of the variable indicates that a link exists. The links can also be checked by selecting “Goto Link Variable” from the context menu of a variable. The opposite linked object, in this case the PDO, is automatically selected:

TwinCAT 3 26:
Application of a “Goto Link Variable”, using “MAIN.bEL1004_Ch4” as an example

The process of creating links can also be performed in the opposite direction, i.e. starting with individual PDOs to a variable. However, in this example, it would not be possible to select all output bits for the EL2008, since the terminal only makes individual digital outputs available. If a terminal has a byte, word, integer or similar PDO, it is also possible to allocate this to a set of bit-standardized variables. Here, too, a “Goto Link Variable” can be executed in the other direction, so that the respective PLC instance can then be selected.

TwinCAT 3 27:

Note on type of variable assignment

The following type of variable assignment can only be used from TwinCAT version V3.1.4024.4 onwards and is only available for terminals with a microcontroller.

In TwinCAT, a structure can be created from the mapped process data of a terminal. An instance of this structure can then be created in the PLC, so it is possible to access the process data directly from the PLC without having to declare own variables.

The procedure for the EL3001 1-channel analog input terminal -10...+10 V is shown as an example.

  1. First, the required process data must be selected in the “Process data” tab in TwinCAT.
  2. After that, the PLC data type must be generated in the “PLC” tab via the check box.
  3. The data type in the “Data Type” field can then be copied using the “Copy” button.
  4. TwinCAT 3 28:
    Creating a PLC data type
  5. An instance of the data structure of the copied data type must then be created in the PLC.
  6. TwinCAT 3 29:
  7. Then the project folder must be created. This can be done either via the key combination “CTRL + Shift + B” or via the “Build” tab in TwinCAT.
  8. The structure in the “PLC” tab of the terminal must then be linked to the created instance.
  9. TwinCAT 3 30:
    Linking the structure
  10. In the PLC, the process data can then be read or written via the structure in the program code.
TwinCAT 3 31:
Reading a variable from the structure of the process data

Activation of the configuration

The allocation of PDO to PLC variables has now established the connection from the controller to the inputs and outputs of the terminals. The configuration can now be activated with TwinCAT 3 32: or via the menu under “TwinCAT” in order to transfer the settings of the development environment to the runtime system. Confirm the messages “Old configurations will be overwritten!” and “Restart TwinCAT system in Run mode” with “OK”. The corresponding assignments can be seen in the project folder explorer:

TwinCAT 3 33:

A few seconds later, the corresponding status of the Run mode is displayed in the form of a rotating symbol TwinCAT 3 34: at the bottom right of the VS shell development environment. The PLC system can then be started as described below.

Starting the controller

Select the menu option “PLC” → “Login” or click on TwinCAT 3 35: to link the PLC with the real-time system and load the control program for execution. This results in the message ”No program on the controller! Should the new program be loaded?”, which should be acknowledged with “Yes”. The runtime environment is ready for the program to be started by clicking on symbol TwinCAT 3 36:, the “F5” key or via “PLC” in the menu, by selecting “Start”. The started programming environment shows the runtime values of individual variables:

TwinCAT 3 37:
TwinCAT 3 development environment (VS shell): logged-in, after program startup

The two operator control elements for stopping TwinCAT 3 38: and logout TwinCAT 3 39: result in the required action (also, “Shift + F5” can be used for stop, or both actions can be selected via the PLC menu).