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/
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 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;}
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}");}
The Winforms Desktop example implements a XML-RPC sample. A popup appears when a request is received.
public class SocketCommunicationServerBase : URServiceBase, ISocketHandler {// List of all connected clients. One robot can open multiple sockets.public SocketClient[] ConnectedClients { get; }// Is the socket server enabledpublic bool Enabled { get; }// Socket server local portpublic 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 disconnectspublic 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 robotpublic 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 clientspublic void Stop()}
public class SocketClient : ISocketHandler {// Indicates that robot socket is still activepublic bool Connected { get; }// Closes socket communication to robotpublic void Disconnect()// IP address and remote port used by the robot for socket communicationpublic readonly IPEndPoint EndPoint// Event handler when the robot socket disconnectspublic 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 robotpublic event SocketCommunicationServerBase.SocketRequestEventHandler SocketRequest// Write a socket message to the robot. The robot should be connected with socket_open()public void SocketWrite(string message)}
public class SocketClientConnectionEventArgs : EventArgs {// Robot remote endpointpublic readonly SocketClient Client}
public class SocketRequestEventArgs : EventArgs {// Robot IP informationpublic readonly SocketClient Client// Message content received from robotpublic readonly string Message}
public class SocketGetVarEventArgs : EventArgs {// Robot remote endpointpublic readonly SocketClient Client// Name of requested variablepublic readonly string Name// Variable value to send to the robot. If value is null, no messsage is replied to the robotpublic int? Value}
public class SocketClientDisconnectionEventArgs : EventArgs {// Robot remote endpointpublic readonly SocketClient Client}