Skip to main content

Server

Struct Server 

Source
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

Source

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.

Source

pub fn with_coils(self, start: u16, values: &[bool]) -> Server

Gives the device coils at consecutive addresses.

§Arguments
  • start - the first coil’s address.
  • values - their states, in address order.
§Returns

The device.

Source

pub fn with_discrete_inputs(self, start: u16, values: &[bool]) -> Server

Gives the device discrete inputs at consecutive addresses.

§Arguments
  • start - the first input’s address.
  • values - their states, in address order.
§Returns

The device.

Source

pub fn with_holding_registers(self, start: u16, values: &[u16]) -> Server

Gives the device holding registers at consecutive addresses.

§Arguments
  • start - the first register’s address.
  • values - their values, in address order.
§Returns

The device.

Source

pub fn with_input_registers(self, start: u16, values: &[u16]) -> Server

Gives the device input registers at consecutive addresses.

§Arguments
  • start - the first register’s address.
  • values - their values, in address order.
§Returns

The device.

Source

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.
Source

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.
Source

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.
Source

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.
Source

pub fn coil(&self, address: u16) -> Option<bool>

Returns a coil’s state, or None when the device has no coil there.

Source

pub fn discrete_input(&self, address: u16) -> Option<bool>

Returns a discrete input’s state, or None when the device has no input there.

Source

pub fn holding_register(&self, address: u16) -> Option<u16>

Returns a holding register’s value, or None when the device has no register there.

Source

pub fn input_register(&self, address: u16) -> Option<u16>

Returns an input register’s value, or None when the device has no register there.

Source

pub fn unit(&self) -> u8

Returns the device’s unit address.

Source

pub fn served(&self) -> usize

Returns how many requests the device has carried out, broadcasts included and refusals not.

Source

pub fn serve(&mut self, pdu: &[u8]) -> Pdu

Serves one request PDU.

§Arguments
  • pdu - the request: a function code followed by its data.
§Returns

The reply PDU: the normal response, or the exception the request earned.

Source

pub fn answer(&mut self, frame: &[u8]) -> Option<Adu>

Answers one RTU frame, as a device on the line does.

§Arguments
  • frame - the frame as it came off the line, CRC included.
§Returns

The frame to send back, or None when the device stays silent: the frame failed its CRC, is for another unit, or is a broadcast, whose write the device still carries out.

Trait Implementations§

Source§

impl Clone for Server

Source§

fn clone(&self) -> Server

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Server

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Server

Source§

impl PartialEq for Server

Source§

fn eq(&self, other: &Server) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
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.

Source§

fn receive(&mut self, bytes: &[u8]) -> Vec<u8> ⓘ

Takes the bytes the port wrote and returns the bytes the device sends back. Read more
Source§

impl StructuralPartialEq for Server

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.