CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Sun Jan 19, 2020 11:41 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Thu Dec 05, 2019 5:19 pm 
Offline

Joined: Tue Apr 02, 2019 5:22 pm
Posts: 18
Hi everybody,
...me again with another question. I have several constant variables in a project (for instance... number_of_motors, number_of_sensors, ...) They are declared under VAR CONSTANT...END_VAR, once in the Main Prog and once gain under a Wrapper Function Block, naturally under VAR CONSTANT...END_VAR too.
In the Main Prog I have an instance of the Wrapper Function Block.
I am aiming to achieve certain modularity in my project, therefore if I need to increase/decrease the number_of_motors constant within the project, it isn't that great to have to adjust this value at two different places (once for the instanced object of the Wrapper Function Block in the Main Prog and once in the Wrapper variables declaration area itself).

I have tried to declare the constants as VAR_GLOBAL in the Main Prog, but they don't seem to be available for the Wrapper Function Block (by building the project again I get a lot of related errors)
How could I manage to declare constants only once in the project and made them truly globally available?

Thanks in advance for your help,
Rob


Top
   
PostPosted: Thu Dec 05, 2019 8:40 pm 
Offline

Joined: Tue Mar 26, 2019 4:28 pm
Posts: 73
Hi Rob! Right click your Application in the device tree and "add object > Global Variable List".
Give your Global Variable List an easy name to type like "GVL".
This is where you make all of your global variables.
Reference them in your code with GVL.ConstantName for example.

[Bonus level]
If you are against global variables for idealogical reasons, you can use a VAR INPUT CONSTANT in your FB and pass it the constant from where you call it in your main PRG.


Top
   
PostPosted: Fri Dec 06, 2019 9:47 am 
Offline

Joined: Tue Apr 02, 2019 5:22 pm
Posts: 18
I-Campbell wrote:
Hi Rob! Right click your Application in the device tree and "add object > Global Variable List".
Give your Global Variable List an easy name to type like "GVL".
This is where you make all of your global variables.
Reference them in your code with GVL.ConstantName for example.

[Bonus level]
If you are against global variables for idealogical reasons, you can use a VAR INPUT CONSTANT in your FB and pass it the constant from where you call it in your main PRG.


Hi I-Campbell, once again thanks for your tips! ;)
The first approach you described seemed unfortunately to not work out for me :( but I went for the second one and it "nearly" has worked out except for one issue...I let here a couple of screenshots where is to see what kind of error messages I've gotten by compiling...


You do not have the required permissions to view the files attached to this post.


Top
   
PostPosted: Fri Dec 06, 2019 9:58 am 
Offline

Joined: Tue Apr 02, 2019 5:22 pm
Posts: 18
(I have uploaded the project as well)


You do not have the required permissions to view the files attached to this post.


Top
   
PostPosted: Sat Dec 07, 2019 7:04 pm 
Offline

Joined: Tue Mar 26, 2019 4:28 pm
Posts: 73
Ah, yes my mistake. The compiler needs just one size for all instances of the functions, but the var input constant is only at the instance level.

You will have to get GVL working.


Top
   
PostPosted: Sun Dec 08, 2019 4:31 pm 
Offline

Joined: Tue Apr 02, 2019 5:22 pm
Posts: 18
...when attempting to use globally defined variables as array upper bound I get these messages...
Border 'GVL.CONSTANT' of array is no constant value
..is this assignment not allowed in Codesys ? :shock:


Top
   
PostPosted: Mon Dec 09, 2019 2:27 pm 
Offline
Frequent User
Frequent User

Joined: Fri Feb 23, 2018 3:41 pm
Posts: 176
No troubles with array bounds defined with constants here.
If you try to define an array bound with variable bound, then use "*".
see : Codesys online help


Top
   
PostPosted: Mon Dec 09, 2019 10:31 pm 
Offline

Joined: Tue Apr 02, 2019 5:22 pm
Posts: 18
...too complex for me for now...I will certainly come back to this topic in a future when my programming oop skills will ever improve. But thanks to all of you for your comments and hints!

In a function block seems to be easy:

FUNCTION POU : INT
VAR_INPUT
END_VAR
VAR
END_VAR
VAR_IN_OUT
MyArrayInPOU:ARRAY[1..GVL.TestVar] OF INT;
END_VAR

( But in the Main Prog I've tried out with no results )


Top
   
PostPosted: Tue Dec 10, 2019 1:23 pm 
Offline

Joined: Tue Mar 26, 2019 4:28 pm
Posts: 73
There ended up being too many snakes in this example, but it sure functions.
Attachment:
GVL.png


You do not have the required permissions to view the files attached to this post.


Top
   
PostPosted: Tue Dec 10, 2019 6:22 pm 
Offline

Joined: Tue Apr 02, 2019 5:22 pm
Posts: 18
oh! I was on the right track except I hadn't declared my constants in the GVL as VAR_GLOBAL CONSTANT but was VAR_GLOBAL ! :oops: :oops:
Once again: Thanks to all of you and to I-Campbell for this very self-explanatory example..
(Yesterday I went some sort of misguided by digging into other "advanced" (to me) topics as LOWER_BOUND / UPPER_BOUND or the * holder)


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 10 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 3 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited