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 recognizes the alternative, a
device that refused the request. The view borrows the PDU and copies nothing.
§Examples
use pamoja_modbus::{Pdu, Response};
// A soil probe's reply to a read of three registers: nitrogen, phosphorus, and
// potassium, in milligrams per kilogram.
let reply = Pdu::read_holding_registers_reply(&[555, 0, 100]).unwrap();
let values: Vec<u16> = Response::new(reply.as_bytes()).registers().unwrap().collect();
assert_eq!(values, [555, 0, 100]);Implementations§
Source§impl<'a> Response<'a>
impl<'a> Response<'a>
Sourcepub fn function_code(&self) -> u8
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.
Sourcepub fn registers(&self) -> Result<Registers<'a>, ModbusError>
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.
Sourcepub fn coils(&self, count: u16) -> Result<Coils<'a>, ModbusError>
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.