Giter Site home page Giter Site logo

easy-modbus's People

Contributors

yangyize avatar

Stargazers

 avatar  avatar

Watchers

 avatar

easy-modbus's Issues

request tid numbers and response tid numbers do not match

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(())

}

v0.0.3

  • Byte&Bit util tools
  • TCP<->RTU converter

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.