SDRPLAY linux commandline tools?

Add useful snippets of code or links to entire SDR projects.
0815
Posts: 45
Joined: Sat Jan 30, 2016 3:07 pm

Re: SDRPLAY linux commandline tools?

Post by 0815 » Sun Mar 06, 2016 4:41 pm

hi,

well tried the whole afternoon and did not get lucky to get any IQ stream.... 16bit int would be ok as log as i get it out but i only get those numbers....

maybe you can help me again as i am not the greatest programmer

thats what usually looks a rtl_sdr IQ RAW stream looks like...

�������������������������~�������������������������������������������������~����~�������������������������������������������������~~�����~������������������~�������~��~�����������������������~��~���~��������������������~���������~�����������~�����������~������~���������������������~������������������������
Last edited by 0815 on Thu Jan 01, 1970 12:00 am, edited 0 times in total.
Reason: No reason

sdrplay
Posts: 978
Joined: Wed Jan 07, 2015 7:58 am

Re: SDRPLAY linux commandline tools?

Post by sdrplay » Sun Mar 06, 2016 6:26 pm

I thought you just wanted the IQ values. I'll take a look at rtl_sdr.c

Best regards,

SDRplay Support

0815
Posts: 45
Joined: Sat Jan 30, 2016 3:07 pm

Re: SDRPLAY linux commandline tools?

Post by 0815 » Sun Mar 06, 2016 7:00 pm

hi,

thanks, would be great if we can get a that raw data out of a simple commandline tool

well i checked the osmocom source also in the afternoon, maybe its more like this way....

#define BYTES_PER_SAMPLE 4 // sdrplay device delivers 16 bit signed IQ data
67 // containing 12 bits of information
68
69#define SDRPLAY_AM_MIN 150e3
70#define SDRPLAY_AM_MAX 30e6
71#define SDRPLAY_FM_MIN 64e6
72#define SDRPLAY_FM_MAX 108e6
73#define SDRPLAY_B3_MIN 162e6
74#define SDRPLAY_B3_MAX 240e6
75#define SDRPLAY_B45_MIN 470e6
76#define SDRPLAY_B45_MAX 960e6
77#define SDRPLAY_L_MIN 1450e6
78#define SDRPLAY_L_MAX 1675e6
79
80#define SDRPLAY_MAX_BUF_SIZE 504

132 _bufi.reserve(SDRPLAY_MAX_BUF_SIZE);
133 _bufq.reserve(SDRPLAY_MAX_BUF_SIZE);


246 while ((cnt - _dev->samplesPerPacket) >= 0)
247 {
248 mir_sdr_ReadPacket(_bufi.data(), _bufq.data(), &sampNum, &grChanged, &rfChanged, &fsChanged);
249 for (int i = 0; i < _dev->samplesPerPacket; i++)
250 {
251 *out++ = gr_complex( float(_bufi) * (1.0f/2048.0f), float(_bufq) * (1.0f/2048.0f) );
252 }
253 cnt -= _dev->samplesPerPacket;
254 }

in gnuradio the block gives me a complex float output, but int8 or int16 is also ok for me as output....
Last edited by 0815 on Thu Jan 01, 1970 12:00 am, edited 0 times in total.
Reason: No reason

sdrplay
Posts: 978
Joined: Wed Jan 07, 2015 7:58 am

Re: SDRPLAY linux commandline tools?

Post by sdrplay » Sun Mar 06, 2016 9:08 pm

ok, I've modified rtl_sdr.c to work with the RSP. 16bits are right shifted to 8bits for compatibility. 16bit version is easy to do if needed. I don't have a way of validating the output so if it's wrong (may need +127 as well as right shifting for example), let me know how you are validating it and I can check it out. Should build as before. Not had a chance to do much checking so is provided AS-IS with no warranty.

Let us know how you get on.

Code: Select all

/*
 * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
 * Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdint.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#ifndef _WIN32
#include <unistd.h>
#include "mirsdrapi-rsp.h"
#else
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include "getopt/getopt.h"
#include "mir_sdr.h"
#endif

#define DEFAULT_SAMPLE_RATE     2048000
#define DEFAULT_BUF_LENGTH      (336 * 2) // (16 * 16384)
#define MINIMAL_BUF_LENGTH      672 // 512
#define MAXIMAL_BUF_LENGTH      (256 * 16384)

static int do_exit = 0;
static uint32_t bytes_to_read = 0;

short *ibuf;
short *qbuf;
unsigned int firstSample;
int samplesPerPacket, grChanged, fsChanged, rfChanged;

double atofs(char *s)
/* standard suffixes */
{
    char last;
    int len;
    double suff = 1.0;
    len = strlen(s);
    last = s[len-1];
    s[len-1] = '\0';
    switch (last) {
        case 'g':
        case 'G':
            suff *= 1e3;
        case 'm':
        case 'M':
            suff *= 1e3;
        case 'k':
        case 'K':
            suff *= 1e3;
            suff *= atof(s);
            s[len-1] = last;
            return suff;
    }
    s[len-1] = last;
    return atof(s);
}

void usage(void)
{
    fprintf(stderr,
        "play_sdr, an I/Q recorder for SDRplay RSP receivers\n\n"
        "Usage:\t -f frequency_to_tune_to [Hz]\n"
        "\t[-s samplerate (default: 2048000 Hz)]\n"
        "\t[-g gain (default: 50)]\n"
//      "\t[-b output_block_size (default: 16 * 16384)]\n"
        "\t[-n number of samples to read (default: 0, infinite)]\n"
        "\tfilename (a '-' dumps samples to stdout)\n\n");
    exit(1);
}

#ifdef _WIN32
BOOL WINAPI
sighandler(int signum)
{
    if (CTRL_C_EVENT == signum) {
        fprintf(stderr, "Signal caught, exiting!\n");
        do_exit = 1;
        rtlsdr_cancel_async(dev);
        return TRUE;
    }
    return FALSE;
}
#else
static void sighandler(int signum)
{
    fprintf(stderr, "Signal caught, exiting!\n");
    do_exit = 1;
    mir_sdr_Uninit();
}
#endif

static void sdrplay_callback(unsigned char *buf, uint32_t len, void *ctx)
{
    if (ctx) {
        if (do_exit)
            return;

        if ((bytes_to_read > 0) && (bytes_to_read < len)) {
            len = bytes_to_read;
            do_exit = 1;
            mir_sdr_Uninit();
        }

        if (fwrite(buf, 1, len, (FILE*)ctx) != len) {
            fprintf(stderr, "Short write, samples lost, exiting!\n");
            mir_sdr_Uninit();
        }

        if (bytes_to_read > 0)
            bytes_to_read -= len;
    }
}

int main(int argc, char **argv)
{
#ifndef _WIN32
    struct sigaction sigact;
#endif
    char *filename = NULL;
    int n_read;
    mir_sdr_ErrT r;
    int opt;
    int gain = 50;
    int ppm_error = 0;
    int sync_mode = 0;
    FILE *file;
    uint8_t *buffer;
    int dev_index = 0;
    int dev_given = 0;
    uint32_t frequency = 100000000;
    uint32_t samp_rate = DEFAULT_SAMPLE_RATE;
    uint32_t out_block_size = DEFAULT_BUF_LENGTH;
    int i, j;

    while ((opt = getopt(argc, argv, "d:f:g:s:b:n:p:S")) != -1) {
        switch (opt) {
        case 'f':
            frequency = (uint32_t)atofs(optarg);
            break;
        case 'g':
            gain = (int)atof(optarg);
            break;
        case 's':
            samp_rate = (uint32_t)atofs(optarg);
            break;
//      case 'b':
//          out_block_size = (uint32_t)atof(optarg);
//          break;
        case 'n':
            bytes_to_read = (uint32_t)atofs(optarg) * 2;
            break;
        default:
            usage();
            break;
        }
    }

    if (argc <= optind) {
        usage();
    } else {
        filename = argv[optind];
    }

    if(out_block_size < MINIMAL_BUF_LENGTH ||
       out_block_size > MAXIMAL_BUF_LENGTH ){
        fprintf(stderr,
            "Output block size wrong value, falling back to default\n");
        fprintf(stderr,
            "Minimal length: %u\n", MINIMAL_BUF_LENGTH);
        fprintf(stderr,
            "Maximal length: %u\n", MAXIMAL_BUF_LENGTH);
        out_block_size = DEFAULT_BUF_LENGTH;
    }

    buffer = malloc(out_block_size * sizeof(uint8_t));

    r = mir_sdr_Init(40, 2.0, 100.00, mir_sdr_BW_1_536, mir_sdr_IF_Zero,
                        &samplesPerPacket);
    if (r != mir_sdr_Success) {
        fprintf(stderr, "Failed to open SDRplay RSP device.\n");
        exit(1);
    }
    mir_sdr_Uninit();
#ifndef _WIN32
    sigact.sa_handler = sighandler;
    sigemptyset(&sigact.sa_mask);
    sigact.sa_flags = 0;
    sigaction(SIGINT, &sigact, NULL);
    sigaction(SIGTERM, &sigact, NULL);
    sigaction(SIGQUIT, &sigact, NULL);
    sigaction(SIGPIPE, &sigact, NULL);
#else
    SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
#endif

    if(strcmp(filename, "-") == 0) { /* Write samples to stdout */
        file = stdout;
#ifdef _WIN32
        _setmode(_fileno(stdin), _O_BINARY);
#endif
    } else {
        file = fopen(filename, "wb");
        if (!file) {
            fprintf(stderr, "Failed to open %s\n", filename);
            goto out;
        }
    }

    r = mir_sdr_Init((78-gain), (samp_rate/1e6), (frequency/1e6),
                       mir_sdr_BW_1_536, mir_sdr_IF_Zero, &samplesPerPacket );

    mir_sdr_SetDcMode(4,0);
    mir_sdr_SetDcTrackTime(63);

    ibuf = malloc(samplesPerPacket * sizeof(short));
    qbuf = malloc(samplesPerPacket * sizeof(short));

    fprintf(stderr, "Writing samples...\n");
    while (!do_exit) {
        r = mir_sdr_ReadPacket(ibuf, qbuf, &firstSample, &grChanged, &rfChanged,
                            &fsChanged);
        if (r != mir_sdr_Success) {
            fprintf(stderr, "WARNING: ReadPacket failed.\n");
            break;
        }

        j = 0;
        for (i=0; i < samplesPerPacket; i++)
        {
            buffer[j++] = (unsigned char) (ibuf[i] >> 8);
            buffer[j++] = (unsigned char) (qbuf[i] >> 8);
        }

        n_read = (samplesPerPacket * 2);

        if ((bytes_to_read > 0) && (bytes_to_read <= (uint32_t)n_read)) {
            n_read = bytes_to_read;
            do_exit = 1;
        }

        if (fwrite(buffer, 1, n_read, file) != (size_t)n_read) {
            fprintf(stderr, "Short write, samples lost, exiting!\n"); break;
        }

        if ((uint32_t)n_read < out_block_size) {
            fprintf(stderr, "Short read, samples lost, exiting!\n");
            break;
        }

        if (bytes_to_read > 0)
            bytes_to_read -= n_read;
    }

    if (do_exit)
        fprintf(stderr, "\nUser cancel, exiting...\n");
    else
        fprintf(stderr, "\nLibrary error %d, exiting...\n", r);

    if (file != stdout)
        fclose(file);

    mir_sdr_Uninit();
    free (buffer);
out:
    return r >= 0 ? r : -r;
}
Last edited by sdrplay on Thu Jan 01, 1970 12:00 am, edited 0 times in total.
Reason: No reason

0815
Posts: 45
Joined: Sat Jan 30, 2016 3:07 pm

Re: SDRPLAY linux commandline tools?

Post by 0815 » Mon Mar 07, 2016 2:42 pm

HI,

thanks alost, you are close to it.

Build works, just a few warnings about unused variables...

but look here:

new-HP-6730b:~/rsp$ ./rawIQ -f 100800000 -s 2000000 -g 50 -
mir_sdr_Init: starting hardware initialization
mir_sdr_Init: gR=40dB fs=2.000MHz rf=100.000MHz bw=1.536MHz if=0.000MHz

mir_sdr_usb_USB DLL: Revision 0.1.1

mir_sdr_2500_Init: fnaddr = 7

mir_sdr_2500_Init: adjusting squelch trim 0x1, rx gating enable 1, tx_trim 0, reg2 = 0x4801
initHw: Register7 = 0x000014
initHw: Tuner Register0 = 0x04f420
mir_sdr_SetFs: Sample Freq requested 2000000.000000
mir_sdr_SetFs: Fs->FsNomHz+dFsHz=2000000.0+0.0Hz=2000000.0Hz FsToggle->1
mir_sdr_SetRf: f->100000000.000Hz (int=21 frac=3e8 afc=0) fSynth:3200000000.000
mir_sdr_SetRf: Rf->RfNomHz+dRfHz+LifHz+Lif1Hz=100000000.0+0.0+0.0Hz+0.0Hz=100000000.0Hz RfToggle->1
mir_sdr_SetGr: GR->40[16,24,0,0] gRset->0x210 DCCALmode=4 DCCALspd=1 GrToggle->1
setToggleStates: initialising sampNum=0x00017a02, gainSetting=0x210, FsToggle=0, RfToggle=1, GrToggle=1
setToggleStates: initialising Fs=2000000.000, Rf=100000000.000, Gr=40
mir_sdr_2500_Readback001Trim: readback = 0xe402c11
mir_sdr_2500_Bypass001Trim: reg13=0x22d reg14=0x7002ce
mir_sdr_Init: starting hardware initialization
mir_sdr_Init: gR=28dB fs=2.000MHz rf=100.800MHz bw=1.536MHz if=0.000MHz

mir_sdr_usb_USB DLL: Revision 0.1.1

mir_sdr_2500_Init: fnaddr = 7

mir_sdr_2500_Init: adjusting squelch trim 0x1, rx gating enable 1, tx_trim 0, reg2 = 0x4801
initHw: Register7 = 0x000014
initHw: Tuner Register0 = 0x04f420
mir_sdr_SetFs: Sample Freq requested 2000000.000000
mir_sdr_SetFs: Fs->FsNomHz+dFsHz=2000000.0+0.0Hz=2000000.0Hz FsToggle->1
mir_sdr_SetRf: f->100800000.000Hz (int=21 frac=708 afc=0) fSynth:3225600000.000
mir_sdr_SetRf: Rf->RfNomHz+dRfHz+LifHz+Lif1Hz=100800000.0+0.0+0.0Hz+0.0Hz=100800000.0Hz RfToggle->1
mir_sdr_SetGr: GR->28[28,0,0,0] gRset->0x1C DCCALmode=4 DCCALspd=1 GrToggle->1
setToggleStates: initialising sampNum=0x00017a02, gainSetting=0x01c, FsToggle=0, RfToggle=0, GrToggle=0
setToggleStates: initialising Fs=2000000.000, Rf=100800000.000, Gr=28
mir_sdr_SetDcMode: DCCAL: mode->4 speedup->0
mir_sdr_SetDcTrackTime: DCTRK_TIM->63
Writing samples...
Short read, samples lost, exiting!

Library error 0, exiting...
����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������mir_sdr_2500_Readback001Trim: readback = 0xe402a11
mir_sdr_2500_Bypass001Trim: reg13=0x22d reg14=0x7002ae
Last edited by 0815 on Thu Jan 01, 1970 12:00 am, edited 0 times in total.
Reason: No reason

0815
Posts: 45
Joined: Sat Jan 30, 2016 3:07 pm

Re: SDRPLAY linux commandline tools?

Post by 0815 » Mon Mar 07, 2016 3:44 pm

ok out_block_size failure which isnt used anymore i guess - removed it make it now test it

will let ya knowif output is good

pls let me know what to change for 16bit - thx

regards
Last edited by 0815 on Thu Jan 01, 1970 12:00 am, edited 0 times in total.
Reason: No reason

0815
Posts: 45
Joined: Sat Jan 30, 2016 3:07 pm

Re: SDRPLAY linux commandline tools?

Post by 0815 » Mon Mar 07, 2016 7:21 pm

Hi,

still testing and alreday getting some data, but i have to check on fm if all is correct.

for testing you can do convert as follow to play it gqrx:

record:
rawIQ -f 100800000 -s 250000 sdrplay_u8.raw

sox -t raw -r 250000 -b 8 -c 1 -e unsigned-integer sdrplay_u8.raw -t raw -r 250000 -c 1 -e sdrplay_fc.raw
Last edited by 0815 on Thu Jan 01, 1970 12:00 am, edited 0 times in total.
Reason: No reason

0815
Posts: 45
Joined: Sat Jan 30, 2016 3:07 pm

Re: SDRPLAY linux commandline tools?

Post by 0815 » Tue Mar 08, 2016 3:47 pm

Hi,

well works perfect -> many thanks to sdrplay support!!!! NOw i can really enjoy my RSP

enclosed a waterfall of a 2000000 recorded u8_IQ converted with sox to .wav and opened in SDR#. works!!

@sdrplay please let me know regarding the u_16 option and the float complex also.

according to this i will try to change the other rtl_sdr(fm,adsb) tools also as this might be a benefit for all users of sdrplay regarding linux.
IQconvert.jpg
IQconvert.jpg (134.6 KiB) Viewed 25838 times
thanks in advance
Last edited by 0815 on Thu Jan 01, 1970 12:00 am, edited 0 times in total.
Reason: No reason

sdrplay
Posts: 978
Joined: Wed Jan 07, 2015 7:58 am

Re: SDRPLAY linux commandline tools?

Post by sdrplay » Wed Mar 09, 2016 12:28 am

For 16bit samples you will need to do 2 things...

Firstly, the following lines will need to change from...

Code: Select all

for (i=0; i < samplesPerPacket; i++)
        {
            buffer[j++] = (unsigned char) (ibuf[i] >> 8);
            buffer[j++] = (unsigned char) (qbuf[i] >> 8);
        }
to

Code: Select all

for (i=0; i < samplesPerPacket; i++)
        {
            buffer[j++] = ibuf[i];
            buffer[j++] = qbuf[i];
        }
and buffer will need to change to a short *

We'll add these code samples to our github repository and post a link so people can use or contribute.

Best regards,

SDRplay Support
Last edited by sdrplay on Thu Jan 01, 1970 12:00 am, edited 0 times in total.
Reason: No reason

sdrplay
Posts: 978
Joined: Wed Jan 07, 2015 7:58 am

Re: SDRPLAY linux commandline tools?

Post by sdrplay » Wed Mar 09, 2016 10:21 am

github repository is up. If anyone wants to add code to it, please send it to software@sdrplay.com with a description and we'll get it pushed up.

https://github.com/SDRplay/examples

Best regards,

SDRplay Support
Last edited by sdrplay on Thu Jan 01, 1970 12:00 am, edited 0 times in total.
Reason: No reason

Post Reply