# UHD Error handling from GNU Radio/Python

**URL:** <https://www.ruby-forum.com/t/uhd-error-handling-from-gnu-radio-python/226353>\
**Category:** GNU Radio\
**Created:** [July 20, 2012, 6:43am UTC](https://www.ruby-forum.com/t/uhd-error-handling-from-gnu-radio-python/226353 "2012-07-20T06:43:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NowlanS\_Sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f05b48/32.png) [@NowlanS\_Sean](https://www.ruby-forum.com/u/NowlanS_Sean)\
**Post date:** [July 20, 2012, 6:43am UTC](https://www.ruby-forum.com/t/uhd-error-handling-from-gnu-radio-python/226353/1 "2012-07-20T06:43:15Z")

</div>

Hi all,

I am interested in being able to recover from UHD Error conditions such  
as not finding a USRP on startup or an Ethernet cable getting knocked  
loose (USRP N200). I implemented an uhd\_async\_message source but all I  
can do with that is read error codes corresponding to sequence errors,  
late packets, underruns, etc. It’s nice to have the ability to take  
action based on these messages, but if an Ethernet cable gets knocked  
loose, nothing gets sent to the message queue (at least, not that I  
could find). Instead, UHD prints to stdout using some other thread and  
then my script hangs.

I hacked UHD’s error printing code to do an exit after it prints  
whatever error occurred, and then a bash script handles restarting the  
Python program that’s controlling GNU Radio behavior. I’m aware that  
there is a way to register a message handler function with UHD so that  
it won’t just print to the screen and hang. Is there any way to  
implement this handler in Python and register the function with UHD? I  
don’t normally post to both lists to avoid redundant spamming, but I’ll  
take a leap of faith here. 🙂

Thanks,  
Sean Nowlan

P.S. - I’ll sneak this question in with the technical one above: is the  
GNU Radio conference free to attend? Is there any registration required?  
Didn’t see any such thing on the site, so I’m assuming it’s free and the  
public can just show up and enjoy some awesomeness.

---

<div class="post-metadata">

**Author:** ![NowlanS\_Sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f05b48/32.png) [@NowlanS\_Sean](https://www.ruby-forum.com/u/NowlanS_Sean)\
**Post date:** [July 20, 2012, 10:07am UTC](https://www.ruby-forum.com/t/uhd-error-handling-from-gnu-radio-python/226353/2 "2012-07-20T10:07:10Z")

</div>

On 07/19/2012 09:42 PM, Nowlan, Sean wrote:

> some other thread and then my script hangs.
> 
> I hacked UHD’s error printing code to do an exit after it prints  
> whatever error occurred, and then a bash script handles restarting  
> the Python program that’s controlling GNU Radio behavior. I’m aware  
> that there is a way to register a message handler function with UHD  
> so that it won’t just print to the screen and hang. Is there any way  
> to implement this handler in Python and register the function with  
> UHD? I don’t normally post to both lists to avoid redundant spamming,  
> but I’ll take a leap of faith here. 🙂

I think I see your point. There really isnt a good way to know that some  
internal thread died. Whether it be in UHD or a gnuradio worker thread.

It might be a decent idea to queue up an async message into the queue  
when an internal thread dies. Not sure how you might get the info from a  
gnuradio scheduler thread though.

In the short term, if you do try to communicate with the device object  
in someway that involves an over-the-bus communication, and the  
transport is down, you will get a nice exception thrown all the way into  
python which you can catch and react to.

Example, periodically set\_time\_source(..) which is harmless to do, but  
causes bus traffic.

-josh

---

<div class="post-metadata">

**Author:** ![NowlanS\_Sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f05b48/32.png) [@NowlanS\_Sean](https://www.ruby-forum.com/u/NowlanS_Sean)\
**Post date:** [July 20, 2012, 2:19pm UTC](https://www.ruby-forum.com/t/uhd-error-handling-from-gnu-radio-python/226353/3 "2012-07-20T14:19:42Z")

</div>

> > least, not that I could find). Instead, UHD prints to stdout using
> 
> I think I see your point. There really isnt a good way to know that some internal  
> thread died. Whether it be in UHD or a gnuradio worker thread.

In this case, I’m not sure if a thread actually dies. It just goes into  
a loop or some kind of blocking state waiting for UHD control message  
responses.

> It might be a decent idea to queue up an async message into the queue when an  
> internal thread dies. Not sure how you might get the info from a gnuradio  
> scheduler \>thread though.

I haven’t needed this, but I could see how it could be useful.

> In the short term, if you do try to communicate with the device object in someway  
> that involves an over-the-bus communication, and the transport is down, you will  
> get a nice exception thrown all the way into python which you can catch and react  
> to.

I’ve seen it throw an exception if the USRP is not connected when the  
UHD GNU Radio block is first instantiated. I’ll try running a heartbeat  
“while True” loop in the main thread and see if I can get it to throw an  
error if I yank the Ethernet cable after the top block has already been  
started.

I’m still curious though, do you know of a way to register a message  
handler function with UHD from Python? If I add %include  
\<uhd/utils/msg.hpp\> to gnuradio/gr-uhd-swig/uhd\_swig.i, will SWIG know  
how to translate a function pointer to Python, or would this require UHD  
to implement a shared pointer interface?  
[http://files.ettus.com/uhd\_docs/manual/html/general.html#disabling-or-redirecting-prints-to-stdout](http://files.ettus.com/uhd_docs/manual/html/general.html#disabling-or-redirecting-prints-to-stdout)

---

<div class="post-metadata">

**Author:** ![NowlanS\_Sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f05b48/32.png) [@NowlanS\_Sean](https://www.ruby-forum.com/u/NowlanS_Sean)\
**Post date:** [August 21, 2012, 10:27am UTC](https://www.ruby-forum.com/t/uhd-error-handling-from-gnu-radio-python/226353/4 "2012-08-21T10:27:55Z")

</div>

Hi!

I am trying to do the same thing, did you have any success registering  
message\_handler with UHD?

best regards,  
Urban K.
