-
Notifications
You must be signed in to change notification settings - Fork 4
Description
This is a simple test program for Modbus. As you see there is an error.
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(())
}

