pub fn extract_frame(buf: &[u8], pdu_len: usize) -> Result<Option<DecodedFrame>> {
    let adu_len = 7 + pdu_len;
    if buf.len() >= adu_len {
        let (adu_buf, _next_frame) = buf.split_at(adu_len);
        let (adu_buf, pdu_data) = adu_buf.split_at(7);
        let (transaction_buf, adu_buf) = adu_buf.split_at(2);
        let (protocol_buf, adu_buf) = adu_buf.split_at(2);
        let (length_buf, adu_buf) = adu_buf.split_at(2);
        let protocol_id = BigEndian::read_u16(protocol_buf);
        if protocol_id != 0 {
            return Err(Error::ProtocolNotModbus(protocol_id));
        }
        let transaction = BigEndian::read_u16(transaction_buf);
        let m_length = BigEndian::read_u16(length_buf) as usize;
        let unit = adu_buf[0];
        if m_length != pdu_len + 1 {
            return Err(Error::LengthMismatch(m_length, pdu_len + 1));
        }
        return Ok(Some(DecodedFrame {
            transaction_id: transaction,
            unit_id: unit,
            pdu: pdu_data,
        }));
    }
    // Incomplete frame
    Ok(None)
}
/// Extract a PDU frame out of a buffer.
Extract a PDU frame out of a buffer.
Extract a PDU frame out of a buffer .
pub fn extract_frame(buf: &[u8], pdu_len: usize) -> Result<Option<DecodedFrame>> {
    let adu_len = 7 + pdu_len;
    if buf.len() >= adu_len {
        let (adu_buf, _next_frame) = buf.split_at(adu_len);
        let (adu_buf, pdu_data) = adu_buf.split_at(7);
        let (transaction_buf, adu_buf) = adu_buf.split_at(2);
        let (protocol_buf, adu_buf) = adu_buf.split_at(2);
        let (length_buf, adu_buf) = adu_buf.split_at(2);
        let protocol_id = BigEndian::read_u16(protocol_buf);
        if protocol_id != 0 {
            return Err(Error::ProtocolNotModbus(protocol_id));
        }
        let transaction = BigEndian::read_u16(transaction_buf);
        let m_length = BigEndian::read_u16(length_buf) as usize;
        let unit = adu_buf[0];
        if m_length != pdu_len + 1 {
            return Err(Error::LengthMismatch(m_length, pdu_len + 1));
        }
        return Ok(Some(DecodedFrame {
            transaction_id: transaction,
            unit_id: unit,
            pdu: pdu_data,
        }));
    }
    // Incomplete frame
    Ok(None)
}
pub fn extract_frame ( buf : & [ u8 ] , pdu_len : usize ) -> Result < Option < DecodedFrame > > { let adu_len = 7 + pdu_len ; if buf . len ( ) >= adu_len { let ( adu_buf , _next_frame ) = buf . split_at ( adu_len ) ; let ( adu_buf , pdu_data ) = adu_buf . split_at ( 7 ) ; let ( transaction_buf , adu_buf ) = adu_buf . split_at ( 2 ) ; let ( protocol_buf , adu_buf ) = adu_buf . split_at ( 2 ) ; let ( length_buf , adu_buf ) = adu_buf . split_at ( 2 ) ; let protocol_id = BigEndian :: read_u16 ( protocol_buf ) ; if protocol_id != 0 { return Err ( Error :: ProtocolNotModbus ( protocol_id ) ) ; } let transaction = BigEndian :: read_u16 ( transaction_buf ) ; let m_length = BigEndian :: read_u16 ( length_buf ) as usize ; let unit = adu_buf [ 0 ] ; if m_length != pdu_len + 1 { return Err ( Error :: LengthMismatch ( m_length , pdu_len + 1 ) ) ; } return Ok ( Some ( DecodedFrame { transaction_id : transaction , unit_id : unit , pdu : pdu_data , } ) ) ; } // Incomplete frame Ok ( None ) }
Extract a PDU frame out of a buffer.
Extract a PDU frame out of a buffer .
buf
&[u8]
pdu_len
usize
buf
&[u8]
pdu_len
usize