Showing results 1 to 3 of 3

Thread: Variable read issue on VSTA

  1. #1
    Join Date
    19.09.2017
    Posts
    8

    Default Variable read issue on VSTA

    A variable shows the value -2147483638 when read by a VSTA even if it linked to a screen and DDE is set to active.

    I have this following C# snippet:

    public string GetProjectUserName(zenOn.IProject globalProj, string depName)
            {
                using (System.IO.StreamWriter w = System.IO.File.AppendText(@"C:\Senmatic\log2.txt"))
                {
                    var userNameVar = globalProj.Variables().Item(depName + ".TextString");
    
                    if (userNameVar != null)
                    {
                        var valu = userNameVar.get_Value(1);
                        if (userNameVar.IsOnline())
                        {
                            Log("Advised -> " + "Depname: " + depName + " username 1: " + valu, w);
                        }
                        else
                        {
                            Log("Not advised -> " + "Depname: " + depName + " username 1: " + valu, w);
                        }
    
                        var val = (string)userNameVar.get_Value(1);
                        return val;
                    }
                }
                return null;
            }

    The output is as follows: "Not advised -> Depname: DEP01 username 1: -2147483638"

    the "GetProjectUserName" function is called in a VSTA Macro on runtime startup.
    The variable which is not found is "DEP01.TextString". I made sure that the variable is linked to the screen on startup:
    Click image for larger version

Name:	screen1.PNG
Views:	11
Size:	16.5 KB
ID:	2839
    It has also been set to DDE active:
    Click image for larger version

Name:	screen2.PNG
Views:	9
Size:	15.9 KB
ID:	2840

    Here i have my AUTOSTART sequence for my project:
    Click image for larger version

Name:	Autostart sequence.png
Views:	10
Size:	23.6 KB
ID:	2841
    The screen switch where DEP01.TextString is included, is called before the VSTA Macro which calls "GetProjcetUserName".

    However the variable gets advised if i call "MessageBox.Show" Before reading the value:

    public string GetProjectUserName(zenOn.IProject globalProj, string depName)
            {
                using (System.IO.StreamWriter w = System.IO.File.AppendText(@"C:\Senmatic\log2.txt"))
                {
                    var userNameVar = globalProj.Variables().Item(depName + ".TextString");
    
                    if (userNameVar != null)
                    {
                        //The variable gets advised if i call this MessageBox
                        MessageBox.Show("Test");
    
                        var valu = userNameVar.get_Value(1);
                        if (userNameVar.IsOnline())
                        {
                            Log("Advised -> " + "Depname: " + depName + " username 1: " + valu, w);
                        }
                        else
                        {
                            Log("Not advised -> " + "Depname: " + depName + " username 1: " + valu, w);
                        }
    
                        var val = (string)userNameVar.get_Value(1);
                        return val;
                    }
                }
                return null;
            }

    Output: "Advised -> Depname: DEP01 username 1: "

    I have been struggling with this issue for a while. I'm not sure if i have a threading issue or something else.
    Why do the variable doesn't get advised even tho it's linked to a screen and DDE is active?
    Last edited by MOSA_SENMATIC : 27th February 2020 at 10:56 Reason: Code was on one line

  2. #
    Join Date
    19.09.2017
    Posts
    8
    Best Answer

    Default Re: Variable read issue on VSTA

    I received a mail from COPA-DATA which solved this issue:
    I think that the variable isn’t initialized yet when you run your function.
    When you use a MessageBox, the delay will be enough to give the variable a value.
    Internal variables also have a poll time, like PLC variables.

    If you call VSTA from the start-frame instead, as a Screen action -> start function, it might work better.
    Last edited by MOSA_SENMATIC : 28th February 2020 at 15:11

  3. #2
    Join Date
    10.12.2007
    Posts
    238

    Default AW: Variable read issue on VSTA

    Your code is hard to read in one line... I think the problem is the index 1 you use with get_value, try ".get_Value(0)"

  4. #3
    Join Date
    19.09.2017
    Posts
    8
    Best Answer

    Default Re: Variable read issue on VSTA

    I received a mail from COPA-DATA which solved this issue:
    I think that the variable isn’t initialized yet when you run your function.
    When you use a MessageBox, the delay will be enough to give the variable a value.
    Internal variables also have a poll time, like PLC variables.

    If you call VSTA from the start-frame instead, as a Screen action -> start function, it might work better.
    Last edited by MOSA_SENMATIC : 28th February 2020 at 15:11

Similar Threads

  1. Permanently read variable
    By joelprasad in forum zenon Energy Edition
    Replies: 4
    Last Post: 25th August 2018, 08:45
  2. Replies: 0
    Last Post: 24th August 2018, 07:21
  3. Read workspace name from VSTA
    By miguel.martinez in forum VSTA
    Replies: 0
    Last Post: 9th November 2016, 14:19
  4. VSTA read variable value's another project!
    By dvangenderen in forum VSTA
    Replies: 5
    Last Post: 1st December 2011, 10:04
  5. Replies: 2
    Last Post: 22nd September 2011, 01:32

Tags for this Thread

Posting Rules

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