How to send and receive information between python and UR5 robot

This How to is made using “The URScript Programming Language” Version 3.8 - November 21, 2018

In order to send and receive information between Python and the UR5 robot—whether it is an integer, a list, or an array—a connection between them needs to be established.

How to create a TCP/IP connection

To create a connection between the two devices, the TCP connection parameters need to be defined:

# Define TCP connection parameters
UR5_IP = "192.168.0.43"
PORT = 30000

Once the connection parameters are defined, the connection can be established using a socket. A socket is a bidirectional communication endpoint.

# Socket to act as a server
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("192.168.0.185", PORT))  # IP address of the device where python program is executed
server_socket.listen(1) # Maximum number of connections that can be queued

Now that the connection is established between the two devices, information can be sent from one end to the other.

How to send and receive information

To Send Information from Python to the Robot

In order to send an array from pyhton to the robot, the array must first be defined.

# Define array
data_list = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

To make the code easier and clearer, a function for sending the data will be used. Including a try-except block helps manage possible exceptions and errors.

def send_data(data_list):
    try:
        byte_data = struct.pack(f'{len(data_list)}B', *data_list)
        client_socket.sendall(byte_data)
        print(f"List sent to the robot: {data_list}")

    except Exception as e:
        print(f"Error sending data: {e}")

To send a list of float numbers ascii encoding is needed.

client_socket.sendall(float_str.encode('ascii')) 

To Send Information from the Robot to Python
To send information from the robot to pyhton, the predefined functions socket_send_byte(), socket_send_int() and socket_send_string() will be used.

socket_send_byte(ready)
socket_send_int(2)
socket_send_string("hello")

To Read Information Sent from the Robot to Python

To receive a byte sent from the UR5 to python, the socket needs to be set to receive.

data = client_socket.recv(1) # 1 byte of data will be received at a time
data_list1 = str(list(struct.unpack('1B', data)))  

To Read Information Sent from Pyhton to the Robot

To receive the list on the UR5, the socket connection must be open.

socket_open("192.168.0.185",30000)

Once the connection is open, the predefined function socket_read_byte_list() can be used to access the information. This will only work if the expected values are integers.

integers= socket_read_byte_list(10) # Modify depending on the size of the array sent

If the list contains floats, the predefined function socket_read_ascii_float() can be used.

floats = socket_read_ascii_float(10) # Modify depending on the size of the array sent

It is important to keep in mind that this function has specific requirements:

  • The format of the numbers should be in parentheses and separated by commas.

Lastly, specific values of the array can be accessed by their index.

textmsg("Integer value on position 1: ", integers[1])
textmsg("Float value on position 1: ", floats[1]

If an error occurs while reading the list, the output will show 0 as the first element, followed by -1 for integers or nan (“Not a Number”) for floats.

1 Like