最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

serial port - Rust visa-rs setting the baud rate - Stack Overflow

programmeradmin4浏览0评论

I have been attempting to create a library for connecting to one of two multimeters we have at my work. One is a Bk 5491B which is of the Serial Com ports, the other is a Siglent SDM 3055-SC which is listed as a Test Measurement Device. I am able to connect to the Siglent with my current program, but I cannot set the baud rate to 38400 using set_attr. I know my implementation of that function is probably not correct but here it is:

impl Meter {
    fn new(resource_name: &VisaString) -> Arc<Mutex<Self>> {
        let mut meter = Meter {
            resource_name: resource_name.clone(),
            instrument: None,
            rm: None,
            is_connected: false,
        };
        // Attempt initial connection
        let _ = meter.connect();
        Arc::new(Mutex::new(meter))
    }

    fn connect(&mut self) {
        // Close existing connection if any
        self.close();

        let rm = match DefaultRM::new() {
            Ok(rm) => {
                let attr = AttrAsrlBaud::new_checked(38400).unwrap(); //HERE IS WHERE MY ISSUE IS
                rm.set_attr(attr).unwrap();
                rm
            }
            Err(e) => {
                println!("Failed to create resource manager: {:?}", e);
                self.is_connected = false;
                return;
            }
        };
        self.rm = Some(rm);

        let instr = match self.rm.as_ref().unwrap().open(
            &self.resource_name,
            AccessMode::LOAD_CONFIG,
            Duration::from_secs(5),
        ) {
            Ok(instr) => instr,
            Err(e) => {
                println!("Failed to open instrument: {:?}", e);
                self.is_connected = false;
                return;
            }
        };

        self.instrument = Some(instr);

        let idn = match self.query("*IDN?") {
            Ok(idn) => idn,
            Err(e) => {
                println!("Failed to query *IDN?: {:?}", e);
                self.is_connected = false;
                return;
            }
        };

        if idn.contains("SIGLENT")
            || idn.contains("SDM3055")
            || idn.contains("BK")
            || idn.contains("5492")
        {
            println!("Connected to: {}", idn);
            self.is_connected = true;
        } else {
            println!("Invalid device connected: {}", idn);
            self.is_connected = false;
        }
    }

    fn write(&mut self, msg: &str) -> visa_rs::Result<()> {
        if let Some(ref mut instr) = self.instrument {
            let formatted_msg = format!("{}\n", msg);
            instr.write_all(formatted_msg.as_bytes()).expect(msg);
            Ok(())
        } else {
            Err(visa_rs::Error(visa_rs::enums::status::ErrorCode::ErrorIo))
        }
    }

    fn query(&mut self, msg: &str) -> visa_rs::Result<String> {
        if let Some(ref mut instr) = self.instrument {
            let formatted_msg = format!("{}\n", msg);
            instr.write_all(msg.as_bytes()).unwrap();
            let mut response = String::new();
            let mut buf = [0; 256];
            loop {
                let bytes_read = instr.read(&mut buf).expect(msg);
                response.push_str(&String::from_utf8_lossy(&buf[..bytes_read]));
                if response.ends_with('\n') {
                    break;
                }
            }
            Ok(response.trim().to_string())
        } else {
            Err(visa_rs::Error(visa_rs::enums::status::ErrorCode::ErrorIo))
        }
    }

    fn get_data(&mut self) -> f64 {
        self.query("READ?")
            .unwrap_or("0.0".to_string())
            .trim()
            .parse()
            .unwrap_or(0.0)
    }

My question is, what is the proper way to change the baud rate for the resource manager? I just want to get it to work for the BK, then create something that will diverge the two meters since the Siglent does not require the baud to be set.

发布评论

评论列表(0)

  1. 暂无评论