-
Notifications
You must be signed in to change notification settings - Fork 183
C_API_Example
Josh Blum edited this page Apr 23, 2021
·
8 revisions
Compile with: gcc -std=c99 example.c -lSoapySDR && ./a.out
#include <SoapySDR/Device.h>
#include <SoapySDR/Formats.h>
#include <stdio.h> //printf
#include <stdlib.h> //free
#include <complex.h>
int main(void)
{
size_t length;
//enumerate devices
SoapySDRKwargs *results = SoapySDRDevice_enumerate(NULL, &length);
for (size_t i = 0; i < length; i++)
{
printf("Found device #%d: ", (int)i);
for (size_t j = 0; j < results[i].size; j++)
{
printf("%s=%s, ", results[i].keys[j], results[i].vals[j]);
}
printf("\n");
}
SoapySDRKwargsList_clear(results, length);
//create device instance
//args can be user defined or from the enumeration result
SoapySDRKwargs args = {};
SoapySDRKwargs_set(&args, "driver", "rtlsdr");
SoapySDRDevice *sdr = SoapySDRDevice_make(&args);
SoapySDRKwargs_clear(&args);
if (sdr == NULL)
{
printf("SoapySDRDevice_make fail: %s\n", SoapySDRDevice_lastError());
return EXIT_FAILURE;
}
//query device info
char** names = SoapySDRDevice_listAntennas(sdr, SOAPY_SDR_RX, 0, &length);
printf("Rx antennas: ");
for (size_t i = 0; i < length; i++) printf("%s, ", names[i]);
printf("\n");
SoapySDRStrings_clear(&names, length);
names = SoapySDRDevice_listGains(sdr, SOAPY_SDR_RX, 0, &length);
printf("Rx gains: ");
for (size_t i = 0; i < length; i++) printf("%s, ", names[i]);
printf("\n");
SoapySDRStrings_clear(&names, length);
SoapySDRRange *ranges = SoapySDRDevice_getFrequencyRange(sdr, SOAPY_SDR_RX, 0, &length);
printf("Rx freq ranges: ");
for (size_t i = 0; i < length; i++) printf("[%g Hz -> %g Hz], ", ranges[i].minimum, ranges[i].maximum);
printf("\n");
free(ranges);
//apply settings
if (SoapySDRDevice_setSampleRate(sdr, SOAPY_SDR_RX, 0, 1e6) != 0)
{
printf("setSampleRate fail: %s\n", SoapySDRDevice_lastError());
}
if (SoapySDRDevice_setFrequency(sdr, SOAPY_SDR_RX, 0, 912.3e6, NULL) != 0)
{
printf("setFrequency fail: %s\n", SoapySDRDevice_lastError());
}
//setup a stream (complex floats)
SoapySDRStream *rxStream = SoapySDRDevice_setupStream(sdr, SOAPY_SDR_RX, SOAPY_SDR_CF32, NULL, 0, NULL);
if (rxStream == NULL)
{
printf("setupStream fail: %s\n", SoapySDRDevice_lastError());
SoapySDRDevice_unmake(sdr);
return EXIT_FAILURE;
}
SoapySDRDevice_activateStream(sdr, rxStream, 0, 0, 0); //start streaming
//create a re-usable buffer for rx samples
complex float buff[1024];
//receive some samples
for (size_t i = 0; i < 10; i++)
{
void *buffs[] = {buff}; //array of buffers
int flags; //flags set by receive operation
long long timeNs; //timestamp for receive buffer
int ret = SoapySDRDevice_readStream(sdr, rxStream, buffs, 1024, &flags, &timeNs, 100000);
printf("ret=%d, flags=%d, timeNs=%lld\n", ret, flags, timeNs);
}
//shutdown the stream
SoapySDRDevice_deactivateStream(sdr, rxStream, 0, 0); //stop streaming
SoapySDRDevice_closeStream(sdr, rxStream);
//cleanup device handle
SoapySDRDevice_unmake(sdr);
printf("Done\n");
return EXIT_SUCCESS;
}
- FAQ
- Build guide
- Driver guide
- SoapySDR header files
- Doxygen documentation
- Python binding support
- LuaJIT binding support
- .NET binding support
- GO binding support
- Rust binding support
- Julia binding support
- Pothos SDR Tutorial
- Help and support
- Pothos users' group
- Twitter @pothosware
- IRC chat #pothos
- Slack workspace
- Contract services
- Developer blog
- Contributing
- Donate
- Example support
- Remote access
- Multi device
- Device sharing
- SIMD converters
- Audio devices
- Osmo support
- NovenaRF support
- EVB7 support
- UHD support
- Blade RF support
- Hack RF support
- RTL-SDR support
- SDR Play support
- Radioberry support
- Red Pitaya support
- Lime Suite support
- Airspy support
- Airspy HF+ support
- PlutoSDR support
- Skylark Iris module
- Funcube Dongle Pro+
- IC-R8600 Receiver
- Epiq Sidekiq
- NetSDR support
- XTRX support
- RTL TCP support
- SpyServer support
- Afedri support
- Pothos SDR
- Cubic SDR
- Rx Tools
- LuaRadio
- GNURadio blocks
- Osmocom blocks
- QSpectrumAnalyzer
- RTL433 project
- Welle.io DAB/DAB+
- SDRangel analyzer
- RTLSDR-Airband
- HABDEC RTTY decoder
- LinHPSDR receiver
- HackTV transmitter
- ODR-DabMod
- QUISK SDR
- SigDigger
- Suscan
- SdrGlut
- QRadioLink
- OpenWebRX
- SDR++
- Seify
- Abraca DAB radio