Universal Robots+

Problem with socket_read_binary_integer() from python server

Hello,

I created a socket server in python and I would like to exchange an integer with the client.

I do not find the right syntax to send the number 3 (0b11) for example.

  • On the robot side (Client):

byte_int = [0,0] # init variable before start

byte_int = socket_read_binary_integer(1) # after socket connection

  • On the server side in python 3.6 I tried :

client.send("(1, 1)".encode(“utf-8”))
client.send("(True, True)".encode(“utf-8”))
client.send("(True, True)\n".encode(“utf-8”))
client.send(b"(True, True)\n")
client.send(b"[True, True]\n")

I make something wrong, I still have an incomprehensible returned huge value.

Thanks for your help !

Anyone could help me ? even if it’s another programming language.

Thanks

Can you try to use struct to pack and unpack the data that you are sending. Try to take a look at this stackoverflow solution with 11 likes where binary format is used - link. Please let me know if it helped.

Hello,

Thanks for your help, it was a packing/unpacking problem.

  • to send 32bits integers from python (server) to ur robot(client)

#Server in python
nb_1 = 3 # type int
nb_2 = 4
nb_1_byte = nb_1.to_bytes(4, ‘big’) # type bytearray - format 4 bytes = 4 * 8bits = 32 bits, big endian
nb_2_byte = nb_2.to_bytes(4, ‘big’)
msg_bytearray = nb_1_byte + nb_2_byte
client.send(msg_bytearray)

#Client robot
data = [0,0,0]
data = socket_read_binary_integer(2) # data = [2, 3, 4]

  • to send string from python to robot

#Server in python
msg = “texte”
client.send(msg.encode(“utf-8”))

#Client robot
data = “”
data = socket_read_string() # data = “texte”

  • to send floats from python to robot

#Server in python
floats = (6.2, 8.4)
floats_msg = floats + “\n”
client.send(floats_msg.encode(“utf-8”))

#Client robot
data = [0, 0.0, 0.0]
data = socket_read_ascii_float(2) # data = [2, 6.2, 8.4]

1 Like

Glad you found the solution ! :grinning:

Hi Florent, I am using Python 3.7, and had difficulty sending floats from the server in the method you suggested as it will throw back an error stating that it is a tuple and cannot be encoded.

My workaround to make it a string :slight_smile:

#Server in python
list = "(6.2, 8.4) "  #note the additional quotation marks
client.send(list.encode("utf-8"))
#client robot
data = [0,0.0,0.0]  #intialise value 
data = socket_read_ascii_float(2) #output data= [2, 6.2,8.4]