Skip to content

Can't get cycle time les then ~ 15 ms in Modbus RTU #297

Open
@ZaytsveDmitriy

Description

@ZaytsveDmitriy

I think that serial port in windows wait some timeout or when buffer will be full. But i don't know how to fix it.
I'm control with oscilloscope that device response immediately (about 1 ms)

OS - Windows
Code:

fn main() -> Result<(), Box<dyn std::error::Error>> {
    use tokio_modbus::prelude::*;

    let tty_path = "COM6";
    let slave = Slave(0x2);

    let builder = tokio_serial::new(tty_path, 115200)
    .data_bits(tokio_serial::DataBits::Eight)
    .parity(tokio_serial::Parity::Even)
    .stop_bits(tokio_serial::StopBits::One);
    
    let mut ctx = sync::rtu::connect_slave(&builder, slave)?;
    println!("Reading a sensor value");
    let mut now;
    loop {
        now = time::Instant::now();
        let res = ctx.read_holding_registers(0x1301, 1);
        match res {
            Err(er) => println!("Not OK {}", er),
            Ok(val) => println!("Sensor value is: ok - {:?}, time is: {}", val, now.elapsed().as_micros()),
        };
    };

    Ok(())
}

Output:
Reading a sensor value
Sensor value is: ok - Ok([15]), time is: 15353
Sensor value is: ok - Ok([15]), time is: 14505
Sensor value is: ok - Ok([15]), time is: 15005
Sensor value is: ok - Ok([15]), time is: 15438
Sensor value is: ok - Ok([15]), time is: 15151
Sensor value is: ok - Ok([15]), time is: 15628
Sensor value is: ok - Ok([15]), time is: 15421
Sensor value is: ok - Ok([15]), time is: 15666
Sensor value is: ok - Ok([15]), time is: 14693
Sensor value is: ok - Ok([15]), time is: 15739

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions