Can CoDeSys do conditional variable type identification?
Example:
I have a scale function that takes in a REAL value and scales it to some new range, then outputs a REAL. But, sometimes Iβm trying to scale an INT, or a DINT, or a BYTE or SINT, etc. how can I make this function automatically recognize the input type, do a xxx_TO_REAL, run my scale function, then do a REAL_TO_xxx, then output the result as an xxx instead of a REAL?
Any solution besides βmake a different function for each variable typeβ would be awesome.
Thanks!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In this case I would use; INT_TO_REAL(your_var) or WORD_TO_REAL(your_var) etc.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2019-02-21
Originally created by: scott_cunningham
Codesys V3 now supports the ANY_TYPE, which would work, but I donβt believe this is available on Codesys V2.3.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2019-11-05
Originally created by: fk5
So, it works this way for using different inputs. That's good.
But, I also want it to output into the same type as whatever is connected.
Is this possible? (Example: I'd like to modify this function to work regardless if dd is a REAL, INT, BYTE, ...)
FUNCTION anyTestFunction : REAL
VAR_INPUT
in1: REAL := ANY_TO_REAL(in1);
in2: REAL := ANY_TO_REAL(in2);
in3: REAL := ANY_TO_REAL(in3);
END_VAR
anyTestFunction := in1 + in2 + in3;
PROGRAM PLC_PRG
VAR
aa: INT := 2;
bb: REAL := 2.1;
cc: BYTE := 3;
dd: REAL;
END_VAR
Originally created by: fk5
Can CoDeSys do conditional variable type identification?
Example:
I have a scale function that takes in a REAL value and scales it to some new range, then outputs a REAL. But, sometimes Iβm trying to scale an INT, or a DINT, or a BYTE or SINT, etc. how can I make this function automatically recognize the input type, do a xxx_TO_REAL, run my scale function, then do a REAL_TO_xxx, then output the result as an xxx instead of a REAL?
Any solution besides βmake a different function for each variable typeβ would be awesome.
Thanks!
In this case I would use; INT_TO_REAL(your_var) or WORD_TO_REAL(your_var) etc.
Originally created by: scott_cunningham
Codesys V3 now supports the ANY_TYPE, which would work, but I donβt believe this is available on Codesys V2.3.
Originally created by: fk5
So, it works this way for using different inputs. That's good.
But, I also want it to output into the same type as whatever is connected.
Is this possible? (Example: I'd like to modify this function to work regardless if dd is a REAL, INT, BYTE, ...)
FUNCTION anyTestFunction : REAL
VAR_INPUT
in1: REAL := ANY_TO_REAL(in1);
in2: REAL := ANY_TO_REAL(in2);
in3: REAL := ANY_TO_REAL(in3);
END_VAR
anyTestFunction := in1 + in2 + in3;
PROGRAM PLC_PRG
VAR
aa: INT := 2;
bb: REAL := 2.1;
cc: BYTE := 3;
dd: REAL;
END_VAR
dd := anyTestFunction(aa, bb, cc);
*******************************************************************
Originally created by: scott_cunningham
You could return the ANY type, but then you would have to determine which type within the ANY type.
i do it by paralleling inputs and outputs in ver 2 so input is real and int same as output.