Module modbus::scoped

source ·
Expand description

A set of objects which automatically change their register or coil value when they go out of scope

Examples

When the auto object goes out of scope and is dropped, the value of coil 10 is switched On:

use modbus::{Client, Coil};
use modbus::tcp;
use modbus::scoped::{ScopedCoil, CoilDropFunction};

let mut cfg = tcp::Config::default();
let mut client = tcp::Transport::new_with_cfg("127.0.0.1", cfg).unwrap();
{
   let mut auto = ScopedCoil::new(&mut client, 10, CoilDropFunction::On).unwrap();
   assert_eq!(auto.mut_transport().read_coils(10, 1).unwrap(), vec![Coil::Off]);
}
assert_eq!(client.read_coils(10, 1).unwrap(), vec![Coil::On]);

When the auto object goes out of scope and is dropped, the value of register 10 is modified by function fun:

use modbus::{Client, Coil};
use modbus::tcp;
use modbus::scoped::{ScopedRegister, RegisterDropFunction};

let mut cfg = tcp::Config::default();
let mut client = tcp::Transport::new_with_cfg("127.0.0.1", cfg).unwrap();
client.write_single_register(10, 1);
{
    let fun = |v| v + 5;
    let mut auto = ScopedRegister::new(&mut client, 10, RegisterDropFunction::Fun(&fun)).unwrap();
    assert_eq!(auto.mut_transport().read_holding_registers(10, 1).unwrap(), vec![1]);
}
assert_eq!(client.read_holding_registers(10, 1).unwrap(), vec![6]);

Structs

  • Auto object which modifies it’s coil value depending on a given modification function if it goes out of scope.
  • Auto object which modifies it’s register value depending on a given modification function if it goes out of scope.

Enums