.

运算符‘__QUERYINTERFACE’

.
.

该运算符是IEC 61131-3标准的扩展。

在runtime中,该运算符将接口引用的类型转换为另一种类型。该运算符返回一个BOOL的结果。TRUE表示CODESYS已成功执行转换。

__QUERYINTERFACE(<ITF_Source>,<ITF_Dest>);

操作数1:接口引用或FB接口

操作数2:具有所需目标类型的接口引用

显式转换的要求是ITF_Source和ITF_Dest都从接口Interface __System.IQueryInterface派生。该接口隐式可用,不需要库。

.

.例如

INTERFACE ItfBase EXTENDS __System.IQueryInterface 
METHOD mbase : BOOL 
END_METHOD 
  
INTERFACE ItfDerived1 EXTENDS ItfBase 
METHOD mderived1 : BOOL 
END_METHOD 

INTERFACE ItfDerived2 EXTENDS ItfBase 
METHOD mderived2 : BOOL 
END_METHOD 

FUNCTION_BLOCK FB1 IMPLEMENTS ItfDerived1
METHOD mbase : BOOL 
    mbase := TRUE;
END_METHOD 
METHOD mderived1 : BOOL 
    mderived1 := TRUE;
END_METHOD 
END_FUNCTION_BLOCK

FUNCTION_BLOCK FB2 IMPLEMENTS ItfDerived2
METHOD mbase : BOOL 
    mbase := FALSE;
END_METHOD 
METHOD mderived2 : BOOL 
    mderived2 := TRUE;
END_METHOD 
END_FUNCTION_BLOCK
  
PROGRAMM POU 
VAR 
    inst1 : FB1; 
    inst2 : FB2; 
    itfbase1 : ItfBase := inst1;
    itfbase2 : ItfBase := inst2;
    itfderived1 : ItfDerived1 := 0;
    itfderived2 : ItfDerived2 := 0;
    xResult1, xResult2, xResult3, xResult4: BOOL; 
END_VAR 


xResult1 := __QUERYINTERFACE(itfbase1, itfderived1); // xResult = TRUE, itfderivedi1 <>0
                                                     // 引用实例inst1
xResult2 := __QUERYINTERFACE(itfbase1, itfderived2); // xResult = FALSE, itfderived2 = 0
xResult3 := __QUERYINTERFACE(itfbase2, itfderived1); // xResult = FALSE, itfderived1 = 0
xResult4 := __QUERYINTERFACE(itfbase2, itfderived2); // xResult = TRUE, itfderived2 <> 0
                                                     // 引用实例inst2