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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use crate::error::TlpmError;
use crate::{PowerMeter, VI_TRUE, sys};
use std::ffi::CStr;
impl PowerMeter {
/// Retrieve comprehensive sensor information using the extended 32-bit flags.
///
/// # Arguments
///
/// * `channel` - The sensor channel (typically `1`).
///
/// # Returns
///
/// A tuple containing `(name, serial_number, message, type, subtype, flags)`.
///
/// # Errors
///
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn get_sensor_info(
&self,
channel: u16,
) -> Result<(String, String, String, i16, i16, i32), TlpmError> {
tracing::debug!("getting sensor info on channel {}", channel);
let mut name = [0i8; sys::TLPM_BUFFER_SIZE as usize];
let mut snr = [0i8; sys::TLPM_BUFFER_SIZE as usize];
let mut message = [0i8; sys::TLPM_BUFFER_SIZE as usize];
let mut p_type: i16 = 0;
let mut p_stype: i16 = 0;
let mut p_flags: i32 = 0;
self.check_status(
unsafe {
sys::TLPMX_getSensorInfoExt(
self.session,
name.as_mut_ptr(),
snr.as_mut_ptr(),
message.as_mut_ptr(),
&mut p_type,
&mut p_stype,
&mut p_flags,
channel,
)
},
"get_sensor_info",
)?;
let to_string = |buf: &[i8]| -> String {
unsafe { CStr::from_ptr(buf.as_ptr()) }
.to_string_lossy()
.into_owned()
};
Ok((
to_string(&name),
to_string(&snr),
to_string(&message),
p_type,
p_stype,
p_flags,
))
}
/// Retrieve the number of channels available on the connected instrument.
///
/// # Returns
///
/// The number of available measurement channels.
///
/// # Errors
///
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn get_channels(&self) -> Result<u16, TlpmError> {
tracing::debug!("getting available channel count");
let mut channel_count: u16 = 0;
self.check_status(
unsafe { sys::TLPMX_getChannels(self.session, &mut channel_count) },
"get_channels",
)?;
Ok(channel_count)
}
/// Retrieve the calibration message for the sensor.
///
/// # Arguments
///
/// * `channel` - The sensor channel (typically `1`).
///
/// # Returns
///
/// The calibration message string.
///
/// # Errors
///
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn get_calibration_msg(&self, channel: u16) -> Result<String, TlpmError> {
tracing::debug!("getting calibration message on channel {}", channel);
let mut message = [0i8; sys::TLPM_BUFFER_SIZE as usize];
self.check_status(
unsafe { sys::TLPMX_getCalibrationMsg(self.session, message.as_mut_ptr(), channel) },
"get_calibration_msg",
)?;
let c_str = unsafe { CStr::from_ptr(message.as_ptr()) };
Ok(c_str.to_string_lossy().into_owned())
}
/// Check if a specific sensor is connected and operable.
///
/// # Arguments
///
/// * `channel` - The sensor channel (typically `1`).
///
/// # Returns
///
/// `true` if the sensor is connected, `false` otherwise.
///
/// # Errors
///
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn is_sensor_connected(&self, channel: u16) -> Result<bool, TlpmError> {
tracing::debug!("checking if sensor is connected on channel {}", channel);
let mut connected: sys::ViUInt16 = 0;
self.check_status(
unsafe { sys::TLPMX_isSensorConnected(self.session, &mut connected, channel) },
"is_sensor_connected",
)?;
Ok(connected != 0)
}
/// Check if the External Measurement Module (EMM) is connected.
///
/// # Returns
///
/// `true` if the EMM is connected, `false` otherwise.
///
/// # Errors
///
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn is_emm_connected(&self) -> Result<bool, TlpmError> {
tracing::debug!("checking if EMM is connected");
let mut connected: sys::ViUInt16 = 0;
self.check_status(
unsafe { sys::TLPMX_isEmmConnected(self.session, &mut connected) },
"is_emm_connected",
)?;
Ok(connected != 0)
}
/// Check if an external NTC thermistor is connected.
///
/// # Arguments
///
/// * `channel` - The sensor channel (typically `1`).
///
/// # Returns
///
/// `true` if the NTC is connected, `false` otherwise.
///
/// # Errors
///
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn is_ext_ntc_connected(&self, channel: u16) -> Result<bool, TlpmError> {
tracing::debug!(
"checking if external NTC is connected on channel {}",
channel
);
let mut connected: sys::ViUInt16 = 0;
self.check_status(
unsafe { sys::TLPMX_isExtNtcConnected(self.session, &mut connected, channel) },
"is_ext_ntc_connected",
)?;
Ok(connected != 0)
}
/// Read the state of the hardware shutter interlock.
///
/// # Returns
///
/// `true` if the interlock is closed/active, `false` otherwise.
///
/// # Errors
///
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn get_shutter_interlock(&self) -> Result<bool, TlpmError> {
tracing::debug!("getting shutter interlock state");
let mut closed: sys::ViBoolean = 0;
self.check_status(
unsafe { sys::TLPMX_getShutterInterlock(self.session, &mut closed) },
"get_shutter_interlock",
)?;
Ok(closed == VI_TRUE)
}
impl_property!(
numeric,
channel,
set_input_adapter_type,
get_input_adapter_type,
TLPMX_setInputAdapterType,
TLPMX_getInputAdapterType,
i16,
"input adapter type"
);
/// Check if the Peak Detector is currently running.
///
/// # Arguments
///
/// * `channel` - The sensor channel (typically `1`).
///
/// # Returns
///
/// `true` if the peak detector is running, `false` otherwise.
///
/// # Errors
///
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn is_peak_detector_running(&self, channel: u16) -> Result<bool, TlpmError> {
tracing::debug!(
"checking if peak detector is running on channel {}",
channel
);
let mut is_running: sys::ViUInt16 = 0;
self.check_status(
unsafe { sys::TLPMX_isPeakDetectorRunning(self.session, &mut is_running, channel) },
"is_peak_detector_running",
)?;
Ok(is_running != 0)
}
/// Retrieve standard 16-bit sensor information.
///
/// Note: `get_sensor_info` (which calls `TLPMX_getSensorInfoExt` under the hood)
/// is generally preferred as it returns the full 32-bit flag data.
///
/// # Arguments
///
/// * `channel` - The sensor channel (typically `1`).
///
/// # Returns
///
/// A tuple containing `(name, serial_number, message, type, subtype, flags)`.
///
/// # Errors
///
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn get_sensor_info_standard(
&self,
channel: u16,
) -> Result<(String, String, String, i16, i16, i16), TlpmError> {
tracing::debug!("getting standard sensor info on channel {}", channel);
let mut name = [0i8; sys::TLPM_BUFFER_SIZE as usize];
let mut snr = [0i8; sys::TLPM_BUFFER_SIZE as usize];
let mut message = [0i8; sys::TLPM_BUFFER_SIZE as usize];
let mut p_type: i16 = 0;
let mut p_stype: i16 = 0;
let mut p_flags: i16 = 0;
self.check_status(
unsafe {
sys::TLPMX_getSensorInfo(
self.session,
name.as_mut_ptr(),
snr.as_mut_ptr(),
message.as_mut_ptr(),
&mut p_type,
&mut p_stype,
&mut p_flags,
channel,
)
},
"get_sensor_info_standard",
)?;
let to_string = |buf: &[i8]| -> String {
unsafe { CStr::from_ptr(buf.as_ptr()) }
.to_string_lossy()
.into_owned()
};
Ok((
to_string(&name),
to_string(&snr),
to_string(&message),
p_type,
p_stype,
p_flags,
))
}
}