Study gnuradio qa_tag_file_sink.py and gnuradio
#! /Usr/bin/env python # A file sink that uses tags to save files. # The sink uses a tag with the key 'burst' to trigger the saving of the burst data to a new file. # If the value of this tag is True, it will open a new file and start writing all incoming data to it. # If the tag is False, it will close the file (if already opened ). the file names are based on the # time when the burst tag was seen. if ther E is an 'rx _ time' tag (standard with UHD sources), # that is used as the time. if no 'rx _ time' tag is found, the new time is calculated based off the # sample rate of the block. from gnuradio import gr, gr_unittest, blocksimport OS, struct ##### the OS module provides a unified operating system interface function, which is usually specified by the platform, the OS module can automatically switch between specific functions of the same operating system platform, such as nt or posix, so as to implement cross-platform operations, ##### http://www.cnblogs.com/wayneye/archive/2010/05/ of the python OS Module 03/1726865. htmlclass test_tag_file_sink (gr_unittest.TestCase): def setUp (self): self. tb = gr. top_block () def tearDown (self): self. tb = None def test_001 (self): # create a flow chart src_data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) trg_data = (-1,-1, 1, 1,-1,-1, 1, 1,-1,-1) src = blocks. vector_source_ I (src_data) trg = blocks. vector_source_s (trg_data) op = blocks. burst_tagger (gr. sizeof_int) snk = blocks. tagged_file _ Sink (gr. sizeof_int, 1) self. tb. connect (src, (op, 0) self. tb. connect (trg, (op, 1) self. tb. connect (op, snk) self. tb. run () # Tagged file sink gets 2 burst tags at index 2 and index 5. # Creates two new files, each with two integers in them from # src_data at these indexes (3, 4) and (7, 8 ). file0 = "file {0} _ 0_2.20.0000.dat ". format (snk. unique_id () file1 = "file {0} _ limit 6.20.0000.dat ". format (snk. unique _ Id () # Open the files and read in the data, then remove the files # to clean up the directory. outfile0 = file (file0, 'rb') # Read Binary files, file read and write in python: http://www.cnblogs.com/allenblogs/archive/2010/09/13/1824842.html outfile1 = file (file1, 'rb') data0 = outfile0.read (8) data1 = outfile1.read (8) outfile0.close () outfile1.close () OS. remove (file0) OS. remove (file1) # Convert the 8 bytes from the files A tuple of 2 ints. #### using struct in python to process binary data: http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html idata0 = struct. unpack ('II', data0) idata1 = struct. unpack ('II', data1) self. assertEqual (idata0, (3, 4) # Test Module self in python. assertEqual (idata1, (7, 8) if _ name _ = '_ main _': gr_unittest.run (test_tag_file_sink, "test_tag_file_sink.xml ") # gr_unittest is an extended standard Python module unit test. Gr_unittest adds support for checking that the tuples of the floating point # And the plural are approximately equal. Unittest uses the Python reflection mechanism to discover all running methods and run # them. Unittest packages call test _ * each time to match the call for creation and removal. When we run the test, in this order # gr_unittest.main will call SETUP, test_001 and tearDown.