pub struct Request<'a> { /* private fields */ }Expand description
Represents an outgoing Modbus RTU request along with its metadata.
Implementations§
Source§impl<'a> Request<'a>
impl<'a> Request<'a>
Sourcepub const fn new(
modbus_id: u8,
function: &'a Function,
timeout: Duration,
) -> Self
pub const fn new( modbus_id: u8, function: &'a Function, timeout: Duration, ) -> Self
Creates a new request for the specified Modbus slave, function, and timeout.
§Examples
use modbus_rtu::{Function, Request};
let func = Function::ReadCoils { starting_address: 0x0000, quantity: 2 };
let request = Request::new(0x01, &func, std::time::Duration::from_millis(200));
assert_eq!(request.modbus_id(), 0x01);
assert_eq!(request.timeout(), std::time::Duration::from_millis(200));Sourcepub const fn modbus_id(&self) -> u8
pub const fn modbus_id(&self) -> u8
Returns the Modbus slave identifier targeted by this request.
Sourcepub fn set_modbus_id(&mut self, modbus_id: u8)
pub fn set_modbus_id(&mut self, modbus_id: u8)
Updates the Modbus slave identifier targeted by this request.
Sourcepub const fn function(&self) -> &Function
pub const fn function(&self) -> &Function
Returns the function payload that will be issued with this request.
Sourcepub fn set_function(&mut self, function: &'a Function)
pub fn set_function(&mut self, function: &'a Function)
Replaces the function payload associated with this request.
Sourcepub fn set_timeout(&mut self, timeout: Duration)
pub fn set_timeout(&mut self, timeout: Duration)
Updates the timeout associated with this request.
pub fn is_broadcasting(&self) -> bool
Sourcepub fn to_bytes(&self) -> Result<Box<[u8]>, RequestPacketError>
pub fn to_bytes(&self) -> Result<Box<[u8]>, RequestPacketError>
Serializes the request into a Modbus RTU frame containing the device id, function payload, and CRC footer.
§Errors
Returns RequestPacketError if the inner
function cannot be encoded within the 256-byte packet limit.
§Examples
use modbus_rtu::{Function, Request};
let func = Function::WriteSingleRegister { address: 0x0010, value: 0xABCD };
let request = Request::new(0x11, &func, std::time::Duration::from_millis(100));
let frame = request.to_bytes().unwrap();
assert_eq!(&frame[..], &[0x11, 0x06, 0x00, 0x10, 0xAB, 0xCD, 0x34, 0x3A]);Trait Implementations§
impl<'a> Eq for Request<'a>
impl<'a> StructuralPartialEq for Request<'a>
Auto Trait Implementations§
impl<'a> Freeze for Request<'a>
impl<'a> RefUnwindSafe for Request<'a>
impl<'a> Send for Request<'a>
impl<'a> Sync for Request<'a>
impl<'a> Unpin for Request<'a>
impl<'a> UnwindSafe for Request<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more