Instrument

Struct Instrument 

Source
pub struct Instrument<Ctx: UsbContext> {
    pub device: Device<Ctx>,
    pub device_desc: DeviceDescriptor,
    pub config_desc: ConfigDescriptor,
    pub endpoints: TMCInterface,
    /* private fields */
}
Expand description

Information about an instrument detected on the USB bus.

Use list_instruments to find all connected instruments. Use Instrument::open to get an InstrumentHandle which can be used to communicate with the instrument.

Fields§

§device: Device<Ctx>§device_desc: DeviceDescriptor§config_desc: ConfigDescriptor§endpoints: TMCInterface

Implementations§

Source§

impl<Ctx: UsbContext> Instrument<Ctx>

Source

pub fn read_resource_string(&mut self) -> TMCResult<String>

Get the device’s resource string; this may involve connecting to it in order to read its serial number.

Examples found in repository?
examples/list_instruments.rs (line 16)
6fn main() -> Result<(), Box<dyn Error>> {
7  let context = rusb::Context::new()?;
8
9  let timer = Instant::now();
10  let instruments = list_instruments(context)?;
11
12  if instruments.len() == 0 {
13    println!("no instruments found");
14  } else {
15    for mut instrument in instruments {
16      println!("Found instrument: {}", instrument.read_resource_string()?);
17
18      let handle = instrument.open()?;
19      println!("    USBTMC: {:?}", handle.usbtmc_capabilities);
20      println!("    USB488: {:?}", handle.usb488_capabilities);
21      println!("    SCPI ID: {:?}", handle.scpi_id);
22      println!("    PULSE result: {:?}", handle.pulse());
23    }
24  }
25
26  println!("Time elapsed: {:?}", timer.elapsed());
27  Ok(())
28}
More examples
Hide additional examples
examples/u2000a.rs (line 23)
11fn main() -> Result<(), Box<dyn Error>> {
12  let context = rusb::Context::new()?;
13  let instruments = list_instruments(context)?;
14
15  if instruments.len() == 0 {
16    println!("no instruments found");
17    return Ok(());
18  }
19
20  let mut power_sensor = None;
21
22  for mut instrument in instruments {
23    println!("Found instrument: {}", instrument.read_resource_string()?);
24
25    let handle = instrument.open()?;
26    if let Some(id) = &handle.scpi_id {
27      if id.starts_with("Keysight Technologies,U2000A") {
28        println!("Found power sensor: {}", id);
29        power_sensor = Some(handle);
30        break;
31      } else {
32        println!("This is not the instrument I'm looking for: {}", id);
33      }
34    } else {
35      println!("Sensor does not seem to support SCPI");
36    }
37  }
38
39  if let Some(mut handle) = power_sensor {
40    handle.set_max_transfer_size(1024);
41    handle.set_term_char(Some(b'\n'))?;
42
43    handle.write(&format!("SENS1:FREQ {}\n", FREQ_HZ as u64))?;
44    loop {
45      let power_str = handle.ask("FETCH1?")?;
46      let power = power_str.trim().parse::<f64>()?;
47      println!("{:2.3} dBm", power);
48    }
49  } else {
50    println!("Sorry, didn't find a U2000A sensor");
51    Ok(())
52  }
53}
Source

pub fn open(self) -> TMCResult<InstrumentHandle<Ctx>>

Examples found in repository?
examples/list_instruments.rs (line 18)
6fn main() -> Result<(), Box<dyn Error>> {
7  let context = rusb::Context::new()?;
8
9  let timer = Instant::now();
10  let instruments = list_instruments(context)?;
11
12  if instruments.len() == 0 {
13    println!("no instruments found");
14  } else {
15    for mut instrument in instruments {
16      println!("Found instrument: {}", instrument.read_resource_string()?);
17
18      let handle = instrument.open()?;
19      println!("    USBTMC: {:?}", handle.usbtmc_capabilities);
20      println!("    USB488: {:?}", handle.usb488_capabilities);
21      println!("    SCPI ID: {:?}", handle.scpi_id);
22      println!("    PULSE result: {:?}", handle.pulse());
23    }
24  }
25
26  println!("Time elapsed: {:?}", timer.elapsed());
27  Ok(())
28}
More examples
Hide additional examples
examples/u2000a.rs (line 25)
11fn main() -> Result<(), Box<dyn Error>> {
12  let context = rusb::Context::new()?;
13  let instruments = list_instruments(context)?;
14
15  if instruments.len() == 0 {
16    println!("no instruments found");
17    return Ok(());
18  }
19
20  let mut power_sensor = None;
21
22  for mut instrument in instruments {
23    println!("Found instrument: {}", instrument.read_resource_string()?);
24
25    let handle = instrument.open()?;
26    if let Some(id) = &handle.scpi_id {
27      if id.starts_with("Keysight Technologies,U2000A") {
28        println!("Found power sensor: {}", id);
29        power_sensor = Some(handle);
30        break;
31      } else {
32        println!("This is not the instrument I'm looking for: {}", id);
33      }
34    } else {
35      println!("Sensor does not seem to support SCPI");
36    }
37  }
38
39  if let Some(mut handle) = power_sensor {
40    handle.set_max_transfer_size(1024);
41    handle.set_term_char(Some(b'\n'))?;
42
43    handle.write(&format!("SENS1:FREQ {}\n", FREQ_HZ as u64))?;
44    loop {
45      let power_str = handle.ask("FETCH1?")?;
46      let power = power_str.trim().parse::<f64>()?;
47      println!("{:2.3} dBm", power);
48    }
49  } else {
50    println!("Sorry, didn't find a U2000A sensor");
51    Ok(())
52  }
53}

Trait Implementations§

Source§

impl<Ctx: Debug + UsbContext> Debug for Instrument<Ctx>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Ctx> Freeze for Instrument<Ctx>
where Ctx: Freeze,

§

impl<Ctx> RefUnwindSafe for Instrument<Ctx>
where Ctx: RefUnwindSafe,

§

impl<Ctx> Send for Instrument<Ctx>

§

impl<Ctx> Sync for Instrument<Ctx>

§

impl<Ctx> Unpin for Instrument<Ctx>
where Ctx: Unpin,

§

impl<Ctx> UnwindSafe for Instrument<Ctx>
where Ctx: UnwindSafe,

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.