An enhanced Modbus protocol stack for MCU
By designing an enhanced Modbus protocol stack suitable for MCUs, using state machine and virtual protocol stack group technologies, the problem of MCUs in the existing technology is difficult to achieve multi-master and multi-slave functions, and it realizes the support of multi-master and multi-slave Modbus communication without RTOS and interrupt resources, and is suitable for micro-intelligent instruments.
Patent Information
- Application Number
- CN202410652861.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-24
- Publication Date
- 2025-03-11
- Estimated Expiration
- 2044-05-24
AI Technical Summary
The existing Modbus protocol stack suitable for MCUs is difficult to achieve multi-master and multi-slave function, and requires RTOS support or frequent use of interrupt resources, which cannot meet the needs of micro-intelligent instruments.
An enhanced Modbus protocol stack suitable for MCU is designed, using state machines, virtual protocol stack groups, virtual protocol stacks, user interface files and user tool modules to realize multi-master and multi-slave functions without RTOS support and interrupt resources.
It realizes the support of multi-master and multi-slave Modbus communication without RTOS and interrupt resources on the MCU, and is suitable for resource-intensive micro-intelligent instruments and instruments, improving the flexibility and efficiency of the protocol stack.
Smart Images

Figure CN118381689B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of industrial field bus communication, and in particular to an enhanced Modbus protocol stack suitable for MCU. Background Art
[0002] With the development of Internet of Things technology, various industrial fieldbus technologies are applied to industrial sites to solve digital communication and information transmission problems between intelligent instruments, controllers, actuators and other field equipment. Common industrial networks include: Profibus-DP, Profinet, CAN, Modbus, etc.
[0003] As the perception layer in the three-layer framework structure of perception layer, transmission layer and platform application layer, micro intelligent instruments have developed rapidly in recent years, and their communication and networking capabilities have been greatly improved. As an economical acquisition terminal, micro intelligent instruments have limited computing power. In order to integrate into the current industrial site, support for Modbus protocol has become a hot topic.
[0004] The current huge equipment network requires that micro intelligent instruments and meters not only exist as a single slave, but more often need to support themselves as multiple slaves, and even need to exist as a master and slave at the same time. Most of the core controllers of micro intelligent instruments and meters use micro MCUs with limited computing power and FLASH, and the frequency of replacement is high, requiring their program architecture to be easier to maintain and upgrade. A simple and efficient Modbus protocol stack that supports multi-master and multi-slave development has become a hot topic.
[0005] At present, the common Modbus protocol stacks suitable for MCU on the market include: FreeModbus, uC / Modbus, eeModbus, emModbus, etc. Although FreeModbus is free and supports master and slave machines, it does not support multiple physical interfaces. If you want to achieve it, you can only double the amount of code and the occupation of MCU on-chip peripheral resources, which is extremely inefficient, that is, it is impossible to achieve multiple masters and multiple slaves. Other Modbus protocol stacks, although they support multiple physical interfaces and multiple masters and multiple slaves, either require the support of RTOS or frequently use the interrupt resources and timer resources of MCU, which is unbearable for micro-intelligent instruments.
[0006] Therefore, there is an urgent need for an enhanced Modbus protocol stack suitable for MCU. Summary of the invention
[0007] The purpose of the present invention is to provide an enhanced Modbus protocol stack suitable for MCU, which supports multi-master and multi-slave, does not require RTOS support, and does not require the use of interrupt and timer resources.
[0008] To achieve the above object, the present invention provides the following solutions:
[0009] An enhanced Modbus protocol stack suitable for MCU, comprising: a state machine, a virtual protocol stack group, a virtual protocol stack, a user interface file and a user tool module;
[0010] The state machine is used to operate and maintain the protocol stack based on the virtual protocol stack group and the user interface file;
[0011] The virtual protocol stack group is used to manage the virtual protocol stack;
[0012] The virtual protocol stack is used to organize operation information;
[0013] The user interface file is used to store the target function that the protocol stack needs to be implemented by the user, wherein the target function is organized and formed by the user according to the functional requirements;
[0014] The user tool module is used to provide the user with preset operations for the protocol stack.
[0015] Optionally, the state machine performs operation and maintenance of the protocol stack based on the virtual protocol stack group and the user interface file, including:
[0016] The state machine includes several states and events, and polls the virtual protocol stack group in an event-driven state manner to perform data reception, data transmission, function analysis, virtual protocol stack management, and user interface calling and management.
[0017] Optionally, the states include: idle, reading, processing data, writing, and window period; the events include: slave turns to reading event, master turns to writing event, master-slave turns to processing data event, host turns to idle event, slave turns to writing event, slave turns to idle event, host enters window period event, host turns to reading event, host enters processing data event, slave turns to idle event, and initialization event.
[0018] Optionally, the state machine changes the event state of events other than the host turning to writing event according to the current situation, and whether the host turning to writing event occurs is determined by the state of the slave request sent by the user.
[0019] Optionally, the virtual protocol stack group managing the virtual protocol stack includes:
[0020] The virtual protocol stack group manages the loading and unloading operations of the virtual protocol stack in the form of a pointer array.
[0021] Optionally, the virtual protocol stack organization operation information includes:
[0022] The virtual protocol stack uses a structure to organize operation information, and the operation information includes: virtual protocol stack ID information, operation status, serial port used, control pin, communication parameters, protocol type, slave address, receive buffer array, data volume, send buffer array, send flag, window period time, TCP communication special ID information, register start address of function code to be set, number of registers, and register array.
[0023] Optionally, the target function is written by referring to the structure of the virtual protocol stack, and the target function includes: a function for modifying the baud rate, a function for obtaining the current amount of received data, a 50us delay function, a function for setting and modifying the slave address, a function for starting the hardware interface, a function for stopping the hardware interface, a function for putting the hardware interface in a read state, a function for putting the hardware interface in a write state, a data frame sending function, a slave receiving data CRC error function, various function functions required by the slave to comply with the modbus protocol, a host request timeout function, a host request return exception code function, and various post-request function functions required by the host to comply with the modbus protocol.
[0024] Optionally, the user tool module provides the user with preset operations for the protocol stack, including:
[0025] The user tool module provides the user with preset operations for the protocol stack through functional functions, and the functional functions include: a protocol stack initialization function, a mounting function, an unmounting function, and a state machine polling function.
[0026] The beneficial effects of the present invention are:
[0027] The enhanced Modbus protocol stack suitable for MCU proposed in the present invention realizes a multi-master and multi-slave mode independent of the RTOS system, and realizes data reception and transmission independent of the timer and serial port interrupt resources of the MCU, compared with the commonly used Modbus protocol stack. The enhanced Modbus protocol stack suitable for MCU is suitable for various layers of equipment using Modbus networking in industry, especially for terminal equipment with multi-master and multi-slave requirements and equipment with limited on-chip interrupt and timer resources. The enhanced Modbus protocol stack can better reflect its excellent performance and can effectively solve the problem that it is difficult to realize the multi-master and multi-slave function under the condition of limited resources. BRIEF DESCRIPTION OF THE DRAWINGS
[0028] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0029] Figure 1 The present invention is a schematic diagram of an enhanced Modbus protocol stack architecture suitable for MCU according to an embodiment of the present invention. DETAILED DESCRIPTION
[0030] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0031] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.
[0032] This embodiment provides an enhanced Modbus protocol stack suitable for MCU, including: a state machine, a virtual protocol stack group, a virtual protocol stack, a user interface file and a user tool module;
[0033] A state machine for operating and maintaining the protocol stack based on the virtual protocol stack group and the user interface file;
[0034] A virtual protocol stack group, used to manage the virtual protocol stack;
[0035] Virtual protocol stack, used to organize operation information;
[0036] The user interface file is used to store the target function that the protocol stack needs to implement by the user, wherein the target function is organized and formed by the user according to the functional requirements;
[0037] The user tool module is used to provide users with preset operations for the protocol stack.
[0038] Furthermore, the state machine performs operation and maintenance of the protocol stack based on the virtual protocol stack group and the user interface file, including:
[0039] The state machine designed with a branch structure contains several states and events. It polls the virtual protocol stack group through event-driven states to receive data, send data, perform function analysis, manage the virtual protocol stack, and call and manage the user interface. As the operating body of the enhanced Modbus protocol stack, the state machine is responsible for the operation and maintenance of the entire protocol stack. Its operation is mainly implemented by polling the virtual protocol stack group to ensure its operating efficiency. The program logic is optimized. The main process cannot use delays throughout the process, and other application interrupts must meet fast-in and fast-out requirements.
[0040] Among them, the states include: idle, reading, processing data, writing, and window period; the events include: slave turns to reading event, host turns to writing event, master-slave turns to processing data event, host turns to idle event, slave turns to writing event, slave turns to idle event, host enters window period event, host turns to reading event, host enters processing data event, slave turns to idle event, and initialization event.
[0041] For events other than the master turning to writing event, the state machine changes the event state according to the current situation. Whether the master turning to writing event occurs is determined by the state of the slave request sent by the user.
[0042] Furthermore, the virtual protocol stack group manages the virtual protocol stack, including: the virtual protocol stack group manages the loading and unloading operations of the virtual protocol stack in a pointer array manner.
[0043] The virtual protocol stack group provides a sub-protocol stack management pool for the entire protocol stack, facilitates the automatic management of sub-protocol stacks in multi-master and multi-slave operation, and automatically allocates ID information to sub-protocol stacks.
[0044] The virtual protocol stack is a virtual protocol stack management subject, which uses a structure to organize the operation information. In this embodiment, the realization of the multi-master and multi-slave function depends on the virtual protocol stack, which contains all the information required for the operation of a sub-body: hardware correspondence, sub-body protocol stack operation intermediate parameters, and sub-body protocol stack register definition. Specifically including: virtual protocol stack ID information, operation status, serial port used, control pin, communication parameters, protocol type, slave address, receive buffer array, data volume, send buffer array, send flag, window period time, TCP communication dedicated ID information, register start address of the function code to be set, register number, and register array.
[0045] Furthermore, in order to ensure that MCUs from different manufacturers can use this protocol stack well, the user interface file is open to users and needs to supplement some functions required by the protocol stack according to the specific MCU. It is open to the outside world in the form of an interface and stored as the target function functions required by the user to ensure the operation of the protocol stack.
[0046] Among them, the target function is written by referring to the structure of the virtual protocol stack, and the target function includes: a function for modifying the baud rate, a function for obtaining the current amount of received data, a 50us delay function, a function for setting and modifying the slave address, a function for starting the hardware interface, a function for stopping the hardware interface, a function for putting the hardware interface in a read state, a function for putting the hardware interface in a write state, a data frame sending function, a slave receiving data CRC error function, various function functions required by the slave to comply with the modbus protocol, a host request timeout function, a host request return exception code function, and various post-request function functions required by the host to comply with the modbus protocol.
[0047] Furthermore, the user tool module provides the user with preset operations for the protocol stack including:
[0048] The user tool module provides users with preset operations for the protocol stack through function functions, which facilitates the free and flexible configuration of the protocol stack, including: registration, uninstallation, and update functions of the master and slave protocol stacks, making the protocol stack easier to use. The function functions specifically include: protocol stack initialization function, mount function, uninstall function, state machine polling function, various function functions required by the host according to modbus, and various function functions required by the slave according to modbus.
[0049] This embodiment provides an enhanced Modbus protocol stack suitable for MCU, which optimizes the code and builds the framework to implement Modbus communication in micro-intelligent instruments that supports multiple masters and multiple slaves, does not require RTOS support, and does not require the use of interrupt and timer resources.
[0050] In short, during the operation of the protocol stack, the state machine realizes the operation of multi-master and multi-slave protocol stacks by operating the virtual protocol stack group and calling the user interface function; the user creates a virtual protocol stack and calls the user tool module to maintain and manage the virtual protocol stack group, thereby realizing the on-demand operation of the entire protocol stack.
[0051] Workflow:
[0052] ⅰ: The number of hosts and slaves planned to be used;
[0053] ⅱ: Virtualize a protocol stack group to manage the protocol stack, and virtualize the protocol stack according to the number of hosts and slaves;
[0054] ⅲ: Fill in the attributes for each protocol stack, including hardware correspondence, type, communication protocol, address, buffer array, and array size;
[0055] ⅳ: Write the corresponding interfaces of the host and slave, public interface: baud rate setting, 50us delay, start hardware, stop hardware, read close, write close, frame sending; slave interface: CRC error, each function code interface; host interface: request timeout interface, function code exception interface, each function code callback function;
[0056] ⅴ: Register and run each virtual protocol stack;
[0057] ⅵ: Polling call state machine.
[0058] In the whole process, all the operating parameters of each host or slave are saved in the independent memory space of the virtual protocol stack, and the virtual protocol stack group manages them as a whole. The state machine polls and accurately times the virtual protocol stack group through program optimization and pointers, and can realize multi-master and multi-slave without RTOS. In the specific functional implementation of the interface function, it is written in combination with the MCU's specific DMA function, without the need to enable the interrupt function. The delay function is implemented by directly reading the clock register. The operation of the modbus protocol stack can be realized without the need for system interrupts and timer resources throughout the process.
[0059] Example:
[0060] like Figure 1 As shown, this embodiment provides an enhanced Modbus protocol stack suitable for MCU, including: a state machine, a virtual protocol stack group, a virtual protocol stack, a user interface file, and a user toolbox;
[0061] As a cell element of the entire protocol stack, the virtual protocol stack can be organized using a structure, whose members include: its own ID information, operating status, serial port used, control pin, communication parameters, protocol type, slave address, receive buffer array, data volume, send buffer array, send flag, window period, TCP communication-specific ID information, register start address of the function code to be set, number of registers, and register array.
[0062] As the organization for managing and organizing the virtual protocol stack, the virtual protocol stack group is the core of the state machine access. It is responsible for the management of the virtual protocol stack and can be organized in the form of a pointer array to facilitate the loading and unloading operations of the virtual protocol stack.
[0063] The state machine is the core of the entire protocol stack and can be encapsulated in a function. To avoid using the system's interrupt resources, it can be called cyclically in the main loop, and the 50us delay provided by the user can be used to perform timing judgment on the modbus requirement for 35T.
[0064] The state machine can be designed with a branch structure into 5 states and 11 events. The 5 states are: idle, reading, processing data, writing, and window period, numbered 0-4; the 11 events are: slave turns to reading event, master turns to writing event, master-slave turns to processing data event, master turns to idle event, slave turns to writing event, slave turns to idle event, host enters window period event, host turns to reading event, host enters processing data event, slave turns to idle event, and initialization event, numbered 0-10.
[0065] The state change is driven by the occurrence of events, thus completing the operation of the entire state machine. The state machine first enters state 0 to determine the occurrence of events. If event 0 occurs, it enters state 1, if event 1 occurs, it enters state 3, and if event 10 occurs, it enters initialization action; in state 1, modify the event according to 35T, if event 2 occurs, it enters state 2; in state 2, if event 3 or 9 occurs, it enters state 0, if event 4 occurs, it enters state 3; in state 3, if event 5 occurs, it enters state 0, if event 6 occurs, it enters state 4; in state 4, if event 8 occurs, it enters state 0, if event 7 occurs, it enters state 1.
[0066] During the operation of the entire state machine, events 0, 2, 3, 4, 5, 6, 7, 8, 9, and 10 are changed by the state machine according to the current situation; whether event 1 occurs is determined by whether the user sends a slave request. When the corresponding event occurs, the user function interface in the corresponding interface file is triggered to execute the action written by the user.
[0067] The user interface file is the user interface provided by this protocol stack for users. Users can write the functions to be implemented in the corresponding interface functions for the protocol stack to call and implement the corresponding functions. It should include: the function of modifying the baud rate, the function of obtaining the current amount of received data, the 50us delay function, the function of setting and modifying the slave address, the function of starting the hardware interface, the function of stopping the hardware interface, the function of putting the hardware interface in the read state, the function of putting the hardware interface in the write state, the function of sending data frame, the function of the slave receiving data CRC error, the various function functions required by the slave to follow the modbus protocol, the host request timeout function, the host request return exception code function, and the host follow the various request functions required by the modbus protocol. The writing parameter of each function should be a virtual protocol stack structure to facilitate the implementation of multiple masters and multiple slaves.
[0068] The user toolbox is a set of functional functions that are pre-written to make it easier for people who use the protocol stack to operate the protocol stack. It is written according to the requirements and must include: protocol stack initialization function, mount function, unload function, state machine polling function, various functional functions required by the host according to modbus, and various functional functions required by the slave according to modbus.
[0069] The above five parts cooperate with each other to realize the operation of the protocol stack in a multi-master and multi-slave state. In the user interface function, the DMA function can be used to write the core sending function, receiving function, and receiving data volume acquisition function. The 50us delay function can be implemented using the system clock without interruption. In this way, the multi-master and multi-slave operation of the protocol stack can be realized without occupying system interrupt and timer resources, effectively solving the problem of requiring multi-master and multi-slave interaction when terminal resources are tight.
[0070] After designing the Modbus protocol according to the above implementation plan, the multi-master and multi-slave function is realized through the following steps:
[0071] i: The number of hosts and slaves planned to be used. In this embodiment, there are 2 hosts and 2 slaves.
[0072] ⅱ: Virtualize a protocol stack group to manage the protocol stack, virtualize a protocol stack, and generate 2 host virtual protocol stacks and 2 slave virtual protocol stacks.
[0073] ⅲ: Fill in the attributes for each protocol stack, including hardware correspondence, type, communication protocol, address, buffer array, and array size.
[0074] ⅳ: Write the corresponding interfaces of the host and slave, public interface: baud rate setting, 50us delay, start hardware, stop hardware, read close, write close, frame sending; slave interface: CRC error, various function code interfaces; host interface: request timeout interface, function code exception interface, various function code callback functions.
[0075] ⅴ: Initialize the protocol stack and mount each virtual protocol stack.
[0076] ⅵ: Polling call state machine.
[0077] The embodiments described above are only descriptions of the preferred embodiments of the present invention and are not intended to limit the scope of the present invention. Without departing from the design spirit of the present invention, various modifications and improvements made to the technical solutions of the present invention by ordinary technicians in this field should all fall within the protection scope determined by the claims of the present invention.
Claims
1. An enhanced Modbus protocol stack system suitable for MCU, characterized in that: include: State machine, virtual protocol stack group, virtual protocol stack, user interface file and user tool module; The state machine is used to operate and maintain the protocol stack based on the virtual protocol stack group and the user interface file; The virtual protocol stack group is used to manage the virtual protocol stack; The virtual protocol stack is used to organize operation information; The user interface file is used to store the target function that the protocol stack needs to be implemented by the user, wherein the target function is organized and formed by the user according to the functional requirements; The user tool module is used to provide the user with preset operations for the protocol stack; The state machine performs operation and maintenance of the protocol stack based on the virtual protocol stack group and the user interface file, including: The state machine includes several states and events, and polls the virtual protocol stack group in an event-driven state manner to receive data, send data, perform function analysis, manage the virtual protocol stack, and call and manage the user interface; The states include: idle, reading, processing data, writing, and window period; the events include: slave turns to reading event, host turns to writing event, master-slave turns to processing data event, host turns to idle event, slave turns to writing event, slave turns to idle event, host enters window period event, host turns to reading event, host enters processing data event, and initialization event; The state machine changes the event state of events other than the host turning to writing event according to the current situation. Whether the host turning to writing event occurs is determined by the state of the slave request sent by the user. The virtual protocol stack group manages the virtual protocol stack including: The virtual protocol stack group manages the loading and unloading operations of the virtual protocol stack in the form of a pointer array; The virtual protocol stack organization operation information includes: The virtual protocol stack uses a structure to organize operation information, and the operation information includes: virtual protocol stack ID information, operation status, serial port used, control pin, protocol type, slave address, receive buffer array, data volume, send buffer array, send flag, window period time, TCP communication special ID information, register start address of function code to be set, register number, register array; The target function is written by referring to the structure of the virtual protocol stack, and the target function includes: a function for modifying the baud rate, a function for obtaining the current amount of received data, a 50us delay function, a function for setting and modifying the slave address, a function for starting the hardware interface, a function for stopping the hardware interface, a function for putting the hardware interface in a read state, a function for putting the hardware interface in a write state, a data frame sending function, a function for CRC error of slave receiving data, a host request timeout function, and a host request return exception code function; The user tool module provides the user with preset operations for the protocol stack, including: The user tool module provides the user with preset operations for the protocol stack through functional functions, and the functional functions include: a protocol stack initialization function, a mounting function, an unmounting function, and a state machine polling function.
Citation Information
Patent Citations
An embedded MODBUS protocol stack suitable for communication among multiple devices
CN109714336A