# Fwd: Write a source block in python

**URL:** <https://www.ruby-forum.com/t/fwd-write-a-source-block-in-python/233928>\
**Category:** GNU Radio\
**Created:** [January 22, 2015, 10:13pm UTC](https://www.ruby-forum.com/t/fwd-write-a-source-block-in-python/233928 "2015-01-22T22:13:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [January 22, 2015, 10:13pm UTC](https://www.ruby-forum.com/t/fwd-write-a-source-block-in-python/233928/1 "2015-01-22T22:13:35Z")

</div>

Hi

I wrote a simple source block that reads a string (as a parameter) and  
outputs the corresponding array of bytes.  
Here is the code:

class myblock(gr.sync\_block):

```
def __init__ (self, helloMsg):
    gr.sync_block. __init__ (self,
        name="myblock",
        in_sig=None,
        out_sig=[numpy.byte])
    self.helloMsg = helloMsg

def work(self, input_items, output_items):
    out = output_items[0]

    test = array.array('B',self.helloMsg)
    for j in range(len(test)):
       out[j] = test[j]
    return len(output_items[0][:len(test)])

```

In the output I repeatedly get the helloMsg. (it seems that the work()  
function is always running)  
Is it possible to get in the output my helloMsg only one time (or a  
specific amount of times) and then stop the execution of the flowgraph?

Thank you in advance!  
Thanasis

---

<div class="post-metadata">

**Author:** ![Thanasis\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@Thanasis\_B](https://www.ruby-forum.com/u/Thanasis_B)\
**Post date:** [January 22, 2015, 10:33pm UTC](https://www.ruby-forum.com/t/fwd-write-a-source-block-in-python/233928/2 "2015-01-22T22:33:09Z")

</div>

I think having the work fn return “-1” will cause the flowgraph to exit,  
so you could potentially have a “self.count = 1” in **init** and then  
when you’ve output \>= self.count, have work return -1

-Doug

Douglas Anderson | Intern  
DOC/NTIA/ITS-T | 325 Broadway St., Boulder, CO 80305 | P: 303 497 3582

* * *

From: discuss-gnuradio-bounces+danderson=removed\_email\_address@domain.invalid  
[discuss-gnuradio-bounces+danderson=removed\_email\_address@domain.invalid] on behalf  
of Thanasis B. [removed\_email\_address@domain.invalid]  
Sent: Thursday, January 22, 2015 2:12 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Discuss-gnuradio] Fwd: Write a source block in python

Hi

I wrote a simple source block that reads a string (as a parameter) and  
outputs the corresponding array of bytes.  
Here is the code:

class myblock(gr.sync\_block):

```
def __init__ (self, helloMsg):
    gr.sync_block. __init__ (self,
        name="myblock",
        in_sig=None,
        out_sig=[numpy.byte])
    self.helloMsg = helloMsg

def work(self, input_items, output_items):
    out = output_items[0]

    test = array.array('B',self.helloMsg)
    for j in range(len(test)):
       out[j] = test[j]
    return len(output_items[0][:len(test)])

```

In the output I repeatedly get the helloMsg. (it seems that the work()  
function is always running)  
Is it possible to get in the output my helloMsg only one time (or a  
specific amount of times) and then stop the execution of the flowgraph?

Thank you in advance!  
Thanasis

---

<div class="post-metadata">

**Author:** ![Thanasis\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@Thanasis\_B](https://www.ruby-forum.com/u/Thanasis_B)\
**Post date:** [January 22, 2015, 11:55pm UTC](https://www.ruby-forum.com/t/fwd-write-a-source-block-in-python/233928/3 "2015-01-22T23:55:16Z")

</div>

Thank you Doug!

return -1 indeed cause the flowgraph to exit. Is there any trick to stop  
also the execution on my program (after 1 sec)?  
Another problem I have is that the output is always 32768 bytes  
def work(self, input\_items, output\_items):  
…  
return len(output\_items[0])

If I change it to this:  
return len(output\_items[0][:len(test)])  
my output becomes empty  
Is it possible to get in the output the exact number of bytes of my  
helloMsg parameter?

2015-01-22 23:31 GMT+02:00 Anderson, Douglas J.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Thanasis\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@Thanasis\_B](https://www.ruby-forum.com/u/Thanasis_B)\
**Post date:** [January 23, 2015, 10:55am UTC](https://www.ruby-forum.com/t/fwd-write-a-source-block-in-python/233928/4 "2015-01-23T10:55:53Z")

</div>

On 01/22/2015 11:53 PM, Thanasis B. wrote:

> Thank you Doug!
> 
> return -1 indeed cause the flowgraph to exit. Is there any trick to stop  
> also the execution on my program (after 1 sec)?

You can exit whenever you like. E.g. run a timer until 1 sec is over and  
then return -1.

When you return -1, the FG _should_ shut down by itself.

> Another problem I have is that the output is always 32768 bytes

You have no problem: Your output _buffer_ is of this size. Your output  
size is whatever you return (something like return len(test) would be  
what you want).

M

---

<div class="post-metadata">

**Author:** ![Thanasis\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@Thanasis\_B](https://www.ruby-forum.com/u/Thanasis_B)\
**Post date:** [April 9, 2015, 4:00am UTC](https://www.ruby-forum.com/t/fwd-write-a-source-block-in-python/233928/5 "2015-04-09T04:00:14Z")

</div>

Hi,

> I think having the work fn return “-1” will cause the flowgraph to exit,  
> so you could potentially have a “self.count = 1” in **init** and then  
> when you’ve output \>= self.count, have work return -1

What exactly do you mean by output \>= self.count? I understand the count  
part obviously, but I didn’t quite get what we are comparing to the  
count. Is it the len(output\_items[0])? And if this is greater than or  
equal to the count then work returns -1? Is that what you mean?

I’m trying to code a python source block myself and I’m facing a similar  
issue, where I am getting a repeated output infinite times.

thanks a lot  
AKY

---

<div class="post-metadata">

**Author:** ![Thanasis\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@Thanasis\_B](https://www.ruby-forum.com/u/Thanasis_B)\
**Post date:** [March 25, 2015, 1:04am UTC](https://www.ruby-forum.com/t/fwd-write-a-source-block-in-python/233928/6 "2015-03-25T01:04:20Z")

</div>

Hi,

I am trying to create a similar source block using python. I am new to  
Python and I have couple questions here:

1. How do you change your work function in order to output the message  
only once? I get either 0 output (output file with 0 Bytes) or repeated  
output, I don’t know where should i put “return -1”.
2. what is the difference between  
“for j in range(len(test)):  
out[j] = test[j]”  
and  
“out[:len(test)] = test” ?
3. what is the difference between “return len(test)” and “return  
len(output\_items[0])”? So the return value only determines the output  
size?

Thank you so much!

Hai

Martin B. wrote in post #1167125:

> On 01/22/2015 11:53 PM, Thanasis B. wrote:
> 
> > Thank you Doug!
> > 
> > return -1 indeed cause the flowgraph to exit. Is there any trick to stop  
> > also the execution on my program (after 1 sec)?
> 
> You can exit whenever you like. E.g. run a timer until 1 sec is over and  
> then return -1.
> 
> When you return -1, the FG _should_ shut down by itself.
> 
> > Another problem I have is that the output is always 32768 bytes
> 
> You have no problem: Your output _buffer_ is of this size. Your output  
> size is whatever you return (something like return len(test) would be  
> what you want).
> 
> M
