modbus_defines.h
4.23 KB
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
101
102
103
104
105
106
107
/****************************************************************************
* Copyright (c) 2022 Priva B.V.
****************************************************************************/
#pragma once
/* Modbus Function codes as defined in the protocol standard */
enum class E_MB_FUNCTION_CODES
{
E_MODBUS_FC_READ_COILS 0x01,
E_MODBUS_FC_READ_DISCRETE_INPUTS 0x02,
E_MODBUS_FC_READ_HOLDING_REGISTERS 0x03,
E_MODBUS_FC_READ_INPUT_REGISTERS 0x04,
E_MODBUS_FC_WRITE_SINGLE_COIL 0x05,
E_MODBUS_FC_WRITE_SINGLE_REGISTER 0x06,
E_MODBUS_FC_READ_EXCEPTION_STATUS 0x07,
E_MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F,
E_MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10,
E_MODBUS_FC_REPORT_SLAVE_ID 0x11,
E_MODBUS_FC_MASK_WRITE_REGISTERS 0x16,
E_MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17
};
#define MODBUS_BROADCAST_ADDRESS 0
/* Modbus Application Protocol V1.1b ( Chapter 6 section 1 page 12 )
* Quantity of Coils to read ( 2 bytes ): 1 to 2000 ( 0x7D0 )
* ( chapter 6 section 11 page 29 )
* Quantity of Coils to write ( 2 bytes ): 1 to 1968 ( 0x7B0 )
*/
#define MODBUS_MAX_READ_BITS 2000
#define MODBUS_MAX_WRITE_BITS 1968
/* Modbus Application Protocol V1.1b ( Chapter 6 section 3 page 12 )
* Quantity of Registers to read ( 2 bytes ): 1 to 125 ( 0x7D )
* ( chapter 6 section 12 page 31 )
* Quantity of Registers to write ( 2 bytes ): 1 to 123 ( 0x7B )
* ( chapter 6 section 17 page 38 )
* Quantity of Registers to write in R/W registers ( 2 bytes ) 1 to 123 ( 0x79 )
*/
#define MODBUS_MAX_READ_REGISTERS 125
#define MODBUS_MAX_WRITE_REGISTERS 123
#define MODBUS_MAX_WR_WRITE_REGISTERS 121
#define MODBUS_MAX_WR_READ_REGISTERS 125
/* The MODBUS PDU is 256 bytes maximum. Therefore MODBUS PDU for serial line
* communication = 256 - Server address ( 1 byte ) - CRC ( 2 bytes ) = 253 bytes
*/
#define MODBUS_MAX_PDU_LENGTH 253
/* Consequently :
* - RTU MODBUS_ADU = 253 bytes + Server address ( 1 byte ) + CRC ( 2 bytes ) = 256
* bytes
* - TCP MODBUS ADU = 253 bytes + MBAP ( 7 bytes ) = 260 bytes
* so the maximum of both backends is 260 bytes. This size can be used to allocate
* an array of bytes to store responses and it will be compatible with the two
* backends.
*/
#define MODBUS_MAX_ADU_LENGTH 260
/* Random number to avoid errno conflicts */
#define MODBUS_ENOBASE 31428571
/* Protocol exceptions */
enum class E_MODBUS_EXCEPTIONS
{
MODBUS_EXCEPTION_ILLEGAL_FUNCTION 0x01,
MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS,
MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE,
MODBUS_EXCEPTION_ACKNOWLEDGE,
MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY,
MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE,
MODBUS_EXCEPTION_MEMORY_PARITY,
MODBUS_EXCEPTION_NOT_DEFINED,
MODBUS_EXCEPTION_GATEWAY_PATH,
MODBUS_EXCEPTION_GATEWAY_TARGET,
MODBUS_EXCEPTION_MAX
};
/* MODBUS Error codes */
#define EMBXILFUN ( MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_FUNCTION )
#define EMBXILADD ( MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS )
#define EMBXILVAL ( MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE )
#define EMBXSFAIL ( MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE )
#define EMBXACK ( MODBUS_ENOBASE + MODBUS_EXCEPTION_ACKNOWLEDGE )
#define EMBXSBUSY ( MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY )
#define EMBXNACK ( MODBUS_ENOBASE + MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE )
#define EMBXMEMPAR ( MODBUS_ENOBASE + MODBUS_EXCEPTION_MEMORY_PARITY )
#define EMBXGPATH ( MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_PATH )
#define EMBXGTAR ( MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_TARGET )
/* Native modbus error codes */
#define EMBBADCRC ( EMBXGTAR + 1 )
#define EMBBADDATA ( EMBXGTAR + 2 )
#define EMBBADEXC ( EMBXGTAR + 3 )
#define EMBUNKEXC ( EMBXGTAR + 4 )
#define EMBMDATA ( EMBXGTAR + 5 )
#define EMBBADSLAVE ( EMBXGTAR + 6 )
/* Recovery Modes */
enum class MODBUS_ERROR_RECOVERY_MODE
{
MODBUS_ERROR_RECOVERY_NONE = 0,
MODBUS_ERROR_RECOVERY_LINK = ( 1 << 1 ),
MODBUS_ERROR_RECOVERY_PROTOCOL = ( 1 << 2 )
};