Skip to main content

visa_sys/
lib.rs

1//! FFI bindings to the VISA (Virtual Instrument Software Architecture) library.
2//!
3//! By default the system VISA library is linked at build time. Enabling the
4//! `dynamic_load` feature instead loads it at run time via `libloading`, so the
5//! same binary can run with or without VISA installed. In that mode the library
6//! auto-loads on the first VISA call (or explicitly via `load_visa_library` /
7//! `load_visa_library_from_path`); the free VISA functions keep identical
8//! signatures in both modes. See the module docs and the crate README for the
9//! initialization model, limitations, and the build-time environment variables
10//! (`LIB_VISA_NAME`, `LIB_VISA_PATH`, `INCLUDE_VISA_PATH`).
11#![allow(non_upper_case_globals)]
12#![allow(non_camel_case_types)]
13#![allow(non_snake_case)]
14
15#[cfg(feature = "bindgen")]
16include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
17
18#[cfg(all(not(feature = "bindgen"), feature = "dynamic_load"))]
19include!("./prebind/bindings_dynamic.rs");
20
21#[cfg(all(not(feature = "bindgen"), not(feature = "dynamic_load")))]
22include!("./prebind/bindings.rs");
23
24#[cfg(feature = "dynamic_load")]
25mod dynamic_loading;
26
27#[cfg(feature = "dynamic_load")]
28pub use dynamic_loading::*;
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    #[test]
34    fn open_default_RM() {
35        let mut session: ViSession = 0;
36        unsafe {
37            assert_eq!(viOpenDefaultRM(&mut session as ViPSession), VI_SUCCESS as _,);
38        }
39    }
40    #[test]
41    fn find_resource() {
42        let mut defaultRM: ViSession = 0;
43        let mut find_list: ViFindList = 0;
44        let mut ret_cnt: ViUInt32 = 0;
45        let mut instr_desc: [ViChar; VI_FIND_BUFLEN as usize] = [0; VI_FIND_BUFLEN as usize];
46        unsafe {
47            assert_eq!(
48                viOpenDefaultRM(&mut defaultRM as ViPSession),
49                VI_SUCCESS as _,
50                "Could not open a session to the VISA Resource Manager!\n"
51            );
52            let expr = std::ffi::CString::new(*b"?*INSTR").unwrap();
53            assert_eq!(
54                viFindRsrc(
55                    defaultRM,
56                    expr.as_ptr(),
57                    &mut find_list,
58                    &mut ret_cnt,
59                    &mut instr_desc as _
60                ),
61                VI_SUCCESS as _,
62                "An error occurred while finding resources.\n"
63            );
64            eprintln!(
65                "{} instruments, serial ports, and other resources found:",
66                ret_cnt,
67            );
68            let mut instr: ViSession = 0;
69
70            loop {
71                eprintln!("{} targets to connect", ret_cnt);
72                let resource = instr_desc
73                    .into_iter()
74                    .map(|x| x as u8 as char)
75                    .collect::<String>();
76                eprintln!("connecting to '{}'", resource);
77                let open_status = viOpen(
78                    defaultRM,
79                    &instr_desc as _,
80                    VI_NULL as _,
81                    VI_NULL as _,
82                    &mut instr as _,
83                );
84                if open_status != VI_SUCCESS as _ {
85                    eprintln!(
86                        "An error '{}' occurred opening a session to '{}'\n",
87                        open_status,
88                        instr_desc
89                            .into_iter()
90                            .map(|x| x as u8 as char)
91                            .collect::<String>()
92                    );
93                } else {
94                    eprintln!("connected to '{}'", resource);
95                    viClose(instr);
96                }
97                ret_cnt -= 1;
98                if ret_cnt > 0 {
99                    let find_status = viFindNext(find_list, &mut instr_desc as _);
100                    if find_status != VI_SUCCESS as _ {
101                        eprintln!(
102                            "An error '{}' occurred finding the next resource\n.",
103                            find_status
104                        );
105                        break;
106                    }
107                } else {
108                    break;
109                }
110            }
111            viClose(defaultRM);
112        }
113    }
114}