条件 Pragmas

概述

ExST(扩展 ST)语言支持多个条件 Pragma 指令,这些指令会影响预编译或编译过程中的代码生成。

注: 不要在声明部分中使用任何条件 pragma。它们不会被考虑。

将被考虑进行编译的实现代码可能取决于以下条件:

  • 是否声明了特定数据类型或变量?

  • 某个类型或变量是否具有特定属性?

  • 某个变量是否具有特定数据类型?

  • 某个特定 POU 或任务是否可用,或者是否属于调用树,等等。

注:POU 树中声明的 POU 或 GVL 无法使用在应用程序中声明的 {define...}。应用程序中的定义将仅影响在各自应用程序下面插入的接口。

{define identifier string}

在预处理期间,如果令牌字符串不为空(允许这种情况并且已恰当定义),则标识符的所有后续实例将被替换为给定顺序的令牌。标识符仍为已定义并且在范围内,直至对象结束或在 {undefine} 指令中取消定义该标识符。用于条件编译

{undefine identifier}

identifier(通过 {define},请参阅下表第一行)的预处理器定义将被删除,因此标识符被取消定义。如果当前未定义指定的标识符,则将忽略该 pragma

{IF expr}

...

{ELSIF expr}

...

{ELSE}

...

{END_IF}

以下是用于条件编译的 pragmas。在编译时,要求指定表达式 exprs 为常量;将按照表达式的出现顺序对表达式进行求值,直至其中一个表达式求值为非零值。与成功指令相关联的文本将以正常方式进行预处理和编译;所有其他指令将被忽略。程序段的顺序是固定的;但是,elsifelse 程序段是可选的,并且 elsif 程序段可以出现任意次。

在常量 expr 中,您可以使用多个条件编译操作符

<expr>

在条件编译 pragma{if}{elsif})(请参阅上表)的常量表达式 expr 中,您可以使用多个操作符。无法通过 {undefine}{define} 来分别取消定义或定义这些操作符。

您还可以在对象的属性对话框中的编译器定义:文本字段中使用这些表达式以及 {define} 完成的表达式(视图 > 属性 > 编译)。

defined (identifier)

一旦使用 {define} 指令定义 identifier 且之后没有通过 {undefine} 指令取消定义,则此操作符将影响表达式获取值 TRUE。否则,此表达式的值为 FALSE。

defined (identifier) 示例

前提条件:有 2 个应用程序 App1App2。标识符 pdef1App2 中定义,但在 App1 中未定义。

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

此外,还包括了消息 pragma 的示例:

由于定义了 pdef1,因此在编译应用程序时,仅信息 pdef1 defined 将显示在消息视图中。当未定义 pdef1 时,将显示消息 pdef1 未定义

defined (variable:variable)

应用于变量时,如果在当前范围内声明了此特定变量,则此表达式的值为值为 TRUE。否则为 FALSE。

defined (variable:variable): 示例

前提条件:有 2 个应用程序 App1App2g_bTest 中声明了变量 App2,但 App1 中未声明。

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

defined (type:identifier)

应用于类型标识符时,如果声明了具有该特定名称的类型,则此表达式的值为 TRUE。否则为 FALSE。

defined (type:identifier) 示例:

前提条件:有 2 个应用程序 App1App2DUT 中定义了数据类型 App2,但 App1 中未定义。

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

defined (pou:pou-name)

应用于 POU 名称时,如果定义了具有该特定 POU 名称的 POU 或操作,则此表达式的值为 TRUE。否则为 FALSE。

defined (pou: pou-name) 示例:

前提条件:有 2 个应用程序 App1App2。POU CheckBoundsApp2 中可用,但是在 App1 中不可用。

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

hasattribute (pou: pou-name, attribute)

应用于 POU 时,如果在 POU 声明部分的第一行中指定了此特定 attribute,则此表达式的值为 TRUE。

hasattribute (pou: pou-name, attribute) 示例

前提条件:有 2 个应用程序 App1App2fun1App1 中定义了函数 App2,但是 App1 中具有属性 vision

fun1App1 中的定义:

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

fun1App2 中的定义:

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

Pragma 指令

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

hasattribute (variable: variable, attribute)

应用于 variable 时,如果通过 {attribute} 指令在声明变量之前的行中指定了此特定属性,则此表达式的值为 TRUE。

hasattribute (variable: variable, attribute) 示例:

前提条件:有 2 个应用程序 App1App2App1App2 中使用变量 g_globalInt,但是 App1 中具有属性 DoCount

g_globalIntApp1 中的声明

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

g_globalIntApp2 中的声明

VAR_GLOBAL
g_globalInt : INT;
g_multiType : STRING;
END_VAR

Pragma 指令

{IF hasattribute (variable: g_globalInt, 'DoCount')}
(* the following code line will only be processed in App1, because there variable g_globalInt has got the attribute 'DoCount' *)
g_globalInt := g_globalInt + 1;
{END_IF}

hastype (variable:variable, type-spec)

应用于 variable 时,如果此特定变量具有指定的 type-spec,则此表达式的值为 TRUE。否则为 FALSE。

type-spec 的可用数据类型

  • BOOL

  • BYTE

  • DATE

  • DATE_AND_TIME

  • DINT

  • DWORD

  • INT

  • LDATE

  • LDATE_AND_TIME (LDT)

  • LINT

  • LREAL

  • LTIME

  • LTIME_OF_DAY (LTOD)

  • LWORD

  • REAL

  • SINT

  • STRING

  • TIME

  • TIME_OF_DAY (TOD)

  • UDINT

  • UINT

  • ULINT

  • USINT

  • WORD

  • WSTRING

操作符 hastype (variable: variable, type-spec) 示例

前提条件:有 2 个应用程序 App1App2。变量 g_multitypeApp1 中声明的类型是 LREAL,在应用程序 App2 中声明的类型为 STRING

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

hasvalue (define-ident, char-string)

如果定义了 define (define-ident) 且其具有指定值 (char-string),则此表达式的值为 TRUE。否则为 FALSE。

hasvalue (define-ident, char-string) 示例:

前提条件:变量 test 用在应用程序 App1App2 中。此表达式在 App1 中获取值 1,在 App2 中获取值 2:

{IF hasvalue(test,'1')}
(* the following code line will be processed in App1, because there variable test has value 1 *)
x := x + 1;
{ELSIF hasvalue(test,'2')}
(* the following code line will be processed in App1, because there variable test has value 2 *)
x := x + 2;
{END_IF}

hasconstantvalue(<variable>, <literal expression>)

使用此操作符请求常量的声明值。

示例

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

NOT operator

operator 的反向值为 TRUE 时,此表达式将获取值 TRUE。operator 可以是本章中描述的操作符之一。

NOT operator 示例

前提条件:有 2 个应用程序 App1App2。POU PLC_PRG1 用在 App1App2 中。POU CheckBounds 仅用在 App1 中。

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

AND operator

如果两个操作符均为 TRUE,则表达式获取值 TRUE。operator 可以是此表中列出的操作符之一。

AND operator 示例:

前提条件:有 2 个应用程序 App1App2。POU PLC_PRG1 用在应用程序 App1App2 中。POU CheckBounds 仅用在 App1 中。

{IF defined (pou: PLC_PRG1) AND (defined (pou: CheckBounds))}
(* the following code line will be processed only in applications App1, because only there "PLC_PRG1" and "CheckBounds" are defined *)
bORTest := TRUE;
{END_IF}

OR operator

如果其中一个操作符为 TRUE,则此表达式为 TRUE。operator 可以是本章中描述的操作符之一。

OR operator 示例:

前提条件:POU PLC_PRG1 用在应用程序 App1App2 中。POU CheckBounds 仅用在 App1 中。

{IF defined (pou: PLC_PRG1) OR (defined (pou: CheckBounds))}
(* the following code line will be processed in applications App1 and App2, because both contain at least one of the POUs "PLC_PRG1" and "CheckBounds" *)
bORTest := TRUE;
{END_IF}

(operator)

(operator) 括起操作符。