Join us today!

Structuring ST Code...
 
Notifications
Clear all

Structuring ST Code in TwinCAT - Creating Regions

3 Posts
2 Users
2 Reactions
859 Views
twinControls
Posts: 114
Admin
Topic starter
(@twincontrols)
Member
Joined: 2 years ago

You can structure your Structured Text program to separate your block of your code. It would help you to identify what you have coded in that specific region and enable you to navigate easier. 

 

Method 1 using commenting: 

////////////////////////////////////////////////////////						 
//Initialize the values							  
	(*your code here*)
////////////////////////////////////////////////////////						 
							   
////////////////////////////////////////////////////////						 
//Start the machine							  
	(*your code here*)
////////////////////////////////////////////////////////

After collapsing the descriptions, the code looks like this: 

commentRegion

 

Method 2 using pragma: 

{region "Description"}
// code
{endregion}	

{region "Initialize the values"}
// your code here 												  
{endregion}
													   
{region "Start the machine"}
// your code here 														
{endregion}	

"region" pragma allows you to create regions in your code. 

regionPragmaInCodesys

 

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

Maybe this is a nice feature for auto generated code, I would not use it for normal code. If you see the need to use regions, then your methods are just too long/complicated.

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

Member
Posts: 114

@rruiter That's right! Ideally, your methods/code should be self explanatory that you don't even need to use comments. However, in reality that's not the case unfortunately.  I've seen many interesting PLC programs that I wish they had used at least regions in the code.

Reply
Share: