Skip to content

Commit

Permalink
handle more error conditions discovering VISA instruments
Browse files Browse the repository at this point in the history
  • Loading branch information
esarver committed Sep 24, 2024
1 parent 9dbe2ed commit 926f9a3
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions kic-discover-visa/src/visa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashSet, ffi::CString, time::Duration};

use async_std::fs::write;
use serde::{Deserialize, Serialize};
use tracing::trace;
use tracing::{error, trace};
use tsp_toolkit_kic_lib::{
instrument::info::{get_info, InstrumentInfo},
interface::connection_addr::ConnectionAddr,
Expand All @@ -15,7 +15,10 @@ use crate::{insert_disc_device, model_check, IoType};
pub async fn visa_discover(timeout: Option<Duration>) -> anyhow::Result<HashSet<InstrumentInfo>> {
let mut discovered_instruments: HashSet<InstrumentInfo> = HashSet::new();

let rm = visa_rs::DefaultRM::new()?;
let Ok(rm) = visa_rs::DefaultRM::new() else {
error!("Unable to get VISA Default Resource Manager");
return Ok(discovered_instruments);
};
let instruments = match rm.find_res_list(&CString::new("?*")?.into()) {
Ok(x) => x,
Err(e) => {
Expand All @@ -26,27 +29,23 @@ pub async fn visa_discover(timeout: Option<Duration>) -> anyhow::Result<HashSet<
trace!("discovered: {instruments:?}");

for i in instruments {
let i = i?;
if i.to_string().contains("SOCKET") {
let Ok(i) = i else {
continue;
};
if i.to_string().contains("SOCKET") || i.to_string().contains("INTFC") {
continue;
}
trace!("Connecting to {i:?} to get info");
let mut connected = match rm.open(&i, AccessMode::NO_LOCK, visa_rs::TIMEOUT_IMMEDIATE) {
Ok(c) => c,
Err(_) => {
trace!("Resource {i} no longer available, skipping.");
continue;
}
let Ok(mut connected) = rm.open(&i, AccessMode::NO_LOCK, visa_rs::TIMEOUT_IMMEDIATE) else {
trace!("Resource {i} no longer available, skipping.");
continue;
};

trace!("Getting info from {connected:?}");
let mut info = match get_info(&mut connected) {
Ok(i) => i,
Err(_) => {
trace!("Unable to write to {i}, skipping");
drop(connected);
continue;
}
let Ok(mut info) = get_info(&mut connected) else {
trace!("Unable to write to {i}, skipping");
drop(connected);
continue;
};
info.address = Some(ConnectionAddr::Visa(i.clone()));
trace!("Got info: {info:?}");
Expand Down

0 comments on commit 926f9a3

Please sign in to comment.