Join us today!
[Solved] How to count the number of items in a struct in TwinCAT?
Hello community,
Does anybody know how to get the number of elements in a structure? Is this possible in TwinCAT?
TYPE ST_Employee : STRUCT sName: STRING; nEmployeeID : INT; nAge : INT; END_STRUCT END_TYPE
For example, I want to have a function block like this :
fbCountStructElements(st := stEmployee, nElementsCount=> nStEmployeeElementCount );
nStEmployeeElementCount will be 3 after running this function block.
Thank you so much in advance!
@iotea Actually, I don't really know how this can be done using TwinCAT 3 only, but this question got me thinking and I used python to count the elements in the struct file and just published this tutorial. Hope it helps!
In case you want to say thank you !)
We'd be very grateful if you could share this community with your colleagues and friends. You can also buy us a coffee to keep us fueled 😊 This is the best way to say thank you to this project and support your community.
twinControls - https://twincontrols.com/
@iotea yep this is possible to do.
Source code of this can be found here... https://github.com/benhar-dev/tc3-count-structure-elements
My blog: codingbytes
My code: https://github.com/benhar-dev
I think the only way this could be done is if the structure has a known format, such as a repeating number of known datatypes. In this case you could use __VARINFO to get the total bit size of the structure, then do the math. (Might be necessary to change pack_mode in struct.) If there is any way to get further information programmatically, I'd also be interested to know!
https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/3527777675.html&id=
https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/2529746059.html&id=
@kolyur Great suggestion! Since this method wouldn't work for different datatypes, I used python to count the lines between STRUCT and END_STRUCT keywords.
Here's the tutorial how I did it:
In case you want to say thank you !)
We'd be very grateful if you could share this community with your colleagues and friends. You can also buy us a coffee to keep us fueled 😊 This is the best way to say thank you to this project and support your community.
twinControls - https://twincontrols.com/
@kolyur Thank you! I also thought about doing this. However, not all the datatypes are unknown in the struct I want to use.
There was already a StackOverflow question asking the same question. I linked to the two answers from here.
My blog: https://cookncode.com/twincat
My code: https://github.com/roald87
You could use ADS to read the Symbol Information of that Variable and count the subitems.
See...
- "PLC_ReadSymInfoByNameEx": https://infosys.beckhoff.com/content/1033/tcplclib_tc2_utilities/35051659.html
- ADS over C++ to read variable declaration: https://infosys.beckhoff.com/content/1033/tc3_adsdll2/124832011.html
- ADS over C# to read variable declaration: https://infosys.beckhoff.com/content/1033/tc3_adssamples_net/185253899.html
@neals Thank you for the suggestions! How would you do it using the first method ? It doesn't show the subitems, but it shows the byteSize.
Have you achieved counting the subitems using the 'PLC_ReadSymInfoByNameEx' function block?
Quick tip if it helps:
{attribute 'pack_mode' := '1'}
TYPE ST_MyStruct:
STRUCT
bEnable : BOOL;
nCounter : INT;
nMaxSize : INT;
bMaxSizeReached : BOOL;
bReset : BOOL;
END_STRUCT
END_TYPE
then if you use SIZEOF(ST_MyStruct) it will give you the size/number of items you have in your structure
@raoliveira Thank you! It will give you the size of the struct, but without knowing the datatypes in the struct, you can't calculate how many items there are 🙂 @benhar-dev's solution works great! https://github.com/benhar-dev/tc3-count-structure-elements
- 17 Forums
- 267 Topics
- 942 Posts
- 0 Online
- 722 Members