def __init__(self): gr.sync_block.__init__(self, name="message consumer", in_sig=None, out_sig=None) self.msg_list = [] self.message_port_register_in(pmt.pmt_intern('in_port')) self.set_msg_handler(pmt.pmt_intern('in_port'), self.handle_msg)
def __init__(self): gr.basic_block.__init__(self, name="audio_ptt_gate", in_sig=[numpy.float32], out_sig=[numpy.float32]) self.message_port_register_in(pmt.pmt_intern('in2')) self.set_msg_handler(pmt.pmt_intern('in2'), self.handle_msg) self.ptt = False
def __init__(self): gr.basic_block.__init__(self, name="audio_ptt_gate", in_sig=[numpy.float32], out_sig=[numpy.float32]) self.message_port_register_in(pmt.pmt_intern('in2')) self.set_msg_handler(pmt.pmt_intern('in2'),self.handle_msg) self.ptt = False
def __init__(self): gr.sync_block.__init__( self, name = "message consumer", in_sig = None, out_sig = None ) self.msg_list = [] self.message_port_register_in(pmt.pmt_intern('in_port')) self.set_msg_handler(pmt.pmt_intern('in_port'), self.handle_msg)
def __init__(self, msg_list, msg_interval): gr.sync_block.__init__(self, name="message generator", in_sig=[numpy.float32], out_sig=None) self.msg_list = msg_list self.msg_interval = msg_interval self.msg_ctr = 0 self.message_port_register_out(pmt.pmt_intern('out_port'))
def work(self, input_items, output_items): inLen = len(input_items[0]) while self.msg_ctr < len(self.msg_list) and \ (self.msg_ctr * self.msg_interval) < \ (self.nitems_read(0) + inLen): self.message_port_pub(pmt.pmt_intern('out_port'), self.msg_list[self.msg_ctr]) self.msg_ctr += 1 return inLen
def run(self, msg_interval): while self.finished == False: self.payload = '' for i in xrange(0,self.msg_len): self.payload = self.payload + chr(self.msg_ctr & 0xFF) self.msg_ctr += 1 if self.debug: print "#" + str(self.node_id) + " ->", for i in range(0,len(self.payload)): # print hex(ord(self.payload[i])), print "%02x" % ord(self.payload[i]), print "" sys.stdout.flush() time.sleep(msg_interval) self.message_port_pub(pmt.pmt_intern('out'), pmt.pmt_intern(self.payload))
def test_000(self): # Just run some data through and make sure it doesn't puke. src_data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) src = blocks.pdu_to_tagged_stream(blocks.byte_t) snk3 = blocks.tagged_stream_to_pdu(blocks.byte_t) snk2 = gr.vector_sink_b() snk = gr.tag_debug(1, "test") snk.set_display(False) dbg = gr.message_debug() # Test that the right number of ports exist. pi = snk3.message_ports_in() po = snk3.message_ports_out() self.assertEqual(pmt.pmt_length(pi), 0) self.assertEqual(pmt.pmt_length(po), 1) time.sleep(0.1) self.tb.connect(src, snk) self.tb.connect(src, snk2) self.tb.connect(src, snk3) self.tb.msg_connect(snk3, "pdus", dbg, "store") self.tb.start() # make our reference and message pmts port = pmt.pmt_intern("pdus") msg = pmt.pmt_cons(pmt.PMT_NIL, pmt.pmt_make_u8vector(16, 0xFF)) # post the message src.to_basic_block()._post(port, msg) # eww, what's that smell? while dbg.num_messages() < 1: time.sleep(0.5) self.tb.stop() self.tb.wait() # Get the vector of data from the vector sink result_data = snk2.data() # Get the vector of data from the message sink # Convert the message PMT as a pair into its vector result_msg = dbg.get_message(0) msg_vec = pmt.pmt_cdr(result_msg) #pmt.pmt_print(msg_vec) # Convert the PMT vector into a Python list msg_data = [] for i in xrange(16): msg_data.append(pmt.pmt_u8vector_ref(msg_vec, i)) actual_data = 16 * [ 0xFF, ] self.assertEqual(actual_data, list(result_data)) self.assertEqual(actual_data, msg_data)
def __init__(self, msg_list, msg_interval): gr.sync_block.__init__( self, name = "message generator", in_sig = [numpy.float32], out_sig = None ) self.msg_list = msg_list self.msg_interval = msg_interval self.msg_ctr = 0 self.message_port_register_out(pmt.pmt_intern('out_port'))
def test_000(self): # Just run some data through and make sure it doesn't puke. src_data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) src = blocks.pdu_to_tagged_stream(blocks.byte_t) snk3 = blocks.tagged_stream_to_pdu(blocks.byte_t) snk2 = gr.vector_sink_b() snk = gr.tag_debug(1, "test") snk.set_display(False) dbg = gr.message_debug() # Test that the right number of ports exist. pi = snk3.message_ports_in() po = snk3.message_ports_out() self.assertEqual(pmt.pmt_length(pi), 0) self.assertEqual(pmt.pmt_length(po), 1) time.sleep(0.1) self.tb.connect(src, snk) self.tb.connect(src, snk2) self.tb.connect(src, snk3) self.tb.msg_connect(snk3, "pdus", dbg, "store") self.tb.start() # make our reference and message pmts port = pmt.pmt_intern("pdus") msg = pmt.pmt_cons( pmt.PMT_NIL, pmt.pmt_make_u8vector(16, 0xFF)) # post the message src.to_basic_block()._post(port, msg) # eww, what's that smell? while dbg.num_messages() < 1: time.sleep(0.5) self.tb.stop() self.tb.wait() # Get the vector of data from the vector sink result_data = snk2.data() # Get the vector of data from the message sink # Convert the message PMT as a pair into its vector result_msg = dbg.get_message(0) msg_vec = pmt.pmt_cdr(result_msg) #pmt.pmt_print(msg_vec) # Convert the PMT vector into a Python list msg_data = [] for i in xrange(16): msg_data.append(pmt.pmt_u8vector_ref(msg_vec, i)) actual_data = 16*[0xFF,] self.assertEqual(actual_data, list(result_data)) self.assertEqual(actual_data, msg_data)
def test_debug_401(self): msg = pmt.pmt_intern("TESTING") src = blocks.message_strobe(msg, 500) snk = blocks.message_debug() tb = gr.top_block() tb.msg_connect(src, "strobe", snk, "store") tb.start() time.sleep(1) tb.stop() tb.wait() rec_msg = snk.get_message(0) self.assertTrue(pmt.pmt_eqv(rec_msg, msg))
def test_001_t (self): """ Simplest possible test: put in zeros, then header, then payload, trigger signal, try to demux. The return signal from the header parser is faked via _post() """ n_zeros = 100 header = (1, 2, 3) payload = tuple(range(17)) data_signal = (0,) * n_zeros + header + payload trigger_signal = [0,] * len(data_signal) trigger_signal[n_zeros] = 1 data_src = gr.vector_source_f(data_signal, False) trigger_src = gr.vector_source_b(trigger_signal, False) hpd = digital.header_payload_demux( len(header), 1, 0, "frame_len", "detect", False, gr.sizeof_float ) self.assertEqual(pmt.pmt_length(hpd.message_ports_in()), 1) header_sink = gr.vector_sink_f() payload_sink = gr.vector_sink_f() self.tb.connect(data_src, (hpd, 0)) self.tb.connect(trigger_src, (hpd, 1)) self.tb.connect((hpd, 0), header_sink) self.tb.connect((hpd, 1), payload_sink) self.tb.start() time.sleep(.2) # Need this, otherwise, the next message is ignored hpd.to_basic_block()._post( pmt.pmt_intern('header_data'), pmt.pmt_from_long(len(payload)) ) while len(payload_sink.data()) < len(payload): time.sleep(.2) self.tb.stop() self.tb.wait() self.assertEqual(header_sink.data(), header) self.assertEqual(payload_sink.data(), payload)
def __init__(self, debug, msg_interval, msg_len, node_id): gr.basic_block.__init__(self, name="message_source", in_sig = None, out_sig = None ) self.debug = debug self.node_id = node_id self.msg_interval = msg_interval self.msg_len = msg_len self.msg_ctr = 0x00 self.message_port_register_out(pmt.pmt_intern('out')) self.finished = False try: self.msg_src_thread = thread.start_new_thread(self.run, (msg_interval,)) except Exception as inst: print "Error: message_source is unable to start thread" print type(inst) # the exception instance print inst.args # arguments stored in .args print inst # __str__ allows args to printed directly
def message_port_register_hier_out(self, portname): self.primitive_message_port_register_hier_out(pmt.pmt_intern(portname))
def message_port_register_hier_out(self, portname): self.primitive_message_port_register_hier_out(pmt.pmt_intern(portname));
def test_001(self): N = 1000 outfile = "test_out.dat" detached = False samp_rate = 200000 key = pmt.pmt_intern("samp_rate") val = pmt.pmt_from_double(samp_rate) extras = pmt.pmt_make_dict() extras = pmt.pmt_dict_add(extras, key, val) extras_str = pmt.pmt_serialize_str(extras) data = sig_source_c(samp_rate, 1000, 1, N) src = gr.vector_source_c(data) fsnk = blocks.file_meta_sink(gr.sizeof_gr_complex, outfile, samp_rate, 1, blocks.GR_FILE_FLOAT, True, 1000000, extras_str, detached) fsnk.set_unbuffered(True) self.tb.connect(src, fsnk) self.tb.run() fsnk.close() handle = open(outfile, "rb") header_str = handle.read(parse_file_metadata.HEADER_LENGTH) if(len(header_str) == 0): self.assertFalse() try: header = pmt.pmt_deserialize_str(header_str) except RuntimeError: self.assertFalse() print header info = parse_file_metadata.parse_header(header, False) extra_str = handle.read(info["extra_len"]) self.assertGreater(len(extra_str), 0) handle.close() try: extra = pmt.pmt_deserialize_str(extra_str) except RuntimeError: self.assertFalse() extra_info = parse_file_metadata.parse_extra_dict(extra, info, False) self.assertEqual(info['rx_rate'], samp_rate) self.assertEqual(pmt.pmt_to_double(extra_info['samp_rate']), samp_rate) # Test file metadata source src.rewind() fsrc = blocks.file_meta_source(outfile, False) vsnk = gr.vector_sink_c() tsnk = gr.tag_debug(gr.sizeof_gr_complex, "QA") ssnk = gr.vector_sink_c() self.tb.disconnect(src, fsnk) self.tb.connect(fsrc, vsnk) self.tb.connect(fsrc, tsnk) self.tb.connect(src, ssnk) self.tb.run() # Test to make sure tags with 'samp_rate' and 'rx_rate' keys # were generated and received correctly. tags = tsnk.current_tags() for t in tags: if(pmt.pmt_eq(t.key, pmt.pmt_intern("samp_rate"))): self.assertEqual(pmt.pmt_to_double(t.value), samp_rate) elif(pmt.pmt_eq(t.key, pmt.pmt_intern("rx_rate"))): self.assertEqual(pmt.pmt_to_double(t.value), samp_rate) # Test that the data portion was extracted and received correctly. self.assertComplexTuplesAlmostEqual(vsnk.data(), ssnk.data(), 5) os.remove(outfile)
def test_001(self): N = 1000 outfile = "test_out.dat" detached = False samp_rate = 200000 key = pmt.pmt_intern("samp_rate") val = pmt.pmt_from_double(samp_rate) extras = pmt.pmt_make_dict() extras = pmt.pmt_dict_add(extras, key, val) extras_str = pmt.pmt_serialize_str(extras) data = sig_source_c(samp_rate, 1000, 1, N) src = gr.vector_source_c(data) fsnk = blocks.file_meta_sink( gr.sizeof_gr_complex, outfile, samp_rate, 1, blocks.GR_FILE_FLOAT, True, 1000000, extras_str, detached ) fsnk.set_unbuffered(True) self.tb.connect(src, fsnk) self.tb.run() fsnk.close() handle = open(outfile, "rb") header_str = handle.read(parse_file_metadata.HEADER_LENGTH) if len(header_str) == 0: self.assertFalse() try: header = pmt.pmt_deserialize_str(header_str) except RuntimeError: self.assertFalse() print header info = parse_file_metadata.parse_header(header, False) extra_str = handle.read(info["extra_len"]) self.assertGreater(len(extra_str), 0) handle.close() try: extra = pmt.pmt_deserialize_str(extra_str) except RuntimeError: self.assertFalse() extra_info = parse_file_metadata.parse_extra_dict(extra, info, False) self.assertEqual(info["rx_rate"], samp_rate) self.assertEqual(pmt.pmt_to_double(extra_info["samp_rate"]), samp_rate) # Test file metadata source src.rewind() fsrc = blocks.file_meta_source(outfile, False) vsnk = gr.vector_sink_c() tsnk = gr.tag_debug(gr.sizeof_gr_complex, "QA") ssnk = gr.vector_sink_c() self.tb.disconnect(src, fsnk) self.tb.connect(fsrc, vsnk) self.tb.connect(fsrc, tsnk) self.tb.connect(src, ssnk) self.tb.run() # Test to make sure tags with 'samp_rate' and 'rx_rate' keys # were generated and received correctly. tags = tsnk.current_tags() for t in tags: if pmt.pmt_eq(t.key, pmt.pmt_intern("samp_rate")): self.assertEqual(pmt.pmt_to_double(t.value), samp_rate) elif pmt.pmt_eq(t.key, pmt.pmt_intern("rx_rate")): self.assertEqual(pmt.pmt_to_double(t.value), samp_rate) # Test that the data portion was extracted and received correctly. self.assertComplexTuplesAlmostEqual(vsnk.data(), ssnk.data(), 5) os.remove(outfile)