b_len is defined in the try block, and is out of scope by the time it is checked below.
This code should work. The routine returns in the except clause and b_len is set after the try/except. import numpy as np from gnuradio import gr import pmt import array class blk(gr.sync_block): def __init__(self): gr.sync_block.__init__( self, name='EPB: Packet to PMT', # will show up in GRC in_sig=None, out_sig=None) self.message_port_register_in(pmt.intern('msg_in')) self.message_port_register_out(pmt.intern('msg_out')) self.set_msg_handler(pmt.intern('msg_in'), self.handle_msg) def handle_msg(self, msg): _debug = 0 # set to zero to turn off diagnostics try: buff = pmt.to_python(pmt.cdr(msg)) except Exception as e: gr.log.error("Error with message conversion: %s" % str(e)) return b_len = len (buff) if (_debug): print ("new_val =", buff, b_len) if (b_len != 52): self.message_port_pub (pmt.intern('msg_out'), pmt.cons(pmt.PMT_NIL,pmt.to_pmt(buff))) elif ((buff[0] != 37) and (buff[51] != 93)): self.message_port_pub (pmt.intern('msg_out'), pmt.cons(pmt.PMT_NIL,pmt.to_pmt(buff))) On Wed, Jul 5, 2023 at 6:11 PM Barry Duggan <ba...@dcsmail.net> wrote: > I would like to receive a PDU message containing a string using an > Embedded Python Block and, after discarding certain messages, send a PMT > message with that string to the output message port. My efforts so far have > not produced a valid PMT format. My current code is: > ``` > import numpy as np > from gnuradio import gr > import pmt > import array > > class blk(gr.sync_block): > def __init__(self): > gr.sync_block.__init__( > self, > name='EPB: Packet to PMT', # will show up in GRC > in_sig=None, > out_sig=None) > self.message_port_register_in(pmt.intern('msg_in')) > self.message_port_register_out(pmt.intern('msg_out')) > self.set_msg_handler(pmt.intern('msg_in'), self.handle_msg) > > def handle_msg(self, msg): > _debug = 0 # set to zero to turn off diagnostics > try: > buff = pmt.to_python(pmt.cdr(msg)) > b_len = len (buff) > except Exception as e: > gr.log.error("Error with message conversion: %s" % str(e)) > if (_debug): > print ("new_val =", buff, b_len) > if (b_len != 52): > self.message_port_pub (pmt.intern('msg_out'), > pmt.cons(pmt.PMT_NIL,pmt.to_pmt(buff))) > elif ((buff[0] != 37) and (buff[51] != 93)): > self.message_port_pub (pmt.intern('msg_out'), > pmt.cons(pmt.PMT_NIL,pmt.to_pmt(buff))) > ``` > > Any help will be appreciated! > > --- > Barry Duggan > > >