0
$\begingroup$

I want to create a functionally equivalent C++ file to the Python top_block.py file that is generated by GRC when executing a (blank example) flow chart.

I know that C++ can be used to define blocks specifically, and so I would assume that by creating a C++ file and including and using the methods from defined blocks through the relevant header files would allow for, essentially, the creation of GNURadio programs without the use of GRC or Python.

Has anyone created similar programs to this? Is there any chance I can make GRC generate such a file, or can someone point me to reference programs / documentation?

At it's core my question is how would I make the following Python file in C++?

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: Top Block
# Generated: Wed Oct 16 17:45:58 2019
##################################################

if __name__ == '__main__':
    import ctypes
    import sys
    if sys.platform.startswith('linux'):
        try:
            x11 = ctypes.cdll.LoadLibrary('libX11.so')
            x11.XInitThreads()
        except:
            print "Warning: failed to XInitThreads()"

from PyQt4 import Qt
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from optparse import OptionParser
import sys


class top_block(gr.top_block, Qt.QWidget):

    def __init__(self):
        gr.top_block.__init__(self, "Top Block")
        Qt.QWidget.__init__(self)
        self.setWindowTitle("Top Block")
        try:
            self.setWindowIcon(Qt.QIcon.fromTheme('gnuradio-grc'))
        except:
            pass
        self.top_scroll_layout = Qt.QVBoxLayout()
        self.setLayout(self.top_scroll_layout)
        self.top_scroll = Qt.QScrollArea()
        self.top_scroll.setFrameStyle(Qt.QFrame.NoFrame)
        self.top_scroll_layout.addWidget(self.top_scroll)
        self.top_scroll.setWidgetResizable(True)
        self.top_widget = Qt.QWidget()
        self.top_scroll.setWidget(self.top_widget)
        self.top_layout = Qt.QVBoxLayout(self.top_widget)
        self.top_grid_layout = Qt.QGridLayout()
        self.top_layout.addLayout(self.top_grid_layout)

        self.settings = Qt.QSettings("GNU Radio", "top_block")
        self.restoreGeometry(self.settings.value("geometry").toByteArray())

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 32000

        ##################################################
        # Blocks
        ##################################################

    def closeEvent(self, event):
        self.settings = Qt.QSettings("GNU Radio", "top_block")
        self.settings.setValue("geometry", self.saveGeometry())
        event.accept()


    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate


def main(top_block_cls=top_block, options=None):

    from distutils.version import StrictVersion
    if StrictVersion(Qt.qVersion()) >= StrictVersion("4.5.0"):
        style = gr.prefs().get_string('qtgui', 'style', 'raster')
        Qt.QApplication.setGraphicsSystem(style)
    qapp = Qt.QApplication(sys.argv)

    tb = top_block_cls()
    tb.start()
    tb.show()

    def quitting():
        tb.stop()
        tb.wait()
    qapp.connect(qapp, Qt.SIGNAL("aboutToQuit()"), quitting)
    qapp.exec_()


if __name__ == '__main__':
    main()

Apologies as I am unsure if this question is better suited here or on stack overflow, feel free to mark it for migration if it is not appropriate here!

$\endgroup$
  • $\begingroup$ Hi! Not so sure this isn't more of a programming question than a signal processing problem, but sure: gr::top_block is, like everything else, a C++ class. Its usage in C++ is pretty similar to in Python. (the python things are mostly just automatically wrapped C++.) Learning to use Qt will, however, take a bit of effort. $\endgroup$ – Marcus Müller Oct 17 at 19:32
  • $\begingroup$ by the way, if you have a current (as in: 3.8, or mostly in 3.7.13.5), GRC can generate C++ source code – by far not for all blocks, but hey, you asked for a template :) $\endgroup$ – Marcus Müller Oct 17 at 19:42
  • $\begingroup$ However: You don't need GRC to execute this python file you're showing, and implementing the same in C++ has zero performance advantage. I'd recommend joining the GNU Radio discuss mailing list and explaining your use case there. $\endgroup$ – Marcus Müller Oct 17 at 19:42
  • $\begingroup$ Hi Marcus, thanks for your comments! What you've said makes sense, and I can generate C++ code blocks using modtool, but I guess I am asking if there is an accepted way to run a C++ code block without converting it to a Python script or yaml? My use case is that I wish to get the output of a flowgraph from an external C++ program by calling some run/execute function without having to use up memory overhead from running programs in C++ and Python simultaneously. I will do as you suggest and ask on the mailing list! Thanks again! $\endgroup$ – madmissmolly Oct 18 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.