Create TCP/IP connection between your PC and a Universal robot

Create TCP/IP connection between your pc and UR robot

This how to will show you bare minimum of establishing communication between the ur robot and your device using the ur script and python script.

Connect your device to ur robot and configure your ip, make sure your ip is within robot’s ip range, something like 192.168.0.xxx
Confirm your device’s ip by typing ipconfig on terminal
Next you can copy the ur script to teaching pendant and run the python script

UR Script

ip = "192.168.78.1" # This is your device's ip
socket_name = "anything"
socket = socket_open(ip, port, socket_name)

while (True);
    # receive the expected 3 floating point indexes of an array
    receive = socket_read_ascii_float(3,socket_name)
    textmsg(receive)

    # send a message
    socket_send_string("Send your strings here", socket_name) 
end

socket_close(socket_name)

Python script

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind socket to your device's ip and port, port can be anything above 1024 
# You can't use port that used by other process
s.bind(("192.168.78.1", 5000))

s.listen(5)
print("Server is listening for connections...")

while True:
    # Accept a connection from a client
    clientsocket, address = s.accept()
    print(f"Connection from {address} has been established!")

    while True:
        # Receive a message with expected byte length of 1024 byte
        msg = clientsocket.recv(1024) 
        if not msg:  # If no message is received, break the loop
            break

        # Decode message to utf-8 format 
        print(msg.decode("utf-8"))

        # Send floating point number
        message_to_send = "(1.2, 1.3, 1.4)"
        clientsocket.send(bytes(message_to_send, "ascii"))

    # Close the client socket
    clientsocket.close()
1 Like