File: self_test.py

package info (click to toggle)
soapyhackrf 0.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 180 kB
  • sloc: cpp: 1,438; xml: 53; python: 32; makefile: 10
file content (81 lines) | stat: -rw-r--r-- 2,962 bytes parent folder | download | duplicates (4)
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
import SoapySDR
from SoapySDR import * #SOAPY_SDR_* constants
import numpy as np
import time

if __name__ == "__main__":
    hackrf = SoapySDR.Device(dict(driver="hackrf"))
    print hackrf

    hackrf.setSampleRate(SOAPY_SDR_RX, 0, 8e6)
    hackrf.setSampleRate(SOAPY_SDR_TX, 0, 8e6)

    """
    for i in range(5):
        print("  Make rx stream #%d"%i)
        rxStream = hackrf.setupStream(SOAPY_SDR_RX, SOAPY_SDR_CF32, [0])
        for j in range(5):
            numSampsTotal = 10000
            print("    Activate, get %d samples, Deactivate #%d"%(numSampsTotal, j))
            hackrf.activateStream(rxStream)
            buff = np.array([0]*1024, np.complex64)
            while numSampsTotal > 0:
                sr = hackrf.readStream(rxStream, [buff], buff.size, timeoutUs=int(1e6))
                #print sr
                assert(sr.ret > 0)
                numSampsTotal -= sr.ret
            hackrf.deactivateStream(rxStream)
        hackrf.closeStream(rxStream)

    for i in range(5):
        print("  Make tx stream #%d"%i)
        txStream = hackrf.setupStream(SOAPY_SDR_TX, SOAPY_SDR_CF32, [0])
        for j in range(5):
            numSampsTotal = 10000
            print("    Activate, send %d samples, Deactivate #%d"%(numSampsTotal, j))
            hackrf.activateStream(txStream)
            buff = np.array([0]*1024, np.complex64)
            while numSampsTotal != 0:
                size = min(buff.size, numSampsTotal)
                sr = hackrf.writeStream(txStream, [buff], size)
                #print sr
                if not (sr.ret > 0): print("Fail %s, %d"%(sr, numSampsTotal))
                assert(sr.ret > 0)
                numSampsTotal -= sr.ret
            hackrf.deactivateStream(txStream)
        hackrf.closeStream(txStream)
    """

    ####################################################################
    #setup both streams at once
    ####################################################################
    rxStream = hackrf.setupStream(SOAPY_SDR_RX, SOAPY_SDR_CF32, [0])
    txStream = hackrf.setupStream(SOAPY_SDR_TX, SOAPY_SDR_CF32, [0])

    hackrf.activateStream(rxStream)
    hackrf.activateStream(txStream)

    numSampsTotal = 10000
    hackrf.activateStream(rxStream)
    buff = np.array([0]*1024, np.complex64)
    while numSampsTotal > 0:
        sr = hackrf.readStream(rxStream, [buff], buff.size, timeoutUs=int(1e6))
        #print sr
        assert(sr.ret > 0)
        numSampsTotal -= sr.ret

    numSampsTotal = 10000
    buff = np.array([0]*1024, np.complex64)
    while numSampsTotal != 0:
        size = min(buff.size, numSampsTotal)
        sr = hackrf.writeStream(txStream, [buff], size)
        #print sr
        if not (sr.ret > 0): print("Fail %s, %d"%(sr, numSampsTotal))
        assert(sr.ret > 0)
        numSampsTotal -= sr.ret

    hackrf.deactivateStream(rxStream)
    hackrf.deactivateStream(txStream)

    hackrf.closeStream(rxStream)
    hackrf.closeStream(txStream)