# How to update the value of a variable by reading message in grc?

**URL:** <https://www.ruby-forum.com/t/how-to-update-the-value-of-a-variable-by-reading-message-in-grc/240857>\
**Category:** GNU Radio\
**Created:** [May 3, 2014, 8:32am UTC](https://www.ruby-forum.com/t/how-to-update-the-value-of-a-variable-by-reading-message-in-grc/240857 "2014-05-03T08:32:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [May 3, 2014, 8:32am UTC](https://www.ruby-forum.com/t/how-to-update-the-value-of-a-variable-by-reading-message-in-grc/240857/1 "2014-05-03T08:32:22Z")

</div>

Hi all,

I’m working on a system with packet headers. I want to use the message  
decoded by the Packet Header Parser to update a variable.

I knew the message can be stored in a message debug block, and be  
retrieved  
by using the “get\_message” function. So I can define a get function to  
read  
the message in the flowgraph class, and call this get function in “main”  
and  
set this value to the variable I want to update. For example, I wrote a  
get  
function like this:

```
def get_type(self):

```

self.num\_msg = self.msg\_debug.num\_messages()  
self.msg = pmt.to\_python(self.msg\_debug.get\_message(self.num\_msg-1))  
self.type = self.msg.get(‘type\_tag’)  
return self.type

and call this get function in “main”  
type = tb.get\_type()  
tb.set\_variable(type)

But as far as I know, the get function can only be called once in  
“main”, so  
the variable can only be updated once. Since we want to update the  
variable  
every time we decoded a message, I’m wondering is there a way in the grc  
to  
change a variable  
by the message?

I actually have a idea in mind. Like the Header/Payload Demux block used  
in  
rx\_ofdm, I can write a block that taking message as input, reading the  
PMT  
dictionary to get the value we want, and output the value. Then the  
desired  
variable can be updated by connecting the output of my designed block to  
a  
function probe.

But this function probe only probes the port in a certain frequency,  
i.e. it  
can not catch all the updates. So I’m wondering if there is a way to  
update  
a predefined variable through a block every time.

Any suggestions will be valued.

Thanks!  
Best,  
Zhe

–  
View this message in context:  
[http://gnuradio.4.n7.nabble.com/How-to-update-the-value-of-a-variable-by-reading-message-in-grc-tp47932.html](http://gnuradio.4.n7.nabble.com/How-to-update-the-value-of-a-variable-by-reading-message-in-grc-tp47932.html)  
Sent from the GnuRadio mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![Zhe\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d07c76/32.png) [@Zhe\_F](https://www.ruby-forum.com/u/Zhe_F)\
**Post date:** [May 11, 2014, 4:23pm UTC](https://www.ruby-forum.com/t/how-to-update-the-value-of-a-variable-by-reading-message-in-grc/240857/2 "2014-05-11T16:23:50Z")

</div>

On Sat, May 3, 2014 at 2:31 AM, Zhe F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> function like this:  
> tb.set\_variable(type)  
> dictionary to get the value we want, and output the value. Then the desired  
> Thanks!  
> Best,  
> Zhe

I think your best option is to create your own block for handling the  
messages and not use the Message Debug block. As that block says, it’s  
designed to help us debug message passing applications; it’s not meant  
to  
be actually used as part of a real application.

Tom
