Request

Struct Request 

Source
pub struct Request<'a> { /* private fields */ }
Expand description

Represents an outgoing Modbus RTU request along with its metadata.

Implementations§

Source§

impl<'a> Request<'a>

Source

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));
Source

pub const fn modbus_id(&self) -> u8

Returns the Modbus slave identifier targeted by this request.

Source

pub fn set_modbus_id(&mut self, modbus_id: u8)

Updates the Modbus slave identifier targeted by this request.

Source

pub const fn function(&self) -> &Function

Returns the function payload that will be issued with this request.

Source

pub fn set_function(&mut self, function: &'a Function)

Replaces the function payload associated with this request.

Source

pub const fn timeout(&self) -> Duration

Returns the timeout associated with this request.

Source

pub fn set_timeout(&mut self, timeout: Duration)

Updates the timeout associated with this request.

Source

pub fn is_broadcasting(&self) -> bool

Source

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§

Source§

impl<'a> Clone for Request<'a>

Source§

fn clone(&self) -> Request<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Request<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> PartialEq for Request<'a>

Source§

fn eq(&self, other: &Request<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Eq for Request<'a>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.