Read the GPIB message Continously

#26 · closed · 5 comments

View on GitHub ↗

kmuthugtk

Hi, Could you please provide a sample based on the following requirements? Send the GPIB command FULLSITES? and read the response. Then, send the command BINON and read the response. Currently, I’m using gpib-32.dll, but I’m now trying to use the visa-rs library instead. My expected will be run as a listener Help me to do this approach ![Image](https://github.com/user-attachments/assets/369d6843-2848-49dc-91d8-b5f2d1ce0228)

Comments

TsuITOAR

Hi. First, you need to find the visa address, something like `GPIB0::1::INSTR`, then connect it with [DefaultRM::open](https://docs.rs/visa-rs/latest/visa_rs/trait.AsResourceManager.html#method.open), and communicate it, like the example in README.

kmuthugtk

Yes,I did the same way. Already open the communication, now how to read the message continuously instead of byte-by-bytes can read as a whole string

TsuITOAR

The [BufRead::read_line](https://doc.rust-lang.org/std/io/trait.BufRead.html#method.read_line) method used in the example keeps reading the response until the first '\n'.

kmuthugtk

Noted with thanks. Here my code and from the code it's continuously timeout and GPIB hangup in Testers side. ``` use std::ffi::CString; use std::io::{BufRead, BufReader, Read, Write}; use std::time::Duration; use visa_rs::enums::attribute::{AttrDmaAllowEn, AttrKind, AttrSendEndEn, AttrTermchar, AttrTermcharEn, AttrTmoValue, Attribute, HasAttribute}; use visa_rs::{prelude::*, vs}; use visa_rs::vs::{VI_ATTR_TMO_VALUE, VI_TRUE}; fn main() -> visa_rs::Result<()> { //println!("Hello, world!"); // Open default resource manager let rm: DefaultRM = DefaultRM::new()?; // expression to match resource name let expr = CString::new("GPIB0?*:INSTR").unwrap().into(); // Find the first resource matched let rsc = rm.find_res(&expr)?; // Open a session to the resource, the session will be closed when rm is dropped let instr: Instrument = rm.open(&rsc, AccessMode::NO_LOCK, TIMEOUT_INFINITE)?; println!("Connected to instrument: {}", rsc); // Set timeout to 30 seconds (30000 ms) (&instr).set_attr(Attribute::AttrTmoValue(unsafe { AttrTmoValue::new_unchecked(30000) }))?; // Set termination character to LF (0x0A) (&instr).set_attr(Attribute::AttrTermchar(unsafe { AttrTermchar::new_unchecked(0x0A) })); // Enable termination character (&instr).set_attr(Attribute::AttrTermcharEn(unsafe { AttrTermcharEn::new_unchecked(VI_TRUE.try_into().unwrap()) }))?; // Enable DMA (&instr).set_attr(Attribute::AttrDmaAllowEn(unsafe { AttrDmaAllowEn::new_unchecked(VI_TRUE.try_into().unwrap()) }))?; // Enable sending END on writes (equivalent to IbcSendEnd) (&instr).set_attr(Attribute::AttrSendEndEn(unsafe { AttrSendEndEn::new_unchecked(VI_TRUE.try_into().unwrap()) }))?; // Wait a moment for the device to process std::thread::sleep(Duration::from_millis(100)); let mut reader = BufReader::new(&instr); let mut buffer = String::new(); println!("Starting continous reading. Press Ctrl+C to stop..."); loop { // Clear the buffer before reading new data buffer.clear(); match reader.read_line(&mut buffer) { Ok(bytes_read) if bytes_read > 0 => { println!("Received data: {}", buffer.trim()); }, Ok(0) => { // EOF reached, but for instruments this is unlikely // Just wait a bit and try again println!("No data (EOF). Waiting..."); std::thread::sleep(Duration::from_millis(500)); }, Ok(_) => { // This shouldn't technically happen, but needed for exhaustive pattern matching println!("Unexpected read result"); }, Err(e) => { eprintln!("Error reading from instrument: {}", e); // Short pause before retrying std::thread::sleep(Duration::from_millis(1000)); } } // Small delay between reads std::thread::sleep(Duration::from_millis(200)); } } ``` Please help me to fix this issue. ![Image](https://github.com/user-attachments/assets/02aa245c-7e0f-41e2-bd26-8e1bad562110) ![Image](https://github.com/user-attachments/assets/bfc99807-32df-4080-80ce-23231394f353) ![Image](https://github.com/user-attachments/assets/3a50f7fc-70af-4f4f-b1de-8be8b5411921)

TsuITOAR

Your code doesn't write any query message to the instrument, it just keeps waiting for response. `0xBFFFF0015` code means time out waiting for response.