pub struct Pdu { /* private fields */ }Expand description
A Modbus protocol data unit: a function code followed by its data.
The PDU is the part of a frame that is the same on every transport. On RTU it sits
between the unit address and the CRC; wrap one with to_adu to get a
frame ready for the wire.
The constructors build the standard requests so callers state intent (“read three
holding registers”) rather than packing bytes, encoding addresses and counts in the
big-endian order Modbus uses. For a function code this crate does not name, raw
carries arbitrary bytes through unchanged. The data is held in a fixed buffer, so a
PDU needs no allocation.
§Examples
use pamoja_modbus::{Pdu, Request};
// Set a pump controller's pressure setpoint, held in register 40, to 1200 kPa.
let pdu = Pdu::write_single_register(40, 1200);
// The controller reads it back as the request it is.
let request = Request::parse(pdu.as_bytes()).expect("a request the controller can serve");
assert!(matches!(
request,
Request::WriteSingleRegister { address: 40, value: 1200 }
));Implementations§
Source§impl Pdu
impl Pdu
Sourcepub const MAX_LEN: usize = 253
pub const MAX_LEN: usize = 253
The largest a Modbus RTU PDU may be, in bytes: the 256-byte ADU less the one-byte address and the two-byte CRC.
Sourcepub const MAX_WRITE_REGISTERS: usize = 123
pub const MAX_WRITE_REGISTERS: usize = 123
The most registers a single write-multiple-registers request may carry.
Sourcepub const MAX_WRITE_COILS: usize = 1968
pub const MAX_WRITE_COILS: usize = 1968
The most coils a single write-multiple-coils request may carry.
Sourcepub const MAX_READ_REGISTERS: usize = 125
pub const MAX_READ_REGISTERS: usize = 125
The most holding or input registers a single read may ask for, which fills a reply’s 250 data bytes.
Sourcepub const MAX_READ_BITS: usize = 2000
pub const MAX_READ_BITS: usize = 2000
The most coils or discrete inputs a single read may ask for.
Sourcepub fn read_coils(start: u16, count: u16) -> Pdu
pub fn read_coils(start: u16, count: u16) -> Pdu
Sourcepub fn read_discrete_inputs(start: u16, count: u16) -> Pdu
pub fn read_discrete_inputs(start: u16, count: u16) -> Pdu
Sourcepub fn read_holding_registers(start: u16, count: u16) -> Pdu
pub fn read_holding_registers(start: u16, count: u16) -> Pdu
Sourcepub fn read_input_registers(start: u16, count: u16) -> Pdu
pub fn read_input_registers(start: u16, count: u16) -> Pdu
Sourcepub fn write_single_coil(address: u16, on: bool) -> Pdu
pub fn write_single_coil(address: u16, on: bool) -> Pdu
Sourcepub fn write_single_register(address: u16, value: u16) -> Pdu
pub fn write_single_register(address: u16, value: u16) -> Pdu
Sourcepub fn write_multiple_registers(
start: u16,
values: &[u16],
) -> Result<Pdu, ModbusError>
pub fn write_multiple_registers( start: u16, values: &[u16], ) -> Result<Pdu, ModbusError>
Builds a write-multiple-registers request (function 0x10).
§Arguments
start- the address of the first holding register to write.values- the 16-bit values to write to consecutive registers.
§Returns
The request PDU.
§Errors
Returns ModbusError::InvalidValueCount if values is empty or holds more than
MAX_WRITE_REGISTERS values.
Sourcepub fn read_holding_registers_reply(values: &[u16]) -> Result<Pdu, ModbusError>
pub fn read_holding_registers_reply(values: &[u16]) -> Result<Pdu, ModbusError>
Builds the reply a device sends to a read-holding-registers request.
This is the answering half of read_holding_registers:
the function code, the byte count, then the registers big-endian. It lets a client
be written and tested against what a device sends without a device on the line.
§Arguments
values- the register values the device reports, in address order.
§Returns
The reply PDU.
§Errors
Returns ModbusError::InvalidValueCount if values is empty or holds more than
MAX_READ_REGISTERS values.
Sourcepub fn read_input_registers_reply(values: &[u16]) -> Result<Pdu, ModbusError>
pub fn read_input_registers_reply(values: &[u16]) -> Result<Pdu, ModbusError>
Builds the reply a device sends to a read-input-registers request.
This is the answering half of read_input_registers.
§Arguments
values- the register values the device reports, in address order.
§Returns
The reply PDU.
§Errors
Returns ModbusError::InvalidValueCount if values is empty or holds more than
MAX_READ_REGISTERS values.
Sourcepub fn write_multiple_coils(
start: u16,
values: &[bool],
) -> Result<Pdu, ModbusError>
pub fn write_multiple_coils( start: u16, values: &[bool], ) -> Result<Pdu, ModbusError>
Builds a write-multiple-coils request (function 0x0F).
The coils are packed into bytes least-significant bit first, the order Modbus uses; any unused bits in the final byte are left zero.
§Arguments
start- the address of the first coil to write.values- the coil states to write, oneboolper coil.
§Returns
The request PDU.
§Errors
Returns ModbusError::InvalidValueCount if values is empty or holds more than
MAX_WRITE_COILS values.
Sourcepub fn read_coils_reply(values: &[bool]) -> Result<Pdu, ModbusError>
pub fn read_coils_reply(values: &[bool]) -> Result<Pdu, ModbusError>
Builds the reply a device sends to a read-coils request: the function code, the byte count, then the coils packed least-significant bit first.
§Arguments
values- the coil states the device reports, in address order.
§Returns
The reply PDU.
§Errors
Returns ModbusError::InvalidValueCount if values is empty or holds more than
MAX_READ_BITS values.
Sourcepub fn read_discrete_inputs_reply(values: &[bool]) -> Result<Pdu, ModbusError>
pub fn read_discrete_inputs_reply(values: &[bool]) -> Result<Pdu, ModbusError>
Builds the reply a device sends to a read-discrete-inputs request.
§Arguments
values- the input states the device reports, in address order.
§Returns
The reply PDU.
§Errors
Returns ModbusError::InvalidValueCount if values is empty or holds more than
MAX_READ_BITS values.
Sourcepub fn write_multiple_coils_reply(start: u16, quantity: u16) -> Pdu
pub fn write_multiple_coils_reply(start: u16, quantity: u16) -> Pdu
Sourcepub fn write_multiple_registers_reply(start: u16, quantity: u16) -> Pdu
pub fn write_multiple_registers_reply(start: u16, quantity: u16) -> Pdu
Sourcepub fn raw(function: u8, data: &[u8]) -> Result<Pdu, ModbusError>
pub fn raw(function: u8, data: &[u8]) -> Result<Pdu, ModbusError>
Builds a PDU from a raw function code and data, the escape hatch for function codes this crate does not name.
§Arguments
function- the function code byte.data- the bytes that follow it, used verbatim.
§Returns
The PDU.
§Errors
Returns ModbusError::FrameTooLong if the function code plus data would not
fit a PDU (more than MAX_LEN bytes).