Home > GNURadio and USRP > Intro to GNURadio and the USRP (Part 4, message blocks)

Intro to GNURadio and the USRP (Part 4, message blocks)

This week I started playing with the message source and sink blocks within GNURadio and found them quite useful. Especially for accessing (sending and receiving) data outside of the actual GNURadio code. Below is the code, but let me explain what is going on (you can get the code from the source repository, see part 0). I create a top block which has a message_source and a message_sink (and a queue for each). Then I created two functions in the top block, one for sending a packet, and the other for receiving. The code should be fairly simple.

Then in the main portion of the code, I show how this is used. You can interact directly with the queues or by using the two functions.

from gnuradio import gr
from time import sleep

class msg_blocks(gr.top_block):

    def __init__(self):
        gr.top_block.__init__(self, 'Message Blocks Test')

        # initialize the queues
        self.sink_queue = gr.msg_queue()
        self.source_queue = gr.msg_queue()

        # initialize the blocks
        self.msg_source = gr.message_source(gr.sizeof_char, self.source_queue)
        self.msg_sink = gr.message_sink(gr.sizeof_char, self.sink_queue, False)

        self.connect((self.msg_source, 0), (self.msg_sink, 0))

    def send_pkt(self, payload):

    def recv_pkt(self):
        pkt = ""

        if self.sink_queue.count():
            pkt = self.sink_queue.delete_head().to_string()

        return pkt            

if __name__=="__main__":
    tb = msg_blocks()

    # operate on queues directly
    print 'operating on the queues directly'
    print 'sink queue count:', tb.sink_queue.count()

    print 'inserting:', 'a'

    sleep(1) # sleep to yield to tb thread

    print 'sink queue count:', tb.sink_queue.count()

    print 'received:', tb.sink_queue.delete_head().to_string()

    # operate using methods
    print 'operating with fuctions supplied by the top block'
    print 'sending:', 'a'*512

    print 'received:', tb.recv_pkt()
Categories: GNURadio and USRP
  1. rahul
    May 23, 2011 at 12:19 pm

    i’ve been trying to do a usrp simulation using qam, but cant get through can you help me with it

  2. August 7, 2011 at 5:10 pm

    Hey, excellent work. However, I have a few quick questions. I am working on a research project dealing with cognitive radio networks, I need two USRP (software based — don’t have access to hardware) nodes talking to each other over a network. From your work I would assume I can simply change the IP address to send over the network and have a receiver on the other end. –Have you experimented with this? The only problem I’m having is trying to generate the code from simple_transceiver.grc gnuradio-companion will not allow a build. Any assistance would be greatly appreciated. I will be sure to acknowledge you in my reports. Thanks in advance.
    Sean G

  3. Juan
    December 14, 2012 at 2:15 pm

    is there any way to create a message source from c++ like any other block?

  4. manju
    March 7, 2013 at 5:54 pm

    in which path should i run this code for it to work?Because it gives me an error from:
    can’t read /var/mail/gnuradio
    from: can’t read /var/mail/time
    ./part_4.py: line 4: syntax error near unexpected token `(‘
    ./part_4.py: line 4: `class msg_blocks(gr.top_block):

  5. manju
    March 7, 2013 at 6:07 pm

    can you please the .grc file for this example?

  6. manju
    March 7, 2013 at 6:07 pm

    manju :
    can you please post the .grc file for this example?

  7. Eva
    August 7, 2013 at 12:00 pm

    Is it possible to send a character/string from one usrp to another? to generate a signal source and send it through USRPs can be done. But to send & receive a ‘real’ message (like ‘Hello world’), how that can be done using GRC or Python environment with USRP?
    Greatly appreciate your sharing. Thanks

    • pwnhome
      August 10, 2013 at 10:50 am

      It definitely is. The easiest way in my opinion is to use a TCP (or UDP) source and sink. Connect to the TCP source (say using telnet) and type. I believe when you hit “return” it should send the data across. Connect in another terminal to the sink and you should see the data.

  8. Yamid
    October 2, 2014 at 4:38 pm

    Hello, are interesting your blog, and your tutorial, but, it doesn’t run on gnuR 3.7… ¿can you helpme with this?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: