modbus_rtu/common/
exception.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2#[repr(u8)]
3pub enum Exception {
4 Undefined(u8),
6
7 IllegalFunction = 0x01,
9
10 IllegalDataAddress = 0x02,
12
13 IllegalDataValue = 0x03,
15
16 DeviceFailure = 0x04,
18
19 Acknowledge = 0x05,
21
22 DeviceBusy = 0x06,
24
25 GatewayPathUnavailable = 0x0A,
27
28 GatewayTargetDeviceFailedToRespond = 0x0B,
30}
31
32
33impl From<Exception> for u8 {
34 fn from(value: Exception) -> Self {
35 match value {
36 Exception::Undefined(c) => c,
37 Exception::IllegalFunction => 0x01,
38 Exception::IllegalDataAddress => 0x02,
39 Exception::IllegalDataValue => 0x03,
40 Exception::DeviceFailure => 0x04,
41 Exception::Acknowledge => 0x05,
42 Exception::DeviceBusy => 0x06,
43 Exception::GatewayPathUnavailable => 0x0A,
44 Exception::GatewayTargetDeviceFailedToRespond => 0x0B,
45 }
46 }
47}
48
49
50impl From<&Exception> for u8 {
51 fn from(value: &Exception) -> Self {
52 match value {
53 Exception::Undefined(c) => *c,
54 Exception::IllegalFunction => 0x01,
55 Exception::IllegalDataAddress => 0x02,
56 Exception::IllegalDataValue => 0x03,
57 Exception::DeviceFailure => 0x04,
58 Exception::Acknowledge => 0x05,
59 Exception::DeviceBusy => 0x06,
60 Exception::GatewayPathUnavailable => 0x0A,
61 Exception::GatewayTargetDeviceFailedToRespond => 0x0B,
62 }
63 }
64}
65
66
67#[test]
68fn test() {
69 let code: u8 = Exception::IllegalFunction.into();
70 assert_eq!(code, 0x01);
71
72 let code: u8 = Exception::Undefined(0x16).into();
73 assert_eq!(code, 0x16);
74}