Commit d39fc1f8c4564854c963fc913b7837f4f8c046e9

Authored by Stéphane Raimbault
1 parent 946fb25c

Define and public export of MODBUS_MAX_PDU_LENGTH (closes #167)

doc/modbus_report_slave_id.txt
... ... @@ -44,17 +44,14 @@ EXAMPLE
44 44 -------
45 45 [source,c]
46 46 -------------------
47   -uint8_t nb = 128;
48   -uint8_t *tab_bytes;
  47 +uint8_t tab_bytes[MODBUS_MAX_PDU_LENGTH];
49 48  
50 49 ...
51 50  
52   -tab_bytes = (uint8_t *) malloc(nb * sizeof(uint8_t));
53   -rc = modbus_report_slave_id(ctx, nb, tab_bytes);
  51 +rc = modbus_report_slave_id(ctx, MODBUS_MAX_PDU_LENGTH, tab_bytes);
54 52 if (rc > 1) {
55 53 printf("Run Status Indicator: %s\n", tab_bytes[1] ? "ON" : "OFF");
56 54 }
57   -free(tab_bytes);
58 55 -------------------
59 56  
60 57  
... ...
src/modbus.h
... ... @@ -105,6 +105,18 @@ MODBUS_BEGIN_DECLS
105 105 #define MODBUS_MAX_WR_WRITE_REGISTERS 121
106 106 #define MODBUS_MAX_WR_READ_REGISTERS 125
107 107  
  108 +/* The size of the MODBUS PDU is limited by the size constraint inherited from
  109 + * the first MODBUS implementation on Serial Line network (max. RS485 ADU = 256
  110 + * bytes). Therefore, MODBUS PDU for serial line communication = 256 - Server
  111 + * address (1 byte) - CRC (2 bytes) = 253 bytes.
  112 + *
  113 + * Consequently:
  114 + * - RS232 / RS485 ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) =
  115 + * 256 bytes.
  116 + * - TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes.
  117 + */
  118 +#define MODBUS_MAX_PDU_LENGTH 253
  119 +
108 120 /* Random number to avoid errno conflicts */
109 121 #define MODBUS_ENOBASE 112345678
110 122  
... ...