Struct Device

Source
pub struct Device { /* private fields */ }

Implementations§

Source§

impl Device

Source

pub fn new(index: u32) -> Result<Self>

Open a RTL-SDR device by index.

§Arguments
  • index - The index of the device to open.
§Returns

A new Device instance if successful, otherwise an Error.

Examples found in repository?
examples/rcv.rs (line 8)
6fn main() -> Result<(), Box<dyn Error>> {
7    // Open the first RTL-SDR device
8    let device = Device::new(0)?;
9
10    // Set the center frequency to 100 MHz
11    device.set_center_freq(100_000_000)?;
12
13    // Set the sample rate to 2.048 MS/s
14    device.set_sample_rate(2_048_000)?;
15
16    // Enable manual gain control and set the gain to maximum
17    device.set_tuner_gain_mode(true)?;
18    if let Some(&max_gain) = device.get_tuner_gains()?.last() {
19        device.set_tuner_gain(max_gain)?;
20    }
21
22    // Reset the buffer before reading
23    device.reset_buffer()?;
24
25    // Read 256,000 bytes of IQ data
26    let data = device.read_sync(256_000)?;
27
28    // Write the data to a binary file
29    let output_file_path = "samples.bin";
30    File::create(output_file_path)?.write_all(&data)?;
31    println!(
32        "Wrote {} bytes of IQ data to {}",
33        data.len(),
34        output_file_path
35    );
36
37    Ok(())
38}
Source

pub fn get_device_count() -> u32

Get the number of available devices.

§Returns

The number of available devices.

Source

pub fn get_device_name(index: u32) -> Option<String>

Get the name of the device by index.

§Arguments
  • index - The index of the device.
§Returns

The name of the device if successful, otherwise None.

Source

pub fn get_device_usb_strings(&self) -> Result<(String, String, String)>

Get the USB strings of the device.

§Returns

The manufacturer, product, and serial strings of the device.

Source

pub fn get_index_by_serial(serial: &str) -> Result<i32>

Get the index of the device by serial number.

§Arguments
  • serial - The serial number of the device.
§Returns

The index of the device if successful, otherwise an Error.

Source

pub fn close(&self) -> Result<()>

Close the device.

§Returns

An Ok result if successful, otherwise an Error.

Source

pub fn set_xtal_freq(&self, rtl_freq_hz: u32, tuner_freq_hz: u32) -> Result<()>

Set the center frequency of the device.

§Arguments
  • rtl_freq_hz - The frequency in Hz to set.
  • tuner_freq_hz - The tuner frequency in Hz to set.
§Returns

An Ok result if successful, otherwise an Error.

Source

pub fn get_xtal_freq(&self) -> Result<(u32, u32)>

Get the crystal frequency of the device.

§Returns

The device’s crystal frequency as a tuple of rtl_freq_hz and `tuner_freq_hz

Source

pub fn get_usb_strings(&self) -> Result<(String, String, String)>

Get the USB strings of the device.

§Returns

The manufacturer, product, and serial strings of the device.

Source

pub fn write_eeprom(&self, data: &[u8], offset: u8) -> Result<()>

Write data to the EEPROM of the device.

§Arguments
  • data - The data to write to the EEPROM.
  • offset - The offset to write the data to.
§Returns

An Ok result if successful, otherwise an Error.

Source

pub fn read_eeprom(&self, offset: u8, len: u16) -> Result<Vec<u8>>

Read data from the EEPROM of the device.

§Arguments
  • offset - The offset to read the data from.
  • len - The length of the data to read.
§Returns

A vector of data read from the EEPROM.

Source

pub fn set_center_freq(&self, freq_hz: u32) -> Result<()>

Set the sample rate of the device.

§Arguments
  • rate_hz - The sample rate in Hz to set.
§Returns

An Ok result if successful, otherwise an Error.

Examples found in repository?
examples/rcv.rs (line 11)
6fn main() -> Result<(), Box<dyn Error>> {
7    // Open the first RTL-SDR device
8    let device = Device::new(0)?;
9
10    // Set the center frequency to 100 MHz
11    device.set_center_freq(100_000_000)?;
12
13    // Set the sample rate to 2.048 MS/s
14    device.set_sample_rate(2_048_000)?;
15
16    // Enable manual gain control and set the gain to maximum
17    device.set_tuner_gain_mode(true)?;
18    if let Some(&max_gain) = device.get_tuner_gains()?.last() {
19        device.set_tuner_gain(max_gain)?;
20    }
21
22    // Reset the buffer before reading
23    device.reset_buffer()?;
24
25    // Read 256,000 bytes of IQ data
26    let data = device.read_sync(256_000)?;
27
28    // Write the data to a binary file
29    let output_file_path = "samples.bin";
30    File::create(output_file_path)?.write_all(&data)?;
31    println!(
32        "Wrote {} bytes of IQ data to {}",
33        data.len(),
34        output_file_path
35    );
36
37    Ok(())
38}
Source

pub fn get_center_freq(&self) -> Result<u32>

Get the center frequency of the device.

§Returns

The device’s center frequency.

Source

pub fn set_freq_correction(&self, ppm: i32) -> Result<()>

Set the frequency correction of the device.

§Arguments
  • ppm - The frequency correction in parts per million (ppm) to set.
§Returns

An Ok result if successful, otherwise an Error.

Source

pub fn get_freq_correction(&self) -> Result<i32>

Get the frequency correction of the device.

§Returns

The device’s frequency correction in parts per million (ppm).

Source

pub fn get_tuner_type(&self) -> Result<RTLSDRTuner>

Get the tuner type of the device.

§Returns

The device’s tuner type as an RTLSDRTuner if successful, otherwise an `Error

Source

pub fn get_tuner_gains(&self) -> Result<Vec<i32>>

Get the tuner gain mode of the device.

§Returns

The device’s tuner gain mode as a Vec if successful, otherwise an Error.

Examples found in repository?
examples/rcv.rs (line 18)
6fn main() -> Result<(), Box<dyn Error>> {
7    // Open the first RTL-SDR device
8    let device = Device::new(0)?;
9
10    // Set the center frequency to 100 MHz
11    device.set_center_freq(100_000_000)?;
12
13    // Set the sample rate to 2.048 MS/s
14    device.set_sample_rate(2_048_000)?;
15
16    // Enable manual gain control and set the gain to maximum
17    device.set_tuner_gain_mode(true)?;
18    if let Some(&max_gain) = device.get_tuner_gains()?.last() {
19        device.set_tuner_gain(max_gain)?;
20    }
21
22    // Reset the buffer before reading
23    device.reset_buffer()?;
24
25    // Read 256,000 bytes of IQ data
26    let data = device.read_sync(256_000)?;
27
28    // Write the data to a binary file
29    let output_file_path = "samples.bin";
30    File::create(output_file_path)?.write_all(&data)?;
31    println!(
32        "Wrote {} bytes of IQ data to {}",
33        data.len(),
34        output_file_path
35    );
36
37    Ok(())
38}
Source

pub fn set_tuner_gain(&self, gain: i32) -> Result<()>

Set the tuner gain of the device.

§Arguments
  • gain - The tuner gain to set.
§Returns

An Ok result if successful, otherwise an Error.

Examples found in repository?
examples/rcv.rs (line 19)
6fn main() -> Result<(), Box<dyn Error>> {
7    // Open the first RTL-SDR device
8    let device = Device::new(0)?;
9
10    // Set the center frequency to 100 MHz
11    device.set_center_freq(100_000_000)?;
12
13    // Set the sample rate to 2.048 MS/s
14    device.set_sample_rate(2_048_000)?;
15
16    // Enable manual gain control and set the gain to maximum
17    device.set_tuner_gain_mode(true)?;
18    if let Some(&max_gain) = device.get_tuner_gains()?.last() {
19        device.set_tuner_gain(max_gain)?;
20    }
21
22    // Reset the buffer before reading
23    device.reset_buffer()?;
24
25    // Read 256,000 bytes of IQ data
26    let data = device.read_sync(256_000)?;
27
28    // Write the data to a binary file
29    let output_file_path = "samples.bin";
30    File::create(output_file_path)?.write_all(&data)?;
31    println!(
32        "Wrote {} bytes of IQ data to {}",
33        data.len(),
34        output_file_path
35    );
36
37    Ok(())
38}
Source

pub fn set_tuner_bandwidth(&self, bw_hz: u32) -> Result<()>

Set the tuner bandwidth of the device.

§Arguments
  • bw_hz - The tuner bandwidth in Hz to set.
§Returns

An Ok result if successful, otherwise an Error.

Source

pub fn get_tuner_gain(&self) -> Result<i32>

Get the tuner gain of the device.

§Returns

The device’s tuner gain if successful, otherwise an Error.

Source

pub fn set_tuner_if_gain(&self, stage: i32, gain: i32) -> Result<()>

Set the tuner IF gain of the device.

§Arguments
  • stage - The stage of the tuner IF gain to set.
  • gain - The tuner IF gain to set.
§Returns

An Ok result if successful, otherwise an Error.

Source

pub fn set_tuner_gain_mode(&self, manual_mode: bool) -> Result<()>

Set the tuner gain mode of the device.

§Arguments
  • manual_mode - The tuner gain mode to set.
§Returns

An Ok result if successful, otherwise an Error.

Examples found in repository?
examples/rcv.rs (line 17)
6fn main() -> Result<(), Box<dyn Error>> {
7    // Open the first RTL-SDR device
8    let device = Device::new(0)?;
9
10    // Set the center frequency to 100 MHz
11    device.set_center_freq(100_000_000)?;
12
13    // Set the sample rate to 2.048 MS/s
14    device.set_sample_rate(2_048_000)?;
15
16    // Enable manual gain control and set the gain to maximum
17    device.set_tuner_gain_mode(true)?;
18    if let Some(&max_gain) = device.get_tuner_gains()?.last() {
19        device.set_tuner_gain(max_gain)?;
20    }
21
22    // Reset the buffer before reading
23    device.reset_buffer()?;
24
25    // Read 256,000 bytes of IQ data
26    let data = device.read_sync(256_000)?;
27
28    // Write the data to a binary file
29    let output_file_path = "samples.bin";
30    File::create(output_file_path)?.write_all(&data)?;
31    println!(
32        "Wrote {} bytes of IQ data to {}",
33        data.len(),
34        output_file_path
35    );
36
37    Ok(())
38}
Source

pub fn set_sample_rate(&self, rate_hz: u32) -> Result<()>

Set the sample rate of the device.

§Arguments
  • rate_hz - The sample rate in Hz to set.
§Returns

An Ok result if successful, otherwise an Error.

Examples found in repository?
examples/rcv.rs (line 14)
6fn main() -> Result<(), Box<dyn Error>> {
7    // Open the first RTL-SDR device
8    let device = Device::new(0)?;
9
10    // Set the center frequency to 100 MHz
11    device.set_center_freq(100_000_000)?;
12
13    // Set the sample rate to 2.048 MS/s
14    device.set_sample_rate(2_048_000)?;
15
16    // Enable manual gain control and set the gain to maximum
17    device.set_tuner_gain_mode(true)?;
18    if let Some(&max_gain) = device.get_tuner_gains()?.last() {
19        device.set_tuner_gain(max_gain)?;
20    }
21
22    // Reset the buffer before reading
23    device.reset_buffer()?;
24
25    // Read 256,000 bytes of IQ data
26    let data = device.read_sync(256_000)?;
27
28    // Write the data to a binary file
29    let output_file_path = "samples.bin";
30    File::create(output_file_path)?.write_all(&data)?;
31    println!(
32        "Wrote {} bytes of IQ data to {}",
33        data.len(),
34        output_file_path
35    );
36
37    Ok(())
38}
Source

pub fn get_sample_rate(&self) -> Result<u32>

Get the sample rate of the device.

§Returns

The device’s sample rate.

Source

pub fn set_test_mode(&self, on: bool) -> Result<()>

Set the test mode of the device.

§Arguments
  • on - The test mode to set.
§Returns

An Ok result if successful, otherwise an Error.

Source

pub fn set_agc_mode(&self, on: bool) -> Result<()>

Set the AGC mode of the device.

§Arguments
  • on - The AGC mode to set.
§Returns

An Ok result if successful, otherwise an Error.

Source

pub fn set_direct_sampling(&self, on: bool) -> Result<()>

Set the direct sampling mode of the device.

§Arguments
  • on - The direct sampling mode to set.
§Returns

An Ok result if successful, otherwise an Error.

Source

pub fn get_direct_sampling(&self) -> Result<bool>

Get the direct sampling state of the device.

§Returns

The device’s direct sampling state.

Source

pub fn set_offset_tuning(&self, on: bool) -> Result<()>

Set the offset tuning mode of the device.

§Arguments
  • on - The offset tuning mode to set.
§Returns

An Ok result if successful, otherwise an Error.

Source

pub fn get_offset_tuning(&self) -> Result<bool>

Get the offset tuning state of the device.

§Returns

The device’s offset tuning state.

Source

pub fn reset_buffer(&self) -> Result<()>

Reset the buffer of the device.

§Returns

An Ok result if successful, otherwise an Error.

Examples found in repository?
examples/rcv.rs (line 23)
6fn main() -> Result<(), Box<dyn Error>> {
7    // Open the first RTL-SDR device
8    let device = Device::new(0)?;
9
10    // Set the center frequency to 100 MHz
11    device.set_center_freq(100_000_000)?;
12
13    // Set the sample rate to 2.048 MS/s
14    device.set_sample_rate(2_048_000)?;
15
16    // Enable manual gain control and set the gain to maximum
17    device.set_tuner_gain_mode(true)?;
18    if let Some(&max_gain) = device.get_tuner_gains()?.last() {
19        device.set_tuner_gain(max_gain)?;
20    }
21
22    // Reset the buffer before reading
23    device.reset_buffer()?;
24
25    // Read 256,000 bytes of IQ data
26    let data = device.read_sync(256_000)?;
27
28    // Write the data to a binary file
29    let output_file_path = "samples.bin";
30    File::create(output_file_path)?.write_all(&data)?;
31    println!(
32        "Wrote {} bytes of IQ data to {}",
33        data.len(),
34        output_file_path
35    );
36
37    Ok(())
38}
Source

pub fn read_sync(&self, length: usize) -> Result<Vec<u8>>

Read data from the device synchronously.

§Arguments
  • length - The length of the data to read.
§Returns

A vector of data read from the device.

Examples found in repository?
examples/rcv.rs (line 26)
6fn main() -> Result<(), Box<dyn Error>> {
7    // Open the first RTL-SDR device
8    let device = Device::new(0)?;
9
10    // Set the center frequency to 100 MHz
11    device.set_center_freq(100_000_000)?;
12
13    // Set the sample rate to 2.048 MS/s
14    device.set_sample_rate(2_048_000)?;
15
16    // Enable manual gain control and set the gain to maximum
17    device.set_tuner_gain_mode(true)?;
18    if let Some(&max_gain) = device.get_tuner_gains()?.last() {
19        device.set_tuner_gain(max_gain)?;
20    }
21
22    // Reset the buffer before reading
23    device.reset_buffer()?;
24
25    // Read 256,000 bytes of IQ data
26    let data = device.read_sync(256_000)?;
27
28    // Write the data to a binary file
29    let output_file_path = "samples.bin";
30    File::create(output_file_path)?.write_all(&data)?;
31    println!(
32        "Wrote {} bytes of IQ data to {}",
33        data.len(),
34        output_file_path
35    );
36
37    Ok(())
38}
Source

pub fn wait_async( &self, callback: Option<unsafe extern "C" fn(buf: *mut c_uchar, len: u32, ctx: *mut c_void)>, ctx: *mut c_void, ) -> Result<()>

Wait for asynchronous data to be read from the device.

§Arguments
  • callback - The callback function to call when data is read.
  • ctx - The context to pass to the callback function.
§Returns

An Ok result if successful, otherwise an Error.

Source

pub fn read_async( &self, callback: Option<unsafe extern "C" fn(buf: *mut c_uchar, len: u32, ctx: *mut c_void)>, ctx: *mut c_void, buf_num: u32, buf_len: u32, ) -> Result<()>

Read data from the device asynchronously.

§Arguments
  • callback - The callback function to call when data is read.
  • ctx - The context to pass to the callback function.
§Returns

An Ok result if successful, otherwise an Error.

Source

pub fn cancel_async(&self) -> Result<()>

Cancel an asynchronous read operation.

§Returns

An Ok result if successful, otherwise an Error.

Source

pub fn get_hw_info(&self) -> Result<HwInfo>

Get the device’s USB vendor, product ID, etc.

§Returns

The hardware information of the device as a HwInfo struct.

Source

pub fn set_hw_info(&self, info: &HwInfo) -> Result<()>

Set the hardware information of the device.

§Arguments
  • info - The hardware information to set as a HwInfo struct.
§Returns

An Ok result if successful, otherwise an Error.

Trait Implementations§

Source§

impl Drop for Device

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for Device

§

impl RefUnwindSafe for Device

§

impl !Send for Device

§

impl !Sync for Device

§

impl Unpin for Device

§

impl UnwindSafe for Device

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.