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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
mod types;
pub use types::*;
use super::NanonisClient;
use crate::error::NanonisError;
impl NanonisClient {
/// Set the mode (User Output, Monitor, Calculated signal) of the selected user output channel.
///
/// # Arguments
/// * `output_index` - Output to be used (1 to number of available outputs)
/// * `output_mode` - Output mode to set
///
/// # Errors
/// Returns `NanonisError` if:
/// - Invalid output index is provided
/// - Communication timeout or protocol error
///
/// # Examples
/// ```no_run
/// use nanonis_rs::{NanonisClient, user_out::OutputMode};
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// // Set output 1 to Monitor mode
/// client.user_out_mode_set(1, OutputMode::Monitor)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn user_out_mode_set(
&mut self,
output_index: i32,
output_mode: OutputMode,
) -> Result<(), NanonisError> {
self.quick_send(
"UserOut.ModeSet",
vec![output_index.into(), output_mode.into()],
vec!["i", "H"],
vec![],
)?;
Ok(())
}
/// Get the mode (User Output, Monitor, Calculated signal) of the selected user output channel.
///
/// # Arguments
/// * `output_index` - Output to query (1 to number of available outputs)
///
/// # Returns
/// The current output mode (UserOutput, Monitor, CalcSignal, or Override)
///
/// # Errors
/// Returns `NanonisError` if communication fails or invalid response received.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// let mode = client.user_out_mode_get(1)?;
/// println!("Output 1 mode: {:?}", mode);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn user_out_mode_get(&mut self, output_index: i32) -> Result<OutputMode, NanonisError> {
let result = self.quick_send(
"UserOut.ModeGet",
vec![output_index.into()],
vec!["i"],
vec!["H"],
)?;
match result.first() {
Some(value) => {
let mode_val = value.as_u16()?;
OutputMode::try_from(mode_val)
}
None => Err(NanonisError::Protocol(
"No output mode value returned".to_string(),
)),
}
}
/// Set the monitor channel of the selected output channel.
///
/// # Arguments
/// * `output_index` - Output to configure (1 to number of available outputs)
/// * `monitor_channel_index` - Index of the channel to monitor (0-127)
///
/// # Errors
/// Returns `NanonisError` if invalid indices provided or communication fails.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// // Set output 1 to monitor channel 5
/// client.user_out_monitor_ch_set(1, 5)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn user_out_monitor_ch_set(
&mut self,
output_index: i32,
monitor_channel_index: i32,
) -> Result<(), NanonisError> {
self.quick_send(
"UserOut.MonitorChSet",
vec![output_index.into(), monitor_channel_index.into()],
vec!["i", "i"],
vec![],
)?;
Ok(())
}
/// Get the monitor channel of the selected output channel.
///
/// # Arguments
/// * `output_index` - Output to query (1 to number of available outputs)
///
/// # Returns
/// The monitor channel index (0-127)
///
/// # Errors
/// Returns `NanonisError` if communication fails or invalid response received.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// let channel = client.user_out_monitor_ch_get(1)?;
/// println!("Output 1 is monitoring channel {}", channel);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn user_out_monitor_ch_get(&mut self, output_index: i32) -> Result<i32, NanonisError> {
let result = self.quick_send(
"UserOut.MonitorChGet",
vec![output_index.into()],
vec!["i"],
vec!["i"],
)?;
match result.first() {
Some(value) => Ok(value.as_i32()?),
None => Err(NanonisError::Protocol(
"No monitor channel value returned".to_string(),
)),
}
}
/// Set the value of the selected user output channel.
///
/// # Arguments
/// * `output_index` - Output to set (1 to number of available outputs)
/// * `output_value` - Value to apply in physical units
///
/// # Errors
/// Returns `NanonisError` if invalid output index or communication fails.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// // Set output 1 to 2.5V (or appropriate physical unit)
/// client.user_out_val_set(1, 2.5)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn user_out_val_set(
&mut self,
output_index: i32,
output_value: f32,
) -> Result<(), NanonisError> {
self.quick_send(
"UserOut.ValSet",
vec![output_index.into(), output_value.into()],
vec!["i", "f"],
vec![],
)?;
Ok(())
}
/// Set the calibration of the selected user output or monitor channel.
///
/// # Arguments
/// * `output_index` - Output to configure (1 to number of available outputs)
/// * `calibration_per_volt` - Calibration factor per volt
/// * `offset_in_physical_units` - Offset in physical units
///
/// # Errors
/// Returns `NanonisError` if invalid parameters or communication fails.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// // Set calibration for output 1: 10 units/V with 0.5 unit offset
/// client.user_out_calibr_set(1, 10.0, 0.5)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn user_out_calibr_set(
&mut self,
output_index: i32,
calibration_per_volt: f32,
offset_in_physical_units: f32,
) -> Result<(), NanonisError> {
self.quick_send(
"UserOut.CalibrSet",
vec![
output_index.into(),
calibration_per_volt.into(),
offset_in_physical_units.into(),
],
vec!["i", "f", "f"],
vec![],
)?;
Ok(())
}
/// Set the Calculated Signal name of the selected output channel.
///
/// # Arguments
/// * `output_index` - Output to configure (1 to number of available outputs)
/// * `calculated_signal_name` - Name of the calculated signal
///
/// # Errors
/// Returns `NanonisError` if invalid output index or communication fails.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// client.user_out_calc_signal_name_set(1, "MyCalcSignal".to_string())?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn user_out_calc_signal_name_set(
&mut self,
output_index: i32,
calculated_signal_name: String,
) -> Result<(), NanonisError> {
self.quick_send(
"UserOut.CalcSignalNameSet",
vec![output_index.into(), calculated_signal_name.into()],
vec!["i", "+*c"],
vec![],
)?;
Ok(())
}
/// Get the Calculated Signal name of the selected output channel.
///
/// # Arguments
/// * `output_index` - Output to query (1 to number of available outputs)
///
/// # Returns
/// The calculated signal name
///
/// # Errors
/// Returns `NanonisError` if communication fails or invalid response received.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// let name = client.user_out_calc_signal_name_get(1)?;
/// println!("Calculated signal name: {}", name);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn user_out_calc_signal_name_get(
&mut self,
output_index: i32,
) -> Result<String, NanonisError> {
let result = self.quick_send(
"UserOut.CalcSignalNameGet",
vec![output_index.into()],
vec!["i"],
vec!["i", "*-c"],
)?;
if result.len() >= 2 {
Ok(result[1].as_string()?.to_string())
} else {
Err(NanonisError::Protocol(
"Invalid calc signal name response".to_string(),
))
}
}
/// Set the configuration of the Calculated Signal for the selected output channel.
///
/// The configuration is a math operation between 2 signals, or the logarithmic value of one signal.
///
/// # Arguments
/// * `output_index` - Output to configure (1 to number of available outputs)
/// * `config` - Calculated signal configuration
///
/// # Errors
/// Returns `NanonisError` if invalid parameters or communication fails.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::{NanonisClient, user_out::{CalcSignalConfig, CalcOperation}};
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// // Configure output 1 to add signals 5 and 10
/// let config = CalcSignalConfig::new(5, CalcOperation::Add, 10);
/// client.user_out_calc_signal_config_set(1, config)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn user_out_calc_signal_config_set(
&mut self,
output_index: i32,
config: CalcSignalConfig,
) -> Result<(), NanonisError> {
self.quick_send(
"UserOut.CalcSignalConfigSet",
vec![
output_index.into(),
config.signal_1.into(),
config.operation.into(),
config.signal_2.into(),
],
vec!["i", "H", "H", "H"],
vec![],
)?;
Ok(())
}
/// Get the configuration of the Calculated Signal for the selected output channel.
///
/// # Arguments
/// * `output_index` - Output to query (1 to number of available outputs)
///
/// # Returns
/// The calculated signal configuration
///
/// # Errors
/// Returns `NanonisError` if communication fails or invalid response received.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// let config = client.user_out_calc_signal_config_get(1)?;
/// println!("Signal 1: {}, Operation: {:?}, Signal 2: {}",
/// config.signal_1, config.operation, config.signal_2);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn user_out_calc_signal_config_get(
&mut self,
output_index: i32,
) -> Result<CalcSignalConfig, NanonisError> {
let result = self.quick_send(
"UserOut.CalcSignalConfigGet",
vec![output_index.into()],
vec!["i"],
vec!["H", "H", "H"],
)?;
if result.len() >= 3 {
let signal_1 = result[0].as_u16()?;
let operation = CalcOperation::try_from(result[1].as_u16()?)?;
let signal_2 = result[2].as_u16()?;
Ok(CalcSignalConfig::new(signal_1, operation, signal_2))
} else {
Err(NanonisError::Protocol(
"Invalid calc signal config response".to_string(),
))
}
}
/// Set the physical limits (in calibrated units) of the selected output channel.
///
/// # Arguments
/// * `output_index` - Output to configure (1 to number of available outputs)
/// * `upper_limit` - Upper physical limit of the user output
/// * `lower_limit` - Lower physical limit of the user output
/// * `raw_limits` - Whether to set physical limits (false) or raw limits (true)
///
/// # Errors
/// Returns `NanonisError` if invalid parameters or communication fails.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// // Set physical limits: -10V to +10V
/// client.user_out_limits_set(1, 10.0, -10.0, false)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn user_out_limits_set(
&mut self,
output_index: i32,
upper_limit: f32,
lower_limit: f32,
raw_limits: bool,
) -> Result<(), NanonisError> {
let raw_flag = if raw_limits { 1u32 } else { 0u32 };
self.quick_send(
"UserOut.LimitsSet",
vec![
output_index.into(),
upper_limit.into(),
lower_limit.into(),
raw_flag.into(),
],
vec!["i", "f", "f", "I"],
vec![],
)?;
Ok(())
}
/// Get the physical limits (in calibrated units) of the selected output channel.
///
/// # Arguments
/// * `output_index` - Output to query (1 to number of available outputs)
/// * `raw_limits` - Whether to get physical limits (false) or raw limits (true)
///
/// # Returns
/// A tuple containing (upper_limit, lower_limit)
///
/// # Errors
/// Returns `NanonisError` if communication fails or invalid response received.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// let (upper, lower) = client.user_out_limits_get(1, false)?;
/// println!("Limits: {} to {}", lower, upper);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn user_out_limits_get(
&mut self,
output_index: i32,
raw_limits: bool,
) -> Result<(f32, f32), NanonisError> {
let raw_flag = if raw_limits { 1u32 } else { 0u32 };
let result = self.quick_send(
"UserOut.LimitsGet",
vec![output_index.into(), raw_flag.into()],
vec!["i", "I"],
vec!["f", "f"],
)?;
if result.len() >= 2 {
let upper_limit = result[0].as_f32()?;
let lower_limit = result[1].as_f32()?;
Ok((upper_limit, lower_limit))
} else {
Err(NanonisError::Protocol(
"Invalid limits response".to_string(),
))
}
}
}