pub struct Device { /* private fields */ }
Implementations§
Source§impl Device
impl Device
Sourcepub fn new(index: u32) -> Result<Self>
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?
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}
Sourcepub fn get_device_count() -> u32
pub fn get_device_count() -> u32
Sourcepub fn get_device_name(index: u32) -> Option<String>
pub fn get_device_name(index: u32) -> Option<String>
Sourcepub fn get_device_usb_strings(&self) -> Result<(String, String, String)>
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.
Sourcepub fn get_index_by_serial(serial: &str) -> Result<i32>
pub fn get_index_by_serial(serial: &str) -> Result<i32>
Sourcepub fn get_xtal_freq(&self) -> Result<(u32, u32)>
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
Sourcepub fn get_usb_strings(&self) -> Result<(String, String, String)>
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.
Sourcepub fn set_center_freq(&self, freq_hz: u32) -> Result<()>
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?
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}
Sourcepub fn get_center_freq(&self) -> Result<u32>
pub fn get_center_freq(&self) -> Result<u32>
Sourcepub fn set_freq_correction(&self, ppm: i32) -> Result<()>
pub fn set_freq_correction(&self, ppm: i32) -> Result<()>
Sourcepub fn get_freq_correction(&self) -> Result<i32>
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).
Sourcepub fn get_tuner_type(&self) -> Result<RTLSDRTuner>
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
Sourcepub fn get_tuner_gains(&self) -> Result<Vec<i32>>
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 VecError
.
Examples found in repository?
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}
Sourcepub fn set_tuner_gain(&self, gain: i32) -> Result<()>
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?
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}
Sourcepub fn set_tuner_bandwidth(&self, bw_hz: u32) -> Result<()>
pub fn set_tuner_bandwidth(&self, bw_hz: u32) -> Result<()>
Sourcepub fn get_tuner_gain(&self) -> Result<i32>
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
.
Sourcepub fn set_tuner_gain_mode(&self, manual_mode: bool) -> Result<()>
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?
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}
Sourcepub fn set_sample_rate(&self, rate_hz: u32) -> Result<()>
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?
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}
Sourcepub fn get_sample_rate(&self) -> Result<u32>
pub fn get_sample_rate(&self) -> Result<u32>
Sourcepub fn set_test_mode(&self, on: bool) -> Result<()>
pub fn set_test_mode(&self, on: bool) -> Result<()>
Sourcepub fn set_agc_mode(&self, on: bool) -> Result<()>
pub fn set_agc_mode(&self, on: bool) -> Result<()>
Sourcepub fn set_direct_sampling(&self, on: bool) -> Result<()>
pub fn set_direct_sampling(&self, on: bool) -> Result<()>
Sourcepub fn get_direct_sampling(&self) -> Result<bool>
pub fn get_direct_sampling(&self) -> Result<bool>
Sourcepub fn set_offset_tuning(&self, on: bool) -> Result<()>
pub fn set_offset_tuning(&self, on: bool) -> Result<()>
Sourcepub fn get_offset_tuning(&self) -> Result<bool>
pub fn get_offset_tuning(&self) -> Result<bool>
Sourcepub fn reset_buffer(&self) -> Result<()>
pub fn reset_buffer(&self) -> Result<()>
Examples found in repository?
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}
Sourcepub fn read_sync(&self, length: usize) -> Result<Vec<u8>>
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?
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}
Sourcepub 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<()>
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<()>
Sourcepub 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<()>
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<()>
Sourcepub fn cancel_async(&self) -> Result<()>
pub fn cancel_async(&self) -> Result<()>
Sourcepub fn get_hw_info(&self) -> Result<HwInfo>
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.