Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Although disconnect the context, the SerialStream reopen error #212

Open
ileon opened this issue Jul 28, 2023 · 1 comment
Open

Although disconnect the context, the SerialStream reopen error #212

ileon opened this issue Jul 28, 2023 · 1 comment
Labels

Comments

@ileon
Copy link

ileon commented Jul 28, 2023

use anyhow::Result;
use tokio_modbus::prelude::*;
use tokio_modbus::{client::rtu, Slave};
use tokio_serial::SerialStream;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
    {
        let builder = tokio_serial::new("COM9", 9600);
        let port = SerialStream::open(&builder)?;
        let mut ctx1 = rtu::attach_slave(port, Slave(1));
        let rsp1 = ctx1.read_holding_registers(1, 2).await;
        println!("rsp1: {:?}", rsp1);
        ctx1.disconnect().await?;
    }
    let builder = tokio_serial::new("COM9", 9600);
    let port_try_open = SerialStream::open(&builder);
    let port = match port_try_open {
        Ok(p) => p,
        Err(err) => {
            println!("Re-open Error: {:?}", err);
            panic!();
        }
    };
    let mut ctx2 = rtu::attach_slave(port, Slave(2));
    let rsp2 = ctx2.read_holding_registers(1, 2).await;
    println!("rsp2: {:?}", rsp2);

    Ok(())
}

The error message is :

rsp1: Ok([250, 1])
Re-open Error: Error { kind: NoDevice, description: "Access is denied." }

Please help to check how to re-build another new Context after drop the previous one. and the disconnect method seems did not work.

@ileon
Copy link
Author

ileon commented Jul 28, 2023

I know there is a set_slave() method, but why in above code, it was panic ? the disconnect looks not work ? the SerialStream did not drop in the first {} block ? how to drop the SerialStream properly? I have tried, but can not get the right method to re-build Context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants