# Merging gnuradio generated python code into the Python web server

I am trying to figure out how I control a Software Defined Radio over a network, and stream the received baseband samples from my remote hardware back to the gnuradio.

What I'm trying to do here is, this Python code should be able to output a user selectable waveform which can be a CW tone, Broadband Noise, or an arbitrary waveform read from a file containing I/Q values.

So I searched on the internet and the closest thing that I found was the BorIP and unfortunately couldn't get it set-up on my machine because the instructions I found were not helpful (at least for a beginner like me).

I created the Python web server using this script (which I found online):

    import http.server
import socketserver

PORT = 8080
Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()


But didn't know how to merge the generated gnuradio python code into the Python web server.

GNU Radio Companion (GRC) generates Python code that is something like this (not exact text). (Make sure you chose the "No GUI" option in GRC.)

class my_block(gr.top_block):
# ...

def main():
tb = my_block()
tb.run()

if __name__ == '__main__':
main()


You can just import this as a module in your Python program (the if __name__ check will skip running the code that wouldn't be appropriate). Once you've done that, the way you use it is to just create the top block and call tb.start(). This will start the flow graph without also waiting for it to finish, which is exactly what you want when you have a separate main loop like a web server.

import my_block

tb = my_block.my_block()
tb.start()


When you're shutting down, do this to stop the GNU Radio threads:

tb.stop()
tb.wait()


Then you can also use the generated methods (a getter and setter per GRC "Variable") to change parameters while the flow graph is running. For example, if you have a "Variable" block in GRC with ID freq, then you can use freq in other blocks' parameters, and GRC will generate a getter and setter method like this:

def get_freq(self):
return self.freq

def set_freq(self, freq):
self.freq = freq
self.freq_xlating_fir_filter_xxx_0.set_center_freq(self.freq)


And so from your controlling application, you call these methods, like tb.set_freq(123e6) to change the frequency to 123 MHz, for example.

There's a lot more that can be said about how to go beyond what GRC generates for you (which would best be asked as separate questions), but this is how you get started with integrating a GNU Radio flow graph into a larger program.

I would recommend expecting to eventually stop using GRC's code generation and write your own Python code. This is because GRC has quite a few limitations in what you can do with it — for example, if you want to decide at runtime which type of signal source block to create, you can't do that in GRC but it's easy when you write your own Python. You can always use GRC to generate examples to copy from, when you're unsure how to configure a block.

• Thank you so much Kevin for taking time and answering my question. Again, I am a real begginer when it comes to python or programming, but definitely I will do my best to figure this out and what I can do. Jul 9 '19 at 22:17
• Kevin, I would be so grateful if you elaborate more on your statement " use the generated methods (a getter and setter per GRC "variable") to change parameters" ... I just got the time to work on this part. If you have/can refer me to an example that would be great! Jul 17 '19 at 0:52
• @Hadad I've added an example of that. If you still need to know more, please post a new question, and make sure to be specific about what you want to do and what you have so far (show the code that sets up tb and what effect you want to have on the flow graph, and so on). Jul 17 '19 at 2:24
• Kevin, that was very helpful. Thanks a lot. Jul 17 '19 at 16:24