We seek to understand and document all radio transmissions, legal and otherwise, as part of the radio listening hobby. We do not encourage any radio operations contrary to regulations. Always consult with the appropriate authorities if you have questions concerning what is permissable in your locale.

Author Topic: Using a Raspberry Pi as an RF Space SDR-14 / IQ USB to network adapter  (Read 318 times)

Offline ChrisSmolinski

  • Administrator
  • Marconi Class DXer
  • *****
  • Posts: 28400
  • Westminster, MD USA
    • View Profile
    • Black Cat Systems
I found this server app to convert my USB RF Space SDR-14 to a networked SDR (same protocol as the netSDR, so it works with SdrDx and similar apps):


Download the files sdrcmds.py and server.py - you will need to edit the latter or use the version below.

It's running on a Raspberry Pi, which shares it over my local network to any other computer running the SDR client software (a Mac running SdrDx or my Amalgamated DGPS app) . In theory as the code is in Python you could run it on another system such as a desktop.

Of course it didn't quite work "out of the box", so here's what I had to do to get it to work:

First I had to install pylibftdi:

pip install pylibftdi

The SDR-14 uses a custom USB PID, so I had to add it (0xf728) to the list of recognized PIDs, I did this at the start of the script itself as creating /etc/modprobe.d/ftdi_sio.conf with this entry did not work:
Code: [Select]
options  ftdi_sio vendor=0x0403 product=0xf728
To get this to work with an SDR-IQ you'd need to use its USB PID, which is probably different.

Then I had to make a bunch of changes to the python code. I don't claim that the result is elegant, but it works.  Note that I hard coded SDR-14 as the name of the USB device.

Start it with:

sudo -E ./server.py -b

If the SDR client disconnects, the python server script terminates, so the next thing to do was wrap it in a loop, i created a short script named sdr14:

Code: [Select]
#! /bin/sh

while true; do
 sudo  -E python server.py -b
done &

And server.py itself:

Code: [Select]


# TODO:  need to support this message from gqrx:
# listening on port 50000
# Connected: on port 61587
# Get Name
# Get Serial Number
# Get Unknown: [0x4] [0x20] [0x9] [0x0]

from pylibftdi.device import Device
from pylibftdi.driver import Driver
from threading import Thread, Event
from time import sleep
from socket import *
from sdrcmds import SdrIQByteCommands as bc
import numpy as np
from struct import unpack
import sys, getopt
from pylibftdi import USB_VID_LIST, USB_PID_LIST, Device

print(map(hex, USB_VID_LIST))
print(map(hex, USB_PID_LIST))

iqDataSendHeaderSize = 4
iqDataSendBlockSize = 1024   # must be a factor of 8192; SdrDx only likes 1024
iqDataSendMsgLength = iqDataSendBlockSize + iqDataSendHeaderSize

def IQ(msg):
    for k in range(2,len(msg),2):
        yield unpack('h',msg[k:k+2])[0]

def power(msg):
    av = 0.0
    for iq in IQ(msg):
        av = av + iq*iq
    return av/(len(msg)-2)

def prnmsg(msg):
    return ' '.join(['['+hex(b).upper()+']' for b in msg]).replace('X','x')

class Validator:
    def __init__(self,output):

        self.MsgGroup = {
            b'\x20' : self.onGet,
            b'\x00' : self.onSet,
            b'\x80' : self.onData,
            b'\xa0' : self.onAD6620

        self.DataItem = {
            b'\x01' : 'Name',
            b'\x02' : 'Serial Number',
            b'\x03' : 'Interface Version',
            b'\x04' : 'PIC Version',
            b'\x05' : 'Status',
            b'\x18' : 'Run/Stop',
            b'\x20' : 'Frequency',
            b'\xB0' : 'Sample Rate',
            b'\x40' : 'IF Gain',
            b'\x38' : 'RF Gain'

    def log(self,msg):
        if msg[0:3] == b'\x04\x20\x05':

    def unknown(self,msg):
        print('Unknown Message: {prnmsg(msg)}')

    def onGet(self,msg):
        print('Get {self.DataItem.get(msg[2:3],f"Unknown: {prnmsg(msg)}")}')

    def onSet(self,msg):
        print('Set {self.DataItem.get(msg[2:3],f"Unknown: {prnmsg(msg)}")}')

    def onData(self,msg):

    def onAD6620(self,msg):
        if msg[0] != 9:
            print('bogus length: {msg[0]}')
        if msg[2:3] == b'\x01':
            print('Start AD6620 Programming')
        if msg[2:3] == b'\xff':
            print('Complete AD6620 Programming')

    def valid(self,msg):
        return Validator.get(msg[0:2],False)

def readMsg(source):

    # First, get the message length from the first two bytes of the message.
    msg = source(2)
    if not msg:
        return msg
    # TODO:  shouldn't the comparison really be msg[0] == 0 && (msg[1] & 0x1f) == 0 because
    #        zero length in the header means 8192?  The 3 upper bits of 0x80 is the message
    #        type (IQ data).
    if msg == b'\x00\x80':
        # This is an IQ data message with 2 header bytes and the maximum number of data bytes (8192).
        length = 8194
        # Byte 0 holds the 8 least-significant bits of the message length.
        # The lower 5 bits of byte 1 are the 5 most-significant bits.
        # TODO:  why aren't we adding 2 for the header bytes?
        length = ord(msg[0])
        length = length + (ord(msg[1]) & 0x1F) * 256
    # Read the rest of the message up to the computed length.
    while len(msg) != length:
        msg = msg + source(length-len(msg))

    return msg

class Listener:
    def __init__(self):
        self.makeItStop  = Event()
        self.warmBoot = False
        self.radioName = b'SDR-14'
        #self.print = self.noOp
        self.boot = self.coldBoot

    def doCommandline(self):
            opts, args = getopt.getopt(sys.argv[1:],'br:v')
        except getopt.GetoptError:
            print('usage: server [-b,-r <radio>, -v]')
        for op in opts:
            if op[0] == '-b':
                self.boot = self.noOp
            elif op[0] == '-r':
                self.radioName = bytes(op[1],encoding='utf-8')
            #elif op[0] == '-v':
                #self.print = print
                print('unknown option: {op}')

    def stop(self):

    def noOp(self,*kargs):

    def coldBoot(self):
        freq = self.GetFreq()
        if (freq == 680000):
            print('Cold boot detected')

    def findRadio(self):
        self.devices = Driver().list_devices()
        self.radio = None
        for d in self.devices:
            # ross - print('d[1]="{0}" self.radioName="{1}"'.format(d[1], self.radioName))
            if (d[1] == self.radioName.decode('utf-8')):
                print('found radio')
                    self.radio = Device(encoding='utf-8')
                except FtdiError as err:
                    print('Device instantiation failed:  {0}'.format(err))
        if self.radio:
            print('connected to radio')
            print('unable to connect to radio')

    def GetFreq(self):
        rep = readMsg(self.radio.read)
        return sum([ rep[k+5] << (8*k) for k in range(4)])

    def SetFreq(self,freq):
        msg = list(bc.SetFreq)
        for k in range(4):
            msg[k+5] = (freq >> (8*k)) & 0xFF
        return readMsg(self.radio.read)

    def SetDSP(self):
        for cmd in bc.BWKHZ_190:

    def SetIFGain(self):
        cmd = list(bc.SetIFGain)
        cmd[5] = 24

    def SetRFGain(self):
        cmd = list(bc.SetRFGain)
        cmd[5] = 0

    def serve(self):
        if not self.radio:
            print('Radio {self.radioName.decode()} not found')
        self.tcp = socket(AF_INET,SOCK_STREAM)
        # ross 2020-06-10:  We want to accept connections from anywhere.
        # '' is equivalent to AF_ANY.
        self.udp = socket(AF_INET,SOCK_DGRAM)
        # ross 2020-06-10:  We want to send data to anywhere
        # '' is equivalent to AF_ANY.
        print('listening on port 50000')
            self.connect = self.tcp.accept()
            # ross TODO:  print the exception
        print('Connected: {self.connect[1][0]} on port {self.connect[1][1]}')
        # ross - Connected: on port 60887
        self.writer = RadioWriter(self)
        self.reader = RadioReader(self)
        print('closing TCP and UDP sockets')
        print('Server - done')

class RadioWriter(Thread):
    def __init__(self,listener):
        self.makeItStop = listener.makeItStop
        self.radio      = listener.radio
        self.tcp        = listener.connect[0]
        #self.print      = listener.print
        #self.logger     = Validator(listener.print)
        self.daemon     = True

    def run(self):
        # Receive messages from the SDR client and pass them on to the radio.
        while not self.makeItStop.isSet():
            msg = readMsg(self.tcp.recv)
            if not msg:
            #self.logger.log(msg)    # ross - does Validator do anything other than just log shit?
        print('RadioWriter - done')

class RadioReader(Thread):
    def __init__(self,listener):
        self.makeItStop = listener.makeItStop
        self.tcp        = listener.connect[0]
        #self.rAddress   = ('localhost',50000)   # ross - address for sending ADC data via UDP - ? in SdrDx
        self.rAddress   = (listener.connect[1][0], 50000)   # send ADC data to the host that connected to us
        self.udp        = listener.udp          # ross - ends up being L's udp object set up in Listener::serve
        self.radio      = listener.radio
        #self.print      = listener.print
        self.sequence   = 0
        self.daemon     = True

    def sequenceNumber(self):
        self.sequence = self.sequence + 1
        if (self.sequence < 0xFFFF):
            return self.sequence
        self.sequence = 1
        return self.sequence

    def sendData(self,msg):
        #self.print('sending data via UDP ({0})'.format(len(msg)))
        ba = bytearray(iqDataSendMsgLength)
        for k in range(int(len(msg) / iqDataSendBlockSize)):
            #self.print(f'  block {k}')
            sn = self.sequenceNumber()
            # TODO:  write a function that formats the 16-bit header
            #ba[0] = 0x04    # length lsb (8 bits)
            #ba[1] = 0x84    # message type 0b100 and length msb (5 bits)
            ba[0] = int(iqDataSendMsgLength) & 0xff                 # length lsb (8 bits)
            ba[1] = 0x80 | ((int(iqDataSendMsgLength) >> 8) & 0x1f)   # message type 0b100 and length msb (5 bits)
            ba[2] = sn & 0xFF
            ba[3] = (sn >> 8) & 0xFF
            ba[4:] = msg[k * iqDataSendBlockSize : (k + 1) * iqDataSendBlockSize]
            #print('k={:} 0x{:02X} 0x{:02X} 0x{:02X} 0x{:02X}'.format(k, ba[0], ba[1], ba[2], ba[3]))
            self.udp.sendto(ba,self.rAddress)  # ross - this is where we send ADC data via UDP

    def run(self):
        # Receive messages from the radio.  Send ADC data via
        # UDP and other messages via TCP to the SDR client.
        while not self.makeItStop.isSet():
            msg = readMsg(self.radio.read)
            if not msg:
            #ross print('got msg {0}'.format(msg))
            if msg[0:2] == b'\x00\x80':
                #print(f'power = {power(msg)}')
                #self.print('sending UDP ({0})'.format(len(msg)))
                self.sendData(msg[2:])  # ross - send ADC data
                #self.print('sending TCP ({0})'.format(len(msg)))
        #print('RadioReader - done')

if __name__ == '__main__':
   L = Listener()
   except KeyboardInterrupt:

« Last Edit: July 25, 2021, 1942 UTC by ChrisSmolinski »
Chris Smolinski
Westminster, MD
eQSLs appreciated! csmolinski@blackcatsystems.com
netSDR / AFE822x / AirSpy HF+ / KiwiSDR / 900 ft Horz skyloop / 500 ft NE beverage / 250 ft V Beam / 58 ft T2FD / 120 ft T2FD / 400 ft south beverage / 43m, 20m, 10m  dipoles / Crossed Parallel Loop / Discone in a tree