Skip to content

Commit

Permalink
Fix rtu-server-address example (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde authored Jul 21, 2024
1 parent 55c327a commit dd1f676
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions examples/rtu-server-address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,35 @@ struct Service {
slave: Slave,
}

impl tokio_modbus::server::Service for Service {
type Request = SlaveRequest<'static>;
type Response = Response;
type Exception = Exception;
type Future = future::Ready<Result<Self::Response, Self::Exception>>;

fn call(&self, req: Self::Request) -> Self::Future {
if req.slave != self.slave.into() {
return future::ready(Err(Exception::IllegalFunction));
impl Service {
fn handle(&self, req: SlaveRequest<'_>) -> Result<Option<Response>, Exception> {
let SlaveRequest { slave, request } = req;
if slave != self.slave.into() {
// Filtering: Ignore requests with mismatching slave IDs.
return Ok(None);
}
match req.request {
match request {
Request::ReadInputRegisters(_addr, cnt) => {
let mut registers = vec![0; cnt.into()];
registers[2] = 0x77;
future::ready(Ok(Response::ReadInputRegisters(registers)))
Ok(Some(Response::ReadInputRegisters(registers)))
}
_ => future::ready(Err(Exception::IllegalFunction)),
_ => Err(Exception::IllegalFunction),
}
}
}

impl tokio_modbus::server::Service for Service {
type Request = SlaveRequest<'static>;
type Response = Option<Response>;
type Exception = Exception;
type Future = future::Ready<Result<Self::Response, Self::Exception>>;

fn call(&self, req: Self::Request) -> Self::Future {
future::ready(self.handle(req))
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let slave = Slave(12);
Expand Down

0 comments on commit dd1f676

Please sign in to comment.