visa_sys/
lib.rs

1#![allow(non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4
5#[cfg(feature = "bindgen")]
6include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
7
8#[cfg(not(feature = "bindgen"))]
9include!("./prebind/bindings.rs");
10
11#[cfg(test)]
12mod tests {
13    use super::*;
14    #[test]
15    fn open_default_RM() {
16        let mut session: ViSession = 0;
17        unsafe {
18            assert_eq!(viOpenDefaultRM(&mut session as ViPSession), VI_SUCCESS as _,);
19        }
20    }
21    #[test]
22    fn find_resource() {
23        let mut defaultRM: ViSession = 0;
24        let mut find_list: ViFindList = 0;
25        let mut ret_cnt: ViUInt32 = 0;
26        let mut instr_desc: [ViChar; VI_FIND_BUFLEN as usize] = [0; VI_FIND_BUFLEN as usize];
27        unsafe {
28            assert_eq!(
29                viOpenDefaultRM(&mut defaultRM as ViPSession),
30                VI_SUCCESS as _,
31                "Could not open a session to the VISA Resource Manager!\n"
32            );
33            let expr = std::ffi::CString::new(*b"?*INSTR").unwrap();
34            assert_eq!(
35                viFindRsrc(
36                    defaultRM,
37                    expr.as_ptr(),
38                    &mut find_list,
39                    &mut ret_cnt,
40                    &mut instr_desc as _
41                ),
42                VI_SUCCESS as _,
43                "An error occurred while finding resources.\n"
44            );
45            eprintln!(
46                "{} instruments, serial ports, and other resources found:",
47                ret_cnt,
48            );
49            let mut instr: ViSession = 0;
50
51            loop {
52                eprintln!("{} targets to connect", ret_cnt);
53                let resource = instr_desc
54                    .into_iter()
55                    .map(|x| x as u8 as char)
56                    .collect::<String>();
57                eprintln!("connecting to '{}'", resource);
58                let open_status = viOpen(
59                    defaultRM,
60                    &instr_desc as _,
61                    VI_NULL,
62                    VI_NULL,
63                    &mut instr as _,
64                );
65                if open_status != VI_SUCCESS as _ {
66                    eprintln!(
67                        "An error '{}' occurred opening a session to '{}'\n",
68                        open_status,
69                        instr_desc
70                            .into_iter()
71                            .map(|x| x as u8 as char)
72                            .collect::<String>()
73                    );
74                } else {
75                    eprintln!("connected to '{}'", resource);
76                    viClose(instr);
77                }
78                ret_cnt -= 1;
79                if ret_cnt > 0 {
80                    let find_status = viFindNext(find_list, &mut instr_desc as _);
81                    if find_status != VI_SUCCESS as _ {
82                        eprintln!(
83                            "An error '{}' occurred finding the next resource\n.",
84                            find_status
85                        );
86                        break;
87                    }
88                } else {
89                    break;
90                }
91            }
92            viClose(defaultRM);
93        }
94    }
95}