MAX_MESSAGE_LENGTH and MAX_REGISTERS deviate from Modbus standard

Bug #341702 reported by n1ywb
4
Affects Status Importance Assigned to Milestone
libmodbus
Fix Released
Medium
Stéphane Raimbault

Bug Description

"#define MAX_MESSAGE_LENGTH 256" is incorrect for Modbus TCP.
Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5:
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 MAX_REGISTERS 100" is incorrect for Modbus.
Modbus_Application_Protocol_V1_1b.pdf Chapter 6 Section 3 Page 15:
Quantity of Registers: 2 Bytes: 1 to 125 (0x7D)

Revision history for this message
n1ywb (jeff-laughlin) wrote :

This patch against 2.0.2 changes MAX_REGISTERS to 125. It also adds a MAX_MESSAGE_LENGTH_RTU and _TCP #defs and a "max_message_length" field to modbus_params_t which is initialized to the appropriate limit in the init functions. I replaced all references to MAX_MESSAGE_LENGTH with a reference to mb_params->max_message_length. It passes unit testing.

Revision history for this message
Stéphane Raimbault (sra) wrote :

I'm not really happy to add yet another variable to modbus_param_t but I'm intend to apply your patch, just some comments:
- the MAX_REGISTERS change is missing (minor)
- could you use MAX_MESSAGE_LENGTH_TCP in the tests because the concerned code only works on TCP devices and I don't like to use internal value (modbus_param_t) in external code (not modbus.c)?
- could you provide two unit tests with the two length (or TCP at least)?
Thank you.

Revision history for this message
n1ywb (jeff-laughlin) wrote : Re: [Bug 341702] Re: MAX_MESSAGE_LENGTH and MAX_REGISTERS deviate from Modbus standard

One alternative might be to declare a global constant array of message
lengths and index it on the connection type, something like

const int message_length[] = { 256, 260 };

assert(message_length[RTU] == 256);
assert(message_length[TCP] == 260);

since type_com is already in mb_params.

On Fri, Mar 13, 2009 at 6:05 PM, Stéphane Raimbault
<email address hidden> wrote:
> I'm not really happy to add yet another variable to modbus_param_t but I'm intend to apply your patch, just some comments:
> - the MAX_REGISTERS change is missing (minor)
> - could you use MAX_MESSAGE_LENGTH_TCP in the tests because the concerned code only works on TCP devices and I don't like to use internal value (modbus_param_t) in external code (not modbus.c)?
> - could you provide two unit tests with the two length (or TCP at least)?
> Thank you.
>
> --
> MAX_MESSAGE_LENGTH and MAX_REGISTERS deviate from Modbus standard
> https://bugs.launchpad.net/bugs/341702
> You received this bug notification because you are a direct subscriber
> of the bug.
>
> Status in A groovy libmodbus!: New
>
> Bug description:
> "#define MAX_MESSAGE_LENGTH 256" is incorrect for Modbus TCP.
> Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5:
> 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 MAX_REGISTERS 100" is incorrect for Modbus.
> Modbus_Application_Protocol_V1_1b.pdf Chapter 6 Section 3 Page 15:
> Quantity of Registers: 2 Bytes: 1 to 125 (0x7D)
>

--
73 de n1ywb

Changed in libmodbus:
assignee: nobody → sra
importance: Undecided → Medium
milestone: none → 2.2.0
status: New → Fix Committed
Changed in libmodbus:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.