Skip to content
guruofquality edited this page Nov 2, 2014 · 13 revisions

Python bindings for SoapySDR

The Python bindings for SoapySDR give the user access to the complete C++ API in Python. Because most of the API calls are identical to the C++ API, users should refer to the C++ headers and Doxygen for documentation. However, some calls have changed because of language differences. We will go over these calls in the examples below.

Dependencies

The Python bindings require SWIG and Python development files. We chose to use SWIG because it greatly simplified the developer effort required. To install the Python bindings, simply provide these dependencies before building SoapySDR.

To get the dependencies on a debian/ubuntu system:

sudo apt-get install python-dev swig

Example

import SoapySDR
from SoapySDR import * #SOAPY_SDR_* constants
import numpy #use numpy for buffers

#enumerate devices
results = SoapySDR.Device.enumerate()
for result in results: print(result)

#create device instance
#Notice that we dont need to use Device.make.
#The constructor is the factory in Python.
args = dict(driver="rtl")
sdr = SoapySDR.Device(args)

#query device info
print(sdr.listAntennas(SOAPY_SDR_RX, 0))
print(sdr.listGains(SOAPY_SDR_RX, 0))
freqs = sdr.getFrequencyRange(SOAPY_SDR_RX, 0)
for freq in freqs: print(freq)

#setup a stream (complex floats)
rxStream = sdr.setupStream(SOAPY_SDR_RX, "CF32")
sdr.activateStream(rxStream) #start streaming

#receive some samples
buff = numpy.array([0]*1024, numpy.complex64) #create a receive buffer
for i in range(10):
    sr = sdr.readStream(rxStream, [buff], len(buff))
    print sr.ret #num samples or error code
    print sr.flags #flags set by receive operation
    print sr.timeNs #timestamp for receive buffer

#shutdown a stream
sdr.deactivateStream(rxStream) #stop streaming
sdr.closeStream(rxStream)

Clone this wiki locally