Master-slave station simulation test system and method based on MODBUS communication protocol
The MODBUS communication testing system with advanced modules and data visualization tools addresses functional limitations, improving efficiency and reducing troubleshooting time for MODBUS communication issues.
Patent Information
- Application Number
- CN202510526218.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-24
- Publication Date
- 2025-07-15
AI Technical Summary
The existing MODBUS communication protocol has a single test function and a single test data presentation form, which requires a lot of manpower and time to fail in communication, affecting the progress of the project.
It provides a master-slave simulation testing system and method based on MODBUS communication protocol, including software authorization module, engineering project management module, user management module, environment configuration module, communication management module and test trend management module, supports MODBUS TCP and MODBUS RTU communication protocols, has variable change trend chart rendering function, and starts the test software when the registration file is successfully checked by introducing engineering project management, testing trend management, user management and software authorization modules.
It improves the testing efficiency of MODBUS communication, helps staff quickly find communication problems and establish communications, saves investigation time, and promotes project progress.
Smart Images

Figure CN120321158A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of industrial communication testing, and specifically relates to a master-slave simulation testing system and method based on the MODBUS communication protocol. Background Art
[0002] As one of the relatively mature communication methods at present, MODBUS communication adopts an architecture of one master station and multiple slave stations. During the process of establishing communication, communication failures often occur due to unreasonable programming of the master station or slave station programs. When a communication failure occurs, a third-party program is often used to simulate the master station or slave station for testing to determine whether there is a problem with the communication program writing of the master station or slave station. The existing master-slave simulation testing programs have a single testing function and a single form of presenting test data. Based on this situation, when a communication failure occurs, testing requires a large amount of labor costs and time costs, affecting the industrial field linkage test, resulting in a slowdown in the project progress and having an adverse impact on the project construction. Summary of the Invention
[0003] In view of this, the present invention provides a master-slave simulation testing system and method based on the MODBUS communication protocol to solve the problem of single testing function in the prior art.
[0004] In a first aspect, the present invention provides a master-slave simulation testing system based on the MODBUS communication protocol. The system includes a software authorization module, a project management module, a user management module, an environment configuration module, a communication management module, a variable management module, and a test trend management module. Among them,
[0005] The software authorization module is used to verify the registration file and start the testing software when the verification is successful;
[0006] The project management module is used to establish a corresponding project according to the user's project requirements in the testing software and select the communication type. The communication types include MODBUS TCP master mode, MODBUS TCP slave mode, MODBUS RTU master mode, and MODBUS RTU slave mode;
[0007] The user management module is used to create user accounts, modify user accounts, and delete user accounts;
[0008] The environment configuration module is used to configure communication environment parameters according to the communication protocol type. The communication protocol types include MODBUS TCP and MODBUS RTU;
[0009] The communication management module is used to receive data, perform data reading, data caching, data writing, and store the real-time data in the Redis database;
[0010] The variable management module is used to configure variable parameters, perform master-slave station simulation tests according to the communication point table, and import, modify, and export variables.
[0011] The test trend management module is used to read the real-time data stored in the Redis database and use matplotlib to render and draw the variable change trend chart.
[0012] By introducing the engineering project management module, test trend management module, user management module, and software authorization module, the present invention starts the test software when the registration file verification is successful to simulate the master-slave station for testing, supports the MODBUS TCP and MODBUS RTU communication protocols, has the function of rendering the variable change trend chart, improves the test efficiency of MODBUS communication, facilitates the staff to quickly find communication problems and establish communication, saves the troubleshooting time, and promotes the progress of the project.
[0013] In an alternative embodiment, the Redis database includes a Redis client and a Redis server. The Redis client sends the instructions sent by the user to the Redis server for execution through a Socket connection, listens using a multiplexer, and uses a file event dispatcher to call the corresponding event handler according to the event type in the multiplexer.
[0014] The present invention uses the architecture design of the Redis client and server, listens to multiple Socket connections using a multiplexer to process the requests of multiple clients simultaneously, and uses a file event dispatcher to call the event handler corresponding to the event type to respond to the requests of the clients in a timely manner.
[0015] In an alternative embodiment, the test trend management module is used to generate real-time trend charts and historical trend charts. The test trend management module obtains historical trend data by calling the historical trend storage function and generates historical trend charts by calling the historical trend generation function.
[0016] The present invention presents real-time trend charts and historical trend charts through the test trend management module, so that the staff can understand the data changes during the test according to the trend charts and provide data basis for equipment maintenance.
[0017] In an alternative embodiment, the environment configuration module is also used to configure network environment parameters and test environment parameters.
[0018] The present invention improves the network stability by configuring network environment parameters, quickly builds a test environment by configuring test environment parameters, and improves the test efficiency.
[0019] In a second aspect, the present invention provides a master-slave station simulation test method based on the MODBUS communication protocol, and the method includes:
[0020] Verify the registration file and perform software authorization for testing.
[0021] When the verification is successful, start the test software, create a corresponding project according to the user's engineering requirements in the test software, and select the communication type. The communication types include MODBUS TCP master mode, MODBUS TCP slave mode, MODBUS RTU master mode, and MODBUS RTU slave mode.
[0022] Configure the communication environment parameters according to the communication protocol type and establish a communication connection. The communication protocol types include MODBUS TCP and MODBUS RTU.
[0023] Receive data, perform data reading, data caching, and data writing, and store the real-time data in the Redis database.
[0024] Perform master-slave simulation testing according to the communication point table.
[0025] Read the real-time data stored in the Redis database and use matplotlib to render and draw a variable change trend graph.
[0026] The present invention starts the test software when the registration file verification is successful by introducing project management, test trend management, user management, and software authorization, performs testing by simulating master-slave stations, supports MODBUS TCP and MODBUS RTU communication protocols, has the function of rendering variable change trend graphs, improves the testing efficiency of MODBUS communication, facilitates the staff to quickly find communication problems and establish communication, saves troubleshooting time, and promotes the progress of the project.
[0027] In an alternative embodiment, when the communication protocol type is MODBUS RTU, performing master-slave simulation testing includes:
[0028] The master constructs a data frame and sends a request. The data frame contains the correct position, function code, data, and CRC checksum.
[0029] The slave receives the request sent by the master, parses the data frame, and checks the CRC.
[0030] If the CRC check is correct and the address matches, the slave sends a response data frame.
[0031] The master receives the response data frame and parses the response data frame.
[0032] If the CRC check is correct and the address matches, process the response data.
[0033] In the present invention, when the master station constructs a data frame, it includes CRC check. The slave station receives the data frame and performs CRC check to detect whether an error occurs during data transmission, ensuring the accuracy of data transmission. By checking whether the addresses match, correct communication between the master station and the slave station is ensured. When the CRC check is correct and the addresses match, the response data is processed to comply with the MODBUS RTU communication protocol, realizing the simulation test of the master-slave station.
[0034] In an optional embodiment, when the communication protocol type is MODBUS TCP, the simulation test of the master-slave station is performed, including:
[0035] Establish a TCP connection between the master station and the slave station;
[0036] The master station constructs a message and sends a request. The message includes a transaction identifier, a protocol identifier, a length, a unit identifier, a function code, and data;
[0037] The slave station receives the request sent by the master station, parses the message, and determines whether the unit identifier matches;
[0038] If the unit identifier matches, the slave station sends a response message;
[0039] The master station receives the response message, parses the response message, and processes the response data, then closes the TCP connection.
[0040] In the present invention, a TCP connection is established between the master station and the slave station to ensure the reliability of communication between the master-slave stations. The slave station uses the unit identifier in the message structure to determine whether to respond to the master station's request, ensuring precise communication between the master-slave stations. After the unit identifier matches, the master station receives the response message from the slave station, ensuring the accuracy of data interaction, and complying with the MODBUS TCP communication protocol to realize the simulation test of the master-slave station.
[0041] In a third aspect, the present invention provides a computer device, including: a memory and a processor, which are communicatively connected to each other. The memory stores computer instructions, and the processor executes the computer instructions to perform the master-slave station simulation test method based on the MODBUS communication protocol according to the second aspect or any corresponding embodiment thereof.
[0042] In a fourth aspect, the present invention provides a computer-readable storage medium, on which computer instructions are stored. The computer instructions are used to cause a computer to perform the master-slave station simulation test method based on the MODBUS communication protocol according to the second aspect or any corresponding embodiment thereof.
[0043] Fifth aspect, the present invention provides a computer program product, including computer instructions for causing a computer to execute the master-slave simulation test method based on the MODBUS communication protocol according to the second aspect or any corresponding embodiment thereof. Description of the Drawings
[0044] In order to more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the following will briefly introduce the drawings required for use in the description of the specific embodiments or the prior art. Obviously, the drawings in the following description are some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.
[0045] Figure 1 is a schematic diagram of a master-slave simulation test system based on the MODBUS communication protocol according to an embodiment of the present invention;
[0046] Figure 2 is a schematic diagram of the verification process of the software authorization module according to an embodiment of the present invention;
[0047] Figure 3 is a schematic diagram of the management use case model of the engineering project management module according to an embodiment of the present invention;
[0048] Figure 4 is a schematic diagram of the management use case model of the user management module according to an embodiment of the present invention;
[0049] Figure 5 is a configuration flow chart of the environment configuration module according to an embodiment of the present invention;
[0050] Figure 6 is a task queue processing flow chart according to an embodiment of the present invention;
[0051] Figure 7 is a data processing flow chart of the Redis real-time database according to an embodiment of the present invention;
[0052] Figure 8 is a schematic diagram of the management process of the variable management module according to an embodiment of the present invention;
[0053] Figure 9 is a schematic diagram of the management process of the test trend management module according to an embodiment of the present invention;
[0054] Figure 10 is a flow chart of the master-slave simulation test method based on the MODBUS communication protocol according to an embodiment of the present invention;
[0055] Figure 11 is a MODBUS communication implementation flow chart according to an embodiment of the present invention;
[0056] Figure 12 is a schematic diagram of the general MODBUS data frame structure according to an embodiment of the present invention;
[0057] Figure 13 is a structural block diagram of a master-slave simulation test device based on the MODBUS communication protocol according to an embodiment of the present invention;
[0058] Figure 14 is a schematic diagram of the hardware structure of a computer device according to an embodiment of the present invention. Detailed implementation manners
[0059] To make the objectives, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention. Apparently, the described embodiments are some but not all of the embodiments of the present invention. All other embodiments obtained by those skilled in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.
[0060] According to an embodiment of the present invention, an embodiment of a master-slave simulation test system based on the MODBUS communication protocol is provided. As Figure 1 shown, project management, trend management, user management and user authorization are introduced for the first time. The test system is developed according to the overall architecture of seven functional modules, including a software authorization module, a project management module, a user management module, an environment configuration module, a communication management module, a variable management module, and a test trend management module.
[0061] From the perspective of the test software itself, the test system can be divided into four layers, namely, a presentation layer (user interface) responsible for vision and user interaction, a business layer implementing business logic, a persistence layer providing and parsing data, and a database storing data. From the perspective of specific implementation manners, the test software is generally developed using the Python programming language, with MODBUS_TK as the communication library, SQLite as the software data storage database, Redis as the software communication real-time data cache, Celery as the software real-time task queue implementation, Influxdb as the historical data storage database, matplotlib for viewing real-time trend graphs, and pyecharts for viewing historical trends. The parsing and import of Excel and Word files are implemented using Openpyxl and Python-word.
[0062] The functions of the test system are as follows:
[0063] (1) It has the function of reading messages, which is convenient for analyzing faults;
[0064] (2) It has the function of simultaneously forcing multiple variables, which is convenient for testing logic;
[0065] (3) It has the function of quickly and simply setting and modifying registers and function codes;
[0066] (4) It has the functions of simulating the master station and slave station, and supports the MODBUS TCP and MODBUS RTU protocols;
[0067] (5) It has a simple and fast communication interface with on-site equipment;
[0068] (6) It has the function of viewing real-time trends and historical trends.
[0069] The following is a detailed description of each module:
[0070] I. Software authorization module
[0071] The test software should only run on an authorized computer. As Figure 2 shown, after the test software is started, permission confirmation should be executed. Only after the system information is scanned according to the registration file and verified to be consistent, will the test software start normally. When the program finds that the registration file is inconsistent with the system information, the test software will not run normally, and a registration authorization window will pop up. After the authorization is completed, the test software can run normally only after verifying the registration file again.
[0072] Specifically, the test software registration encryption generates an activation code by performing DES-CBC encryption on the unique disk symbol of the computer's hard disk. Among them, DES is a symmetric encryption algorithm, and CBC represents the cipher block chaining mode. The previous ciphertext block is used as the input of the next plaintext block, increasing the randomness and security of encryption. When the test software runs, it will compare the encryption information obtained by encrypting and calculating the activation code (provided by the registration machine) and the local disk drive letter provided during registration to determine whether the current computer has authorization.
[0073] In the actual use process, the test system needs to be used in conjunction with the corresponding registration file. When running the registration file on the computer, it is necessary to click to generate the activation code and fill it in the activation code field, and then click activate. If the activation is successful, it will prompt that the registration is successful.
[0074] II. Engineering project management module
[0075] As Figure 3As shown in the figure, according to different usage scenarios or different communication environments, specific projects are created to achieve multi-scenario applications. At the same time, the project supports import and export functions to facilitate backup and restoration of the project. Some data of the software will also be stored in units of projects, such as user data, variable data, historical trend data, communication configuration data, etc. The project can be exported as a project file in the.project format. The project file contains database files (.db SQLite database files), cache files (in JSON format, including information such as the name of the last opened project), running logs (shielded terminal log information), and other information.
[0076] In the test software, use the new project function to create the project required by the user according to the user's project requirements, such as MODBUS TCP master station, MODBUS TCP slave station, MODBUS RTU master station, MODBUS RTU slave station, etc., and select the communication type. The communication types include MODBUS TCP master station mode, MODBUS TCP slave station mode, MODBUS RTU master station mode, and MODBUS RTU slave station mode. When the project is created, the corresponding project database is generated. Set information such as the project name and project description in the project interface and write it using sqlite library functions. When the current project (test environment) is no longer needed, the project can be deleted using the delete project function.
[0077] III. User Management Module
[0078] When creating a project, a default administrator account will be generated, and user login will be performed every time the project is opened. The administrator account has full permissions for the software and can also add, delete, and modify the account names, passwords, and user permissions of sub-accounts (all data is stored in SQLIte). The management of sub-user permissions includes user management permissions, variable forcing permissions, historical trend viewing permissions, communication configuration permissions, and other permissions. When using different accounts to log in, the software should block some functions of the software according to the permissions assigned to the account.
[0079] Such as Figure 4 As shown in the figure, according to the usage requirements of different users at the project site, different accounts can be created using the new user function, the permissions of different accounts can be defined using the modify permission function, the accounts with different permissions can be switched using the modify account function, and the user can modify the password of their own account using the modify password function. When too many accounts are not needed, the accounts can be deleted using the delete user function.
[0080] IV. Environment Configuration Module
[0081] Such as Figure 5As shown in the figure, the environment configuration module includes communication environment configuration, network environment configuration, and test environment configuration. When using different communication protocols, configure the communication environment parameters. The user needs to create a project with the specified protocol. After opening the project, the test software should generate a corresponding communication configuration interface according to the communication protocol type. Among them, the communication protocol types include MODBUS RTU and MODBUS TCP. For MODBUS RTU serial communication, relevant parameters such as the serial port number, baud rate, data bits, and stop bits need to be set. For MODBUS TCP, relevant parameters such as the IP address and port number need to be set. The interface should have a good interaction interface to facilitate the user to configure various parameters. At the same time, the configured parameters will be saved into the SQLite database and automatically communicate according to the parameters saved in the database when the project is opened next time.
[0082] V. Communication Management Module
[0083] The MODBUS communication transmission of the test software is realized by asynchronously processing communication data through the interface of the MODBUS_TK library called by the real-time task queue Celery. That is, after receiving the data transmitted by the device, the test software simultaneously performs operations such as data reading, data caching, and data writing to the historical database InfluxDB. As Figure 6 shown in the figure, the client sends tasks to the task queue broker. The task queue temporarily stores tasks such as Task 1, Task 2, Task 3, Task 4, etc. The task processor obtains tasks from the task queue and processes them. At the same time, after the operation is completed, the processing results of all tasks will be stored in the Redis cache to facilitate the software to re-execute the previously failed task.
[0084] The Redis database will be used as a variable real-time data cache and a storage database for task results. Its main task is to accept data writing and reading from the client, including storing variable real-time data, storing task queue processing results, and storing the message generated by each communication to read the real-time message on the message display interface.
[0085] The data processing process of the Redis real-time database is as Figure 7 shown in the figure. The Redis client sends the instructions sent by the user to the Redis server for execution through a Socket connection. During the process, the multiplexer is responsible for listening. The file event distributor calls different event processors (connection reply processor, command request processor, command reply processor) according to the event types in the multiplexer.
[0086] By using the architecture design of the Redis client and server, using the multiplexer to listen to multiple Socket connections to simultaneously process requests from multiple clients, and using the file event distributor to call the event processor corresponding to the event type to respond to the client's requests in a timely manner.
[0087] VI. Variable Management Module
[0088] After completing the communication parameter configuration, the user can import variables in the format of an EXCEL sheet, or as Figure 8 shown, perform operations such as adding, deleting, modifying, and querying variables in the variable management interface (such as modifying function codes, variable names, register addresses, engineering quantity upper and lower limits, etc.). In variable management, according to the communication point table, master-slave station simulation tests can be carried out, and variable tables can be imported in batches or entered manually. At the same time, the test software has a variable table export function, which facilitates the user to export the variables modified in the software to an EXCEL sheet for convenient backup, restoration, and modification.
[0089] The EXCEL content imported into the software will be stored in SQLite. Each time the celery framework starts a timed communication task, it will query the variable parameters in the SQLite database to operate the MODBUS_TK interface function for reading and writing operations.
[0090] The software variables are forced to have an engineering quantity conversion function. According to the variable range configured by the user, the input actual value is converted into an electrical signal value. At the same time, the software supports batch forcing of variables, that is, after setting the forced values for multiple variables, all the selected variables can be forced by clicking a specified button.
[0091] VII. Test Trend Management Module
[0092] The real-time trend query in the report is obtained by reading the real-time data cache generated by communication (stored in Redis), and then rendering and drawing a variable change trend graph through matplotlib. The test software has a variable value saving function. After the user completes the communication parameter configuration and variable parameter configuration, and the software communication is running normally, the software should save the obtained variable values to facilitate the generation of historical trends.
[0093] As Figure 9 shown, the test trend management module is used to generate real-time trends, view real-time trends, and also used to call the historical trend storage module to obtain historical trend data and call the historical trend generation function to generate historical trend graphs.
[0094] The software historical trend interface will view the historical trends of specified variables in units of days. The historical trend graph should have a zoom function to facilitate the user to view the overall trend and the trend changes in a specific time period. The trend graph should also ensure readability and accuracy.
[0095] The real-time trend graph and historical trend graph are presented through the test trend management module, so that the staff can understand the data changes during the test according to the trend graph and provide a data basis for equipment maintenance.
[0096] The master-slave simulation test system based on the MODBUS communication protocol provided in this embodiment starts the test software when the registration file verification is successful by introducing an engineering project management module, a test trend management module, a user management module, and a software authorization module, to simulate master-slave stations for testing, supports MODBUS TCP and MODBUS RTU communication protocols, has the function of rendering variable change trend graphs, improves the test efficiency of MODBUS communication, facilitates the staff to quickly find communication problems and establish communication, saves troubleshooting time, and promotes the progress of the project.
[0097] According to an embodiment of the present invention, there is provided an embodiment of a master-slave simulation test method based on the MODBUS communication protocol. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions, and although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order than here.
[0098] In this embodiment, a master-slave simulation test method based on the MODBUS communication protocol is provided. Figure 10 is a flowchart of the master-slave simulation test method based on the MODBUS communication protocol according to an embodiment of the present invention, as Figure 10 shown, the process includes the following steps:
[0099] Step S101, verify the registration file and perform test software authorization.
[0100] In an embodiment of the present invention, first install the tool software for MODBUS communication test on the debugging notebook, make a hardware connection with the test device. The MODBUS TCP communication method uses an Ethernet cable, and the MODBUS RTU communication method uses a 485 serial port cable. Perform test software authorization and use the registration file for test software authorization.
[0101] Step S102, start the test software when the verification is successful, create a corresponding project according to the engineering requirements of the user in the test software, and select the communication type.
[0102] In an embodiment of the present invention, when the verification is successful and the authorization is completed, start the test software, create a new project in the project management, and select the communication type according to the test situation. The communication types include MODBUS TCP master mode, MODBUS TCP slave mode, MODBUS RTU master mode, and MODBUS RTU slave mode.
[0103] Step S103, configure the communication environment parameters according to the communication protocol type and perform a communication connection.
[0104] In an embodiment of the present invention, the communication protocol types include MODBUS TCP and MODBUS RTU. Communication configuration is performed according to the communication protocol type, communication parameters are set, and after the communication parameters are set, click on communication connection to perform the communication connection.
[0105] Step S104, receive data, perform data reading, data caching, data writing, and store the real-time data in the Redis database.
[0106] In an embodiment of the present invention, data transmitted by the device is received through communication management. At the same time, data reading, data caching, and input writing to the historical database are performed. After the operations are completed, the processing results of all tasks are stored in the Redis cache, and the real-time data is stored in the Redis database.
[0107] Step S105, perform master-slave station simulation test according to the communication point table.
[0108] In an embodiment of the present invention, in variable management, a master-slave station simulation test is performed according to the communication point table, and the variable table can be imported in batches or manually input.
[0109] Step S106, read the real-time data stored in the Redis database, and use matplotlib to render and draw a variable change trend graph.
[0110] In an embodiment of the present invention, after reading the real-time data generated by communication (stored in the Redis database), a variable change trend graph is rendered and drawn through matplotlib.
[0111] Specifically, first, the implementation process of MODBUS communication is introduced, as Figure 11 shown, Figure 11 which includes the conversion of different states and related operations in MODBUS communication, specifically including:
[0112] Initial state: The starting point of the process. When a character is received, initialize and start timer t3.5. After t3.5 times out, enter the idle state.
[0113] Idle: Ready to receive or send. It can receive a request to send and enter the send state. When the first character is received, initialize and start timer t1.5 and timer t3.5, and enter the receive state.
[0114] Receive: When a character is received, initialize and start timer t1.5 and timer t3.5. When t1.5 times out, enter the control and wait state.
[0115] Control and wait: Receive characters, perform frame control operations such as CRC, verification, and word address, mark the frame as normal or abnormal. When t3.5 times out, enter the idle state.
[0116] Send: Send a character. If it is the last character, initialize and start timer t3.5. After t3.5 times out, return to the idle state.
[0117] Specifically, when the communication protocol type is MODBUS RTU, the master-slave simulation test in step S105 includes:
[0118] Step S1051: The master station constructs a data frame and sends a request. The data frame includes the correct position, function code, data, and CRC check.
[0119] Step S1052: The slave station receives the request sent by the master station, parses the data frame, and checks the CRC.
[0120] Step S1053: If the CRC check is correct and the address matches, the slave station sends a response data frame.
[0121] Step S1054: The master station receives the response data frame and parses the response data frame.
[0122] Step S1055: If the CRC check is correct and the address matches, process the response data.
[0123] In the embodiment of the present invention, first, the RTU communication method is introduced:
[0124] In the RTU communication method, the MODBUS_TK library realizes communication with the device by sending and receiving MODBUS data frames.
[0125] MODBUS_RTU uses a binary format to transmit data on a serial link. It uses asynchronous serial communication and requires a specific time interval to be set between data to distinguish different data frames. After the master station sends a data frame, it needs to wait for an inter-frame timeout time (for example, 1.750 ms) to ensure a clear boundary between data frames.
[0126] These time intervals are very important because they help identify the start and end of the frame.
[0127] Inter-character timeout time (t1.5): In RTU communication, when the idle time between two consecutive characters exceeds this value, a new frame is considered to start. The recommended value is 750 microseconds (μs).
[0128] Inter-frame timeout time (t3.5): When the idle time between two consecutive frames exceeds this value, a complete frame is considered to have been received. The recommended value is 1.750 milliseconds (ms).
[0129] Such as Figure 12As shown, the MODBUS_RTU protocol data frame contains the following information:
[0130] (1) Address field: The device address identifies the identity of the device.
[0131] (2) Function code: Identifies the specific operation to be performed on the device.
[0132] (3) Data: The data for the operation to be performed on the device.
[0133] (4) CRC check (error check): Performs a CRC check on the entire data frame to verify data integrity. The CRC-16 standard check algorithm in the MODBUS protocol is used for the check. When the slave receives the data frame, it recalculates through the CRC algorithm and compares the calculated value with the actual value received in the CRC field. If the two are different, an error is generated and an exception response is returned to inform the sending device.
[0134] The software encapsulates the above data frame to implement read and write operations on the device and also supports reading the register information of the device.
[0135] Introduce the implementation method of the MODBUS_RTU master-slave station:
[0136] Master: The master is responsible for constructing the data frame and sending it to the specified slave.
[0137] The master needs to ensure that the data frame sent follows the MODBUS_RTU format, including the correct address, function code, data part, and CRC check.
[0138] The master needs to handle timeouts to take corresponding measures when the network does not respond.
[0139] After sending a data frame, the master needs to wait for an inter-frame timeout (e.g., 1.750ms) to ensure a clear boundary between data frames.
[0140] Slave: The slave listens for data frames on the network and checks if the address in the frame matches its own address.
[0141] If the address matches, the slave will perform the corresponding operation according to the function code (such as reading registers, writing registers, etc.).
[0142] The slave needs to generate a response data frame and send it back to the master.
[0143] The slave needs to ensure that the data frame sent contains the correct CRC check.
[0144] The specific process is as follows:
[0145] Step 1: The master station sends a request: Construct a data frame containing the correct address, function code, data, and CRC checksum. Send the data frame through the serial port. Record the sending time and start the timer.
[0146] Step 2: The slave station receives the request: Monitor the serial port and wait to receive the data frame. Parse the address, function code, and data in the data frame. Check whether the CRC checksum is correct. If the CRC is correct and the address matches, perform the corresponding operation.
[0147] Step 3: The slave station sends a response: Construct a data frame containing the correct address, function code, response data, and CRC checksum. Send the response data frame through the serial port.
[0148] Step 4: The master station receives the response: Monitor the serial port and wait to receive the response data frame. Parse the address, function code, and data in the data frame. Check whether the CRC checksum is correct. If the CRC is correct and the address matches, process the response data.
[0149] By introducing project management, test trend management, user management, and software authorization, the test software is started when the registration file verification is successful to simulate the master-slave stations for testing, support the MODBUS TCP and MODBUS RTU communication protocols, have the function of rendering the variable change trend graph, improve the test efficiency of MODBUS communication, so that the staff can quickly find communication problems and establish communication, save the troubleshooting time, and promote the project progress.
[0150] Specifically, when the communication protocol type is MODBUS TCP, the above step S105 includes:
[0151] Step S1056, establish a TCP connection between the master station and the slave station.
[0152] Step S1057, the master station constructs a message and sends a request. The message includes a transaction identifier, protocol identifier, length, unit identifier, function code, and data.
[0153] Step S1058, the slave station receives the request sent by the master station, parses the message, and determines whether the unit identifier matches.
[0154] Step S1059, if the unit identifier matches, the slave station sends a response message.
[0155] Step S10510, the master station receives the response message, parses the response message, and processes the response data, and closes the TCP connection.
[0156] In the embodiment of the present invention, the following introduces the TCP communication method:
[0157] The software realizes communication with the device by sending and receiving MODBUS-TCP messages.
[0158] MODBUS_TCP is a variant of the MODBUS protocol that transmits data over a TCP / IP network. Different from RTU, MODBUS_TCP does not require an additional time interval to distinguish the start and end of a frame because it utilizes the connection characteristics of TCP.
[0159] The MODBUS-TCP message contains the following information:
[0160] Message structure:
[0161] (1) Transaction identifier: Used to identify a request or response message.
[0162] (2) Protocol identifier: Identifies the MODBUS-TCP protocol.
[0163] (3) Length: The length of the message.
[0164] (4) Unit identifier: The device address, which identifies the identity of the device.
[0165] (5) Function code: Identifies the specific operation to be performed on the device.
[0166] (6) Data: The data required to operate on the device.
[0167] The implementation method of the MODBUS_TCP master-slave station is introduced:
[0168] Master: The master is responsible for establishing a TCP connection with the slave.
[0169] Construct a message containing the transaction identifier, protocol identifier, length, unit identifier, function code, and data.
[0170] Send the message and wait for a response through the TCP connection.
[0171] Parse the response message returned by the slave.
[0172] Slave: The slave listens on the TCP port and waits for the master to establish a connection.
[0173] Receive the message sent by the master and parse the message content.
[0174] Execute the corresponding operation according to the function code and construct a response message.
[0175] Send the response message back to the master through the TCP connection.
[0176] The specific process is as follows:
[0177] Step 1: The master station sends a request: Establish a TCP connection with the slave station. Construct a message containing a transaction identifier, protocol identifier, length, unit identifier, function code, and data. Send the message through the TCP connection.
[0178] Step 2: The slave station receives the request: Listen on the TCP port and wait for the master station to establish a connection. Receive the message sent by the master station and parse the message content. Check whether the unit identifier in the message matches its own address. If it matches, perform the corresponding operation and construct a response message.
[0179] Step 3: The slave station sends a response: Construct a message containing a transaction identifier, protocol identifier, length, unit identifier, function code, and response data. Send the response message through the TCP connection.
[0180] Step 4: The master station receives the response: Receive the response message returned by the slave station through the TCP connection. Parse the transaction identifier, protocol identifier, length, unit identifier, function code, and data in the message. Process the response data and close the TCP connection. The software realizes the read and write operations of the MODBUS register by encapsulating the above message response and request functions.
[0181] Parameter examples are: The recommended inter-character timeout (t1.5) is 750 μs, and the inter-frame timeout (t3.5) is 1.750 ms.
[0182] By establishing a TCP connection between the master station and the slave station to ensure the reliability of communication between the master and slave stations, the slave station uses the unit identifier in the message structure to determine whether to respond to the master station's request, ensuring precise communication between the master and slave stations. After the unit identifiers match, the master station receives the response message from the slave station to ensure the accuracy of data interaction. Follow the MODBUS TCP communication protocol to implement the master-slave station simulation test.
[0183] The master-slave station simulation test method based on the MODBUS communication protocol provided in this embodiment starts the test software when the registration file verification is successful by introducing project management, test trend management, user management, and software authorization to simulate the master and slave stations for testing. It supports the MODBUS TCP and MODBUS RTU communication protocols, has the function of rendering the variable change trend graph, improves the test efficiency of MODBUS communication, facilitates the staff to quickly find communication problems and establish communication, saves the troubleshooting time, and promotes the progress of the project.
[0184] In this embodiment, a master-slave simulation test device based on the MODBUS communication protocol is also provided. This device is used to implement the above-mentioned embodiments and preferred implementation manners, and those that have been described will not be repeated. As used hereinafter, the term "module" can be a combination of software and / or hardware that realizes a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, implementation in hardware, or a combination of software and hardware is also possible and contemplated.
[0185] This embodiment provides a master-slave simulation test device based on the MODBUS communication protocol. As Figure 13 shown, it includes:
[0186] An authorization unit 1301, used to verify the registration file and perform test software authorization.
[0187] A communication type selection unit 1302, used to start the test software when the verification is successful, create a corresponding project according to the engineering requirements of the user in the test software, and select the communication type. The communication types include MODBUS TCP master mode, MODBUS TCP slave mode, MODBUS RTU master mode, and MODBUS RTU slave mode.
[0188] A communication environment configuration unit 1303, used to configure communication environment parameters according to the communication protocol type and perform a communication connection. The communication protocol types include MODBUS TCP and MODBUS RTU.
[0189] A data storage unit 1304, used to receive data, perform data reading, data caching, data writing, and store real-time data in the Redis database.
[0190] A test unit 1305, used to perform master-slave simulation tests according to the communication point table.
[0191] A trend graph drawing unit 1306, used to read the real-time data stored in the Redis database and use matplotlib to render and draw a variable change trend graph.
[0192] In some alternative implementation manners, the test unit 1305 includes:
[0193] A first request sending subunit, used for the master station to construct a data frame and send a request. The data frame includes the correct position, function code, data, and CRC check.
[0194] A data frame parsing subunit, used for the slave station to receive the request sent by the master station, parse the data frame, and verify the CRC.
[0195] A response data frame sending subunit, used for the slave station to send a response data frame if the CRC check is correct and the address matches.
[0196] A response data frame parsing subunit, configured to receive a response data frame by the master station and parse the response data frame.
[0197] A first processing subunit, configured to process the response data if the CRC check is correct and the address matches.
[0198] In some alternative embodiments, the testing unit 1305 further includes:
[0199] A connection establishment subunit, configured to establish a TCP connection between the master station and the slave station.
[0200] A second request sending subunit, configured to construct a message by the master station and send the request, where the message includes a transaction identifier, a protocol identifier, a length, a unit identifier, a function code, and data.
[0201] A message parsing subunit, configured to receive the request sent by the master station by the slave station, parse the message, and determine whether the unit identifiers match.
[0202] A response message sending subunit, configured to send a response message by the slave station if the unit identifiers match.
[0203] A second processing subunit, configured to receive the response message by the master station, parse the response message, process the response data, and close the TCP connection.
[0204] The further function descriptions of the above-mentioned respective modules and units are the same as those in the corresponding foregoing embodiments, and will not be elaborated herein.
[0205] The master-slave simulation testing device based on the MODBUS communication protocol in this embodiment is presented in the form of functional units. Here, the unit refers to an ASIC (Application Specific Integrated Circuit) circuit, a processor and a memory that execute one or more software or fixed programs, and / or other devices that can provide the above functions.
[0206] An embodiment of the present invention further provides a computer device having the above-mentioned Figure 13 master-slave simulation testing device based on the MODBUS communication protocol as shown.
[0207] Please refer to Figure 14 , Figure 14 is a schematic structural diagram of a computer device provided by an alternative embodiment of the present invention. As Figure 14As shown, the computer device includes: one or more processors 10, a memory 20, and interfaces for connecting various components, including a high-speed interface and a low-speed interface. Each component communicates with each other using different buses and can be installed on a common motherboard or installed in other ways as needed. The processor can process instructions executed within the computer device, including instructions stored in the memory or on the memory to display graphical information of the GUI on an external input / output device (such as a display device coupled to the interface). In some alternative embodiments, if needed, multiple processors and / or multiple buses can be used together with multiple memories. Similarly, multiple computer devices can be connected, and each device provides part of the necessary operations (for example, as a server array, a set of blade servers, or a multi-processor system). Figure 14 Take one processor 10 as an example in
[0208] The processor 10 can be a central processing unit, a network processor, or a combination thereof. Among them, the processor 10 can further include a hardware chip. The above hardware chip can be an application-specific integrated circuit, a programmable logic device, or a combination thereof. The above programmable logic device can be a complex programmable logic device, a field-programmable gate array, a generic array logic, or any combination thereof.
[0209] Among them, the memory 20 stores instructions executable by at least one processor 10, so that at least one processor 10 executes the method shown in the above embodiments.
[0210] The memory 20 can include a program storage area and a data storage area. Among them, the program storage area can store an operating system and application programs required for at least one function; the data storage area can store data created according to the use of the computer device. In addition, the memory 20 can include a high-speed random access memory, and can also include a non-transitory memory, such as at least one disk storage device, a flash memory device, or other non-transitory solid-state storage devices. In some alternative embodiments, the memory 20 can optionally include a memory remotely set relative to the processor 10, and these remote memories can be connected to the computer device through a network. Examples of the above network include but are not limited to the Internet, an enterprise intranet, a local area network, a mobile communication network, and combinations thereof.
[0211] The memory 20 can include a volatile memory, such as a random access memory; the memory can also include a non-volatile memory, such as a flash memory, a hard disk, or a solid-state drive; the memory 20 can also include a combination of the above types of memories.
[0212] The computer device further includes an input device 30 and an output device 40. The processor 10, the memory 20, the input device 30, and the output device 40 can be connected through a bus or other means.Figure 14 Take the bus connection as an example.
[0213] The input device 30 can receive the input digital or character information, and generate key signal inputs related to the user settings and function controls of the computer device, such as a touch screen, etc. The output device 40 may include a display device, etc.
[0214] Embodiments of the present invention also provide a computer-readable storage medium. The methods according to the embodiments of the present invention can be implemented in hardware, firmware, or be implemented as computer code that can be recorded on a storage medium, or be implemented as computer code originally stored in a remote storage medium or a non-transitory machine-readable storage medium and downloaded through a network and will be stored in a local storage medium, so that the methods described herein can be stored as such software processing on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. Among them, the storage medium can be a magnetic disk, an optical disc, a read-only memory, a random access memory, a flash memory, a hard disk, or a solid-state drive, etc.; further, the storage medium can also include a combination of the above-mentioned types of memories. It can be understood that a computer, a processor, a microprocessor controller, or programmable hardware includes a storage component that can store or receive software or computer code. When the software or computer code is accessed and executed by the computer, the processor, or the hardware, the methods shown in the above embodiments are implemented.
[0215] A part of the present invention can be applied as a computer program product, such as computer program instructions. When executed by a computer, through the operation of the computer, the methods and / or technical solutions according to the present invention can be called or provided. Those skilled in the art should understand that the forms of existence of computer program instructions in a computer-readable medium include, but are not limited to, source files, executable files, installation package files, etc. Correspondingly, the ways in which computer program instructions are executed by a computer include, but are not limited to: the computer directly executes the instruction, or the computer compiles the instruction and then executes the corresponding compiled program, or the computer reads and executes the instruction, or the computer reads and installs the instruction and then executes the corresponding installed program. Here, the computer-readable medium can be any available computer-readable storage medium or communication medium accessible to the computer.
[0216] Although the embodiments of the present invention are described in conjunction with the accompanying drawings, those skilled in the art can make various modifications and variations without departing from the spirit and scope of the present invention, and such modifications and variations all fall within the scope of this application.
Claims
1. A master-slave simulation test system based on the MODBUS communication protocol, characterized in that The system includes a software authorization module, a project management module, a user management module, an environment configuration module, a communication management module, a variable management module, and a test trend management module. Among them, the software authorization module is used to verify the registration file and start the test software when the verification is successful; the project management module is used to create a corresponding project according to the user's project requirements in the test software and select the communication type, where the communication type includes MODBUS TCP master mode, MODBUS TCP slave mode, MODBUS RTU master mode, and MODBUS RTU slave mode; the user management module is used to create user accounts, modify user accounts, and delete user accounts; the environment configuration module is used to configure communication environment parameters according to the communication protocol type, where the communication protocol type includes MODBUS TCP and MODBUS RTU; the communication management module is used to receive data, perform data reading, data caching, data writing, and store the real-time data in the Redis database; the variable management module is used to configure variable parameters, perform master-slave simulation tests according to the communication point table, and import, modify, and export variables; the test trend management module is used to read the real-time data stored in the Redis database and use matplotlib to render and draw the variable change trend graph.
2. The system according to claim 1, characterized in that, The Redis database includes a Redis client and a Redis server. The Redis client sends the instructions sent by the user to the Redis server for execution through a Socket connection, listens using a multiplexer, and uses a file event dispatcher to call the corresponding event handler according to the event type in the multiplexer.
3. The system according to claim 1, wherein The test trend management module is used to generate real-time trend graphs and historical trend graphs. The test trend management module obtains historical trend data by calling the historical trend storage function and generates historical trend graphs by calling the historical trend generation function.
4. The system according to claim 1, characterized in that, The environment configuration module is also used to configure network environment parameters and test environment parameters.
5. A master-slave simulation test method based on the MODBUS communication protocol, characterized in that, Applied to the master-slave simulation test system based on the MODBUS communication protocol described in any one of claims 1-4, the method includes: Verifying the registration file and performing software authorization for the test; Starting the test software when the verification is successful, creating a corresponding project according to the user's project requirements in the test software, and selecting the communication type, where the communication type includes MODBUS TCP master mode, MODBUS TCP slave mode, MODBUS RTU master mode, and MODBUS RTU slave mode; Configuring communication environment parameters according to the communication protocol type and establishing a communication connection, where the communication protocol type includes MODBUS TCP and MODBUS RTU; Receiving data, performing data reading, data caching, data writing, and storing the real-time data in the Redis database; Performing master-slave simulation tests according to the communication point table; Read the real-time data stored in the Redis database and use matplotlib to render and draw a trend chart of variable changes.
6. The method according to claim 5, wherein When the communication protocol type is MODBUS RTU, the master-slave simulation test is performed, including: The master constructs a data frame and sends a request. The data frame includes the correct address, function code, data, and CRC check. The slave receives the request sent by the master, parses the data frame, and checks the CRC. If the CRC check is correct and the address matches, the slave sends a response data frame. The master receives the response data frame and parses the response data frame. If the CRC check is correct and the address matches, the response data is processed.
7. The method according to claim 5, wherein When the communication protocol type is MODBUS TCP, the master-slave simulation test is performed, including: Establish a TCP connection between the master and the slave. The master constructs a message and sends a request. The message includes a transaction identifier, protocol identifier, length, unit identifier, function code, and data. The slave receives the request sent by the master, parses the message, and determines whether the unit identifier matches. If the unit identifier matches, the slave sends a response message. The master receives the response message, parses the response message, processes the response data, and closes the TCP connection.
8. A computer device, characterized in that, Including: A memory and a processor. The memory and the processor are communicatively connected to each other. The memory stores computer instructions. The processor executes the computer instructions to perform the master-slave simulation test method based on the MODBUS communication protocol according to any one of claims 5 to 7.
9. A computer-readable storage medium, characterized in that, Computer instructions are stored on the computer-readable storage medium. The computer instructions are used to cause a computer to perform the master-slave simulation test method based on the MODBUS communication protocol according to any one of claims 5 to 7.
10. A computer program product, characterized in that, Including computer instructions. The computer instructions are used to cause a computer to perform the master-slave simulation test method based on the MODBUS communication protocol according to any one of claims 5 to 7.
Citation Information
Cited By
Test tool implementation method and device, electronic equipment and storage medium
CN121283913A
Configurable Modbus protocol analysis and simulation system, method, equipment and medium
CN121619388A