oxirs_modbus/
error.rs

1use thiserror::Error;
2
3/// Errors that can occur when working with Modbus protocols
4#[derive(Error, Debug)]
5pub enum ModbusError {
6    /// I/O error during communication
7    #[error("I/O error: {0}")]
8    Io(#[from] std::io::Error),
9
10    /// CRC checksum mismatch (RTU only)
11    #[error("CRC error: expected {expected:04x}, got {actual:04x}")]
12    CrcError {
13        /// Expected CRC value
14        expected: u16,
15        /// Actual CRC value received
16        actual: u16,
17    },
18
19    /// Invalid Modbus function code
20    #[error("Invalid function code: {0}")]
21    InvalidFunctionCode(u8),
22
23    /// Connection timeout
24    #[error("Connection timeout after {0:?}")]
25    Timeout(std::time::Duration),
26
27    /// Invalid register address
28    #[error("Invalid register address: {0} (max: 65535)")]
29    InvalidAddress(u16),
30
31    /// Invalid register count
32    #[error("Invalid register count: {0} (max: 125)")]
33    InvalidCount(u16),
34
35    /// Modbus exception response
36    #[error("Modbus exception: code {code}, function {function}")]
37    ModbusException {
38        /// Exception code (0x01-0x0B)
39        code: u8,
40        /// Function code that caused the exception
41        function: u8,
42    },
43
44    /// Configuration error
45    #[error("Configuration error: {0}")]
46    Config(String),
47
48    /// RDF mapping error
49    #[error("RDF mapping error: {0}")]
50    RdfMapping(String),
51}
52
53/// Result type for Modbus operations
54pub type ModbusResult<T> = Result<T, ModbusError>;