RobotStudio event

Write to RAPID variable from C# PC SDK

Hello, i'am trying to write to the RAPID variable from my PC SDK App written on C# (Visual Studio 2017).
Here is a piece my code where the problem occurs:
                
double val;
RapidData rd = this.controller.Rapid.GetRapidData("T_ROB1", "Module1", "val_try");
      if (rd.Value is ABB.Robotics.Controllers.RapidDomain.Dnum)
                {
                    val = (ABB.Robotics.Controllers.RapidDomain.Dnum)rd.Value;
                    val = 1.25;
                    using (Mastership.Request(this.controller.Rapid))
                    {
                        rd.Value = val; //here I have an error which says that it is not possible to convert the type double to 'ABB.Robotics.Controllers.IRapidData'
                    }
                }
I have tried this with a bool variable and it works fine, but Ido not understand how to change the number value from C# in RAPID code running on the controller. 

Thanks in advance,
Nazar

Comments

  • Hello,

    Have you tried searching the forum before writing your post?

    I did a quick search and found this post: https://forums.robotstudio.com/discussion/comment/22811#Comment_22811

    Is it perhaps all you need?

    Maxim Riabichev
    PC Software Support Engineer
  • Hi,
    Dnum is structure so casting or setting directly the value won't work.
    You can fill the Dum from string like following
         Dnum dn = new Dnum();
         dn.FillFromString(newValue);
         _rapidData.Value = dn;
    or at Dnum constructor give the double value, so no need to fill from string. (Like: Dnum dn = new Dnum(val);)
    There is also method dn.FillFromDnum(val) but don't use it as documentation said it is obsolete

    Mika
  • Nazarinho
    Nazarinho
    edited August 2018


    Hello Maxim and Mika, first of all thanks for your answer.
    Hello,

    Have you tried searching the forum before writing your post?

    I did a quick search and found this post: https://forums.robotstudio.com/discussion/comment/22811#Comment_22811

    Is it perhaps all you need?

    I have already used API from the link like a base for my code project, and finally, after modifying some assignments I have solved the problem of writing on variables.
    msalminen said:
    Hi,
    Dnum is structure so casting or setting directly the value won't work.
    You can fill the Dum from string like following
         Dnum dn = new Dnum();
         dn.FillFromString(newValue);
         _rapidData.Value = dn;
    or at Dnum constructor give the double value, so no need to fill from string. (Like: Dnum dn = new Dnum(val);)
    There is also method dn.FillFromDnum(val) but don't use it as documentation said it is obsolete

    Mika
    My solution is using the following method:

    //After establishing comunication with controller i am asigning tasks
     aTask = controller.Rapid.GetTasks();
    //After that i am updating my RapidData variables :
            void update_values()
            {
                rdice = aTask[0].GetRapidData("Module1", "dice_txt");
                rangle = aTask[0].GetRapidData("Module1", "angle_txt");
                rx = aTask[0].GetRapidData("Module1", "x_txt");
                ry = aTask[0].GetRapidData("Module1", "y_txt");
                if ((rdice.Value is ABB.Robotics.Controllers.RapidDomain.Num)
                    &&(rangle.Value is ABB.Robotics.Controllers.RapidDomain.Num)
                    && (rx.Value is ABB.Robotics.Controllers.RapidDomain.Num)
                    && ((ry.Value is ABB.Robotics.Controllers.RapidDomain.Num)))
                {
                    val = (ABB.Robotics.Controllers.RapidDomain.Num)rdice.Value;
                    val.FillFromNum(arr[0]);
                    val = (ABB.Robotics.Controllers.RapidDomain.Num)rangle.Value;
                    val.FillFromNum(arr[1]);
                    val = (ABB.Robotics.Controllers.RapidDomain.Num)rx.Value;
                    val.FillFromNum(arr[2]);
                    val = (ABB.Robotics.Controllers.RapidDomain.Num)ry.Value;
                    val.FillFromNum(arr[3]);
                    Mastership m;
                    using (m = Mastership.Request(controller.Rapid))
                    {
                        rdice.Value = val;
                        rangle.Value = val;
                        rx.Value = val;
                        ry.Value = val;
                        m.Release();
                    }
                    
                }

    At this point everything is working fine if the update_values() is executing ones, for example by clicking on the button. 
    But If I try to execute it periodically (every 500ms), using a timer:
     
    private void timer1_Tick(object sender, EventArgs e)
            {
             update_values();
            }
    I have another problem related to the mastership. It says that there is another request holding on. Can it be robotstuido fault? It will be a problem with the real controller? 
    Thanks in advance,
    Best regards,
    Nazar