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):
self.source_queue.insert_tail(gr.message_from_string(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()
tb.start()
# operate on queues directly
print 'operating on the queues directly'
print 'sink queue count:', tb.sink_queue.count()
print 'inserting:', 'a'
tb.source_queue.insert_tail(gr.message_from_string('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
tb.send_pkt('a'*512)
sleep(1)
print 'received:', tb.recv_pkt()
Categories: GNURadio and USRP
hey
i’ve been trying to do a usrp simulation using qam, but cant get through can you help me with it
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
is there any way to create a message source from c++ like any other block?
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):
can you please the .grc file for this example?