1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use crate::error::TlpmError;
use crate::{PowerMeter, VI_FALSE, VI_TRUE, sys};
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
impl PowerMeter {
/// Initialize a new session with the Thorlabs power meter.
///
/// # Examples
///
/// ```
/// let power_meter = PowerMeter::init("USB0::0x1313::0x8078::P000000::INSTR", true, true);
/// ```
///
/// # Errors
///
/// Returns a `TlpmError::VisaError` if the initialization fails.
pub fn new(resource_name: &str, id_query: bool, reset_device: bool) -> Result<Self, TlpmError> {
tracing::debug!("initializing power meter at resource: {}", resource_name);
let c_resource_name = CString::new(resource_name)
.map_err(|_| TlpmError::InvalidResourceName(resource_name.to_string()))?;
let mut session: sys::ViSession = 0;
let c_id_query = if id_query { VI_TRUE } else { VI_FALSE };
let c_reset_device = if reset_device { VI_TRUE } else { VI_FALSE };
let status = unsafe {
sys::TLPMX_init(
c_resource_name.as_ptr() as *mut _,
c_id_query,
c_reset_device,
&mut session,
)
};
// thorlabs visa functions return less than 0 for errors, 0 for success, and greater than 0 for warnings
if status < 0 {
if session != 0 {
unsafe {
sys::TLPMX_close(session);
}
}
return Err(TlpmError::VisaError {
code: status,
action: "new".to_string(),
message: "initialization failed".to_string(),
});
}
tracing::debug!("succesfully initialized session: {}", session);
Ok(Self {
session,
_marker: PhantomData,
})
}
/// Initialize a new encrypted session with the Thorlabs power meter.
///
/// # Arguments
///
/// * `resource_name` - The VISA resource string (e.g., "USB0::0x1313::...").
/// * `id_query` - `true` to perform an ID query during initialization.
/// * `reset_device` - `true` to reset the device to its default state during initialization.
/// * `password` - The password string required to unlock the encrypted connection.
///
/// # Returns
///
/// A new, authenticated `PowerMeter` instance.
///
/// # Errors
///
/// Returns a `TlpmError::InvalidResourceName` or `TlpmError::StringConversion` for invalid strings,
/// or a `TlpmError::VisaError` if the initialization or authentication fails.
pub fn new_with_encryption(
resource_name: &str,
id_query: bool,
reset_device: bool,
password: &str,
) -> Result<Self, TlpmError> {
tracing::debug!(
"initializing encrypted power meter session at resource: {}",
resource_name
);
let c_resource_name = CString::new(resource_name)
.map_err(|_| TlpmError::InvalidResourceName(resource_name.to_string()))?;
let c_password = CString::new(password)
.map_err(|_| TlpmError::StringConversion("invalid password string".to_string()))?;
let mut session: sys::ViSession = 0;
let c_id_query = if id_query { VI_TRUE } else { VI_FALSE };
let c_reset_device = if reset_device { VI_TRUE } else { VI_FALSE };
let status = unsafe {
sys::TLPMX_initWithEncryption(
c_resource_name.as_ptr() as *mut _,
c_id_query,
c_reset_device,
c_password.as_ptr() as *mut _,
&mut session,
)
};
// Thorlabs VISA functions return less than 0 for errors
if status < 0 {
if session != 0 {
unsafe {
sys::TLPMX_close(session);
}
}
return Err(TlpmError::VisaError {
code: status,
action: "new_with_encryption".to_string(),
message: "initialization with encryption failed".to_string(),
});
}
tracing::debug!("successfully initialized encrypted session: {}", session);
Ok(Self {
session,
_marker: PhantomData,
})
}
/// Reset the Thorlabs power meter to its default parameters.
///
/// # Errors
///
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn reset(&self) -> Result<(), TlpmError> {
self.check_status(unsafe { sys::TLPMX_reset(self.session) }, "reset")
}
// helper method to translate a visa status into a rust result with context
pub(crate) fn check_status(
&self,
status: sys::ViStatus,
action: &str,
) -> Result<(), TlpmError> {
if status < 0 {
let message = self.get_error_message(status);
tracing::debug!(
"visa error during {}: {} (code: {})",
action,
message,
status
);
Err(TlpmError::VisaError {
code: status,
action: action.to_string(),
message: self.get_error_message(status),
})
} else {
// ignore warnings
Ok(())
}
}
// queries the thorlabs driver for the human readable error description
pub(crate) fn get_error_message(&self, error_code: sys::ViStatus) -> String {
let mut buffer: [sys::ViChar; sys::TLPM_ERR_DESCR_BUFFER_SIZE as usize] =
[0; sys::TLPM_ERR_DESCR_BUFFER_SIZE as usize];
let status =
unsafe { sys::TLPMX_errorMessage(self.session, error_code, buffer.as_mut_ptr()) };
if status < 0 {
return "failed to retrieve error message from driver".to_string();
}
let c_str = unsafe { CStr::from_ptr(buffer.as_ptr()) };
c_str.to_string_lossy().into_owned()
}
}
impl Drop for PowerMeter {
fn drop(&mut self) {
if self.session != 0 {
unsafe {
// ensure the session is cleanly closed when the power meter goes out of scope
sys::TLPMX_close(self.session);
}
}
}
}