Read response from universal robot with a socket

Hi,

I’m trying to read the log from a universal robot E-series with a python socket. It connects with success with the robot and it receives information. The robot is running an easy UR-script and prints test constantly in the log.

The problem is that I receive a byte string, How can I decode that information to human-readable information?

Martijn

b'\x00\x00\x02\xa8\x10\x00\x00\x00/\x00\x00\x00\x00\x02!\xddU@\x01\x01\x01\x00\x00\x01\x00\x07\x00?\xe3333333?\xf0\x00\x00\x00\x00\x00\x00?\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfb\x01?\xd2\xef\n\xc0\x00\x00\x00?\xd2\xee5\xae\x88\xd6\xb0?\xd5F\xaf \x00\x00\x00?\xde\xa8\x01B?\x94{A\xcd\xc2\x8bA\xfd\x15O\xfd\xbf\xfc\xfc\xc5\xd1\x10\xb4`\xbf\xfc\xfc~\xabh\xb9\xd4\xbf\xd1\x95\xb1`\x00\x00\x00@8\x92\x01B?\xd1\xecA\xc9\xc2\x8bA\xf1\xb6\x97\xfd\xbf\xfe\x02dQ\x10\xb4`\xbf\xfe\x02HC8#x\xbf\xd6\x1am`\x00\x00\x00@\rw\x9bB>\x9a\xa0A\xcd\xe2\xb3B\x05\x8a\xee\xfd\xbf\xf0?NQ\x10\xb4`\xbf\xf0;!\xe4\x8c\xbe\xe8?\xe43\xd8\xe0\x00\x00\x00?3(\x01B?\xb33A\xeafbB\x0f\xa3}\xfd?\xf9\x14\x89`\x00\x00\x00?\xf9\x14?\x9d\xc4\x80\xe5\xbfb\x10l\xa0\x00\x00\x00\xbeSP\x01B?e`B\x04\x8fXB\x11\x82\xbf\xfd\xbf\xd7B\x0fDB\xd1\x80\xbf\xd7C\x13/\xb6[0?\xd8*\x1c@\x00\x00\x00?\x05\xcb\x01B?\x17\x8dA\xf4\xa3\xcfB\x00\xbb\xba\xfd\x00\x00\x00e\x04?\xe8\x80>\xa7s\xa2\xaf?\xb0\xb2w\xbb\x15\x85u?\xd2:\xa4\x16\xa0\xf9\x88\xc0\x06\x83\x9d\xf4\xf0\xf5\x7f?\xf6\x0cE\xfd\xe2\xd89\xbf\x82\xf4\x89\xd6\xb9\x7f\xe6\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\xa9\x99\x99\x99\x99\x99\x9a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x005\t?\xf8\'\xda\xee\x83\xcb\xaa\xc0\x1c\xb2(\xa0\xfb\xff\xd5\xbf\xe9\x93)\x1c\x1b\x96#?\xfcUK\xc1\x84|\xfe?\xfb:\xb3\xc5\x15\xc3C?\xcc\x92\xb1\xf0\xc4*r\x00\x00\x00J\x03\x00\x00\x00\x00\x00\x00\x01\x00\x01\x01?p\x7f\xc0\x00\x00\x00\x00?p\x7f\xc0\x00\x00\x00\x00\x00\x00?pbM\xe0\x00\x00\x00?pbM\xe0\x00\x00\x00A\xeb\xa0\x00B@G\x19?e*\x99>\\\x82\xd3\x01\x00\x00\x88\xce\x84\x9c\x00\x00\x00\x00\x00%\x02\x01\x01?\xa8\x10\xc9\xe0\x00\x00\x00?\xa5\xd5\x97\x00\x00\x00\x00C\xe2\x9c\x05\x00;\xc4\x9b\xa6B4\x00\x00\xfd\x00\x00\x00=\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\xb0\x1c\xc06i\xb3\x97\x00\x00\x00\x08\x08\x00\x01\x00\x00\x00\x00+\n\x88\xce\x84\x9c\x00\x00?\xc1\xe0\xdeV\xa5\xb6y?\xa5\xcb\xd3"+\xdbP?\xe7\x17\x8e\x17_ ,\x00\x00\x00\x00\x00\x00\x00\x00'
    import socket    
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    ip=socket.gethostbyname("192.168.0.2")
    port=30002
    address=(ip,port)
    client.connect(address)
    while True:
        data = client.recv(1024)
        print(data)

You have to decode a byte first with .decode(‘encoding set’), judging by your byte string im gona gues its utf, wich would be decoded with .decode(‘utg-8’). Try the following code sample, and let me know if it works:

import socket    
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip=socket.gethostbyname("192.168.0.2")
port=30002
address=(ip,port)
client.connect(address)
while True:
    data = client.recv(1024)
    string = str(data.decode('utf-8'))
    print(string)

Hey Martijn,

You could look at the following repo for some code that already connects to the UR:

Also the page below has some interesting information about using TCP for communication with the UR robot:
http://www.zacobria.com/universal-robots-zacobria-forum-hints-tips-how-to/script-via-socket-connection/

# Echo client program
import socket
HOST = “192.168.0.9″    # The remote host
PORT = 30002              # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send (“set_digital_out(2,True)” + “\n”)
data = s.recv(1024)
s.close()
print (“Received”, repr(data))