Skip to main content

Request

Enum Request 

Source
pub enum Request<'a> {
    ReadCoils {
        start: u16,
        quantity: u16,
    },
    ReadDiscreteInputs {
        start: u16,
        quantity: u16,
    },
    ReadHoldingRegisters {
        start: u16,
        quantity: u16,
    },
    ReadInputRegisters {
        start: u16,
        quantity: u16,
    },
    WriteSingleCoil {
        address: u16,
        on: bool,
    },
    WriteSingleRegister {
        address: u16,
        value: u16,
    },
    WriteMultipleCoils {
        start: u16,
        values: Coils<'a>,
    },
    WriteMultipleRegisters {
        start: u16,
        values: Registers<'a>,
    },
}
Expand description

A request as a device reads it, checked against the limits the specification sets.

parse follows the order of the specification’s state diagrams: a function the device does not serve is refused with IllegalFunction, and a quantity, a byte count, or a coil value outside what the function allows with IllegalDataValue. Whether the addresses exist is the device’s own question, answered with IllegalDataAddress once the request has parsed.

§Examples

use pamoja_modbus::{Exception, Pdu, Request};

let pdu = Pdu::read_holding_registers(0x006B, 3);
assert_eq!(
    Request::parse(pdu.as_bytes()),
    Ok(Request::ReadHoldingRegisters { start: 0x006B, quantity: 3 })
);

// 126 registers is one more than a reply can carry.
let greedy = Pdu::read_holding_registers(0, 126);
assert_eq!(Request::parse(greedy.as_bytes()), Err(Exception::IllegalDataValue));

Variants§

§

ReadCoils

Read coils, function 0x01.

Fields

§start: u16

The first coil’s address.

§quantity: u16

How many coils, 1 to 2000.

§

ReadDiscreteInputs

Read discrete inputs, function 0x02.

Fields

§start: u16

The first input’s address.

§quantity: u16

How many inputs, 1 to 2000.

§

ReadHoldingRegisters

Read holding registers, function 0x03.

Fields

§start: u16

The first register’s address.

§quantity: u16

How many registers, 1 to 125.

§

ReadInputRegisters

Read input registers, function 0x04.

Fields

§start: u16

The first register’s address.

§quantity: u16

How many registers, 1 to 125.

§

WriteSingleCoil

Write one coil, function 0x05.

Fields

§address: u16

The coil’s address.

§on: bool

The state to write.

§

WriteSingleRegister

Write one holding register, function 0x06.

Fields

§address: u16

The register’s address.

§value: u16

The value to write.

§

WriteMultipleCoils

Write a run of coils, function 0x0F.

Fields

§start: u16

The first coil’s address.

§values: Coils<'a>

The states to write, 1 to 1968 of them, in address order.

§

WriteMultipleRegisters

Write a run of holding registers, function 0x10.

Fields

§start: u16

The first register’s address.

§values: Registers<'a>

The values to write, 1 to 123 of them, in address order.

Implementations§

Source§

impl<'a> Request<'a>

Source

pub fn parse(pdu: &'a [u8]) -> Result<Request<'a>, Exception>

Reads a request PDU, checking it the way the specification’s state diagrams do.

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

The request.

§Errors

Exception::IllegalFunction for an empty PDU or a function this type does not name, and Exception::IllegalDataValue for a PDU too short for its function, a quantity outside the function’s range, a byte count that does not match the quantity, or a single coil written with anything but 0x0000 or 0xFF00.

Source

pub fn function(&self) -> Function

Returns the function the request asks for.

Source

pub fn span(&self) -> (u16, usize)

Returns the addresses the request reads or writes, as its first address and how many follow, which a device checks against the addresses it has.

§Returns

The first address and the count.

Source

pub fn reads(&self) -> bool

Returns whether the request only reads.

Trait Implementations§

Source§

impl<'a> Clone for Request<'a>

Source§

fn clone(&self) -> Request<'a>

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<'a> Copy for Request<'a>

Source§

impl<'a> Debug for Request<'a>

Source§

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

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

impl PartialEq for Request<'_>

Source§

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

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

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

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Request<'a>

§

impl<'a> RefUnwindSafe for Request<'a>

§

impl<'a> Send for Request<'a>

§

impl<'a> Sync for Request<'a>

§

impl<'a> Unpin for Request<'a>

§

impl<'a> UnsafeUnpin for Request<'a>

§

impl<'a> UnwindSafe for Request<'a>

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.