Coder Social home page Coder Social logo

Comments (6)

flosse avatar flosse commented on May 27, 2024

@conorf50 Thank you for reporting.
I'm busy at the moment but can have a look at this at the end of next week.
Meanwhile you could ask @uklotzde, @Darksonn or @not-fl3

from tokio-modbus.

keisrk avatar keisrk commented on May 27, 2024

Hi guys, I'm also the one looking forward to async/await. Thank you for this nice project @flosse . Probably this is not the issue in the branch actually. @conorf50 , could you try below?

This is the synchronous code which compiles without the error.

use tokio_modbus::prelude::*;
use tokio_modbus::client::tcp;
use tokio_modbus::client::sync::*; // TODO, refactor this to use async I/O

pub fn main() {

    let ctx: Result<Context, _> = connect_sync();
    let mut ctx = ctx.unwrap();
    let power = ctx.read_holding_registers(40204, 1);
    println!("Power = {:?}", power);
    
}

/*async*/ fn connect_sync() -> Result<tokio_modbus::client::sync::Context, std::io::Error> {
    
    let socket_addr = "127.0.0.1:5020".parse().unwrap();
    
    println!("attempting to connect");
    println!("{:?}", socket_addr);
    
    let ctx = tokio_modbus::client::sync::tcp::connect(socket_addr)?;
    Ok(ctx)
}

Latter is the async/await counterpart. I tried to guess and follow your original intention. You need to include futures = "0.3" as one of dependencies in your Cargo.toml.

// refactor to use async I/O
use futures::executor::block_on;
use tokio_modbus::prelude::*;
use tokio_modbus::client::Context;
use tokio_modbus::client::tcp::connect;

fn main() {
                     
    let ctx = connect_async();
    let ctx: Result<Context, _> = block_on(ctx);
    let mut ctx: Context = ctx.unwrap();

    let power = ctx.read_holding_registers(40204, 1);
    let power = block_on(power).unwrap();
    println!("Power = {:?}", power);
    
}

async fn connect_async() -> Result<Context, std::io::Error> {
    
    let socket_addr = "127.0.0.1:5020".parse().unwrap();
    
    println!("attempting to connect");
    println!("{:?}", socket_addr);
    
    connect(socket_addr).await
}

from tokio-modbus.

Darksonn avatar Darksonn commented on May 27, 2024

You defined connect_sync as an async fn, and all async fns automatically return a future (this is what it means to mark a function async). Thus the error regarding an impl Future. If you remove the async marker on connect_sync, it works:

use tokio_modbus::client::sync::Reader;

pub fn main() {

    let mut ctx = connect_sync().unwrap();
    let power = ctx.read_holding_registers(40204, 1);
    println!("Power = {:?}", power);
}

fn connect_sync() -> Result<tokio_modbus::client::sync::Context, std::io::Error> {

    let socket_addr = "127.0.0.1:5020".parse().unwrap();

    println!("attempting to connect");
    println!("{:?}", socket_addr);

    let ctx = tokio_modbus::client::sync::tcp::connect(socket_addr)?;
    Ok(ctx)
}

from tokio-modbus.

conorf50 avatar conorf50 commented on May 27, 2024

You defined connect_sync as an async fn, and all async fns automatically return a future (this is what it means to mark a function async). Thus the error regarding an impl Future. If you remove the async marker on connect_sync, it works:

I had it marked as async because of a previous attempt at using async/await. I didn't realise that was what was causing the problem.

Thanks @Darksonn for the help!

from tokio-modbus.

flosse avatar flosse commented on May 27, 2024

@conorf50 can we close this issue?

from tokio-modbus.

conorf50 avatar conorf50 commented on May 27, 2024

@conorf50 can we close this issue?

Yes go ahead. Thanks everyone for the help

from tokio-modbus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.