我编写了一个自定义记录器,并希望添加 POU 的名称pszComponent
以便于调试。
我编写了一个自定义FB_init
,它接受 POU 的名称作为字符串作为输入,但遇到了令人费解的行为(至少对我而言)。
有人可以向我解释为什么以下编译
VAR
name : STRING := __POUNAME();
test : FB_logAdd(sCmpName:=name);
END_VAR
但以下
VAR
test : FB_logAdd(sCmpName:=__POUNAME());
END_VAR
引发以下编译器错误?
[ERROR] Internal error:System.NullReferenceException: Object reference not set to an instance of an object. at _3S.CoDeSys.LanguageModelManager.CompileContext.NeedsExternalFunctionCall(IOperatorExpression op, ICompiledType type, String& stFunctionName, TypeClass& tcWithType) at _3S.CoDeSys.Compiler35160.ExpressionTypifier.(_IOperatorExpression ) at _3S.CoDeSys.LanguageModelManager.OperatorExpression.Accept(IExprementVisitor visitor) at _3S.CoDeSys.Compiler35160.InterfaceCompiler.(_IVariable , _ISignature , ISignature , ISignature , IScope5 , _ICompileContext , IList`1 , IVariable[] , ExpressionTypifier , TypeChecker , ErrorVisitor ) at _3S.CoDeSys.Compiler35160.InterfaceCompiler.(IList`1& , IVariable[] , _IVariable , IScope5 , _ICompileContext , _ISignature , ISignature , ISignature , ExpressionTypifier , TypeChecker , ErrorVisitor ) at _3S.CoDeSys.Compiler35160.InterfaceCompiler.(_IVariable , IScope5 , _ICompileContext , _ISignature , ISignature , ISignature , ExpressionTypifier , TypeChecker , ErrorVisitor ) at _3S.CoDeSys.Compiler35160.InterfaceCompiler.(_IVariable , IScope5 , _ICompileContext , _ISignature , ExpressionTypifier , TypeChecker , ErrorVisitor , ISignature , ISignature ) at _3S.CoDeSys.Compiler35160.InterfaceCompiler.(_IVariable , IScope5 , _ICompileContext , _ISignature ) at _3S.CoDeSys.Compiler35160.InterfaceCompiler.(_ISignature , IScope5 , _ICompileContext ) at _3S.CoDeSys.Compiler35160.Compiler.(_ICompileContext , _IPreCompileContext , _IPreCompileContext , _ICompileContext , Boolean , Boolean , Boolean& , IProgressCallback ) at _3S.CoDeSys.Compiler35160.Compiler.(_ICompileContext , Boolean , Boolean , _IPreCompileContext , _IPreCompileContext , _ICompileContext , Boolean , Boolean& , IProgressCallback ) at _3S.CoDeSys.Compiler35160.Compiler.(Guid , Boolean , Boolean , Boolean , Boolean& , _ICompileContext& , _ICompileContext& , IProgressCallback , Boolean , Boolean , Boolean )
是因为它__POUNAME()
的行为像一个功能块而不是我假设的功能吗?
这就是为什么
VAR
test : FB_logAdd(pCmpName:=ADR(__POUNAME()));
END_VAR
如果我将输入更改为字符串指针也可以吗?
编辑 1
我最初的FB_init
样子是这样的
//FB_Init is always available implicitly and it is used primarily for initialization.
//The return value is not evaluated. For a specific influence, you can also declare the
//methods explicitly and provide additional code there with the standard initialization
//code. You can evaluate the return value.
METHOD FB_Init: BOOL
VAR_INPUT
bInitRetains: BOOL; // TRUE: the retain variables are initialized (reset warm / reset cold)
bInCopyCode: BOOL; // TRUE: the instance will be copied to the copy code afterward (online change)
sCmpName : STRING;
END_VAR
THIS^.sCmpName := sCmpName;
因为我想用FB_logAdd(sCmpName:=__POUNAME())
(仍然抛出上述错误)初始化功能块
如果我使用FB_logAdd(sCmpName:=THIS^.___POUNAME())
,它会引发编译器错误
C0004: '__POUNAME' is no component of 'THIS^'
这是有道理的,因为__POUNAME()
它是一个运算符,实际上并不属于父功能块(我认为)