pub struct FifoQueue { /* private fields */ }Expand description
A collection of register values retrieved from a Modbus FIFO queue.
This structure maintains the pointer address used for the request and stores the
resulting register values in a fixed-size array, making it suitable for no_std
and memory-constrained environments.
§Internal Representation
The queue field is a fixed-size array ([u16; MAX_FIFO_QUEUE_COUNT_PER_PDU])
that stores the 16-bit register values. The length field tracks the actual
number of valid registers currently present in the queue, allowing the struct
to manage a variable number of registers within its fixed capacity.
§Examples
use mbus_core::models::fifo_queue::FifoQueue;
use mbus_core::models::fifo_queue::MAX_FIFO_QUEUE_COUNT_PER_PDU;
// 1. Create a new FifoQueue instance for pointer address 0x1000.
// Initially, it's empty.
let mut fifo = FifoQueue::new(0x1000);
assert_eq!(fifo.ptr_address(), 0x1000);
assert_eq!(fifo.length(), 0);
assert!(fifo.queue().is_empty());
// 2. Simulate receiving a Modbus response with FIFO data.
// Let's say we read 3 registers: 0x1111, 0x2222, 0x3333.
// The `values` array would typically come from parsing the PDU.
let mut received_values = [0; MAX_FIFO_QUEUE_COUNT_PER_PDU];
received_values[0] = 0x1111;
received_values[1] = 0x2222;
received_values[2] = 0x3333;
let received_length = 3;
// 3. Populate the FifoQueue with the received data using `with_values`.
fifo = fifo.with_values(received_values, received_length);
// 4. Verify the contents and properties of the FIFO queue.
assert_eq!(fifo.length(), 3);
assert_eq!(fifo.queue()[..3], [0x1111, 0x2222, 0x3333]);
assert_eq!(fifo.queue()[..1], [0x1111]);
assert_eq!(fifo.queue()[1..2], [0x2222]);
assert_eq!(fifo.queue()[2..3], [0x3333]);Implementations§
Source§impl FifoQueue
impl FifoQueue
Sourcepub fn ptr_address(&self) -> u16
pub fn ptr_address(&self) -> u16
Returns the Modbus address of the FIFO pointer.
This is the address that was provided in the original Read FIFO Queue request.
§Returns
The u16 Modbus address of the FIFO pointer.
Sourcepub fn queue(&self) -> &[u16]
pub fn queue(&self) -> &[u16]
Returns a reference to the active values in the FIFO queue.
This method provides a slice &[u16] containing only the length
valid registers, effectively hiding the unused capacity of the
internal fixed-size array.
§Returns
A slice &[u16] of the register values.
Sourcepub fn length(&self) -> usize
pub fn length(&self) -> usize
Returns the number of registers currently held in this FIFO response.
§Returns
The usize number of registers in the queue.
Sourcepub fn with_values(self, values: [u16; 31], length: usize) -> Self
pub fn with_values(self, values: [u16; 31], length: usize) -> Self
Loads the register values into the FIFO model and sets the active length.
This method is typically used by the service layer after parsing a Modbus response PDU.
§Arguments
values- A fixed-size array containing the 16-bit register values. This array should have a length ofMAX_FIFO_QUEUE_COUNT_PER_PDU.length- The number of registers to be considered active.
§What happens:
- The entire
valuesarray is copied into the internalself.queue. - The
lengthis set, but it is clamped toMAX_FIFO_QUEUE_COUNT_PER_PDUto prevent exceeding the buffer’s capacity.
§Returns
The updated FifoQueue instance.