Lib write follow standard MODBUS APPLICATION PROTOCOL SPECIFICATION V1.1b3
publish by modbus.org. Download Modbus_Application_Protocol_V1_1b3.pdf or
doc
Lib source path: lib
Choose UART and enable global interrupt as bellow:
Note: The modbus lib implement the get data using interrupt and ring-buffer
it has limit of speed over UART to make sure it's working well the limit
of speed is 115200
Modbus lib use Segger RTT for log. Default it's enabled.
If don't use this log can disable by edit file: lib/mb_log.h
and comment macro MB_LOG
#ifdef MB_DEBUG
#define MB_LOG
#endif
#ifdef MB_LOG
#include "SEGGER_RTT.h"
#define mb_log_init() SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP)
#define mb_log(_c, ...) SEGGER_RTT_printf(0, _c, ##__VA_ARGS__);
#else
#define mb_log_init()
#define mb_log(_c, ...)
#endif
If keep using the Segger RTT log need to edit include on project configure like bellow:
Edit file lib/mb_stm32_include.h
, example project run on STM32F7, if you are using other MCU should edit for suitable
#ifndef MB_STM32_INCLUDE_H_
#define MB_STM32_INCLUDE_H_
#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal_uart.h"
#include "stm32f7xx_hal_tim.h"
#endif /* MB_STM32_INCLUDE_H_ */
Modbus lib already implement the UART and Timer callback handle on bsp_mb_slave.c
. Do not implement the interrupt callback other place to avoid error while compile
Edit file lib/data/mb_data_config.h
to change the limit register of data. Update the value of macro bellow:
#ifndef _MB_DATA_CONFIG_H_
#define _MB_DATA_CONFIG_H_
#define MB_COIL_NUM 32 // number of coil, @note: multiple of 8, @TODO edit
#define MB_INPUT_NUM 32 // number of input, @note: multiple of 8, @TODO edit
#define MB_REG_INPUT_NUM 32 // number of reg input , @TODO edit
#define MB_REG_HOLDING_NUM 32 // number of reg holding, @TODO edit
#endif /* _MB_DATA_CONFIG_H_ */
Edit the macro value on lib/bsp_mb_slave.c
. UART speed like value use on CubeMX
configure
/* macro ======================================================*/
#define BSP_MB_SLAVE_ID 0x01
#define BSP_MB_SLAVE_SPEED 115200 // bps
#define BSP_MB_TIMER_CLOCK_SOURCE 100 // Mhz
Edit timer
and uart
instance on bsp_slave.c
/* UART and TIMER instance */
#define uart_instance huart6
#define timer_instance htim3
The lib optimize for high performance working without delay and multiple modbus request handle limit 10 request. The request can increase depend on target of application. It's require more RAM. The limit can edit on file lib/mb_buffer.h
change value of macro MB_BUFFER_SIZE
/* mb_buffer.h */
#define MB_BUFFER_SIZE 10
Check on main.c
Test example, edit the value of register
// Update coil status
bsp_mb_coil_set(0, 1);
bsp_mb_coil_set(0, 0);
bsp_mb_coil_set(0, 1);
bsp_mb_coil_set(0, 0);
// Update input register
bsp_mb_discrete_input_set(0, 0);
bsp_mb_discrete_input_set(0, 1);
bsp_mb_discrete_input_set(0, 0);
bsp_mb_discrete_input_set(0, 1);
// Update input register
bsp_mb_input_reg_set(0, 1);
bsp_mb_input_reg_set(1, 2);
bsp_mb_input_reg_set(2, 3);
bsp_mb_input_reg_set(3, 4);
// Update holding register
bsp_mb_holding_reg_set(0, 5);
bsp_mb_holding_reg_set(1, 6);
bsp_mb_holding_reg_set(2, 7);
bsp_mb_holding_reg_set(3, 8);
Test on modbus master modbus poll
Modbus and application work via the register, modbus read/write value to register then application write the value to ouput or update value/status from peripheral