pub fn extract_frame(buf: &[u8], pdu_len: usize) -> Result<Option<DecodedFrame>> {
    let adu_len = 1 + pdu_len;
    if buf.len() >= adu_len + 2 {
        let (adu_buf, buf) = buf.split_at(adu_len);
        let (crc_buf, _) = buf.split_at(2);
        // Read trailing CRC and verify ADU
        let expected_crc = BigEndian::read_u16(crc_buf);
        let actual_crc = crc16(adu_buf);
        if expected_crc != actual_crc {
            return Err(Error::Crc(expected_crc, actual_crc));
        }
        let (slave_id, pdu_data) = adu_buf.split_at(1);
        let slave_id = slave_id[0];
        return Ok(Some(DecodedFrame {
            slave: slave_id,
            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 = 1 + pdu_len;
    if buf.len() >= adu_len + 2 {
        let (adu_buf, buf) = buf.split_at(adu_len);
        let (crc_buf, _) = buf.split_at(2);
        // Read trailing CRC and verify ADU
        let expected_crc = BigEndian::read_u16(crc_buf);
        let actual_crc = crc16(adu_buf);
        if expected_crc != actual_crc {
            return Err(Error::Crc(expected_crc, actual_crc));
        }
        let (slave_id, pdu_data) = adu_buf.split_at(1);
        let slave_id = slave_id[0];
        return Ok(Some(DecodedFrame {
            slave: slave_id,
            pdu: pdu_data,
        }));
    }
    // Incomplete frame
    Ok(None)
}
pub fn extract_frame ( buf : & [ u8 ] , pdu_len : usize ) -> Result < Option < DecodedFrame > > { let adu_len = 1 + pdu_len ; if buf . len ( ) >= adu_len + 2 { let ( adu_buf , buf ) = buf . split_at ( adu_len ) ; let ( crc_buf , _ ) = buf . split_at ( 2 ) ; // Read trailing CRC and verify ADU let expected_crc = BigEndian :: read_u16 ( crc_buf ) ; let actual_crc = crc16 ( adu_buf ) ; if expected_crc != actual_crc { return Err ( Error :: Crc ( expected_crc , actual_crc ) ) ; } let ( slave_id , pdu_data ) = adu_buf . split_at ( 1 ) ; let slave_id = slave_id [ 0 ] ; return Ok ( Some ( DecodedFrame { slave : slave_id , 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