Skip to main content

instrument_core/
connect.rs

1use std::time::Duration;
2
3/// VISA access mode (maps to visa-rs AccessMode when using visa backend).
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
5pub struct AccessMode {
6    pub exclusive_lock: bool,
7    pub shared_lock: bool,
8}
9
10impl AccessMode {
11    pub const NO_LOCK: Self = Self {
12        exclusive_lock: false,
13        shared_lock: false,
14    };
15
16    pub const SHARED_LOCK: Self = Self {
17        exclusive_lock: false,
18        shared_lock: true,
19    };
20
21    pub const EXCLUSIVE_LOCK: Self = Self {
22        exclusive_lock: true,
23        shared_lock: false,
24    };
25}
26
27/// Options when opening an instrument session.
28#[derive(Debug, Clone)]
29pub struct ConnectOptions {
30    pub open_timeout: Duration,
31    pub read_timeout: Duration,
32    pub write_timeout: Duration,
33    pub per_op_timeout: Option<Duration>,
34    pub terminator: String,
35    pub access_mode: AccessMode,
36    pub reset_on_connect: bool,
37    pub retries: u32,
38    pub retry_backoff: Duration,
39    pub reconnect_on_failure: bool,
40}
41
42impl Default for ConnectOptions {
43    fn default() -> Self {
44        Self {
45            open_timeout: Duration::from_secs(5),
46            read_timeout: Duration::from_secs(10),
47            write_timeout: Duration::from_secs(10),
48            per_op_timeout: None,
49            terminator: "\n".to_string(),
50            access_mode: AccessMode::NO_LOCK,
51            reset_on_connect: false,
52            retries: 2,
53            retry_backoff: Duration::from_millis(100),
54            reconnect_on_failure: true,
55        }
56    }
57}
58
59impl ConnectOptions {
60    pub fn with_read_timeout(mut self, timeout: Duration) -> Self {
61        self.read_timeout = timeout;
62        self
63    }
64
65    pub fn with_per_op_timeout(mut self, timeout: Duration) -> Self {
66        self.per_op_timeout = Some(timeout);
67        self
68    }
69}