diff options
author | Jacob Gilbert <jacob.gilbert@protonmail.com> | 2021-06-01 19:23:08 -0600 |
---|---|---|
committer | mormj <34754695+mormj@users.noreply.github.com> | 2021-06-07 07:35:30 -0400 |
commit | ab1e21c114465fa03d7fb1b56b3d5f8c9f56671e (patch) | |
tree | 115918cf9dbcc1c7b3be5abff70ebb6d76320aed /gr-blocks/python/blocks/var_to_msg.py | |
parent | d84df80edaf1e8491a1b4fc208b407b7e5c5198c (diff) |
blocks: improvements to var/msg blocks
Fixes an issue where a user was feeding a dictionary into the msg_to_var
block which was then setting the variable to 'None' causing unexpected
behavior. This improves input sanitization for both blocks and adds
some extra info to the docstrings.
Signed-off-by: Jacob Gilbert <jacob.gilbert@protonmail.com>
Diffstat (limited to 'gr-blocks/python/blocks/var_to_msg.py')
-rw-r--r-- | gr-blocks/python/blocks/var_to_msg.py | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/gr-blocks/python/blocks/var_to_msg.py b/gr-blocks/python/blocks/var_to_msg.py index 77a947ed0c..823609182c 100644 --- a/gr-blocks/python/blocks/var_to_msg.py +++ b/gr-blocks/python/blocks/var_to_msg.py @@ -13,7 +13,9 @@ import pmt class var_to_msg_pair(gr.sync_block): """ - This block will monitor a variable, and when it changes, generate a message. + This block has a callback that will emit a message pair with the updated variable + value when called. This is useful for monitoring a GRC variable and emitting a message + when it is changed. """ def __init__(self, pairname): gr.sync_block.__init__(self, name="var_to_msg_pair", in_sig=None, out_sig=None) @@ -23,16 +25,23 @@ class var_to_msg_pair(gr.sync_block): self.message_port_register_out(pmt.intern("msgout")) def variable_changed(self, value): - if type(value) == float: - p = pmt.from_float(value) - elif type(value) == int: - p = pmt.from_long(value) - elif type(value) == bool: - p = pmt.from_bool(value) - else: - p = pmt.intern(value) - - self.message_port_pub(pmt.intern("msgout"), pmt.cons(pmt.intern(self.pairname), p)) + try: + if type(value) == float: + p = pmt.from_double(value) + elif type(value) == int: + p = pmt.from_long(value) + elif type(value) == bool: + p = pmt.from_bool(value) + elif type(value) == str: + p = pmt.intern(value) + else: + p = pmt.to_pmt(value) + + self.message_port_pub(pmt.intern("msgout"), pmt.cons(pmt.intern(self.pairname), p)) + + except Exception as e: + gr.log.error("Unable to convert " + repr(value) + " to PDU, no message will be emitted (reason: %s)" % repr(e)) + def stop(self): return True |