File transmission method and device based on Modbus protocol and storage medium
By encapsulating data in file form and utilizing the multi-byte read and write function of the Modbus protocol, the efficient collaboration between the Modbus protocol and other IoT communication protocols between different physical layer media and MCUs is achieved, and the problem of data structure and length limitation in transmission of traditional Modbus protocol is solved.
Patent Information
- Application Number
- CN202510286457.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-12
- Publication Date
- 2025-04-15
- Estimated Expiration
- 2045-03-12
AI Technical Summary
The data transmitted by the traditional Modbus protocol cannot be structured, and the data length is limited, so it cannot efficiently coordinate with other IoT communication protocols between different physical layer media and MCUs.
The data is summarized using file concepts, and the file and file index are encapsulated into data blocks through different encapsulation formats. The data block transmission is completed using the Modbus standard multi-byte read and multi-byte write, realizing the decoupling of the transmission protocol and content.
It realizes efficient coordination between Modbus protocol and other IoT communication protocols between different physical layer media and MCUs, solving the problems of data structure and length limitation.
Smart Images

Figure CN119835266A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of Modbus communication technology, and in particular to a file transmission method, device and storage medium based on the Modbus protocol. Background Art
[0002] In the existing IoT communication network, there are various physical transmission media, such as Ethernet (wired, wireless, WIFI, / 2 / 3 / 4 / 5G cellular network), serial communication (UART / RS232 / RS485), CAN (Controller Area Network), etc. Equivalent to the OSI (Open System Interconnect) communication data model, different physical transmission media correspond to different application layer communication protocols. In the prior art, Ethernet and serial communication often use the standard Modbus communication protocol, and the standard application layer protocols commonly used by the CAN bus include CANopen, SAE J1939, etc. Each standard is only applicable to the limited range of each specific physical layer, and there is no good compatibility for cross-physical layer data transmission.
[0003] The data between IoT devices generally includes real-time data, historical data, configuration parameters, etc. Conventional transmission methods based on the Modbus protocol are based on limited specific signal quantity requirements, and gradually expand the data point table (or register) based on the demand, and increase the table length infinitely by spreading the data. Because the data content is related to the data point table, the data cannot be well structured. Different data structures need to be distinguished by different data point table addresses, and the data length is limited by the protocol address length range. The maximum address length of the Modbus protocol is 65536.
[0004] In order to simplify and unify data communication in IoT embedded systems, there is an urgent need for a data transmission method that enables the Modbus protocol to efficiently collaborate (such as latency and throughput) with other IoT communication protocols based on the same data on different physical layer media and MCUs (Microcontroller Units). Summary of the invention
[0005] The embodiments of the present application provide a file transmission method, device and storage medium based on the Modbus protocol, which solve the problem that the data transmitted by the traditional Modbus protocol cannot be structured and the data length is limited, resulting in the inability to efficiently collaborate with other Internet of Things communication protocols based on the same data between different physical layer media and MCUs.
[0006] In a first aspect, an embodiment of the present application provides a file transmission method based on the Modbus protocol, which is applied to a host computer, and the method includes: Encapsulate the target file index into a file read data block according to a preset first encapsulation format, and initiate a multi-byte write request to the subordinate computer based on a first preset address space corresponding to the first encapsulation format and the file read data block; After receiving a normal write response returned by the lower computer, a multi-byte read request is initiated to the lower computer based on a second preset address space corresponding to a preset second encapsulation format; A response including a file content data block returned by the lower computer is received, and the file content data block is parsed based on the second encapsulation format to obtain target file content.
[0007] Optionally, the data block encapsulated in the first encapsulation format includes index information and data offset information, and the data block encapsulated in the second encapsulation format includes data classification, total data, current data transmission length, data offset information and current load data.
[0008] Optionally, the index information field includes a directory and a file name.
[0009] Optionally, the data after parsing the file content data block based on the second encapsulation format includes the total data amount, the current data transmission length, the data offset information and the current load data, and the method further includes: Determining whether the target file has been transferred according to the total amount of data, the current data transfer length and the data offset information; If not, new data offset information is determined according to the current data transmission length and the data offset information; the target file index and the new data offset information are encapsulated into a new file read data block according to the first encapsulation format, a new round of multi-byte write requests is initiated to the subordinate computer based on the first preset address space and the new file read data block, after receiving a normal write response returned by the subordinate computer, a new round of multi-byte read requests is initiated to the subordinate computer based on the second preset address space, and a response containing a new file content data block is received returned by the subordinate computer, and the new file content data block is parsed based on the second encapsulation format to obtain the total number of data, the current data transmission length, the data offset information and the current load data; Repeat the above steps until all target file contents are transferred.
[0010] Optionally, the method further includes: Encapsulating the query directory index into a file query data block according to the first encapsulation format, and initiating a multi-byte write request to the subordinate computer based on the first preset address space and the file query data block; After receiving a normal write response returned by the lower computer, initiating a multi-byte read request to the lower computer based on the second preset address space; A response including an index content data block returned by the lower computer is received, and the index content data block is parsed based on the second encapsulation format to obtain target index content.
[0011] In a second aspect, an embodiment of the present application provides a file transmission method based on the Modbus protocol, which is applied to a lower computer, and the method includes: After receiving a multi-byte write request initiated by the host computer based on the first preset address space corresponding to the preset first encapsulation format and the file read data block, returning a normal write response to the host computer; Parsing the file reading data block based on the first encapsulation format to obtain a target file index, determining a target file corresponding to the target file index according to the target file index, and encapsulating the target file content into a file content data block based on a preset second encapsulation format; After receiving a multi-byte read request initiated by the host computer based on the second preset address space corresponding to the second encapsulation format, a response including the file content data block is returned to the host computer based on the second preset address space.
[0012] Optionally, the data block encapsulated in the first encapsulation format includes index information and data offset information, and the data block encapsulated in the second encapsulation format includes data classification, total data, current data transmission length, data offset information and current load data.
[0013] Optionally, the method further includes: After receiving the multi-byte write request initiated by the host computer based on the first preset address space and the file query data block, returning a normal write response to the host computer; Parsing the file query data block based on the first encapsulation format to obtain a query directory index, determining a target index content corresponding to the query directory index according to the query directory index, and encapsulating the target index content into an index content data block based on the second encapsulation format; After receiving the multi-byte read request initiated by the host computer based on the second preset address space, the index content data block is returned to the host computer based on the second preset address space.
[0014] In a third aspect, an embodiment of the present application provides an electronic device, comprising: at least one processor and a memory communicatively connected to the at least one processor, the memory storing instructions executable by the at least one processor, the instructions being executed by the at least one processor so that the at least one processor can execute any of the methods described above.
[0015] In a fourth aspect, an embodiment of the present application provides a computer storage medium, which stores instructions or programs. When the instructions or programs are executed by at least one processor, the at least one processor executes any one of the methods described above.
[0016] In an embodiment of the present application, a file transmission method based on the Modbus protocol is provided, wherein the host computer encapsulates the target file index into a file read data block according to a preset first encapsulation format, and initiates a multi-byte write request to the lower computer based on the first preset address space corresponding to the first encapsulation format and the file read data block; after receiving the normal write response returned by the lower computer, a multi-byte read request is initiated to the lower computer based on the second preset address space corresponding to the preset second encapsulation format; a response containing a file content data block returned by the lower computer is received, and the file content data block is parsed based on the second encapsulation format to obtain the target file content. The method of the present application adopts the file concept to summarize various types of data, and establishes a mapping relationship between files and file indexes. When performing file transmission, files and file indexes are encapsulated into data blocks using different encapsulation formats, and the transmission of data blocks is completed through multi-byte read and multi-byte write of the Modbus standard, realizing the decoupling of the transmission protocol and the transmission content, so that the Modbus protocol can be efficiently coordinated with other Internet of Things communication protocols based on the same data between different physical layer media and MCUs. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 An exemplary bus topology diagram based on the Modbus protocol is shown; Figure 2 The structure diagram of a Modbus RTU message frame is shown as an example; Figure 3 A schematic diagram of the hardware structure of an electronic device is exemplarily shown; Figure 4 The flowchart of the file transmission method based on Modbus protocol applied to the host computer is exemplified; Figure 5 The flowchart of the file transmission method based on Modbus protocol applied to the lower computer is exemplified; Figure 6 The structure diagram of the first packaging format is exemplarily shown; Figure 7 The structure diagram of the second packaging format is exemplarily shown; Figure 8 The format diagram of the Modbus protocol standard multi-byte write request frame and multi-byte write response frame is exemplarily shown; Fig. 9 The schematic diagram of the format of the Modbus protocol standard multi-byte read request frame and multi-byte read response frame is exemplarily shown; Fig.10 The schematic diagram of the whole process of file transmission based on Modbus protocol is shown as an example. DETAILED DESCRIPTION
[0018] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0019] Please refer to Figure 1 , Figure 1 The following is an example of a bus topology diagram based on the Modbus protocol. Figure 1 As shown in Figure 1, multiple devices are connected to the same bus. To avoid data conflicts, the Modbus protocol uses a master-slave communication method. In a Modbus communication system, there is a host device (such as Figure 1 device A) and multiple slave devices (such as Figure 1 Devices B, C and D in the middle). The Modbus protocol adopts a request-response mode and stipulates that Modbus requests can only be initiated by the host, that is, the host sends request data to the slave, and the slave can only receive requests from the host and make corresponding responses, returning response data to the host. The slave cannot actively initiate requests and can only passively receive requests from the host. In order to avoid data conflicts, the host cannot send data in parallel. After sending the first data, the second data can only be sent after the slave returns a response or the response times out. Each slave has only one slave address in a system. When the host sends a data request, it will carry the address of the target slave in the request data. When the host sends data to the bus, each slave will receive the data from the host, but only the slave with the same target address as the request data will respond, and other slaves will ignore this request.
[0020] Please refer to Figure 2 , Figure 2 The structure diagram of the Modbus RTU message frame is shown as an example. Figure 2As shown in the figure, the RTU (Remote Terminal Unit) message frame consists of an address code, a function code, a data field, and a check code. The address code occupies one byte and is used to identify the destination address of the message. Each slave has a unique address. The function code occupies one byte and is used to indicate the type of operation the host has on the slave. The data field occupies 0 to 252 bytes, and its length and content are determined by the function code. The check code occupies two bytes and is used to ensure the correct transmission of the message.
[0021] Please refer to Figure 3 , Figure 3 The hardware structure diagram of the electronic device is shown as an example. Figure 3 As shown, the electronic device 100 includes a processor 10, a memory 20 and a communication interface 30, and the processor 10, the memory 20 and the communication interface 30 are connected through a line. Figure 3 In the illustrated embodiment, the processor 10, the memory 20 and the communication interface 30 are communicatively connected to each other via a bus.
[0022] The memory 20 is used to store software programs, computer executable program instructions, etc. The memory 20 may include a program storage area and a data storage area, wherein the program storage area may store an operating system and application programs required for at least one function; the data storage area may store data created according to the use of the electronic device, etc.
[0023] The memory 20 may be a read-only memory (ROM), or other types of static storage devices that can store static information and instructions, or a random access memory (RAM), or other types of dynamic storage devices that can store information and instructions, or an electrically erasable programmable read-only memory (EEPROM), and the specifics are not limited here.
[0024] Exemplarily, the aforementioned memory 20 may be a double data rate synchronous dynamic random access memory DDR SDRAM (DDR for short). The memory 20 may exist independently but be connected to the processor 10. Optionally, the memory 20 may also be integrated with the processor 10. For example, integrated into one or more chips.
[0025] In some embodiments, the memory 20 may include a memory remotely located relative to the processor 10, and the remote memory may be connected to the electronic device via a network. Examples of the network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0026] The processor 10 uses various interfaces and lines to connect various parts of the entire electronic device 100, and performs various functions of the electronic device and processes data by running or executing software programs stored in the memory 20 and calling data stored in the memory 20, such as implementing the method described in any embodiment of the present application.
[0027] The processor 10 may be a Field Programmable Gate Array (FPGA), a Digital Signal Processor (DSP), a Central Processing Unit (CPU), or the like.
[0028] The processor 10 may be a single-core processor or a multi-core processor. For example, the processor 10 may be composed of multiple FPGAs or multiple DSPs. In addition, the processor 10 may refer to one or more devices, circuits and / or processing cores for processing data (e.g., computer program instructions). The processor 10 may be a separate semiconductor chip or may be integrated into a semiconductor chip together with other circuits. For example, the processor 10 may form a system-on-a-chip (SoC) with other circuits (e.g., codec circuits, hardware acceleration circuits, or various buses and interface circuits), or may be integrated into an application-specific integrated circuit (ASIC) as a built-in processor of the ASIC. The ASIC with the integrated processor may be packaged separately or may be packaged together with other circuits.
[0029] The communication interface 30 may use a transceiver, for example, to implement communication between the electronic device and other devices or a communication network.
[0030] The data between IoT devices generally include real-time data, historical data, configuration parameters, etc. Conventional transmission methods based on the Modbus protocol are based on limited specific signal quantity requirements, and the data point table (or register) is gradually expanded based on the demand in a toothpaste-squeezing manner, and the table length is infinitely increased by spreading the cake. It is not possible to efficiently collaborate with other IoT communication protocols based on the same data between different physical layer media and MCUs (such as delay and throughput). In order to solve the above problems, this application adopts the concept of files to summarize various types of data, establish a mapping relationship between files and file indexes, and encapsulate files and file indexes into data blocks using different encapsulation formats when performing file transmission. The transmission of data blocks is completed through the Modbus standard multi-byte read and multi-byte write, which realizes the decoupling of the transmission protocol and the transmission content, so that the Modbus protocol can be efficiently collaborated with other IoT communication protocols based on the same data between different physical layer media and MCUs.
[0031] The files in this application are formed by combining some data with interrelated contents and meanings in a preset format according to the requirements of the demander. For example, in the field of energy storage systems, various types of record files can be formed according to the data content that different demanders (such as customers, operation and maintenance engineers, and R&D engineers) need to view, such as fault records, operation records, historical power generation, equipment replacement records, etc.
[0032] Files have attributes such as file name, folder, file directory, file content, and file length. A file directory has different folders and folder levels, and a folder can have different files and subordinate folders. Directories and file names are used to distinguish the meanings of different files. A file may have different file lengths and file contents based on specific needs.
[0033] In order to adapt to data transmission of various sizes, this application adopts a data block-based transmission method. When the file to be transmitted is large, the file can be split into multiple data blocks for transmission, and the data offset is used to specify the data block to be transmitted each time, thereby realizing the breakpoint resume function.
[0034] Furthermore, based on the usage scenario of file transfer, the data block types are divided into two types: index type and content type. Among them, the index type data block mainly includes the directory and the file name, and is encapsulated in the first encapsulation format to indicate the directory to be queried or the target file to be read. The content type data block mainly includes the target index content or the target file content, and is encapsulated in the second encapsulation format to feedback the queried target index content or the read target file content to the requesting party. For example, the file reading data block and the file query data block below are index type data blocks, and the index content data block and the file content data block are content type data blocks.
[0035] The following describes the implementation process of the file transmission method based on the Modbus protocol in detail from the perspectives of the requester (i.e., the upper computer below) and the provider (i.e., the lower computer below).
[0036] Please refer to Figure 4 , Figure 4 The flowchart of the file transmission method based on the Modbus protocol applied to the host computer is exemplified, including: Step S401: encapsulate the target file index into a file read data block according to a preset first encapsulation format, and initiate a multi-byte write request to a subordinate computer based on a first preset address space corresponding to the first encapsulation format and the file read data block.
[0037] Specifically, the target file index includes the directory and file name of the target file. In one embodiment, the data block encapsulated in the first encapsulation format includes index information, data offset information and extension information. The index information includes directory and file name information. In one embodiment, the directory and file name information are represented by an ASCII string.
[0038] Please refer to Figure 6 , Figure 6 The structure diagram of the first packaging format is exemplarily shown. Figure 6 As shown, in the first encapsulation format, the index information field occupies 10 bytes, of which the 1st to 2nd bytes represent the directory, and the 3rd to 10th bytes represent the file name. Assuming that the 1st byte is ' / ' and the 2nd byte is the secondary directory name, 61 secondary directories under the root directory can be represented: '1'~'9', 'A'~'Z', 'a'~'z'. For example, " / Z012345678" represents the "012345678" file under the secondary directory 'Z'. The directory and file name are up to 10 bytes long, and if the length is insufficient, it is supplemented with '\0'. When all 10 bytes are '\0', it means reading the root directory; when the secondary directory is not '\0' and the file name is all '\0', it means reading the file and folder information under the secondary directory (i.e. the target index content); when the secondary directory is '\0' and the file name is not '\0', it means reading the target file under the root directory (i.e. the target file content); when both the secondary directory and the file name are not '\0', it means reading the target file under the secondary directory (i.e. the target file content). The upper computer notifies the lower computer to perform corresponding operations by filling different contents in the index information field.
[0039] The data offset information field occupies 4 bytes and contains the read offset position of the target file or target index. The extended information field occupies 2 bytes. Figure 6 The size of the data block after encapsulation in the first encapsulation format shown is 16 bytes in total, and the corresponding first preset address space is M~M+7, where M can be any specified value.
[0040] Please refer to Figure 8 , Figure 8 The format diagram of the Modbus protocol standard multi-byte write request frame and multi-byte write response frame is shown as an example. Specifically, the host computer fills the device address field of the multi-byte write request frame with the address of the slave computer, fills "0x10" in the function code field, fills the starting address of the first preset address space in the starting address field of the data field, and fills the size of the first preset address space in the register number field of the data field (such as Figure 6 8), fill the byte number field of the data field with data to read the total number of bytes of the data block (such as Figure 6 The upper computer sends the filled multi-byte write request frame to the communication bus to initiate a multi-byte write request to the lower computer. The communication buses supported by the Modbus protocol include RS-232, RS-485, RS-422, and Ethernet.
[0041] Step S402: after receiving a normal write response returned by the lower computer, a multi-byte read request is initiated to the lower computer based on a second preset address space corresponding to a preset second encapsulation format.
[0042] After the lower computer receives the multi-byte write request frame from the communication bus, it returns the multi-byte write response frame to the upper computer based on the received data. Specifically, the lower computer fills the lower computer address in the device address field of the multi-byte write response frame, fills "0x10" in the function code field, fills the starting address of the first preset address space in the starting address field of the data field, fills the size of the first preset address space in the register quantity field of the data field, and fills the CRC check value of the received data in the check field. After the lower computer fills the multi-byte write response frame, the multi-byte write response frame is sent to the communication bus. Wherein, when the lower computer verifies the data in the data field in the multi-byte write request frame correctly, it returns to write a normal response. Further, the lower computer parses the file read data block in the multi-byte write request frame, obtains the target file index, determines the corresponding target file according to the target file index, encapsulates the target file content into a file content data block based on the preset second encapsulation format, and waits for the upper computer to initiate a multi-byte read request.
[0043] After receiving the normal write response returned by the lower computer, the upper computer initiates a multi-byte read request to the lower computer based on the second preset address space corresponding to the preset second encapsulation format. In one embodiment, the data block encapsulated in the second encapsulation format includes data classification, total data, current data transmission length, data offset information and current load data.
[0044] Please refer to Figure 7 , Figure 7 The structure diagram of the second packaging format is exemplarily shown. Figure 7 As shown, in the second encapsulation format, the data classification field occupies 2 bytes, which are used to indicate the data category of the current load data. Among them, 0 indicates that the current load data is the target index content, and 1 indicates that the current load data is the target file content. The total data field occupies 4 bytes. When the data classification is 0, it indicates the total number of bytes of the target index content. When the data classification is 1, it indicates the total number of bytes of the target file content. The current data transmission length field occupies 2 bytes, indicating the number of bytes of the target index content or target file content transmitted this time. The data offset information field occupies 4 bytes, indicating the data offset of the current load data in the target index content or target file content. The current load data field is used to indicate the valid data uploaded. If the maximum length of a single load is 128 bytes, when a 200-byte target file content is transmitted, the target file content can be split into two data blocks. The data offset information field corresponding to the first data block is 0, and the data offset information field corresponding to the second data block is 128. Taking the current data transmission length of 128 bytes as an example, the data block size encapsulated by the second encapsulation format is 140 bytes in total, and the corresponding second preset address space size is N~N+70. It should be noted that the first preset address space and the second preset address space do not overlap each other.
[0045] Please refer to Fig. 9 , Fig. 9 The format diagram of the Modbus protocol standard multi-byte read request frame and multi-byte read response frame is exemplarily shown. Specifically, the host computer fills the device address field of the multi-byte read request frame with the address of the slave computer, fills "0x03" in the function code field, fills the starting address of the second preset address space in the starting address field of the data field, fills the size of the second preset address space in the register quantity field of the data field, and fills the CRC check value of all data in the data field in the check field. The host computer sends the filled multi-byte read request frame to the communication bus to initiate a multi-byte read request to the slave computer.
[0046] Step S403: receiving a response including a file content data block returned by the lower computer, parsing the file content data block based on the second encapsulation format, and obtaining target file content.
[0047] After receiving the multi-byte read request frame from the communication bus, the lower computer returns a multi-byte read response frame to the upper computer based on the received data. Specifically, the lower computer fills the lower computer address in the device address field of the multi-byte read response frame, fills "0x03" in the function code field, fills twice the size of the second preset address space in the byte number field of the data field, fills the content of the file content data block in order in the register value field of the data field, and fills the CRC check value of all data in the data field in the check field. The lower computer sends the filled multi-byte read response frame to the communication bus.
[0048] After the upper computer receives the response containing the file content data block returned by the lower computer, the file content data block is parsed based on the second encapsulation format to obtain data classification, total data, current data transmission length, data offset information and current load data. When the target file content to be transmitted is large, the method of the present application supports encapsulating the target file content into a plurality of file content data blocks and transmitting them to the upper computer in sequence. Specifically, the upper computer determines whether the target file has been transferred according to the total amount of data, the current data transmission length and the data offset information; if not, new data offset information is determined according to the current data transmission length and the data offset information, and the target file index and the new data offset information are encapsulated into a new file read data block according to the first encapsulation format, and a new round of multi-byte write requests is initiated to the lower computer based on the first preset address space and the new file read data block. After receiving a normal write response returned by the lower computer, a new round of multi-byte read requests is initiated to the lower computer based on the second preset address space, and a response containing a new file content data block returned by the lower computer is received, and the new file content data block is parsed based on the second encapsulation format to obtain the total amount of data, the current data transmission length, the data offset information and the current load data; the above steps are repeated until all target file contents are transferred.
[0049] When all the target file contents are received, the host computer will splice the target file contents received multiple times into a complete file according to the data offset information. Furthermore, the host computer can parse the corresponding signal information according to the target file content encoding rules agreed upon in advance with the lower computer. For example, if the target file is formed in the order of current signal, voltage signal, power signal, etc., the host computer can parse the value of the current signal, the value of the voltage signal, and the value of the power signal according to the rule.
[0050] In one embodiment, the host computer can also send a query directory index to the subordinate computer to obtain the file or folder information under the query directory index. Specifically, the file transmission method based on the Modbus protocol applied to the host computer also includes: encapsulating the query directory index into a file query data block according to the first encapsulation format, initiating a multi-byte write request to the subordinate computer based on the first preset address space and the file query data block; after receiving the normal write response returned by the subordinate computer, initiating a multi-byte read request to the subordinate computer based on the second preset address space; receiving the response containing the index content data block returned by the subordinate computer, parsing the index content data block based on the second encapsulation format, and obtaining the target index content. Among them, the query directory index includes the query directory information (please refer to the previous description for details), and the target index content includes the file or folder information of the preset level under the query directory. For example, if the preset level is level 2, the target index content includes the files and folders under the query directory, and the file and folder information under the query directory folder.
[0051] Please refer to Figure 5 , Figure 5 The flowchart of the file transmission method based on the Modbus protocol applied to the lower computer is exemplarily shown, including: Step S501, after receiving a multi-byte write request initiated by a host computer based on a first preset address space corresponding to a preset first encapsulation format and a file read data block, a normal write response is returned to the host computer based on the first preset address space.
[0052] Step S502, parsing the file reading data block based on the first encapsulation format to obtain a target file index, determining a target file corresponding to the target file index, and encapsulating the target file content into a file content data block based on a preset second encapsulation format.
[0053] Step S503: after receiving the multi-byte read request initiated by the host computer based on the second preset address space corresponding to the second encapsulation format, a response including the file content data block is returned to the host computer based on the second preset address space.
[0054] In one embodiment, before returning a response containing a file content data block, the lower computer determines the total number of bytes of the target file and the maximum payload length of a single transmission (e.g., Figure 7 The target file content is split, and based on the offset information carried by the host computer in the file reading module, the target file content corresponding to the offset information is encapsulated into a file content data block and returned to the host computer.
[0055] For example, after the lower computer parses the file read data block, it obtains the target file index as " / z123.txt0" and the offset information as 0. According to the target file index, it can be determined that the target file is the "123.txt" file in the secondary directory "z" folder. If the size of the "123.txt" file is 200 bytes and the maximum load length of a single transmission is 128 bytes, the "123.txt" file can be split into two parts and then encapsulated into two file content data blocks. Among them, the current load data of the first file content data block starts from the starting data of the "123.txt" file and has a length of 128 bytes; the current load data of the second file content data block starts from the 128th byte of the "123.txt" file and has a length of 72 bytes. It can be understood that the length of the current load data in the first file content data block may be less than 128 bytes.
[0056] In one embodiment, the lower computer can also obtain the query directory index sent by the upper computer, and return the file or folder information under the query directory index to the upper computer. Specifically, the file transfer method based on the Modbus protocol applied to the lower computer also includes: after receiving the multi-byte write request initiated by the upper computer based on the first preset address space and the file query data block, returning a normal write response to the upper computer based on the first preset address space; parsing the file query data block based on the first encapsulation format to obtain the query directory index, determining the corresponding target index content according to the query directory index, and encapsulating the target index content into an index content data block based on the second encapsulation format; after receiving the multi-byte read request initiated by the upper computer based on the second preset address space, returning the index content data block to the upper computer based on the second preset address space. It can be understood that when the target index content data is large, the method of the present application also supports encapsulating the target index content into several index content data blocks, and the upper computer and the lower computer can obtain all the target index contents through multiple multi-byte write requests and multi-byte read requests.
[0057] Please refer to Fig.10 , Fig.10 The following is a schematic diagram of the entire file transfer process based on the Modbus protocol. Specifically, the entire file transfer process includes the following four steps: Step 1001, the upper computer sends the query directory index to the lower computer by writing a first preset address space with multiple bytes, and the lower computer returns a normal write response.
[0058] Specifically, the host computer sends the query directory index through the multi-byte write [M~M+7] address method. For example, if the query directory index is the root directory, the index information field of the [M~M+7] address space is filled with "0000000000", the data offset information field is filled with 0, and the extended information field is filled with 0.
[0059] Step 1002: The upper computer queries the target index content of the lower computer by multi-byte reading of the second preset address space, and the lower computer returns a response including the target index content.
[0060] Specifically, the host computer obtains the target index content through the multi-byte read [address N~N+69] method, and the lower computer responds to the directory query of the host computer, constructs a response frame containing the index content data block and returns it to the host computer. If there is only one file named "alarm" in the query directory, the data classification of the constructed index content data block is 0, the total number of bytes of the target index content is 5, the current data transmission length is 5, the data offset is 0, and the current load data is "alarm" + 123 "0" (a total of 128 bytes).
[0061] Step 1003, the upper computer sends the target file index to the lower computer by writing a first preset address space with multiple bytes, and the lower computer returns a normal write response.
[0062] Specifically, the host computer sends the target file index through the multi-byte write [M~M+7] address method. For example, if the target file index is the "alarm" file in the root directory, then fill "00alarm000" in the index information field of the [M~M+7] address space, fill 0 in the data offset information field, and fill 0 in the extended information field.
[0063] Step 1004, the upper computer queries the target file content of the lower computer by multi-byte reading of the second preset address space, and the lower computer returns a response including the target file content.
[0064] Specifically, the host computer obtains the target file content through the multi-byte read [address N~N+69] method, and the lower computer responds to the file query of the host computer, constructs a response frame containing the file content data block and returns it to the host computer. If the total number of bytes of the "alarm" file is 64 bytes, the data classification of the constructed file content data block is 1, the total number of bytes of the target file content is 64, the current data transmission length is 64, the data offset is 0, and the current load data is 64 bytes of the "alarm" file content + 64 "0".
[0065] The implementation method of step 1003 and step 1004 is similar to step 1001 and step 1002. When the target file content exceeds the maximum payload length of a single transmission, step 1003 and step 1004 may be repeated multiple times until all target file content is transmitted; when the target index content exceeds the maximum payload length of a single transmission, step 1001 and step 1002 may be repeated multiple times until all target index content is transmitted.
[0066] The file transmission method based on the Modbus protocol provided in the embodiment of the present application adopts the file concept to summarize various types of data and establishes a mapping relationship between files and file indexes. When performing file transmission, the files and file indexes are encapsulated into data blocks using different encapsulation formats, and the transmission of data blocks is completed through the Modbus standard multi-byte read and multi-byte write, thereby realizing the decoupling of the transmission protocol and the transmission content, so that the Modbus protocol can be efficiently coordinated with other Internet of Things communication protocols based on the same data between different physical layer media and MCUs.
[0067] According to an embodiment of the present application, a computer-readable storage medium is provided, the type of which is as described above, the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the processor executes the steps of the above-mentioned file transmission method based on the Modbus protocol.
[0068] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Under the concept of the present application, the technical features in the above embodiments or different embodiments can also be combined, the steps can be implemented in any order, and there are many other changes in different aspects of the present application as described above, which are not provided in detail for the sake of simplicity. Although the present application has been described in detail with reference to the aforementioned embodiments, a person of ordinary skill in the art should understand that the technical solutions described in the aforementioned embodiments can still be modified, or some of the technical features can be replaced by equivalents. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A file transmission method based on Modbus protocol, characterized in that: Applied to a host computer, the method includes: Encapsulate the target file index into a file read data block according to a preset first encapsulation format, and initiate a multi-byte write request to the subordinate computer based on a first preset address space corresponding to the first encapsulation format and the file read data block; After receiving a normal write response returned by the lower computer, a multi-byte read request is initiated to the lower computer based on a second preset address space corresponding to a preset second encapsulation format; A response including a file content data block returned by the lower computer is received, and the file content data block is parsed based on the second encapsulation format to obtain target file content.
2. The method according to claim 1, characterized in that The data block encapsulated in the first encapsulation format includes index information and data offset information, and the data block encapsulated in the second encapsulation format includes data classification, total data, current data transmission length, data offset information and current load data.
3. The method according to claim 2, characterized in that The index information field contains directory and file names.
4. The method according to claim 2, characterized in that: The data after parsing the file content data block based on the second encapsulation format includes the total number of data, the current data transmission length, the data offset information and the current load data, the method further includes: Determining whether the target file has been transferred according to the total amount of data, the current data transfer length and the data offset information; If not, new data offset information is determined according to the current data transmission length and the data offset information; the target file index and the new data offset information are encapsulated into a new file read data block according to the first encapsulation format, a new round of multi-byte write requests is initiated to the subordinate computer based on the first preset address space and the new file read data block, after receiving a normal write response returned by the subordinate computer, a new round of multi-byte read requests is initiated to the subordinate computer based on the second preset address space, and a response containing a new file content data block is received returned by the subordinate computer, and the new file content data block is parsed based on the second encapsulation format to obtain the total number of data, the current data transmission length, the data offset information and the current load data; Repeat the above steps until all target file contents are transferred.
5. The method according to any one of claims 1 to 4, characterized in that: The method further comprises: Encapsulating the query directory index into a file query data block according to the first encapsulation format, and initiating a multi-byte write request to the subordinate computer based on the first preset address space and the file query data block; After receiving a normal write response returned by the lower computer, initiating a multi-byte read request to the lower computer based on the second preset address space; A response including an index content data block returned by the lower computer is received, and the index content data block is parsed based on the second encapsulation format to obtain target index content.
6. A file transmission method based on Modbus protocol, characterized in that: Applied to a lower computer, the method includes: After receiving a multi-byte write request initiated by the host computer based on the first preset address space corresponding to the preset first encapsulation format and the file read data block, returning a normal write response to the host computer; Parsing the file reading data block based on the first encapsulation format to obtain a target file index, determining a target file corresponding to the target file index according to the target file index, and encapsulating the target file content into a file content data block based on a preset second encapsulation format; After receiving a multi-byte read request initiated by the host computer based on the second preset address space corresponding to the second encapsulation format, a response including the file content data block is returned to the host computer based on the second preset address space.
7. The method according to claim 6, characterized in that The data block encapsulated in the first encapsulation format includes index information and data offset information, and the data block encapsulated in the second encapsulation format includes data classification, total data, current data transmission length, data offset information and current load data.
8. The method according to claim 6 or 7, characterized in that: The method further comprises: After receiving the multi-byte write request initiated by the host computer based on the first preset address space and the file query data block, returning a normal write response to the host computer; Parsing the file query data block based on the first encapsulation format to obtain a query directory index, determining a target index content corresponding to the query directory index according to the query directory index, and encapsulating the target index content into an index content data block based on the second encapsulation format; After receiving the multi-byte read request initiated by the host computer based on the second preset address space, the index content data block is returned to the host computer based on the second preset address space.
9. An electronic device, characterized in that: The electronic device includes at least one processor and a memory communicatively connected to the at least one processor, the memory storing instructions executable by the at least one processor, the instructions being executed by the at least one processor so that the at least one processor can execute the method as described in any one of claims 1 to 8.
10. A computer storage medium, characterized in that: The computer storage medium stores instructions or programs, and when the instructions or programs are executed by at least one processor, the at least one processor is caused to perform the method according to any one of claims 1 to 8.
Citation Information
Patent Citations
A Modbus protocol-based data receiving and transmitting system and method
CN109743301A
Software burning method and device
CN109976767A
Data reading method and computer equipment
CN112015797A
File transmission method and device based on communication protocol of multifunctional electric energy meter
CN117221310A
Batch file transmission method, client and server
CN117560361A
Cited By
Data communication method, device and equipment based on line concentration equipment
CN120639847A