modbus_rtu/common/
exception.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2#[repr(u8)]
3pub enum Exception {
4    /// An undefined exception code not covered by this crate.
5    Undefined(u8),
6
7    /// The function code received is not supported by the device or is invalid in the current state.
8    IllegalFunction = 0x01,
9
10    /// The requested address range is invalid for the device.
11    IllegalDataAddress = 0x02,
12
13    /// A value in the request is not valid or does not match the expected structure.
14    IllegalDataValue = 0x03,
15
16    /// An unrecoverable device error occurred during processing.
17    DeviceFailure = 0x04,
18
19    /// The request was accepted but requires a long time to complete. Prevents master timeout.
20    Acknowledge = 0x05,
21
22    /// The device is busy processing a long-duration command. Try again later.
23    DeviceBusy = 0x06,
24
25    /// The gateway could not establish a communication path. Check configuration or load.
26    GatewayPathUnavailable = 0x0A,
27
28    /// The gateway received no response from the target device.
29    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}