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

Access variable subrange values

tvm
2019-01-30
2019-02-01
  • tvm - 2019-01-30

    If I declare a variable like this...

    VAR
       TestVar : INT (-4095..4095);
    END_VAR
    

    ...then I can use implict checks to limit the value of TestVar to the lower and upper limits.

    Is there a way to access the upper and lower limits of a variable? A function I can call or something? How are the lower and upper input variables passed to the CheckRangeSigned implicit check function?

    FUNCTION CheckRangeSigned : DINT
    VAR_INPUT
    value, lower, upper: DINT;
    END_VAR
    

    thanks
    Tim

     
  • dFx

    dFx - 2019-01-30

    Using constants in déclaration ? Then you may just use thoses constants.

     
  • Lo5tNet - 2019-01-30

    I am also interested in knowing if there is a way to get the lower and upper limit.

     
  • Anonymous - 2019-01-31

    Originally created by: scott_cunningham

    Brute-force method (assuming you have control of the whole project and can add implicit checks):

    Turn on implicit checks for subrange (CheckRangeSigned and CheckRangeUnsigned). These POUs will return the value or the limited version of the value.

    PROGRAM PLC_PRG
    VAR
       SubrangeINT : INT (-4096..4096);
       MaxInt : INT := 32767;
       MinInt : INT := -32768;
       Highest : INT;
       Lowest : INT;
    END_VAR
    SubrangeINT := MaxInt;
    Highest := SubrangeINT;
    SubrangeINT := MinInt;
    Lowest := SubrangeINT;
    

    But really, if you have access to the whole project, then you can see the limits by looking at the var definition. So I don't think this is much help...

    IMG: subrange.png

     
  • tvm - 2019-02-01

    That's an interesting idea, and it almost works for what I have in mind. The only problem is that I would actually have to change the value of the variable in question in order to test it.
    I set a breakpoint in the CheckRangeSigned function, and it gets the min and max range as inputs to the function, but there's nothing in the call stack to indicate where the inputs come from--the values must be stored in memory somewhere.

     

Log in to post a comment.