Comments (6)
@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.
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.
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.
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 animpl Future
. If you remove the async marker onconnect_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.
@conorf50 can we close this issue?
from tokio-modbus.
@conorf50 can we close this issue?
Yes go ahead. Thanks everyone for the help
from tokio-modbus.
Related Issues (20)
- "Device or resource busy" in RTU server example HOT 3
- Generalize impl around `Service` usage HOT 5
- Tokio module 'runtime' is private HOT 12
- Asyncronous responses / Multiplexing HOT 2
- How to convert tokio_modbus::Request to bytes? HOT 5
- TCP server example - shared data hashmap HOT 5
- TCP Sync Client HOT 6
- write_multiple_registers taking array as a param HOT 3
- Can not disconnect slave, need help HOT 3
- Although disconnect the context, the SerialStream reopen error HOT 1
- Patterns/best practice for reconnect-after-failure? HOT 1
- How to turn 'tokio_serial::SerialStream' into global static to save it for reuse
- is_connected()? HOT 1
- Modbus RTU - Serial Direction Pin HOT 6
- Dev Dependencies: Upgrade rustls-pemfile and tokio-rustls HOT 4
- Return Modbus exception codes for client and server.
- Use `async-trait` in `Service` trait ? HOT 10
- tokio-modbus 0.12 entering unreachable code in any response error HOT 7
- Dev Dependencies: Upgrade rustls
- How to get hexadecimal modbus rtu message data HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tokio-modbus.