| View previous topic :: View next topic |
| Author |
Message |
mojo Senior

Joined: 01 Oct 2011 Posts: 2404
|
Posted: 17/11/2011 12:35 Post subject: TwinCat dinamic arrays while running |
|
|
| How can i Resize an array while my program is in running mode??? |
|
| Back to top |
|
 |
rott Senior

Joined: 23 Aug 2011 Posts: 2266
|
Posted: 17/11/2011 12:37 Post subject: |
|
|
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 |
|
 |
rumsfeld Member 3

Joined: 29 Oct 2011 Posts: 572
|
Posted: 17/11/2011 12:39 Post subject: |
|
|
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 |
|
 |
mayo Administrator

Joined: 14 Jun 2009 Posts: 700
|
Posted: 17/11/2011 12:46 Post subject: |
|
|
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 |
|
 |
|