Socket communication
Your application can become a socket server to exchange data with the robot through your own protocol.
Your application can become a socket server to exchange data with the robot through your own protocol.
Socket communication allows you to create a local server to which the robot can connect via the URScript socket_open() function.
Once the connection is open, the robot and your program can exchange custom data (number, text, position). You can then invent your own bidirectional protocol.
For more Information about socket communication, see : https://www.universal-robots.com/articles/ur/interface-communication/tcpip-socket-communication-via-urscript/
Enable socket server
Socket communication starts a server on the configured local port. Using URScript, it is possible to connect to your application and exchange data.
You can download a socket upr program example from here : https://github.com/underautomation/UniversalRobots/raw/master/.NET/Examples/WindowsDesktop/Samples/socket_sample.urp
Here is an example of URScript to use in the robot (where your PC IP is 192.168.0.10):
# Connect to robot socket server in URScriptsocket_open("192.168.0.10", 50001)# Raise event SocketRequest is your appsocket_send_string("Hello from robot")# Raise event SocketGetVar is your appvar1 := socket_get_var("MY_VAR")
Here is an example of a program that supports socket communication :
using System.Net;using UnderAutomation.UniversalRobots;using UnderAutomation.UniversalRobots.SocketCommunication;class Socket{static void Main(string[] args){var robot = new UR();var param = new ConnectParameters("192.168.0.1");// Enable socket server on port 50001param.SocketCommunication.Enable = true;param.SocketCommunication.Port = 50001;// onnect to robot with default interfaces and enable socket serverrobot.Connect(param);//...// Send a message to all connected soket clientsrobot.SocketCommunication.SocketWrite("Hi cobot !");// Event raised when a robot connects with socket_open()robot.SocketCommunication.SocketClientConnection += SocketCommunication_SocketClientConnection;// Event raised when a robot disconnects with socket_close()robot.SocketCommunication.SocketClientDisconnection += SocketCommunication_SocketClientDisconnection;// Event raised when a connected robot sends a messagerobot.SocketCommunication.SocketRequest += SocketCommunication_SocketRequest;// Respond to a client requesting the value of a variable with the URScript line : var1 := socket_get_var("VAR_NAME")robot.SocketCommunication.SocketGetVar += SocketCommunication_SocketGetVar;// List of all connected robotsSocketClient[] clients = robot.SocketCommunication.ConnectedClients;foreach (SocketClient client in clients){// Client IP and remote portIPAddress clientIp = client.EndPoint.Address;int clientRemotePort = client.EndPoint.Port;// Send a message to a specifique clientclient.SocketWrite("Hello :)");}}private static void SocketCommunication_SocketGetVar(object sender, SocketGetVarEventArgs request){Console.WriteLine($"{request.Client.EndPoint.Address} asks value for variable {request.Name}");// Send variable value to the robotrequest.Value = 12;}private static void SocketCommunication_SocketRequest(object sender, SocketRequestEventArgs request){Console.WriteLine($"{request.Client.EndPoint.Address} says {request.Message}");}private static void SocketCommunication_SocketClientDisconnection(object sender, SocketClientDisconnectionEventArgs request){// Get disconnected clientSocketClient newClient = request.Client;}private static void SocketCommunication_SocketClientConnection(object sender, SocketClientConnectionEventArgs request){// Get new connected robotSocketClient newClient = request.Client;}}
using UnderAutomation.UniversalRobots.SocketCommunication;class SocketDirect{static void Main(string[] args){var server = new SocketCommunicationServer();server.Start(50001);server.SocketRequest += Server_SocketRequest;}private static void Server_SocketRequest(object sender, SocketRequestEventArgs request){Console.WriteLine($"{request.Client.EndPoint.Address} says {request.Message}");}}
Example
The Winforms Desktop example implements a XML-RPC sample. A popup appears when a request is received.

API Reference
Base for Socket communication server
| Member | Type | Description |
|---|---|---|
SocketCommunicationServerBase() Constructor | ||
ConnectedClients Property read only | SocketClient[] | List of all connected clients. One robot can open multiple sockets. |
Enabled Property read only | bool | Is the socket server enabled |
Port Property read only | int | Socket server local port |
SocketClientConnection Event | SocketClientConnectionEventHandler | Event raised when a robot connects with URScript function socket_open() |
SocketClientDisconnection Event | SocketClientDisconnectionEventHandler | Event raised when the robot socket disconnects |
SocketGetVar Event | SocketGetVarEventHandler | Event raised when the robot calls socket_get_var() |
SocketRequest Event | SocketRequestEventHandler | Event raised when a message is received from robot |
SocketWrite(string) Method | void | Write a socket message to the robot. The robot should be connected with socket_open()
|
Start(int) Method | void | Starts socket server. Robot can connect with URScript function socket_open()
|
Stop() Method | void | Disable local socket server and disconnect all connected clients |
Represent a UR robot connected with URScript function socket_open()
| Member | Type | Description |
|---|---|---|
Connected Property read only | bool | Indicates that robot socket is still active |
EndPoint Field | IPEndPoint | IP address and remote port used by the robot for socket communication |
SocketClientDisconnection Event | SocketClientDisconnectionEventHandler | Event handler when the robot socket disconnects |
SocketGetVar Event | SocketGetVarEventHandler | Event raised when the robot calls socket_get_var() |
SocketRequest Event | SocketRequestEventHandler | Event raised when a message is received from robot |
Disconnect() Method | void | Closes socket communication to robot |
SocketWrite(string) Method | void | Write a socket message to the robot. The robot should be connected with socket_open()
|
Event args raised when a socket client is connected with socket_open()
| Member | Type | Description |
|---|---|---|
Client Field | SocketClient | Robot remote endpoint |
Event args raised when a socket message is received
| Member | Type | Description |
|---|---|---|
Client Field | SocketClient | Robot IP information |
Message Field | string | Message content received from robot |
Event args raised when a socket message sent with socket_get_var() is received
| Member | Type | Description |
|---|---|---|
Client Field | SocketClient | Robot remote endpoint |
Name Field | string | Name of requested variable |
Value Field | int? | Variable value to send to the robot. If value is null, no messsage is replied to the robot |