Conditional pragmas

The purpose of conditional pragmas is to influence the generation of code in the pre-compilation process or the compilation process. The programming language ST supports these pragmas.

Hint

You must use the conditional pragmas in the implementation part of POUs. CODESYS does not evaluate these conditional pragmas if you use them in the declaration part!

With conditional pragmas you affect whether implementation code is taken into account for the compilation. For example, you can make this dependent on whether a certain variable is declared, whether a certain function block is present, etc.

Pragma Description
{define <identifier> <string>} The value can be queried and compared later with hasvalue.
{undefine <identifier>} The {define} instruction of the identifier <identifier> is cancelled; the identifier is ‘undefined’ again from now on. The pragma is ignored if the specified identifier is not defined at all.

{IF <expr>}...

{ELSIF <expr>}...

{ELSE}...

END_IF}

These are pragmas for the conditional compilation.

The specified expressions <expr> must be constant at the time of compilation; they are evaluated in the order in which they appear here until one of the expressions indicates a non-zero value. The text linked to the instruction is compiled; the other lines are ignored. The order of the sections is fixed. The ELSIF and ELSE sections are optional. The ELSIF sections may occur any number of times. You can use several conditional compilation operators within the constants <expr>.

<expr>} You can use one or more operators within the constant expression <expr> within a conditional compilation pragma {IF} or {ELSIF} .

Note

You can enter expressions and define definitions as compiler definitions in the Compile tab in the Properties dialog box of POUs. If you enter define definitions in the properties dialog box, you must omit the term {define}, contrary to the definition in the implementation code. In addition, you can specify several define definitions in the properties dialog box, separated by commas.

See also

Operator defined (<identifier>)

This operator causes the expression to be given the value TRUE. The requirement is that the identifier <identifier> was defined with the help of a {define} instruction and not undefined again afterwards with an {undefine} instruction; otherwise FALSE is returned.

Example

Requirement: There are two applications, App1 and App2. Variable pdef1 is defined by a {define} instruction in App1, but not in App2.

{IF defined (pdef1)}
(* This code is processed in App1 *)
{info 'pdef1 defined'}
hugo := hugo + SINT#1;
{ELSE}
(* the following code is only processed in App2 *)
{info 'pdef1 not defined'}
hugo := hugo - SINT#1;
{END_IF}

This additionally contains an example of a message pragma: only the information pdef1 defined is displayed in the Message window when the application is compiled, because pdef1 is actually defined. The message ‘pdef1 not defined’ is displayed if pdef1 is not defined

Operator defined (variable: <variable>)

This operator causes the expression to be given the value TRUE if the variable <variable> is declared within the current scope of validity; otherwise FALSE is returned.

Example

Requirement: There are two applications, App1 and App2. Variable g_bTest is declared in App1, but not in App2.

{IF defined (variable: g_bTest)}
(* the following code is only processed in App2*)
g_bTest := x > 300;
{END_IF}

Operator defined (type: <identifier>)

The operator causes the expression to be given the value TRUE if a data type is declared with the identifier <identifier>; otherwise FALSE is returned.

Example

Requirement: There are two applications, App1 and App2. Data type DUT is declared in App1, but not in App2.

{IF defined (type: DUT)}
(* the following code is only processed in App1*)
bDutDefined := TRUE;
{END_IF}

Operator defined (pou: <pou name>)

The operator causes the expression to be given the value TRUE if a function block or an action with name <pou-name> is present; otherwise FALSE is returned.

Example

Requirement: There are two applications, App1 and App2. The function block CheckBounds exists in App1, but not in App2.

{IF defined (pou: CheckBounds)}
(* the following code is only processed in App1 *)
arrTest[CheckBounds(0,i,10)] := arrTest[CheckBounds(0,i,10)] + 1;
{ELSE}
(* the following code is only processed in App2 *)
arrTest[i] := arrTest[i]+1;
{END_IF}

Operator defined (task: <identifier>)

Not yet implemented!

The operator causes the expression to be given the value TRUE if a task is defined with the name <identifier>; otherwise FALSE is returned.

Example

Requirement: There are two applications, App1 and App2. Task PLC_PRG_Task is defined in App1, but not in App2.

IF defined (task: PLC_PRG_Task)}
(* the following code is only processed in App1 *)
erg := plc_prg.x;
{ELSE}
(* the following code is only processed in App2 *)
erg := prog.x;
{END_IF}

Operator defined (resource: <identifier>)

Not yet implemented!

The operator causes the expression to be given the value TRUE if a resource object with the name <identifier> exists for the application; otherwise FALSE is returned.

Example

Requirement: There are two applications, App1 and App2. A resource object glob_var1 of the global variable list exists for App1, but not for App2.

{IF defined (resource:glob_var1)}
(* the following code is only processed in App1 *)
gvar_x := gvar_x + ivar;
{ELSE}
(* the following code is only processed in App2 *)
x := x + ivar;
{END_IF}

Operator defined (IsSimulationMode)

The operator causes the expression to be given the value TRUE if the application runs on a simulated device, i.e. in simulation mode.

Operator defined (IsLittleEndian)

The operator causes the expression to be given the value FALSE, if the CPU is ‘Big-Endian (Motorola byte order)’.

Operator defined (IsFPUSupported)

If this expression returns the value TRUE, the code generator FPU (Floating point unit) generates code for the calculations with REAL values. Otherwise CODESYS emulates FPU operations; however, this is much slower.

Operator hasvalue (RegisterSize, '<register size>')

<register size>: Size of a CPU register in bits

This operator causes the expression to return the value TRUE if the size of a CPU register is equal to <register size>.

Possible values for <register size>

  • 16 for 186 and C16x,
  • 64 for X86-64 bit
  • 32 for X86

Operator hasvalue (PackMode, '<pack mode value>')

The checked pack mode depends on the device description, not on the pragma that can be specified for individual DUTs.

Operator hasattribute (pou: <pou name>, '<attribute>')

This operator causes the expression to be given the value TRUE if the attribute <attribute> is specified in the first line of the declaration part of the function block pou-name; otherwise FALSE is returned.

Example

Requirement: There are two applications, App1 and App2. A function fun1 is defined in App1 and App2, but in App1 the attribute vision is assigned to it in addition.

In App1:

{attribute 'vision'}
FUNCTION fun1 : INT
VAR_INPUT
 i : INT;
END_VAR
VAR
END_VAR

In App2:

FUNCTION fun1 : INT
VAR_INPUT
 i : INT;
END_VAR
VAR
END_VAR

Pragma instruction:

{IF hasattribute (pou: fun1, 'vision')}
(* the following code is only processed in App1 *)
ergvar := fun1(ivar);
{END_IF}

See also

Operator hasattribute (variable: <variable>, '<attribute>')

This operator causes the expression to be given the value TRUE if the attribute '<attribute>' is assigned to the variable with the help of the instruction {attribute '<attribute>'} in the line before the variable declaration; otherwise FALSE is returned

Example

Requirement: There are two applications, App1 and App2. Variable g_globalInt is used in App1 and App2, but in App1 the attribute 'DoCount' is assigned to it in addition.

Declaration of g_GlobalInt in App1

VAR_GLOBAL
 {attribute 'DoCount'}
 g_globalInt : INT;
 g_multiType : STRING;
END_VAR

Declaration of g_GlobalInt in App2

VAR_GLOBAL
 g_globalInt : INT;
 g_multiType : STRING;
END_VAR

Pragma instruction:

{IF hasattribute (variable: g_globalInt, 'DoCount')}
(* the following code is only processed in App1 *)
g_globalInt := g_globalInt + 1;
{END_IF}

See also

Operator hastype (variable: <variable>, <type-spec>)

This operator causes the expression to be given the value TRUE if the variable <variable> is of the data type <type-spec>; otherwise FALSE is returned.

Possible data types for <type-spec>:

  • LREAL
  • REAL
  • LINT
  • DINT
  • INT
  • SINT
  • ULINT
  • UDINT
  • UINT
  • USINT
  • TIME
  • LWORD
  • DWORD
  • WORD
  • BYTE
  • BOOL
  • STRING
  • WSTRING
  • DATE_AND_TIME
  • DATE
  • TIME_OF_DAY

Example

Requirement: There are two applications, App1 and App2. Variable g_multitype is declared in App1 with data type LREAL, in App2 with data type STRING.

{IF (hastype (variable: g_multitype, LREAL))}
(*  the following code is only processed in App1 *)
g_multitype := (0.9 + g_multitype) * 1.1;
{ELSIF (hastype (variable: g_multitype, STRING))}
(* the following code is only processed in App2 *)
g_multitype := 'this is a multitalent';
{END_IF}

Operator hasvalue (<define-ident>, '<char-string>')

This operator causes the expression to be given the value TRUE if a variable is defined with the identifier <define-ident> and has the value <char-string>; otherwise FALSE is returned.

Example

Requirement: There are two applications, App1 and App2. The variable test is used in the applications App1 and App2; in App1 it is given the value 1, in App2 the value 2.

{IF hasvalue(test,'1')}
(*  the following code is only processed in App1 *)
x := x + 1;
{ELSIF hasvalue(test,'2')}
(*  the following code is only processed in App2 *)
x := x + 2;
{END_IF}

Operator NOT <operator>

The expression is given the value TRUE if the reverse value of <operator> returns the value TRUE. <operator> can be one of the operators described in this chapter.

Example

Requirement: There are two applications, App1 and App2. PLC_PRG1 is present in App1 and App2, POU CheckBounds exists only in App1.

{IF defined (pou: PLC_PRG1) AND NOT (defined (pou: CheckBounds))}
(* the following code is only processed in App2 *)
bANDNotTest := TRUE;
{END_IF}

Operator <operator> AND <operator>

The expression is given the value TRUE if the two specified operators return TRUE. <operator> can be one of the operators described in this chapter.

Example

Requirement: There are two applications, App1 and App2. PLC_PRG1 is present in App1 and App2, the POU CheckBounds only in App1.

{IF defined (pou: PLC_PRG1) AND (defined (pou: CheckBounds))}
(* the following code is only processed in App1,  *)
bANDTest := TRUE;
{END_IF}

Operator <operator> OR <operator>

The expression returns TRUE if one of the two specified operators returns TRUE. <operator> can be one of the operators described in this chapter.

Example

Requirement: There are two applications, App1 and App2. PLC_PRG1 is present in App1 and App2, POU CheckBounds only in App1.

{IF defined (pou: PLC_PRG1) OR (defined (pou: CheckBounds))}
(* the following code is only processed in App1 and in App2 *)
bORTest := TRUE;
{END_IF}

Operator (<operator>)

() parenthesizes the operators.

See also