diff --git a/src/modbus.c b/src/modbus.c index 8d15e92..36f1c51 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -146,6 +146,11 @@ static const int TAB_CHECKSUM_LENGTH[2] = { CHECKSUM_LENGTH_TCP }; +static const int TAB_MAX_ADU_LENGTH[2] = { + MAX_ADU_LENGTH_RTU, + MAX_ADU_LENGTH_TCP, +}; + /* Treats errors and flush or close connection if necessary */ static void error_treat(modbus_param_t *mb_param, int code, const char *string) { @@ -561,8 +566,7 @@ static int receive_msg(modbus_param_t *mb_param, case BYTE: length_to_read = compute_query_length_data(mb_param, msg); msg_length_computed += length_to_read; - /* FIXME Wrong length */ - if (msg_length_computed > MAX_MESSAGE_LENGTH) { + if (msg_length_computed > TAB_MAX_ADU_LENGTH[mb_param->type_com]) { error_treat(mb_param, TOO_MANY_DATA, "Too many data"); return TOO_MANY_DATA; } diff --git a/src/modbus.h b/src/modbus.h index 1b91f29..8f171d9 100644 --- a/src/modbus.h +++ b/src/modbus.h @@ -26,7 +26,7 @@ extern "C" { #endif -#define MODBUS_TCP_DEFAULT_PORT 502 +#define MODBUS_TCP_DEFAULT_PORT 502 /* Slave index */ #define HEADER_LENGTH_RTU 1 @@ -37,8 +37,8 @@ extern "C" { #define PRESET_QUERY_LENGTH_TCP 12 #define PRESET_RESPONSE_LENGTH_TCP 8 -#define CHECKSUM_LENGTH_RTU 2 -#define CHECKSUM_LENGTH_TCP 0 +#define CHECKSUM_LENGTH_RTU 2 +#define CHECKSUM_LENGTH_TCP 0 /* It's not really the minimal length (the real one is report slave ID * in RTU (4 bytes)) but it's a convenient size to use in RTU or TCP @@ -47,25 +47,30 @@ extern "C" { * - HEADER_LENGTH_TCP (7) + function (1) + address (2) + number (2) * - HEADER_LENGTH_RTU (1) + function (1) + address (2) + number (2) + CRC (2) */ -#define MIN_QUERY_LENGTH 12 +#define MIN_QUERY_LENGTH 12 -/* Page 102, Application Notes of PI–MBUS–300: - * The maximum length of the entire message must not exceed 256 - * bytes. +/* Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5: + * - RS232 / RS485 ADU = 253 bytes + slave (1 byte) + CRC (2 bytes) = 256 bytes + * - TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes */ -#define MAX_MESSAGE_LENGTH 256 +#define MAX_PDU_LENGTH 253 +#define MAX_ADU_LENGTH_RTU 256 +#define MAX_ADU_LENGTH_TCP 260 + +/* Kept for compatibility reasons (deprecated) */ +#define MAX_MESSAGE_LENGTH 260 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12) * Quantity of Coils (2 bytes): 1 to 2000 (0x7D0) */ -#define MAX_STATUS 2000 +#define MAX_STATUS 2000 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15) * Quantity of Registers (2 bytes): 1 to 125 (0x7D) */ -#define MAX_REGISTERS 125 +#define MAX_REGISTERS 125 -#define REPORT_SLAVE_ID_LENGTH 75 +#define REPORT_SLAVE_ID_LENGTH 75 /* Time out between trames in microsecond */ #define TIME_OUT_BEGIN_OF_TRAME 500000