Crate sawp_modbus[][src]

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 {};
    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

Diagnostic

Information on the diagnostic subfunction code parsed

Exception

Information on the exception code parsed

Flags

Re-export of the Flags struct that is used to represent bit flags in this crate.

Function

Information on the function code parsed

MEI

Information on the mei code parsed

Message

Breakdown of the parsed modbus bytes

Modbus

Enums

AccessType

Function code groups based on general use. Allows for easier parsing of certain functions, since generally most functions in a group will have the same request/response structure.

CodeCategory

Function Code Categories as stated in the protocol reference

Data

Represents the various fields found in the PDU

DiagnosticSubfunction

Subfunction code names as stated in the protocol reference

ErrorFlags

Flags which identify messages which parse as modbus but contain invalid data. The caller can use the message’s error flags to see if and what errors were in the pack of bytes and take action using this information.

ExceptionCode

Exception code names as stated in the protocol reference

FunctionCode

Function code names as stated in the protocol reference

MEIType

MEI function code names as stated in the protocol reference

Read

Read information on parsed in function data

Write

Write information on parsed in function data

Traits

Flag

Re-export of the Flags struct that is used to represent bit flags in this crate.