admin管理员组文章数量:1026941
I am writing a program that loops through an array to check Boolean alarm variable statuses.
I want to dynamically generate the for-loop count integer to end the loop.
I have looked through Benchoff's Twincat 3 Documentation and can't find any generic SIZEOF()
functions to generate the array size, for example
nArraySize := SIZEOF(Array);
another thought is the Generate the Upper and lower bounds of the array to determine the size but I cannot find any functions to do this either.
I am writing a program that loops through an array to check Boolean alarm variable statuses.
I want to dynamically generate the for-loop count integer to end the loop.
I have looked through Benchoff's Twincat 3 Documentation and can't find any generic SIZEOF()
functions to generate the array size, for example
nArraySize := SIZEOF(Array);
another thought is the Generate the Upper and lower bounds of the array to determine the size but I cannot find any functions to do this either.
Share Improve this question edited Jan 5 at 16:13 asys 7317 silver badges29 bronze badges asked Nov 16, 2024 at 16:26 Alex ChalaAlex Chala 231 silver badge2 bronze badges 1- Please provide some context, at least how "Array" is declared. – Fred Commented Nov 16, 2024 at 16:45
1 Answer
Reset to default 2You can't really determine the size like that, the SIZEOF returns number of bytes the variable occupies. You are missing one step, divide the array size (byte count) with the base type, to determine how many slices of data you have:
nArraySize := SIZEOF(array)/SIZEOF(array[0]) - 1;
and then you can call the for loop as
for i := 0 to nArraySize by 1 do
myArray[i] ...
This of course assumes your array starts at 0.
Upper and lower bounds can only be used on variable sized arrays. One way you could do something is have the array defined somewhere in your program, and then have a function or a method, that has the variable array as IN_OUT
parameter, as required by the variable arrays:
myArray : ARRAY[5..10] of ST_MyDataType;
F_DoSomething
VAR_IN_OUT
refArray : ARRAY[*] of ST_MyDataType
END_VAR
VAR
nIteration
END_VAR
FOR nIteration := LOWER_BOUND(refArray,1) to UPPER_BOUND(refArray,1) by 1 do
if refArray[nIteration].Something = TRUE then
...
END_FOR
and then call the function as is:
F_DoSomething(myArray);
One last way you could work is with memory access and pointers, but that gets a bit more complicated then, and is unsafe if not taken extra care of addresses.
I am writing a program that loops through an array to check Boolean alarm variable statuses.
I want to dynamically generate the for-loop count integer to end the loop.
I have looked through Benchoff's Twincat 3 Documentation and can't find any generic SIZEOF()
functions to generate the array size, for example
nArraySize := SIZEOF(Array);
another thought is the Generate the Upper and lower bounds of the array to determine the size but I cannot find any functions to do this either.
I am writing a program that loops through an array to check Boolean alarm variable statuses.
I want to dynamically generate the for-loop count integer to end the loop.
I have looked through Benchoff's Twincat 3 Documentation and can't find any generic SIZEOF()
functions to generate the array size, for example
nArraySize := SIZEOF(Array);
another thought is the Generate the Upper and lower bounds of the array to determine the size but I cannot find any functions to do this either.
Share Improve this question edited Jan 5 at 16:13 asys 7317 silver badges29 bronze badges asked Nov 16, 2024 at 16:26 Alex ChalaAlex Chala 231 silver badge2 bronze badges 1- Please provide some context, at least how "Array" is declared. – Fred Commented Nov 16, 2024 at 16:45
1 Answer
Reset to default 2You can't really determine the size like that, the SIZEOF returns number of bytes the variable occupies. You are missing one step, divide the array size (byte count) with the base type, to determine how many slices of data you have:
nArraySize := SIZEOF(array)/SIZEOF(array[0]) - 1;
and then you can call the for loop as
for i := 0 to nArraySize by 1 do
myArray[i] ...
This of course assumes your array starts at 0.
Upper and lower bounds can only be used on variable sized arrays. One way you could do something is have the array defined somewhere in your program, and then have a function or a method, that has the variable array as IN_OUT
parameter, as required by the variable arrays:
myArray : ARRAY[5..10] of ST_MyDataType;
F_DoSomething
VAR_IN_OUT
refArray : ARRAY[*] of ST_MyDataType
END_VAR
VAR
nIteration
END_VAR
FOR nIteration := LOWER_BOUND(refArray,1) to UPPER_BOUND(refArray,1) by 1 do
if refArray[nIteration].Something = TRUE then
...
END_FOR
and then call the function as is:
F_DoSomething(myArray);
One last way you could work is with memory access and pointers, but that gets a bit more complicated then, and is unsafe if not taken extra care of addresses.
本文标签: twincatBeckhoff Twincat3 Structured text Dynamic Array size calculationStack Overflow
版权声明:本文标题:twincat - Beckhoff Twincat3 Structured text Dynamic Array size calculation - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745652655a2161410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论