UnderAutomation
有问题吗?

[email protected]

联系我们
UnderAutomation
⌘Q
本页面仅提供英文版本。

Socket communication

Exchange your own messages with a UR robot program through the socket functions of URScript. The SDK is the socket server.

  • How it works
  • Example
  • Try it in the demo application
  • Reference
  • What to read next

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 robotEvent of the SDKYour code
socket_open("192.168.0.10", 50001)SocketClientConnectionKeep the client, send it a first message
socket_send_string("Hello")SocketRequestRead Message
value := socket_get_var("COUNTER")SocketGetVarSet Value, an integer, sent back to the robot
socket_read_string()noneSend 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.

Ruby
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 default
parameters.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 robot
robot.SocketCommunication.SocketWrite("Start");
// Or to one robot
foreach (SocketClient client in robot.SocketCommunication.ConnectedClients)
client.SocketWrite("Hello " + client.EndPoint.Address);
}
}
Click to see the full code

The same server works without UR:

static void Main(string[] args)
{
// A socket server, without a UR instance
var server = new SocketCommunicationServer();
server.SocketRequest += (sender, e) =>
{
Console.WriteLine(e.Client.EndPoint.Address + " says " + e.Message);
};
server.Start(50001);
// ...
server.Stop();
}
}
Click to see the full code

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.

Socket communication page of the Universal Robots SDK demo application

The demo application is open source. The C# source of this page is SocketControl.cs.

Reference

Class
SocketCommunicationServerBaseinherits URServiceBase
C#Python

Base for Socket communication server

MemberTypeDescription
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()
  • message : Message to send to the robot
Start(int)
Method
void
Starts socket server. Robot can connect with URScript function socket_open()
  • port : Socket server port
Stop()
Method
void
Disable local socket server and disconnect all connected clients
Class
SocketClient
C#Python

Represent a UR robot connected with URScript function socket_open()

MemberTypeDescription
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()
  • message : Message to send to the robot
ToString()
Method
string
Returns the remote endpoint address and port of the connected robot.
Class
SocketClientConnectionEventArgsinherits EventArgs
C#Python

Event args raised when a socket client is connected with socket_open()

MemberTypeDescription
Client
Field
SocketClient
Robot remote endpoint
Class
SocketRequestEventArgsinherits EventArgs
C#Python

Event args raised when a socket message is received

MemberTypeDescription
Client
Field
SocketClient
Robot IP information
Message
Field
string
Message content received from robot
Class
SocketGetVarEventArgsinherits EventArgs
C#Python

Event args raised when a socket message sent with socket_get_var() is received

MemberTypeDescription
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
Class
SocketClientDisconnectionEventArgsinherits EventArgs
C#Python

Event args raised when a socket client disconnects

MemberTypeDescription
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.

轻松将 Universal Robots、Fanuc、Yaskawa、ABB 或 Staubli 机器人集成到您的 .NET、Python、LabVIEW 或 Matlab 应用程序中

UnderAutomation
联系我们Legal

© All rights reserved.