CODESYS - the IEC 61131-3 automation software
https://forum.codesys.com/

Check input status with loop
https://forum.codesys.com/viewtopic.php?f=2&t=8866
Page 1 of 1

Author:  Justelis [ Thu May 24, 2018 9:05 am ]
Post subject:  Check input status with loop

Hello everybody,

I would like to create a loop in codesys which would check DI status and then activate DO. For this experiment I'm using wago 750-831 controller and wago 750-330 bacnet coupler.

In my mind it should look like this (I'm puting this example just to show what I have in mind):

VAR

Input_1,Input_2, Input_3;
Output_1, Output_2, Output_3;
i:INT;

END VAR

FOR i:=1 TO 3 BY 1
DO
If Input_i = TRUE THEN;
Output_i = TRUE;
ELSE
Output_i:=FALSE;
END_IF
END_FOR

Basicly what I though I could do is to create string variable with CONCAT (varname:=CONCAT('Input_',INT_TO_STRING(i)) and then put it into IF but not as string but as variable name somehow. I couldn't find function or function block that would do that. Is it even posible?
I of course could do the whole page of IF and ELSE IF but I would like to use more "beutiful" solution than this. Keep in mind that these variables is phisical DI DO signals and Bacnet variables.

Author:  Comingback4u [ Fri May 25, 2018 6:08 pm ]
Post subject:  Re: Check input status with loop

Could you just use an array?
Code:
VAR
    aInput : ARRAY[1..IOMAXSIZE] OF BOOL;
    aOutput : ARRAY[1..IOMAXSIZE] OF BOOL;
    iCount : INT;
END_VAR
VAR_CONSTANT
    IOMAXSIZE : INT := 3;
END_VAR

FOR iCount := 1 TO IOMAXSIZE BY 1 DO
    aOutput[iCount] := aInput[iCount];
END_FOR

Author:  teichhei [ Sat May 26, 2018 10:08 am ]
Post subject:  Re: Check input status with loop

I think I know what you mean but I'm not aware of a way to manipulate variable names dynamically.
I have applications like a number of compactors where the IOs are called something like Compactor1_xExtLimit but I have to hand over the IO to a function block call at some point.
An array won't help either because then you will have to assign the array element as the IO variable. Same Same but different, same amount of work.

Sent from my SM-G935F using Tapatalk

Author:  scott_cunningham [ Mon May 28, 2018 5:49 am ]
Post subject:  Re: Check input status with loop

Maybe use REFERENCE TO BOOL like this:

RefDigIn:ARRAY(0..15) OF REFERENCE TO BOOL;
RefDigOut:ARRAY(0..15) OF REFERENCE TO BOOL;

Later, explicitly map you speaking variable and the ref array...

RefDigIn[0] := Input_0;
RefDigIn[1] := Input_1;

RefDigOut[0] := Output_0;
RefDigOut[1] := Output_1;

Then you can do you loop:

FOR I:= 0 TO 15 DO
RefDigOut[I] := RefDigIn[I];
END_FOR

It’s a little work, but if you really need to “roll through” the VARs, this can help.

Author:  teichhei [ Mon May 28, 2018 7:26 am ]
Post subject:  Re: Check input status with loop

Scott but then he may simply map the variable straight into for example an array of bool or if it's more complex an array of struct. The work of allocating each input remains the same.

Sent from my SM-G935F using Tapatalk

Author:  scott_cunningham [ Mon May 28, 2018 11:59 am ]
Post subject:  Re: Check input status with loop

You are stuck with the amount of work. CoDeSys doesn’t offer building a variable name dynamically except by array. I usually use generic arrays for the EtherCAT IO mapping, use speaking variable names in my code and then have one program that maps all of the generic names with the software names. This means I have doubled my variables usage space and added some CPU time, but then I have one place to see or adjust my hardware IO mapping. Customers sometimes decide to change wires or maybe a IO is damaged... easy change in the code. Just requires extra setup work in the beginning.

Author:  Justelis [ Tue May 29, 2018 6:57 am ]
Post subject:  Re: Check input status with loop

All of your answers were great, I'll probably try using arrays, I hope that extra work in the beginning will help me in the future project adjustments :)

Page 1 of 1 All times are UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/