Getting Started with UHD and C++

From Ettus Knowledge Base
Revision as of 11:54, 10 August 2017 by Ettus (Talk | contribs) (CMake)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Application Note Number


Revision History

Date Author Details
2016-05-01 Neel Pandeya
Nate Temple
Initial creation


This AN explains how to write and build C++ programs that use the UHD API.


This Application Note will walk through building a basic C++ program with UHD. This program will initialize, configure the USRP device, set the sample rate, frequency, gain, bandwidth, and select the antenna.

UHD Manual

The UHD Manual is hosted at and provides information on how to use the USRP devices and how to use the UHD API to connect to them through your own software. The manual is split into two parts: The device manual, and the UHD/API manual. The first part describes details of Ettus Research devices, motherboards and daughterboards, as well as aspects of using UHD. The second is meant for developers writing UHD-based applications, and includes descriptions of the API, sorted by namespaces, classes, and files.

Including Header Files

    #include <uhd/utils/thread_priority.hpp>
    #include <uhd/utils/safe_main.hpp>
    #include <uhd/usrp/multi_usrp.hpp>
    #include <uhd/exception.hpp>
    #include <uhd/types/tune_request.hpp>
    #include <boost/program_options.hpp>
    #include <boost/format.hpp>
    #include <boost/thread.hpp>
    #include <iostream>


Defines a safe wrapper that places a catch-all around main. If an exception is thrown, it prints to stderr and returns.

    int UHD_SAFE_MAIN(int argc, char *argv[]) {


Set the scheduling priority on the current thread. Same as set_thread_priority but does not throw on failure.


Create Variables

    std::string device_args("addr=");
    std::string subdev("A:0");
    std::string ant("TX/RX");
    std::string ref("internal");

    double rate(1e6);
    double freq(915e6);
    double gain(10);
    double bw(1e6);

Creating a USRP Object

Make a new multi usrp from the device address.

    uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(device_args);

Setting the Motherboard Clocks

Set the clock source for the usrp device. This sets the source for a 10 MHz reference clock. Typical options for source: internal, external, MIMO.


Selecting the Sub Device

Set the RX frontend specification. The subdev spec maps a physical part of a daughter-board to a channel number. Set the subdev spec before calling into any methods with a channel number. The subdev spec must be the same size across all motherboards. For more details on selecting the Sub Device, see the Specifying the Subdevice section of the UHD Manual.


Getting the selected Sub Device

Get a printable summary for this USRP configuration.


Setting the Sample Rate

Set the RX sample rate. The rate is in Samples Per Second.


Getting the Sample Rate

Gets the RX sample rate. Returns the rate is in Samples Per Second.


Setting Center Frequency

Create a tune request, with the RF frequency in Hz. Set the RX center frequency.

    uhd::tune_request_t tune_request(freq)

Getting the Center Frequency

Get the RX center frequency. Returns the frequency in Hz.


Setting the RF Gain

Set the RX gain value for the specified gain element. For an empty name, distribute across all gain elements. Sets the gain in dB.


Reading the RF Gain

Get the RX gain value for the specified gain element. For an empty name, sum across all gain elements. Returns the gain in dB.


Setting the IF Filter Bandwidth

Set the RX bandwidth on the frontend. Sets the bandwidth in Hz.


Getting the IF Filter Bandwidth

Get the RX bandwidth on the frontend. Returns the bandwidth in Hz.


Selecting the Antenna

Select the RX antenna on the frontend.


Getting the Antenna

Get the selected RX antenna on the frontend. Returns the antenna name.



    return EXIT_SUCCESS;

Full Example

#include <uhd/utils/thread_priority.hpp>
#include <uhd/utils/safe_main.hpp>
#include <uhd/usrp/multi_usrp.hpp>
#include <uhd/exception.hpp>
#include <uhd/types/tune_request.hpp>
#include <boost/program_options.hpp>
#include <boost/format.hpp>
#include <boost/thread.hpp>
#include <iostream>

int UHD_SAFE_MAIN(int argc, char *argv[]) {

    std::string device_args("addr=");
    std::string subdev("A:0");
    std::string ant("TX/RX");
    std::string ref("internal");

    double rate(1e6);
    double freq(915e6);
    double gain(10);
    double bw(1e6);

    //create a usrp device
    std::cout << std::endl;
    std::cout << boost::format("Creating the usrp device with: %s...") % device_args << std::endl;
    uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(device_args);

    // Lock mboard clocks
    std::cout << boost::format("Lock mboard clocks: %f") % ref << std::endl;
    //always select the subdevice first, the channel mapping affects the other settings
    std::cout << boost::format("subdev set to: %f") % subdev << std::endl;
    std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl;

    //set the sample rate
    if (rate <= 0.0) {
        std::cerr << "Please specify a valid sample rate" << std::endl;
        return ~0;

    // set sample rate
    std::cout << boost::format("Setting RX Rate: %f Msps...") % (rate / 1e6) << std::endl;
    std::cout << boost::format("Actual RX Rate: %f Msps...") % (usrp->get_rx_rate() / 1e6) << std::endl << std::endl;

    // set freq
    std::cout << boost::format("Setting RX Freq: %f MHz...") % (freq / 1e6) << std::endl;
    uhd::tune_request_t tune_request(freq);
    std::cout << boost::format("Actual RX Freq: %f MHz...") % (usrp->get_rx_freq() / 1e6) << std::endl << std::endl;

    // set the rf gain
    std::cout << boost::format("Setting RX Gain: %f dB...") % gain << std::endl;
    std::cout << boost::format("Actual RX Gain: %f dB...") % usrp->get_rx_gain() << std::endl << std::endl;

    // set the IF filter bandwidth
    std::cout << boost::format("Setting RX Bandwidth: %f MHz...") % (bw / 1e6) << std::endl;
    std::cout << boost::format("Actual RX Bandwidth: %f MHz...") % (usrp->get_rx_bandwidth() / 1e6) << std::endl << std::endl;

    // set the antenna
    std::cout << boost::format("Setting RX Antenna: %s") % ant << std::endl;
    std::cout << boost::format("Actual RX Antenna: %s") % usrp->get_rx_antenna() << std::endl << std::endl;

    return EXIT_SUCCESS;


Use the uhd/host/examples/init_usrp/CMakeLists.txt file as template

  • Add the names of your C++ source files to the add_executable(...) section
  • Put both modified CMakeLists.txt file and C++ file into an empty folder

uhd cpp makefile edits.png

Compile and Install

  • Create a “build” folder and invoke CMake the usual way:
   mkdir build
   cd build
   cmake ../

Running the Application

$ ./usrp_basic
linux; GNU C++ version 4.8.4; Boost_105400; UHD_003.010.git-202-g9e0861e1

Creating the usrp device with: addr=
-- Opening a USRP2/N-Series device...
-- Current recv frame size: 1472 bytes
-- Current send frame size: 1472 bytes
Lock mboard clocks: internal
subdev set to: A:0
Using Device: Single USRP:
  Device: USRP2 / N-Series Device
  Mboard 0: N210r4
  RX Channel: 0
    RX DSP: 0
    RX Dboard: A
    RX Subdev: WBXv2 RX+GDB
  TX Channel: 0
    TX DSP: 0
    TX Dboard: A
    TX Subdev: WBXv2 TX+GDB

Setting RX Rate: 1.000000 Msps...
Actual RX Rate: 1.000000 Msps...

Setting RX Freq: 915.000000 MHz...
Actual RX Freq: 915.000000 MHz...

Setting RX Gain: 10.000000 dB...
Actual RX Gain: 10.000000 dB...

Setting RX Bandwidth: 1.000000 MHz...
Actual RX Bandwidth: 1.000000 MHz...

Setting RX Antenna: TX/RX
Actual RX Antenna: TX/RX

Additional Example Programs

Additional C++ example programs using the UHD API are provided within the Ettus Research Github Repository, located in the host/examples/ directory. These examples are installed with UHD and will be located at $prefix/lib/uhd/examples directory of your system.