CodeSys var_in_out 接口,然后传递实现该接口的 FB
我正在尝试创建一个 FB,它可以完成可以在许多最终 FB 中实现的接口上的工作。
INTERFACE IBaseInterface
FUNCTION_BLOCK Base IMPLEMENTS IBaseInterface
FUNCTION_BLOCK SYSTEM // the function block that will do the work
METHOD GetMax : REAL
VAR_IN_OUT
arrData : ARRAY[*] OF IBaseInterface;
END_VAR
//then in PLC_PRG
PROGRAM PLC_PRG
VAR
BaseArray : ARRAY [1..50] OF Base;
result: REAL;
boolResult : BOOL;
System : SYSTEM;
END_VAR
result := System.GetMax(BaseArray);
我收到一条错误,指出 Base 无法转换为 IBaseInterface。有人可以让我知道我做错了什么吗?如何对接口进行编程,然后传递实现该接口的最终 FB 的动态数组?
I'm trying to create an FB which can complete work on an interface that can be implemented in many FINAL FBs.
INTERFACE IBaseInterface
FUNCTION_BLOCK Base IMPLEMENTS IBaseInterface
FUNCTION_BLOCK SYSTEM // the function block that will do the work
METHOD GetMax : REAL
VAR_IN_OUT
arrData : ARRAY[*] OF IBaseInterface;
END_VAR
//then in PLC_PRG
PROGRAM PLC_PRG
VAR
BaseArray : ARRAY [1..50] OF Base;
result: REAL;
boolResult : BOOL;
System : SYSTEM;
END_VAR
result := System.GetMax(BaseArray);
I'm getting an error that Base can't be converted to IBaseInterface. Can someone let me know what I'm doing wrong? how can I program to an interface, and then pass a dynamic array of a final FB that implements the interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 codesys 中,您可以将 FunctionBlock 视为具有方法(函数)指针的结构。但是,"CODESYS 始终处理使用以下类型声明的变量接口的引用作为[功能块]的引用。”,因此,codesys 中的 FB 数组不能静态映射到引用(接口)数组。其他语言也将对象视为引用,或者自动为您进行转换。不幸的是,在 codesys 中,您必须手动执行此操作:
或者:
In codesys, you can think of a FunctionBlock as a Structure with Method (function) Pointers. However, "CODESYS always treats variables declared with the type of an interface as references [of the Function Block].", as such, an array of FB in codesys can't statically be mapped to an array of references (interfaces). Other languages either also treat objects as references, or do the conversion automatically for you. Unfortunately afaik, in codesys you'll have to do that manually:
Or: