Join us today!

Notifications
Clear all

[Solved] How to count the number of items in a struct in TwinCAT?

15 Posts
8 Users
12 Reactions
791 Views
IOTea
Posts: 28
Topic starter
(@iotea)
Eminent Member
Joined: 2 years ago

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! 

 

Reply
14 Replies
6 Replies
twinControls
Admin
(@twincontrols)
Joined: 2 years ago

Member
Posts: 114

@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! 

Counting the elements in STRUCT

Reply
IOTea
(@iotea)
Joined: 2 years ago

Eminent Member
Posts: 28

@twincontrols Looks quite interesting! Thank you for the tutorial!

Reply
benhar-dev
(@benhar-dev)
Joined: 2 years ago

Eminent Member
Posts: 17

@iotea yep this is possible to do. 

Screenshot

Source code of this can be found here... https://github.com/benhar-dev/tc3-count-structure-elements

Reply
IOTea
(@iotea)
Joined: 2 years ago

Eminent Member
Posts: 28

@benhar-dev This is impressive! Exactly what I needed! Thank you so much!! 🤗

Reply
(@joris)
Joined: 2 years ago

Eminent Member
Posts: 17

@iotea they are great answers. My question is : why do you want to know number of elements?

Reply
IOTea
(@iotea)
Joined: 2 years ago

Eminent Member
Posts: 28

@joris I need it for the array and xml implementations in our project.

Reply
kolyur
Posts: 12
(@kolyur)
Active Member
Joined: 2 years ago

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=

Reply
2 Replies
twinControls
Admin
(@twincontrols)
Joined: 2 years ago

Member
Posts: 114

@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: 

 

Reply
IOTea
(@iotea)
Joined: 2 years ago

Eminent Member
Posts: 28

@kolyur Thank you! I also thought about doing this. However, not all the datatypes are unknown in the struct I want to use.

Reply
rruiter
Posts: 63
(@rruiter)
Estimable Member
Joined: 2 years ago

There was already a StackOverflow question asking the same question. I linked to the two answers from here.

Reply
Posts: 1
(@neals)
New Member
Joined: 2 years ago

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

Reply
1 Reply
IOTea
(@iotea)
Joined: 2 years ago

Eminent Member
Posts: 28

@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. 

structTest

 

Have you achieved counting the subitems using the 'PLC_ReadSymInfoByNameEx' function block? 

Reply
raoliveira
Posts: 27
(@raoliveira)
Eminent Member
Joined: 2 years ago

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

 

https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/2529746059.html&id=3686945105176987925

Reply
1 Reply
IOTea
(@iotea)
Joined: 2 years ago

Eminent Member
Posts: 28

@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

Reply
Share: