XML-RPC : Remote Procedure Call

Overview

This feature allows a robot to request or transmit information to the PC.

The XML-RPC protocol is used. Via this SDK, the PC is a server and opens a TCP Listener on port 50000. This port can be changed either by calling ur.Connect("ip_robot", xmlRpcServerPort: 50001) or by calling ur.EnableXmlRpcServer(50001).

When a request from the robot arrives, the SDK raises the event XmlRpcServerRequest which contains the name of the method called, its arguments, the IP of the robot and an Answer property which you can set and which contains the value returned to the robot.

To communicate with the SDK, you should execute the following script on your robot. If you don't know your PC IP, you can check the property ur.DataStreamingLocalEndPoint.

# Connect to the SDK and specifie the IP and port of the PC
rpc:=rpc_factory("xmlrpc","http://192.168.0.10:50000")

# Call method get_answer and wait for the reply. The reply will be assigned in variable "answer"
answer:=rpc.get_answer("Hello", True, False, 12, 12.2, p[100,100,120,0.1,0,0], [12,1.2,123])

Example

This script can be downloaded here : xml_rpc_sample.urp

You can try it with this sample below or with the downloadable Winforms and console examples.

private ur = new UR();
private void Example()
{
// Connection to the robot with default parameters : XML-RPC server is started on port 50000
ur.Connect("192.168.0.1");
// Handle XML-RPC event
ur.XmlRpcServerRequest += Ur_XmlRpcServerRequest;
}
// Method called when the robot sends a request
// You shoud execute on your robot : rpc:=rpc_factory("xmlrpc","http://192.168.0.10:50000")
// Replace the IP address 192.168.0.10 with the IP of the machine running this .NET code
// If you don't know your IP, you can find it in your interface properties or in with this SDK in the property : ur.DataStreamingLocalEndPoint
private void Ur_XmlRpcServerRequest(object sender, XmlRpcEventArg request)
{
Console.WriteLine("Robot IP : " + request.EndPoint.Address);
// Prints :
// Robot IP : 192.168.0.1
// Set the returned answer according to the method and its arguments
switch (request.MethodName)
{
case "get_answer":
// Robot script : answer1:=rpc.get_answer("Hello", True, False, 12, 12.2, p[100,100,120,0.1,0,0], [12,1.2,123])
// Reply : answer1:=TRUE
foreach (var argument in request.Arguments)
{
Console.WriteLine(argument.ToString()); // Prints argument value : "Hello", "true", "false", "12", ...
}
request.Answer = true;
break;
case "GetPose":
// Robot script : answer2:=rpc.GetPose()
// Reply : answer2:=p[100,200,100,0,0,0]
request.Answer = new Pose(100, 200, 100, 0, 0, 0);
break;
case "HowAreYou":
// Robot script : answer3:=rpc.HowAreYou("Alfred")
// Reply : answer3:="Fine thx Alfred"
request.Answer = "Fine thx " + request.Arguments[0];
break;
case "SumFirstArray":
// Robot script : answer4:=rpc.SumFirstArray([1,3.5,-2])
// Reply : answer4:=2.5
double[] argument1 = request.Arguments[0];
double sum1 = 0;
for (int i = 0; i < argument1.Length; i++) sum1 += argument1[i];
request.Answer = sum1;
break;
case "SumMyArguments":
// Robot script : answer5:=rpc.SumMyArguments(1,3.5,-2)
// Reply : answer5:=2.5
double sum = 0;
for (int i = 0; i < request.Arguments.Length; i++)
{
double argValue = request.Arguments[i];
sum += argValue;
}
request.Answer = sum;
break;
default:
// Do not reply and the answer variable is not assigned
break;
}
}

Start/Stop XML-RPC

You can enable or disable and XML-RPC server at any time with the following methods:

// Enable the local XML-RPC server to receive commads from the robot
public void EnableXmlRpcServer(int port = 50000)
// Disable and close the socket used for the XML-RPC server
public void DisableXmlRpcServer()

You can then check if XML-RPC is enabled with the following properties :

// Is the XML-RPC server enabled
public bool XmlRpcServerEnabled { get; }

If you want to know with which of your local IP addresses you are connected to the robot, you can use the DataStreamingLocalEndPoint property. This property also allows to know which IP address to use in the robot's rpc_factory("xmlrpc", "http://????:50000") function.

// Indicates the current local endpoint (i.e. IP Address) used to communicate with the robot. You can use this IP in your UR script in the function rpc_factory()
public IPEndPoint DataStreamingLocalEndPoint { get; }

Winforms

In the Winforms example, a popup is displayed and contains all information about the request and allows you to return a typed object to the robot with buttons.

Console

The console example returns a default value for each methods.

LabView

The LabView example is used to respond to the GetPose() method and return a position. The position returned to the robot can be entered in the Front Panel.

For all other requests a string is returned. However, you can adapt this example to return arrays, numbers, ...

A Callback VI is called when a XML RPC request is received from the robot.

XML-RPC types

// Event raised when a XML-RPC request has been sent from the robot to this machine. You should answer to the robot in this event via the property request.Answer
public event UR.XmlRpcServerRequestEventHandler XmlRpcServerRequest
Members of XmlRpcEventArg :
public class XmlRpcEventArg : EventArgs {
// Response to be provided to the robot by the user
public XmlRpcValue Answer,
// The arguments of the method called
public readonly XmlRpcValue[] Arguments,
// IP address of the robot
public readonly IPEndPoint EndPoint,
// The method called by the robot
public readonly string MethodName,
// The XML document received via HTTP
public readonly XDocument XmlRequest
}

The class XmlRpcValue has implicit operators that allow it to be implicitly casted in native types (int, double, string, Pose, array). This is why it is for example possible to write request.Answer = 12 instead of request.Answer = new XmlRpcIntegerValue(12).

Members of XmlRpcValue :
public abstract class XmlRpcValue {
public static implicit operator XmlRpcValue(bool value),
public static implicit operator XmlRpcValue(double value),
public static implicit operator XmlRpcValue(int value),
public static implicit operator XmlRpcValue(string value),
public static implicit operator XmlRpcValue(Pose value),
public static implicit operator bool (XmlRpcValue value),
public static implicit operator bool[](XmlRpcValue value),
public static implicit operator double (XmlRpcValue value),
public static implicit operator double[](XmlRpcValue value),
public static implicit operator int (XmlRpcValue value),
public static implicit operator int[](XmlRpcValue value),
public static implicit operator string (XmlRpcValue value),
public static implicit operator string[](XmlRpcValue value),
public static implicit operator Pose(XmlRpcValue value),
public static implicit operator Pose[](XmlRpcValue value),
public static implicit operator XmlRpcValue(XmlRpcValue[] value),
public override string ToString(),
// Determines the class of this message
public abstract XmlRpcType Type { get; },
// The XML description of the message that has been received from the robot or will be sent to the robot
public XElement Xml { get; }
}

The classes XmlRpcIntegerValue, XmlRpcDoubleValue, XmlRpcBooleanValue, XmlRpcStringValue, XmlRpcPoseValue, XmlRpcArrayValue, XmlRpcStructValue inherits from XmlRpcValue and have a Type field that contains the value in the right type.

If an unknown object is received, a XmlRpcUnknownValue value is returned that contains a AdditionalInformation explaining why it is not supported.

Logo

UnderAutomation
66 Avenue des Champs-Elysées
75008 Paris
FRANCE

© 2021 UnderAutomation - UnderAutomation Universal Robots .NET Communication SDK (Software Development Kit) offre la possibilité de créer des applications personnalisées .NET Core, .NET Framework et .NET Standard qui peuvent communiquer avec tous les robots UR. Des exemples prêts à l'emploi pour Windows, Mac OS et Linux, LabView, Node.js et Python sont fournis. Ce SDK utilise le serveur de Dashboard Server TCP/IP et XML-RPC pour contrôler le robot à distance et le protocole Client Interface pour recevoir et décoder le flux de données.