Skip to content

Commit 9611c5f

Browse files
authored
Add all function codes defined in the protocol specification (#284)
1 parent 1d78fe6 commit 9611c5f

File tree

2 files changed

+64
-18
lines changed

2 files changed

+64
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
### Breaking Changes
1212

13-
- Added `FunctionCode::ReportServerId`.
13+
- Added all `FunctionCode`s defined in the protocol specification.
1414
- Renamed `Exception` to `ExceptionCode` to be more consistent with
1515
`FunctionCode`.
1616
- Added `ExceptionCode::Custom`.

src/frame/mod.rs

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,67 @@ use std::{
1616
use crate::bytes::Bytes;
1717

1818
/// A Modbus function code.
19+
///
20+
/// All function codes as defined by the protocol specification V1.1b3.
1921
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2022
pub enum FunctionCode {
21-
/// Modbus Function Code: `01` (`0x01`).
23+
/// 01 (0x01) Read Coils.
2224
ReadCoils,
23-
/// Modbus Function Code: `02` (`0x02`).
25+
26+
/// 02 (0x02) Read Discrete Inputs
2427
ReadDiscreteInputs,
2528

26-
/// Modbus Function Code: `05` (`0x05`).
29+
/// 03 (0x03) Read Holding Registers
30+
ReadHoldingRegisters,
31+
32+
/// 04 (0x04) Read Input Registers
33+
ReadInputRegisters,
34+
35+
/// 05 (0x05) Write Single Coil
2736
WriteSingleCoil,
28-
/// Modbus Function Code: `06` (`0x06`).
37+
38+
/// 06 (0x06) Write Single Register
2939
WriteSingleRegister,
3040

31-
/// Modbus Function Code: `03` (`0x03`).
32-
ReadHoldingRegisters,
33-
/// Modbus Function Code: `04` (`0x04`).
34-
ReadInputRegisters,
41+
/// 07 (0x07) Read Exception Status (Serial Line only)
42+
ReadExceptionStatus,
43+
44+
/// 08 (0x08) Diagnostics (Serial Line only)
45+
Diagnostics,
3546

36-
/// Modbus Function Code: `15` (`0x0F`).
47+
/// 11 (0x0B) Get Comm Event Counter (Serial Line only)
48+
GetCommEventCounter,
49+
50+
/// 12 (0x0C) Get Comm Event Log (Serial Line only)
51+
GetCommEventLog,
52+
53+
/// 15 (0x0F) Write Multiple Coils
3754
WriteMultipleCoils,
38-
/// Modbus Function Code: `16` (`0x10`).
55+
56+
/// 16 (0x10) Write Multiple Registers
3957
WriteMultipleRegisters,
4058

41-
/// Modbus Function Code: `17` (`0x11`).
59+
/// 17 (0x11) Report Slave ID (Serial Line only)
4260
ReportServerId,
4361

44-
/// Modbus Function Code: `22` (`0x16`).
62+
/// 20 (0x14) Read File Record
63+
ReadFileRecord,
64+
65+
/// 21 (0x15) Write File Record
66+
WriteFileRecord,
67+
68+
/// 22 (0x16) Mask Write Register
4569
MaskWriteRegister,
4670

47-
/// Modbus Function Code: `23` (`0x17`).
71+
/// 23 (0x17) Read/Write Multiple Registers
4872
ReadWriteMultipleRegisters,
4973

74+
/// 24 (0x18) Read FIFO Queue
75+
ReadFifoQueue,
76+
77+
/// 43 ( 0x2B) Encapsulated Interface Transport
78+
EncapsulatedInterfaceTransport,
79+
5080
/// Custom Modbus Function Code.
5181
Custom(u8),
5282

@@ -60,15 +90,23 @@ impl FunctionCode {
6090
match value {
6191
0x01 => Self::ReadCoils,
6292
0x02 => Self::ReadDiscreteInputs,
63-
0x05 => Self::WriteSingleCoil,
64-
0x06 => Self::WriteSingleRegister,
6593
0x03 => Self::ReadHoldingRegisters,
6694
0x04 => Self::ReadInputRegisters,
95+
0x05 => Self::WriteSingleCoil,
96+
0x06 => Self::WriteSingleRegister,
97+
0x07 => Self::ReadExceptionStatus,
98+
0x08 => Self::Diagnostics,
99+
0x0B => Self::GetCommEventCounter,
100+
0x0C => Self::GetCommEventLog,
67101
0x0F => Self::WriteMultipleCoils,
68102
0x10 => Self::WriteMultipleRegisters,
69103
0x11 => Self::ReportServerId,
104+
0x14 => Self::ReadFileRecord,
105+
0x15 => Self::WriteFileRecord,
70106
0x16 => Self::MaskWriteRegister,
71107
0x17 => Self::ReadWriteMultipleRegisters,
108+
0x18 => Self::ReadFifoQueue,
109+
0x2B => Self::EncapsulatedInterfaceTransport,
72110
code => Self::Custom(code),
73111
}
74112
}
@@ -83,15 +121,23 @@ impl FunctionCode {
83121
match self {
84122
Self::ReadCoils => 0x01,
85123
Self::ReadDiscreteInputs => 0x02,
86-
Self::WriteSingleCoil => 0x05,
87-
Self::WriteSingleRegister => 0x06,
88124
Self::ReadHoldingRegisters => 0x03,
89125
Self::ReadInputRegisters => 0x04,
126+
Self::WriteSingleCoil => 0x05,
127+
Self::WriteSingleRegister => 0x06,
128+
Self::ReadExceptionStatus => 0x07,
129+
Self::Diagnostics => 0x08,
130+
Self::GetCommEventCounter => 0x0B,
131+
Self::GetCommEventLog => 0x0C,
90132
Self::WriteMultipleCoils => 0x0F,
91133
Self::WriteMultipleRegisters => 0x10,
92134
Self::ReportServerId => 0x11,
135+
Self::ReadFileRecord => 0x14,
136+
Self::WriteFileRecord => 0x15,
93137
Self::MaskWriteRegister => 0x16,
94138
Self::ReadWriteMultipleRegisters => 0x17,
139+
Self::ReadFifoQueue => 0x18,
140+
Self::EncapsulatedInterfaceTransport => 0x2B,
95141
Self::Custom(code) => code,
96142
Self::Disconnect => unreachable!(),
97143
}

0 commit comments

Comments
 (0)