pub struct Server { /* private fields */ }Expand description
A Modbus device: a unit address and the four tables it serves, coils, discrete inputs, holding registers, and input registers, each holding only the addresses it was given.
answer takes one RTU frame and returns the frame the device sends
back, following the Modbus over Serial Line specification: a frame that fails its CRC,
is for another unit, or is a broadcast gets no answer, and a broadcast write is still
carried out. serve is the same at the PDU level, for a transport with
its own framing. A request the device cannot serve is answered with the exception the
application protocol’s state diagrams give: an unknown function, a quantity or value out
of range, then an address the device does not have.
§Examples
use pamoja_modbus::{Adu, Pdu, Server};
// A meter at unit 17 holding voltage, current, and a fault word from register 107.
let mut meter = Server::new(17)?.with_holding_registers(107, &[2301, 418, 0]);
let request = Pdu::read_holding_registers(107, 3).to_adu(17);
let reply = meter.answer(request.as_bytes()).expect("the meter answers its own unit");
let registers: Vec<u16> = reply.response().registers()?.collect();
assert_eq!(registers, [2301, 418, 0]);
// Register 110 is not in its table.
let past = Pdu::read_holding_registers(108, 3).to_adu(17);
let refused = meter.answer(past.as_bytes()).expect("an exception is an answer");
assert_eq!(refused.exception(), Some(pamoja_modbus::Exception::IllegalDataAddress));Implementations§
Source§impl Server
impl Server
Sourcepub fn new(unit: u8) -> Result<Server, ModbusError>
pub fn new(unit: u8) -> Result<Server, ModbusError>
Makes a device at a unit address, with every table empty.
§Arguments
unit- its address, 1 to 247.
§Returns
The device.
§Errors
ModbusError::UnitOutOfRange for 0, the broadcast address, or 248 to 255, which the
specification reserves.
Sourcepub fn with_coils(self, start: u16, values: &[bool]) -> Server
pub fn with_coils(self, start: u16, values: &[bool]) -> Server
Sourcepub fn with_discrete_inputs(self, start: u16, values: &[bool]) -> Server
pub fn with_discrete_inputs(self, start: u16, values: &[bool]) -> Server
Sourcepub fn with_holding_registers(self, start: u16, values: &[u16]) -> Server
pub fn with_holding_registers(self, start: u16, values: &[u16]) -> Server
Sourcepub fn with_input_registers(self, start: u16, values: &[u16]) -> Server
pub fn with_input_registers(self, start: u16, values: &[u16]) -> Server
Sourcepub fn set_holding_registers(&mut self, start: u16, values: &[u16])
pub fn set_holding_registers(&mut self, start: u16, values: &[u16])
Sets holding registers from an address on, adding any the device did not have, as its own measurements change.
§Arguments
start- the first register’s address.values- their values, in address order.
Sourcepub fn set_input_registers(&mut self, start: u16, values: &[u16])
pub fn set_input_registers(&mut self, start: u16, values: &[u16])
Sets input registers from an address on, adding any the device did not have.
§Arguments
start- the first register’s address.values- their values, in address order.
Sourcepub fn set_coils(&mut self, start: u16, values: &[bool])
pub fn set_coils(&mut self, start: u16, values: &[bool])
Sets coils from an address on, adding any the device did not have.
§Arguments
start- the first coil’s address.values- their states, in address order.
Sourcepub fn set_discrete_inputs(&mut self, start: u16, values: &[bool])
pub fn set_discrete_inputs(&mut self, start: u16, values: &[bool])
Sets discrete inputs from an address on, adding any the device did not have.
§Arguments
start- the first input’s address.values- their states, in address order.
Sourcepub fn coil(&self, address: u16) -> Option<bool>
pub fn coil(&self, address: u16) -> Option<bool>
Returns a coil’s state, or None when the device has no coil there.
Sourcepub fn discrete_input(&self, address: u16) -> Option<bool>
pub fn discrete_input(&self, address: u16) -> Option<bool>
Returns a discrete input’s state, or None when the device has no input there.
Sourcepub fn holding_register(&self, address: u16) -> Option<u16>
pub fn holding_register(&self, address: u16) -> Option<u16>
Returns a holding register’s value, or None when the device has no register there.
Sourcepub fn input_register(&self, address: u16) -> Option<u16>
pub fn input_register(&self, address: u16) -> Option<u16>
Returns an input register’s value, or None when the device has no register there.
Sourcepub fn served(&self) -> usize
pub fn served(&self) -> usize
Returns how many requests the device has carried out, broadcasts included and refusals not.
Trait Implementations§
impl Eq for Server
Source§impl Peer for Server
Available on crate feature port only.A server on the far end of a simulated port answers each write as one frame.
impl Peer for Server
port only.A server on the far end of a simulated port answers each write as one frame.