Dashboard Communication using Python problem

I pulled the following python example from somewhere online:

import socket
import time

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
s.connect((‘192.168.216.129’, 29999))

def sendCommand(cmd):
cmd = 'def program():\n ’ + cmd + ‘\nend\n’
print(cmd)
s.sendall(cmd.encode())
time.sleep(5)

while (1):
sendCommand(‘play’)
sendCommand(‘pause’)

The program works correctly and alternates between playing and pausing on the robot.

I want also use dashboard communication to get the status fo the robot.

If I send programState\n to the robot using the program Packet Sender, I get a proper response back.

Trying to add this tom my example, I changed the line sending the request to the robot:

robotresponse = s.sendall(cmd.encode())

thinking the response would come back from the robot and be stored in the robotresponse variable.

robotresponse = s.sendall(cmd.encode())
time.sleep(2)
print(robotresponse)

This does not work.

It just prints None and then cycles through the loop again.

Any ideas ?

Thanks,
Mark

Found the answer after looking at some other example code. This works:

import socket
import time

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
s.connect((‘192.168.216.129’, 29999))

def sendCommand(cmd):
cmd = cmd + ‘\n’
print(cmd)
s.sendall(cmd.encode())
time.sleep(5)
rcvd = s.recv(4096)
print(rcvd)

while (1):
sendCommand(‘programState’)

Mark

3 Likes

Hi @mchollett,

Glad to see you’ve reached a solution that works.

Just to clear up why the first sample didn’t work - it’s a mix up between what you would send to the client interfaces (receive urscript commands on ports 30001-30003) and the dashboard server (receive plain text commands on port 29999), and also not receiving any data from the socket for a reply.

The def program() part is URScript that you would use when sending a whole program to one of the client interface ports, much like that shown here:

https://www.universal-robots.com/how-tos-and-faqs/how-to/ur-how-tos/secondary-program-17257/

This is not necessary for the dashboard server. I would assume the reason the play and pause worked is because the \n newline characters meant the dashboard server interpreted it as 3 separate commands and only recognized the middle part.

So as you’ve discovered just sending the dashboard commands listed at the link below with \n newline at the end is sufficient for them to be received by the dashboard server. The .encode() encoding of the string is also not necessary.

https://www.universal-robots.com/how-tos-and-faqs/how-to/ur-how-tos/dashboard-server-cb-series-port-29999-15690/

Then if you want to actually get some feedback from the you need to receive from the socket as you have done with rcvd = s.recv(4096). I think the recv command will also wait until it receives something, so the sleep command after it shouldn’t be necessary either.

Andrew

1 Like