# How to process package recieved from port 30002 in Python

**URL:** <https://forum.universal-robots.com/t/how-to-process-package-recieved-from-port-30002-in-python/15627>\
**Category:** Technical Questions\
**Created:** [July 27, 2021, 3:21pm UTC](https://forum.universal-robots.com/t/how-to-process-package-recieved-from-port-30002-in-python/15627 "2021-07-27T15:21:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon63378011](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@anon63378011](https://forum.universal-robots.com/u/anon63378011)\
**Post date:** [July 27, 2021, 3:21pm UTC](https://forum.universal-robots.com/t/how-to-process-package-recieved-from-port-30002-in-python/15627/1 "2021-07-27T15:21:15Z")

</div>

HI!

I’ve recently been trying to receive robot readings from port 30002 using Sockets in Python. I’ve looked into how to process the package received in this thread here:

- [How to process package from robot controller](https://forum.universal-robots.com/t/how-to-process-package-from-robot-controller/390)

But the code examples posted are outdated and leaves me with errors.  
I’ve also looked into the client\_Interfaces.xlsx, but it seems to be outdated perhaps too?

Anyways, i am trying to receive readings on the Robot mode data, more specifically to check for protective stop.  
How would i go about processing the package with the Struct librabry in python 3?  
Or if someone could explain to me how to unpack the data stream from port 30002 that would be of great help too!

---

<div class="post-metadata">

**Author:** ![anon63378011](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@anon63378011](https://forum.universal-robots.com/u/anon63378011)\
**Post date:** [July 28, 2021, 12:28pm UTC](https://forum.universal-robots.com/t/how-to-process-package-recieved-from-port-30002-in-python/15627/2 "2021-07-28T12:28:28Z")

</div>

I have Solved the issue. If anyone stumbles upon the same problem, i would gladly share my implementation

---

<div class="post-metadata">

**Author:** ![ajp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/ajp/32/471_2.png) [@ajp](https://forum.universal-robots.com/u/ajp)\
**Post date:** [August 2, 2021, 4:47am UTC](https://forum.universal-robots.com/t/how-to-process-package-recieved-from-port-30002-in-python/15627/3 "2021-08-02T04:47:50Z")

</div>

Great to hear you’ve resolved it. Was it just a case of updating the indices to reflect the current data structure?

---

<div class="post-metadata">

**Author:** ![anon63378011](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@anon63378011](https://forum.universal-robots.com/u/anon63378011)\
**Post date:** [August 4, 2021, 10:21am UTC](https://forum.universal-robots.com/t/how-to-process-package-recieved-from-port-30002-in-python/15627/4 "2021-08-04T10:21:14Z")

</div>

Yes that was exactly the problem.

---

<div class="post-metadata">

**Author:** ![cags](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/cags/32/6304_2.png) [@cags](https://forum.universal-robots.com/u/cags)\
**Post date:** [December 16, 2021, 10:21am UTC](https://forum.universal-robots.com/t/how-to-process-package-recieved-from-port-30002-in-python/15627/5 "2021-12-16T10:21:05Z")

</div>

Hi, could you share it? I have been going through the same issue and I updated some indices, but it still does not work for me.  
A link to a repository in GitHub or similar would be great 😄

---

<div class="post-metadata">

**Author:** ![anon63378011](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@anon63378011](https://forum.universal-robots.com/u/anon63378011)\
**Post date:** [December 17, 2021, 11:11am UTC](https://forum.universal-robots.com/t/how-to-process-package-recieved-from-port-30002-in-python/15627/6 "2021-12-17T11:11:15Z")

</div>

It’s around 3 months since i’ve last used the script, so i quiet frankly don’t remember how exactly i got it to work. I don’t have an active repo with the code, but here is the code snippet.

As far as i can remember,it prints the 3 states; is\_program\_running, is\_protective\_stopped, is\_robot\_on.

```
    while cycles > count:
                print('\n')
                data = self.SOCKET.recv(4096)
                # size = sys.getsizeof(data)
                # print(size)
                i = 0
                package_length = (struct.unpack('!i', data[0:4]))[0]
                package_type = (struct.unpack('!B', data[4:5]))[0]
    
                if package_type == 16:
                    while i + 5 < package_length:
                        message_length = (struct.unpack('!i', data[5 + i:9 + i]))[0]
                        message_type = (struct.unpack('!B', data[9:10]))[0]
    
                        if message_type == 0:
                            sub_package = data[5:i + message_length]
                            is_program_running = struct.unpack('!?', sub_package[18:19])[0]
                            is_protective_stop = struct.unpack('!?', sub_package[17:18])[0]
                            is_robot_on = struct.unpack('!?', sub_package[15:16])[0]
    
                            print('Is robot on: ' + str(is_robot_on) + '\n' +
                                  'Is protective stopped: ' + str(is_protective_stop) + '\n' +
                                  'is program running: ' + str(is_program_running) + '\n' +
                                  'Count: ' + str(count) + '\n')
                            time.sleep(0.1)
                            count += 1
                            break
                        i = message_length + i
```
