Skip to main content

batch_properties/
batch_properties.rs

1// Stage property writes on two channels and apply them all at once with a
2// batched update.  While the batch is open the writes are only staged: reads
3// still return the old values, and nothing takes effect until the commit.
4
5use opendaq::{BatchedPropertyUpdate, Channel, Instance};
6
7fn show_settings(label: &str, channel: &Channel) -> opendaq::Result<()> {
8    println!(
9        "{label} {}:  Amplitude={}  Frequency={}  Waveform={}",
10        channel.name()?,
11        channel.property_value("Amplitude")?,
12        channel.property_value("Frequency")?,
13        channel.property_value("Waveform")?
14    );
15    Ok(())
16}
17
18fn main() -> opendaq::Result<()> {
19    let instance = Instance::new()?;
20    let device = instance.add_device("daqref://device0")?.expect("device");
21    let channels = device.channels()?;
22    let (ch0, ch1) = (&channels[0], &channels[1]);
23
24    show_settings("Before:          ", ch0)?;
25    show_settings("Before:          ", ch1)?;
26
27    let batch = BatchedPropertyUpdate::new(&[ch0, ch1])?;
28    ch0.set_property_value("Amplitude", 2.5)?;
29    ch0.set_property_value("Frequency", 25.0)?;
30    ch0.set_property_value("Waveform", 1)?;
31    ch1.set_property_value("Amplitude", 4.0)?;
32    ch1.set_property_value("Frequency", 50.0)?;
33    ch1.set_property_value("Waveform", 2)?;
34    // Still inside the batch: the writes above are staged but not yet applied.
35    show_settings("During (staged): ", ch0)?;
36    show_settings("During (staged): ", ch1)?;
37    batch.commit()?;
38
39    show_settings("After the batch: ", ch0)?;
40    show_settings("After the batch: ", ch1)?;
41    Ok(())
42}