# Writing packet payloads to file

**URL:** <https://www.ruby-forum.com/t/writing-packet-payloads-to-file/224565>\
**Category:** GNU Radio\
**Created:** [November 17, 2012, 12:29am UTC](https://www.ruby-forum.com/t/writing-packet-payloads-to-file/224565 "2012-11-17T00:29:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [November 17, 2012, 12:29am UTC](https://www.ruby-forum.com/t/writing-packet-payloads-to-file/224565/1 "2012-11-17T00:29:01Z")

</div>

Hello all,

I am researching a new coding algorithm. I am transmitting and coding a  
text file using payload\_tx.py and then write the binary of the packet  
payload to a file to do offline analysis.

I am having trouble writing packet payloads to file, however, because  
the I  
can’t seem to find a way to get data out of a gr\_message except for the  
to\_string() method. If I try and convert the string back to binary (my  
attempt involved python’s binascii module), I get an “Error: illegal  
char”  
message.

This makes sense, since the data is still coded, and should not be a  
valid  
ascii string. But how can I get around this problem and write binary to  
file?

---

<div class="post-metadata">

**Author:** ![Colleen\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@Colleen\_J](https://www.ruby-forum.com/u/Colleen_J)\
**Post date:** [November 20, 2012, 7:09pm UTC](https://www.ruby-forum.com/t/writing-packet-payloads-to-file/224565/2 "2012-11-20T19:09:12Z")

</div>

On Fri, Nov 16, 2012 at 6:28 PM, Colleen J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> message.  
> Class of 2013  
> Colleen,

Have you tried to use struck.unpack?

Tom

---

<div class="post-metadata">

**Author:** ![Colleen\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@Colleen\_J](https://www.ruby-forum.com/u/Colleen_J)\
**Post date:** [November 21, 2012, 8:22pm UTC](https://www.ruby-forum.com/t/writing-packet-payloads-to-file/224565/3 "2012-11-21T20:22:46Z")

</div>

Thanks for the response. I’m not sure I understand how struct.unpack  
will  
help, since the only data I can get seems to already be a python string.  
If  
I transmit a packet with “hello world” as my payload, the only thing I  
ever  
see in the receive script is the python string “hello world”. I don’t  
know  
how to access the intermediate binary representation.

---

<div class="post-metadata">

**Author:** ![Colleen\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@Colleen\_J](https://www.ruby-forum.com/u/Colleen_J)\
**Post date:** [November 24, 2012, 4:31pm UTC](https://www.ruby-forum.com/t/writing-packet-payloads-to-file/224565/4 "2012-11-24T16:31:54Z")

</div>

On Wed, Nov 21, 2012 at 2:21 PM, Colleen J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanks for the response. I’m not sure I understand how struct.unpack will  
> help, since the only data I can get seems to already be a python string. If  
> I transmit a packet with “hello world” as my payload, the only thing I ever  
> see in the receive script is the python string “hello world”. I don’t know  
> how to access the intermediate binary representation.

Colleen,

Take a look at how we handle this stuff in gr-digital. Specifically,  
in gr-digital/python/pkt.py, the method run() of \_queue\_watcher\_thread  
extracts the message contents as a string and passes it to  
packet\_utils.unmake\_packet (in packet\_utils.py). We have a bunch of  
helper functions in there to handle the string data (such as  
string\_to\_hex\_list and the like). Finally, the paylad (As a string) is  
passed up to the example scripts like  
gr-digital/examples/narrowband/benchmark\_rx.py where the rx\_callback  
metho uses struct.unpack to extract the packet number, which is  
non-ASCII.

Hopefully, following the way we handle the messages as strings and the  
various methods we use to manipulate them here will help you with your  
tasks.

Tom
