Modbus address optimization method based on small stack chip
By defining multiple global arrays on the domestic small stack chip according to parameter modules and optimizing Modbus address management, the problem of insufficient stack space was solved, and the requirement of saving stack space and supporting logic programming in diesel generator controller was realized.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING CAMPOWER ELECTRIC SCIENCE & TECHNOLOGY CO LTD
- Filing Date
- 2022-12-30
- Publication Date
- 2026-05-01
AI Technical Summary
On domestically produced small stack chips, the Modbus address definition method is insufficient to meet the needs of diesel generator controllers, resulting in insufficient stack space and inability to perform complex logic programming.
Multiple global arrays defined by parameter modules are used to optimize Modbus address management. When reading and writing reserved address space, illusory operations are performed to save stack space.
It saves about 40% of stack space, meets the normal use of the Modbus protocol, and provides more space for logic code development, supporting the normal operation of domestic diesel generator functions and subsequent function expansion.
Smart Images

Figure CN116107918B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of technology, and in particular to a Modbus address optimization method based on a small stack chip. Background Technology
[0002] Currently, the supply of foreign chips is becoming increasingly severe in the current international environment. Many companies are starting to choose domestically produced chips, but due to technological limitations, the stack size of domestically produced chips is generally relatively small. However, diesel generator controllers require as many as 3000-4000 Modbus addresses, which is insufficient for development on chips with limited memory. In the past, Modbus address management relied heavily on large-stack foreign chips like STM32, so the definition of physical memory was relatively simple, typically involving a few global arrays (usually one each for input registers, holding registers, discrete registers, and coil registers). However, with the advancement of domestic chip development, the stack size of domestically produced chips is generally small; for example, GigaDevice's standard stack size is only 8K. Since diesel generator controllers typically require 3000-4000 registers, using the previous simple address definition method leaves very little stack space for logic code, making complex logic programming impossible. Therefore, a physical address definition method suitable for chips with small stack sizes is needed, allowing for Modbus protocol compliance while saving stack space for logic code programming. Summary of the Invention
[0003] The purpose of this invention is to provide a Modbus address optimization method based on a small stack chip, which is applicable to the programming of domestic small stack chips and can save some stack space for logic programming while the Modbus protocol is used normally.
[0004] The technical solution adopted in this invention is as follows:
[0005] A Modbus address optimization method based on a small stack chip, wherein reading data via Modbus specifically includes the following steps:
[0006] A: Upon receiving the Modbus read command, determine the starting address and the length to be read, and check if it exceeds the Modbus address range. If it does, generate an error response frame and proceed to step E; otherwise, proceed to the next step.
[0007] B: Traverse the structure, find the module parameter based on the data read address. If a specific module is found, read the reserved space data, fix it to 0, and then proceed to step C; otherwise, read the specific element value of the defined module array based on the data read address and then proceed to step C.
[0008] C: Save the read data to the response frame buffer, increment the read data address and read data length, return to step B, and continue until the data is read completely, then proceed to the next step;
[0009] D: Generate a response data frame.
[0010] E: Send a reply data frame.
[0011] The process of writing data in Modbus is the same as the process of reading data in Modbus, as detailed below:
[0012] A1: Upon receiving a Modbus write data frame command, determine the starting address and the length to be read, and check if it exceeds the Modbus address range. If it does, generate an error response frame and proceed to step E1; otherwise, proceed to the next step.
[0013] B1: Traverse the structure, find the module parameter based on the write data address. If a specific module is found, retrieve the written value from the write data frame command based on the write data address, write the specific element value of the defined module array, and then proceed to step C; otherwise, reserve space data, do not process it, and then proceed to step C1.
[0014] C1: Increment the write data address and the write data length, return to step B1, and continue until the write data is complete, then proceed to the next step;
[0015] D1: Generate write reply data frame
[0016] E1: Send a reply data frame.
[0017] This invention changes the original method of using several global arrays to multiple global arrays defined by parameter modules, thus saving the previously reserved space. Further, the read / write operation is such that reading from the reserved address space only reads 0, while writing to the reserved address space allows writing any data without actually writing to the physical address. This optimization saves approximately 40% of the original stack space, while the Modbus protocol remains functional. The saved stack space can be used for logic code development. Using this method maximizes stack space savings without affecting the normal operation of the Modbus protocol. The saved memory can be used to develop more new functions, which is of great significance in the development and use of current small-stack chips. This allows for the normal operation of domestically produced diesel generators under current conditions, while also providing technical possibilities for future functional expansion. Attached Figure Description
[0018] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0019] Figure 1 This is a flowchart of the data reading process of the present invention;
[0020] Figure 2 This is a flowchart for writing data for this invention. Detailed Implementation
[0021] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0022] like Figure 1 and 2 As shown, the Modbus data reading method of this invention specifically includes the following steps:
[0023] A: Upon receiving the Modbus read command, determine the starting address and the length to be read, and check if it exceeds the Modbus address range. If it does, generate an error response frame and proceed to step E; otherwise, proceed to the next step.
[0024] B: Traverse the structure and find the module parameter based on the data read address. If it is not in a specific module, it is reserved space data. After reading the reserved space data and fixing it to 0, proceed to step C; otherwise, read the specific element value of the defined module array based on the data read address and proceed to step C.
[0025] C: Save the read data to the response frame buffer, increment the read data address and read data length, return to step B, and continue until the data is read completely, then proceed to the next step;
[0026] D: Generate a response data frame.
[0027] E: Send a reply data frame.
[0028] The process of writing data in Modbus is the same as the process of reading data in Modbus, as detailed below:
[0029] A1: Upon receiving a Modbus write data frame command, determine the starting address and the length to be read, and check if it exceeds the Modbus address range. If it does, generate an error response frame and proceed to step E1; otherwise, proceed to the next step.
[0030] B1: Traverse the structure, find the module parameter based on the write data address. If a specific module is found, retrieve the written value from the write data frame command based on the write data address, write the specific element value of the defined module array, and then proceed to step C; otherwise, it is reserved space data, and no processing is performed before proceeding to step C1.
[0031] C1: Increment the write data address and the write data length, return to step B1, and continue until the write data is complete, then proceed to the next step;
[0032] D1: Generate write reply data frame
[0033] E1: Send a reply data frame.
[0034] This invention changes the original method of using several global arrays to multiple global arrays defined by parameter modules, thus saving the previously reserved space. Further, the read / write operation is such that reading from the reserved address space only reads 0, while writing to the reserved address space allows writing any data without actually writing to the physical address. This optimization saves approximately 40% of the original stack space, while the Modbus protocol remains functional. The saved stack space can be used for logic code development. Using this method maximizes stack space savings without affecting the normal operation of the Modbus protocol. The saved memory can be used to develop more new functions, which is of great significance in the development and use of current small-stack chips. This allows for the normal operation of domestically produced diesel generators under current conditions, while also providing technical possibilities for future functional expansion.
[0035] Specifically, the following examples illustrate this:
[0036] Modbus address definition method
[0037] Assume the controller has 3000 output registers (input registers use the same method; discrete registers and coil registers themselves don't consume much space and can be defined as separate arrays), which can be divided into 25 modules based on their functions. We first define 25 structures to store the information for these 25 modules, mainly including three important parameters: start address, end address, and offset address. After defining these, we define 25 arrays of different lengths according to the functional parameter modules to store the data. These 25 arrays are the actual physical addresses of the Modbus registers.
[0038] Modbus protocol read / write implementation
[0039] After defining the actual physical addresses, reading and writing data using Modbus essentially involves modifying the data in 25 arrays. When receiving a Modbus read data command, we first convert it into an index of the actual physical address and then read the data one by one. Since we haven't defined any reserved space, we also need to send 0 to the host when reading data from the reserved space. For example, let's assume 0~29 is the address range for basic parameters, 30~99 is the address range for reserved space, 100~112 is the address range for timer parameters, 113~119 is the address range for reserved space, and 120~152 is the address range for engine parameters. Assuming a read command is received to read 100 data items from starting address 10, the first step is to locate which array address 10 belongs to. By checking the start and end addresses of the 25 structures, it is found to be in the first array. This is then converted to the physical address of the actual array. The conversion method is to subtract the address offset from the starting address to obtain the position in the actual array, and then read the data. Then, the address is incremented sequentially according to the length, and when the address reaches the reserved space, 0 is read until the next module's array is entered, at which point data is read again. If an address beyond 3000 is read, it is treated as a normal invalid address. The write process is similar to the read process; see the read / write flowchart for details.
[0040] The following example, using a specific diesel generator controller Modbus address analysis, illustrates the conceptual process of this invention.
[0041] As mentioned earlier, diesel generator controllers typically have 3000-4000 Modbus registers, each generally defined as a 2-byte global array. Using the original definition method, this would require 5.7-7.9KB of stack space. Domestic chip manufacturers like GigaDevice, however, only require 8KB of stack space under standard conditions, which can be expanded to 12KB after modifying the underlying code. Therefore, it is necessary to find an economical physical address definition method that satisfies the Modbus protocol while also leaving sufficient stack space for the logic code.
[0042] Analyzing the Modbus address definition of the diesel generator controller, it's evident that its initial design involved dividing the address space into modules. However, a significant amount of address space was reserved between modules to allow for future functional expansion, resulting in some waste. For example, the address range for basic parameters is 0-29, while the address space for timer parameters is 100-112, with 30-99 reserved. These reserved registers can be read and written by Modbus but have no functional impact on the program. It is precisely because of this distribution that a new approach to defining the physical space can be proposed.
[0043] Regarding the optimization of Modbus physical addresses, as mentioned above based on the controller's address distribution method, we can adopt a different approach to define the physical addresses. Instead of using several global arrays, we can change to defining multiple global arrays based on parameter modules, thus saving the previously reserved space. However, the controller still needs to function correctly with the Modbus protocol, and the reserved address space must be able to be read and written correctly. But since we haven't defined the actual physical space for the reserved addresses, the read and write operations are essentially illusory. When reading from the reserved address space, only 0 can be read; when writing to the reserved address space, any data can be written without actually writing to the physical address. Optimizing in this way can save approximately 40% of the original stack space, while still allowing the Modbus protocol to function correctly. The saved stack space can then be used for logic code development.
[0044] Finally, program maintenance issues
[0045] After the method is used, the cost of future program maintenance also needs to be considered. However, maintaining the program is very convenient when using this method. Only the lengths of the 25 defined structure parameters and 25 arrays need to be modified. The maintenance time cost is not very high.
[0046] The relationship between the Modbus address table and the actual definitions is shown in the following example table (example 6012 is used because there are too many address definitions).
[0047]
[0048] Received a Modbus read command to read 100 addresses from 6010
[0049] First, based on the address to be read, the system searches within the defined structure for the starting address space. A loop through the search yields the result 6010, which is greater than 6000 and less than 6029. Therefore, the system starts reading from array 1.
[0050] Reply frame data is generated starting from the 10th element of array 1, increasing in increments according to the read length. When the length is greater than 6029 but less than 6100, the generated data is 0. Then the data from array 2 is used until the length reaches 6109.
[0051] The generated Modbus message frame is then sent back to complete the data reading process.
[0052] In the description of this invention, it should be noted that directional terms such as "center", "lateral", "longitudinal", "length", "width", "thickness", "up", "down", "front", "back", "left", "right", "vertical", "horizontal", "top", "bottom", "inner", "outer", "clockwise", and "counterclockwise" indicate the orientation and positional relationship based on the orientation or positional relationship shown in the accompanying drawings. They are only for the convenience of describing this invention and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. They should not be construed as limiting the specific protection scope of this invention.
[0053] It should be noted that the terms "first," "second," etc., used in the specification and claims of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be used interchangeably where appropriate for the embodiments of this application described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0054] Note that the above description is merely a preferred embodiment and application of the technical principles of the present invention. Those skilled in the art will understand that the present invention is not limited to the specific embodiments described herein, and various obvious changes, readjustments, and substitutions can be made without departing from the scope of protection of the present invention. Therefore, although the present invention has been described in detail through the above embodiments, the present invention is not limited to the specific embodiments described herein, and may include many other effective embodiments without departing from the concept of the present invention. The scope of the present invention is determined by the scope of the appended claims.
Claims
1. A Modbus address optimization method based on a small stack chip, characterized in that: Reading data from Modbus involves the following steps: A: Upon receiving the Modbus read command, determine the starting address and the length to be read, and check if it exceeds the Modbus address range. If it does, generate an error response frame and proceed to step E; otherwise, proceed to the next step. B: Traverse the structure, which stores information about the defined module array. The information about the defined module array includes the start address, end address, and offset address. Find the parameter of the defined module array based on the read data address. If it is not in the specific defined module array, it is reserved space data. After reading the reserved space data and fixing it to 0, proceed to step C. Otherwise, read the specific element value of the defined module array based on the read data address and proceed to step C. C: Save the read data to the response frame buffer, increment the read data address and read data length, return to step B, and continue until the data is read completely, then proceed to the next step; D: Generate a response data frame. E: Send a reply data frame.
2. The Modbus address optimization method based on a small stack chip according to claim 1, characterized in that: The process of writing data in Modbus is the same as the process of reading data in Modbus, as detailed below: A1: Upon receiving a Modbus write data frame command, determine the starting address and the length to be read, and check if it exceeds the Modbus address range. If it does, generate an error response frame and proceed to step E1; otherwise, proceed to the next step. B1: Traverse the structure, find the definition module array parameter based on the write data address. If the specific definition module array is found, retrieve the written value from the write data frame command based on the write data address, write the specific element value of the definition module array, and then proceed to step C; otherwise, reserve space data, do not process it, and then proceed to step C1. C1: Increment the write data address and the write data length, return to step B1, and continue until the write data is complete, then proceed to the next step; D1: Generate write reply data frame E1: Send a reply data frame.
Citation Information
Patent Citations
Realization method for NVMe extension and solid state disk
CN108549610A
Apparatus, system, and method for caching data
US20120210041A1