LogoUnderAutomation
DownloadUniversal RobotsFanucQuote and orderLicenseContact us

Socket communication

Socket Communication

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 URScript
socket_open("192.168.0.10", 50001)

# Raise event SocketRequest is your app
socket_send_string("Hello from robot")

# Raise event SocketGetVar is your app
var1 := socket_get_var("MY_VAR")

Here is an example of a program that supports socket communication :

var robot = new UR();
var param = new ConnectParameters("192.168.0.1");
// Enable socket server on port 50001
param.SocketCommunication.Enable = true;
param.SocketCommunication.Port = 50001;
// onnect to robot with default interfaces and enable socket server
robot.Connect(param);
//...
// Send a message to all connected soket clients
robot.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 message
robot.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 robots
SocketClient[] clients = robot.SocketCommunication.ConnectedClients;
foreach (SocketClient client in clients)
{
// Client IP and remote port
IPAddress clientIp = client.EndPoint.Address;
int clientRemotePort = client.EndPoint.Port;
// Send a message to a specifique client
client.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 robot
request.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 client
SocketClient newClient = request.Client;
}
private static void SocketCommunication_SocketClientConnection(object sender, SocketClientConnectionEventArgs request)
{
// Get new connected robot
SocketClient newClient = request.Client;
}
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

Members of SocketCommunication.Internal.SocketCommunicationServerBase :
public class SocketCommunicationServerBase : URServiceBase, ISocketHandler {
// List of all connected clients. One robot can open multiple sockets.
public SocketClient[] ConnectedClients { get; }
// Is the socket server enabled
public bool Enabled { get; }
// Socket server local port
public int Port { get; }
// Event raised when a robot connects with URScript function socket_open()
public event SocketCommunicationServerBase.SocketClientConnectionEventHandler SocketClientConnection
// Event raised when the robot socket disconnects
public event SocketCommunicationServerBase.SocketClientDisconnectionEventHandler SocketClientDisconnection
// Event raised when the robot calls socket_get_var()
public event SocketCommunicationServerBase.SocketGetVarEventHandler SocketGetVar
// Event raised when a message is received from robot
public event SocketCommunicationServerBase.SocketRequestEventHandler SocketRequest
// Write a socket message to the robot. The robot should be connected with socket_open()
public void SocketWrite(string message)
// Starts socket server. Robot can connect with URScript function socket_open()
public void Start(int port)
// Disable local socket server and disconnect all connected clients
public void Stop()
}
Members of SocketCommunication.SocketClient :
public class SocketClient : ISocketHandler {
// Indicates that robot socket is still active
public bool Connected { get; }
// Closes socket communication to robot
public void Disconnect()
// IP address and remote port used by the robot for socket communication
public readonly IPEndPoint EndPoint
// Event handler when the robot socket disconnects
public event SocketCommunicationServerBase.SocketClientDisconnectionEventHandler SocketClientDisconnection
// Event raised when the robot calls socket_get_var()
public event SocketCommunicationServerBase.SocketGetVarEventHandler SocketGetVar
// Event raised when a message is received from robot
public event SocketCommunicationServerBase.SocketRequestEventHandler SocketRequest
// Write a socket message to the robot. The robot should be connected with socket_open()
public void SocketWrite(string message)
}
Members of SocketCommunication.SocketClientConnectionEventArgs :
public class SocketClientConnectionEventArgs : EventArgs {
// Robot remote endpoint
public readonly SocketClient Client
}
Members of SocketCommunication.SocketRequestEventArgs :
public class SocketRequestEventArgs : EventArgs {
// Robot IP information
public readonly SocketClient Client
// Message content received from robot
public readonly string Message
}
Members of SocketCommunication.SocketGetVarEventArgs :
public class SocketGetVarEventArgs : EventArgs {
// Robot remote endpoint
public readonly SocketClient Client
// Name of requested variable
public readonly string Name
// Variable value to send to the robot. If value is null, no messsage is replied to the robot
public int? Value
}
Members of SocketCommunication.SocketClientDisconnectionEventArgs :
public class SocketClientDisconnectionEventArgs : EventArgs {
// Robot remote endpoint
public readonly SocketClient Client
}

Read more

Logo

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

© 2024 UnderAutomationEULA
  • Download
  • Universal Robots
  • Fanuc
  • Quote and order
  • License
  • Contact us