linux_gpib_rs/lowlevel/
multidevice.rs

1#![allow(non_snake_case)]
2use crate::error::{GpibError, IbError};
3use crate::lowlevel::utility::Addr4882;
4#[cfg(feature = "linuxgpib")]
5use crate::lowlevel::utility::{ThreadIbcnt, ThreadIbcntl};
6#[cfg(feature = "nigpib")]
7use crate::lowlevel::utility::Ibcnt;
8
9use crate::status::IbStatus;
10use crate::types::{IbSendEOI, PrimaryAddress, SecondaryAddress};
11use linux_gpib_sys::Addr4882_t;
12use std::default::Default;
13use std::os::raw::{c_int, c_uint, c_short, c_void};
14
15/// find devices
16///
17/// FindLstn() will check the primary addresses in the padList array for devices. The GPIB addresses of all devices found will be stored in the resultList array, and ibcnt will be set to the number of devices found. The maxNumResults parameter limits the maximum number of results that will be returned, and is usually set to the number of elements in the resultList array. If more than maxNumResults devices are found, an ETAB error is returned in iberr. The padList should consist of primary addresses only, with no secondary addresses (all possible secondary addresses will be checked as necessary).
18///
19/// Your GPIB board must have the capability to monitor the NDAC bus line in order to use this function (see iblines).
20///
21/// This function has the additional effect of addressing the board as talker for the duration of the Find Listeners protocol, which is beyond what IEEE 488.2 specifies. This is done because some boards cannot reliably read the state of the NDAC bus line unless they are the talker. Being the talker causes the board's gpib transceiver to configure NDAC as an input, so its state can be reliably read from the bus through the transceiver.
22///
23/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-findlstn.html)
24///
25pub fn FindLstn(board_desc: c_int, padList: Vec<Addr4882>) -> Result<Vec<Addr4882>, GpibError> {
26    let mut result: Vec<Addr4882_t> = Vec::new();
27    result.resize_with(31, || linux_gpib_sys::NOADDR);
28    let mut padList = padList
29        .into_iter()
30        .map(|a| a.addr)
31        .collect::<Vec<Addr4882_t>>();
32    padList.push(linux_gpib_sys::NOADDR);
33    log::debug!("FindLstn({}, {:?})", board_desc, padList);
34    unsafe {
35        linux_gpib_sys::FindLstn(
36            board_desc,
37            padList.as_ptr(),
38            result.as_mut_ptr(),
39            padList.len().try_into()?,
40        )
41    };
42
43    #[cfg(feature = "linuxgpib")]
44    let status = IbStatus::current_thread_local_status();
45    #[cfg(feature = "nigpib")]
46    let status = unsafe { IbStatus::current_global_status() };
47
48    if status.err {
49        #[cfg(feature = "linuxgpib")]
50        let error = IbError::current_thread_local_error()?;
51        #[cfg(feature = "nigpib")]
52        let error = unsafe { IbError::current_global_error() }?;
53
54        match error {
55            IbError::EARG => {
56                #[cfg(feature = "linuxgpib")]
57                log::error!(
58                    "Invalid primary address at index {} in padlist",
59                    ThreadIbcnt(),
60                );
61                #[cfg(feature = "nigpib")]
62                log::error!(
63                    "Invalid primary address at index {} in padlist",
64                    Ibcnt(),
65                );
66            }
67            IbError::EBUS => {
68                log::error!("No devices are connected to the GPIB.");
69            }
70            IbError::ETAB => {
71                log::error!("The number of devices found on the GPIB exceed limit.");
72            }
73            _ => {}
74        }
75        log::debug!("-> {:?}", error);
76        Err(GpibError::DriverError(status, error))
77    } else {
78        #[cfg(feature = "linuxgpib")]
79        let n_values: usize = ThreadIbcntl().try_into()?;
80        #[cfg(feature = "nigpib")]
81        let n_values: usize = Ibcnt().try_into()?;
82
83        result.truncate(n_values);
84        log::debug!("-> {:?}", result);
85        Ok(result.into_iter().map(|a| Addr4882 { addr: a }).collect())
86    }
87}
88
89/// Find all listeners on board.
90pub fn FindAllLstn(board_desc: c_int) -> Result<Vec<Addr4882>, GpibError> {
91    let padList = (1..31)
92        .into_iter()
93        .map(|pad| Addr4882::new(PrimaryAddress::new(pad)?, SecondaryAddress::default()))
94        .collect::<Result<Vec<Addr4882>, GpibError>>()?;
95    FindLstn(board_desc, padList)
96}
97
98/// clear a device
99///
100/// DevClear() causes the interface board specified by board_desc to send the clear command to the GPIB addresses specified by address. The results of the serial polls are stored into resultList. If you wish to clear multiple devices simultaneously, use DevClearList()
101pub fn DevClear(board: c_int, address: Addr4882) -> Result<(), GpibError> {
102    log::debug!("DevClear({}, {})", board, address);
103    unsafe {
104        linux_gpib_sys::DevClear(board, address.addr);
105    }
106    #[cfg(feature = "linuxgpib")]
107    let status = IbStatus::current_thread_local_status();
108    #[cfg(feature = "nigpib")]
109    let status = unsafe { IbStatus::current_global_status() };
110    if status.err {
111        log::debug!("-> {:?}", status);
112        Err(GpibError::DriverError(
113            status,
114            #[cfg(feature = "linuxgpib")]
115            IbError::current_thread_local_error()?,
116            #[cfg(feature = "nigpib")]
117            unsafe { IbError::current_global_error() }?,
118        ))
119    } else {
120        log::debug!("-> {:?}", status);
121        Ok(())
122    }
123}
124
125/// clear multiple devices
126///
127/// DevClear() causes the interface board specified by board_desc to send the clear command simultaneously to all the GPIB addresses specified by the addressList array. If addressList is empty or NULL, then the clear command is sent to all devices on the bus. If you only wish to clear a single device, DevClear() or ibclr() may be slightly more convenient.
128pub fn DevClearList(board: c_int, addresses: &Vec<Addr4882>) -> Result<(), GpibError> {
129    let mut instruments = addresses
130        .iter()
131        .map(|a| a.addr)
132        .collect::<Vec<Addr4882_t>>();
133    instruments.push(linux_gpib_sys::NOADDR);
134    log::debug!("DevClearList({:?}, {:?})", board, addresses);
135    unsafe {
136        linux_gpib_sys::DevClearList(board, instruments.as_ptr());
137    }
138    #[cfg(feature = "linuxgpib")]
139    let status = IbStatus::current_thread_local_status();
140    #[cfg(feature = "nigpib")]
141    let status = unsafe { IbStatus::current_global_status() };
142    log::debug!("-> {:?}", status);
143    if status.err {
144        Err(GpibError::DriverError(
145            status,
146            #[cfg(feature = "linuxgpib")]
147            IbError::current_thread_local_error()?,
148            #[cfg(feature = "nigpib")]
149            unsafe { IbError::current_global_error() }?,
150        ))
151    } else {
152        Ok(())
153    }
154}
155
156/// put devices into local mode.
157///
158/// 	EnableLocal() addresses all of the devices in the addressList array as listeners then sends the GTL (go to local) command byte, causing them to enter local mode. This requires that the board is the controller-in-charge. Note that while the REN (remote enable) bus line is asserted, the devices will return to remote mode the next time they are addressed.
159///
160///If addressList is empty or NULL, then the REN line is unasserted and all devices enter local mode. The board must be system controller to change the state of the REN line.
161///
162/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-enablelocal.html)
163pub fn EnableLocal(board: c_int, addresses: &Vec<Addr4882>) -> Result<(), GpibError> {
164    let mut instruments = addresses
165        .iter()
166        .map(|a| a.addr)
167        .collect::<Vec<Addr4882_t>>();
168    instruments.push(linux_gpib_sys::NOADDR);
169    unsafe {
170        linux_gpib_sys::EnableLocal(board, instruments.as_ptr());
171    }
172    #[cfg(feature = "linuxgpib")]
173    let status = IbStatus::current_thread_local_status();
174    #[cfg(feature = "nigpib")]
175    let status = unsafe { IbStatus::current_global_status() };
176    if status.err {
177        Err(GpibError::DriverError(
178            status,
179            #[cfg(feature = "linuxgpib")]
180            IbError::current_thread_local_error()?,
181            #[cfg(feature = "nigpib")]
182            unsafe { IbError::current_global_error() }?,
183        ))
184    } else {
185        Ok(())
186    }
187}
188
189/// put devices into remote mode.
190///
191/// EnableRemote() asserts the REN (remote enable) line, and addresses all of the devices in the addressList array as listeners (causing them to enter remote mode). The board must be system controller.
192///
193/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-enableremote.html)
194pub fn EnableRemote(board: c_int, addresses: &Vec<Addr4882>) -> Result<(), GpibError> {
195    let mut instruments = addresses
196        .iter()
197        .map(|a| a.addr)
198        .collect::<Vec<Addr4882_t>>();
199    instruments.push(linux_gpib_sys::NOADDR);
200    unsafe {
201        linux_gpib_sys::EnableRemote(board, instruments.as_ptr());
202    }
203    #[cfg(feature = "linuxgpib")]
204    let status = IbStatus::current_thread_local_status();
205    #[cfg(feature = "nigpib")]
206    let status = unsafe { IbStatus::current_global_status() };
207    if status.err {
208        Err(GpibError::DriverError(
209            status,
210            #[cfg(feature = "linuxgpib")]
211            IbError::current_thread_local_error()?,
212            #[cfg(feature = "nigpib")]
213            unsafe { IbError::current_global_error() }?,
214        ))
215    } else {
216        Ok(())
217    }
218}
219
220/// find device requesting service and read its status byte
221///
222/// FindRQS will serial poll the GPIB addresses specified in the addressList array until it finds a device requesting service. The status byte of the device requesting service and its address are returned. If no device requesting service is found, an ETAB error is returned.
223///
224/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-findrqs.html)
225pub fn FindRQS(board: c_int, addresses: &Vec<Addr4882>) -> Result<(Addr4882, c_short), GpibError> {
226    let mut instruments = addresses
227        .iter()
228        .map(|a| a.addr)
229        .collect::<Vec<Addr4882_t>>();
230    instruments.push(linux_gpib_sys::NOADDR);
231    let mut status_byte: c_short = 0;
232    unsafe {
233        linux_gpib_sys::FindRQS(board, instruments.as_ptr(), &mut status_byte);
234    }
235    #[cfg(feature = "linuxgpib")]
236    let status = IbStatus::current_thread_local_status();
237    #[cfg(feature = "nigpib")]
238    let status = unsafe { IbStatus::current_global_status() };
239    if status.err {
240        Err(GpibError::DriverError(
241            status,
242            #[cfg(feature = "linuxgpib")]
243            IbError::current_thread_local_error()?,
244            #[cfg(feature = "nigpib")]
245            unsafe { IbError::current_global_error() }?,
246        ))
247    } else {
248        #[cfg(feature = "linuxgpib")]
249        let index: usize = ThreadIbcnt().try_into()?;
250        #[cfg(feature = "nigpib")]
251        let index: usize = Ibcnt().try_into()?;
252        if index >= addresses.len() {
253            Err(GpibError::ValueError(
254                "index stored in Ibcnt is larger than addresses array length".to_owned(),
255            ))
256        } else {
257            Ok((addresses[index], status_byte))
258        }
259    }
260}
261
262/// make device controller-in-charge
263///
264/// PassControl() causes the board specified by board_desc to pass control to the device specified by address. On success, the device becomes the new controller-in-charge.
265///
266/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-passcontrol.html)
267pub fn PassControl(board: c_int, address: Addr4882) -> Result<(), GpibError> {
268    unsafe {
269        linux_gpib_sys::PassControl(board, address.addr);
270    }
271    #[cfg(feature = "linuxgpib")]
272    let status = IbStatus::current_thread_local_status();
273    #[cfg(feature = "nigpib")]
274    let status = unsafe { IbStatus::current_global_status() };
275    if status.err {
276        Err(GpibError::DriverError(
277            status,
278            #[cfg(feature = "linuxgpib")]
279            IbError::current_thread_local_error()?,
280            #[cfg(feature = "nigpib")]
281            unsafe { IbError::current_global_error() }?,
282        ))
283    } else {
284        Ok(())
285    }
286}
287
288/// parallel poll devices
289///
290/// PPoll() is similar to the 'traditional' API function ibrpp(). It causes the interface board to perform a parallel poll, and stores the parallel poll byte in the location specified by result. Bits 0 to 7 of the parallel poll byte correspond to the dio lines 1 to 8, with a 1 indicating the corresponding dio line is asserted. The devices on the bus you wish to poll should be configured beforehand with PPollConfig(). The board must be controller-in-charge to perform a parallel poll.
291///
292/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ppoll.html)
293pub fn PPoll(board: c_int) -> Result<c_short, GpibError> {
294    let mut result: c_short = 0;
295    unsafe {
296        linux_gpib_sys::PPoll(board, &mut result);
297    }
298    #[cfg(feature = "linuxgpib")]
299    let status = IbStatus::current_thread_local_status();
300    #[cfg(feature = "nigpib")]
301    let status = unsafe { IbStatus::current_global_status() };
302    if status.err {
303        Err(GpibError::DriverError(
304            status,
305            #[cfg(feature = "linuxgpib")]
306            IbError::current_thread_local_error()?,
307            #[cfg(feature = "nigpib")]
308            unsafe { IbError::current_global_error() }?,
309        ))
310    } else {
311        Ok(result)
312    }
313}
314
315/// configure a device's parallel poll response
316///
317/// PPollConfig() configures the device specified by address to respond to parallel polls. The dio_line (valid values are 1 through 8) specifies which dio line the device being configured should use to send back its parallel poll response. The line_sense argument specifies the polarity of the response. If line_sense is nonzero, then the specified dio line will be asserted to indicate that the 'individual status bit' (or 'ist') is 1. If sense is zero, then the specified dio line will be asserted when ist is zero.
318///
319/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ppollconfig.html)
320pub fn PPollConfig(
321    board: c_int,
322    address: Addr4882,
323    dio_line: c_int,
324    line_sense: c_int,
325) -> Result<(), GpibError> {
326    unsafe {
327        linux_gpib_sys::PPollConfig(board, address.addr, dio_line, line_sense);
328    }
329    #[cfg(feature = "linuxgpib")]
330    let status = IbStatus::current_thread_local_status();
331    #[cfg(feature = "nigpib")]
332    let status = unsafe { IbStatus::current_global_status() };
333    if status.err {
334        Err(GpibError::DriverError(
335            status,
336            #[cfg(feature = "linuxgpib")]
337            IbError::current_thread_local_error()?,
338            #[cfg(feature = "nigpib")]
339            unsafe { IbError::current_global_error() }?,
340        ))
341    } else {
342        Ok(())
343    }
344}
345
346/// disable devices' parallel poll response
347///
348/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ppollunconfig.html)
349pub fn PPollUnconfig(board: c_int, addresses: &Vec<Addr4882>) -> Result<(), GpibError> {
350    let mut instruments = addresses
351        .iter()
352        .map(|a| a.addr)
353        .collect::<Vec<Addr4882_t>>();
354    instruments.push(linux_gpib_sys::NOADDR);
355    unsafe {
356        linux_gpib_sys::PPollUnconfig(board, instruments.as_ptr());
357    }
358    #[cfg(feature = "linuxgpib")]
359    let status = IbStatus::current_thread_local_status();
360    #[cfg(feature = "nigpib")]
361    let status = unsafe { IbStatus::current_global_status() };
362    if status.err {
363        Err(GpibError::DriverError(
364            status,
365            #[cfg(feature = "linuxgpib")]
366            IbError::current_thread_local_error()?,
367            #[cfg(feature = "nigpib")]
368            unsafe { IbError::current_global_error() }?,
369        ))
370    } else {
371        Ok(())
372    }
373}
374
375/// read data
376///
377/// 	RcvRespMsg() reads data from the bus. A device must have already been addressed as talker (and the board as listener) before calling this function. Addressing may be accomplished with the ReceiveSetup() function.
378///
379/// Up to count bytes are read into the array specified by buffer. The termination argument specifies the 8-bit end-of-string character (which must be a value from 0 to 255) whose reception will terminate a read. termination can also be set to the 'STOPend' constant, in which case no end-of-string character will be used. Assertion of the EOI line will always end a read.
380///
381/// You may find it simpler to use the slightly higher level function Receive(), since it does not require addressing and reading of data to be performed separately.
382///
383/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-rcvrespmsg.html)
384pub fn RcvRespMsg(board: c_int, buffer: &mut [u8], termination: c_int) -> Result<(), GpibError> {
385    unsafe {
386        linux_gpib_sys::RcvRespMsg(
387            board,
388            buffer.as_mut_ptr() as *mut c_void,
389            buffer.len().try_into()?,
390            termination,
391        );
392    }
393    #[cfg(feature = "linuxgpib")]
394    let status = IbStatus::current_thread_local_status();
395    #[cfg(feature = "nigpib")]
396    let status = unsafe { IbStatus::current_global_status() };
397    if status.err {
398        Err(GpibError::DriverError(
399            status,
400            #[cfg(feature = "linuxgpib")]
401            IbError::current_thread_local_error()?,
402            #[cfg(feature = "nigpib")]
403            unsafe { IbError::current_global_error() }?,
404        ))
405    } else {
406        Ok(())
407    }
408}
409
410/// serial poll a device
411///
412/// ReadStatusByte() causes the board specified by the board descriptor board_desc to serial poll the GPIB address specified by address. The status byte is stored at the location specified by the result pointer. If you wish to serial poll multiple devices, it may be slightly more efficient to use AllSPoll(). Serial polls may also be conducted with the 'traditional API' function ibrsp().
413///
414/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-readstatusbyte.html)
415pub fn ReadStatusByte(board: c_int, address: Addr4882) -> Result<c_short, GpibError> {
416    let mut result: c_short = 0;
417    unsafe {
418        linux_gpib_sys::ReadStatusByte(board, address.addr, &mut result);
419    }
420    #[cfg(feature = "linuxgpib")]
421    let status = IbStatus::current_thread_local_status();
422    #[cfg(feature = "nigpib")]
423    let status = unsafe { IbStatus::current_global_status() };
424    if status.err {
425        Err(GpibError::DriverError(
426            status,
427            #[cfg(feature = "linuxgpib")]
428            IbError::current_thread_local_error()?,
429            #[cfg(feature = "nigpib")]
430            unsafe { IbError::current_global_error() }?,
431        ))
432    } else {
433        Ok(result)
434    }
435}
436
437/// perform receive addressing and read data
438///
439/// Receive() performs the necessary addressing, then reads data from the device specified by address. It is equivalent to a ReceiveSetup() call followed by a RcvRespMsg() call.
440///
441/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-receive.html)
442pub fn Receive(
443    board: c_int,
444    address: Addr4882,
445    buffer: &mut [u8],
446    termination: c_int,
447) -> Result<(IbStatus, usize), GpibError> {
448    log::debug!("Receive({:?}, {:?})", board, address);
449    unsafe {
450        linux_gpib_sys::Receive(
451            board,
452            address.addr,
453            buffer.as_mut_ptr() as *mut c_void,
454            buffer.len().try_into()?,
455            termination,
456        );
457    }
458    #[cfg(feature = "linuxgpib")]
459    let status = IbStatus::current_thread_local_status();
460    #[cfg(feature = "nigpib")]
461    let status = unsafe { IbStatus::current_global_status() };
462    if status.err {
463        log::debug!("-> {:?}", status);
464        Err(GpibError::DriverError(
465            status,
466            #[cfg(feature = "linuxgpib")]
467            IbError::current_thread_local_error()?,
468            #[cfg(feature = "nigpib")]
469            unsafe { IbError::current_global_error() }?,
470        ))
471    } else {
472        #[cfg(feature = "linuxgpib")]
473        let n_read = ThreadIbcntl().try_into()?;
474        #[cfg(feature = "nigpib")]
475        let n_read = Ibcnt().try_into()?;
476        log::debug!(
477            "Receive({:?}, {:?}) -> Read {} bytes",
478            board,
479            address,
480            n_read
481        );
482        Ok((status, n_read))
483    }
484}
485
486/// perform receive addressing
487///
488/// 	ReceiveSetup() addresses the device specified by address as talker, and addresses the interface board as listener. A subsequent RcvRespMsg() call will read data from the device.
489///
490///You may find it simpler to use the slightly higher level function Receive(), since it does not require addressing and reading of data to be performed separately.
491///
492/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-receivesetup.html)
493pub fn ReceiveSetup(board: c_int, address: Addr4882) -> Result<(), GpibError> {
494    unsafe {
495        linux_gpib_sys::ReceiveSetup(board, address.addr);
496    }
497    #[cfg(feature = "linuxgpib")]
498    let status = IbStatus::current_thread_local_status();
499    #[cfg(feature = "nigpib")]
500    let status = unsafe { IbStatus::current_global_status() };
501    if status.err {
502        Err(GpibError::DriverError(
503            status,
504            #[cfg(feature = "linuxgpib")]
505            IbError::current_thread_local_error()?,
506            #[cfg(feature = "nigpib")]
507            unsafe { IbError::current_global_error() }?,
508        ))
509    } else {
510        Ok(())
511    }
512}
513
514/// reset system
515///
516/// ResetSys() has the following effects:
517///
518/// - The remote enable bus line is asserted.
519/// - An interface clear is performed (the interface clear bus line is asserted for at least 100 microseconds).
520/// - The device clear command is sent to all the devices on the bus.
521/// - The *RST message is sent to every device specified in the addressList.
522///
523/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-resetsys.html)
524pub fn ResetSys(board: c_int, addresses: &Vec<Addr4882>) -> Result<(), GpibError> {
525    let mut instruments = addresses
526        .iter()
527        .map(|a| a.addr)
528        .collect::<Vec<Addr4882_t>>();
529    instruments.push(linux_gpib_sys::NOADDR);
530    unsafe {
531        linux_gpib_sys::ResetSys(board, instruments.as_ptr());
532    }
533    #[cfg(feature = "linuxgpib")]
534    let status = IbStatus::current_thread_local_status();
535    #[cfg(feature = "nigpib")]
536    let status = unsafe { IbStatus::current_global_status() };
537    if status.err {
538        Err(GpibError::DriverError(
539            status,
540            #[cfg(feature = "linuxgpib")]
541            IbError::current_thread_local_error()?,
542            #[cfg(feature = "nigpib")]
543            unsafe { IbError::current_global_error() }?,
544        ))
545    } else {
546        Ok(())
547    }
548}
549
550/// perform send addressing and write data
551///
552/// Send() addresses the device specified by address as listener, then writes data onto the bus. It is equivalent to a SendList() except it only uses a single GPIB address to specify the listener instead of allowing an array of listeners.
553///
554/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-send.html)
555pub fn Send(
556    board: c_int,
557    address: Addr4882,
558    buffer: &[u8],
559    eot_mode: IbSendEOI,
560) -> Result<(), GpibError> {
561    log::debug!("Send({:?}, {:?}, {:?})", board, address, buffer);
562    unsafe {
563        linux_gpib_sys::Send(
564            board,
565            address.addr,
566            buffer.as_ptr() as *const c_void,
567            buffer.len().try_into()?,
568            eot_mode.as_eot(),
569        );
570    }
571    #[cfg(feature = "linuxgpib")]
572    let status = IbStatus::current_thread_local_status();
573    #[cfg(feature = "nigpib")]
574    let status = unsafe { IbStatus::current_global_status() };
575    log::debug!(
576        "Send({:?}, {:?}, {:?}) -> {:?}",
577        board,
578        address,
579        buffer,
580        status
581    );
582    if status.err {
583        Err(GpibError::DriverError(
584            status,
585            #[cfg(feature = "linuxgpib")]
586            IbError::current_thread_local_error()?,
587            #[cfg(feature = "nigpib")]
588            unsafe { IbError::current_global_error() }?,
589        ))
590    } else {
591        Ok(())
592    }
593}
594
595/// perform interface clear
596///
597/// SendIFC() resets the GPIB bus by asserting the 'interface clear' (IFC) bus line for a duration of at least 100 microseconds. The board specified by board_desc must be the system controller in order to assert IFC. The interface clear causes all devices to untalk and unlisten, puts them into serial poll disabled state (don't worry, you will still be able to conduct serial polls), and the board becomes controller-in-charge.
598pub fn SendIFC(board: c_int) -> Result<(), GpibError> {
599    log::debug!("SendIFC({})", board);
600    unsafe {
601        linux_gpib_sys::SendIFC(board);
602    }
603    #[cfg(feature = "linuxgpib")]
604    let status = IbStatus::current_thread_local_status();
605    #[cfg(feature = "nigpib")]
606    let status = unsafe { IbStatus::current_global_status() };
607    log::debug!("endIFC({}) -> {:?}", board, status);
608    if status.err {
609        Err(GpibError::DriverError(
610            status,
611            #[cfg(feature = "linuxgpib")]
612            IbError::current_thread_local_error()?,
613            #[cfg(feature = "nigpib")]
614            unsafe { IbError::current_global_error() }?,
615        ))
616    } else {
617        Ok(())
618    }
619}
620
621/// write data to multiple devices
622///
623/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-sendlist.html)
624pub fn SendList(
625    board: c_int,
626    addresses: &Vec<Addr4882>,
627    buffer: &[u8],
628    eot_mode: IbSendEOI,
629) -> Result<(), GpibError> {
630    log::debug!("SendList({:?}, {:?}, {:?})", board, addresses, buffer);
631    let mut instruments = addresses
632        .iter()
633        .map(|a| a.addr)
634        .collect::<Vec<Addr4882_t>>();
635    instruments.push(linux_gpib_sys::NOADDR);
636    unsafe {
637        linux_gpib_sys::SendList(
638            board,
639            instruments.as_ptr(),
640            buffer.as_ptr() as *const c_void,
641            buffer.len().try_into()?,
642            eot_mode.as_eot(),
643        );
644    }
645    #[cfg(feature = "linuxgpib")]
646    let status = IbStatus::current_thread_local_status();
647    #[cfg(feature = "nigpib")]
648    let status = unsafe { IbStatus::current_global_status() };
649    log::debug!(
650        "SendList({:?}, {:?}, {:?}) -> {:?}",
651        board,
652        addresses,
653        buffer,
654        status
655    );
656    if status.err {
657        Err(GpibError::DriverError(
658            status,
659            #[cfg(feature = "linuxgpib")]
660            IbError::current_thread_local_error()?,
661            #[cfg(feature = "nigpib")]
662            unsafe { IbError::current_global_error() }?,
663        ))
664    } else {
665        Ok(())
666    }
667}
668
669/// put devices into local lockout mode
670///
671/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-sendllo.html)
672pub fn SendLLO(board: c_int) -> Result<(), GpibError> {
673    unsafe {
674        linux_gpib_sys::SendLLO(board);
675    }
676    #[cfg(feature = "linuxgpib")]
677    let status = IbStatus::current_thread_local_status();
678    #[cfg(feature = "nigpib")]
679    let status = unsafe { IbStatus::current_global_status() };
680    if status.err {
681        Err(GpibError::DriverError(
682            status,
683            #[cfg(feature = "linuxgpib")]
684            IbError::current_thread_local_error()?,
685            #[cfg(feature = "nigpib")]
686            unsafe { IbError::current_global_error() }?,
687        ))
688    } else {
689        Ok(())
690    }
691}
692
693/// put devices into remote with lockout state
694///
695/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-setrwls.html)
696pub fn SetRWLS(board: c_int, addresses: &Vec<Addr4882>) -> Result<(), GpibError> {
697    let mut instruments = addresses
698        .iter()
699        .map(|a| a.addr)
700        .collect::<Vec<Addr4882_t>>();
701    instruments.push(linux_gpib_sys::NOADDR);
702    unsafe {
703        linux_gpib_sys::SetRWLS(board, instruments.as_ptr());
704    }
705    #[cfg(feature = "linuxgpib")]
706    let status = IbStatus::current_thread_local_status();
707    #[cfg(feature = "nigpib")]
708    let status = unsafe { IbStatus::current_global_status() };
709    if status.err {
710        Err(GpibError::DriverError(
711            status,
712            #[cfg(feature = "linuxgpib")]
713            IbError::current_thread_local_error()?,
714            #[cfg(feature = "nigpib")]
715            unsafe { IbError::current_global_error() }?,
716        ))
717    } else {
718        Ok(())
719    }
720}
721
722///  query state of SRQ bus line
723///
724/// Returns true if the SRQ line is asserted, false if it is not asserted.
725///
726/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-testsrq.html)
727pub fn TestSRQ(board: c_int) -> Result<bool, GpibError> {
728    let mut result: c_short = 10;
729    unsafe {
730        linux_gpib_sys::TestSRQ(board, &mut result);
731    }
732    #[cfg(feature = "linuxgpib")]
733    let status = IbStatus::current_thread_local_status();
734    #[cfg(feature = "nigpib")]
735    let status = unsafe { IbStatus::current_global_status() };
736    if status.err {
737        Err(GpibError::DriverError(
738            status,
739            #[cfg(feature = "linuxgpib")]
740            IbError::current_thread_local_error()?,
741            #[cfg(feature = "nigpib")]
742            unsafe { IbError::current_global_error() }?,
743        ))
744    } else {
745        match result {
746            0 => Ok(false),
747            1 => Ok(true),
748            other => Err(GpibError::ValueError(format!("Unexpected value {}", other))),
749        }
750    }
751}
752
753/// perform self-test queries on devices
754///
755/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-testsys.html)
756pub fn TestSys(board: c_int, addresses: &Vec<Addr4882>) -> Result<Vec<c_short>, GpibError> {
757    let mut instruments = addresses
758        .iter()
759        .map(|a| a.addr)
760        .collect::<Vec<Addr4882_t>>();
761    instruments.push(linux_gpib_sys::NOADDR);
762    let mut results: Vec<c_short> = Vec::with_capacity(addresses.len());
763    results.resize(addresses.len(), 0);
764    unsafe {
765        linux_gpib_sys::TestSys(board, instruments.as_ptr(), results.as_mut_ptr());
766    }
767    #[cfg(feature = "linuxgpib")]
768    let status = IbStatus::current_thread_local_status();
769    #[cfg(feature = "nigpib")]
770    let status = unsafe { IbStatus::current_global_status() };
771    if status.err {
772        Err(GpibError::DriverError(
773            status,
774            #[cfg(feature = "linuxgpib")]
775            IbError::current_thread_local_error()?,
776            #[cfg(feature = "nigpib")]
777            unsafe { IbError::current_global_error() }?,
778        ))
779    } else {
780        Ok(results)
781    }
782}
783
784/// trigger a device
785///
786/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-trigger.html)
787pub fn Trigger(board: c_int, address: Addr4882) -> Result<(), GpibError> {
788    unsafe {
789        linux_gpib_sys::Trigger(board, address.addr);
790    }
791    #[cfg(feature = "linuxgpib")]
792    let status = IbStatus::current_thread_local_status();
793    #[cfg(feature = "nigpib")]
794    let status = unsafe { IbStatus::current_global_status() };
795    if status.err {
796        Err(GpibError::DriverError(
797            status,
798            #[cfg(feature = "linuxgpib")]
799            IbError::current_thread_local_error()?,
800            #[cfg(feature = "nigpib")]
801            unsafe { IbError::current_global_error() }?,
802        ))
803    } else {
804        Ok(())
805    }
806}
807
808/// trigger multiple devices
809///
810/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-triggerlist.html)
811pub fn TriggerList(board: c_int, addresses: &Vec<Addr4882>) -> Result<(), GpibError> {
812    let mut instruments = addresses
813        .iter()
814        .map(|a| a.addr)
815        .collect::<Vec<Addr4882_t>>();
816    instruments.push(linux_gpib_sys::NOADDR);
817    unsafe {
818        linux_gpib_sys::TriggerList(board, instruments.as_ptr());
819    }
820    #[cfg(feature = "linuxgpib")]
821    let status = IbStatus::current_thread_local_status();
822    #[cfg(feature = "nigpib")]
823    let status = unsafe { IbStatus::current_global_status() };
824    if status.err {
825        Err(GpibError::DriverError(
826            status,
827            #[cfg(feature = "linuxgpib")]
828            IbError::current_thread_local_error()?,
829            #[cfg(feature = "nigpib")]
830            unsafe { IbError::current_global_error() }?,
831        ))
832    } else {
833        Ok(())
834    }
835}
836
837#[cfg(feature = "async-tokio")]
838/// sleep until the SRQ bus line is asserted
839///
840/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-waitsrq.html)
841pub async fn WaitSRQ(board: c_int) -> Result<c_short, GpibError> {
842    tokio::task::spawn_blocking(move || {
843        let mut result: c_short = 0;
844        unsafe {
845            linux_gpib_sys::WaitSRQ(board, &mut result);
846        }
847        #[cfg(feature = "linuxgpib")]
848        let status = IbStatus::current_thread_local_status();
849        #[cfg(feature = "nigpib")]
850        let status = unsafe { IbStatus::current_global_status() };
851        if status.err {
852            Err(GpibError::DriverError(
853                status,
854                #[cfg(feature = "linuxgpib")]
855                IbError::current_thread_local_error()?,
856                #[cfg(feature = "nigpib")]
857                unsafe { IbError::current_global_error() }?,
858            ))
859        } else {
860            Ok(result)
861        }
862    })
863    .await?
864}