plc.myforum.ro Forum Index plc.myforum.ro
PLCs users www.plc.myforum.ro!
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TwinCat dinamic arrays while running

 
Post new topic   Reply to topic    plc.myforum.ro Forum Index -> Other PLCs Reg. U
View previous topic :: View next topic  
Author Message
mojo
Senior
Senior


Joined: 01 Oct 2011
Posts: 2404

PostPosted: 17/11/2011 12:35    Post subject: TwinCat dinamic arrays while running Reply with quote

How can i Resize an array while my program is in running mode???
Back to top
View user's profile Send private message
rott
Senior
Senior


Joined: 23 Aug 2011
Posts: 2266

PostPosted: 17/11/2011 12:37    Post subject: Reply with quote

In the PLC environments(not only TwinCat) there isn't such possibility to have a dynamic memory declaration, the memory usage determine while compiling and kept fixed as long as the program is running.
The maximum that you can do is to declare a constant variable such - c_MaxArray:=10; and to declare the array as [1..c_MaxArray] so in the future you'll have to change the code only at one place-the value of the c_MaxArray.
The constant vars can't be changed while program is running!
Back to top
View user's profile Send private message
rumsfeld
Member 3
Member 3


Joined: 29 Oct 2011
Posts: 572

PostPosted: 17/11/2011 12:39    Post subject: Reply with quote

The Redim functionality isn't available in TwinCat. The closest thing you could get is something like this
1) You define a constant variable for the maximum range of your array e.g.

VAR_GLOBAL CONSTANT
MAX_TEST : INT := 20;
END_VAR

2)You define an array using this constant value
TestOutput : ARRAY[1..MAX_TEST] OF BOOL;

3)Inside the program you could use a part of the array
e.g.
CurrentSizeForThisInstallation : INT := 10;

FOR TestCounter:= 1 TO CurrentSizeForThisInstallation DO
TestOutput[TestCounter] := TRUE;
END_FOR

NextSizeForThisInstallation : INT := 10;
FOR TestCounter:= 1 TO NextSizeForThisInstallation DO
TestOutput[TestCounter] := FALSE;
END_FOR

As long as CurrentSizeForThisInstallation and NextSizeForThisInstallation don't go beyond the predefined MAX_TEST constant you're ok.
Back to top
View user's profile Send private message
mayo
Administrator
Administrator


Joined: 14 Jun 2009
Posts: 700

PostPosted: 17/11/2011 12:46    Post subject: Reply with quote

Maybe you can do something like this:

Code:

VAR_GLOBAL CONSTANT
MAX_SIZE : INT := 500000; (* Maximum data size *)
END_VAR

VAR_INPUT
event: INT; (* Event#, should be 1..maxEvents *)
channel: INT; (* Channel#, should be 1..maxChannels *)
group: INT; (* Event#, should be 1..maxGroups *)
END_VAR

VAR
arData : ARRAY[1..MAX_SIZE] OF INT; (* Array to hold the data *)
index: INT; (* Index in the data array *)

(* maxEvents * maxChannels * maxGroups <= MAX_SIZE *)
maxEvents: INT := 5000; (* Maximum number of events allowed *)
maxChannels: INT := 20; (* Maximum number of channels allowed *)
maxGroups: INT := 5; (* Maximum number of groups allowed *)
END_VAR

(* Calculate the index in the array from the event#, channel# and group# *)
index := (event - 1) * maxChannels * maxGroups + (channel - 1) * maxGroups + (group - 1);

(* Do something with the data *)
arData[index] := 66;

 


In that way you can tweak the data need by adjusting the maxEvents, maxChannels and/or maxGroups, even from the VB side.
The MAX_SIZE array can be smaller than a multi-dimensional array because, you now can allow for 10,000 events but fewer channels.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    plc.myforum.ro Forum Index -> Other PLCs Reg. U All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Fa-ti si tu propriul forum GRATUIT, pe www.myforum.ro