|
Important |
|---|---|
|
You use conditional pragmas in the implementations of ST POUs. CODESYS does not evaluate the conditional pragmas when you use them in the declaration part. The exception is the conditional pragma with the |
The purpose of conditional pragmas is to influence how code is generated in the pre-compile or compile process. The ST implementation language supports these pragmas.
With conditional pragmas, you influence whether or not implementation code is taken into account for the compile. For example, you can make this dependent on whether a specific variable is declared, whether a specific POU exists, etc.
|
Pragma |
Description |
|---|---|
|
|
The value can be queried and compared with |
|
|
The |
|
|
These are pragmas for the conditional compile. The specified expressions |
|
|
You can use one or more operators within the constant expression |
|
Tip |
|---|---|
|
You can specify expressions and |
The operator causes the expression to be given the value TRUE when the variable <variable name> is declared within the current scope. Otherwise FALSE is returned.
Requirement: The two applications App1 and App2 exist. The 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}
The operator causes the expression to be given the value TRUE. The requirement is that the identifier <identifier> has been defined by means of a {define} statement and not undefined afterwards with an {undefine} statement. Otherwise FALSE is returned.
Requirement: The applications App1 and App2 exist. The pdef1 variable is defined by a {define} statement 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 also includes an example of a message pragma: Only the message pdef1 defined is displayed in the message view when the application is compiled because pdef1 is actually defined. The message pdef1 not defined is displayed when pdef1 is not defined.
The operator causes the expression to be given the value TRUE when a data type is declared with the identifier <identifier>. Otherwise FALSE is returned.
Requirement: The two applications App1 and App2 exist. The 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}
The operator causes the expression to be given the value TRUE when one of the following objects with name <pou-name> exists:
-
Function block
-
Function
-
Program
-
Action
-
Method
-
Interface
Otherwise FALSE is returned.
Requirement: The two applications App1 and App2 exist. The POU 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}
|
Important |
|---|---|
|
Not implemented yet |
The operator causes the expression to be given the value TRUE if a resource object named <identifier> exists for the application; otherwise FALSE is returned.
Requirement: The two applications App1 and App2 exist. 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}
The operator causes the expression to be given the value TRUE when a task is defined with the name <task name>. Otherwise FALSE is returned.
Syntax
{ IF defined (task: <task name> }
{ ELSIF defined (task: <task name> }
Example 294. Example
Requirement: The two applications App1 and App2 exist. The 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}
The operator causes the expression to be given the value FALSE when the CPU memory is organized in Big Endian (Motorola byte order).
The operator causes the expression to be given the value TRUE when the application runs on a simulated device (in simulation mode).
For more information, see: Testing in Simulation Mode
If the expression returns the value TRUE, then the code generator produces an FPU code (for the floating-point unit processor) when calculating with REAL values. Otherwise the FPU operations are emulated, which is much slower.
The operator causes the expression to be given the value TRUE when the attribute <attribute> is specified in the first line of the declaration part of the function block <pou name>. Otherwise FALSE is returned.
Example 295. Example
Requirement: The two applications App1 and App2 exist. The function fun1 is declared in App1 and App2. However, in App1 it is also provided with the pragma {attribute 'vision'}.
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 statement:
{IF hasattribute (pou: fun1, 'vision')}
(* the following code is only processed in App1 *)
ergvar := fun1(ivar);
{END_IF}
The operator causes the expression to be given the value TRUE when the pragma {attribute '<attribute>'} is assigned to the variable in the line before the variable declaration. Otherwise FALSE is returned.
Example 296. example
Requirement: The two applications App1 and App2 exist. The variable g_globalInt is used in App1 and App2, but in App1 the attribute 'DoCount' is also assigned to it.
Declaration of g_GlobalInt in App1
VAR_GLOBAL
{attribute 'DoCount'}
g_globalInt : INT;
g_multiType : STRING;
END_VAR
Declaration g_GlobalInt in App2:
VAR_GLOBAL
g_globalInt : INT;
g_multiType : STRING;
END_VAR
Pragma statement:
{IF hasattribute (variable: g_globalInt, 'DoCount')}
(* the following code is only processed in App1 *)
g_globalInt := g_globalInt + 1;
{END_IF}
The operator checks whether or not the constant, which is identified with <constant name>, has been replaced. The second parameter (Boolean value) controls what is checked:
-
TRUE: Checks if the constant has been replaced -
FALSE: Checks if the constant has not been replaced
When the respective case occurs, the operator returns TRUE.
Syntax
{ IF hasconstanttype( <constant namne> , <boolean literal> ) }
{ ELSIF hasconstanttype( <constant namne> , <boolean literal> ) }
The automatic replacement of constants in principle depends on the following:
-
Compile option Replace constants
-
Constant type (For example,
STRINGtypes are never replaced.) -
Usage of the attribute
{attribute 'const_non_replaced'} -
Usage of the attribute
{attribute 'const_replaced'}
Example 298. example
VAR
iCntMAXIsReplaced: INT;
xErrorOccured : BOOL;
END_VAR
VAR CONSTANT
c_iMAX: INT := 99;
END_VAR
{IF hasconstanttype(c_iMAX, TRUE)}
iCntMAXIsReplaced := iCntMAXIsReplaced + 1;
{ELSE}
xErrorOccured := FALSE;
{END_IF}
The operator compares the value of the constant, which is identified with <constant name>, with the value of the second parameter. The second parameter can be specified either as a literal <literal> or as a variable <variable name>.
Comparison operators <comparison operator>:
-
Greater than (
>) -
Greater than or equal to (
>=) -
Equal to (
=) -
Not equal to (
<>) -
Less than or equal to (
<=) -
Less than (
<)
Syntax
{ IF hasconstantvalue( <constant name> , <variable name> , <comparison operator> )
{ IF hasconstantvalue( <constant name> , <literal> , <comparison operator> )
{ ELSIF hasconstantvalue( <constant name> , <variable name> , <comparison operator> )
{ ELSIF hasconstantvalue( <constant name> , <literal> , <comparison operator> )
Example 299. Example
{IF hasconstantvalue(PLC_PRG.aConst, 99, >)}
{ELSIF hasconstantvalue(PLC_PRG.aConst, GVL.intconst99, =)}
Example 300. Example
PROGRAM PRG_ConditionConstantValue
VAR
iCntMAX: INT;
iCntGlobalMAX : INT;
iCntABC: INT;
iCntGlobalABC : INT;
xErrorOccured : BOOL;
END_VAR
VAR CONSTANT
c_iMAX: INT := 999;
c_sABC: STRING := 'ABC';
{attribute 'const_non_replaced'}
c_iNonReplaceable: INT := 888;
END_VAR
{IF hasconstantvalue(c_iMAX, 999, =)}
iCntMAX := iCntMAX + 1;
{ELSE}
xErrorOccured := FALSE;
{END_IF}
{IF hasconstantvalue(c_iMAX, GVL.gc_iMAX, =)}
iCntGlobalMAX := iCntGlobalMAX + 1;
{ELSE}
xErrorOccured := FALSE;
{END_IF}
{IF hasconstantvalue(c_sABC, 'ABC', =)}
iCntABC := iCntMAX + 1;
{ELSE}
xErrorOccured := FALSE;
{END_IF}
{IF hasconstantvalue(c_sABC, GVL.gc_sABC, =)}
iCntGlobalABC := iCntMAX + 1;
{ELSE}
xErrorOccured := FALSE;
{END_IF}
The operator causes the expression to be given the value TRUE when the variable <variable> is of the data type <type-spec>. Otherwise FALSE is returned.
Possible data types:
BOOL | BYTE | DATE | DATE_AND_TIME | DT | DINT | DWORD | INT | LDATE | LDATE_AND_TIME | LDT | LINT | LREAL | LTIME | LTIME_OF_DAY | LTOD | LWORD | REAL | SINT | STRING | TIME | TIME_OF_DAY | TOD | ULINT | UDINT | UINT | USINT | WORD | WSTRING
Example 301. Example
Requirement: The two applications App1 and App2 exist. The 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}
The checked pack mode depends on the device description, not on the pragma that can be specified for individual DUTs.
<register size>: Size of a CPU register (in bits)
The operator causes the expression to return the value TRUE when the size of a CPU register is equal to <register size>.
Possible values for <register size>
-
16for C16x, -
64for X86-64 bit -
32for X86-32 bit
The operator causes the expression to be given the value TRUE when a variable is defined with the identifier <define-ident> and has the value <char-string>. Otherwise FALSE is returned.
Requirement: The two applications App1 and App2 exist. The variable test is used in the applications App1 and App2. In App1, it is given the value 1. In App2, it is given 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}
This operator is available as of CODESYS V3.5 SP20.
The operator causes the expression to receive the value TRUE if the global define <global define> is included in the list of global defines in the Compile options of the project settings.
The operator is specified as a condition of an IF statement and can be used both in the implementation part and in the declaration part of ST POUs and libraries. They can be combined with other defines by means of the AND and OR operators if these are allowed at the respective position.
In the declaration part of POUs, IF statements with the operator project_defined (<global define>) can contain the following constructs:
-
Variable Declarations
Example:
{If project_defined(define1)} x : DINT; {END_IF} -
Comments
-
Attribute declarations
-
Pragma statements
The following constructs are not possible:
-
Full variable declarations
Example
{If project_defined(define1)} VAR x : DINT; END_VAR {END_IF} -
Full declarations from POUs
Example:
{If project_defined(define1)} FUNCTION POU : BOOL VAR_INPUT x : INT; END_VAR {END_IF} -
Scopes:
VAR_INPUT,VAR_OUTPUT,VAR_IN_OUT
The expression is given the value TRUE when the reverse value of <operator> returns the value TRUE. <operator> can be one of the operators described in this chapter.
Example 302. example
Requirement: The two applications App1 and App2 exist. PLC_PRG1 exists in App1 and App2, and the 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}
The expression is given the value TRUE when the two specified operators return TRUE. <operator> can be one of the operators described in this chapter.
Requirement: The applications App1 and App2 exist. PLC_PRG1 exists in App1 and App2, and the POU CheckBounds exists only in App1.
{IF defined (pou: PLC_PRG1) AND (defined (pou: CheckBounds))}
(* the following code is only processed in App1 *)
bANDTest := TRUE;
{END_IF}
The expression returns TRUE when one of the two specified operators returns TRUE. <operator> can be one of the operators described here.
Example 303. example
Requirement: The two applications App1 and App2 exist. The POU PLC_PRG1 exists in App1 and App2, and the POU CheckBounds exists 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}