Showing results 1 to 5 of 5

Thread: Variable Problem

  1. #1
    Join Date
    07.07.2010
    Posts
    9

    Default Variable Problem

    When I read a zenOn variable in VSTA (C#) I get the value -2147483638 but the datatype is int with init value zero(0). If I link the variable to a "Numeric Value" on my mainscreen and read the variable again the value is 0(correct).

    I read the variable with the following code

    private static zenOn.IVariables ZenOnVariabelen = null;
    
    private void Init() // When starting the program
    {
       ZenOnVariabelen = this.Variables();
    }
    
    private void GetValue() 
    {
       int Var_1 = 0;
    
     try
            {
                Var_1 = Convert.ToInt32( (ZenOnVariabelen.Item("Var_1").get_Value("Var_1")) );
            }
            catch
            {
                MessageBox.Show("Var Error");
            }
    }

    How do I get the correct value in VSTA without linking the variable to a numeric value

  2. #2
    Join Date
    28.07.2008
    Posts
    64

    Default Re: Variable Problem

    Hello!

    If you don't use the variable in your project, it will not exsist in the Runtime. You can only read Variables which are projected in the runtime.

    Quote Originally Posted by ippela View Post
    When I read a zenOn variable in VSTA (C#) I get the value -2147483638 but the datatype is int with init value zero(0). If I link the variable to a "Numeric Value" on my mainscreen and read the variable again the value is 0(correct).

    I read the variable with the following code

    private static zenOn.IVariables ZenOnVariabelen = null;
    
    private void Init() // When starting the program
    {
       ZenOnVariabelen = this.Variables();
    }
    
    private void GetValue() 
    {
       int Var_1 = 0;
    
     try
            {
                Var_1 = Convert.ToInt32( (ZenOnVariabelen.Item("Var_1").get_Value("Var_1")) );
            }
            catch
            {
                MessageBox.Show("Var Error");
            }
    }

    How do I get the correct value in VSTA without linking the variable to a numeric value

  3. #3
    Join Date
    07.07.2010
    Posts
    9

    Default Re: Variable Problem

    I have tested and it works on the mainscreen but if i link a varaible on a passive screen that is not shown the value wil be incorrect -2147483638.

    Is there any solution

  4. #4
    tobiassedlmeier Gast

    Default Re: Variable Problem

    You have to put the variable in the online container, that the variable is advised on the driver. Otherwise you can't read out the value.
    In the form there is an example how to create an online container.

  5. #5

    Default Re: Variable Problem

    please search for "advised" and "online container" in the VSTA and VBA forum for further details.

    By default, only the variables which are currently required by the runtime, are read from the PLC by the driver. In this case, the variable is "advised". A variable which is in the current screen for example is "advised". A variable in a screen which is currently not open, is "unadvised" if it is not needed anywhere else in the project (e.g. alarming, historian etc.)

    To play it safe, you should always check if the method "IsOnline()" returns TRUE, before READING a value with "get_Value()".

    If "IsOnline()" returns FALSE, there is no valid value for this variable (yet). This is the case when the variable is not advised, but may also happen when the driver has not (yet) provided a valid value to the runtime for the advised variable.


    public void Macro_Read()
            {
                zenOn.IVariable myVar = this.Variables().Item("advised");
                zenOn.IVariable myVar2 = this.Variables().Item("not_advised");
    
                if (myVar != null)
                {
                    if (myVar.IsOnline())
                    {
                        // isOnline TRUE then there is a valid value
                        string mVal;
                        mVal = myVar.get_Value(0).ToString();
                        System.Diagnostics.Debug.WriteLine("variable value: " + mVal);
                    }
                    else
                    {
                        //isOnline is FALSE, no valid value!
                        System.Diagnostics.Debug.WriteLine("no valid value for variable: " + (myVar.Name));
                    }
                }
    
                if (myVar2 != null)
                {
    
                    if (myVar2.IsOnline())
                    {
                        // isOnline TRUE then there is a valid value
                        string mVal;
                        mVal = myVar2.get_Value(0).ToString();
                        System.Diagnostics.Debug.WriteLine("variable value: " + mVal);
                    }
                    else
                    {
                        //isOnline is FALSE, no valid value!
                        System.Diagnostics.Debug.WriteLine("no valid value for variable: " + (myVar2.Name));
                    }
    
                }
    
            }

Posting Rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •