Modbus communication method and device based on data list structure body
By using data list structures and FIFO linked list buffers to manage commands, the problem of tightly binding protocols and parameters in MODBUS communication software was solved, improving the readability of the software and the stability and reliability of communication.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-29
- Publication Date
- 2026-03-24
AI Technical Summary
In existing MODBUS communication software designs, the communication protocol and communication parameter variables are tightly bound together, resulting in excessive coupling and correlation of the software code, making it difficult to maintain and upgrade. Furthermore, the communication stability and reliability are poor, and it is prone to failure due to command collisions.
By adopting a data list structure design, the communication protocol and communication parameter variables are made independent. Commands are managed through a FIFO linked list buffer, simplifying the state machine switching design and achieving decoupling between the protocol and parameters.
It improves the readability and portability of the software, reduces the risk of command collisions, enhances the stability and reliability of communication, and simplifies the software design process.
Smart Images

Figure CN116366391B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of communication technology, and in particular to a MODBUS communication method, apparatus, electronic device and storage medium based on a data list structure. Background Technology
[0002] MODBUS communication is an application layer message transmission protocol at layer 7 of the OSI model. It is used in different types of buses or networks to provide communication services between devices. It transmits data based on the address of data variables, using the number of access parameters or the parameter value as the data unit.
[0003] Currently, most MODBUS communication software designs deeply bind the communication protocol and communication parameter variables, directly embedding the communication parameter variables within the communication protocol. Whether sending or receiving data, the communication protocol and communication parameter variables are always integrated. This results in an overly tight binding and complex dependency between the MODBUS communication protocol and communication parameter variables. If the attributes of the communication parameter variables (such as register addresses, read / write access permissions, parameter value ranges, etc.) change, the MODBUS communication protocol must be modified accordingly. Furthermore, the order of MODBUS communication parameter variable register addresses in a product software is not necessarily continuous. In existing MODBUS communication software designs, if the communication parameter variable register addresses are not continuous, the software design typically distributes the sending and parsing functions of the communication parameter variables into different code segments according to the number of segments of the communication parameter variable register addresses. In addition, since the MODBUS protocol has multiple communication commands, the more commands there are, the more segmentation processing the software code becomes, leading to many disadvantages such as excessive resource consumption.
[0004] In addition to sending query commands to the slave device, the master device usually needs to send some configuration parameters to the slave device during MODBUS communication. When the master device is setting parameters to the slave device, the entire communication state and steps are unknown. The master device may be sending other commands to the slave device, the master device may be waiting for a reply from the slave device, or the slave device may be replying to the master device, etc. Currently, many MODBUS communication mechanism software designs either do not consider the above situations, resulting in data collision and damage on the communication transmission line, leading to a sharp increase in the communication failure rate and increasing the unreliability of communication; or they adopt a cumbersome multi-state machine switching design pattern, which increases the complexity of the software.
[0005] In summary, the current design of MODBUS communication software results in overly tight coupling and correlation of software code, leading to poor readability, difficulty in software maintenance and upgrades, and cumbersome porting of previously designed MODBUS communication protocol software by users. The reusability rate is too low, and improper porting can easily lead to new errors. Almost every time the product's communication protocol requirements change, the MODBUS communication protocol has to be rewritten, which not only increases the workload of product developers but also prolongs the product development cycle. Summary of the Invention
[0006] In view of this, embodiments of this application provide a MODBUS communication method, apparatus, electronic device, and storage medium based on a data list structure, which enables communication protocols and communication parameter variables to be independent and completely decoupled.
[0007] The technical solution of this application embodiment is implemented as follows:
[0008] In a first aspect, embodiments of this application provide a MODBUS communication method based on a data list structure, applied to a host, comprising the following steps:
[0009] In response to a command storage request, at least one command is sequentially placed into the linked list cache via the target interface;
[0010] In response to a command extraction request, extract the target command from the at least one command;
[0011] Based on the target command, the first communication parameters matching the target command are obtained by traversing the preset data list structure, and the first communication parameters are packaged to obtain the host command.
[0012] The host command is sent to the slave device, so that the slave device responds to the host command based on the data list structure.
[0013] In one possible implementation, the data list structure includes the following members:
[0014] The parameters include the number of bits in the parameter variable, the parameter storage pointer, the parameter register address, the parameter access permissions, the parameter modification flag, the parameter variable value compliance check callback function, the parameter index number, the data list type, and the data list name.
[0015] The number of bits of the parameter variable is used to identify the number of bits occupied by the communication parameter variable in the memory;
[0016] The parameter storage pointer is used to point to the storage address of the communication parameter variable;
[0017] The parameter register address is the register address value allocated to the communication parameter variable during MODBUS communication protocol transmission;
[0018] The parameter access permissions include three categories: read-only, write-only, and read-write.
[0019] The parameter modification flag is used to indicate when the parameter value changes;
[0020] The parameter variable value compliance judgment callback function is used to judge the limited value range of the set parameters;
[0021] The parameter index number is used in conjunction with the parameter variable value compliance judgment callback function to perform limit value judgment and modification on the corresponding communication parameter according to the index number;
[0022] The data list type is a container type that stores the number of bits of parameter variables, parameter storage pointer, parameter register address, parameter access permission, parameter modification flag, parameter variable value compliance judgment callback function, and parameter index number.
[0023] The data list name is the container name for storing the number of bits of the parameter variable, the parameter storage pointer, the parameter register address, the parameter access permission, the parameter modification flag, the parameter variable value compliance judgment callback function, and the parameter index number.
[0024] In one possible implementation, the linked list buffer is a FIFO linked list buffer, and the step of responding to a command storage request by sequentially placing at least one command into the linked list buffer through the target interface includes:
[0025] In response to a command storage request, the empty / full status of the FIFO linked list buffer is detected; when the FIFO linked list buffer is not full, at least one command is sequentially placed into the linked list buffer through the target interface;
[0026] When the FIFO linked list buffer is full, feeding is stopped.
[0027] In one possible implementation, the linked list buffer is a FIFO linked list buffer, and the step of retrieving the target command from the at least one command in response to the command retrieval request includes:
[0028] In response to the command retrieval request, the empty / full state of the FIFO linked list buffer is detected;
[0029] When the FIFO linked list buffer is not empty, the target command is retrieved from the FIFO linked list buffer in a first-in-first-out order;
[0030] When the FIFO linked list buffer is empty, retrieval stops.
[0031] Secondly, this application also provides a MODBUS communication method based on a data list structure, applied to a slave device, comprising the following steps:
[0032] In response to a host command sent by the host, the host command is parsed to obtain first parsed data, and the CRC16 of the first parsed data is verified.
[0033] After the CRC16 check of the first parsed data is correct, the second communication parameter matching the first parsed data is obtained by traversing the preset data list structure according to the first parsed data, and the second communication parameter is packaged to obtain the first response data.
[0034] The first response data is verified. When the verification result of the first response data is normal, the first response data is sent to the host so that the host responds to the first response data based on the data list structure.
[0035] Thirdly, embodiments of this application also provide a MODBUS communication method based on a data list structure, applied to a host, comprising the following steps:
[0036] In response to the first response data sent by the slave device, the first response data is parsed to obtain the second parsed data, and the CRC16 of the second parsed data is verified.
[0037] Once the CRC16 check of the second parsed data is successful, the third communication parameter matching the second parsed data is obtained by traversing the preset data list structure based on the second parsed data, and the third communication parameter is packaged to obtain the second response data.
[0038] Fourthly, embodiments of this application also provide a MODBUS communication device based on a data list structure, the device comprising:
[0039] The first response module is used to respond to command storage requests and put at least one command into the linked list cache sequentially through the target interface;
[0040] The second response module is used to extract the target command from the at least one command in response to the command extraction request;
[0041] The first traversal and packet assembly module is used to traverse from a preset data list structure to obtain the first communication parameters that match the target command based on the target command, and to assemble the first communication parameters into a packet to obtain the host command.
[0042] The first sending module is used to send the host command to the slave device, so that the slave device can respond to the host command based on the data list structure.
[0043] Fifthly, embodiments of this application also provide a MODBUS communication device based on a data list structure, the device comprising:
[0044] The third response module is used to respond to a host command sent by the host, parse the host command to obtain first parsed data, and perform CRC16 verification on the first parsed data.
[0045] The second traversal and packet assembly module is used to obtain the second communication parameters that match the first parsed data by traversing from a preset data list structure according to the first parsed data after the CRC16 check of the first parsed data is correct, and to assemble the second communication parameters into a packet to obtain the first response data.
[0046] The second sending module is used to verify the first response data. When the verification result of the first response data is normal, the module sends the first response data to the host so that the host responds to the first response data based on the data list structure.
[0047] Sixthly, embodiments of this application also provide an electronic device, including: a processor, a storage medium, and a bus. The storage medium stores machine-readable instructions executable by the processor. When the electronic device is running, the processor communicates with the storage medium via the bus. The processor executes the machine-readable instructions to perform the MODBUS communication method based on a data list structure as described in any of the first aspects, or to perform the MODBUS communication method based on a data list structure as described in any of the second aspects, or to perform the MODBUS communication method based on a data list structure as described in any of the third aspects.
[0048] Fourthly, embodiments of this application also provide a computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, performs the MODBUS communication method based on a data list structure as described in any of the first aspects, or performs the MODBUS communication method based on a data list structure as described in any of the second aspects, or performs the MODBUS communication method based on a data list structure as described in any of the third aspects.
[0049] The embodiments of this application have the following beneficial effects:
[0050] To address the shortcomings and deficiencies of existing technologies, a novel "data list structure" design approach is adopted. All communication parameter variables are listed in the structure as a data list, forming a "data list set." This allows the communication protocol and communication parameter variables to be independent, completely decoupling them. This greatly solves the problem of overly tight binding and complex dependencies between the MODBUS communication protocol and communication parameter variables. It also significantly reduces the coupling and correlation of the software code, enhances the readability and portability of the software, and improves its reusability. Meanwhile, a FIFO linked list buffer design is adopted for commands issued by the master in the MODBUS communication protocol. When the master needs to issue a command to the slave, the command is put into the FIFO linked list buffer one by one. Then the master retrieves the commands from the FIFO linked list buffer one by one. When the communication bus is idle, the commands stored in the FIFO linked list buffer are sent to the slave first, avoiding collision damage caused by data on the communication transmission line and improving the stability, reliability and real-time performance of the communication. In terms of communication management, the cumbersome multi-state machine switching design pattern is abandoned, simplifying the software design process and enhancing the organization and readability of the software. Attached Figure Description
[0051] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0052] Figure 1 This is a flowchart illustrating steps S101-S104 provided in the embodiments of this application;
[0053] Figure 2 This is a flowchart illustrating steps S201-S203 provided in the embodiments of this application;
[0054] Figure 3 This is a flowchart illustrating steps S301-S302 provided in the embodiments of this application;
[0055] Figure 4 This is a flowchart of the host protocol stack sending commands provided in an embodiment of this application;
[0056] Figure 5 This is a control flowchart of the host command FIFO linked list buffer provided in the embodiments of this application;
[0057] Figure 6 It is a component of the MODBUS communication protocol stack of the data list structure provided in the embodiments of this application;
[0058] Figure 7 This is an example diagram of a data list structure provided in an embodiment of this application;
[0059] Figure 8 This is a flowchart of the slave protocol stack responding to the host command provided in an embodiment of this application;
[0060] Figure 9 This is a flowchart of the host protocol stack parsing slave response provided in an embodiment of this application;
[0061] Figure 10 This is a schematic diagram of the structure of the MODBUS communication device based on a data list structure provided in the embodiments of this application;
[0062] Figure 11 This is a schematic diagram of another MODBUS communication device based on a data list structure provided in an embodiment of this application;
[0063] Figure 12 This is a schematic diagram of the composition structure of the electronic device provided in the embodiments of this application. Detailed Implementation
[0064] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. It should be understood that the accompanying drawings in this application are for illustrative and descriptive purposes only and are not intended to limit the scope of protection of this application. Furthermore, it should be understood that the schematic drawings are not drawn to scale. The flowcharts used in this application illustrate operations implemented according to some embodiments of this application. It should be understood that the operations in the flowcharts may not be implemented in sequence, and steps without logical contextual relationships may be reversed or implemented simultaneously. In addition, those skilled in the art, guided by the content of this application, may add one or more other operations to the flowcharts, or remove one or more operations from the flowcharts.
[0065] In the following description, references are made to “some embodiments,” which describe a subset of all possible embodiments. However, it is understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.
[0066] Furthermore, the described embodiments are merely some, not all, of the embodiments of this application. The components of the embodiments of this application described and illustrated herein can typically be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of this application provided in the accompanying drawings is not intended to limit the scope of the claimed application, but merely to illustrate selected embodiments of the application. All other embodiments obtained by those skilled in the art based on the embodiments of this application without inventive effort are within the scope of protection of this application.
[0067] In the following description, the terms "first, second, third" are used merely to distinguish similar objects and do not represent a specific ordering of objects. It is understood that "first, second, third" may be interchanged in a specific order or sequence where permitted, so that the embodiments of this application described herein can be implemented in an order other than that illustrated or described herein.
[0068] It should be noted that the term "comprising" will be used in the embodiments of this application to indicate the presence of the features declared thereafter, but does not exclude the addition of other features.
[0069] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used herein is for the purpose of describing embodiments of this application and is not intended to limit this application.
[0070] See Figure 1 , Figure 1 This is a flowchart illustrating steps S101-S104 of the MODBUS communication method based on a data list structure provided in this application embodiment. Applied to a host, it will be combined with... Figure 1 Steps S101-S104 are explained below.
[0071] Step S101: In response to the command storage request, at least one command is sequentially placed into the linked list buffer through the target interface;
[0072] Step S102: In response to the command extraction request, extract the target command from the at least one command;
[0073] Step S103: Based on the target command, traverse the preset data list structure to obtain the first communication parameters that match the target command, and assemble the first communication parameters into a packet to obtain the host command;
[0074] Step S104: The host command is sent to the slave device so that the slave device responds to the host command based on the data list structure.
[0075] The MODBUS communication method based on the data list structure described above addresses the shortcomings and deficiencies of existing technologies by adopting a novel "data list structure" design. All communication parameter variables are listed in the structure as a data list, forming a "data list set." This allows the communication protocol and communication parameter variables to be independent, completely decoupling them. This significantly solves the problem of overly tight binding and complex dependencies between the MODBUS communication protocol and communication parameter variables. It also greatly reduces the coupling and correlation of the software code, enhances the readability and portability of the software, and improves its reusability. Meanwhile, a FIFO linked list buffer design is adopted for commands issued by the master in the MODBUS communication protocol. When the master needs to issue a command to the slave, the command is put into the FIFO linked list buffer one by one. Then the master retrieves the commands from the FIFO linked list buffer one by one. When the communication bus is idle, the commands stored in the FIFO linked list buffer are sent to the slave first, avoiding collision damage caused by data on the communication transmission line and improving the stability, reliability and real-time performance of the communication. In terms of communication management, the cumbersome multi-state machine switching design pattern is abandoned, simplifying the software design process and enhancing the organization and readability of the software.
[0076] The exemplary steps described above in the embodiments of this application will be explained below.
[0077] In step S101, in response to the command storage request, at least one command is sequentially placed into the linked list buffer through the target interface.
[0078] For example, see Figure 4 , Figure 4 This is a flowchart of the host protocol stack sending commands provided in an embodiment of this application, such as... Figure 4 As shown, the host sends a command, enters the communication protocol stack (host protocol stack), and then selects the corresponding target interface (host read coil command interface, host read register command interface, host write single or multiple coil command interface, host write single or multiple register command interface) according to the specific command sent, and stores the command in the linked list buffer in sequence.
[0079] In some embodiments, the linked list buffer is a FIFO linked list buffer, and the step of responding to a command storage request by sequentially placing at least one command into the linked list buffer through the target interface includes:
[0080] In response to the command storage request, the empty / full status of the FIFO linked list buffer is detected;
[0081] When the FIFO linked list buffer is not full, at least one command is sequentially placed into the linked list buffer through the target interface;
[0082] When the FIFO linked list buffer is full, feeding is stopped.
[0083] For example, see Figure 5 , Figure 5 This is a control flowchart of the host command FIFO linked list buffer provided in the embodiments of this application, such as... Figure 5 As shown, the host FIFO list buffer is first initialized. After the host sends a storage command, it is necessary to check whether the FIFO list buffer is full. The command can only be put into the FIFO list buffer when the FIFO list buffer is not full; otherwise, the storage will be stopped.
[0084] In step S102, in response to a command extraction request, a target command is extracted from the at least one command.
[0085] In some embodiments, the linked list cache is a FIFO linked list cache, and the step of retrieving the target command from the at least one command in response to a command retrieval request includes:
[0086] In response to the command retrieval request, the empty / full state of the FIFO linked list buffer is detected;
[0087] When the FIFO linked list buffer is not empty, the target command is retrieved from the FIFO linked list buffer in a first-in-first-out order;
[0088] When the FIFO linked list buffer is empty, retrieval stops.
[0089] For examples, see below. Figure 5 When the host sends the extraction command, it also needs to check the empty or full state of the FIFO linked list buffer. Only when the FIFO linked list buffer is not empty can the command be put into the host protocol stack. The host protocol stack of this application includes a data list structure. In the subsequent processing, the data list set is traversed and then packets are assembled and sent.
[0090] In step S103, based on the target command, the first communication parameters matching the target command are obtained by traversing from the preset data list structure, and the first communication parameters are packaged to obtain the host command.
[0091] For examples, see below. Figure 4In the communication protocol stack (host protocol stack), depending on the command (host read holding register traversal, host read coil traversal, host write register traversal, host write coil traversal), the first communication parameter matching the target command is traversed from the data list set (a specific lower-level representation of the data list structure, which is a data list set formed by listing all communication parameter variables in the form of a data list in the data list structure), and then the parameters are sorted and packetized to finally obtain the host command.
[0092] In step S104, the host command is sent to the slave device so that the slave device responds to the host command based on the data list structure.
[0093] For examples, see below. Figure 4 The host command is sent to the slave device through the host data packet transmission interface and the host data transmission sending interface. The slave device responds to the host command based on the data list structure.
[0094] In some embodiments, see Figure 6 , Figure 6 It is a component of the MODBUS communication protocol stack of the data list structure provided in the embodiments of this application, such as Figure 6 As shown, the MODBUS communication protocol stack based on the data list structure is a new type of MODBUS communication protocol stack software. It includes host protocol stack software and slave protocol stack software, strictly adheres to the MODBUS transmission standard specification, has a complete error and exception handling mechanism, and can be used in various MODBUS communication devices. The physical layer supports RTU and Ethernet. It adopts a brand-new "data list structure" design concept, listing all communication parameter variables in the form of a data list in the structure to construct a "data list set". In addition to the data list structure, it also includes traversal and comparison of the data list structure, MODBUS data frame packet assembly algorithm, parsing algorithm, and send and receive interface functions.
[0095] It uses a data list structure as a carrier to completely decouple the communication protocol and communication parameter variables, thus breaking the coupling between them. Therefore, the data list structure is the core component of this MODBUS communication protocol stack software. The entire protocol stack always revolves around the data list structure, searching and addressing the register addresses of the communication parameter variables around the data list structure. Both the host protocol stack and the slave protocol stack revolve around the communication parameter variables in the data list structure for packet assembly, transmission, reception, and parsing.
[0096] See Figure 7 , Figure 7This is an example diagram of a data list structure provided in an embodiment of this application. In some embodiments, the data list structure includes the following members:
[0097] The parameters include the number of bits in the parameter variable, the parameter storage pointer, the parameter register address, the parameter access permissions, the parameter modification flag, the parameter variable value compliance check callback function, the parameter index number, the data list type, and the data list name.
[0098] In some embodiments, if a functional upgrade of the protocol stack is required, additional members can be added to the data list structure. Between the same master-slave communication group, the "parameter register address" in the data list structure on both the master and slave sides must be completely identical; other members are specified according to the respective needs of the master and slave.
[0099] The bit count of the parameter variables is used to identify the number of bits occupied by the communication parameter variables in memory. In the MODBUS protocol, coil states, discrete quantities, etc., are usually defined as bool, unsigned char, or unsigned short data types, occupying only 1 bit in the actual storage address. Communication parameter variables with values ranging from -128 to 255 are usually defined as signed char or unsigned char data types, occupying 8 bits. Communication parameter variables with values ranging from -32768 to 32767 are usually defined as unsigned short or signed short data types, occupying 16 bits. In the data list set, bit counts must be set for each communication parameter variable, explicitly indicating that when the protocol stack reads or writes communication parameter variables, it must access their respective storage areas according to the pre-set bit counts to complete the read / write of the corresponding communication parameter variables. Otherwise, the protocol stack cannot know the number of bits occupied by each parameter in its storage area.
[0100] The parameter storage pointer is used to point to the storage address of the communication parameter variable; pointing to a specific communication parameter variable in the form of a pointer avoids repeatedly defining parameter variables with the same meaning in the software code, and also facilitates direct reading and writing operations on the value of the communication parameter variable. The maximum value of the MODBUS protocol transmission register parameter is 2 bytes, so the pointer is defined as an unsigned short pointer type.
[0101] The parameter register address (in) Figure 7 The parameter register address is the register address value allocated to the communication parameter variable in the MODBUS communication protocol transmission. The MODBUS protocol specifies that the register address is 2 bytes, so the "parameter register address" is defined as an unsigned short data type.
[0102] The parameter access permissions include three categories: read-only, write-only, and read-write. The MODBUS protocol specifies "read-only" and "read-write" attributes for communication parameter variables. These attributes are divided into three categories: "read-only," "write-only," and "read-write." The access permissions for each communication parameter variable are specifically defined in the data list set beforehand. If the transmitted parameter value does not meet the predefined access attributes, the parameter value will be ignored to prevent accidental modification of parameter values during communication transmission, thereby ensuring the stability and reliability of product operation.
[0103] The parameter modification flag is used to indicate when the parameter value changes; specifically, it is used to identify situations where the parameter value needs to be forwarded to the next level sub-component via MODBUS communication through an intermediate component; when the parameter value changes, the protocol stack reserves this flag for other functional modules of the product software to call and judge, and forward the parameter value to the next level sub-component in a timely manner.
[0104] The parameter variable value compliance judgment callback function is used to judge the limited value range of the set parameters; it exists in the form of a callback function, and users can write corresponding parameter variable value compliance judgment processing functions according to the parameter requirements of the callback function; when the transmitted value does not conform to the limited value range of the communication parameter, the modification of this communication parameter value is ignored.
[0105] The parameter index number is used in conjunction with the parameter variable value compliance judgment callback function to perform limit value judgment and modification on the corresponding communication parameter according to the index number;
[0106] The data list type is a container type that stores the number of bits of parameter variables, parameter storage pointer, parameter register address, parameter access permissions, parameter modification flag, parameter variable value compliance judgment callback function, and parameter index number, and is usually a List;
[0107] The name of the data list is the container name for storing the number of bits of the parameter variable, the parameter storage pointer, the parameter register address, the parameter access permission, the parameter modification flag, the parameter variable value compliance judgment callback function, and the parameter index number, i.e., the name of the List.
[0108] See Figure 2 , Figure 2 This is a flowchart illustrating steps S201-S203 of the MODBUS communication method based on a data list structure provided in this application embodiment. Applied to the slave device, it combines... Figure 2 Steps S201-S203 will be explained as shown.
[0109] In step S201, in response to a host command sent by the host, the host command is parsed to obtain first parsed data, and the CRC16 of the first parsed data is verified.
[0110] In step S202, after the CRC16 check of the first parsed data is correct, the second communication parameter matching the first parsed data is obtained by traversing the preset data list structure according to the first parsed data, and the second communication parameter is packaged to obtain the first response data.
[0111] In step S203, the first response data is verified. When the verification result of the first response data is normal, the first response data is sent to the host so that the host responds to the first response data based on the data list structure.
[0112] For example, see Figure 8 , Figure 8 This is a flowchart of the slave protocol stack responding to the host command provided in an embodiment of this application, such as... Figure 8 As shown, after receiving a command from the host, the system enters the communication protocol stack (slave protocol stack). First, it needs to parse the data frame to extract the address, function code, etc., to obtain the first parsed data. Then, it checks the CRC16 checksum. After verification, based on the specific first parsed data (read holding register, read coil, read / write multiple registers, mask write register, write multiple registers, read input register, write single register, write single coil, write multiple coils, read discrete input), it iterates through the data list to obtain the second communication parameters that match the first parsed data. Then, it assembles the packets to form the first response data and determines whether it is normal. If it is normal, it sends the response data; if it is abnormal, it returns an error code.
[0113] See Figure 3 , Figure 3 This is a flowchart illustrating steps S301-S302 of the MODBUS communication method based on a data list structure provided in this application embodiment. Applied to a host, it will be combined with... Figure 3 Steps S301-S302 shown will be explained.
[0114] In step S301, in response to the first response data sent by the slave device, the first response data is parsed to obtain second parsed data, and the CRC16 of the second parsed data is verified.
[0115] In step S302, after the CRC16 check of the second parsed data is correct, the third communication parameter matching the second parsed data is obtained by traversing the preset data list structure according to the second parsed data, and the third communication parameter is packaged to obtain the second response data.
[0116] As an example, see Figure 9 , Figure 9 This is a flowchart of the host protocol stack parsing slave response provided in an embodiment of this application, as shown below. Figure 9 As shown, the host needs to parse the response data (second response data) sent by the slave. After receiving the second response data sent by the slave, it enters the communication protocol stack (host protocol stack). It still parses the data frame first to extract the address, function code, etc., to obtain the second parsed data. Then, it checks the CRC16 checksum. After the checksum is correct, it iterates through the data list set according to the specific second parsed data (read holding register, read coil, read / write multiple registers, mask write register, write multiple registers, read input register, write single register, write single coil, write multiple coils, read discrete input) to obtain the third communication parameters that match the second parsed data. Then, it assembles the packets to form the second response data.
[0117] In summary, the embodiments of this application have the following beneficial effects:
[0118] To address the shortcomings and deficiencies of existing technologies, a novel "data list structure" design approach is adopted. All communication parameter variables are listed in the structure as a data list, forming a "data list set." This allows the communication protocol and communication parameter variables to be independent, completely decoupling them. This greatly solves the problem of overly tight binding and complex dependencies between the MODBUS communication protocol and communication parameter variables. It also significantly reduces the coupling and correlation of the software code, enhances the readability and portability of the software, and improves its reusability. Meanwhile, a FIFO linked list buffer design is adopted for commands issued by the master in the MODBUS communication protocol. When the master needs to issue a command to the slave, the command is put into the FIFO linked list buffer one by one. Then the master retrieves the commands from the FIFO linked list buffer one by one. When the communication bus is idle, the commands stored in the FIFO linked list buffer are sent to the slave first, avoiding collision damage caused by data on the communication transmission line and improving the stability, reliability and real-time performance of the communication. In terms of communication management, the cumbersome multi-state machine switching design pattern is abandoned, simplifying the software design process and enhancing the organization and readability of the software.
[0119] Based on the same inventive concept, this application also provides a MODBUS communication device based on a data list structure, which corresponds to the MODBUS communication method based on a data list structure in the first aspect embodiment. Since the principle of the device in this application embodiment is similar to the above-mentioned MODBUS communication method based on a data list structure, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be described again.
[0120] like Figure 10 As shown, Figure 10 This is a schematic diagram of the structure of a MODBUS communication device based on a data list structure provided in an embodiment of this application. The MODBUS communication device based on a data list structure includes:
[0121] The first response module 1001 is used to respond to a command storage request by sequentially placing at least one command into the linked list cache through the target interface;
[0122] The second response module 1002 is used to extract the target command from the at least one command in response to the command extraction request;
[0123] The first traversal and packet assembly module 1003 is used to traverse from a preset data list structure to obtain the first communication parameters that match the target command based on the target command, and to assemble the first communication parameters into a packet to obtain the host command.
[0124] The first sending module 1004 is used to send the host command to the slave device, so that the slave device responds to the host command based on the data list structure.
[0125] Those skilled in the art should understand that Figure 10 The functions of each unit in the MODBUS communication device based on the data list structure shown can be understood by referring to the relevant description of the MODBUS communication method based on the data list structure mentioned above. Figure 10 The functions of each unit in the MODBUS communication device based on the data list structure shown can be implemented by a program running on a processor or by specific logic circuits.
[0126] In one possible implementation, the data list structure used by the first traversal grouping module includes the following members:
[0127] The parameters include the number of bits in the parameter variable, the parameter storage pointer, the parameter register address, the parameter access permissions, the parameter modification flag, the parameter variable value compliance check callback function, the parameter index number, the data list type, and the data list name.
[0128] The number of bits of the parameter variable is used to identify the number of bits occupied by the communication parameter variable in the memory;
[0129] The parameter storage pointer is used to point to the storage address of the communication parameter variable;
[0130] The parameter register address is the register address value allocated to the communication parameter variable during MODBUS communication protocol transmission;
[0131] The parameter access permissions include three categories: read-only, write-only, and read-write.
[0132] The parameter modification flag is used to indicate when the parameter value changes;
[0133] The parameter variable value compliance judgment callback function is used to judge the limited value range of the set parameters;
[0134] The parameter index number is used in conjunction with the parameter variable value compliance judgment callback function to perform limit value judgment and modification on the corresponding communication parameter according to the index number;
[0135] The data list type is a container type that stores the number of bits of parameter variables, parameter storage pointer, parameter register address, parameter access permission, parameter modification flag, parameter variable value compliance judgment callback function, and parameter index number.
[0136] The data list name is the container name for storing the number of bits of the parameter variable, the parameter storage pointer, the parameter register address, the parameter access permission, the parameter modification flag, the parameter variable value compliance judgment callback function, and the parameter index number.
[0137] In one possible implementation, the linked list buffer is a FIFO linked list buffer. The first response module 1001, in response to a command storage request, sequentially places at least one command into the linked list buffer via the target interface, including:
[0138] In response to the command storage request, the empty / full status of the FIFO linked list buffer is detected;
[0139] When the FIFO linked list buffer is not full, at least one command is sequentially placed into the linked list buffer through the target interface;
[0140] When the FIFO linked list buffer is full, feeding is stopped.
[0141] In one possible implementation, the linked list buffer is a FIFO linked list buffer, and the second response module 1002, in response to the command retrieval request, retrieves the target command from the at least one command, including:
[0142] In response to the command retrieval request, the empty / full state of the FIFO linked list buffer is detected;
[0143] When the FIFO linked list buffer is not empty, the target command is retrieved from the FIFO linked list buffer in a first-in-first-out order;
[0144] When the FIFO linked list buffer is empty, retrieval stops.
[0145] The aforementioned MODBUS communication device based on a data list structure addresses the shortcomings and deficiencies of existing technologies by adopting a novel "data list structure" design approach. All communication parameter variables are listed in the structure as a data list, forming a "data list set." This allows the communication protocol and communication parameter variables to be independent, completely decoupling them. This significantly solves the problem of overly tight binding and complex dependencies between the MODBUS communication protocol and communication parameter variables. It also greatly reduces the coupling and correlation of software code, enhances software readability and portability, and improves the reusability of the software. Meanwhile, a FIFO linked list buffer design is adopted for commands issued by the master in the MODBUS communication protocol. When the master needs to issue a command to the slave, the command is put into the FIFO linked list buffer one by one. Then the master retrieves the commands from the FIFO linked list buffer one by one. When the communication bus is idle, the commands stored in the FIFO linked list buffer are sent to the slave first, avoiding collision damage caused by data on the communication transmission line and improving the stability, reliability and real-time performance of the communication. In terms of communication management, the cumbersome multi-state machine switching design pattern is abandoned, simplifying the software design process and enhancing the organization and readability of the software.
[0146] Based on the same inventive concept, this application also provides a MODBUS communication device based on a data list structure, which corresponds to the MODBUS communication method based on a data list structure in the second aspect embodiment. Since the principle of the device in this application embodiment is similar to the above-mentioned MODBUS communication method based on a data list structure, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be described again.
[0147] like Figure 11 As shown, Figure 11 This is a schematic diagram of another MODBUS communication device based on a data list structure provided in an embodiment of this application. The other MODBUS communication device based on a data list structure includes:
[0148] The third response module 1101 is used to respond to a host command sent by the host, parse the host command to obtain first parsed data, and perform CRC16 verification on the first parsed data.
[0149] The second traversal and packet assembly module 1102 is used to obtain the second communication parameters that match the first parsed data by traversing from a preset data list structure according to the first parsed data after the CRC16 check of the first parsed data is correct, and to assemble the second communication parameters into a packet to obtain the first response data.
[0150] The second sending module 1103 is used to verify the first response data. When the verification result of the first response data is normal, it sends the first response data to the host so that the host responds to the first response data based on the data list structure.
[0151] like Figure 12 As shown, Figure 12 This is a schematic diagram of the composition structure of the electronic device 1200 provided in the embodiments of this application. The electronic device 1200 includes:
[0152] The device 1200 includes a processor 1201, a storage medium 1202, and a bus 1203. The storage medium 1202 stores machine-readable instructions executable by the processor 1201. When the electronic device 1200 is running, the processor 1201 communicates with the storage medium 1202 via the bus 1203. The processor 1201 executes the machine-readable instructions to perform the steps of the MODBUS communication method based on a data list structure as described in the embodiments of this application.
[0153] In practical applications, the various components in the electronic device 1200 are coupled together via bus 1203. It is understood that bus 1203 is used to achieve communication between these components. In addition to a data bus, bus 1203 also includes a power bus, a control bus, and a status signal bus. However, for clarity, in... Figure 12 The general labeled all buses as Bus 1203.
[0154] To address the shortcomings and deficiencies of existing technologies, the aforementioned electronic device adopts a novel "data list structure" design approach. All communication parameter variables are listed in the structure as a data list, forming a "data list set." This allows the communication protocol and communication parameter variables to operate independently, completely decoupling them. This significantly solves the problem of overly tight binding and complex dependencies between the MODBUS communication protocol and communication parameter variables. It also greatly reduces the coupling and correlation of software code, enhances software readability and portability, and improves the software's reusability. Meanwhile, a FIFO linked list buffer design is adopted for commands issued by the master in the MODBUS communication protocol. When the master needs to issue a command to the slave, the command is put into the FIFO linked list buffer one by one. Then the master retrieves the commands from the FIFO linked list buffer one by one. When the communication bus is idle, the commands stored in the FIFO linked list buffer are sent to the slave first, avoiding collision damage caused by data on the communication transmission line and improving the stability, reliability and real-time performance of the communication. In terms of communication management, the cumbersome multi-state machine switching design pattern is abandoned, simplifying the software design process and enhancing the organization and readability of the software.
[0155] This application also provides a computer-readable storage medium storing executable instructions. When the executable instructions are executed by at least one processor 1201, the MODBUS communication method based on a data list structure described in this application is implemented.
[0156] In some embodiments, the storage medium may be a magnetic random access memory (FRAM), a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), a flash memory, a magnetic surface memory, an optical disc, or a compact disc read-only memory (CD ROM), etc.; or it may be a device that includes one or any combination of the above-mentioned memories.
[0157] In some embodiments, executable instructions may take the form of a program, software, software module, script, or code, written in any form of programming language (including compiled or interpreted languages, or declarative or procedural languages), and may be deployed in any form, including as a standalone program or as a module, component, subroutine, or other unit suitable for use in a computing environment.
[0158] As an example, executable instructions may, but do not necessarily, correspond to files in the file system. They may be stored as part of a file that holds other programs or data, for example, in one or more scripts in a HyperText Markup Language (HTML) document, in a single file dedicated to the program in question, or in multiple collaborating files (e.g., a file that stores one or more modules, subroutines, or code sections).
[0159] As an example, executable instructions can be deployed to execute on a single computing device, or on multiple computing devices located in one location, or on multiple computing devices distributed across multiple locations and interconnected via a communication network.
[0160] Addressing the shortcomings and deficiencies of existing technologies, the aforementioned computer-readable storage medium adopts a novel "data list structure" design approach. All communication parameter variables are listed in the structure as a data list, forming a "data list set." This allows the communication protocol and communication parameter variables to operate independently, completely decoupling them. This significantly solves the problem of overly tight binding and complex dependencies between the MODBUS communication protocol and communication parameter variables. It also greatly reduces the coupling and correlation of software code, enhances software readability and portability, and improves the reusability of the software. Meanwhile, a FIFO linked list buffer design is adopted for commands issued by the master in the MODBUS communication protocol. When the master needs to issue a command to the slave, the command is put into the FIFO linked list buffer one by one. Then the master retrieves the commands from the FIFO linked list buffer one by one. When the communication bus is idle, the commands stored in the FIFO linked list buffer are sent to the slave first, avoiding collision damage caused by data on the communication transmission line and improving the stability, reliability and real-time performance of the communication. In terms of communication management, the cumbersome multi-state machine switching design pattern is abandoned, simplifying the software design process and enhancing the organization and readability of the software.
[0161] In the several embodiments provided in this application, it should be understood that the disclosed methods and electronic devices can be implemented in other ways. The device embodiments described above are merely illustrative. For example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods, such as: multiple units or components may be combined, or integrated into another system, or some features may be ignored or not executed. In addition, the coupling, direct coupling, or communication connection between the various components shown or discussed may be through some interfaces, and the indirect coupling or communication connection between devices or units may be electrical, mechanical, or other forms.
[0162] The modules described as separate components may or may not be physically separate. The components shown as modules may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0163] In addition, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0164] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a processor-executable, non-volatile, computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, a platform server, or a network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, ROM, RAM, magnetic disks, or optical disks.
[0165] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A MODBUS communication method based on a data list structure body, characterized by, The application is applied to a host computer, and comprises the following steps: in response to a command storage request, sequentially storing at least one command in a linked list buffer through a target interface; in response to a command extraction request, extracting a target command from the at least one command; based on the target command, traversing a preset data list structure to obtain a first communication parameter matched with the target command, and packaging the first communication parameter to obtain a host command; sending the host command to a slave computer, so that the slave computer responds to the host command based on the data list structure; the linked list buffer is a FIFO linked list buffer, and the step of storing at least one command in the linked list buffer through the target interface in response to the command storage request comprises the following steps: in response to the command storage request, detecting the fullness of the FIFO linked list buffer; when the FIFO linked list buffer is not full, sequentially storing at least one command in the linked list buffer through the target interface; when the FIFO linked list buffer is full, stopping storing; the step of extracting a target command from the at least one command in response to the command extraction request comprises the following steps: in response to the command extraction request, detecting the fullness of the FIFO linked list buffer; when the FIFO linked list buffer is not empty, extracting a target command from the FIFO linked list buffer in a first-in-first-out order; when the FIFO linked list buffer is empty, stopping extracting; the method further comprises the following steps: in response to first response data sent by the slave computer, analyzing the first response data to obtain second analysis data, and checking the CRC16 of the second analysis data; when the CRC16 of the second analysis data is correct, traversing the preset data list structure to obtain a third communication parameter matched with the second analysis data according to the second analysis data, and packaging the third communication parameter to obtain second response data.
2. The method of claim 1, wherein, the data list structure comprises the following members: bit number of a parameter variable, parameter storage pointer, parameter register address, parameter access permission, parameter modification flag bit, parameter variable value compliance judgment callback function, parameter index number, data list type and data list name; the bit number of the parameter variable is used to identify the bit number of the communication parameter variable in the memory; the parameter storage pointer is used to point to the storage address of the communication parameter variable; the parameter register address is a register address value allocated to the communication parameter variable in MODBUS communication protocol transmission; the parameter access permission includes read-only, write-only and read-write; the parameter modification flag bit is used to indicate when the parameter value is changed; the parameter variable value compliance judgment callback function is used to judge the limited value range of the set parameter; the parameter index number is used to limit the value of the corresponding communication parameter according to the index number in cooperation with the parameter variable value compliance judgment callback function. The data list type is a container type of bit number of a parameter variable, a parameter storage pointer, a parameter register address, parameter access authority, a parameter modification flag, a parameter variable value compliance judgment callback function, and a parameter index number. The data list name is a container name of bit number of a parameter variable, a parameter storage pointer, a parameter register address, parameter access authority, a parameter modification flag, a parameter variable value compliance judgment callback function, and a parameter index number.
3. A MODBUS communication method based on a data list structure body, characterized by, The application is applied to a slave machine, and comprises the following steps: In response to a host command sent by a host, first analysis data is obtained by analyzing the host command, and CRC16 of the first analysis data is checked; the host command is obtained in the following manner: the host responds to a command storage request, and sequentially stores at least one command in a linked list buffer through a target interface; a target command is extracted from the at least one command in response to a command extraction request; based on the target command, a first communication parameter matched with the target command is obtained by traversing a preset data list structure body, and the first communication parameter is packaged to obtain a host command; the linked list buffer is a FIFO linked list buffer, and the at least one command is sequentially stored in the linked list buffer through the target interface in response to the command storage request, which comprises the following steps: in response to the command storage request, the fullness of the FIFO linked list buffer is detected; when the FIFO linked list buffer is not full, the at least one command is sequentially stored in the linked list buffer through the target interface; when the FIFO linked list buffer is full, the storing is stopped; in response to the command extraction request, the target command is extracted from the at least one command, which comprises the following steps: in response to the command extraction request, the fullness of the FIFO linked list buffer is detected; when the FIFO linked list buffer is not empty, the target command is taken out from the FIFO linked list buffer in a first-in first-out order; when the FIFO linked list buffer is empty, the taking out is stopped; When the CRC16 of the first analysis data is correct, a second communication parameter matched with the first analysis data is obtained by traversing the preset data list structure body according to the first analysis data, and the second communication parameter is packaged to obtain first response data; The first response data is verified, and when the first response data verification result is normal, the first response data is sent to the host, so that the host responds to the first response data based on the data list structure body.
4. A MODBUS communication device based on a data list structure body, characterized by, The method of claim 1 is implemented, and the device comprises: The first response module is used to respond to a command storage request by sequentially placing at least one command into a linked list buffer via a target interface. The linked list buffer is a FIFO linked list buffer. The process of sequentially placing at least one command into the linked list buffer via the target interface in response to the command storage request includes: detecting the empty / full state of the FIFO linked list buffer in response to the command storage request; when the FIFO linked list buffer is not full, sequentially placing at least one command into the linked list buffer via the target interface; and stopping the placement when the FIFO linked list buffer is full. The second response module is used to extract a target command from the at least one command in response to a command extraction request; the extraction of the target command from the at least one command in response to the command extraction request includes: detecting the empty / full state of the FIFO linked list buffer in response to the command extraction request; when the FIFO linked list buffer is not empty, retrieving the target command from the FIFO linked list buffer in a first-in-first-out order; stopping the retrieval when the FIFO linked list buffer is empty; The first traversal and packet assembly module is used to traverse from a preset data list structure to obtain the first communication parameters that match the target command based on the target command, and to assemble the first communication parameters into a packet to obtain the host command. The first sending module is used to send the host command to the slave device, so that the slave device can respond to the host command based on the data list structure.
5. A MODBUS communication device based on a data list structure body, characterized by, The apparatus for implementing the method of claim 3 comprises: The third response module is used to respond to a host command sent by the host, parse the host command to obtain first parsed data, and perform CRC16 verification on the first parsed data. The second traversal and packet assembly module is used to obtain the second communication parameters that match the first parsed data by traversing from a preset data list structure according to the first parsed data after the CRC16 check of the first parsed data is correct, and to assemble the second communication parameters into a packet to obtain the first response data. The second sending module is used to verify the first response data. When the verification result of the first response data is normal, the module sends the first response data to the host so that the host responds to the first response data based on the data list structure.
6. An electronic device, comprising: include: The device includes a processor, a storage medium, and a bus, wherein the storage medium stores machine-readable instructions executable by the processor. When the electronic device is running, the processor communicates with the storage medium via the bus, and the processor executes the machine-readable instructions to perform the MODBUS communication method based on a data list structure as described in any one of claims 1 to 2, or to perform the MODBUS communication method based on a data list structure as described in claim 3.
7. A computer readable storage medium characterized in that, The computer-readable storage medium stores a computer program, which, when executed by a processor, performs the MODBUS communication method based on a data list structure as described in any one of claims 1 to 2, or performs the MODBUS communication method based on a data list structure as described in claim 3.
Citation Information
Patent Citations
Modbus protocol file encoding method, Modbus protocol file decoding method, encoder and decoder
CN106712902A
Method and system for realizing Modbus communication on serial link
CN110955626A