visa_device_handler/visa_interface/
object.rs

1#![allow(non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4#![allow(dead_code)]
5
6
7use super::visa_ffi::*;
8use super::err::VisaWrapperError;
9use dlopen2::wrapper::Container;
10use std::borrow::Cow;
11// use visa::Visa;
12#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
13pub enum Binary {
14    ///Keysight specific Visa binary which only exists if Keysight IO is installed. Source: <https://www.keysight.com/de/de/lib/software-detail/computer-software/io-libraries-suite-downloads-2175637/keysight-io-libraries-suite-2022-for-windows.html>
15    Keysight,
16    ///National Instruments specific Visa binary which only exists if Ni-Visa is installed. Source: <https://www.ni.com/en-us/support/downloads/drivers/download.ni-visa.html>
17    NiVisa,
18    #[default]
19    ///Primary visa binary. This could be any vendor implementation. If visa from any vendor is installed, this option typically works. The primary binary is typically named visa32.dll in windows.
20    PlatformDefault,
21    ///Custom path to a binary
22    Custom(String),
23}
24
25impl Binary {
26    fn binary_name(&self) -> Result<Cow<str>, VisaWrapperError> {
27        Ok(match self {
28            Binary::Keysight => {
29                if cfg!(target_family = "windows") {
30                    "ktvisa32.dll".into()
31                } else if cfg!(target_family = "unix") && cfg!(target_pointer_width = "64") {
32                    "libiovisa.so".into()
33                } else {
34                    return Err(VisaWrapperError::UnsupportedPlatform);
35                }
36            } //Keysight doesn't have official support for unix 32bit however it might have a .so file for 32bit
37            Binary::NiVisa => {
38                if cfg!(target_family = "windows") && cfg!(target_pointer_width = "64") {
39                    "nivisa64.dll".into()
40                } else if cfg!(target_family = "windows") && cfg!(target_pointer_width = "32") {
41                    "visa32.dll".into()
42                } else if cfg!(target_family = "unix") && cfg!(target_pointer_width = "64") {
43                    "libvisa.so".into()
44                } else {
45                    return Err(VisaWrapperError::UnsupportedPlatform);
46                }
47            } //NiVisa doesn't have official support for unix 32bit however it might have a .so file for 32bit
48            Binary::PlatformDefault => {
49                if cfg!(target_family = "windows") {
50                    "visa32".into()
51                } else if cfg!(target_family = "unix") && cfg!(target_pointer_width = "64") {
52                    "libvisa.so".into()
53                } else if cfg!(target_family = "unix") && cfg!(target_pointer_width = "32") {
54                    "libvisa32.so".into()
55                } else {
56                    return Err(VisaWrapperError::UnsupportedPlatform);
57                }
58            }
59            Binary::Custom(path) => path.into(),
60        })
61    }
62}
63
64impl ToString for Binary {
65    fn to_string(&self) -> String {
66        self.binary_name()
67            .unwrap_or_else(|e| e.to_string().into())
68            .into()
69    }
70}
71///This factory method loads a visa dynamically linked library .dll or .so etc.
72///```rust
73/// let visa =   visa::create(&visa::Binary::Keysight)
74/// .or_else(|_| visa::create(&visa::Binary::NiVisa))
75/// .or_else(|_| visa::create(&visa::Binary::Primary))
76/// .or_else(|_| visa::create(&visa::Binary::Custom("visa.so".into())));
77///```
78pub fn make(bin: &Binary) -> Result<Container<VisaFFI>, VisaWrapperError> {
79    unsafe { Container::load(bin.binary_name()?.as_ref()).map_err(|e| VisaWrapperError::from(e)) }
80}