I've attached the cpp files that I have written for the USRP. Some of the
other functions defined in the hpp file are implemented in other files that
I have not included as they are not relevant.

I can't necessarily speak for what Windows may or may not be doing because
"Windows", but for the housekeeping, I think I am doing that fine, ( see
end_tx function in n_usrp_impl.cpp ) when I Stop the flowgraph, wait for it
to finish then reset it to clear the memory.

Also to reiterate these errors occur when using GNURadio python and the
companion to create the same code.

Thanks for the help.
-----------------------------
Jacob Knoles



On Mon, Jul 9, 2018 at 12:38 PM Marcus D. Leech <mle...@ripnet.com> wrote:

> On 07/09/2018 02:00 PM, Jacob Knoles wrote:
> > When using GNURadio Companion, they only appear when closing the
> > flowgraph. On my application, which uses the same gnuradio code
> > directly in c++ multiple times ( creates a usrp, then a flowgraph that
> > uses it, stops the flowgraph, then starts another after a few seconds)
> > I see the errors at the end of the first, then during streaming on all
> > subsequent flowgraphs.
> >
> > Occasionally I am getting a IOError: no response packet AssertionError
> > when I start streaming (typically after a couple of other stream
> > instances) and it causes my entire application to hang.
> >
> > -----------------------------
> > Jacob Knoles
> >
> The issue may be twofold:
>
> (A) When windows shuts-down a socket, it may do so in a way that is
> "dirty" from UHD's perspective
>
> (B) The correct housekeeping is probably not being done by your app to
> deal with flow-graph shutdown and restart, leading to "dangling"
>        socket descriptors and the like.
>
> If you could post the relevant parts of your code, or reduce it to the
> minimum that shows the issue and post that, it would help us
>    decide whether this is a bug in UHD, or you're just using it improperly.
>
>
>
// Start transmitting asynchronously a pre-generated signal.
// 1) load the signal from the given file into memory
// 2) Send the loaded signal to the USRP repeatedly. 

#include "n_usrp_impl.hpp"

#include <gnuradio/top_block.h>
#include <gnuradio/blocks/file_source.h>
#include <gnuradio/blocks/vector_sink_c.h>
#include <gnuradio/blocks/vector_source_c.h>
#include <gnuradio/blocks/throttle.h>
#include "utils.hpp"

namespace mitest
{
    namespace usrp
    {
        int n_usrp::n_usrp_impl::begin_tx( const std::string& data_file, const 
double bandwidth )
        {
            if ( _is_signal_on )
            {
                _lgr->log( severity::warn, "Attempted to create new Flow Graph 
with one running." );
                return -999;
            }

            try
            {
                _flow_graph = gr::make_top_block( "Transmitter" );
                gr::top_block_sptr tb = gr::make_top_block( "Load_Data" );

                ///////////////////////////////
                // blocks
                ///////////////////////////////

                gr::blocks::file_source::sptr file_source = 
gr::blocks::file_source::make(
                    sizeof( gr_complex ), data_file.c_str( ), false );

                gr::blocks::vector_sink_c::sptr vsink = 
gr::blocks::vector_sink_c::make( );

                if ( bandwidth >= 100e6 ) _samp_rate = 200e6;
                else if ( static_cast<int>(200e6 / bandwidth) % 2 == 0 ) 
_samp_rate = bandwidth;
                else if ( bandwidth < 20e6 ) _samp_rate = 20e6;
                else _samp_rate = 100e6;

                set_samp_rate( _samp_rate );

                if ( _sink->get_samp_rate( ) != _samp_rate ) _samp_rate = 
_sink->get_samp_rate( );

                gr::blocks::throttle::sptr throttle = 
gr::blocks::throttle::make(
                    sizeof( gr_complex ), _samp_rate, true );

                ///////////////////////////////
                // Connections
                ///////////////////////////////

                tb->connect( file_source, 0, throttle, 0 );
                tb->connect( throttle, 0, vsink, 0 );

                // Load the data from the file
                tb->run( );

                // Setup TX
                gr::blocks::vector_source_c::sptr vsource = 
gr::blocks::vector_source_c::make(
                    vsink->data( ), true, 1 );
                _flow_graph->connect( vsource, 0, _sink, 0 );

                _flow_graph->start( );
                _is_signal_on = true;
            }
            catch ( ... )
            {
                _lgr->log( std::current_exception( ) );
                _is_disabled = true;
            }
            return 0;
        }
    }
}
#include "n_usrp_impl.hpp"

#include <gnuradio/top_block.h>

namespace mitest
{
    namespace usrp
    {
        n_usrp::n_usrp_impl::n_usrp_impl( int log_verbosity )
            : _ampl( 0.7 ),
              _device_addr( "" ),
              _is_disabled( false ),
              _samp_rate( 100e6 ),
              _serial_num( "TEST CPP" ),
              _is_x300( true ),
              _is_signal_on( false ),
              _flow_graph( nullptr )
        {
            _lgr = types::logger::make( log_verbosity );
            _lgr->log( severity::info, "==================== Initializing 
====================" );

            find_devices( );
            if ( _is_disabled )
            {
                _lgr->log( severity::error, "No USRP device found, cannot 
continue" );
                throw std::exception( "No USRP device found, cannot continue" );
            }

            _lgr->log( severity::debug, "Creating initial USRP device" );

            uhd::stream_args_t args = uhd::stream_args_t( "fc32", "sc16" );
            args.channels.push_back( 1 );
            _sink = gr::uhd::usrp_sink::make( _device_addr, args );
            args.channels.pop_back( );
            args.channels.push_back( 0 );
            _source = gr::uhd::usrp_source::make( _device_addr, args );
        }

        double n_usrp::n_usrp_impl::get_tx_gain( size_t chan )
        {
            if ( _is_disabled ) return -1.0;
            try { return _sink->get_gain( chan ); }
            catch ( uhd::exception& )
            {
                                _lgr->log( std::current_exception( ) );
                _is_disabled = true;
                return -1.0;
            }
        }

        double n_usrp::n_usrp_impl::get_tx_gain_limit( )
        {
            if ( _is_disabled ) return -1.0;
            try { return _sink->get_gain_range( ).stop( ); }
            catch ( uhd::exception& )
            {
                                _lgr->log( std::current_exception( ) );
                _is_disabled = true;
                return -1.0;
            }
        }

        double n_usrp::n_usrp_impl::get_rx_gain( size_t chan )
        {
            if ( _is_disabled ) return -1.0;
            try { return _source->get_gain( chan ); }
            catch ( uhd::exception& )
            {
                                _lgr->log( std::current_exception( ) );
                _is_disabled = true;
                return -1.0;
            }
        }

        double n_usrp::n_usrp_impl::get_rx_gain_limit( )
        {
            if ( _is_disabled ) return -1.0;
            try { return _source->get_gain_range( ).stop( ); }
            catch ( uhd::exception& )
            {
                                _lgr->log( std::current_exception( ) );
                _is_disabled = true;
                return -1.0;
            }
        }

        double n_usrp::n_usrp_impl::get_tx_freq( size_t chan )
        {
            if ( _is_disabled ) return -1.0;
            try { return _sink->get_center_freq( chan ); }
            catch ( uhd::exception& )
            {
                                _lgr->log( std::current_exception( ) );
                _is_disabled = true;
                return -1.0;
            }
        }

        double n_usrp::n_usrp_impl::get_rx_freq( size_t chan )
        {
            if ( _is_disabled ) return -1.0;
            try { return _source->get_center_freq( chan ); }
            catch ( uhd::exception& )
            {
                                _lgr->log( std::current_exception( ) );
                _is_disabled = true;
                return -1.0;
            }
        }

        void n_usrp::n_usrp_impl::set_tx_gain( double gain, size_t chan )
        {
            if ( _is_disabled ) return;
            try 
                        { 
                _lgr->log( severity::trace, boost::format( "Setting TX gain to: 
%2f dB" ) % gain );
                                _sink->set_gain( gain, chan );
                _lgr->log( severity::trace, boost::format( "Actual gain: %2f 
dB" ) % _sink->get_gain( )  );
                        }
            catch ( uhd::exception& )
            {
                                _lgr->log( std::current_exception( ) );
                _is_disabled = true;
            }
        }

        void n_usrp::n_usrp_impl::set_rx_gain( double gain, size_t chan )
        {
            if ( _is_disabled ) return;
            _source->set_gain( chan );
            try 
            { 
                _lgr->log( severity::trace, boost::format( "Setting RX gain to 
%2f dB" ) % gain );
                _source->set_center_freq( gain, chan ); 
                _lgr->log( severity::trace, boost::format( "Actual RX gain: %2f 
dB" ) % _source->get_gain( ) );
            }
            catch ( uhd::exception& )
            {
                                _lgr->log( std::current_exception( ) );
                _is_disabled = true;
            }
        }

        void n_usrp::n_usrp_impl::set_tx_freq( double freq, size_t chan, double 
offset )
        {
            if ( _is_disabled ) return;
            try 
                        { 
                _lgr->log( severity::trace, boost::format( "Setting TX center 
frequency to: %2f MHz" ) % (freq / 1e6) );
                                _sink->set_center_freq( uhd::tune_request_t( 
freq, offset ), chan );
                _lgr->log( severity::trace, boost::format( "Actual center 
frequency: %2f MHz" ) % (_sink->get_center_freq( ) / 1e6) );
                        }
            catch ( uhd::exception& )
            {
                                _lgr->log( std::current_exception( ) );
                _is_disabled = true;
            }
        }

        void n_usrp::n_usrp_impl::set_rx_freq( double freq, size_t chan )
        {
            if ( _is_disabled ) return;
            try 
            {
                _lgr->log( severity::trace, boost::format( "Setting RX center 
frequency to %2f MHz" ) % (freq / 1e6) );
                                _source->set_center_freq( uhd::tune_request_t( 
freq, -20e6 ), chan );
                _lgr->log( severity::trace, boost::format( "Actual RX 
Frequency: %2f MHz" ) % (_source->get_center_freq( ) / 1e6) );
            }
            catch ( uhd::exception& )
            {
                                _lgr->log( std::current_exception( ) );
                _is_disabled = true;
            }
        }

        void n_usrp::n_usrp_impl::set_samp_rate( double samp_rate ) 
                {  
            if ( _is_disabled ) return;
            try 
                        { 
                _lgr->log( severity::trace, boost::format( "Setting USRP sample 
rate to: %2f MSps" ) % (samp_rate / 1e6) );
                                _sink->set_samp_rate( samp_rate );
                                _samp_rate = _sink->get_samp_rate( );
                _lgr->log( severity::trace, boost::format( "Actual sample rate: 
%2f MSps" ) % (_samp_rate / 1e6) );
                        }
            catch ( uhd::exception& )
            {
                                _lgr->log( std::current_exception( ) );
                _is_disabled = true;
            }
                }


        std::string n_usrp::n_usrp_impl::get_serial_num( ) const { return 
_serial_num; }

        void n_usrp::n_usrp_impl::generate_signal( const std::string& 
output_path,
                                                   operational_mode 
operational_mode,
                                                   mod_signal_t sig_type,
                                                   modulation_t mod_type )
        {
            try
            {
                const types::modulation_variables::sptr args = 
types::modulation_variables::make(
                    output_path, operational_mode, sig_type, mod_type );
                switch ( sig_type )
                {
                    case awgn: break;
                    case cw: return;
                    case lte: break;
                    case ofdm: generate_ofdm_signal( args );
                        break;
                    default: throw std::exception( "Invalid Argument: 
mod_signal_t" );
                }
            }
            catch ( ... ) { _lgr->log( std::current_exception( ) ); }
        }

        void n_usrp::n_usrp_impl::end_tx( )
        {
            if ( _flow_graph == nullptr ) return;

                        try
                        {
                                _flow_graph->stop( );
                                _flow_graph->wait( );
                                //_flow_graph->disconnect_all( );

                                _flow_graph.reset( );
                                _flow_graph = nullptr;
                                _is_signal_on = false;
                        }
            catch(...)
                        { 
                                _lgr->log( std::current_exception( ) ); 
                        }
        }

        void n_usrp::n_usrp_impl::find_devices( )
        {
            _lgr->log( severity::info, "Searching for connected USRP devices." 
);
            try
            {
                _lgr->log( severity::trace, boost::format( "Using search hint: 
'%s'" ) % _device_addr.to_string( ) );
                auto devices = uhd::device::find( _device_addr );

                if ( devices.size( ) > size_t( 0 ) )
                {
                    _lgr->log( severity::debug, boost::format( "Found %d 
devices" ) % devices.size( ) );
                    _serial_num = devices[0]["serial"];
                    if ( devices.size( ) > size_t( 1 ) )
                    {
                        _device_addr = uhd::device_addr_t( str(
                            boost::format( "type=%s,addr=%s,second_addr=%s" ) % 
devices[0]["type"] % devices[0]["addr"]
                            % devices[1]["addr"] ) );
                        _is_x300 = true;
                    }
                    else
                    {
                        _device_addr = uhd::device_addr_t( str( boost::format( 
"type=%s" ) % devices[0]["type"] ) );
                        _is_x300 = false;
                        _samp_rate = 26e6;
                    }
                }
                else
                {
                    _lgr->log( severity::error, "NO DEVICES FOUND!!" );
                    _serial_num = std::string( "NONE FOUND" );
                    _is_disabled = true;
                }
            }
            catch ( ... ) 
                        { 
                                _lgr->log( std::current_exception( ) );
                                _serial_num = "ERROR";
                        }
        }

        burst_tracker_results n_usrp::n_usrp_impl::get_tracker_results( ) const
        { 
                        burst_tracker_results results = burst_tracker_results( 
);
                        if ( _burst_tracker != nullptr )
                        {
                                results.set_burst_count( 
_burst_tracker->burst_count( ) );
                                results.set_time_on( time_spec_t( 
_burst_tracker->full_secs( ), _burst_tracker->frac_secs( ) ) );
                        }
                        return results;
                }


    }
}
#ifndef N_USRP_IMPL_HPP
#define N_USRP_IMPL_HPP

#include "n_usrp.hpp"
#include "types/logger.hpp"
#include "types/modulation_variables.hpp"
#include "blocks/burst_tracker_ff.hpp"
#include "../../MiTest.Usrp.common/burst_tracker_results.hpp"

#include <gnuradio/uhd/usrp_sink.h>
#include <gnuradio/uhd/usrp_source.h>

namespace mitest
{
    namespace usrp
    {
        class n_usrp::n_usrp_impl
        {
        public:
            n_usrp_impl( int log_verbosity );

            ~n_usrp_impl( ) = default;

            n_usrp_impl( const n_usrp_impl& other ) = delete;

            n_usrp_impl( n_usrp_impl&& other ) noexcept
                : _lgr( std::move( other._lgr ) ),
                  _ampl( other._ampl ),
                  _device_addr( std::move( other._device_addr ) ),
                  _is_disabled( other._is_disabled ),
                  _samp_rate( other._samp_rate ),
                  _serial_num( std::move( other._serial_num ) ),
                  _is_x300( other._is_x300 ),
                  _is_signal_on( other._is_signal_on ),
                  _sink( std::move( other._sink ) ),
                  _source( std::move( other._source ) ) {}

            n_usrp_impl& operator=( const n_usrp_impl& other ) = delete;

            n_usrp_impl& operator=( n_usrp_impl&& other ) noexcept
            {
                if ( this == &other ) return *this;
                _lgr = std::move( other._lgr );
                _ampl = other._ampl;
                _device_addr = std::move( other._device_addr );
                _is_disabled = other._is_disabled;
                _samp_rate = other._samp_rate;
                _serial_num = std::move( other._serial_num );
                _is_x300 = other._is_x300;
                _is_signal_on = other._is_signal_on;
                _sink = std::move( other._sink );
                _source = std::move( other._source );
                return *this;
            }

            double get_tx_gain( size_t chan = 0 );

            double get_tx_gain_limit( );

            double get_rx_gain( size_t chan = 0 );

            double get_rx_gain_limit( );

            double get_tx_freq( size_t chan = 0 );

            double get_rx_freq( size_t chan = 0 );

            std::string get_serial_num( ) const;

            void set_tx_gain( double gain, size_t chan = 0 );

            void set_rx_gain( double gain, size_t chan = 0 );

                        void set_tx_freq( double freq, size_t chan = 0, double 
offset = 0 );

            void set_rx_freq( double freq, size_t chan = 0 );

            void set_samp_rate( double samp_rate );

            void generate_signal( const std::string& output_path,
                                  operational_mode operational_mode,
                                  mod_signal_t sig_type,
                                  modulation_t mod_type );

            int begin_tx( const std::string& data_file, double bandwidth );

                        int begin_cw( );

            void end_tx( );

            void initiate_tx_rx( const std::string& data_file,
                                 float trigger_level,
                                 double bandwidth,
                                 uhd::time_spec_t& tx_start,
                                 uhd::time_spec_t& tx_stop );

            burst_tracker_results get_tracker_results( ) const;

        private:
            types::logger::sptr _lgr;

            double _ampl;
            uhd::device_addr_t _device_addr;
            bool _is_disabled;
            double _samp_rate;
            std::string _serial_num;
            bool _is_x300;
            bool _is_signal_on{ };

            gr::uhd::usrp_sink::sptr _sink;
            gr::uhd::usrp_source::sptr _source;

            blocks::burst_tracker_ff::sptr _burst_tracker;
            gr::top_block_sptr _flow_graph;

            void find_devices( );

            std::string generate_ofdm_signal( types::modulation_variables::sptr 
args );
        };
    }
}
#endif // N_USRP_IMPL_HPP
_______________________________________________
USRP-users mailing list
USRP-users@lists.ettus.com
http://lists.ettus.com/mailman/listinfo/usrp-users_lists.ettus.com

Reply via email to