# Additional code to GRC flowgraph

**URL:** <https://www.ruby-forum.com/t/additional-code-to-grc-flowgraph/242573>\
**Category:** GNU Radio\
**Created:** [July 3, 2015, 4:49pm UTC](https://www.ruby-forum.com/t/additional-code-to-grc-flowgraph/242573 "2015-07-03T16:49:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [July 3, 2015, 4:49pm UTC](https://www.ruby-forum.com/t/additional-code-to-grc-flowgraph/242573/1 "2015-07-03T16:49:17Z")

</div>

Hi

I’m working with a receiver application in GNU Radio Companion. I would  
like to add some additional code to the top block but I still want to be  
able to work with the GRC. Is there a way to add this extra code without  
the code will be removed when I re-generate the flowgraph from GRC  
or  
Could I create a new block that can access the variables in the top  
block or can execute the set/get functions for the variables in the top  
block.

```
What I'm trying to accomplish is that every 3rd second I will check 

```

the SNR and then do som trimming of a freq-offset. I’m a newbie with GNU  
Radio and Python but have read through the guided tutorials.

```
BR
Daniel Brogren
```

---

<div class="post-metadata">

**Author:** ![Daniel\_Brogren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@Daniel\_Brogren](https://www.ruby-forum.com/u/Daniel_Brogren)\
**Post date:** [July 3, 2015, 6:47pm UTC](https://www.ruby-forum.com/t/additional-code-to-grc-flowgraph/242573/2 "2015-07-03T18:47:17Z")

</div>

Hi Daniel,

if your code really just looks like

from awesometoolkit import skynet  
skynet.kill\_sarah()

you can put that into an “import” block.

But: what you describe doesn’t sound like you want to modify the top  
block – it sounds more like you’d want a block to calculate SNR, and  
every f\_sample\*3 samples change some other block.

Now, this completely depends on the block where the freq. offset is  
used, but you can just write a block (in python or C++, only matters for  
performance), and give that block an message output port, and send  
messages containing the changed parameter to the block that needs  
adjustment.

If you take a look at the frequency xlating fir filter block, it has a  
message port named “freq”, which takes in PMT messages in form of  
tuples; these tuples must be of the form  
pmt.pair(pmt.intern(“freq”), pmt.from\_double(123.131) )

Best regards,  
Marcus

---

<div class="post-metadata">

**Author:** ![Daniel\_Brogren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@Daniel\_Brogren](https://www.ruby-forum.com/u/Daniel_Brogren)\
**Post date:** [July 6, 2015, 9:34am UTC](https://www.ruby-forum.com/t/additional-code-to-grc-flowgraph/242573/3 "2015-07-06T09:34:35Z")

</div>

Hi again

I have a MPSK SNR Estimator Probe and a Function Probe that calculates  
the SNR.

I can not find any information regarding “import” block.

BR  
Daniel Brogren

---

<div class="post-metadata">

**Author:** ![Daniel\_Brogren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@Daniel\_Brogren](https://www.ruby-forum.com/u/Daniel_Brogren)\
**Post date:** [July 6, 2015, 9:44am UTC](https://www.ruby-forum.com/t/additional-code-to-grc-flowgraph/242573/4 "2015-07-06T09:44:31Z")

</div>

Try the block search (magnifier button in GRC), type in “import” 🙂

---

<div class="post-metadata">

**Author:** ![Daniel\_Brogren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@Daniel\_Brogren](https://www.ruby-forum.com/u/Daniel_Brogren)\
**Post date:** [July 6, 2015, 3:33pm UTC](https://www.ruby-forum.com/t/additional-code-to-grc-flowgraph/242573/5 "2015-07-06T15:33:53Z")

</div>

As I said, if that’s the case, avoid using function probes but just use  
the MPSK estimator (not the estimator probe), and write your own block  
that just takes the tags that come from that, and translates them to  
messages that you send to another block that does something useful with  
them (e.g. adjusting a frequency correction etc.)

Best regards,

Marcus

---

<div class="post-metadata">

**Author:** ![Daniel\_Brogren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@Daniel\_Brogren](https://www.ruby-forum.com/u/Daniel_Brogren)\
**Post date:** [July 6, 2015, 12:12pm UTC](https://www.ruby-forum.com/t/additional-code-to-grc-flowgraph/242573/6 "2015-07-06T12:12:19Z")

</div>

Sorry for for that… I thought the “imoprt” block was a type of block  
that I could create from the modtool.

but

In the import-block I can import modules. But I can still not import  
actuall code into my top\_block and I can not call functions from the  
import-block. What I want to avoid is to add code in the top\_block  
manually since I’m normally using the GRC to generate the top\_block.

/Daniel

Date: Mon, 6 Jul 2015 09:43:24 +0200  
From: removed\_email\_address@domain.invalid  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Discuss-gnuradio] Additional code to GRC flowgraph

```
Try the block search (magnifier button in GRC), type in "import" :)

On 07/06/2015 09:33 AM, Daniel Brogren
  wrote:

  Hi again

    I have a MPSK SNR Estimator Probe and a Function Probe that
    calculates the SNR.

    I can not find any information regarding "import" block.

    BR

    Daniel Brogren

    > Date: Fri, 3 Jul 2015 18:46:12 +0200

      > From: removed_email_address@domain.invalid

      > To: removed_email_address@domain.invalid

      > Subject: Re: [Discuss-gnuradio] Additional code to GRC
      flowgraph

      >

      > Hi Daniel,

      >

      > if your code really just looks like

      >

      > from awesometoolkit import skynet

      > skynet.kill_sarah()

      >

      > you can put that into an "import" block.

      >

      > But: what you describe doesn't sound like you want to
      modify the top

      > block -- it sounds more like you'd want a block to
      calculate SNR, and

      > every f_sample*3 samples change some other block.

      >

      > Now, this completely depends on the block where the freq.
      offset is

      > used, but you can just write a block (in python or C++,
      only matters for

      > performance), and give that block an message output port,
      and send

      > messages containing the changed parameter to the block
      that needs

      > adjustment.

      >

      > If you take a look at the frequency xlating fir filter
      block, it has a

      > message port named "freq", which takes in PMT messages in
      form of

      > tuples; these tuples must be of the form

      > pmt.pair(pmt.intern("freq"), pmt.from_double(123.131) )

      >

      >

      > Best regards,

      > Marcus

      > On 07/03/2015 04:27 PM, Daniel Brogren wrote:

      > > Hi

      > >

      > > I'm working with a receiver application in GNU Radio
      Companion. I would like to add some additional code to the top
      block but I still want to be able to work with the GRC. Is
      there a way to add this extra code without the code will be
      removed when I re-generate the flowgraph from GRC

      > > or

      > > Could I create a new block that can access the
      variables in the top block or can execute the set/get
      functions for the variables in the top block.

      > >

      > > What I'm trying to accomplish is that every 3rd
      second I will check the SNR and then do som trimming of a
      freq-offset. I'm a newbie with GNU Radio and Python but have
      read through the guided tutorials.

      > >

      > > BR

      > > Daniel Brogren

      > > _______________________________________________

      > > Discuss-gnuradio mailing list

      > > removed_email_address@domain.invalid

      > >
      https://lists.gnu.org/mailman/listinfo/discuss-gnuradio

      >

      >

      > _______________________________________________

      > Discuss-gnuradio mailing list

      > removed_email_address@domain.invalid

      > https://lists.gnu.org/mailman/listinfo/discuss-gnuradio

  _______________________________________________

```

Discuss-gnuradio mailing list  
removed\_email\_address@domain.invalid  
[https://lists.gnu.org/mailman/listinfo/discuss-gnuradio](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)

---

<div class="post-metadata">

**Author:** ![Daniel\_Brogren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@Daniel\_Brogren](https://www.ruby-forum.com/u/Daniel_Brogren)\
**Post date:** [July 6, 2015, 3:52pm UTC](https://www.ruby-forum.com/t/additional-code-to-grc-flowgraph/242573/7 "2015-07-06T15:52:35Z")

</div>

One way to do this is to create a flowgraph, such as foo.grc. This will  
generate a Python file that you can import into another file, bar.py:

#!/usr/bin/env python2

import foo  
if **name** == ‘ **main** ’:  
tb = foo.foo()

# custom code

```
print "samp_rate:", tb.get_samp_rate()

# standard stuff
tb.start()
try:
    raw_input('Press Enter to quit: ')
except EOFError:
    pass
tb.stop()
tb.wait()

```

You can add custom stuff into bar.py while being able to modify foo in  
GRC. I’m not sure if this applies directly to what you want to do, but  
I’ve found it useful for testing while avoiding having GRC overwrite  
your custom code.

Of course, another way to do this is to export your flowgraph as a  
hierarchical block, which you then import into a top level flowgraph.  
Your top level can stay the same but you can keep iterating on the  
hierarchical block.

Sean

From: [discuss-gnuradio-bounces+sean.nowlan=removed\_email\_address@domain.invalid](mailto:discuss-gnuradio-bounces+sean.nowlan=removed_email_address@domain.invalid)  
[[mailto:discuss-gnuradio-bounces+sean.nowlan=removed\_email\_address@domain.invalid](mailto:discuss-gnuradio-bounces+sean.nowlan=removed_email_address@domain.invalid)] On  
Behalf Of Marcus M.  
Sent: Monday, July 06, 2015 9:33 AM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Discuss-gnuradio] Additional code to GRC flowgraph

As I said, if that’s the case, avoid using function probes but just use  
the MPSK estimator (not the estimator probe), and write your own block  
that just takes the tags that come from that, and translates them to  
messages that you send to another block that does something useful with  
them (e.g. adjusting a frequency correction etc.)

Best regards,

Marcus  
On 07/06/2015 12:11 PM, Daniel Brogren wrote:  
Sorry for for that… I thought the “imoprt” block was a type of block  
that I could create from the modtool.

but

In the import-block I can import modules. But I can still not import  
actuall code into my top\_block and I can not call functions from the  
import-block. What I want to avoid is to add code in the top\_block  
manually since I’m normally using the GRC to generate the top\_block.

/Daniel

* * *

Date: Mon, 6 Jul 2015 09:43:24 +0200  
From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Discuss-gnuradio] Additional code to GRC flowgraph

Try the block search (magnifier button in GRC), type in “import” 🙂  
On 07/06/2015 09:33 AM, Daniel Brogren wrote:  
Hi again

I have a MPSK SNR Estimator Probe and a Function Probe that calculates  
the SNR.

I can not find any information regarding “import” block.

BR  
Daniel Brogren

> skynet.kill\_sarah()  
> messages containing the changed parameter to the block that needs  
> On 07/03/2015 04:27 PM, Daniel Brogren wrote:
> 
> > * * *
> > 
> > Discuss-gnuradio mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [Discuss-gnuradio Info Page](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)
> 
> * * *
> 
> Discuss-gnuradio mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [Discuss-gnuradio Info Page](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)

* * *

Discuss-gnuradio mailing list

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> **[Discuss-gnuradio Info Page](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)**

\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ Discuss-gnuradio mailing  
list [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[Discuss-gnuradio Info Page](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)

* * *

Discuss-gnuradio mailing list

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> **[Discuss-gnuradio Info Page](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)**
