Home› Programming
Discussion
Back to discussions page
Tr1peiro
Posts: 4 Apprentice
How to create a socket_open to UR5 to read coordinates send by a PC |
213 views
|
Answered | |
/ Most recent by matthewd92
in Programming
|
9 comments |

in Programming
I was wondering if you cold help me in a proggram for the robot UR5, because I have been struggling in the creation of a socket/TCP that can run in the robot but at the same time can read the coordinates that have been sent by the computer.
I have already the code for user/server
already created but, the client program(URrobot) i can make a program that cand
read this coordinates and I have already tried to use the scripts
socket_read_interger and the socket_read_ascii_float and none of them work
Best Answer
-
matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,267 Handy
This is an example test socket that I created years ago to test some camera connection. You can see here how the return is formatted, this is the required format per the UR documentation. We read this as a string and then decoded it on the robot side since we wanted a mix of string and floats.import socketimport timehost = "127.0.0.1"port = 5001print("Program Start")tel = 0s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.bind((host, port))s.listen(1)c, addr = s.accept()while (tel < 10):try:msg = c.recv(1024)print(msg)# c.send("Messsage '"+msg+"' was received")c.send("Pass,2.33,5.55,ok")if msg == "Stop":tel = 10print(tel)except socket.error as socketError:tel = 10print(socketError)print(tel)c.close()s.close()print("Program Finished")
flag = False
sync_flag = False
open = False
while(open == False):
open = socket_open("192.168.1.6",5050)
end
# popup("Connected to server!")
while (sync_flag != True):
data = socket_read_ascii_float(6)
if (data[1] == 1):
flag = True
end
end
socket_close()
flag = False
end
test2()
from struct import pack
#Variaveis
S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
IP = "192.168.1.6"
PORTA = 5050
FORMAT = "utf-8"
server_address = (IP, PORTA)
S.bind(server_address)
S.listen(5)
connection, client_address = S.accept()
val = pack('i!', 4)
#Escolha das coordenadas
waypoint1 = input('Inserir coordenadas: ' + "\n")
waypoint2 = input('Inserir coordenadas: ' + "\n")
waypoint3 = input('Inserir coordenadas: ' + "\n")
waypoint4 = input('Inserir coordenadas: ' + "\n")
Ligar = input('Enviar coordenadas e ligaar Servidor? ')
#Decidir se conecto ao servidor ou não
if Ligar == ('yes' or 'Y'):
connection.send(val)
S.send(waypoint1.encode())
S.send(waypoint2.encode())
S.send(waypoint3.encode())
S.send(waypoint4.encode())
elif Ligar == ('no' or 'N'):
connection.close()
else:
print("tenta outra vez")
connection.close()"
Remember the data variable will be an array of length = 7, you are requesting 6 values which will be indices 1 to 6 and the robot will place the size of the resulting read in index 0, if the read fails you will get a 0 in that position, otherwise it will be the number of values that were returned.
My big trouble now is how i write the socket to connect the machine to the PC.....
# open = socket_open("192.168.1.7", 5050)
# movel(Plane_1, accel_mss, speed_ms, 0,0)
# sendToServer = 'Coordenadas: '
# socket_send_string(sendToServer)
# data_1 = socket_read_byte_list(8)
# if data[0]=8
# prog()