Open a TwinCAT 2 PLC project

TwinCAT XAE is started. A TwinCAT project is open. You should be aware of the restrictions described below the following guide.
1. Mark the PLC object in the Solution Explorer view and select the Add Existing Item command in the Project menu or the context menu.
2. In the Open dialog, select the desired Plc 2.x project or library from the file system. To search, you can set the file filter in the bottom right-hand corner of the dialog.
After that the TwinCAT 2.x converter starts automatically.
3. The TwinCAT 2.x converter checks whether the project can be compiled without error. If so, it processes the project automatically.
If the project contains visualization objects with placeholder variables that the converter cannot resolve, the respective visualizations are integrated directly as a grouping in place of the visualization references.
4. Library conversion: If a library for which no conversion rule has been defined yet is referenced in the project to be opened, the dialog Converting a library reference appears. Define here whether and how the converter should replace the previous library reference by an up-to-date one. If in doing so you select a library for which the project information is missing, the Project information dialog appears, which you have to fill out.
The converter loads the adapted project.

Restrictions when reusing a TwinCAT 2.x project in TwinCAT 3.1

Open a TwinCAT 2 PLC project 1:

Automatic syntax adjustments only with previous compilability

The syntax differs between TwinCAT 2 (TC2) and TwinCAT 3 (TC3) in certain respects (e.g. when initializing arrays). Please note that the converter only adapts the syntax at these code positions if the TC2 project to be converted can be compiled on the TC2 side.

Specifically, this means:

During the conversion process, the converter contained in TC3 first compiles the selected TC2 project with a TC2 compiler. Syntax adjustments of the TC2 code are only made during the creation of the TC3 SPS project if this TC2-side compilation is successful. The prerequisite for a successful TC2-side compilation is that all necessary TC2 libraries are made available to the converter. This can be achieved, for example, by inserting the libraries referenced in the TC2 project into the folder C:\TwinCAT\\3.1\Components\\Plc\Converter\\Lib, or by notifying the converter of the locations of the TC2 libraries via the displayed conversion dialogs.

Compilation

It must be possible to compile the project in TwinCAT 2.x PLC Control without compilation errors. Nevertheless, TwinCAT issues warnings during the compilation. These are evoked by implicit conversions that can lead to the loss of information (for example due to the change of a sign).

TwinCAT 3.1 tests case statements against the switch variable: CASE USINT OF INT is not checked in TwinCAT 2.x, but an error message is displayed when importing into TwinCAT 3.1.

Libraries

All variables and constants used in a library also have to be declared in this library. It must be possible to compile the library in TwinCAT 2.x without errors.

Syntactic and semantic restrictions

  • FUNCTIONBLOCK is no longer a valid keyword in place of FUNCTION_BLOCK
  • A ":" must follow after TYPE (declaration of a structure).
  • ARRAY initialization must be enclosed in parentheses.
  • A local declaration of an enumeration is no longer possible, except within TYPE: in the case of a TwinCAT 2.x import a local enumeration declaration is automatically converted into an explicit type definition.
  • INI is no longer supported (you have to replace this in the code by the Init method).
  • In function calls, it is no longer possible to mix explicit parameter assignments with implicit assignments. Therefore the order of the parameter input assignments can be changed:
    fun(formal1 := actual1, actual2); // → Fehlermeldung
    fun(formal2 := actual2, formal1 := actual1); // gleiche Semantik wie folgende Zeile:
    fun(formal1 := actual1, formal2 := actual2);
  • TwinCAT 2.x pragmas are not converted. They generate a warning in TwinCAT 3.1.
  • The TRUNC operator now converts to the data type DINT instead of INT; in the case of a TwinCAT 2.x import, TwinCAT 3.1 automatically adds an appropriate type conversion.

Memory

  • In contrast to TwinCAT 2, TwinCAT 3.1 has an 8-byte alignment. For further details and samples see Reference Programming > Alignment.
  • Instances of the data type STRING are pre-initialized in TwinCAT 2 by zeroing the entire memory area. In TwinCAT 3.1, however, only the first byte of such an instance is zeroed. Due to the zero termination, the instance is also empty.

Visualization

Placeholders and their replacement

Placeholder

VAR_INPUT

Use

Replacement

 

MAIN.$LocalVar$.aArr[0]

localVar: MyStruct;

localVar.aArr[0]

localVar := MAIN.myStructVar

 

$Var$.aArr[0]

Var : MyStruct;

Var.aArr[0]

Var := MAIN.myStructVar

 

MAIN.myStructVar.aArr[$Index$]

Index : INT;

MAIN.myStructVar.aArr[Index]

Index := 0

Problematic placeholders

  • Placeholders inside a text:
    Text: $axle$-Axis
    Correction:
    localVar : STRING;
    Text: %s-Axis
    TextVariable : localVar
  • Placeholder describes only part of a variable name:
    axis$axis$spur$spur$.fActPosition
    Correction:
    Define only one placeholder for the placeholder axis$axis$spur$spur$.
    axis_spur : MyFunctionBlock;
    Then transfer the corresponding instance of the function block directly.
    axis_spur := MAIN.axis1spur2;
  • Placeholder is replaced by an expression:
    $Expression$ → MAIN.var1 + MAIN.var2
    Correction:
    You must transfer the expression to a help variable and then transfer this help variable as an instance.
  • Placeholder describes a program name: $Program$.bToggle → MAIN.bToggle D
    The converter cannot transfer this form of placeholder to TwinCAT 3.1. However, they are rarely used in practice.
  • Placeholder is replaced by various types:
    $Var$
    → Replacement 1: MAIN.n (INT)
    → Replacement 2: MAIN.st (STRING)
    Correction:
    Define two different placeholders for this in the interface.
  • The visualization is located in a library. You replace the placeholder later from any project where you use the visualization.
    Correction:
    You must replace the TYPE_NONE data types manually here. However, there is a possibility for you to integrate the library in a project and for the placeholder to be replaced properly. If you now import this project, the data type will also be determined correctly in the library.

Non-importable elements

Trend, ActiveX – the import is not possible because the implementation is very different. In TwinCAT 3.1 a corresponding warning is given and a corresponding manual reproduction is necessary.

Programming languages

ST, IL, FBD

No restrictions

 

LD

TwinCAT 3.1 imports function blocks with parallel branches so that the part before the branch is repeated for each branch. This corresponds to the generated code that TwinCAT 2.x generates for parallel branches.

Open a TwinCAT 2 PLC project 2:

 

SFC

  • Step variables explicitly declared by the user must be declared locally in the SFC editor. You must not declare them as VAR_INPUT, VAR_OUTPUT or VAR_INOUT, because TwinCAT 3.1 cannot automatically adapt the calls. Explanation: Steps no longer use boolean variables for the management of the internal states in TwinCAT 3.1, but also structures of the type SFCStepType.
  • Identifier: The following identifiers may not begin with an underscore:
    • Names of IEC actions in the tree
    • Variables that are called in an IEC association list
    • Names of programmed-out transitions

Explanation: Implicit variables that TwinCAT 3.1 creates for actions are given an underscore as a prefix in TwinCAT 3.1. An invalid identifier with a double underscore would result.

 

CFC

  • Large function blocks: The layout of large function blocks may suffer a loss of quality through being imported; the function block boxes may overlap significantly.
  • Macros: Macros cannot be imported.

See also: