1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use num_derive::FromPrimitive;
use std::fmt;
use std::io;
#[derive(Debug, PartialEq, FromPrimitive)]
pub enum ExceptionCode {
IllegalFunction = 0x01,
IllegalDataAddress = 0x02,
IllegalDataValue = 0x03,
SlaveOrServerFailure = 0x04,
Acknowledge = 0x05,
SlaveOrServerBusy = 0x06,
NegativeAcknowledge = 0x07,
MemoryParity = 0x08,
NotDefined = 0x09,
GatewayPath = 0x0a,
GatewayTarget = 0x0b
}
#[derive(Debug, PartialEq)]
pub enum Reason {
UnexpectedReplySize,
BytecountNotEven,
SendBufferEmpty,
RecvBufferEmpty,
SendBufferTooBig,
DecodingError,
EncodingError,
InvalidByteorder,
InvalidRequestParameter,
Custom(String),
}
#[derive(Debug)]
pub enum Error {
Exception(ExceptionCode),
Io(io::Error),
InvalidResponse,
InvalidData(Reason),
InvalidFunction,
ParseCoilError,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Error::*;
match *self {
Exception(ref code) => write!(f, "modbus exception: {:?}", code),
Io(ref err) => write!(f, "I/O error: {}", err),
InvalidResponse => write!(f, "invalid response"),
InvalidData(ref reason) => write!(f, "invalid data: {:?}", reason),
InvalidFunction => write!(f, "invalid modbus function"),
ParseCoilError => write!(f, "parse coil could not be parsed"),
}
}
}
impl std::error::Error for Error {
fn description(&self) -> &str {
use Error::*;
match *self {
Exception(_) => "modbus exception",
Io(_) => "I/O error",
InvalidResponse => "invalid response",
InvalidData(_) => "invalid data",
InvalidFunction => "invalid modbus function",
ParseCoilError => "parse coil could not be parsed",
}
}
fn cause(&self) -> Option<&dyn std::error::Error> {
match *self {
Error::Io(ref err) => Some(err),
_ => None,
}
}
}
impl From<ExceptionCode> for Error {
fn from(err: ExceptionCode) -> Error {
Error::Exception(err)
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::Io(err)
}
}
pub type Result<T> = std::result::Result<T, Error>;