forked from manbo/modbus-parser
83 lines
3.1 KiB
Markdown
83 lines
3.1 KiB
Markdown
# Modbus Types API Documentation
|
|
|
|
This document provides API documentation for the public types and functions in the `modbus.rs` file.
|
|
|
|
## Structs
|
|
|
|
### Config
|
|
Configuration struct for Modbus function descriptors.
|
|
|
|
#### Fields
|
|
- `functions`: `Vec<FunctionDescriptor>` - A vector of function descriptors defining the Modbus functions to be parsed.
|
|
|
|
### FunctionDescriptor
|
|
Describes a Modbus function with its associated field descriptors.
|
|
|
|
#### Fields
|
|
- `function_code`: `u8` - The Modbus function code (1-255).
|
|
- `name`: `Option<String>` - Optional human-readable name for the function.
|
|
- `request`: `Option<Vec<FieldDescriptor>>` - Optional vector of field descriptors for the request message.
|
|
- `response`: `Option<Vec<FieldDescriptor>>` - Optional vector of field descriptors for the response message.
|
|
|
|
### FieldDescriptor
|
|
Describes a field within a Modbus message.
|
|
|
|
#### Fields
|
|
- `name`: `String` - The name of the field.
|
|
- `ty`: `FieldType` - The type of the field.
|
|
- `length`: `Option<usize>` - Optional length for byte fields (defaults to None).
|
|
- `length_from`: `Option<String>` - Optional field name to read length from (defaults to None).
|
|
- `scale`: `Option<f64>` - Optional scaling factor for numeric values (defaults to None).
|
|
- `enum_map`: `Option<HashMap<u64, String>>` - Optional mapping of raw values to enum names (defaults to None).
|
|
|
|
## Enums
|
|
|
|
### FieldType
|
|
Defines the data types for Modbus message fields.
|
|
|
|
#### Variants
|
|
- `U8` - Unsigned 8-bit integer
|
|
- `U16` - Unsigned 16-bit integer
|
|
- `U32` - Unsigned 32-bit integer
|
|
- `I16` - Signed 16-bit integer
|
|
- `Bytes` - Byte array with specified length
|
|
- `Rest` - Remaining bytes in the message
|
|
|
|
## Type Aliases
|
|
|
|
### FuncMap
|
|
A type alias for a HashMap that maps Modbus function codes (u8) to their corresponding FunctionDescriptor.
|
|
|
|
```rust
|
|
type FuncMap = HashMap<u8, FunctionDescriptor>;
|
|
```
|
|
|
|
## Functions
|
|
|
|
### parse_sawp_message
|
|
Parses a Modbus message using a function descriptor map and returns a JSON value representation.
|
|
|
|
```rust
|
|
pub fn parse_sawp_message(
|
|
msg: &Message,
|
|
map: &FuncMap,
|
|
is_response: bool,
|
|
) -> Result<Value, String>
|
|
```
|
|
|
|
#### Parameters
|
|
- `msg`: `&Message` - Reference to the Modbus message to parse
|
|
- `map`: `&FuncMap` - Reference to the function map containing field descriptors
|
|
- `is_response`: `bool` - Flag indicating if the message is a response (true) or request (false)
|
|
|
|
#### Returns
|
|
- `Result<Value, String>` - On success, returns a JSON Value representing the parsed message; on failure, returns an error string
|
|
|
|
#### Description
|
|
This function takes a Modbus message and parses it according to the provided function descriptor map. It handles both requests and responses, and properly handles exception messages. The function extracts the function code from the message and looks up the appropriate field descriptors in the map. It then parses the message payload according to the field descriptors and returns a JSON representation of the parsed data.
|
|
|
|
The resulting JSON object contains:
|
|
- `unit`: The unit ID from the message
|
|
- `function`: The function code
|
|
- `exception`: Present only if the message is an exception response
|
|
- `fields`: An object containing the parsed field values |