Defining Arrays in Codesys

darnbar

Member
Join Date
Feb 2015
Location
Australia
Posts
6
Hi guys,

I asked this question on the Codesys website and didn't have much success with it.

I'm looking at a way of defining arrays for real numbers.

An example of which is as follows

SampleArray : ARRAY[0..5] OF REAL :=[0.0,0.1,0.2,0.3,0.4,0.5];

That's fine for 6 values but I need to extend an array to over 1000 values is there a quicker method of assigning the values in an array without typing each values manually?

Thanks
 
Last edited:
Is there a pattern to how the array is populated?

If so, I would run a FOR loop on the first scan of the PLC that initializes it.

Code:
arrayVal := 0;
increment := 0.1;

FOR i := 0 TO 999 BY 1 DO
    arrayTag[i] := arrayVal;
    arrayVal := arrayVal + increment;
END_FOR;

I haven't watched this video, but Kurt's are usually good: https://www.youtube.com/watch?v=fFksQMdVbDI
 
Last edited:
send me the table, in any form.
if it is in excel it is easy export it as cdf, it will put, get the file with ctrl C and put it in with ctrl V
are you sure you need it in a array .
have a look at oscat function linear_int it has 10 places, however as it is open source you can change it yourself.
obvious as 1000 array of real is > 4000 bytes
 
i received this question via PM.

Hi Shooter,

We meet again :D!

You said in the forum to message you the code.I got help with the code and I'm using the following.

PROGRAM Tag_Arrays
VAR
PowerArray : ARRAY[1..5000] OF REAL;
i:INT:=0;
isInitialized:BOOL:=FALSE;


IF NOT isInitialized THEN
FOR i:=1 TO 5000 BY 1 DO
PowerArray:=i*0.1;

PowerArray:= INT_TO_REAL(i)/10.0;
END_FOR;
isInitialized:=TRUE;
END_IF

I have another query if you don't mind me asking.

So as you can see the array increases in increments of 0.1.

So we have

PowerArray [1] 0.1
PowerArray [2] 0.2
PowerArray [3] 0.3

and so on.

The RFID system I'm using is limited to a 16bit integer and the card is limited to values of 0-32767.

Unfortunately it can't be changed to another variable and can't even be changed to an Unsigned integer,what I'm using the array for is to store numbers with decimal points.

What I want to be able to do is reference the array.

If I have a tag with a value 0.3 I can't write that to the RFID tag.

So I need to be able reference the 'PowerArray'.

For example if I have a tag value of 0.3 and compare it to the array to get a value of 3 which relates to the original decimal value and write that value to the RFID tag.

I tried using an EQ block and compare in ST in codesys but it wont allow an array to be compared to an Real number.

Have you any suggestions of how to attempt this?

Thanks in advance
-darnbar
 
The solution is a lot better and smaller.
so you need a INT from a REAL.
the problem is a INT can have decimals, so you made already solution, but cant see it.
any number (positive or negative)
mulvalue:=anyvalue*10; that is easy.
if mulvalue >= 0.0 then sign="P" else sign="N";
intvalue := REAL_TO_INT(ABS(mulvalue));
 
your answer is also possible, however you have to compare between 2.5 and 3.5
as in if (value >= 2.05 AND value <2.15 )or any number from the array,
then you found the correct one, this is not practicle as it takes 10000 compares.
have a look at linear_int from oscat, that program does it on curved lines.
 
Looking at all of this I'm wondering if the array is really necessary, since it is (as best as I can figure) constant and linear (0.1, 0.2, ...).

Would a simple function work better?

The incoming REAL value is assigned to "realVal". The final value is "intVal". Obviously, this can be reduced to fewer lines of code (or even a single line). The "TRUNC" (truncate" command may not be necessary depending on if the "INT_TO_REAL" conversion rounds or truncates the REAL value.

Code:
newRealVal := realVal + 0.05;                /* takes care of rounding issues when truncated */
newRealValTimes10 := newRealVal * 10;
truncVal := TRUNC(newRealValTimes10);         /* change the scale of the number and remove the decimal portion */
intVal := INT_TO_REAL(truncVal);


Two sample executions:
realVal = 0.3
newRealVal = 0.35
newRealValTimes10 = 3.5
truncVal = 3.0
intVal = 3

realVal = 0.37
newRealVal = 0.42
newRealValTimes10 = 4.2
truncVal = 4.0
intVal = 4
 
My impression from the OP was that the incoming value is always going to be a positive value.

You are absolutely correct that my last line should have been REAL_TO_INT, not INT_TO_REAL as I posted.
 

Similar Topics

Non-PLC guys asking me to explain all the interlocks of my system in a document. Problem is for this system its a little complicated (OR...
Replies
16
Views
5,455
Hi Everyone, I have a somewhat silly question. I've primarily programmed with Beckhoff PLCs in the past. In those, we can assign the string TRUE...
Replies
8
Views
2,417
Hello all. I am working on a project where there are 7 vents which have a cooling function with a S7-1200 station and a KTP Basic panel (1st.gen)...
Replies
6
Views
2,588
hi to everyone, we are using ISaGRAF for programming our PLC. The problem is that we want to change the IO boards of the computer that we use...
Replies
0
Views
1,885
Hi I'm trying to define a string array using Citects SCADA package and I've got an error that I can't shift. I've defined a variable of the...
Replies
10
Views
13,127
Back
Top Bottom