Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Check input status with loop

  • Justelis

    Justelis - 2018-05-24

    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):


    Input_1,Input_2, Input_3;
    Output_1, Output_2, Output_3;


    FOR i:=1 TO 3 BY 1
    If Input_i = TRUE THEN;
    Output_i = TRUE;

    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.

  • Comingback4u

    Comingback4u - 2018-05-25

    Could you just use an array?

        aInput : ARRAY[1..IOMAXSIZE] OF BOOL;
        aOutput : ARRAY[1..IOMAXSIZE] OF BOOL;
        iCount : INT;
        IOMAXSIZE : INT := 3;
    FOR iCount := 1 TO IOMAXSIZE BY 1 DO
        aOutput[iCount] := aInput[iCount];
  • teichhei

    teichhei - 2018-05-26

    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

  • Anonymous - 2018-05-28

    Originally created by: scott_cunningham

    Maybe use REFERENCE TO BOOL like this:


    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];

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

  • teichhei

    teichhei - 2018-05-28

    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

  • Anonymous - 2018-05-28

    Originally created by: scott_cunningham

    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.

  • Justelis

    Justelis - 2018-05-29

    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


Log in to post a comment.