ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
modbus_definitions.h
Go to the documentation of this file.
1#pragma once
2
5
6namespace esphome::modbus {
7
10// 5 Function Code Categories
11const uint8_t FUNCTION_CODE_USER_DEFINED_SPACE_1_INIT = 65; // 0x41
12const uint8_t FUNCTION_CODE_USER_DEFINED_SPACE_1_END = 72; // 0x48
13
14const uint8_t FUNCTION_CODE_USER_DEFINED_SPACE_2_INIT = 100; // 0x64
15const uint8_t FUNCTION_CODE_USER_DEFINED_SPACE_2_END = 110; // 0x6E
16
17enum class ModbusFunctionCode : uint8_t {
18 INVALID = 0x00, // 0x00 is not a valid function code (even for custom functions).
19 CUSTOM = 0x00, // The CUSTOM alias should be removed in future.
20 READ_COILS = 0x01,
24 WRITE_SINGLE_COIL = 0x05,
26 READ_EXCEPTION_STATUS = 0x07, // not implemented
27 DIAGNOSTICS = 0x08, // not implemented
28 GET_COMM_EVENT_COUNTER = 0x0B, // not implemented
29 GET_COMM_EVENT_LOG = 0x0C, // not implemented
32 REPORT_SERVER_ID = 0x11, // not implemented
33 READ_FILE_RECORD = 0x14, // not implemented
34 WRITE_FILE_RECORD = 0x15, // not implemented
35 MASK_WRITE_REGISTER = 0x16, // not implemented
36 READ_WRITE_MULTIPLE_REGISTERS = 0x17, // not implemented
37 READ_FIFO_QUEUE = 0x18, // not implemented
38};
39
40/*Allow direct comparison operators between ModbusFunctionCode and uint8_t*/
41inline bool operator==(ModbusFunctionCode lhs, uint8_t rhs) { return static_cast<uint8_t>(lhs) == rhs; }
42inline bool operator==(uint8_t lhs, ModbusFunctionCode rhs) { return lhs == static_cast<uint8_t>(rhs); }
43inline bool operator!=(ModbusFunctionCode lhs, uint8_t rhs) { return !(static_cast<uint8_t>(lhs) == rhs); }
44inline bool operator!=(uint8_t lhs, ModbusFunctionCode rhs) { return !(lhs == static_cast<uint8_t>(rhs)); }
45
46// 4.3 MODBUS Data model
47enum class ModbusRegisterType : uint8_t {
48 CUSTOM = 0x00,
49 COIL = 0x01,
50 DISCRETE_INPUT = 0x02,
51 HOLDING = 0x03,
52 // Named INPUT_REGISTER (not INPUT) because Arduino cores define INPUT as a macro.
53 INPUT_REGISTER = 0x04,
54 // Remove before 2027.2.0
55 READ ESPDEPRECATED("Use ModbusRegisterType::INPUT_REGISTER instead. Removed in 2027.2.0", "2026.7.0") =
57};
58
59// 7 MODBUS Exception Responses:
60const uint8_t FUNCTION_CODE_MASK = 0x7F;
61const uint8_t FUNCTION_CODE_EXCEPTION_MASK = 0x80;
62
74
75// 6.12 16 (0x10) Write Multiple registers:
76static constexpr uint16_t MAX_NUM_OF_REGISTERS_TO_WRITE = 123; // 0x7B
77
78// 6.1 01 (0x01) Read Coils
79// 6.2 02 (0x02) Read Discrete Inputs
80static constexpr uint16_t MAX_NUM_OF_COILS_TO_READ = 2000; // 0x7D0
81static constexpr uint16_t MAX_NUM_OF_DISCRETE_INPUTS_TO_READ = 2000; // 0x7D0
82
83// 6.3 03 (0x03) Read Holding Registers
84// 6.4 04 (0x04) Read Input Registers
85static constexpr uint16_t MAX_NUM_OF_REGISTERS_TO_READ = 125; // 0x7D
86
87// Smallest possible frame is 4 bytes (custom function with no data): address(1) + function(1) + CRC(2)
88static constexpr uint16_t MIN_FRAME_SIZE = 4;
89static constexpr uint16_t MAX_PDU_SIZE = 253; // Max PDU size is 256 - address(1) - CRC(2) = 253
90static constexpr uint16_t MAX_RAW_SIZE = 254; // Max RAW size is 256 - CRC(2) = 254
91static constexpr uint16_t MAX_FRAME_SIZE = 256;
93} // namespace esphome::modbus
const uint8_t FUNCTION_CODE_MASK
const uint8_t FUNCTION_CODE_EXCEPTION_MASK
const uint8_t FUNCTION_CODE_USER_DEFINED_SPACE_2_INIT
const uint8_t FUNCTION_CODE_USER_DEFINED_SPACE_1_INIT
Modbus definitions from specs: https://modbus.org/docs/Modbus_Application_Protocol_V1_1b3....
const uint8_t FUNCTION_CODE_USER_DEFINED_SPACE_2_END
const uint8_t FUNCTION_CODE_USER_DEFINED_SPACE_1_END
bool operator!=(ModbusFunctionCode lhs, uint8_t rhs)
bool operator==(ModbusFunctionCode lhs, uint8_t rhs)