RobotStudio event

Question about the UserDefined struct

I'm working with the PC SDK version 5.08 and I tried to get the C# userdefined read and write example to work.

What I noticed is that the UserDefined struct requires a RapidDataType and you need a connection to the controller for it. With other structures like Num or RobTarget you don't need a RapidDataType and no connection to the controller.

My question is: Why does the UserDefined struct need a RapidDataType and a connection to the controller?

Comments

  • I haven't used this, but it would seem that, since a RECORD type can be any combination of other types (for instance nums AND strings AND bools), you need to get retrieve a reference to the particular RECORD type you are trying to access so that your app will know how to allocate space for it and access its individual elements, of which it has no prior knowledge.
    Russell Drown
  • Did you get the example to work? In the RunData struct the constructor of the UserDefined struct gets called with one string as argument, but this constructor doesn't exist.

    Did anyone succeed in makeing the example work?

  • I tested this today and it is not necessary to do it the way it is shown in the RAB sample. Basically all you need to do is is obtain a RapidData reference to your item of interest then cast it to UserDefined, and then interact with it. To write to it, you must get mastership of the controller. Below is a sample.

    To use this sample, do the following:

    1. Create a recod type called "myRec" in the user module of T_ROB1, and then create an instance called "Test1"

    RECORD myRec
      num X;
      string Y; 
    ENDRECORD

    Var myRec Test1;

    2. Run this code in your app. It requires a controller object called ctrl, and the method WriteMessage is something I use to write to a text box, you can write it to the debug window for simplicity.

    Dim rd As RapidData
    Dim ms As Mastership
    Dim ud As UserDefined

    Try
       If ctrl.OperatingMode = ControllerOperatingMode.Auto Then
          'In auto, so get mastership and write data
          ms = Mastership.Request(ctrl.Rapid)
          If ms Is Nothing Then
             Throw New Exception("MastershipFailed")
          End If
          ms.ReleaseOnDispose = True
          rd = ctrl.Rapid.GetRapidData("T_ROB1", "user", "Test1")
          'Cast it to UserDefined type
          ud = DirectCast(rd.Value, UserDefined)
          'display the current values
          WriteMessage("Component [0] is: " & ud.Components(0).ToString)
          WriteMessage("Component [1] is: " & ud.Components(1).ToString)
          'update the values
          ud.Components(0).FillFro mString("1")
          ud.Components(1).FillFro mString("test")
          'now write the data back to the controller
          rd.Value = ud
          'display the updated values
          WriteMessage("Component [0] is: " & ud.Components(0).ToString)
          WriteMessage("Component [1] is: " & ud.Components(1).ToString)  

       End If
    Catch ex As Exception
       WriteMessage("Exception: " & ex.ToString)
    Finally
       If Not rd Is Nothing Then
          rd.Dispose()
          rd = Nothing
       End If
       If Not ms Is Nothing Then
          ms.Dispose()
          ms = Nothing
       End If
    End Try

    You can look in the RAPID Data Viewer to verify that the values are updated.

    Russell Drown