Skip to main content

demo/
demo.rs

1use simple_scpi::{Command, CommandSet, Handler, Param};
2
3const TABLE: &[(&str, Handler)] = &[
4    ("*IDN?", idn),
5    ("*RST", rst),
6    ("SOURce#:FREQuency num", src_freq),
7    ("SOURce#:FREQuency?", src_freq_q),
8    ("SOURce#:VOLTage[:LEVel] num", src_volt),
9    ("OUTPut#:STATe bool", outp_stat),
10    ("DISPlay:TEXT str", disp_text),
11];
12
13fn main() {
14    let set = CommandSet::from_table(TABLE).expect("bad command table");
15    let stdin = std::io::stdin();
16    let mut line = String::new();
17    while stdin.read_line(&mut line).is_ok_and(|n| n > 0) {
18        match set.parse(line.trim()) {
19            Ok(cmds) => {
20                for cmd in &cmds {
21                    set.dispatch(cmd);
22                }
23            }
24            Err(e) => eprintln!("ERROR: {e}"),
25        }
26        line.clear();
27    }
28}
29
30fn idn(_: &Command) {
31    println!("ACME Instruments,Model 100,SN0001,1.0");
32}
33
34fn rst(_: &Command) {
35    println!("reset");
36}
37
38fn src_freq(cmd: &Command) {
39    let Param::Numeric(f) = cmd.params[0] else { return };
40    println!("source {} freq = {f} Hz", cmd.suffixes[0]);
41}
42
43fn src_freq_q(cmd: &Command) {
44    println!("source {} freq?", cmd.suffixes[0]);
45}
46
47fn src_volt(cmd: &Command) {
48    let Param::Numeric(v) = cmd.params[0] else { return };
49    println!("source {} volt = {v} V", cmd.suffixes[0]);
50}
51
52fn outp_stat(cmd: &Command) {
53    let Param::Bool(b) = cmd.params[0] else { return };
54    println!("output {} = {}", cmd.suffixes[0], if b { "ON" } else { "OFF" });
55}
56
57fn disp_text(cmd: &Command) {
58    let Param::String(ref s) = cmd.params[0] else { return };
59    println!("display: {s:?}");
60}