PLC

Using pragmas

 

What is a pragma in TwinCAT?

A pragma is a special instruction in the source code of an application. It influences the properties of one or several variables with regard to precompilation or compilation (code generation).

There are pragmas for different effects: initialization of a variable, monitoring of a variable, adding of a variable to a parameter list or symbol configuration, enforcing of message outputs during the compile process, behavior of a variable under certain conditions etc.

 
Case-sensitivity must be respected!
 
 

How and where do you insert a pragma?

A pragma instruction is specified in curly brackets. Attributes and texts used in the instruction are enclosed in inverted commas. The opening bracket may immediately follow a variable name. The opening and closing brackets must be on the same line.

Examples:

{warning 'This is not allowed'} 
{attribute 'obsolete' := 'datatype fb1 not valid!'} 

Possible insert positions:

In the declaration part of a POU. In the text-based declaration editor, pragmas can be added directly as line. In the tabular editor, pragmas, which must be above the first declaration line, can be added in the Attributes dialog (double-click on the Attributes column).
In a global variable list
In the implementation part of a POU in a separate line, or in a line together with code text.
FBD/LD/AWL: Pragmas in networks of the FBD/LD/IL editor are entered like a label: Select the command FBD/LD/IL > Insert label and replace the standard text Label: in the text field of the brand with the corresponding pragma instruction. If you want to use a pragma in addition to a label, first enter the pragma, then the label.

 

 
Pragmas in TwinCAT are not a one-to-one implementation of the C processor directives. A pragma has to be positioned like a normal instruction. It cannot be used within an expression.

Incorrect and correct positioning of a conditional pragma:

Incorrect:
Correct:
{IF defined(abc)}
IF x = abc THEN
{ELSE}
IF x = 12 THEN
{END_IF}
y := {IF defined(cde)} 12;
{ELSE} 13;
{END_IF}
END_IF
{IF defined(abc)}
IF x = abc THEN
{IF defined(cde)}
y := 12;
{ELSE}
y := 13;
{END_IF}
END_IF
{ELSE}
IF x = 12 THEN
{IF defined(cde)}
y := 12;
{ELSE}
y := 13;
{END_IF}
END_IF
{END_IF}
 
 
In the POU Properties, category Advanced, you can enter Defines, which can be queried in pragmas.

Scope:

Depending on the type and content of a pragma, it affects the following:

the following declarations
specifically the following instruction
all following instructions until it is canceled with a corresponding pragma
all following instructions, until the same pragma is executed with other parameters or the end of the code is reached. "Code" in this context means: declaration part, implementation part, global variable list, type declaration. A pragma, which appears on its own in the first line of the declaration part and is not superseded or canceled by another pragma, is therefore active for the entire object.
 
 

What types of pragmas are there?

The TwinCAT pragmas are divided into the following categories:

Attribute pragmas: influence compilation and precompilation (Attribute pragmas)
Message pragmas: output of user-defined messages during the compile process (Message pragmas)
Conditional pragmas: influence the code generation (Conditional pragmas)
User-defined pragmas (User-defined attributes)
 

See also:

Using the Auto Declare dialog