Universal Robots+

Sending JSON with socket_send_string URScript command

Hi all! Maybe someone can help with this problem I currently have. Thanks!

I am trying to send a JSON string via socket_send_string URScript command. The URScript code is generated in an URCaps plugin. A simple example of the line generating the code in the URCaps:

writer.appendRaw("socket_send_string(\"{\"ProcessState\": 4}\", \"my_socket\")");

This produces a syntax error when trying to execute the URScript. The reason for this seems to be the inner quotation marks in the JSON string I am trying to send, as just sending \"{ProcessState: 4}\" works fine. Unfortunately I need these for proper JSON parsing. Is there a way to properly send JSON with this command (or a different URScript command)?

Hi Stefan,

in a similar case I sent my values to a python daemon which did the json job and the request.

You could convert your message to bytes and send it in a loop:
=> socket_send_byte(value, socket_name=’socket_0’)

or as byte string
=> socket_send_line(str, socket_name=’socket_0’)

On receiving side prepare your json. Iam not sure if you could escape characters in urscript

We send JSON strings via XMLRPC back to the server. What we had to do though was invert the quote structure and then we swap it on the server side before processing the now valid JSON. Basically you can use single quotes inside a string but in UR a string must be wrapped in double quotes so a json string would look something like this:

"{'key':numberValue, 'key:'stringValue'}"

Then on the server we changed the ' to " using a regex command then parsed the JSON and returned the result back to the calling function. Our XMLRPC server is running NodeJS so handling JSON is rather straightforward. We even have a function for taking in a series of keys and values as a number of parameters and returning a valid stringified JSON string that UR would accept, then we could keep adding values to the same string if we wanted.

So in your example we would have sent
writer.appendRaw("socket_send_string(\"{'ProcessState': 4}\", \"my_socket\")");

1 Like