TsuITOAR / visa-rs

High-level Rust bindings to VISA(Virtual Instrument Software Architecture)
Apache License 2.0
17 stars 2 forks source link

Unwrap causing panic #22

Open Devlyn-Nelson opened 2 weeks ago

Devlyn-Nelson commented 2 weeks ago

When a device is not present in the system and I try to connect via TCP using DefaultRM::new()?.open(), visa-rs panics with a 0xBFFF0011 error which should just be returned as an visa-rs::Error because the function already returns a result contain that error type. The root cause of the panic is an unwrap after a try_into in the wrap_raw_error_in_unsafe macro.

Devlyn-Nelson commented 2 weeks ago

I should say I am willing to do work in order to fix this. I have a workaround currently working. there are 2 problems:

TsuITOAR commented 2 days ago

Thanks for your response, I will check this problem ASAP.

TsuITOAR commented 1 day ago

On my PC, it returns the Error ErrorRsrcNfound (error code 0xBFFF0011) as expected, this problem might be related to your specific environment. 0xBFFF0011 is a negative number, so it should match the Error arm in the macro. Here are codes that I tried.

use std::ffi::CString;

use visa_rs::{prelude::*, vs::ViStatus};

fn main() -> visa_rs::Result<()> {
    let res = CString::new("TCPIP0::192.168.3.1::hislip0::INSTR")
        .unwrap()
        .into();
    let r = DefaultRM::new()?.open(&res, AccessMode::NO_LOCK, TIMEOUT_IMMEDIATE);
    if let Result::Err(e) = r {
        eprintln!("Returned error in rust: `{}`", e);
        eprintln!("Error code: `{:#0X}`", e.0 as ViStatus);
    }
    Ok(())
}

Output

Returned error in rust: `Insufficient location information or the device or resource is not present in the system.`
Error code: `0xBFFF0011`

Feel free to ask if you want more help.