C# + CoDeSys Automation Platform SDK -> PLC's variable = VarRefState.NotMonitoredYet

2.3k views Asked by At

I'm trying to read the variable of the PLC.

In the Automation Platform, I have a plugin who start a my test (that I've write in C#).

When I execute the plugin for the first time, it always give me the same error. But if a execute it again it's good.

I use a List<IOnlineVarRef6> vars to read my variable. My error is that my vars's State is NotMonitoredYet. Exemple :

    private bool CompareValues(TestCase test, List<IOnlineVarRef6> vars)
    {
        // Stop here the first time
        if (vars.First().State == VarRefState.NotMonitoredYet)
            return false;

        // Execute well the other times
        List<Variable> initialVars = test.initialVariables;
        for (int i = 0; i < vars.Count(); i++)
        {
            object initialValue = initialVars.Single(v => v.Name == vars[i].Expression.ToString()).Value;
            if (!vars[i].Value.Equals(initialValue))
                return false;
        }
        return true;
    }

I think the problem is in the method who get my variable :

    /// <summary>
    /// Create a variable watch for each of the specified variable in the list
    /// </summary>
    /// <example>
    /// Variable should include object hierarchy up to Device
    /// GPX.Diesel_Control.CTRL.PB_CRANK1
    /// </example>
    public List<IOnlineVarRef6> CreateVariableWatch(List<string> vars)
    {
        IVarRef2 varref;
        IOnlineVarRef6 iov;
        IOnlineApplication17 onlineapp = (IOnlineApplication17)onlineMgr.GetApplication(SystemInstances.Engine.Projects.PrimaryProject.ActiveApplication);
        List<IOnlineVarRef6> lstVarRef = new List<IOnlineVarRef6>();
        foreach (string var in vars)
        {
            varref = (IVarRef2)SystemInstances.LanguageModelMgr.GetVarReference(var);
            iov = (IOnlineVarRef6)onlineMgr.CreateWatch(varref);                
            lstVarRef.Add(iov);
        }
        return lstVarRef;
    }

I have a method that wait before calling the CompareValues() and it retries 3 times and it wait before trying again :

    public void SetIsTestPassed(TestCase test)
    {
        Thread.Sleep(test.delayedStart);

        int retries = test.retries;
        do
        {
            List<IOnlineVarRef6> vars = SetCurrentVars(test);
            test.IsTestPassed = CompareValues(test, vars);

            if (test.retryDelay > 0 && !test.IsTestPassed)
                Thread.Sleep(test.retryDelay);
            retries--;
        } while (retries != 0 && !test.IsTestPassed);
    }
    private List<IOnlineVarRef6> SetCurrentVars(TestCase test)
    {
        OnlineManagerHelper OnlineMgr = new OnlineManagerHelper(true);
        return OnlineMgr.CreateVariableWatch(
            test.initialVariables
            .Select(v => Settings.Default.InstancePath + v.Name)
            .ToList());
    }
0

There are 0 answers