diff --git a/doc/modbus_report_slave_id.txt b/doc/modbus_report_slave_id.txt index 73de2e4..f2a9d62 100644 --- a/doc/modbus_report_slave_id.txt +++ b/doc/modbus_report_slave_id.txt @@ -44,17 +44,14 @@ EXAMPLE ------- [source,c] ------------------- -uint8_t nb = 128; -uint8_t *tab_bytes; +uint8_t tab_bytes[MODBUS_MAX_PDU_LENGTH]; ... -tab_bytes = (uint8_t *) malloc(nb * sizeof(uint8_t)); -rc = modbus_report_slave_id(ctx, nb, tab_bytes); +rc = modbus_report_slave_id(ctx, MODBUS_MAX_PDU_LENGTH, tab_bytes); if (rc > 1) { printf("Run Status Indicator: %s\n", tab_bytes[1] ? "ON" : "OFF"); } -free(tab_bytes); ------------------- diff --git a/src/modbus.h b/src/modbus.h index e0dbed8..2ea6895 100644 --- a/src/modbus.h +++ b/src/modbus.h @@ -105,6 +105,18 @@ MODBUS_BEGIN_DECLS #define MODBUS_MAX_WR_WRITE_REGISTERS 121 #define MODBUS_MAX_WR_READ_REGISTERS 125 +/* The size of the MODBUS PDU is limited by the size constraint inherited from + * the first MODBUS implementation on Serial Line network (max. RS485 ADU = 256 + * bytes). Therefore, MODBUS PDU for serial line communication = 256 - Server + * address (1 byte) - CRC (2 bytes) = 253 bytes. + * + * Consequently: + * - RS232 / RS485 ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) = + * 256 bytes. + * - TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes. + */ +#define MODBUS_MAX_PDU_LENGTH 253 + /* Random number to avoid errno conflicts */ #define MODBUS_ENOBASE 112345678