Socket communication
Exchange your own messages with a UR robot program through the socket functions of URScript. The SDK is the socket server.
With the socket functions of URScript, a program that runs on a Universal Robots cobot connects to your application and exchanges text and numbers with it. This page shows the socket server of the SDK: it listens on a port of the PC, 50001 by default, and raises an event for each message of a robot.
How it works
The robot program opens the connection with socket_open, then sends and receives messages. On the PC, the SDK raises an event for each step:
| URScript of the robot | Event of the SDK | Your code |
|---|---|---|
socket_open("192.168.0.10", 50001) | SocketClientConnection | Keep the client, send it a first message |
socket_send_string("Hello") | SocketRequest | Read Message |
value := socket_get_var("COUNTER") | SocketGetVar | Set Value, an integer, sent back to the robot |
socket_read_string() | none | Send with SocketWrite |
socket_close() | SocketClientDisconnection |
192.168.0.10 is the address of the PC. See TCP/IP socket communication via URScript by Universal Robots.
socket_open("192.168.0.10", 50001)socket_send_string("Hello")counter := socket_get_var("COUNTER")reply := socket_read_string()socket_close()
The program socket_sample.urp uses these functions.
Example
The socket server is disabled by default: set SocketCommunication.Enable in ConnectParameters.
static void Main(string[] args){var robot = new UR();var parameters = new ConnectParameters("192.168.0.1");// The socket server is disabled by defaultparameters.SocketCommunication.Enable = true;parameters.SocketCommunication.Port = 50001;robot.Connect(parameters);// URScript: socket_open("192.168.0.10", 50001)robot.SocketCommunication.SocketClientConnection += (sender, e) =>{e.Client.SocketWrite("Welcome");};// URScript: socket_send_string("Hello")robot.SocketCommunication.SocketRequest += (sender, e) =>{Console.WriteLine(e.Client.EndPoint.Address + " says " + e.Message);};// URScript: value := socket_get_var("COUNTER")robot.SocketCommunication.SocketGetVar += (sender, e) =>{if (e.Name == "COUNTER") e.Value = 12; // integer only};// URScript: socket_close()robot.SocketCommunication.SocketClientDisconnection += (sender, e) =>{Console.WriteLine(e.Client.EndPoint + " disconnected");};// Send a message to every connected robotrobot.SocketCommunication.SocketWrite("Start");// Or to one robotforeach (SocketClient client in robot.SocketCommunication.ConnectedClients)client.SocketWrite("Hello " + client.EndPoint.Address);}}
The same server works without UR:
static void Main(string[] args){// A socket server, without a UR instancevar server = new SocketCommunicationServer();server.SocketRequest += (sender, e) =>{Console.WriteLine(e.Client.EndPoint.Address + " says " + e.Message);};server.Start(50001);// ...server.Stop();}}
Several robots can connect to the same server: ConnectedClients lists them, and each SocketClient has its own SocketWrite. Allow the port of the server in the firewall of the PC.
Try it in the demo application
Everything on this page can be tried without writing code, in the Socket communication page of the demo application.

The demo application is open source. The C# source of this page is SocketControl.cs.
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()
|
ToString() Method | string | Returns the remote endpoint address and port of the connected robot. |
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 |
Event args raised when a socket client disconnects
| Member | Type | Description |
|---|---|---|
Client Field | SocketClient | Robot remote endpoint |
What to read next
- XML-RPC: the robot program calls a function of the PC and waits for its answer.
- Read and write registers: exchange numbers without a server.