aboutsummaryrefslogtreecommitdiffstats
path: root/airspyhf_rx.py
blob: 7d55c35de3311361a5946e76a39f9dd489375411 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/python3
import os
from airspyhf import *
from ctypes import *
import time
import sys
import wave
import struct
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-f","--frequency")
parser.add_argument("-s","--samplerate")
parser.add_argument("-o","--outputfile")
parser.add_argument("-sn","--serial")
args = parser.parse_args()

print("Check airspyHF version")

p = airspyhf_lib_version_t()
libairspyhf.airspyhf_lib_version(byref(p))
print(f"libairspyhf version {p.major_version}.{p.minor_version}.{p.revision}")

print("Get list of devices if there is any")
ndev = libairspyhf.airspyhf_list_devices(None,0)
print("Found %d devices"%(ndev))
if ndev < 1:
    sys.exit(0)

for devi in range(0,ndev):
    serial = c_uint64(0)
    libairspyhf.airspyhf_list_devices(byref(serial),devi+1)
    print("Device %d: Serial number %s"%(int(devi),hex(serial.value) ))

print("Try to open device")
dev_p = airspyhf_device_t_p(None)
if args.serial != None:
    ret = libairspyhf.airspyhf_open_sn(dev_p, int(args.serial,16))
else:
    serial = c_uint64(0)
    libairspyhf.airspyhf_list_devices(byref(serial), 1)
    print(hex(serial.value))
    print(type(serial.value))
    ret = libairspyhf.airspyhf_open_sn(dev_p, serial.value)
print("open_sn: Returned %d"%(ret))
if (ret != 0):
    print("airspyhf_open_sn returned != 0, error")
    sys.exit()

print("List sample rates")
nsrates = c_uint32(0)

ret = libairspyhf.airspyhf_get_samplerates(dev_p,byref(nsrates),c_uint32(0))
print("ret %d"%ret)
print("sample rates %d"% nsrates.value)

supported_samplerates = (c_uint32*nsrates.value)(0)
ret = libairspyhf.airspyhf_get_samplerates(dev_p,supported_samplerates,nsrates)
print("ret %d"%ret)
print("Sample rate list:")
for i in range(0,nsrates.value):
    print("    %d s/sec"% supported_samplerates[i])

#try to get some samples
supported_samplerates = list(supported_samplerates)
if int(args.samplerate) in supported_samplerates:
    print("Setting sample rate to %s"%(args.samplerate))
    ret = libairspyhf.airspyhf_set_samplerate(dev_p, int(args.samplerate))
else:
    print("Setting sample rate to %s"% (supported_samplerates[len(supported_samplerates)-1]))
    ret = libairspyhf.airspyhf_set_samplerate(dev_p, supported_samplerates[len(supported_samplerates)-1])
print(f"airspyhf_set_samplerate ret={ret}")

ret = libairspyhf.airspyhf_set_hf_agc(dev_p, 1)
print(f"airspyhf_set_hf_agc ret={ret}")

ret = libairspyhf.airspyhf_set_hf_agc_threshold(dev_p, 0)
print(f"airspyhf_set_hf_agc_threshold ret={ret}")

ret = libairspyhf.airspyhf_set_hf_lna(dev_p, 1)
print(f"airspyhf_set_hf_lna ret={ret}")

sample_count = 0
RECORD_FILE_NAME="record.wav"
if args.outputfile != None:
    RECORD_FILE_NAME = args.outputfile
wave_file = wave.open(RECORD_FILE_NAME,"w")
wave_file.setnchannels(2)
wave_file.setsampwidth(4)
wave_file.setframerate(supported_samplerates[1])
def read_samples(transfer):
    global sample_count
    global wave_file
    #print("Python call back")
    t = transfer.contents
    bytes_to_write = t.sample_count * 4 * 2
    rx_buffer = t.samples
    #print(f"{bytes_to_write} bytes receieved")
    sample_count += t.sample_count
    for i in range(0,t.sample_count):
        d_re = t.samples[i].re
        d_im = t.samples[i].im
        data = struct.pack("<f",d_re) # FIX ?!
        wave_file.writeframesraw(data)
        data = struct.pack("<f", d_im)  # FIX ?!
        wave_file.writeframesraw(data)
    #print("End call back")
    return 0


#read_samples_cb = airspyhf_sample_block_cb_fn(read_samples)


ret = libairspyhf.airspyhf_start(dev_p, airspyhf_sample_block_cb_fn(read_samples), None)
print(f"airspyhf_start ret={ret}")

if args.frequency == None:
    ret = libairspyhf.airspyhf_set_freq(dev_p, 3865000)
else:
    ret = libairspyhf.airspyhf_set_freq(dev_p, int(args.frequency))
print(f"airspyhf_set_freq ret={ret}")

count = 0
try:
    while (libairspyhf.airspyhf_is_streaming(dev_p)) and (count < 3):
        print("Main loop")
        time.sleep(1)
        count += 1
except:
    print("Error in main loop")

ret = libairspyhf.airspyhf_stop(dev_p)
print(f"airspyhf_stop ret={ret}")
time.sleep(1)

#Not close for now
ret = libairspyhf.close(dev_p)
print("closed: Returned %d"%(ret))

print(f"Total samples received {sample_count}")

wave_file.close()

print("All is ok")