CodeSys var_in_out 接口,然后传递实现该接口的 FB

发布于 2025-01-11 03:19:30 字数 577 浏览 0 评论 0原文

我正在尝试创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性 2025-01-18 03:19:30

在 codesys 中,您可以将 FunctionBlock 视为具有方法(函数)指针的结构。但是,"CODESYS 始终处理使用以下类型声明的变量接口的引用作为[功能块]的引用。”,因此,codesys 中的 FB 数组不能静态映射到引用(接口)数组。其他语言也将对象视为引用,或者自动为您进行转换。不幸的是,在 codesys 中,您必须手动执行此操作:

PROGRAM PLC_PRG
VAR
    BaseArray : ARRAY [1..50] OF Base;
    InterArray : ARRAY [1..50] OF IBaseInterface := [
        BaseArray[1],
        BaseArray[2],
        BaseArray[3],
        BaseArray[4],
        ...
        BaseArray[50],
    ];
END_VAR

result := System.GetMax(InterArray);

或者:

PROGRAM PLC_PRG
VAR
    BaseArray : ARRAY [1..50] OF Base;
    InterArray : ARRAY [1..50] OF IBaseInterface;
    i: UDINT;
END_VAR

FOR i := 1 TO 50 DO
    InterArray[i] := BaseArray[i];
END_FOR
result := System.GetMax(InterArray);

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:

PROGRAM PLC_PRG
VAR
    BaseArray : ARRAY [1..50] OF Base;
    InterArray : ARRAY [1..50] OF IBaseInterface := [
        BaseArray[1],
        BaseArray[2],
        BaseArray[3],
        BaseArray[4],
        ...
        BaseArray[50],
    ];
END_VAR

result := System.GetMax(InterArray);

Or:

PROGRAM PLC_PRG
VAR
    BaseArray : ARRAY [1..50] OF Base;
    InterArray : ARRAY [1..50] OF IBaseInterface;
    i: UDINT;
END_VAR

FOR i := 1 TO 50 DO
    InterArray[i] := BaseArray[i];
END_FOR
result := System.GetMax(InterArray);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文