我正在尝试构建一个函数,该函数总和twincat中任何类型的单个维数阵列。
我对指针的经验不足,所以答案可能很明显,但是我找不到任何解决方案。我阅读了所有这些,它们有所帮助。
My conceptual code works for an INT数组,但我意识到我必须为每个数据类型编写一个循环以使其正常工作,这似乎是错误的。 我还想避免在循环中遇到一个案例,因为这似乎效率低下。
是否可以将指针(未设置类型)指向(特定类型的指针)?在C中看起来可以,但不确定IEC-61131。
FUNCTION fn_SumArray : DINT
VAR_INPUT
inArr : ANY;
inElem : ANY;
END_VAR
VAR
i : DINT := 0;
stepsize : DINT :=1;
pLREAL : POINTER TO LREAL; //64 bit
pREAL : POINTER TO REAL; //32 bit
pBYTE : POINTER TO BYTE; //8 bit
pWORD : POINTER TO WORD; //16 bit
pDWORD : POINTER TO DWORD; //32 bit
pLWORD : POINTER TO LWORD; //64 bit
pSINT : POINTER TO SINT; //8 bit
pUSINT : POINTER TO USINT; //8 bit
pINT : POINTER TO INT; //16 bit
pUINT : POINTER TO UINT; //16 bit
pDINT : POINTER TO DINT; //32 bit
pUDINT : POINTER TO UDINT; //32 bit
pLINT : POINTER TO LINT; //64 bit
pULINT : POINTER TO ULINT; //64 bit
END_VAR
--------------------------------------------------
CASE inElem.TypeClass OF
__SYSTEM.TYPE_CLASS.TYPE_LREAL:
stepsize := 8;
//set generic pointer here
__SYSTEM.TYPE_CLASS.TYPE_INT:
stepsize := 2;
//set generic pointer here
ELSE
fn_SumArray :=0;
END_CASE;
FOR i := 0 TO inArr.diSize-1 BY stepsize DO
genericPointer:= ADR(inarr.pValue[i]);
fn_SumArray := fn_SumArray +genericPointer^;
END_FOR;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,您必须重复要涵盖的所有类型的代码。 codesys没有 true 通用编程,因此是:
rel:
如果为每种类型编写相同的代码可以始终使用脚本来自动化这一点:
只需使用Python 3.x运行上面的脚本,您就会获得脚本中列出的类型的案例语句代码。
As far as I know, you'll have to repeat the code for all types you want to cover. CODESYS doesn't have true generic programming, so it is what it is:
The result of the program above:
If writing the same code for every type is too tedious, you can always use a script to automate this:
Just run the above script with python 3.x and you'll get the case statements code for the types listed in the script.
我最终要做的是一个案例声明,里面有循环。
不像我希望的那样优雅,但仍然具有功能。
What I ended up going with was a case statement, with for loops inside.
Not as elegant as I had hoped, but still functional.