Skip to main content

Response

Struct Response 

Source
pub struct Response<'a> { /* private fields */ }
Expand description

A borrowed view over a response PDU, for reading the values a device returned.

A read response is a function code, a byte count, and then the data. registers and coils decode that data into the 16-bit words or the packed bits it represents; exception recognises the alternative, a device that refused the request. The view borrows the PDU and copies nothing.

§Examples

use pamoja_modbus::Response;

// A read-holding-registers reply: function 0x03, byte count 6, three registers.
let pdu = [0x03, 0x06, 0x02, 0x2B, 0x00, 0x00, 0x00, 0x64];
let values: Vec<u16> = Response::new(&pdu).registers().unwrap().collect();
assert_eq!(values, [0x022B, 0x0000, 0x0064]);

Implementations§

Source§

impl<'a> Response<'a>

Source

pub fn new(pdu: &'a [u8]) -> Self

Wraps a response PDU for reading.

§Arguments
  • pdu - the response PDU, a function code followed by its data.
§Returns

The response view.

Source

pub fn function_code(&self) -> u8

Returns the function code, the first byte of the PDU.

§Returns

The function code, or 0 if the PDU is empty.

Source

pub fn exception(&self) -> Option<Exception>

Returns the exception a device reported, if this is an exception response.

§Returns

The Exception if the function code’s high bit is set and a defined exception byte follows it, otherwise None.

Source

pub fn registers(&self) -> Result<Registers<'a>, ModbusError>

Reads the 16-bit registers from a read-registers response.

§Returns

An iterator over the registers in order, each decoded from its big-endian pair.

§Errors

Returns ModbusError::MalformedResponse if the PDU is truncated, its declared byte count does not match its data, or that data is not a whole number of registers.

Source

pub fn coils(&self, count: u16) -> Result<Coils<'a>, ModbusError>

Reads the coils or discrete inputs from a read-bits response.

The response packs the bits least-significant first; this unpacks exactly count of them and ignores the padding in the final byte.

§Arguments
  • count - how many bits to read, the quantity the request asked for.
§Returns

An iterator over count bits in order.

§Errors

Returns ModbusError::MalformedResponse if the PDU is truncated or its declared byte count does not match the data or the requested count.

Trait Implementations§

Source§

impl<'a> Clone for Response<'a>

Source§

fn clone(&self) -> Response<'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 Response<'a>

Source§

impl<'a> Debug for Response<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Response<'a>

§

impl<'a> RefUnwindSafe for Response<'a>

§

impl<'a> Send for Response<'a>

§

impl<'a> Sync for Response<'a>

§

impl<'a> Unpin for Response<'a>

§

impl<'a> UnsafeUnpin for Response<'a>

§

impl<'a> UnwindSafe for Response<'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, 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.