Skip to main content

simple/
simple.rs

1fn main() -> rtlsdr::Result<()> {
2    let devices = rtlsdr::get_devices();
3    println!("Found {} device(s)", devices.len());
4
5    let mut device = devices[0];
6    device.open()?;
7
8    // Get and print the device information strings
9    let (manufacturer, product, serial) = device.get_usb_device_strings()?;
10    println!("Device: {manufacturer} {product} {serial}");
11
12    // Configure the device with some common settings
13    device.set_center_freq(101_700_000)?; // 101.7 MHz
14    device.set_direct_sampling(rtlsdr::DirectSampling::Disabled)?;
15    device.set_tuner_bandwidth(0)?;
16    device.set_agc_mode(true)?;
17    device.set_tuner_gain_mode(false)?;
18    device.reset_buffer()?;
19
20    // read some samples synchronously
21    let mut buf = [0u8; 1024];
22    let n_read = device.read(&mut buf)?;
23    println!("Read {} bytes of data", n_read);
24
25    // start asynchronous reading
26    println!("Starting asynchronous reading for 3 seconds...");
27    let start = std::time::Instant::now();
28    device.start_reading(
29        |data| {
30            let avg = data.iter().map(|&b| b as u32).sum::<u32>() as f32 / data.len() as f32;
31
32            println!(
33                "Received {} bytes of data, average value: {:.2}",
34                data.len(),
35                avg
36            );
37
38            // return false to stop reading after 3 seconds
39            start.elapsed().as_secs_f32() >= 3.0
40        },
41        0,
42        0,
43    )?;
44
45    println!("Finished asynchronous reading.");
46
47    Ok(())
48}