### Implement a Complete Receive Application Source: https://context7.com/pothosware/soapynetsdr/llms.txt A full C++ application example showing device discovery, configuration, and a continuous signal processing loop. ```cpp #include #include #include #include #include #include #include static bool running = true; void sigintHandler(int) { running = false; } int main() { signal(SIGINT, sigintHandler); // Discover and connect to NetSDR SoapySDR::KwargsList results = SoapySDR::Device::enumerate("driver=netsdr"); if (results.empty()) { std::cerr << "No NetSDR device found" << std::endl; return 1; } std::cout << "Connecting to: " << results[0].at("label") << std::endl; SoapySDR::Device* device = SoapySDR::Device::make(results[0]); // Configure receiver const double centerFreq = 14.074e6; // 20m FT8 const double sampleRate = 500e3; device->setFrequency(SOAPY_SDR_RX, 0, centerFreq); device->setSampleRate(SOAPY_SDR_RX, 0, sampleRate); device->setGain(SOAPY_SDR_RX, 0, -10.0); device->setBandwidth(SOAPY_SDR_RX, 0, 0.0); // Auto filter std::cout << "Tuned to " << device->getFrequency(SOAPY_SDR_RX, 0) / 1e6 << " MHz" << std::endl; std::cout << "Sample rate: " << device->getSampleRate(SOAPY_SDR_RX, 0) / 1e3 << " kHz" << std::endl; // Setup streaming std::vector channels = {0}; SoapySDR::Stream* rxStream = device->setupStream(SOAPY_SDR_RX, SOAPY_SDR_CF32, channels); const size_t bufferSize = device->getStreamMTU(rxStream) * 4; std::vector> buffer(bufferSize); void* buffs[] = {buffer.data()}; device->activateStream(rxStream, 0, 0, 0); std::cout << "Streaming started. Press Ctrl+C to stop." << std::endl; // Receive loop long long totalSamples = 0; while (running) { int flags = 0; long long timeNs = 0; int ret = device->readStream(rxStream, buffs, bufferSize, flags, timeNs, 100000); if (ret > 0) { totalSamples += ret; // Calculate signal level every ~1 second if (totalSamples % (int)sampleRate < bufferSize) { float power = 0.0f; for (int i = 0; i < ret; i++) { power += std::norm(buffer[i]); } power /= ret; float dB = 10.0f * std::log10(power + 1e-10f); std::cout << "\rSignal level: " << dB << " dBFS " << std::flush; } } else if (ret == SOAPY_SDR_TIMEOUT) { continue; } else { std::cerr << "readStream error: " << ret << std::endl; break; } } // Cleanup std::cout << "\nTotal samples received: " << totalSamples << std::endl; device->deactivateStream(rxStream, 0, 0); device->closeStream(rxStream); SoapySDR::Device::unmake(device); return 0; } ``` -------------------------------- ### Setup and Activate RX Stream for NetSDR Source: https://context7.com/pothosware/soapynetsdr/llms.txt Create and activate an RX stream for receiving I/Q samples in CF32 format. This stream delivers normalized samples in the range -1.0 to +1.0. ```cpp #include #include #include #include SoapySDR::Device* device = SoapySDR::Device::make("driver=netsdr"); // Configure the device device->setFrequency(SOAPY_SDR_RX, 0, 14.2e6); // 20m band device->setSampleRate(SOAPY_SDR_RX, 0, 500e3); device->setGain(SOAPY_SDR_RX, 0, 0.0); // Check supported stream formats std::vector formats = device->getStreamFormats(SOAPY_SDR_RX, 0); // Returns: SOAPY_SDR_CF32 (complex float32) // Get native format and scale double fullScale; std::string nativeFormat = device->getNativeStreamFormat(SOAPY_SDR_RX, 0, fullScale); std::cout << "Native format: " << nativeFormat << ", scale: " << fullScale << std::endl; // Setup receive stream std::vector channels = {0}; SoapySDR::Stream* rxStream = device->setupStream(SOAPY_SDR_RX, SOAPY_SDR_CF32, channels); // Get stream MTU (maximum transfer unit) size_t mtu = device->getStreamMTU(rxStream); std::cout << "Stream MTU: " << mtu << " samples" << std::endl; // 240 or 256 // Activate the stream to start receiving device->activateStream(rxStream, 0, 0, 0); // ... read samples ... // Deactivate and close when done device->deactivateStream(rxStream, 0, 0); device->closeStream(rxStream); SoapySDR::Device::unmake(device); ``` -------------------------------- ### CMake Minimum Version and Project Setup Source: https://github.com/pothosware/soapynetsdr/blob/master/CMakeLists.txt Sets the minimum required CMake version and defines the project name. Ensure your CMake version is compatible. ```cmake cmake_minimum_required(VERSION 3.1...3.10) project(SoapyNetSDR CXX) ``` -------------------------------- ### Create and Query Device Instance Source: https://context7.com/pothosware/soapynetsdr/llms.txt Initialize a device instance from enumeration results and query hardware identification properties. ```cpp #include #include #include #include // Create device from enumeration results SoapySDR::KwargsList results = SoapySDR::Device::enumerate("driver=netsdr"); if (results.empty()) { throw std::runtime_error("No NetSDR devices found"); } // Connect to the first discovered device SoapySDR::Device* device = SoapySDR::Device::make(results[0]); // Query device identification std::cout << "Driver: " << device->getDriverKey() << std::endl; // "netSDR" std::cout << "Hardware: " << device->getHardwareKey() << std::endl; // "NetSDR" std::cout << "Channels: " << device->getNumChannels(SOAPY_SDR_RX) << std::endl; // Clean up when done SoapySDR::Device::unmake(device); ``` -------------------------------- ### Read I/Q Sample Data with SoapySDR Source: https://context7.com/pothosware/soapynetsdr/llms.txt Demonstrates how to configure a device, activate a stream, and read interleaved complex float samples into a buffer. ```cpp #include #include #include #include #include #include SoapySDR::Device* device = SoapySDR::Device::make("driver=netsdr"); // Configure device device->setFrequency(SOAPY_SDR_RX, 0, 7.074e6); // FT8 frequency device->setSampleRate(SOAPY_SDR_RX, 0, 250e3); device->setGain(SOAPY_SDR_RX, 0, -10.0); // Setup and activate stream std::vector channels = {0}; SoapySDR::Stream* rxStream = device->setupStream(SOAPY_SDR_RX, SOAPY_SDR_CF32, channels); device->activateStream(rxStream, 0, 0, 0); // Allocate sample buffer const size_t numSamples = 1024; std::vector> buffer(numSamples); void* buffs[] = {buffer.data()}; // Read samples with 1 second timeout int flags = 0; long long timeNs = 0; long timeoutUs = 1000000; // 1 second int ret = device->readStream(rxStream, buffs, numSamples, flags, timeNs, timeoutUs); if (ret > 0) { std::cout << "Received " << ret << " samples" << std::endl; // Process samples - compute average power float power = 0.0f; for (int i = 0; i < ret; i++) { power += std::norm(buffer[i]); // |I|^2 + |Q|^2 } power /= ret; std::cout << "Average power: " << 10.0f * std::log10(power) << " dB" << std::endl; } else if (ret == SOAPY_SDR_TIMEOUT) { std::cout << "Timeout waiting for samples" << std::endl; } else if (ret == SOAPY_SDR_STREAM_ERROR) { std::cout << "Stream error occurred" << std::endl; } // Cleanup device->deactivateStream(rxStream, 0, 0); device->closeStream(rxStream); SoapySDR::Device::unmake(device); ``` -------------------------------- ### Creating a Device Instance Source: https://context7.com/pothosware/soapynetsdr/llms.txt This section explains how to create an instance of a NetSDR device using the SoapySDR factory after discovery. ```APIDOC ## Creating a Device Instance ### Description Connect to a discovered NetSDR device by passing the device arguments to the SoapySDR factory. The constructor establishes TCP and UDP connections, queries device information (product ID, firmware versions, hardware options), and configures the receiver channel mode. ### Method `SoapySDR::Device::make(const SoapySDR::Kwargs &args)` ### Parameters #### Request Body - **args** (SoapySDR::Kwargs) - Required - A dictionary of arguments specifying the device to connect to. Typically includes 'driver' and 'serial' or other discovery results. ### Request Example ```json { "driver": "netsdr", "serial": "12345" } ``` ### Response #### Success Response (Device Object) - **device** (SoapySDR::Device*) - A pointer to the created SoapySDR::Device object. ### Response Example ```cpp // Assuming 'device' is a valid SoapySDR::Device* pointer std::cout << "Driver: " << device->getDriverKey() << std::endl; std::cout << "Hardware: " << device->getHardwareKey() << std::endl; std::cout << "Channels: " << device->getNumChannels(SOAPY_SDR_RX) << std::endl; ``` ### Code Example (C++) ```cpp #include #include #include #include // Create device from enumeration results SoapySDR::KwargsList results = SoapySDR::Device::enumerate("driver=netsdr"); if (results.empty()) { throw std::runtime_error("No NetSDR devices found"); } // Connect to the first discovered device SoapySDR::Device* device = SoapySDR::Device::make(results[0]); // Query device identification std::cout << "Driver: " << device->getDriverKey() << std::endl; // "netSDR" std::cout << "Hardware: " << device->getHardwareKey() << std::endl; // "NetSDR" std::cout << "Channels: " << device->getNumChannels(SOAPY_SDR_RX) << std::endl; // Clean up when done SoapySDR::Device::unmake(device); ``` ``` -------------------------------- ### Enumerate and Connect to NetSDR Devices Source: https://context7.com/pothosware/soapynetsdr/llms.txt Use device enumeration to find available hardware on the network and connect using specific arguments like serial numbers. ```cpp #include #include // Enumerate all available NetSDR devices on the network SoapySDR::KwargsList results = SoapySDR::Device::enumerate("driver=netsdr"); for (const auto& device : results) { std::cout << "Found device: " << device.at("name") << std::endl; std::cout << " Serial: " << device.at("serial") << std::endl; std::cout << " Address: " << device.at("netsdr") << std::endl; std::cout << " Label: " << device.at("label") << std::endl; } // Connect to a specific device by serial number SoapySDR::Kwargs args; args["driver"] = "netsdr"; args["serial"] = "12345"; // Filter by specific serial number SoapySDR::Device* device = SoapySDR::Device::make(args); ``` -------------------------------- ### Platform-Specific Network Interface Source Files Source: https://github.com/pothosware/soapynetsdr/blob/master/CMakeLists.txt Selects the appropriate source file for network interface enumeration based on the operating system. Use InterfacesWindows.cpp for Windows and InterfacesUnix.cpp for other systems. ```cmake if (WIN32) target_sources(netSDRSupport PRIVATE InterfacesWindows.cpp) else () target_sources(netSDRSupport PRIVATE InterfacesUnix.cpp) endif () ``` -------------------------------- ### Configure Sample Rate for NetSDR Source: https://context7.com/pothosware/soapynetsdr/llms.txt Set the I/Q output sample rate for the NetSDR. Supported rates range from 20 kHz to 2 MHz. The sample rate affects the bit format used for streaming. ```cpp #include #include SoapySDR::Device* device = SoapySDR::Device::make("driver=netsdr"); // List available sample rates std::vector rates = device->listSampleRates(SOAPY_SDR_RX, 0); std::cout << "Available sample rates:" << std::endl; for (double rate : rates) { std::cout << " " << rate / 1e3 << " kHz" << std::endl; } // Output: 20, 32, 40, 50, 80, 100, 125, 160, 200, 250, 500, 625, 800, 1000, 1250, 2000 kHz // Set sample rate to 250 kHz (uses 24-bit mode) device->setSampleRate(SOAPY_SDR_RX, 0, 250e3); // Verify sample rate double actualRate = device->getSampleRate(SOAPY_SDR_RX, 0); std::cout << "Sample rate: " << actualRate / 1e3 << " kHz" << std::endl; SoapySDR::Device::unmake(device); ``` -------------------------------- ### Device Discovery Source: https://context7.com/pothosware/soapynetsdr/llms.txt This section details how to discover NetSDR devices on the local network using UDP broadcast and how to enumerate them with SoapySDR. ```APIDOC ## Device Discovery and Registration ### Description The plugin automatically discovers NetSDR devices on the local network using UDP broadcast messages. When SoapySDR enumerates available devices, SoapyNetSDR sends discovery requests on port 48321 and listens for responses on port 48322. Discovered devices are returned with their name, serial number, IP address, and port information. ### Method `SoapySDR::Device::enumerate("driver=netsdr")` ### Parameters #### Query Parameters - **driver** (string) - Required - Specifies the driver to use, in this case, "netsdr". ### Response Example ```json [ { "name": "NetSDR", "serial": "12345", "netsdr": "192.168.1.100:5000", "label": "NetSDR Receiver" } ] ``` ### Code Example (C++) ```cpp #include #include // Enumerate all available NetSDR devices on the network SoapySDR::KwargsList results = SoapySDR::Device::enumerate("driver=netsdr"); for (const auto& device : results) { std::cout << "Found device: " << device.at("name") << std::endl; std::cout << " Serial: " << device.at("serial") << std::endl; std::cout << " Address: " << device.at("netsdr") << std::endl; std::cout << " Label: " << device.at("label") << std::endl; } // Connect to a specific device by serial number SoapySDR::Kwargs args; args["driver"] = "netsdr"; args["serial"] = "12345"; // Filter by specific serial number SoapySDR::Device* device = SoapySDR::Device::make(args); ``` ``` -------------------------------- ### Windows Network Library Linking Source: https://github.com/pothosware/soapynetsdr/blob/master/CMakeLists.txt Adds definitions and links the ws2_32 library for Windows. This is required for network functionality on Windows systems. ```cmake if (WIN32) add_definitions(-D_CRT_SECURE_NO_WARNINGS) add_definitions(-D_WINSOCK_DEPRECATED_NO_WARNINGS) target_link_libraries(netSDRSupport PRIVATE ws2_32) endif (WIN32) ``` -------------------------------- ### Configure RF Bandwidth Filter for NetSDR Source: https://context7.com/pothosware/soapynetsdr/llms.txt Configure the RF bandpass filter for the NetSDR. Setting bandwidth to 0 enables automatic filter selection. Setting bandwidth to 34 MHz bypasses the bandpass filter. ```cpp #include #include SoapySDR::Device* device = SoapySDR::Device::make("driver=netsdr"); // List available bandwidths std::vector bandwidths = device->listBandwidths(SOAPY_SDR_RX, 0); for (double bw : bandwidths) { std::cout << "Bandwidth: " << bw / 1e6 << " MHz" << std::endl; } // Output: 34 MHz (antialiasing filter bypass) // Enable automatic bandpass filter selection device->setBandwidth(SOAPY_SDR_RX, 0, 0.0); // Or bypass bandpass filter for wideband reception device->setBandwidth(SOAPY_SDR_RX, 0, 34e6); // Query current bandwidth setting double bw = device->getBandwidth(SOAPY_SDR_RX, 0); std::cout << "Current bandwidth: " << bw / 1e6 << " MHz" << std::endl; SoapySDR::Device::unmake(device); ``` -------------------------------- ### Define SoapySDR Module Target and Sources Source: https://github.com/pothosware/soapynetsdr/blob/master/CMakeLists.txt Defines the NetSDR support module target and lists its source files. This is a core part of building the SoapySDR module. ```cmake SOAPY_SDR_MODULE_UTIL( TARGET netSDRSupport SOURCES NetSDR_Registration.cpp NetSDR_Settings.cpp NetSDR_Streaming.cpp ) ``` -------------------------------- ### Configure Receiver Frequency Source: https://context7.com/pothosware/soapynetsdr/llms.txt Retrieve supported frequency ranges and set the center frequency for the receiver channel. ```cpp #include #include SoapySDR::Device* device = SoapySDR::Device::make("driver=netsdr"); // Get supported frequency ranges SoapySDR::RangeList ranges = device->getFrequencyRange(SOAPY_SDR_RX, 0); for (const auto& range : ranges) { std::cout << "Frequency range: " << range.minimum() / 1e6 << " - " << range.maximum() / 1e6 << " MHz" << std::endl; } // Set center frequency to 7.1 MHz (40m amateur band) double frequency = 7.1e6; device->setFrequency(SOAPY_SDR_RX, 0, frequency); // Verify the frequency was set double actualFreq = device->getFrequency(SOAPY_SDR_RX, 0); std::cout << "Tuned to: " << actualFreq / 1e6 << " MHz" << std::endl; SoapySDR::Device::unmake(device); ``` -------------------------------- ### Find and Verify SoapySDR Package Source: https://github.com/pothosware/soapynetsdr/blob/master/CMakeLists.txt Finds the SoapySDR package using CMake's find_package. If not found, the build will terminate with an error message. ```cmake find_package(SoapySDR CONFIG) if (NOT SoapySDR_FOUND) message(FATAL_ERROR "Soapy SDR development files not found...") endif () ``` -------------------------------- ### Configure RF Gain/Attenuation for NetSDR Source: https://context7.com/pothosware/soapynetsdr/llms.txt Control the RF attenuator for the NetSDR. Supported attenuation levels range from 0 dB to -30 dB in 10 dB steps. Lower values increase signal attenuation. ```cpp #include #include SoapySDR::Device* device = SoapySDR::Device::make("driver=netsdr"); // Get gain range SoapySDR::Range gainRange = device->getGainRange(SOAPY_SDR_RX, 0); std::cout << "Gain range: " << gainRange.minimum() << " to " << gainRange.maximum() << " dB" << std::endl; // Output: -30 to 0 dB // List gain elements std::vector gains = device->listGains(SOAPY_SDR_RX, 0); for (const auto& name : gains) { std::cout << "Gain element: " << name << std::endl; // "ATT" } // Set attenuation to -10 dB device->setGain(SOAPY_SDR_RX, 0, -10.0); // Verify gain setting double gain = device->getGain(SOAPY_SDR_RX, 0); std::cout << "Current gain: " << gain << " dB" << std::endl; SoapySDR::Device::unmake(device); ``` -------------------------------- ### Enable C++11 Features with Compiler Checks Source: https://github.com/pothosware/soapynetsdr/blob/master/CMakeLists.txt Enables C++11 features for GNU and Clang compilers. It checks for C++11 support and falls back to C++0x if necessary. Thread support is enabled using -pthread. ```cmake if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") #C++11 is a required language feature for this project include(CheckCXXCompilerFlag) CHECK_CXX_COMPILER_FLAG("-std=c++11" HAS_STD_CXX11) if(HAS_STD_CXX11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") else(HAS_STD_CXX11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") endif() #Thread support enabled (not the same as -lpthread) list(APPEND RTLSDR_LIBRARIES -pthread) #disable warnings for unused parameters add_compile_options(-Wall -Wextra -Wno-unused-parameter -Wno-overloaded-virtual)) endif() ``` -------------------------------- ### Setting Receiver Frequency Source: https://context7.com/pothosware/soapynetsdr/llms.txt This section explains how to set and verify the center frequency of the NetSDR receiver using the SoapySDR API. ```APIDOC ## Setting Receiver Frequency ### Description Configure the receiver's center frequency using the setFrequency method. The frequency is sent to the device as a 32-bit unsigned integer via the NetSDR protocol. The getFrequency method queries the current tuned frequency from the hardware. ### Method `device->setFrequency(const int direction, const size_t channel, const double frequency)` `device->getFrequency(const int direction, const size_t channel)` ### Parameters #### Path Parameters - **direction** (int) - Required - The direction of the channel, SOAPY_SDR_RX for receive. - **channel** (size_t) - Required - The channel index (usually 0 for single-channel devices). #### Query Parameters - **frequency** (double) - Required - The desired center frequency in Hz. ### Request Example ```cpp // Set center frequency to 7.1 MHz (40m amateur band) double frequency = 7.1e6; device->setFrequency(SOAPY_SDR_RX, 0, frequency); ``` ### Response #### Success Response (Frequency) - **actualFreq** (double) - The actual frequency the receiver is tuned to, in Hz. ### Response Example ```cpp // Verify the frequency was set double actualFreq = device->getFrequency(SOAPY_SDR_RX, 0); std::cout << "Tuned to: " << actualFreq / 1e6 << " MHz" << std::endl; ``` ### Code Example (C++) ```cpp #include #include SoapySDR::Device* device = SoapySDR::Device::make("driver=netsdr"); // Get supported frequency ranges SoapySDR::RangeList ranges = device->getFrequencyRange(SOAPY_SDR_RX, 0); for (const auto& range : ranges) { std::cout << "Frequency range: " << range.minimum() / 1e6 << " - " << range.maximum() / 1e6 << " MHz" << std::endl; } // Set center frequency to 7.1 MHz (40m amateur band) double frequency = 7.1e6; device->setFrequency(SOAPY_SDR_RX, 0, frequency); // Verify the frequency was set double actualFreq = device->getFrequency(SOAPY_SDR_RX, 0); std::cout << "Tuned to: " << actualFreq / 1e6 << " MHz" << std::endl; SoapySDR::Device::unmake(device); ``` ``` -------------------------------- ### Apple Platform Specific C++ Flags Source: https://github.com/pothosware/soapynetsdr/blob/master/CMakeLists.txt Sets C++11 standard and extensions for Apple platforms. This ensures compatibility with C++11 features on macOS and iOS. ```cmake if(APPLE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wc++11-extensions") endif(APPLE) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.