Universal Robots Forum

Check socket open / event socket closed


I have a robot program where a thread constantly waits for messages from a pc with the socket_read_ascii_float command. Therefore I created a socket connection to the pc which is permanently open.

Is there a possibility to check wether the socket connection is closed after the timeout of socket_read_ascii_float? In that case it shall reopen the connection, send some string to the pc and wait again for an ascii_float (it should not send the string if timeout but still connected).

The only thing I’m missing is how to detect if a socket connection is still open or closed. Im guessing that socket_open() isn’t the right function because it establishes a new connection but doesn’t check if the connection is already opened. Am I missing something?

Thank you in advance.

You can assign a variable to the socket connection. When connection times out then the variable will turn to false that’s when you can have it reconnect.

Here is an example I wrote up real quick

You can also see this used here https://www.universal-robots.com/how-tos-and-faqs/how-to/ur-how-tos/ethernet-socket-communication-via-urscript-15678/

1 Like

Thank you for your answer.

I don’t know if I get it right. In your example, what happens when the socket is open? “open” gets True and the loop will be never called again (if robot program is looping). Now what happens if the other pc closes the connection? In my opinion “open” doesn’t get False. I tested it.

Your solution is perfect for the start of the program, but what happens if the connection gets lost afterwards?

Any answer is appreciated.

Sorry the loop should be loop open = false. When open turns false, hence the connection was closed either by the other PC or the UR, then open will turn to false. Once false, loop until the connection is true (open is true).

Hello @roman.reiner,
did you tested if this case will work in cases where the other side closes connection in the middle of excahning data ?

In my opinion this is only good to be used for initial setting up of the connection. I have tested and it doesn t register sudden close of the connection or network interuption.

I believe @fratz and as well my doubt is does the UR have some monitoring variable that can be attached to the opened socket listening if the communciation channel is open or not.



Try putting this into a thread, will run continuously in the background of main robotic program and trip almost immediately, then connection can be regained. It should work rather the host or client closed connection.

I have tried this, it doesn t work. This function only returns the result when you try to open the socket, it is not monitoring the connection chanel in time.

If I close the connection after processing the information and reopen when need to send again ti works and will return false in case connection is not established.

My goal was not to open and close every time I need to send something, but to open one time and monitor the status of the channel.