XML-RPC

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 a specified port (default is 50000).

When a request from the robot arrives, the library 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 your app, the robot should execute the following script on your robot. If you don't know your PC IP, you can check the property robot.PrimaryInterface.LocalEndPoint.

For more information about XML-RPC, see : https://www.universal-robots.com/articles/ur/interface-communication/xml-rpc-communication/

# 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])
static void Main(string[] args)
{
// Create a new robot instance
var robot = new UR();
// Setup connection to the robot
var param = new ConnectParameters();
param.IP = "192.168.0.1";
// Enable XML-RPC server on port 50000
param.XmlRpc.Enable = true;
param.XmlRpc.Port = 50000;
// Connect to the robot
robot.Connect(param);
robot.XmlRpc.XmlRpcServerRequest += XmlRpc_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 static void XmlRpc_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;
}
}

It is possible to create a socket server outside an instance of UR. To do this, you just need to instantiate a XmlRpcServer object.

var server = new XmlRpcServer();
server.Start(50000);
server.XmlRpcServerRequest += Server_XmlRpcServerRequest;
private static void Server_XmlRpcServerRequest(object sender, XmlRpcEventArg request)
{
string methodName = request.MethodName;
}

Example

A urp program file can be downloaded here : xml_rpc_sample.urp

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

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

Members of XmlRpc.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 XmlRpc.XmlRpcValue :
public abstract class XmlRpcValue {
public static implicit operator XmlRpcValue(bool value)
public static implicit operator XmlRpcValue(bool[] value)
public static implicit operator XmlRpcValue(double value)
public static implicit operator XmlRpcValue(double[] value)
public static implicit operator XmlRpcValue(int value)
public static implicit operator XmlRpcValue(int[] value)
public static implicit operator XmlRpcValue(string value)
public static implicit operator XmlRpcValue(string[] value)
public static implicit operator XmlRpcValue(Pose 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.

Read more