TCP/IP Socket Communication via URScript with Universal Robots

Universal Robots provides an efficient method for establishing TCP/IP socket communication between a UR robot and external equipment. This guide demonstrates how to set up the robot as a client using URScript, communicating with a server (a PC in this case).

Server Setup

A C# console application, built using Visual Studio 2010, acts as the server. The server listens for connections on IP 127.0.0.1 and port 21. The following steps outline the server setup:

  1. Initialize Server:

    IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
    TcpListener tcpListener = new TcpListener(ipAddress, 21);
    tcpListener.Start();
    
  2. Accept Connections:

    TcpClient tcpClient = tcpListener.AcceptTcpClient();
    NetworkStream stream = tcpClient.GetStream();
    
  3. Handle Client Requests:

    while (tcpClient.Client.Connected) {
        byte[] arrayBytesRequest = new byte[tcpClient.Available];
        int nRead = stream.Read(arrayBytesRequest, 0, arrayBytesRequest.Length);
        if (nRead > 0) {
            string sMsgRequest = ASCIIEncoding.ASCII.GetString(arrayBytesRequest);
            // Process the request and send a response
        }
    }
    

Client Setup in URScript

The UR robot is programmed to act as a client, connecting to the server and exchanging data. Here is a basic URScript example for the client:

  1. Open Socket:

    open≔socket_open("127.0.0.1",21)
    Loop open=False
      open≔socket_open("127.0.0.1",21)
    
  2. Send Data:

    sendToServer≔'send to server'
    socket_send_string(sendToServer)
    
  3. Receive Data:

    receiveFromServ≔socket_read_ascii_float(6)
    Loop receiveFromServ[0]≠6
      Wait: 0.3
      receiveFromServ≔socket_read_ascii_float(6)
    
  4. Move Robot Based on Received Data:

    Loop counter<6
      targetPos[counter]=receiveFromServ[counter+1]
      counter≔counter+1
    MoveJ
      targetPos
    

Data Format Requirements

When using socket_read_ascii_float in URScript, ensure that:

  • Data is surrounded by parentheses.

  • Each data unit is separated by a comma.

  • The message ends with \n.

Example:

  • Server sends: "(0.4, 0, 0.5, 0, -3.14159, 0)\n".

  • Client receives: [6,0.4, 0, 0.5, 0, -3.14159, 0].

For more details and to download the necessary files, visit the Universal Robots TCP/IP socket communication guide.