Advertisement
Guest User

Untitled

a guest
Jun 29th, 2014
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from gnuradio import gr
  4. from gnuradio import blocks
  5. import gnuradio.gr.gr_threading as _threading
  6.  
  7.  
  8. class _mythread(_threading.Thread):
  9.  
  10.         def __init__(self, msgq_in, msgq_out):
  11.         self._msgq_in = msgq_in
  12.                 self._msgq_out = msgq_out
  13.                 _threading.Thread.__init__(self)
  14.                 self.setDaemon(1)
  15.                 self.keep_running = True
  16.                 self.start()
  17.  
  18.        
  19.     def run(self):
  20.         while self.keep_running:
  21.             # Read msg from input
  22.             msg = self._msgq_in.delete_head()
  23.             sample = msg.to_string()
  24.  
  25.             # do some stuffs on samples[]
  26.             # ...
  27.  
  28.             # sending out
  29.             msg = gr.message_from_string(sample)
  30.             self._msgq_out.insert_tail(msg)
  31.  
  32.  
  33. class my_top_block(gr.top_block):
  34.  
  35.     def __init__(self):
  36.         gr.top_block.__init__(self)
  37.  
  38.         # blocks
  39.         self.blocks_file_source = blocks.file_source(gr.sizeof_char*1, "/tmp/source", True) # True -> repeat
  40.         self.blocks_file_sink  = blocks.file_sink(gr.sizeof_char*1, "/tmp/sink", False)
  41.         self.hier_block        = hier_block()
  42.  
  43.         # connections
  44.         self.connect((self.blocks_file_source, 0), (self.hier_block, 0))
  45.         self.connect((self.hier_block, 0), (self.blocks_file_sink, 0))
  46.  
  47.  
  48. class hier_block(gr.hier_block2):
  49.  
  50.         def __init__(self):
  51.                 gr.hier_block2.__init__(
  52.                         self,
  53.                         "hier_block",
  54.                         gr.io_signature(1, 1, gr.sizeof_char), # Input signature
  55.                         gr.io_signature(1, 1, gr.sizeof_char)  # Output signature
  56.                 )
  57.  
  58.                 #create blocks
  59.                 msgq_in = gr.msg_queue(2)
  60.         msgq_out = gr.msg_queue(2)
  61.                 msg_sink = blocks.message_sink(gr.sizeof_char, msgq_in, False)
  62.         msg_source = blocks.message_source(gr.sizeof_char, msgq_out)
  63.  
  64.                 #connect
  65.                 self.connect(self, msg_sink)
  66.                 self.connect(msg_source, self)
  67.  
  68.                 #start thread
  69.                 _mythread(msgq_in, msgq_out)
  70.  
  71.  
  72. if __name__ == '__main__':
  73.     try:
  74.         my_top_block().run()
  75.     except [[KeyboardInterrupt]]:
  76.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement