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):
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.
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];
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
Originally created by: scott_cunningham
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 := Input_0;
RefDigIn := Input_1;
RefDigOut := Output_0;
RefDigOut := 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.
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.
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.
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.