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:
§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§
- 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.
- Function Code Categories as stated in the protocol reference
- Represents the various fields found in the PDU
- Subfunction code names as stated in the protocol reference
- 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.
- Exception code names as stated in the protocol reference
- Function code names as stated in the protocol reference
- MEI function code names as stated in the protocol reference
- Read information on parsed in function data
- Write information on parsed in function data
Traits§
- Re-export of the
Flags
struct that is used to represent bit flags in this crate. A trait implemented by all flag enums.