GNU Radio Companion - GNU Radio Embedded Python Example 4

GNUR Radio Embedded Python Example 4 - Text message example

Prepared by Dr. Aaron Scher
aaron.scher@oit.edu
Oregon Institute of Technology

Back to Aaron's home page.
Back to My collection of GNU radio companion flow graphs.

(Last updated: Aug 2016)

Download file: Message_Example.grc.

This is a simple simulation of a one-way text messaging system. When the flow graph is run, the user can type messages like "Hello world!" into the text box. The messages is displayed using the Message Debug Print block. This flowgraph uses a custom Embedded Python block, which I created to convert strings to uint8 vectors.

Screen shots:

hello world

hello world

Double click on "Embedded Python Block" and then click the bottom that says "Open in Editor" and you can enter Python code. Here is the Python code for this exmaple:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
Embedded Python Blocks:

Each time this file is saved, GRC will instantiate the first class it finds
to get ports and parameters of your block. The arguments to __init__  will
be the parameters. All of them are required to have default values!
"""

import numpy as np
import pylab
from gnuradio import gr
import pmt


class msg_block(gr.basic_block):  # other base classes are basic_block, decim_block, interp_block
    """This block converts strings from the QT GUI Message Edit Box to uint8 vectors"""

    def __init__(self):  # only default arguments here
        """arguments to this function show up as parameters in GRC"""
        gr.basic_block.__init__(
            self,
            name='Embedded Python Block',   # will show up in GRC
            in_sig=None,
            out_sig=None
        )
        self.message_port_register_out(pmt.intern('msg_out'))
        self.message_port_register_in(pmt.intern('msg_in'))
        self.set_msg_handler(pmt.intern('msg_in'), self.handle_msg)
    
    def handle_msg(self, msg):
        nvec = pmt.to_python(msg)
        self.message_port_pub(pmt.intern('msg_out'), pmt.cons(pmt.make_dict(), pmt.pmt_to_python.numpy_to_uvector(np.array([ord(c) for c in nvec], np.uint8))))
    
    def work(self, input_items, output_items):
        pass