Configuring µC/Modbus

Configuration of µC/Modbus is done at compile time via about 20 #define constants. Configuration values are found in mb_cfg.h which should be placed in your product’s directory or, you can copy the #define constants in a header file of your choice. It’s recommended that you copy the mb_cfg.h file that is provided with the µC/Modbus distribution, and modify its content instead of creating mb_cfg.h from scratch. This way you have a better chance of not forgetting any #define constants. Default values are shown in RED.

Configuring µC/Modbus, MODBUS_CFG_SLAVE_EN

This #define constant specifies whether your product will support Modbus slave (or server) mode. Set this #define to DEF_ENABLED to enable SLAVE code. Set this #define to DEF_DISABLED to disable SLAVE code. You must have purchased the µC/Modbus-S package in order to set this #define to DEF_ENABLED .

Configuring µC/Modbus, MODBUS_CFG_MASTER_EN

This #define constant specifies whether your product will support Modbus master (or client) mode. Set this #define to DEF_ENABLED to enable MASTER code. Set this #define to DEF_DISABLED to disable Master code You must have purchased the µC/Modbus-M package in order to set this #define to DEF_ENABLED .

Configuring µC/Modbus, MODBUS_CFG_ASCII_EN

This #define constant specifies whether your product will support the Modbus ASCII protocol. Setting this value to DEF_ENABLED allows any Modbus channel to be configured for Modbus ASCII mode. Note that each channel must be configured to either Modbus ASCII or Modbus RTU mode at run-time. Setting MODBUS_CFG_ASCII_EN to DEF_ENABLED doesn’t mean that your product MUST use ASCII mode, it just means that the code to support Modbus ASCII will be included in the compilation.

Configuring µC/Modbus, MODBUS_CFG_RTU_EN

This #define constant specifies whether your product will support the MODBUS RTU protocol. Setting this value to DEF_ENABLED allows any Modbus channel to be configured for Modbus RTU mode. Note that each channel must be configured to either MODBUS ASCII or MODBUS RTU mode at run-time. Setting MODBUS_CFG_RTU_EN to DEF_ENABLED doesn’t mean that your product MUST use RTU mode, it just means that the code to support MODBUS RTU will be included in the compilation.

Configuring µC/Modbus, MODBUS_CFG_MAX_CH

µC/Modbus allows you to provide multiple communication ‘channels’ in your product. Each channel allows a MODBUS master to request data from your product. If your product only provides one channel, you should set MODBUS_CFG_MAX_CH to 1 .

Configuring µC/Modbus, MODBUS_CFG_BUF_SIZE

MODBUS protocol packets can contain up to 256 bytes of data. To hold this data, each µC/Modbuschannel allocates storage buffers: TWO for received packets and TWO for transmit packets. If your application sends and receives small packets, you can reduce the buffer size in order to conserve RAM. However, we recommend that you leave MODBUS_CFG_BUF_SIZE to it’s default value of 255 . With 255, a Modbus channel will require 1020 bytes of RAM for buffers.

Configuring µC/Modbus, MODBUS_CFG_FP_EN

When set to DEF_ENABLED , this #define constant is used to enable code generation for floating-point support of the “Daniels Flow Meter Floating-Point Extension”. The default value should be DEF_DISABLED .

Configuring µC/Modbus, MODBUS_CFG_FP_START_IX

This #define establishes the start address for floating-point numbers use in Input Registers and Holding Registers. Basically, integer input registers and holding registers go from address (or index) 0 to MODBUS_CFG_FP_START_IX-1 and floating?point input registers and holding registers, from MODBUS_CFG_FP_START_IX to 65535.

Configuring µC/Modbus, MODBUS_CFG_FC01_EN

When set to DEF_ENABLED , this #define determines whether µC/Modbuswill support Coil Read commands (Function Code #1). When set to DEF_DISABLED , code will not be generated for this command.

Configuring µC/Modbus, MODBUS_CFG_FC02_EN

When set to DEF_ENABLED , this #define determines whether µC/Modbuswill support Discrete Input Read commands (Function Code #2). When set to DEF_DISABLED , code will not be generated for this command.

Configuring µC/Modbus, MODBUS_CFG_FC03_EN

When set to DEF_ENABLED , this #define determines whether µC/Modbuswill support Holding register Read commands (Function Code #3). When set to DEF_DISABLED , code will not be generated for this command.

Configuring µC/Modbus, MODBUS_CFG_FC04_EN

When set to DEF_ENABLED , this #define determines whether µC/Modbuswill support Input register Read commands (Function Code #4). When set to DEF_DISABLED , code will not be generated for this command.

Configuring µC/Modbus, MODBUS_CFG_FC05_EN

When set to DEF_ENABLED , this #define determines whether µC/Modbuswill support Coil Write commands (Function Code #5). When set to DEF_DISABLED , code will not be generated for this command.

Configuring µC/Modbus, MODBUS_CFG_FC06_EN

When set to DEF_ENABLED , this #define determines whether µC/Modbuswill support writing to a single Holding Register commands (Function Code #6). When set to DEF_DISABLED , code will not be generated for this command.

Configuring µC/Modbus, MODBUS_CFG_FC08_EN

When set to DEF_ENABLED , this #define determines whether µC/Modbuswill support diagnostic loopback commands (Function Code #8). When set to DEF_DISABLED , code will not be generated for this command.

Configuring µC/Modbus, MODBUS_CFG_FC15_EN

When set to DEF_ENABLED , this #define determines whether µC/Modbuswill support the Multiple Coil Write command (Function Code #15). When set to DEF_DISABLED , code will not be generated for this command.

Configuring µC/Modbus, MODBUS_CFG_FC16_EN

When set to DEF_ENABLED , this #define determines whether µC/Modbuswill support the Multiple Holding Register Write command (Function Code #16). When set to DEF_DISABLED , code will not be generated for this command.

Configuring µC/Modbus-S, MODBUS_CFG_FC20_EN

When set to DEF_ENABLED, this #define determines whether µC/Modbuswill support the File Read command (Function Code #20). When set to DEF_DISABLED , code will not be generated for this command.

Configuring µC/Modbus-S, MODBUS_CFG_FC21_EN

When set to DEF_ENABLED, this #define determines whether µC/Modbuswill support the File Write command (Function Code #21). When set to DEF_DISABLED , code will not be generated for this command.

Configuring µC/Modbus, RAM Memory Requirements

The amount of RAM required by each µC/Modbuschannel is shown in the table below. The table assumes that pointers are 32 bits wide.

RAM Requirements for each µC/Modbuschannel.

Data TypeData Type Size
(Bytes)
#Elements for Specific Data TypeTotal Bytes
CPU_BOOLEAN111
CPU_INT08U18 +
4 * MODBUS_CFG_BUF_SIZE
1028
CPU_INT16U213 +
2 * MODBUS_CFG_RTU_EN
30
CPU_INT32U4416
CPU_INT08U *428
  Total (per µC/Modbus channel):
(see MB_ChSize )
1083

The ‘global’ variable MB_TotalRAMSize contains the total amount of RAM (in bytes) needed by µC/Modbus for the configuration you specify. Similarly, MB_ChSize contains the amount of RAM (in bytes) needed by each Modbus channel. Both of these ‘variables’ are 32-bit values and are actually declared as ‘const’ and thus, use 8 bytes of ROM and no RAM.

Related pages