#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#[cfg(feature = "bindgen")]
#[allow(clippy::missing_safety_doc, clippy::too_many_arguments)]
mod bindings {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
#[cfg(all(not(feature = "bindgen"), feature = "dynamic_load"))]
#[allow(clippy::missing_safety_doc, clippy::too_many_arguments)]
mod bindings {
include!("./prebind/bindings_dynamic.rs");
}
#[cfg(all(not(feature = "bindgen"), not(feature = "dynamic_load")))]
#[allow(clippy::missing_safety_doc, clippy::too_many_arguments)]
mod bindings {
include!("./prebind/bindings.rs");
}
pub use bindings::*;
#[cfg(feature = "dynamic_load")]
mod dynamic_loading;
#[cfg(feature = "dynamic_load")]
pub use dynamic_loading::*;
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "dynamic_load")]
#[test]
fn load_missing_library_is_err() {
let r = unsafe { LibVisa::new("nonexistent-visa-library-xyz123") };
assert!(r.is_err(), "loading a missing library should return Err");
}
#[cfg(all(feature = "dynamic_load", target_os = "macos", target_arch = "aarch64"))]
#[test]
fn vsprintf_reads_va_list_arg() {
use std::ffi::{CStr, CString};
unsafe {
let mut rm: ViSession = 0;
assert_eq!(
viOpenDefaultRM(&mut rm as ViPSession),
VI_SUCCESS as ViStatus
);
let mut find: ViFindList = 0;
let mut cnt: ViUInt32 = 0;
let mut desc = [0 as ViChar; VI_FIND_BUFLEN as usize];
let expr = CString::new("?*INSTR").unwrap();
let found = viFindRsrc(rm, expr.as_ptr(), &mut find, &mut cnt, desc.as_mut_ptr());
if found != VI_SUCCESS as ViStatus || cnt == 0 {
viClose(rm);
return; }
let mut instr: ViSession = 0;
if viOpen(
rm,
desc.as_ptr(),
VI_NULL as ViAccessMode,
VI_NULL as ViUInt32,
&mut instr as ViPSession,
) != VI_SUCCESS as ViStatus
{
viClose(rm);
return; }
let mut args: [u64; 3] = [1, 22, 333];
let mut out = [0u8; 64];
let st = viVSPrintf(
instr,
out.as_mut_ptr() as ViPBuf,
c"%d %d %d".as_ptr() as ViConstString,
args.as_mut_ptr() as ViVAList,
);
viClose(instr);
viClose(rm);
assert_eq!(st, VI_SUCCESS as ViStatus, "viVSPrintf returned an error");
let got = CStr::from_ptr(out.as_ptr() as *const std::os::raw::c_char)
.to_str()
.unwrap();
assert_eq!(got, "1 22 333", "va_list arguments not read correctly");
}
}
#[test]
fn open_default_RM() {
let mut session: ViSession = 0;
unsafe {
assert_eq!(viOpenDefaultRM(&mut session as ViPSession), VI_SUCCESS as _,);
}
}
#[test]
fn find_resource() {
let mut defaultRM: ViSession = 0;
let mut find_list: ViFindList = 0;
let mut ret_cnt: ViUInt32 = 0;
let mut instr_desc: [ViChar; VI_FIND_BUFLEN as usize] = [0; VI_FIND_BUFLEN as usize];
unsafe {
assert_eq!(
viOpenDefaultRM(&mut defaultRM as ViPSession),
VI_SUCCESS as _,
"Could not open a session to the VISA Resource Manager!\n"
);
let expr = std::ffi::CString::new(*b"?*INSTR").unwrap();
assert_eq!(
viFindRsrc(
defaultRM,
expr.as_ptr(),
&mut find_list,
&mut ret_cnt,
&mut instr_desc as _
),
VI_SUCCESS as _,
"An error occurred while finding resources.\n"
);
eprintln!(
"{} instruments, serial ports, and other resources found:",
ret_cnt,
);
let mut instr: ViSession = 0;
loop {
eprintln!("{} targets to connect", ret_cnt);
let resource = instr_desc
.into_iter()
.map(|x| x as u8 as char)
.collect::<String>();
eprintln!("connecting to '{}'", resource);
let open_status = viOpen(
defaultRM,
&instr_desc as _,
VI_NULL as _,
VI_NULL as _,
&mut instr as _,
);
if open_status != VI_SUCCESS as _ {
eprintln!(
"An error '{}' occurred opening a session to '{}'\n",
open_status,
instr_desc
.into_iter()
.map(|x| x as u8 as char)
.collect::<String>()
);
} else {
eprintln!("connected to '{}'", resource);
viClose(instr);
}
ret_cnt -= 1;
if ret_cnt > 0 {
let find_status = viFindNext(find_list, &mut instr_desc as _);
if find_status != VI_SUCCESS as _ {
eprintln!(
"An error '{}' occurred finding the next resource\n.",
find_status
);
break;
}
} else {
break;
}
}
viClose(defaultRM);
}
}
}