Commit f639d14fb8b9ffbc289a9c9058a879c3f8d63d66
0 parents
Initial setup
Showing
1 changed file
with
107 additions
and
0 deletions
src/modbus_defines.h
0 → 100644
| 1 | +++ a/src/modbus_defines.h | ||
| 1 | +/**************************************************************************** | ||
| 2 | + * Copyright (c) 2022 Priva B.V. | ||
| 3 | + ****************************************************************************/ | ||
| 4 | + | ||
| 5 | +#pragma once | ||
| 6 | + | ||
| 7 | +/* Modbus Function codes as defined in the protocol standard */ | ||
| 8 | +enum class E_MB_FUNCTION_CODES | ||
| 9 | +{ | ||
| 10 | + E_MODBUS_FC_READ_COILS 0x01, | ||
| 11 | + E_MODBUS_FC_READ_DISCRETE_INPUTS 0x02, | ||
| 12 | + E_MODBUS_FC_READ_HOLDING_REGISTERS 0x03, | ||
| 13 | + E_MODBUS_FC_READ_INPUT_REGISTERS 0x04, | ||
| 14 | + E_MODBUS_FC_WRITE_SINGLE_COIL 0x05, | ||
| 15 | + E_MODBUS_FC_WRITE_SINGLE_REGISTER 0x06, | ||
| 16 | + E_MODBUS_FC_READ_EXCEPTION_STATUS 0x07, | ||
| 17 | + E_MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F, | ||
| 18 | + E_MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10, | ||
| 19 | + E_MODBUS_FC_REPORT_SLAVE_ID 0x11, | ||
| 20 | + E_MODBUS_FC_MASK_WRITE_REGISTERS 0x16, | ||
| 21 | + E_MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17 | ||
| 22 | +}; | ||
| 23 | + | ||
| 24 | +#define MODBUS_BROADCAST_ADDRESS 0 | ||
| 25 | + | ||
| 26 | +/* Modbus Application Protocol V1.1b ( Chapter 6 section 1 page 12 ) | ||
| 27 | + * Quantity of Coils to read ( 2 bytes ): 1 to 2000 ( 0x7D0 ) | ||
| 28 | + * ( chapter 6 section 11 page 29 ) | ||
| 29 | + * Quantity of Coils to write ( 2 bytes ): 1 to 1968 ( 0x7B0 ) | ||
| 30 | + */ | ||
| 31 | +#define MODBUS_MAX_READ_BITS 2000 | ||
| 32 | +#define MODBUS_MAX_WRITE_BITS 1968 | ||
| 33 | + | ||
| 34 | +/* Modbus Application Protocol V1.1b ( Chapter 6 section 3 page 12 ) | ||
| 35 | + * Quantity of Registers to read ( 2 bytes ): 1 to 125 ( 0x7D ) | ||
| 36 | + * ( chapter 6 section 12 page 31 ) | ||
| 37 | + * Quantity of Registers to write ( 2 bytes ): 1 to 123 ( 0x7B ) | ||
| 38 | + * ( chapter 6 section 17 page 38 ) | ||
| 39 | + * Quantity of Registers to write in R/W registers ( 2 bytes ) 1 to 123 ( 0x79 ) | ||
| 40 | + */ | ||
| 41 | +#define MODBUS_MAX_READ_REGISTERS 125 | ||
| 42 | +#define MODBUS_MAX_WRITE_REGISTERS 123 | ||
| 43 | +#define MODBUS_MAX_WR_WRITE_REGISTERS 121 | ||
| 44 | +#define MODBUS_MAX_WR_READ_REGISTERS 125 | ||
| 45 | + | ||
| 46 | +/* The MODBUS PDU is 256 bytes maximum. Therefore MODBUS PDU for serial line | ||
| 47 | + * communication = 256 - Server address ( 1 byte ) - CRC ( 2 bytes ) = 253 bytes | ||
| 48 | + */ | ||
| 49 | +#define MODBUS_MAX_PDU_LENGTH 253 | ||
| 50 | + | ||
| 51 | +/* Consequently : | ||
| 52 | + * - RTU MODBUS_ADU = 253 bytes + Server address ( 1 byte ) + CRC ( 2 bytes ) = 256 | ||
| 53 | + * bytes | ||
| 54 | + * - TCP MODBUS ADU = 253 bytes + MBAP ( 7 bytes ) = 260 bytes | ||
| 55 | + * so the maximum of both backends is 260 bytes. This size can be used to allocate | ||
| 56 | + * an array of bytes to store responses and it will be compatible with the two | ||
| 57 | + * backends. | ||
| 58 | + */ | ||
| 59 | +#define MODBUS_MAX_ADU_LENGTH 260 | ||
| 60 | + | ||
| 61 | +/* Random number to avoid errno conflicts */ | ||
| 62 | +#define MODBUS_ENOBASE 31428571 | ||
| 63 | + | ||
| 64 | +/* Protocol exceptions */ | ||
| 65 | +enum class E_MODBUS_EXCEPTIONS | ||
| 66 | +{ | ||
| 67 | + MODBUS_EXCEPTION_ILLEGAL_FUNCTION 0x01, | ||
| 68 | + MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS, | ||
| 69 | + MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, | ||
| 70 | + MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE, | ||
| 71 | + MODBUS_EXCEPTION_ACKNOWLEDGE, | ||
| 72 | + MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY, | ||
| 73 | + MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE, | ||
| 74 | + MODBUS_EXCEPTION_MEMORY_PARITY, | ||
| 75 | + MODBUS_EXCEPTION_NOT_DEFINED, | ||
| 76 | + MODBUS_EXCEPTION_GATEWAY_PATH, | ||
| 77 | + MODBUS_EXCEPTION_GATEWAY_TARGET, | ||
| 78 | + MODBUS_EXCEPTION_MAX | ||
| 79 | +}; | ||
| 80 | + | ||
| 81 | +/* MODBUS Error codes */ | ||
| 82 | +#define EMBXILFUN ( MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_FUNCTION ) | ||
| 83 | +#define EMBXILADD ( MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS ) | ||
| 84 | +#define EMBXILVAL ( MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE ) | ||
| 85 | +#define EMBXSFAIL ( MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE ) | ||
| 86 | +#define EMBXACK ( MODBUS_ENOBASE + MODBUS_EXCEPTION_ACKNOWLEDGE ) | ||
| 87 | +#define EMBXSBUSY ( MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY ) | ||
| 88 | +#define EMBXNACK ( MODBUS_ENOBASE + MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE ) | ||
| 89 | +#define EMBXMEMPAR ( MODBUS_ENOBASE + MODBUS_EXCEPTION_MEMORY_PARITY ) | ||
| 90 | +#define EMBXGPATH ( MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_PATH ) | ||
| 91 | +#define EMBXGTAR ( MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_TARGET ) | ||
| 92 | + | ||
| 93 | +/* Native modbus error codes */ | ||
| 94 | +#define EMBBADCRC ( EMBXGTAR + 1 ) | ||
| 95 | +#define EMBBADDATA ( EMBXGTAR + 2 ) | ||
| 96 | +#define EMBBADEXC ( EMBXGTAR + 3 ) | ||
| 97 | +#define EMBUNKEXC ( EMBXGTAR + 4 ) | ||
| 98 | +#define EMBMDATA ( EMBXGTAR + 5 ) | ||
| 99 | +#define EMBBADSLAVE ( EMBXGTAR + 6 ) | ||
| 100 | + | ||
| 101 | +/* Recovery Modes */ | ||
| 102 | +enum class MODBUS_ERROR_RECOVERY_MODE | ||
| 103 | +{ | ||
| 104 | + MODBUS_ERROR_RECOVERY_NONE = 0, | ||
| 105 | + MODBUS_ERROR_RECOVERY_LINK = ( 1 << 1 ), | ||
| 106 | + MODBUS_ERROR_RECOVERY_PROTOCOL = ( 1 << 2 ) | ||
| 107 | +}; |