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 Thu Jun 27, 2019 7:02 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Array questions
PostPosted: Tue Feb 20, 2018 4:07 pm 
Offline

Joined: Wed Feb 07, 2018 6:57 pm
Posts: 3
Dear all,
1.is it possible to declare an array, with a size taken from initializer.
In C it is:
int a[]={1,2,3}

How I do the same in codesys?
Please note, I DON'T request run-time dynamic array. I only request to don't declare a size abut take it from amount of initializers, like in C.

2. How array are passed in function blocks?

Var input
invar array [0..5] of int;
end var

What is passed only the "reference" or the copy of the whole array on the stack. If so, how do I only pass array by reference? I want to read from array inside of the block, but don't want to write something back.

Note: Pointers are not allowed in my company.
Thanks a lot!


Top
   
 Post subject: Re: Array questions
PostPosted: Thu Feb 22, 2018 3:43 am 
Offline
Frequent User
Frequent User

Joined: Fri Sep 02, 2011 8:02 pm
Posts: 302
An answer to your second question:

There are two solutions for passing an array reference without passing the whole array while not using pointers:

  • Use VAR_IN_OUT - this forces the inclusion of the variable on every call and uses by reference behind the scenes. The downside is if you really just want a var input, you could adjust the value inside the FB without problems and create bugs.
  • Use the keyword REFERENCE TO when you define your variable. It will pass the variable by reference and you can use it for VAR_INPUT, VAR_OUTPUT, and VAR. I put an example below. Be aware you should still check for validity of the reference in case it has never been set! Hint - if you only use REFERENCE TO in Method VAR_INPUTS, the programmer must assign it, so you don’t have to check validity!

Code:
VAR_INPUT
   History : REFERENCE TO ARRAY[0..99] OF DWORD;
END_VAR

_________________
Scott Cunningham
KEB America, Inc.
www.kebblog.com
www.kebamerica.com


Top
   
 Post subject: Re: Array questions
PostPosted: Fri Feb 23, 2018 10:47 am 
Offline

Joined: Wed Feb 07, 2018 6:57 pm
Posts: 3
Thanks a lot, Scott!
I would like ask Codesys (if they read) to implement Const References for that and also implement my 1. point in ST.


Top
   
 Post subject: Re: Array questions
PostPosted: Tue Apr 03, 2018 8:07 pm 
Offline

Joined: Tue Feb 07, 2017 11:03 pm
Posts: 5
EmbeddedSalsa wrote:
Note: Pointers are not allowed in my company.

Why??? :shock:


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

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 1 guest


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