# Adding python block to gnuradio

**URL:** <https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371>\
**Category:** GNU Radio\
**Created:** [March 13, 2013, 12:01pm UTC](https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371 "2013-03-13T12:01:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrashi12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mrashi12](https://www.ruby-forum.com/u/mrashi12)\
**Post date:** [March 13, 2013, 12:01pm UTC](https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371/1 "2013-03-13T12:01:35Z")

</div>

i made python block and want to add it to module in gnuradio, i open the  
directory of the module and use gr\_motool add -t source filename.py. the  
tool created the .cc file and .h but as templates no processing done in  
the files.  
so can anyone advise me to add python block?

---

<div class="post-metadata">

**Author:** ![mrashi12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mrashi12](https://www.ruby-forum.com/u/mrashi12)\
**Post date:** [March 13, 2013, 12:19pm UTC](https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371/2 "2013-03-13T12:19:43Z")

</div>

On Wed, Mar 13, 2013 at 5:57 AM, Mohammed R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> i made python block and want to add it to module in gnuradio, i open the  
> directory of the module and use gr\_motool add -t source filename.py. the  
> tool created the .cc file and .h but as templates no processing done in the  
> files.  
> so can anyone advise me to add python block?

There’s a great tutorial here:  
[http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules](http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules)

The gr\_modtool work flow is that gr\_modtool generates a lot of the  
required  
boilerplate code used by GR and then you edit those files with your  
signal  
processing code. Notice there’s section on python blocks in the tutorial  
(Tutorial #3).

It sounds like you’ve already got your signal processing code done. I  
would  
suggest copying what you have in to the boiler plate code generated by  
gr\_modtool. An alternative is importing what you already have and edit  
the  
work: function to call what you already have.

Make sure you tell gr\_modtool that you want to generate a python block  
with  
the -l switch. Going through that tutorial will be the single most  
useful  
thing in learning how to make a new block.

---

<div class="post-metadata">

**Author:** ![mrashi12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mrashi12](https://www.ruby-forum.com/u/mrashi12)\
**Post date:** [March 13, 2013, 12:31pm UTC](https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371/3 "2013-03-13T12:31:25Z")

</div>

Hi Mohammed,

Nathan just beat me to it and as he said, this link explains more about  
gr\_modtool:

[http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules](http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules)

gr\_modtool makes the template for you to paste your code into.

Here’s my methodology on how I do it:

1. $ gr\_modtool newmod  
(type in your project name)

2. $ cd gr-

3. $ gr\_modtool add  
(you need to make a dummy c++ block before you can build the project and  
therefore to make a python template. pick ‘sync’ without any qa files.  
see:  
[http://gnuradio.org/redmine/issues/522](http://gnuradio.org/redmine/issues/522) for explanation)

4. $ gr\_modtool add -l python  
(this is where you make the python template, if in doubt pick the ‘sync’  
block)

5. $ mkdir build && cd build && cmake …/ && make  
(look at the errors and you will see that the ‘\<+’ and ‘+\>’ need to be  
removed from the files in order for it to compile. When make works,  
‘sudo  
make install’ will put your new project into action. don’t forget to  
reload  
GRC or hit the blue refresh button at the top. the GRC templates need  
to  
be edited too in order for them to turn into GRC icons)

Once you can compile the templates successfully, modify the files that  
gr\_modtool created by pasting in your code. Good luck 🙂

Mike

–  
Mike J. M0MIK BSc  
Radio Communications Technology Specialist  
Email: removed\_email\_address@domain.invalid  
Web: [http://www.scanoo.com](http://www.scanoo.com)

---

<div class="post-metadata">

**Author:** ![mrashi12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mrashi12](https://www.ruby-forum.com/u/mrashi12)\
**Post date:** [March 14, 2013, 8:59am UTC](https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371/4 "2013-03-14T08:59:37Z")

</div>

1. step:

bynkln@ubuntu:~$ gr\_modtool.py create qpsk  
Module directory is “./gr-qpsk”.  
Creating directory…  
Copying howto example…  
Unpacking…  
Replacing occurences of ‘howto’ to ‘qpsk’… Done.  
Use ‘gr\_modtool add’ to add a new block to this currently empty module.  
bynkln@ubuntu:~$ gr\_modtool.py add  
No GNU Radio module found in the given directory. Quitting.

1. step:

bynkln@ubuntu:~$ cd gr-qpsk

1. step:

bynkln@ubuntu:~/gr-qpsk$ gr\_modtool.py add  
Operating in directory .  
GNU Radio module name identified: qpsk  
Enter code type: sync  
Language: C++  
Enter name of block/code (without module name prefix): QPSK  
Block/code identifier: QPSK  
Enter valid argument list, including default arguments: int val1, double  
val2=0  
Add Python QA code? [Y/n] n  
Add C++ QA code? [Y/n] y  
Traversing lib…  
Adding file ‘QPSK\_impl.h’…  
Adding file ‘QPSK\_impl.cc’…  
Adding file ‘QPSK.h’…  
Adding file ‘qa\_QPSK.cc’…  
Adding file ‘qa\_QPSK.h’…  
Traversing swig…  
Editing swig/qpsk\_swig.i…  
Traversing grc…  
Adding file ‘qpsk\_QPSK.xml’…  
Editing grc/CMakeLists.txt…

1. step:

bynkln@ubuntu:~/gr-qpsk$ gr\_modtool.py add -l python  
Operating in directory .  
GNU Radio module name identified: qpsk  
Enter code type: sync  
Language: Python  
Enter name of block/code (without module name prefix): QPSK  
Block/code identifier: QPSK  
Enter valid argument list, including default arguments: int val1, double  
val2=0  
Add Python QA code? [Y/n] y  
Traversing python…  
Adding file ‘QPSK.py’…  
Adding Python QA…  
Adding file ‘qa\_QPSK.py’…  
Editing python/CMakeLists.txt…  
Traversing grc…  
Adding file ‘qpsk\_QPSK.xml’…  
Editing grc/CMakeLists.txt…

before 5th step i had removed \<+ your compan… +\> then

1. step:

bynkln@ubuntu:~/gr-qpsk$ mkdir build && cd build && cmake …/ && make  
– The CXX compiler identification is GNU 4.7.2  
– The C compiler identification is GNU 4.7.2  
– Check for working CXX compiler: /usr/bin/c++  
– Check for working CXX compiler: /usr/bin/c++ – works  
– Detecting CXX compiler ABI info  
– Detecting CXX compiler ABI info - done  
– Check for working C compiler: /usr/bin/gcc  
– Check for working C compiler: /usr/bin/gcc – works  
– Detecting C compiler ABI info  
– Detecting C compiler ABI info - done  
– Build type not specified: defaulting to release.  
– Boost version: 1.49.0  
– Found the following Boost libraries:  
– filesystem  
– system  
– Found PkgConfig: /usr/bin/pkg-config (found version “0.26”)  
– checking for module ‘gruel’  
– found gruel, version 3.6.4  
– Found GRUEL: /usr/local/lib/libgruel.so  
– checking for module ‘gnuradio-core’  
– found gnuradio-core, version 3.6.4  
– Found GNURADIO\_CORE: /usr/local/lib/libgnuradio-core.so  
– checking for module ‘cppunit’  
– found cppunit, version 1.12.1  
– Found CPPUNIT: /usr/lib/libcppunit.so;dl  
– Found SWIG: /usr/bin/swig2.0 (found version “2.0.7”)  
– Found PythonLibs: /usr/lib/python3.2/config/libpython3.2.so (found  
version “2.7.3”)  
– Found PythonInterp: /usr/bin/python (found version “2.7.3”)  
– Found Doxygen: /usr/bin/doxygen (found version “1.8.1.2”)  
– Configuring done  
– Generating done  
– Build files have been written to: /home/bynkln/gr-qpsk/build  
Scanning dependencies of target gnuradio-qpsk  
[5%] Building CXX object  
lib/CMakeFiles/gnuradio-qpsk.dir/QPSK\_impl.cc.o  
/home/bynkln/gr-qpsk/lib/QPSK\_impl.cc: In constructor  
gr::qpsk::QPSK\_impl::QPSK\_impl(int, double):  
/home/bynkln/gr-qpsk/lib/QPSK\_impl.cc:42:30: error: expected  
primary-expression before \< token  
/home/bynkln/gr-qpsk/lib/QPSK\_impl.cc:42:32: error: MIN\_IN was not  
declared in this scope  
/home/bynkln/gr-qpsk/lib/QPSK\_impl.cc:42:39: error: expected  
primary-expression before \> token  
/home/bynkln/gr-qpsk/lib/QPSK\_impl.cc:42:40: error: expected  
primary-expression before , token  
/home/bynkln/gr-qpsk/lib/QPSK\_impl.cc:42:42: error: expected  
primary-expression before \< token  
/home/bynkln/gr-qpsk/lib/QPSK\_impl.cc:42:44: error: MAX\_IN was not  
declared in this scope  
/home/bynkln/gr-qpsk/lib/QPSK\_impl.cc:42:51: error: expected  
primary-expression before \> token  
/home/bynkln/gr-qpsk/lib/QPSK\_impl.cc:42:52: error: expected  
primary-expression before , token  
/home/bynkln/gr-qpsk/lib/QPSK\_impl.cc:42:62: error: expected  
primary-expression before \< token  
/home/bynkln/gr-qpsk/lib/QPSK\_impl.cc:42:64: error: expected  
primary-expression before float  
/home/bynkln/gr-qpsk/lib/QPSK\_impl.cc:42:64: error: expected ) before  
float  
/home/bynkln/gr-qpsk/lib/QPSK\_impl.cc:67:3: error: expected { before }  
token  
make[2]: \*\*\* [lib/CMakeFiles/gnuradio-qpsk.dir/QPSK\_impl.cc.o] Error 1  
make[1]: \*\*\* [lib/CMakeFiles/gnuradio-qpsk.dir/all] Error 2  
make: \*\*\* [all] Error 2  
bynkln@ubuntu:~/gr-qpsk/build$

_then gived to me error like this, again… what shoul i do, thank you._

2013/3/13 Mike J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mrashi12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mrashi12](https://www.ruby-forum.com/u/mrashi12)\
**Post date:** [March 14, 2013, 8:29am UTC](https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371/5 "2013-03-14T08:29:49Z")

</div>

The best way to mke a new block is by following  
tutorial here:  
[http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules](http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules).  
you can easily make duumy block from this tutorial. i also made square  
dummy block from this tutorial.by making your block available in  
gnuardio you have to create the xml files whose procedure is also given  
in this tutorial.  
— On Wed, 3/13/13, Mohammed R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

From: Mohammed R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: [Discuss-gnuradio] adding python block to gnuradio  
To: removed\_email\_address@domain.invalid  
Date: Wednesday, March 13, 2013, 10:57 AM

i made python block and want to add it to module in gnuradio, i open the  
directory of the module and use gr\_motool add -t source filename.py. the  
tool created the .cc file and .h but as templates no processing done in  
the files.  
so can anyone advise me to add python block?

-----Inline Attachment Follows-----

---

<div class="post-metadata">

**Author:** ![mrashi12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mrashi12](https://www.ruby-forum.com/u/mrashi12)\
**Post date:** [March 14, 2013, 10:11am UTC](https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371/6 "2013-03-14T10:11:22Z")

</div>

How should i add or replace c++ code and python code could you explain  
it  
with an example, and i knew there is psk.py code but need to show it at  
the  
GRC as a block.

---

<div class="post-metadata">

**Author:** ![mrashi12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mrashi12](https://www.ruby-forum.com/u/mrashi12)\
**Post date:** [March 15, 2013, 3:28am UTC](https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371/7 "2013-03-15T03:28:09Z")

</div>

You simply open that file in text editor and change the lines that cause  
trouble.

---

<div class="post-metadata">

**Author:** ![mrashi12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mrashi12](https://www.ruby-forum.com/u/mrashi12)\
**Post date:** [March 14, 2013, 9:14am UTC](https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371/8 "2013-03-14T09:14:14Z")

</div>

On Thu, Mar 14, 2013 at 2:58 AM, Serhat BOYNUKALIN  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> bynkln@ubuntu:~$ gr\_modtool.py add  
> No GNU Radio module found in the given directory. Quitting.

As the error message indicates this last command didn’t do anything.

> Enter code type: sync  
> Adding file ‘qa\_QPSK.cc’…  
> bynkln@ubuntu:~/gr-qpsk$ gr\_modtool.py add -l python  
> Adding Python QA…  
> Adding file ‘qa\_QPSK.py’…  
> Editing python/CMakeLists.txt…  
> Traversing grc…  
> Adding file ‘qpsk\_QPSK.xml’…  
> Editing grc/CMakeLists.txt…
> 
> before 5th step i had removed \<+ your compan.. +\> then

From [http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules](http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules)  
The C++ code (part 1)

“gr\_modtool hints at where you have to change code by adding \<++\>  
symbols.”

> – Check for working C compiler: /usr/bin/gcc  
> – found gruel, version 3.6.4  
> – Found Doxygen: /usr/bin/doxygen (found version “1.8.1.2”)  
> /home/bynkln/gr-qpsk/lib/QPSK\_impl.cc:42:42: error: expected primary-expression  
> before \< token  
> bynkln@ubuntu:~/gr-qpsk/build$
> 
> then gived to me error like this, again… what shoul i do, thank you.

Look at those line numbers that give errors. You need to replace the  
“\<++ hint ++\>” with actual C++ code.

As a side note, unless you’re doing something fancy there’s already a  
QPSK modulator/demodulator that comes with GR as a python hier\_block.  
It’s in gr-digital/python/psk.py .

-Nathan

---

<div class="post-metadata">

**Author:** ![mrashi12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mrashi12](https://www.ruby-forum.com/u/mrashi12)\
**Post date:** [March 15, 2013, 3:34am UTC](https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371/9 "2013-03-15T03:34:35Z")

</div>

On Thu, Mar 14, 2013 at 4:27 AM, Serhat BOYNUKALIN  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> it is possible to add qpsk as a block to grc just using python code?

It’s already there. Search for PSK Modulator. M is the variable to  
change the modulation order. 4 will give you PSK.

> 2013/3/14 Nemanja S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> > How should i add or replace c++ code and python code could you explain it with  
> > an \>\> example

I can’t stress this enough.  
If you want to learn how to create a new block you should go through  
the tutorial.  
[http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules](http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules)

The tutorial shows you what the changed code should look like for a  
very simple block. It’s a very good tutorial and it won’t take you  
more than an hour, which is probably more time than you’ve spent  
trying to fix problems that you wouldn’t have if you go through the  
tutorial.

---

<div class="post-metadata">

**Author:** ![mrashi12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mrashi12](https://www.ruby-forum.com/u/mrashi12)\
**Post date:** [March 15, 2013, 3:40am UTC](https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371/10 "2013-03-15T03:40:26Z")

</div>

In the terminal transcript you provided, you created an block without a  
name, and after that you created a python block. One of the errors comes  
from the first block. I am not sure where you have found this sequence  
of  
actions, but the only thing you should to to create a new signal  
processing  
block is to firsr, define a module, which you have done, second, add a  
new  
signal processign block (in python or C++), EDIT the files created by  
gr\_modtool in order to implement the behaviour of your block, third: do  
make and make test to see whether your block really does what you  
wanted,  
make xml file for GRC and finaly do make install, and you will be able  
to  
use it.

On Thu, Mar 14, 2013 at 10:27 AM, Serhat BOYNUKALIN \<

---

<div class="post-metadata">

**Author:** ![mrashi12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mrashi12](https://www.ruby-forum.com/u/mrashi12)\
**Post date:** [March 15, 2013, 3:31am UTC](https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371/11 "2013-03-15T03:31:39Z")

</div>

it is possible to add qpsk as a block to grc just using python code?

2013/3/14 Nemanja S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mrashi12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mrashi12](https://www.ruby-forum.com/u/mrashi12)\
**Post date:** [March 15, 2013, 3:52am UTC](https://www.ruby-forum.com/t/adding-python-block-to-gnuradio/227371/12 "2013-03-15T03:52:07Z")

</div>

Ok, but just a little.

On Thu, Mar 14, 2013 at 10:47 AM, Serhat BOYNUKALIN \<
