Colway Solutions
Modbus Member Logo

Optimise MMPL-C

 

Design constrains change from one platform to another. While someone is constrained for Data Memory (RAM) space, someone else is short of Code (Program) Memory (ROM/Flash) while yet another is short of both. In order to accommodate MMPL within the design constraints of most users, we have provided mechanisms to save RAM, ROM or both. The following sub sections describe the steps involved in using each of these techniques.

2.7.1       Set optimal buffer sizes

The library uses memory buffers to store incoming Modbus packets before decoding them and to store response packets before transmitting them. The sizes of these two buffers can be controlled by limiting the maximum number of Modbus data items (i.e. coils, registers etc.) that a master can request in one Modbus transaction. For instance if a Modbus Master sends a read request for 100 registers in one packet, the resulting response packet size will be greater than 200 bytes in comparison to a read request for just 10 registers. You can configure the library to entertain requests that can fit into a specific buffer size by defining the following macros:

Macro Name

Location

Remarks

RX_BUFFER_SIZE

MMPL_C.h

Limits the size of incoming packets. If the incoming request packet size cannot be accommodated in this buffer size, the library outputs an "Error" debugger message, discards the received packet and sends no response to the master.

TX_BUFFER_SIZE

MMPL_C.h

Limits the size of outgoing packets.

Note: No check is made by the library to verify if a Modbus request results in a response packet whose size is larger than this size.

 

2.7.1.1       Modbus Block Size Macros

Modbus block size is the number of data items that a master zor operate upon in one Modbus transaction. The size of a Modbus packet is limited to 256 bytes for Modbus RTU and 260 bytes for Modbus TCP. This in effect itself limits the number of items that can be operated upon in one transaction as below:

Transaction

Max permissible block size

Read Coils, Read Discrete Inputs

2000 coils and Discrete Inputs respectively

Read Holding Registers, Read Input Registers

125 registers

Write Multiple Coils

1968 coils

Write Multiple Registers

123 registers

 

However, in order to receive and service Modbus transactions that stretch up to the above max permissible limits, a device needs a transmit and a receive buffer of 256 bytes (260 in case of Modbus TCP). This may not be available or necessary in small devices employing low end microcontrollers. MSPL provides a way of using a lower buffer sizes and a set of macros which can be used to filter out Modbus transactions that exceed a set limit for block size. They are:

-    RD_BLK_SIZE_BITINFO

-    WR_BLK_SIZE_BITINFO

-    RD_BLK_SIZE_REGINFO

-    WR_BLK_SIZE_REGINFO

These macros must be set along with RX_BUFFER_SIZE and TX_BUFFER_SIZE to optimize the use of memory.

 

2.7.2       Include only the function you require

The code size occupied by the library can be minimized by including only the Modbus functions required in your application and excluding others. For instance, if your device has only digital inputs, there is no use of including support for Modbus function Read Holding Register.

 

2.7.2.1       How does the library respond to an unsupported function request

When the library receives a request for an unsupported Modbus function it responds with Modbus Exception code 0x01 (ILLEGAL FUNCTION).

 

2.7.3       Reduce Code Memory size by configuring CRC macros (Modbus RTU only)

The amount of Code Memory (sometimes called Program Memory) used by the library can be reduced using two technics.

Method: Move CRC tables into Data Memory (RAM)

Steps

a)      Open file MMPL_UserIf.h

b)      Locate the definition of macro CRC_TABLE_LOCATION

c)       Change its value to IN_RAM. The modified line should look like this:

#define CRC_TABLE_LOCATION     IN_RAM

d)      Rebuild your project. You should see a reduction in code size by approximately 512 bytes and a corresponding increase in RAM usage.

 

Description

Two tables of 256 constant values are used in computing CRC bytes. The location of these tables is configurable. The above steps cause the tables to be stored in data memory. This saves code memory at the expense of data memory by moving the tables into RAM. Since RAM is faster than ROM access, this method may also improve the efficiency of code execution.

 

2.7.4       Reduce Data Memory (RAM) size by configuring CRC macros

The amount of Data Memory (sometimes called as RAM) used by the library can be reduced using two techniques.

Method: Move CRC tables into Code Memory (ROM)

Steps

a)      Open file MMPL_UserIf.h

b)      Locate the definition of macro CRC_TABLE_LOCATION

c)       Change its value to IN_ROM. The modified line should look like this:

#define CRC_TABLE_LOCATION    IN_ROM

d)      Rebuild your project. You should see a reduction in RAM usage but an increase in the code size.

 

Description

Two tables of 256 constant values are used in computing CRC bytes. The location of these tables is configurable. The above steps cause the tables to be stored in code memory. This saves data memory (RAM) at the expense of code memory (ROM) by moving the tables into ROM.

 

Back to Index

Privacy Policy Site Map FAQ Contact Us