Universal Robots Forum

How to check Socket connection and reconnect if sloses


WE open our socket in our before start, and i can’t figure out how to make sure the connection has not dropped and if so reconnect or spit out a popup and stop the robot if it fails to connect.

The reason for it, is that it relies on being connected to the pc at all times to take results from measuring a part on a gauge, as the computer sends a code to the cobot to tell which tray the part belongs in.

We are a bearing company that codes our bearings we have the operation working, just that when the connection drops it puts the parts into a error box.

we want to prevent that from happening.

I’ve looked at a couple of looping examples but, tried and don’t seem to work in our situation

Here is some snippets from our program showing where we are executing the socket send and socket read


Try this “variable = socket_open(xxx)” and in the program if that variable equals false you need to reconnect. We usually put an if statement at the beginning of loop or in a thread.

1 Like

The idea is good - except UR doesn’t monitor that really nice - i wouldn’t say that is almost impossible to get the real time/date from a robot in to a variable or somethin, except if you are processing it from an external device …
In this example I’ve dropped the connection and changed the port - UR didn’t know that :slight_smile:

We went the long way route and now just open and close it when require its solved our problem

I had the same problem. My solution is to use another Thread to monitor results of socket_read() function. If result is invalid - for example [0,-1,-1,-1] then you should handle it properly (prevent UR from moving), wait some time and try to open socket again.

I don’t have access to my code right now, but it is something like this.

Thread 1
If data_valid
data= socket_read_binary_integer(5)

Thread 2

if data[0] =? 0 and not data[1] = 12345

I use variable 12345 as control sum to be sure i dont mess something with my data sending order in the other device conncected.

I hope it will help you.

if data[0] =? 0 or not data[1] = 12345