Skip to content

request tid numbers and response tid numbers do not match #5

@tonicChord

Description

@tonicChord

image

This is a simple test program for Modbus. As you see there is an error.

image

That is our program's response. I wonder, why does our program respond in three different responses and why do the tid numbers not match?

The code of program is a copy of example.

use std::error::Error;

use futures::SinkExt;
use tokio::net::{TcpListener, TcpStream};
use tokio_stream::StreamExt;
use tokio_util::codec::Framed;

use easy_modbus::{codec::TcpServerCodec, Frame};

use std::fmt;

#[tokio::main]
async fn main() -> Result<(), Box> {
let addr = "127.0.0.1:502".to_string();
let server = TcpListener::bind(&addr).await?;
println!("Listening on: {}", addr);

loop {
    let (stream, _) = server.accept().await?;
    tokio::spawn(async move {
        if let Err(e) = process(stream).await {
            println!("failed to  process connection; error = {}", e);
        }
    });
}

}

async fn process(stream: TcpStream) -> Result<(), Box> {
let mut transport = Framed::new(stream, TcpServerCodec);
let frame = Frame::tcp();

let mut test_vec: Vec<u8> = vec![0xff, 0xff, 0xff, 0xff];

while let Some(request) = transport.next().await {
    match request {
        Ok(request) => {
            println!("load request --- {:?}\n", request);

            let mut response = frame.read_holding_register_response(0x01, test_vec.clone());

            println!("send response --- {:?}\n", response);
            transport.send(response).await?;
        }
        Err(e) => return Err(e.into()),
    }
}
Ok(())

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions