TCP/IP Socket connection

UR5e, URSoftware 5.5.1, Python 3

Hello,

I am currently dealing with the TCP/IP socket connection. I have been able to establish a connection between the UR5e and my laptop via TCP/IP socket. I was able to turn signals on and off, as well as drive to positions.

import socket

HOST = “”
PORT = 30002
ADDR = (HOST, PORT)

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

Befehl = “set_digital_out(0,True)” + “\n”
#Befehl = “set_digital_out(0,False)” + “\n”
msg = Befehl.encode (“utf8”)
s.send (msg)

s.close()

My next step was to get data from the robot. Here I tried to get the weight of the lifted part by using the force() function through repr(data). The result of this, for me an arbitrary looking data stream in hex.

import socket
import time
HOST = “”
PORT = 30002
ADDR = (HOST, PORT)
FORMAT = ‘utf-8’

Ort = “movej(p[-0.425, 0.075, 0.4, 2.0, -2.4, -0.019], a=1.0, v=0.1)” + “\n”

print(“Programm wird gestartet!”)

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

s.send(Ort.encode(FORMAT))
time.sleep(4)
s.send((“set_tool_digital_out(0,True)” + “\n”).encode(FORMAT))
time.sleep(2)
s.send((“force()” + “\n”).encode(FORMAT))

DATA = s.recv(1024)
print(“The weight is”, repr(DATA))

time.sleep(2)

s.send((“set_tool_digital_out(0,False)” + “\n”).encode(FORMAT))

s.close()

Answer:

b’\x00\x00\x007\x14\xff\xff\xff\xff\xff\xff\xff\xff\xfe\x03\tURControl\x05\x05\x00\x00\x00\x00\x00\x00\x00\x0028-08-2019, 08:56:36\x00\x00\x00\x18\x14\xff\xff\xff\xff\xff\xff\xff\xff\xfe\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x05j\x10\x00\x00\x00/\x00\x00\x00\x00\x01\xbd\ …

Then I tried to decode this data stream, but I got back an arbitrary number, when I asked repeatedly. I also tried different ports.

import socket
import time
import struct
from codecs import encode, decode
from pip._vendor.html5lib.constants import voidElements

HOST = “”
PORT = 30002
ADDR = (HOST, PORT)
FORMAT = ‘utf8’

print(“Programm wird gestartet!”)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(ADDR)

time.sleep(1)
for x in range(0, 5):
DATA = s.recv(8)
DATA = encode(DATA, “hex”)
x = struct.unpack(’!d’, decode(DATA,‘hex’))[0]
print(x)

s.close()
print(“Programmende”)

Answer:

1.1688383847e-312
nan
1.022634920301348e+103
2.2113118192368592e+212
1.06565439285e-312

Now to my questions:

  1. what exactly do I get back? (State data of the robot?)
  2. is it possible to filter out the force data from the data stream? So how do I decode the result?

HI
Have you tried converting it from a ascii/ bytes value instead of a hex.
I gather its a float value you should be recieving

For this type of application we would recommend utilizing the RTDE interface. While it requires a recipe setup (a small xml file), it eliminates the need for parsing through the Datastream. Take a look at our knowledge article below for a list of controller inputs/outputs that can be used, as well as a couple pieces of example code:
https://www.universal-robots.com/articles/ur/real-time-data-exchange-rtde-guide/

Check this: RTDE C++ Interface. Maybe it will be helpful for you.

In addition, this library implements RTDE in .NET (C# / VB), Python, MATLAB and LabVIEW