# Handling Messages

**URL:** <https://www.ruby-forum.com/t/handling-messages/168475>\
**Category:** GNU Radio\
**Created:** [May 21, 2009, 6:38am UTC](https://www.ruby-forum.com/t/handling-messages/168475 "2009-05-21T06:38:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali\_siddiqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ali\_siddiqi](https://www.ruby-forum.com/u/ali_siddiqi)\
**Post date:** [May 21, 2009, 6:38am UTC](https://www.ruby-forum.com/t/handling-messages/168475/1 "2009-05-21T06:38:31Z")

</div>

Hi,

I am trying to make a flowgraph as given:

signal source --\> stream2vector --\> fft --\> c2m --\> msg\_sink

If the size of fft is 4096, then I want to retrieve these 4096 points in  
python and then do simple maths and logic based on some of the values  
within  
these 4096 points within python.

And then based on the result, I want to initiate another flowgraph!

Currently, there is an error between the interface between c2m and  
msg\_sink.

The code is:

class top(gr.top\_block):  
def **init** (self):  
gr.top\_block. **init** (self)

```
        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 32000

        ##################################################
        # Blocks
        ##################################################
        self.sigsource = gr.sig_source_c(samp_rate, gr.GR_COS_WAVE,

```

1000, 10, 0)  
self.sigsource1 = gr.sig\_source\_c(samp\_rate, gr.GR\_COS\_WAVE,  
2000, 20, 0)  
self.sigadder = gr.add\_vcc(1)

```
        fftsize = 4096

        mywin = window.blackmanharris(fftsize)

        fft = gr.fft_vcc(fftsize, True, mywin)

        c2m = gr.complex_to_mag(fftsize)

        s2f1 = gr.short_to_float()

        s2f2 = gr.short_to_float()

        # Vector Sink/Source

        ss2v = gr.stream_to_vector(gr.sizeof_gr_complex, fftsize)

        v2ss = gr.vector_to_stream(gr.sizeof_float, fftsize)

        # Message Settings

        self.qsize = 10

        self.msgq0 = gr.msg_queue(self.qsize) # queue amplitude

        # Message sink

        m_sink0 = gr.message_sink(gr.sizeof_float, self.msgq0, True)

        self.connect((self.sigsource, 0), (self.sigadder, 0))
        self.connect((self.sigsource1, 0), (self.sigadder, 1))

        self.connect(self.sigadder, ss2v, fft, c2m, m_sink0)

```

def main ():

```
    # Start Top Block
    tb = top()

    #Start Flowgraph
    tb.start()

    # Read first message

    amsg = tb.msgq0.delete_head() # get first amplitude message

    raw_a = amsg.to_string() # raw amplitude data

    a_data = numpy.fromstring(raw_a, numpy.float32, count =

```

int(amsg.arg2())) # converted amplitude data

```
    print "Data in sink is: ",a_data

    tb.stop()

```

if **name** == ‘ **main** ’:

```
main ()

```

Regards,

---

<div class="post-metadata">

**Author:** ![ali\_siddiqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ali\_siddiqi](https://www.ruby-forum.com/u/ali_siddiqi)\
**Post date:** [May 21, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/handling-messages/168475/2 "2009-05-21T17:35:36Z")

</div>

Hi,

I am trying to make a flowgraph as given:

signal source --\> stream2vector --\> fft --\> c2m --\> msg\_sink

If the size of fft is 4096, then I want to retrieve these 4096 points in  
python and then do simple maths and logic based on some of the values  
within  
these 4096 points within python.

And then based on the result, I want to initiate another flowgraph!

Currently, there is an error between the interface between c2m and  
msg\_sink.

The code is:

class top(gr.top\_block):  
def **init** (self):  
gr.top\_block. **init** (self)

```
        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 32000

        ##################################################
        # Blocks
        ##################################################
        self.sigsource = gr.sig_source_c(samp_rate, gr.GR_COS_WAVE,

```

1000, 10, 0)  
self.sigsource1 = gr.sig\_source\_c(samp\_rate, gr.GR\_COS\_WAVE,  
2000, 20, 0)  
self.sigadder = gr.add\_vcc(1)

```
        fftsize = 4096

        mywin = window.blackmanharris(fftsize)

        fft = gr.fft_vcc(fftsize, True, mywin)

        c2m = gr.complex_to_mag(fftsize)

        s2f1 = gr.short_to_float()

        s2f2 = gr.short_to_float()

        # Vector Sink/Source

        ss2v = gr.stream_to_vector(gr.sizeof_gr_complex, fftsize)

        v2ss = gr.vector_to_stream(gr.sizeof_float, fftsize)

        # Message Settings

        self.qsize = 10

        self.msgq0 = gr.msg_queue(self.qsize) # queue amplitude

        # Message sink

        m_sink0 = gr.message_sink(gr.sizeof_float, self.msgq0, True)

        self.connect((self.sigsource, 0), (self.sigadder, 0))
        self.connect((self.sigsource1, 0), (self.sigadder, 1))

        self.connect(self.sigadder, ss2v, fft, c2m, m_sink0)

```

def main ():

```
    # Start Top Block
    tb = top()

    #Start Flowgraph
    tb.start()

    # Read first message

    amsg = tb.msgq0.delete_head() # get first amplitude message

    raw_a = amsg.to_string() # raw amplitude data

    a_data = numpy.fromstring(raw_a, numpy.float32, count =

```

int(amsg.arg2())) # converted amplitude data

```
    print "Data in sink is: ",a_data

    tb.stop()

```

if **name** == ‘ **main** ’:

```
main ()

```

Regards,

---

<div class="post-metadata">

**Author:** ![ali\_siddiqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ali\_siddiqi](https://www.ruby-forum.com/u/ali_siddiqi)\
**Post date:** [May 21, 2009, 8:06pm UTC](https://www.ruby-forum.com/t/handling-messages/168475/3 "2009-05-21T20:06:16Z")

</div>

Hi Ali - I think the issue is that the output of the C2M is of  
item\_size ‘fftsize’, whereas the message\_sink has item\_size of  
‘gr.sizeof\_float’. Try incorporating the ‘gr.vector\_to\_stream’ you  
created between the C2M and the message\_sink. - MLD
