forked from incognitojam/rtl-sdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
125 lines (99 loc) · 3.43 KB
/
example.js
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
const rtlsdr = require("rtl-sdr");
// Index of the device we want to use
const deviceIndex = 0;
// You can either set the gain manually, choose to have the RTLSDR
// device select the right gain for you, or probe it to find the maxium
// gain and then use that:
//
// A) Set to true to ask RTLSDR device to automatically set gain
const autoGain = false;
// B) Or probe RTLSDR device to find the maximum possible gain
const maxGain = true;
// C) Or just manually set a gain value (in tenths of a dB: e.g. 115 == 11.b dB)
let gain = 0;
// Correction value in parts per million (ppm) to use in frequency
// correction
const ppmCorrection = 0;
// Set to false to disable digital AGC mode
const agcMode = true;
// Set center frequency to tune to
const freq = 1090e6;
// Set center frequency to tune to
const sampleRate = 2e6;
// Output buffers used with get_device_usb_strings
const vendor = Buffer.alloc(256);
const product = Buffer.alloc(256);
const serial = Buffer.alloc(256);
const deviceCount = rtlsdr.get_device_count();
if (!deviceCount) {
console.log("No supported RTLSDR devices found.");
process.exit(1);
}
console.log("Found %d device(s):", deviceCount);
for (let i = 0; i < deviceCount; i++) {
rtlsdr.get_device_usb_strings(i, vendor, product, serial);
console.log(
"%d: %s, %s, SN: %s %s",
i,
vendor,
product,
serial,
i === deviceIndex ? "(currently selected)" : ""
);
}
const dev = rtlsdr.open(deviceIndex);
if (typeof dev === "number") {
console.log("Error opening the RTLSDR device: %s", dev);
process.exit(1);
}
// Set gain mode to max
rtlsdr.set_tuner_gain_mode(dev, 1);
if (!autoGain) {
if (maxGain) {
// Find the maximum gain available
// Prepare output array for rtl-sdr to write out possible gain
// values for the device. We set it a large size so it should be
// possible to accomondate all types of devices:
const gains = new Int32Array(100);
// Populate the gains array and get the actual number of different
// gains available. This number will be less than the actual size of
// the array:
const numgains = rtlsdr.get_tuner_gains(dev, gains);
// Get the highest gain possible
gain = gains[numgains - 1];
console.log("Max available gain is: %d", gain / 10);
}
// Set the tuner to the selected gain
console.log("Setting gain to: %d", gain / 10);
rtlsdr.set_tuner_gain(dev, gain);
} else {
console.log("Using automatic gain control");
}
// Set the frequency correction value for the device
rtlsdr.set_freq_correction(dev, ppmCorrection);
// Enable or disable the internal digital AGC of the RTL2822
rtlsdr.set_agc_mode(dev, agcMode ? 1 : 0);
// Tune center frequency
rtlsdr.set_center_freq(dev, freq);
// Select sample rate
rtlsdr.set_sample_rate(dev, sampleRate);
// Reset the internal buffer
rtlsdr.reset_buffer(dev);
console.log("Gain reported by device: %d", rtlsdr.get_tuner_gain(dev) / 10);
// Start reading data from the device:
//
// bufNum: optional buffer count, bufNum * bufLen = overall buffer size
// set to 0 for default buffer count (15)
//
// bufLen: optional buffer length, must be multiple of 512, should be a
// multiple of 16384 (URB size), set to 0 for default buffer
// length (2^18)
const bufNum = 12;
const bufLen = 2 ** 18; // 2^18 == 256k
rtlsdr.read_async(dev, onData, onEnd, bufNum, bufLen);
const onData = (data, size) => {
console.log("onData[%d]:", size, data);
};
const onEnd = () => {
console.log("onEnd");
};