How-to Connect a Cobot to your laptop using Python (Complete Guide)

Introduction
So you would like to control a cobot using your laptop? That’s great! This manual will show you how and consist on a couple different ways to do so! These different ways have already been described in other How-to’s and this guide will compress these or refer you to the others.

For this How-to you will need the following items:

  • A Cobot
  • A laptop
  • A Ethernet cable

Connection methods
There are 2 major ways in which you can connect a cobot to a laptop, these are either by using a socket (Ethernet connection) or using RTDE (Real Time Data Exchange). Both of these work similarly, but need very different set-ups to actually work.

Let’s begin by showing you the easiest way of connecting to the robot

Using an Ethernet socket
There are two types of internet sockets: TCP sockets and UDP sockets. This How to manual makes use of TCP sockets as it’s the safest and most reliable way of sending data through the socket.

To be able to start a connection, two things are needed, which are the IP address and a port number

IP-Address
This is an ID for a Device on a network which usually looks something like 192.168.0.xx The last number is somewhere between 0 and 255 and can be assigned to whatever you want by setting a Static IP address like you have been told in the integration practicum.

Port
this is a number somewhere between 0 and 65535, which specifies what kind of service you are using. Below is a break-down of the meaning of these different port number:

  • Port 0-1023 are preserved for HTTP, HTTPS, FTP or SSH
  • Port 1024-49151 are used for Internet Assigned Numbers Authority (IANA)
  • Port 49151-65535 Are temporary connection ports, hence the perfect ports for our robot communications.

With these IDs out of the way, we can start a connection by creating a Server client to be able to connect to

Server (Hosting Device)
the basics of TCP socket communications are shown below and are fairly simple to explain:

  1. S.bind opens a connection on said IP and port number to the outside world
  2. S.listen makes sure that your server is able to receive messages by making it listen to the port opened by the first step
  3. S.accept, accepts incoming connection requests and makes an object out of it which is called server_client which can be used to receive and send messages to
import socket

HOST = '192.168.0.1'
PORT = 56666

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    server_client, address = s.accept()

If we go into a little more modular design for communicating constantly and over multiple events down under this paragraph you’ll find a usable class which isn’t perfect but a great start to a modular approach of TCP connection

'''This code is supporting the use of an client to connect an 6 axis robotic arm to anything that is equiped with Ethernet acces 
Authored by: Dave Vermeulen and Bastiaan Wolters (Original authors) SMR1 Sept 2025
Version: 0.2.3

Note that this code isnt flawless might take some time debugging'''

import socket as sck
import time

class Network_client:
    def __init__(self, IP, PORT):
        self.addr = (IP,PORT)
        
        print(f'Socket created on following address: {self.addr}')
        
        self.server_socket = sck.socket(sck.AF_INET, sck.SOCK_STREAM)
        self.server_socket.bind(self.addr)
    
    def strt_socket(self):
        self.server_socket.listen(5)
        print(f"Socket with address: {self.addr} Started listening")
        
    def clse_socket(self):
        print(f"!!!Socket with address: {self.addr} is CLOSING DOWN!!!")
        self.server_socket.close()
    
    def connect_client(self):
        self.client_socket, self.client_addr = self.server_socket.accept()
        print(f"Client connection ESTABLISHED From {self.client_addr} at {time.strftime('%d-%m-%Y %H:%M:%S', time.localtime())}")
        self.client_socket.send("Server is listening".encode('utf-8'))
    
    def disconnect_client(self):
        print(f"CLOSING: {self.client_addr}")
        self.client_socket.close() 
    
    def receive_client(self):
        data_receive = self.client_socket.recv(2048).decode('utf-8')
        
        print(f"AWAITING Data from: {self.client_addr}")
        
        if not data_receive:
            return None
        else:
            print(f"Data RECEIVED From: {self.client_addr} \n Data: {data_receive}")
            return data_receive

    def send_client(self, message):
        msg = str(message).replace("[","(").replace("]",")")
        print(f"DATA TO SEND: {msg}\n SENDING to : {self.client_addr}")
        self.client_socket.send(msg.encode('utf-8'))

        
if __name__ == '__main__':
    IP_address = "192.168.0.3"
    PORT_num = 62513
    
    NetwkCl = Network_client(IP_address, PORT_num)

Client Side (Robot)
The client on a TCP connection can be both a Doosan robot or A UR robot, It could even just be a little Raspberry Pi in the following paragraphs a little example snippet will be given for each Usage of the TCP connection

UR-Script Socket Client

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-Client

import socket

# Server information
HOST = '127.0.0.1'  # Server IP address (localhost)
PORT = 65432        # Server port

# Create a TCP/IP socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))  # Connect to server
    s.sendall(b'Hello, server!')  # Send message
    data = s.recv(1024)  # Receive response

print('Received from server:', data.decode())

Doosan

from DCRF import *
# Socket port
PORT = 56666
# Open socket connection
tp_log("Waiting for Connection!")
sock = server_socket_open(PORT)
tp_log("Connected!")

With these, you can connect anything to your laptop using an Ethernet cable.

Real Time Data Exchange
Another way of connecting to The UR robot is by using RTDE a great Guide for this is written by mark Jensen

link to this How to