Commit f5e2f2c97117ea5e974973917d066c5e4ec746d9

Authored by Stéphane Raimbault
1 parent 535e696f

Conformance of the TCP trame length

The maximum length of the ADU is 260 in TCP not 256.
Showing 2 changed files with 22 additions and 13 deletions
src/modbus.c
@@ -146,6 +146,11 @@ static const int TAB_CHECKSUM_LENGTH[2] = { @@ -146,6 +146,11 @@ static const int TAB_CHECKSUM_LENGTH[2] = {
146 CHECKSUM_LENGTH_TCP 146 CHECKSUM_LENGTH_TCP
147 }; 147 };
148 148
  149 +static const int TAB_MAX_ADU_LENGTH[2] = {
  150 + MAX_ADU_LENGTH_RTU,
  151 + MAX_ADU_LENGTH_TCP,
  152 +};
  153 +
149 /* Treats errors and flush or close connection if necessary */ 154 /* Treats errors and flush or close connection if necessary */
150 static void error_treat(modbus_param_t *mb_param, int code, const char *string) 155 static void error_treat(modbus_param_t *mb_param, int code, const char *string)
151 { 156 {
@@ -561,8 +566,7 @@ static int receive_msg(modbus_param_t *mb_param, @@ -561,8 +566,7 @@ static int receive_msg(modbus_param_t *mb_param,
561 case BYTE: 566 case BYTE:
562 length_to_read = compute_query_length_data(mb_param, msg); 567 length_to_read = compute_query_length_data(mb_param, msg);
563 msg_length_computed += length_to_read; 568 msg_length_computed += length_to_read;
564 - /* FIXME Wrong length */  
565 - if (msg_length_computed > MAX_MESSAGE_LENGTH) { 569 + if (msg_length_computed > TAB_MAX_ADU_LENGTH[mb_param->type_com]) {
566 error_treat(mb_param, TOO_MANY_DATA, "Too many data"); 570 error_treat(mb_param, TOO_MANY_DATA, "Too many data");
567 return TOO_MANY_DATA; 571 return TOO_MANY_DATA;
568 } 572 }
src/modbus.h
@@ -26,7 +26,7 @@ @@ -26,7 +26,7 @@
26 extern "C" { 26 extern "C" {
27 #endif 27 #endif
28 28
29 -#define MODBUS_TCP_DEFAULT_PORT 502 29 +#define MODBUS_TCP_DEFAULT_PORT 502
30 30
31 /* Slave index */ 31 /* Slave index */
32 #define HEADER_LENGTH_RTU 1 32 #define HEADER_LENGTH_RTU 1
@@ -37,8 +37,8 @@ extern "C" { @@ -37,8 +37,8 @@ extern "C" {
37 #define PRESET_QUERY_LENGTH_TCP 12 37 #define PRESET_QUERY_LENGTH_TCP 12
38 #define PRESET_RESPONSE_LENGTH_TCP 8 38 #define PRESET_RESPONSE_LENGTH_TCP 8
39 39
40 -#define CHECKSUM_LENGTH_RTU 2  
41 -#define CHECKSUM_LENGTH_TCP 0 40 +#define CHECKSUM_LENGTH_RTU 2
  41 +#define CHECKSUM_LENGTH_TCP 0
42 42
43 /* It's not really the minimal length (the real one is report slave ID 43 /* It's not really the minimal length (the real one is report slave ID
44 * in RTU (4 bytes)) but it's a convenient size to use in RTU or TCP 44 * in RTU (4 bytes)) but it's a convenient size to use in RTU or TCP
@@ -47,25 +47,30 @@ extern "C" { @@ -47,25 +47,30 @@ extern "C" {
47 * - HEADER_LENGTH_TCP (7) + function (1) + address (2) + number (2) 47 * - HEADER_LENGTH_TCP (7) + function (1) + address (2) + number (2)
48 * - HEADER_LENGTH_RTU (1) + function (1) + address (2) + number (2) + CRC (2) 48 * - HEADER_LENGTH_RTU (1) + function (1) + address (2) + number (2) + CRC (2)
49 */ 49 */
50 -#define MIN_QUERY_LENGTH 12 50 +#define MIN_QUERY_LENGTH 12
51 51
52 -/* Page 102, Application Notes of PI–MBUS–300:  
53 - * The maximum length of the entire message must not exceed 256  
54 - * bytes. 52 +/* Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5:
  53 + * - RS232 / RS485 ADU = 253 bytes + slave (1 byte) + CRC (2 bytes) = 256 bytes
  54 + * - TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes
55 */ 55 */
56 -#define MAX_MESSAGE_LENGTH 256 56 +#define MAX_PDU_LENGTH 253
  57 +#define MAX_ADU_LENGTH_RTU 256
  58 +#define MAX_ADU_LENGTH_TCP 260
  59 +
  60 +/* Kept for compatibility reasons (deprecated) */
  61 +#define MAX_MESSAGE_LENGTH 260
57 62
58 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12) 63 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
59 * Quantity of Coils (2 bytes): 1 to 2000 (0x7D0) 64 * Quantity of Coils (2 bytes): 1 to 2000 (0x7D0)
60 */ 65 */
61 -#define MAX_STATUS 2000 66 +#define MAX_STATUS 2000
62 67
63 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15) 68 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
64 * Quantity of Registers (2 bytes): 1 to 125 (0x7D) 69 * Quantity of Registers (2 bytes): 1 to 125 (0x7D)
65 */ 70 */
66 -#define MAX_REGISTERS 125 71 +#define MAX_REGISTERS 125
67 72
68 -#define REPORT_SLAVE_ID_LENGTH 75 73 +#define REPORT_SLAVE_ID_LENGTH 75
69 74
70 /* Time out between trames in microsecond */ 75 /* Time out between trames in microsecond */
71 #define TIME_OUT_BEGIN_OF_TRAME 500000 76 #define TIME_OUT_BEGIN_OF_TRAME 500000