• Stars
    star
    211
  • Rank 186,828 (Top 4 %)
  • Language
    C
  • License
    Other
  • Created over 5 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Modbus ASCII/RTU and TCP implementation

Freemodbus

中文页 | English

Introduction

This is the Freemodbus protocol stack ported by armink. Support the functions of the master and slave at the same time.

FreeModbus is an open source Modbus protocol stack, but only the slave is open source, and the host source code is charged. At the same time, no better open source Modbus host protocol stack was found on the Internet, so this FreeModbus protocol stack supporting host mode was developed. This version of FreeModbus has been changed to V1.6 and features are as follows:

  • The newly added host source code is consistent with the style and interface of the original slave;
  • Support the host and slave to run in the same protocol stack;
  • Support real-time operating system and bare metal transplantation;
  • Provide a variety of request modes for applications, users can choose blocking or non-blocking mode, custom timeout time, etc., to facilitate flexible calls at the application layer;
  • Support all common Modbus methods.

File structure

Source File Description
FreeModbus\modbus\mb.c Provides Modbus slave settings and polling related interfaces for the application layer
FreeModbus\modbus\mb_m.c Provides Modbus host settings and polling related interfaces for the application layer
FreeModbus\modbus\ascii\mbascii.c ASCII mode settings and state machine
FreeModbus\modbus\functions\mbfunccoils.c Slave coil related functions
FreeModbus\modbus\functions\mbfunccoils_m.c Host coil related functions
FreeModbus\modbus\functions\mbfuncdisc.c Slave Discrete Input Related Functions
FreeModbus\modbus\functions\mbfuncdisc_m.c Discrete input related functions of the host
FreeModbus\modbus\functions\mbfuncholding.c Slave holding register related functions
FreeModbus\modbus\functions\mbfuncholding_m.c Host holding register related functions
FreeModbus\modbus\functions\mbfuncinput.c Slave input register related functions
FreeModbus\modbus\functions\mbfuncinput_m.c Host input register related functions
FreeModbus\modbus\functions\mbfuncother.c Other Modbus functions
FreeModbus\modbus\functions\mbutils.c Some small tools that need to be used in the protocol stack
FreeModbus\modbus\rtu\mbcrc.c CRC check function
FreeModbus\modbus\rtu\mbrtu.c Slave RTU mode settings and state machine
FreeModbus\modbus\rtu\mbrtu_m.c Host RTU mode settings and state machine
FreeModbus\modbus\tcp\mbtcp.c TCP mode settings and state machine
FreeModbus\port\port.c Implement hardware porting part of the interface
FreeModbus\port\portevent.c Implement slave event porting interface
FreeModbus\port\portevent_m.c Implement host event and error handling porting interface
FreeModbus\port\portserial.c Slave port porting
FreeModbus\port\portserial_m.c Host serial port porting
FreeModbus\port\porttimer.c Slave timer porting
FreeModbus\port\porttimer_m.c Host timer porting
FreeModbus\port\user_mb_app.c Define slave data buffer, realize the callback interface of slave Modbus function
FreeModbus\port\user_mb_app_m.c Define host data buffer, realize the callback interface of host Modbus function
FreeModbus\samples\sample_mb_master.c Sample code for host use
FreeModbus\samples\sample_mb_slave.c Slave use sample code
FreeModbus\samples\README.md Sample code description document

Note: All files with the _m suffix are the files that must be used in the master mode. If the slave mode is used, these files are not required.

License

The Freemodbus software package complies with the BSD license, see the LICENSE file for details.

Dependence

  • RT_Thread UART device

method of obtaining

To use the Freemodbus software package, you need to select it in the RT-Thread package manager. The specific path is as follows:

RT-Thread online packages
    IoT-internet of things --->
        [*] FreeModbus: Modbus master and slave stack --->
             [*] Master mode --->
             [*] Slave mode --->

Finally, let RT-Thread's package manager automatically update, or use the pkgs --update command to update the package to the BSP.

Instructions

Data buffer

The location defined by the data buffer is at the top of the FreeModbus\port\user_mb_app_m.c file, with a total of 4 data types. By default, FreeModbus slaves use one-dimensional array as the data structure of the buffer area. The host can store the data of all slaves in the network, so the host uses two-dimensional array to store all slave node data. The column number of the two-dimensional array represents the register, coil and discrete address, and the row number represents the slave node ID, but it needs to be reduced by one. For example, usMRegHoldBuf[2][1] means the slave ID is 3, and the register address is maintained The slave data is 1.

Modbus data processing callback interface

Modbus has 4 different data types in total, and all Modbus functions operate around these data types. Since different user data buffer structures may be different, the corresponding Modbus data processing methods are also different, so users need to customize the operations corresponding to each data type according to their own data buffer structure. All Modbus data processing callback interfaces are as follows:

Interface Function description
eMBMasterRegInputCB Input register callback interface
eMBMasterRegHoldingCB Holding register callback interface
eMBMasterRegCoilsCB Coil callback interface
eMBMasterRegDiscreteCB Discrete input callback interface

For the data buffer structure in the form of an array, the source code has already been transplanted and can be used directly.

Initial configuration process

All configuration parameters of this protocol stack are located in FreeModbus\modbus\include\mbconfig.h, currently the protocol stack supports two modes of master and slave, and supports both modes to be turned on at the same time. The slave supports Modbus RTU, Modbus ASCII and Modbus TCP 3 modes, and the master now only supports the commonly used Modbus RTU mode. In the process of using the master, the user needs to configure the broadcast conversion delay time, command response timeout time and the number of slaves. It should be noted that the current protocol stack only supports continuous slave addresses, and the starting address starts from 1**.

Normal use process

  1. Call the eMBMasterInit method to initialize the Modbus host protocol stack, and some hardware related to the host is initialized at this time
  2. Call the eMBMasterEnable method to start the Modbus master
  3. By calling the eMBMasterPoll method in thread or timer polling, the polling cycle determines the response time of the command.
  4. Call the host to request the API method, set a certain request timeout period, and will not return until the method has a result. If the method is executed successfully and the command is a read command, you can obtain the latest slave data by viewing the data buffer of the Modbus master.

For specific usage, please refer to the sample code in the /samples directory. Debugging the Modbus master-slave program can be debugged with Modbus Poll and Modbus slave software on the PC.

Exception handling process

Exception handling mainly occurs during the normal use of the host. All the error codes of the host request API have been described at the beginning of Chapter 3. For these error codes, users need to complete different actions according to their own product features. It is recommended that users encapsulate and implement the retransmission mechanism of the host request method. This implementation is more flexible. Generally, retransmission is required when receiving frame data errors and command response timeout error codes. The number of retransmissions is automatically increased by one. If the set value is exceeded, the slave is considered to be offline, and all subsequent commands sent to this slave are intercepted in advance; if the second retransmission command response is successful, automatically cleared the slave retransmits frequency. All the above functions can be realized by using the host request method or using the callback interface in FreeModbus\port\portevent_m.c, and users can choose flexibly according to their needs.

API detailed

The Modbus master is very different from the slave in the use process. The slave needs to passively wait for the request of the master, while the master actively sends out the request and receives and processes the response from the slave. When the host sends a broadcast request, the slave does not need to return a response, so the broadcast request is suitable for the master's write slave data command, not suitable for the read slave data command. The return value format of all methods in the host request API is the same, and the meaning of the return value is as follows.

Return value Description
MB_MRE_NO_ERR Normal, no error
MB_MRE_NO_REG Register, coil or discrete input address error
MB_MRE_ILL_ARG Incorrect input parameter format
MB_MRE_REV_DATA Receive data error
MB_MRE_TIMEDOUT Response timed out. The host did not receive the response from the slave within the set time.
MB_MRE_MASTER_BUSY The host is busy. The request was not sent within the set time.
MB_MRE_EXE_FUN After the host receives the response, an error occurs when executing the Modbus method (function).

All host request methods are thread safe and blocking mode. During use, as long as the host resource is not obtained within the set timeout period, it will return that the host is busy; if the host resource is obtained within the set timeout period, it must wait for the request result before returning.

Write a single holding register

Write data to a holding register of the slave

eMBMasterReqErrCode eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr,
                                                      USHORT usRegAddr,
                                                      USHORT usRegData,
                                                      LONG lTimeOut );
Parameters Description
ucSndAddr Requested slave address, 0 means broadcast.
usRegAddr Write register address
usRegData Write register data
lTimeOut Request timeout time. To support permanent waiting, just use the permanent waiting parameter of the operating system.

Write multiple holding registers

Write data to multiple holding registers of the slave.

eMBMasterReqErrCode eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,
                                                              USHORT usRegAddr,
                                                              USHORT usNRegs,
                                                              USHORT * pusDataBuffer,
                                                              LONG lTimeOut)
Parameters Description
ucSndAddr Requested slave address, 0 means broadcast.
usRegAddr Start address of write register
usNRegs Total number of write registers
pusDataBuffer Write register data
lTimeOut Request timeout time. To support permanent waiting, just use the permanent waiting parameter of the operating system.

Read multiple holding registers

Read data in multiple holding registers

eMBMasterReqErrCode eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr,
                                                     USHORT usRegAddr,
                                                     USHORT usNRegs,
                                                     LONG lTimeOut );
Parameters Description
ucSndAddr Requested slave address, 0 means broadcast.
usRegAddr Read register address
usRegData Number of read registers
lTimeOut Request timeout time. To support permanent waiting, just use the permanent waiting parameter of the operating system.

Read and write multiple holding registers

Read multiple registers first, and then write multiple registers.

eMBMasterReqErrCode eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
                                                                  USHORT usReadRegAddr,
                                                                  USHORT usNReadRegs,
                                                                  USHORT * pusDataBuffer,
                                                                  USHORT usWriteRegAddr,
                                                                  USHORT usNWriteRegs,
                                                                  LONG lTimeOut)
Parameters Description
ucSndAddr Requested slave address, 0 means broadcast.
usReadRegAddr Read register address
usNReadRegs Number of read registers
pusDataBuffer Write register data
usWriteRegAddr Write register address
usNWriteRegs Number of write registers
lTimeOut Request timeout time. To support permanent waiting, just use the permanent waiting parameter of the operating system.

Read multiple input registers

Read data in multiple input registers

eMBMasterReqErrCode eMBMasterReqReadInputRegister( UCHAR ucSndAddr,
                                                   USHORT usRegAddr,
                                                   USHORT usNRegs,
                                                   LONG lTimeOut );
Parameters Description
ucSndAddr Requested slave address, 0 means broadcast.
usRegAddr Read register address
usRegData Number of read registers
lTimeOut Request timeout time. To support permanent waiting, just use the permanent waiting parameter of the operating system.

Write a single coil

Write data to a coil of the slave

eMBMasterReqErrCode eMBMasterReqWriteCoil( UCHAR ucSndAddr,
                                           USHORT usCoilAddr,
                                           USHORT usCoilData,
                                           LONG lTimeOut)
Parameters Description
ucSndAddr Requested slave address, 0 means broadcast.
usCoilAddr Write the address of the coil
usCoilData Number of write coils
lTimeOut Request timeout time. To support permanent waiting, just use the permanent waiting parameter of the operating system.

Write multiple coils

Write data to multiple coils of the slave.

eMBMasterReqErrCode eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
                                                    USHORT usCoilAddr,
                                                    USHORT usNCoils,
                                                    UCHAR * pucDataBuffer,
                                                    LONG lTimeOut)
Parameters Description
ucSndAddr Requested slave address, 0 means broadcast.
usCoilAddr Write the start address of the coil
usNCoils Total number of write coils
pucDataBuffer Write coil data
lTimeOut Request timeout time. To support permanent waiting, just use the permanent waiting parameter of the operating system.

Read multiple coils

Read data from multiple coils

eMBMasterReqErrCode eMBMasterReqReadCoils( UCHAR ucSndAddr,
                                           USHORT usCoilAddr,
                                           USHORT usNCoils,
                                           LONG lTimeOut)
Parameters Description
ucSndAddr Requested slave address, 0 means broadcast.
usCoilAddr Read the address of the coil
usNCoils Number of reading coils
lTimeOut Request timeout time. To support permanent waiting, just use the permanent waiting parameter of the operating system.

Read multiple discrete inputs

Read data from multiple discrete inputs

eMBMasterReqErrCode eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr,
                                                    USHORT usDiscreteAddr,
                                                    USHORT usNDiscreteIn,
                                                    LONG lTimeOut)
Parameters Description
ucSndAddr Requested slave address, 0 means broadcast.
usDiscreteAddr Read the address of discrete input
usNDiscreteIn Read the number of discrete inputs
lTimeOut Request timeout time. To support permanent waiting, just use the permanent waiting parameter of the operating system.

Precautions

  • The slave supports Modbus RTU, Modbus ASCII and Modbus TCP 3 modes. The master now only supports the commonly used Modbus RTU mode.
  • Currently the protocol stack only supports continuous slave address, and the starting address starts from 1.

Contact information

More Repositories

1

at_device

AT component porting or samples for different devices
C
214
star
2

fal

Flash Abstraction Layer implentment. Manage flash device and partition.
C
130
star
3

nimble

An Apache open-source Bluetooth 5.0 stack porting on RT-Thread
C
128
star
4

rt-robot

a platform in creating new exciting robots
C
112
star
5

kernel-sample

RT-Thread kernel samples
C
81
star
6

netutils

IoT networking utilities for RT-Thread. Such as: ping, tftp, iperf, netio, ntp, telnet and tcpdump.
C
71
star
7

webclient

http client library by RT-Thread
C
67
star
8

ota_downloader

The firmware downloader which using on RT-Thread OTA component.
C
65
star
9

gui_engine

Graphics Engine (GE) in RT-Thread
C
53
star
10

wiznet

WIZnet TCP/IP chips (such as W5500/W5100..) SAL framework implement.
C
49
star
11

webnet

A lightweight, customizable embedded Web Server for RT-Thread
C
48
star
12

paho-mqtt

Eclipse Paho MQTT C/C++ client for Embedded platforms
C
43
star
13

ppp_device

lwIP PPP porting for GSM modem (like sim800)
C
39
star
14

mbedtls

An open source, portable, easy to use, readable and flexible SSL library
C
36
star
15

tinycrypt

A simple and configurable crypt library
C
34
star
16

quicklz

the world's fastest compression library
C
33
star
17

qrcode

A simple library for generating QR codes in C.
C
31
star
18

ali-iotkit

Ali Cloud SDK for IoT platform
C
30
star
19

cmux

connection multiplexing protocol for RT-Thread, support GSM0710 .etc
C
30
star
20

umqtt

A light weight, powerful, customizable, easy-to-use and embeddable mqtt client for RT-Thread
C
27
star
21

miniLZO

A mini subset of the LZO real-time data compression library
C
26
star
22

mpu-6xxx

a package of mpu6xxx driver library, compatible with mpu6000, mpu6050, mpu6500, mpu9250 and other chips.
C
25
star
23

FreeRTOS-Wrapper

RT-Thread操作系统的FreeRTOS兼容层 | FreeRTOS Application Compatibility Layer (ACL) for RT-Thread
C
25
star
24

wavplayer

Minimal music player for wav file.
C
23
star
25

peripheral-sample

RT-Thread peripheral samples
C
22
star
26

cJSON

Ultralightweight JSON parser in ANSI C
C
21
star
27

jerryscript

JerryScript port for RT-Thread
C
18
star
28

samples

RT-Thread kernel and components samples.
C
18
star
29

bsal

蓝牙协议栈抽象层
C
16
star
30

vi

The screen-oriented text editor for RT-Thread.
C
16
star
31

SQLite

SQLite is a self-contained, high-reliability, embedded, full-featured, public-domain, SQL database engine.
C
15
star
32

coap

CoAP on RT-Thread
C
13
star
33

TJpgDec

a generic JPEG image decompressor module.
C
12
star
34

CMSIS

CMSIS(Cortex Microcontroller Software Interface Standard) package on RT-Thread
C
12
star
35

network-sample

RT-Thread network samples
C
11
star
36

aht10

digital humidity and temperature sensor AHT10 driver library
C
11
star
37

nopoll

ASPLes/nopoll - OpenSource WebSocket toolkit for RT-Thread
C
11
star
38

lv_demo_music

LVGL music player demo for RT-Thread | LVGL音乐播放器演示示例(RT-Thread定制版)
C
11
star
39

fastlz

lightning-fast compression library
C
10
star
40

nanopb

Protocol Buffers for Embedded Systems
C
9
star
41

rw007

RW007 (SPI Wi-Fi module) driver for RT-Thread
C
9
star
42

SEGGER_SystemView

SEGGER SystemView
C
9
star
43

CMSIS_RTOS2

RT-Thread操作系统的CMSIS-RTOS2兼容层 | CMSIS-RTOS2 Application Compatibility Layer (ACL) for RT-Thread
C
8
star
44

gt9147

gt9147 touch driver
C
8
star
45

infrared_framework

Infrared framework based on RT-Thread's pin,pwm and hwtimer driver.
C
8
star
46

stm32_sdio

STM32 SDIO peripheral universal driver library
C
8
star
47

filesystem-sample

RT-Thread filesystem samples
C
8
star
48

ulog_file

ulog file backend
C
8
star
49

onenet

China Mobile OneNet cloud SDK for RT-Thread
C
8
star
50

zlib

general purpose data compression library
C
7
star
51

joylink

Joylink Cloud SDK for IoT platform
C
7
star
52

bmi160_bmx160

The device driver package for BMX160 and BMI160.
C
7
star
53

LPM

逻辑分区管理(Logical partition management),支持动态创建、删除、查找、读写物理存储设备上的逻辑分区。
C
7
star
54

tlsf

TLSF is a dynamic memory allocation algorithm with predictable execution time and low fragmentation.
C
7
star
55

atsrv_socket

C
7
star
56

icm20608

a 3-axis gyroscope and a 3-axis accelerometer driver library. Compatible with MPU6050, MPU6500, MPUxxxx etc
C
6
star
57

wlan-wiced

wlan driver from WICED.
C
6
star
58

dstr

dynamic string in C
C
6
star
59

GAgent

GAgent of Gizwits in RT-Thread
C
6
star
60

vsensor

虚拟传感器
C
6
star
61

uffs

UFFS is a file system that uses NAND flash in a small memory environment
C
6
star
62

azure-iot-sdk

Microsoft azure cloud SDK for RT-Thread
C
6
star
63

lsm6dsl

This is the LSM6DSL sensor driver package, support: accelerometer, gyroscope, step.
C
6
star
64

rdb

RT-Thread Debug Bridge
C
5
star
65

logmgr

log system manager
C
5
star
66

jffs2

JFFS2 is a log file system implemented on MTD devices
C
5
star
67

ap3216c

digital ambient light and a proximity sensor ap3216c driver library
C
5
star
68

rti

RT-Thread insight, a probe tool for RT-Thread to help to analyze internal behavior of the system.
C
5
star
69

sht2x

digital humidity and temperature sensor SHT2x driver library
C
4
star
70

minimp3

MPEG Audio Layer III decoder from the FFmpeg libavcodec library.
C
4
star
71

iperf

iperf-liked network performance tool in RT-Thread.
C
4
star
72

event_recorder

A lightweight event record and replay tools for debug and test.
C
4
star
73

st7789

st7789 lcd driver
C
4
star
74

persimmon

Persimmon UI for RT-Thread.
C++
4
star
75

mlibc

mlibc - A small libc for MCU and RT-Thread
4
star
76

spl0601

The Digital Air Pressure Sensor SPL06-01 driver package.
C
4
star
77

bma400

This is the BMA400 sensor driver package, support: accelerometer, step.
C
3
star
78

kdb

kernel debug log tool
C
3
star
79

partition

A simple partition on block device.
C
3
star
80

ft6206

ft6206 touch driver
C
3
star
81

pcf8574

Remote 8-bit I/O expander for I2C-bus
C
3
star
82

kendryte_sdk

Kendryte K210 SDK
Python
3
star
83

ft6236

C
3
star
84

MultiButton

A compact and easy to use event-driven button driver module for RT-Thread. | 一个小巧易用的事件驱动按钮驱动模块.
C
3
star
85

libsodium-legacy

A modern and easy-to-use crypto library.
C
2
star
86

CMSIS_RTOS1

RT-Thread操作系统的CMSIS-RTOS1兼容层 | CMSIS-RTOS1 Application Compatibility Layer (ACL) for RT-Thread
C
2
star
87

jsmn

Jsmn is a world fastest JSON parser/tokenizer.
C
2
star
88

ezXML

An XML parser C library that's simple and easy to use.
C
2
star
89

pms

pms :power management system
2
star
90

trusted-firmware-m

ARM trusted-firmware-m porting for RT-Thread
Python
2
star
91

cairo

Multi-platform 2D graphics library
C
2
star
92

fdt

Device Tree package in RT-Thread
C
2
star
93

qianxun

C
2
star
94

gdbstub

gdbstub on RT-Thread
C
2
star
95

yaffs2

yaffs2 file system for RT-Thread
1
star
96

micro-ecc

micro-ecc package
Python
1
star
97

lsm303agr

This is the LSM303AGR sensor driver package, support: accelerometer, magnetometer.
C
1
star
98

duktape

Duktape - embeddable Javascript engine with a focus on portability and compact footprint, port for RT-Thread
1
star
99

player

Multi-Media Audio Stream Player for RT-Thread
1
star
100

snippets

Some code snippets for RT-Thread
Python
1
star