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;
let pdu = Pdu::write_single_register(0x0001, 0x0003);
assert_eq!(pdu.as_bytes(), &[0x06, 0x00, 0x01, 0x00, 0x03]);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 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_WRITE_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_WRITE_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 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).