sawp-modbus 0.1.1-rc.2

SAWP Protocol Parser for Modbus
Documentation

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)
}