pub struct Header {
pub transaction_id: u16,
pub protocol_id: u16,
pub length: u16,
pub unit_id: UnitId,
}Expand description
Modbus Application Protocol (Data Unit) header aka «MBAP header».
Fields§
§transaction_id: u16Transaction ID used to match responses with requests.
protocol_id: u16Protocol ID. Always 0 for Modbus.
length: u16Number of following codec, including the Unit Identifier and data fields.
unit_id: UnitIdUnit identifier aka «slave ID».
Identification of a remote slave connected on a serial line or on other buses.
Implementations§
Source§impl Header
impl Header
pub const N_BYTES: usize = 7
Sourcepub const fn payload_length(&self) -> u16
pub const fn payload_length(&self) -> u16
Expected PDU length.
TCP transport implementation should read exactly this number of codec
and parse as crate::protocol::Response.
Trait Implementations§
Source§impl Decode for Header
impl Decode for Header
Source§fn decode(from: &mut impl Buf) -> Result<Self, Error>
fn decode(from: &mut impl Buf) -> Result<Self, Error>
Decode a header.
§Example
use fennec_modbus::{
protocol::codec::Decode,
tcp::{Header, UnitId},
};
let mut bytes: &[u8] = &[
0x15, 0x01, // transaction ID: high, low
0x00, 0x00, // protocol ID
0x00, 0x06, // length
0xFF, // unit ID
];
let header = Header::decode(&mut bytes).unwrap();
assert_eq!(header.transaction_id, 0x1501);
assert_eq!(header.protocol_id, 0);
assert_eq!(header.unit_id, UnitId::NonSignificant);Source§impl Encode for Header
impl Encode for Header
Source§fn encode(&self, buf: &mut impl BufMut)
fn encode(&self, buf: &mut impl BufMut)
Encode the header.
§Example
use fennec_modbus::{
protocol::codec::Encode,
tcp::{Header, UnitId, header},
};
const EXPECTED: &[u8] = &[
0x15, 0x01, // transaction ID: high, low
0x00, 0x00, // protocol ID
0x00, 0x06, // length
0xFF, // unit ID
];
let header = Header {
unit_id: UnitId::NonSignificant,
transaction_id: 0x1501,
length: 6,
protocol_id: 0,
};
let mut bytes = Vec::new();
header.encode(&mut bytes);
assert_eq!(bytes, EXPECTED);fn to_bytes(&self) -> Vec<u8> ⓘ
Auto Trait Implementations§
impl Freeze for Header
impl RefUnwindSafe for Header
impl Send for Header
impl Sync for Header
impl Unpin for Header
impl UnsafeUnpin for Header
impl UnwindSafe for Header
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