Commit 98c4aca38412f6b86ffeb3fd01c68ee9f1fd04eb
1 parent
130cf4b2
Export MODBUS_MAX_ADU_LENGTH and documentation (ref #241)
Showing
2 changed files
with
25 additions
and
6 deletions
doc/modbus_receive_confirmation.txt
| @@ -14,11 +14,18 @@ SYNOPSIS | @@ -14,11 +14,18 @@ SYNOPSIS | ||
| 14 | 14 | ||
| 15 | DESCRIPTION | 15 | DESCRIPTION |
| 16 | ----------- | 16 | ----------- |
| 17 | -The *modbus_receive_confirmation(*_ function shall receive a request via the | 17 | +The *modbus_receive_confirmation()* function shall receive a request via the |
| 18 | socket of the context _ctx_. This function must be used for debugging purposes | 18 | socket of the context _ctx_. This function must be used for debugging purposes |
| 19 | because the received response isn't checked against the initial request. This | 19 | because the received response isn't checked against the initial request. This |
| 20 | function can be used to receive request not handled by the library. | 20 | function can be used to receive request not handled by the library. |
| 21 | 21 | ||
| 22 | +The maximum size of the response depends on the used backend, in RTU the _rsp_ | ||
| 23 | +array must be _MODBUS_RTU_MAX_ADU_LENGTH_ bytes and in TCP it must be | ||
| 24 | +_MODBUS_TCP_MAX_ADU_LENGTH_ bytes. If you want to write code compatible with | ||
| 25 | +both, you can use the constant _MODBUS_MAX_ADU_LENGTH_ (maximum value of all | ||
| 26 | +libmodbus backends). Take care to allocate enough memory to store responses to | ||
| 27 | +avoid crashes of your server. | ||
| 28 | + | ||
| 22 | 29 | ||
| 23 | RETURN VALUE | 30 | RETURN VALUE |
| 24 | ------------ | 31 | ------------ |
| @@ -27,6 +34,13 @@ response length if sucessful. The returned request length can be zero if the | @@ -27,6 +34,13 @@ response length if sucessful. The returned request length can be zero if the | ||
| 27 | indication request is ignored (eg. a query for another slave in RTU | 34 | indication request is ignored (eg. a query for another slave in RTU |
| 28 | mode). Otherwise it shall return -1 and set errno. | 35 | mode). Otherwise it shall return -1 and set errno. |
| 29 | 36 | ||
| 37 | +EXAMPLE | ||
| 38 | +------- | ||
| 39 | +[source,c] | ||
| 40 | +------------------- | ||
| 41 | +uint8_t rsp[MODBUS_MAX_ADU_LENGTH]; | ||
| 42 | +rc = modbus_receive_confirmation(ctx, rsp); | ||
| 43 | +------------------- | ||
| 30 | 44 | ||
| 31 | SEE ALSO | 45 | SEE ALSO |
| 32 | -------- | 46 | -------- |
src/modbus.h
| @@ -97,14 +97,19 @@ MODBUS_BEGIN_DECLS | @@ -97,14 +97,19 @@ MODBUS_BEGIN_DECLS | ||
| 97 | * the first MODBUS implementation on Serial Line network (max. RS485 ADU = 256 | 97 | * the first MODBUS implementation on Serial Line network (max. RS485 ADU = 256 |
| 98 | * bytes). Therefore, MODBUS PDU for serial line communication = 256 - Server | 98 | * bytes). Therefore, MODBUS PDU for serial line communication = 256 - Server |
| 99 | * address (1 byte) - CRC (2 bytes) = 253 bytes. | 99 | * address (1 byte) - CRC (2 bytes) = 253 bytes. |
| 100 | - * | ||
| 101 | - * Consequently: | ||
| 102 | - * - RS232 / RS485 ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) = | ||
| 103 | - * 256 bytes. | ||
| 104 | - * - TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes. | ||
| 105 | */ | 100 | */ |
| 106 | #define MODBUS_MAX_PDU_LENGTH 253 | 101 | #define MODBUS_MAX_PDU_LENGTH 253 |
| 107 | 102 | ||
| 103 | +/* Consequently: | ||
| 104 | + * - RTU MODBUS ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) = 256 | ||
| 105 | + * bytes. | ||
| 106 | + * - TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes. | ||
| 107 | + * so the maximum of both backend in 260 bytes. This size can used to allocate | ||
| 108 | + * an array of bytes to store responses and it will be compatible with the two | ||
| 109 | + * backends. | ||
| 110 | + */ | ||
| 111 | +#define MODBUS_MAX_ADU_LENGTH 260 | ||
| 112 | + | ||
| 108 | /* Random number to avoid errno conflicts */ | 113 | /* Random number to avoid errno conflicts */ |
| 109 | #define MODBUS_ENOBASE 112345678 | 114 | #define MODBUS_ENOBASE 112345678 |
| 110 | 115 |