# Reading Global Variables from Python project

**URL:** <https://forum.universal-robots.com/t/reading-global-variables-from-python-project/27664>\
**Category:** URScript\
**Created:** [March 29, 2023, 2:58pm UTC](https://forum.universal-robots.com/t/reading-global-variables-from-python-project/27664 "2023-03-29T14:58:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Berbatov98](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@Berbatov98](https://forum.universal-robots.com/u/Berbatov98)\
**Post date:** [March 29, 2023, 2:58pm UTC](https://forum.universal-robots.com/t/reading-global-variables-from-python-project/27664/1 "2023-03-29T14:58:03Z")

</div>

Hello everyone!  
I have a global variable called: Force\_Limit inside my program and I am trying to read its value from my Python program. I have created to following Python script:

 ![Python](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/3/3529b4218852f32860377f2a1ea6a6f68683f2c7.jpeg)  
It produces the following output:

the force limit is: b’\x00\x00\x007\x14\xff\xff\xff\xff\xff\xff\xff\xff\xfe\x03\tURControl\x03\x0f\x00\x00\x00\x08\x00\x00\x00\x0014-06-2022,

Is there a fault with my logic or am I simply using the wrong function to get the variable ? I would like to read this variable and get the integer value (also can anyone explain what is this protocol message I am getting ? and how do I decode it ?)

Thank you for your answers in advance.

---

<div class="post-metadata">

**Author:** ![smarmstr](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/smarmstr/32/4896_2.png) [@smarmstr](https://forum.universal-robots.com/u/smarmstr)\
**Post date:** [March 29, 2023, 5:21pm UTC](https://forum.universal-robots.com/t/reading-global-variables-from-python-project/27664/2 "2023-03-29T17:21:08Z")

</div>

There is a bit of a conceptual misunderstanding.

Universal Robots’ offers various methods for interfacing with external devices, and in this case, you are utilizing their primary client interface. In the given scenario, your Python code serves as the client, while the robot’s controller unit acts as the server. An internal process within the robot constantly listens for request messages on port 30002, enabling communication between the client and the server.

As you have figured out, the client can send URScript commands to the server for execution. Unfortunately, receiving data from the server with the client is not as simple; the approach on line 20 will not work. The server periodically sends messages containing robot data to all connected clients. These messages are transmitted as hexadecimal strings representing binary data about the robot’s state, which is what you are observing in your console. The variables coded into these messages are predefined as outlined in their [spec](https://s3-eu-west-1.amazonaws.com/ur-support-site/16496/ClientInterfaces_Primary.pdf). You would need to receive the entire message then decode it. I wrote [this](https://gist.github.com/Shawn-Armstrong/d10932fd992fd5493d93b83520698988) which has a few examples.

The URScript `socket_get_var()` is intended to be used inside a running robot program where the robot is the client and an external device is acting as the server. The robot’s program will say `x = socket_get_var("POS_X")` and the python server would respond something like `server_socket.sendall(b"POS_X 20\n")` which would store 20 into the robot’s variable x.

---

<div class="post-metadata">

**Author:** ![Berbatov98](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@Berbatov98](https://forum.universal-robots.com/u/Berbatov98)\
**Post date:** [March 30, 2023, 7:02am UTC](https://forum.universal-robots.com/t/reading-global-variables-from-python-project/27664/3 "2023-03-30T07:02:04Z")

</div>

Thank you for clearing it up quickly.
