Crate sawp_modbus

source ·
Expand description

A modbus protocol parser. Given bytes and a sawp::parser::Direction, it will attempt to parse the bytes and return a Message. The parser will inform the caller about what went wrong if no message is returned (see sawp::parser::Parse for details on possible return types).

The following protocol references were used to create this module:

Modbus_V1_1b

PI_MBUS_300

Example

use sawp::parser::{Direction, Parse};
use sawp::error::Error;
use sawp::error::ErrorKind;
use sawp_modbus::{Modbus, Message};

fn parse_bytes(input: &[u8]) -> std::result::Result<&[u8], Error> {
    let modbus = Modbus::default();
    let mut bytes = input;
    while bytes.len() > 0 {
        // If we know that this is a request or response, change the Direction
        // for a more accurate parsing
        match modbus.parse(bytes, Direction::Unknown) {
            // The parser succeeded and returned the remaining bytes and the parsed modbus message
            Ok((rest, Some(message))) => {
                println!("Modbus message: {:?}", message);
                bytes = rest;
            }
            // The parser recognized that this might be modbus and made some progress,
            // but more bytes are needed
            Ok((rest, None)) => return Ok(rest),
            // The parser was unable to determine whether this was modbus or not and more
            // bytes are needed
            Err(Error { kind: ErrorKind::Incomplete(_) }) => return Ok(bytes),
            // The parser determined that this was not modbus
            Err(e) => return Err(e)
        }
    }

    Ok(bytes)
}

Structs

  • Information on the diagnostic subfunction code parsed
  • Information on the exception code parsed
  • Re-export of the Flags struct that is used to represent bit flags in this crate. Storage type for handling flags
  • Information on the function code parsed
  • Information on the mei code parsed
  • Breakdown of the parsed modbus bytes

Enums

Traits

  • Re-export of the Flags struct that is used to represent bit flags in this crate. A trait implemented by all flag enums.