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

Cannot run RTU client sync example #107

Closed
sirhcel opened this issue Dec 15, 2021 · 12 comments
Closed

Cannot run RTU client sync example #107

sirhcel opened this issue Dec 15, 2021 · 12 comments

Comments

@sirhcel
Copy link
Contributor

sirhcel commented Dec 15, 2021

  • I wanted to give to give the Modbus RTU client examples a test drive but failed in both cases
    • rtu-client-sync.rs panics with
      $ cargo run --features rtu,sync --example rtu-client-sync
         Compiling tokio-modbus v0.5.2 (/.../tokio-modbus)
          Finished dev [unoptimized + debuginfo] target(s) in 0.85s
           Running `target/debug/examples/rtu-client-sync`
      thread 'main' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime', /.../.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.14.0/src/runtime/context.rs:29:26
      note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
      
  • Is this examples expected to run out of the box?
  • What could I do to actually get it running?
  • At a first first glance into context.rs:29 it looks like borrowing from CONTEXT fails and I bet there is some initialization missing. But this is my first contact with Tokio and I have no idea about the what, how, and why.
@sirhcel sirhcel changed the title Cannot run RTU client examples Cannot run RTU client sync example Dec 16, 2021
@sirhcel
Copy link
Contributor Author

sirhcel commented Dec 16, 2021

  • I meanwhile managed to get rtu-client-sync.rs running explicitly bringing in a Tokio runtime:
    use tokio::runtime::Runtime;
    fn main() -> Result<(), Box<dyn std::error::Error>> {
        let rt = Runtime::new().unwrap();
        let _guard = rt.enter();
    
        println!("Hello, world!");
        // ...
    }
  • What's the right way, to get the sync example running?
  • I initially erroneously reported the async example hanging (I used the wrong parity configuration) and updated title and description accordingly

@manio
Copy link

manio commented Dec 22, 2021

Maybe the example is outdated?
What if you add the line similar as in this example:

#[tokio::main(flavor = "current_thread")]

in the first line before main, so it will start the tokio runtime.
Is it OK then?

@sirhcel
Copy link
Contributor Author

sirhcel commented Dec 22, 2021

Thank you for this tip! I already tried this, but this still results in the same error message as shown in the description. My example looks like

$ git diff examples/rtu-client-sync.rs
diff --git a/examples/rtu-client-sync.rs b/examples/rtu-client-sync.rs
index 5872f26..cf11c96 100644
--- a/examples/rtu-client-sync.rs
+++ b/examples/rtu-client-sync.rs
@@ -1,4 +1,5 @@
-pub fn main() -> Result<(), Box<dyn std::error::Error>> {
+#[tokio::main(flavor = "current_thread")]
+pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
     use tokio_modbus::prelude::*;

     let tty_path = "/dev/ttyUSB0";

and I was running it like shown in the description. I would have expected this to work as well.

@sirhcel
Copy link
Contributor Author

sirhcel commented Dec 28, 2021

What is the "official" way for providing a runtime for this example? If it has to be done explicitly like in #107 (comment), I will create a PR for updating it.

@vita1it4
Copy link

Did someone manage to run an example of reading modbus in synchronous mode? It doesn't work for me either, same error as described in the first post. The asynchronous read example works, but I need a synchronous one.

@fastfailure
Copy link

fastfailure commented Jun 17, 2022

Did someone manage to run an example of reading modbus in synchronous mode? It doesn't work for me either, same error as described in the first post. The asynchronous read example works, but I need a synchronous one.

I did manage as suggested in #107 (comment)

@vita1it4
Copy link

This example doesn't work for me:

   |
10 |     let rt = Runtime::new().unwrap();
   |                       ^^^ function or associated item not found in `Runtime`

@fastfailure
Copy link

This example doesn't work for me:

   |
10 |     let rt = Runtime::new().unwrap();
   |                       ^^^ function or associated item not found in `Runtime`

Have you brought into scope the correct Runtime object? use tokio::runtime::Runtime;

Can you post your code?

@vita1it4
Copy link

Here is my code:

//use tokio_serial::{DataBits, Parity, StopBits};
//use tokio_modbus::prelude::*;
use tokio::runtime::Runtime;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let rt = Runtime::new().unwrap();
    let _guard = rt.enter();

    println!("Hello, world!");
/*
    let slave = Slave(4);
    let builder = tokio_serial::new("/dev/ttyUSB0", 19200);
    let builder = builder
        .data_bits(DataBits::Eight)
        .stop_bits(StopBits::Two)
        .parity(Parity::None);
*/
//    let mut ctx = sync::rtu::connect_slave(&builder, slave)?;
//    println!("Reading a sensor value");
//    let rsp = ctx.read_input_registers(25, 1)?;
//    println!("Sensor value is: {:?}", rsp);
    Ok(())
}

@fastfailure
Copy link

fastfailure commented Jun 21, 2022

I think you are missing tokio rt-multi-thread feature (or another flavor feature).

I have this in Cargo.toml:

tokio-modbus = { version = "0.5.2", default-features = false, features = [
    "rtu",
    "sync",
] }
tokio-serial = "5.4.2"
tokio = { version = "1.17.0", features = ["rt-multi-thread"] }

@vita1it4
Copy link

Yes you are right. I'm missing tokio rt-multi-thread feature.
Now that I've added it, my code is working. And synchronous reading modbus now also works.
Thank you!

@sirhcel
Copy link
Contributor Author

sirhcel commented Jun 22, 2022

#116 resolves this and the "vanilla" example works now out of the box.

@sirhcel sirhcel closed this as completed Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants