File transmission method and device based on modbus protocol and storage medium
By adopting a data block transmission method based on file concepts and encapsulation formats in the Modbus protocol, the problems of data structuring and length limitations are solved, enabling efficient collaborative transmission between IoT devices.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SHENZHEN POWEROAK NEWENER CO LTD
- Filing Date
- 2025-03-12
- Publication Date
- 2026-05-29
AI Technical Summary
The existing Modbus protocol cannot achieve data structuring and has limited data length in IoT communication, which makes it impossible to achieve efficient collaboration between different physical layer media and MCUs.
Data is aggregated using the concept of files, and files and file indexes are encapsulated into data blocks using different encapsulation formats. Data block transmission is completed using multi-byte read and write of the Modbus protocol, thereby decoupling the transmission protocol and content.
It enables efficient collaboration between the Modbus protocol and other IoT communication protocols across different physical layer media and MCUs, and supports breakpoint resumption of large file transfers and the transmission of various data types.
Smart Images

Figure CN119835266B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of Modbus communication technology, specifically to a file transfer method, device, and storage medium based on the Modbus protocol. Background Technology
[0002] In existing IoT communication networks, various physical transmission media are used, such as Ethernet (wired, wireless, Wi-Fi, / 2 / 3 / 4 / 5G cellular networks), serial communication (UART / RS232 / RS485), and CAN (Controller Area Network). Equivalently unified under the OSI (Open System Interconnect) communication data model, different physical transmission media correspond to different application layer communication protocols. In current technologies, Ethernet and serial communication commonly use the standard Modbus communication protocol, while CAN bus commonly uses standard application layer protocols such as CANopen and SAE J1939. Each standard is only applicable to a limited range within a specific physical layer and lacks good compatibility for cross-physical layer data transmission.
[0003] Data between IoT devices typically includes real-time data, historical data, and configuration parameters. Conventional transmission methods based on the Modbus protocol are limited by specific semaphore requirements, incrementally expanding the data point table (or register) as needed, thus endlessly increasing the table length. Because data content is linked to the data point table, the data cannot be well structured. Different data structures require different data point table addresses for differentiation, and the data length is limited by the protocol address length range; the maximum address length in the Modbus protocol is 65536.
[0004] 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 perform efficient collaboration (such as latency and throughput) with other IoT communication protocols based on the same data across different physical layer media and MCUs (Microcontroller Units). Summary of the Invention
[0005] This application provides a file transfer method, device, and storage medium based on the Modbus protocol, which solves the problem that traditional Modbus protocol data cannot be structured and has limited data length, thus preventing efficient collaboration with other IoT communication protocols based on the same data across different physical layer media and MCUs.
[0006] In a first aspect, embodiments of this application provide a file transfer method based on the Modbus protocol, applied to a host computer, the method comprising:
[0007] The target file index is encapsulated into a file reading data block according to a preset first encapsulation format, and a multi-byte write request is initiated to the lower-level machine based on the first preset address space corresponding to the first encapsulation format and the file reading data block.
[0008] After receiving a write success response from the lower-level machine, a multi-byte read request is initiated to the lower-level machine based on the second preset address space corresponding to the preset second encapsulation format.
[0009] The system receives a response from the lower-level machine containing a block of file content data, and parses the block of file content data based on the second encapsulation format to obtain the target file content.
[0010] Optionally, the data block encapsulated using the first encapsulation format includes index information and data offset information, while the data block encapsulated using the second encapsulation format includes data category, total number of data, current data transmission length, data offset information, and current load data.
[0011] Optionally, the index information field includes the directory and filename.
[0012] Optionally, the data parsed from the file content data blocks based on the second encapsulation format includes the total number of data, the current data transmission length, data offset information, and the current payload data. The method further includes:
[0013] Determine whether the target file has been completely transmitted based on the total number of data, the current data transmission length, and the data offset information;
[0014] If not, then determine new data offset information based on the current data transmission length and the data offset information; encapsulate the target file index and the new data offset information into a new file read data block according to the first encapsulation format; initiate a new round of multi-byte write requests to the lower-level machine based on the first preset address space and the new file read data block; after receiving a write normal response from the lower-level machine, initiate a new round of multi-byte read requests to the lower-level machine based on the second preset address space; and receive a response from the lower-level machine containing a new file content data block; then parse the new file content data block 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.
[0015] Repeat the above steps until all target file contents have been transferred.
[0016] Optionally, the method further includes:
[0017] The query directory index is encapsulated into a file query data block according to the first encapsulation format, and a multi-byte write request is initiated to the lower-level machine based on the first preset address space and the file query data block.
[0018] After receiving a write success response from the lower-level machine, a multi-byte read request is initiated to the lower-level machine based on the second preset address space;
[0019] The system receives a response from the lower-level machine containing a data block of index content, and parses the data block of index content based on the second encapsulation format to obtain the target index content.
[0020] Secondly, embodiments of this application provide a file transfer method based on the Modbus protocol, applied to a lower-level machine, the method comprising:
[0021] After receiving a multi-byte write request initiated by the host computer based on the first preset address space and file read data block corresponding to the first preset encapsulation format, return a write normal response to the host computer.
[0022] The file reading data block is parsed based on the first encapsulation format to obtain the target file index. The target file corresponding to the target file is determined based on the target file index, and the content of the target file is encapsulated into file content data blocks based on the preset second encapsulation format.
[0023] 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, the host computer returns a response containing the file content data block based on the second preset address space.
[0024] Optionally, the data block encapsulated using the first encapsulation format includes index information and data offset information, while the data block encapsulated using the second encapsulation format includes data category, total number of data, current data transmission length, data offset information, and current load data.
[0025] Optionally, the method further includes:
[0026] Upon receiving a multi-byte write request initiated by the host computer based on the first preset address space and file query data block, return a write normal response to the host computer.
[0027] The file query data block is parsed based on the first encapsulation format to obtain the query directory index. The target index content corresponding to the query directory index is determined, and the target index content is encapsulated into an index content data block based on the second encapsulation format.
[0028] Upon receiving a 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.
[0029] Thirdly, embodiments of this application provide an electronic device, including: 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 to enable the at least one processor to perform the method described above.
[0030] Fourthly, embodiments of this application provide a computer storage medium storing instructions or programs that, when executed by at least one processor, cause the at least one processor to perform any of the methods described above.
[0031] This application provides a file transfer method based on the Modbus protocol. The host computer encapsulates the target file index into a file read data block according to a preset first encapsulation format. Based on the first preset address space corresponding to the first encapsulation format and the file read data block, it initiates a multi-byte write request to the slave computer. After receiving a write success response from the slave computer, it initiates a multi-byte read request to the slave computer based on the second preset address space corresponding to a preset second encapsulation format. Upon receiving a response from the slave computer containing a file content data block, it parses the file content data block based on the second encapsulation format to obtain the target file content. This method uses the concept of a file to aggregate various types of data and establishes a mapping relationship between files and file indexes. During file transfer, files and file indexes are encapsulated into data blocks using different encapsulation formats. Data block transfer is completed through Modbus standard multi-byte read and multi-byte write operations, achieving decoupling between the transmission protocol and the transmitted content. This allows the Modbus protocol to efficiently collaborate with other IoT communication protocols based on the same data across different physical layer media and MCUs. Attached Figure Description
[0032] Figure 1 An exemplary bus topology diagram based on the Modbus protocol is shown.
[0033] Figure 2 An exemplary diagram of a Modbus RTU message frame is shown.
[0034] Figure 3 An exemplary schematic diagram of the hardware structure of an electronic device is shown;
[0035] Figure 4 An exemplary flowchart illustrates a file transfer method based on the Modbus protocol applied to a host computer.
[0036] Figure 5 An exemplary flowchart illustrates a file transfer method based on the Modbus protocol applied to a lower-level machine;
[0037] Figure 6 An exemplary structural diagram of the first encapsulation format is shown;
[0038] Figure 7 An exemplary structural diagram of the second encapsulation format is shown;
[0039] Figure 8 The diagram illustrates the format of the Modbus protocol standard multibyte write request frame and multibyte write response frame.
[0040] Figure 9 The diagram illustrates the format of the Modbus protocol standard multibyte read request frame and multibyte read response frame.
[0041] Figure 10 An exemplary diagram illustrating the entire file transfer process based on the Modbus protocol is provided. Detailed Implementation
[0042] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0043] Please refer to Figure 1 , Figure 1 An exemplary bus topology diagram based on the Modbus protocol is shown. Figure 1 As shown, multiple devices are connected to the same bus. To avoid data conflicts, the Modbus protocol uses a master-slave communication method. A Modbus communication system includes a master device (such as...). Figure 1 (A) and multiple slave devices (such as...) Figure 1(Device B, C, and D in the diagram). The Modbus protocol uses a request-response model and stipulates that Modbus requests can only be initiated by the master. That is, the master sends request data to the slave, and the slave can only receive requests from the master and respond accordingly, returning response data to the master. Slaves cannot initiate requests; they can only passively receive requests from the master. To avoid data conflicts, the master cannot send data in parallel. After sending the first piece of data, the master can only send the second piece of data after waiting for a response from the slave or after the response times out. Each slave has only one slave address in the system. When the master sends a data request, it includes the target slave's address in the request data. When the master sends data to the bus, every slave receives the data, but only the slave with the same target address as the one in the request data will respond; other slaves will ignore the request.
[0044] Please refer to Figure 2 , Figure 2 An exemplary diagram of a Modbus RTU message frame is shown. Figure 2 As shown, an RTU (Remote Terminal Unit) message frame consists of an address code, a function code, a data field, and a checksum. The address code, occupying one byte, identifies the destination address of the message; each slave device has a unique address. The function code, also occupying one byte, indicates the type of operation performed by the master on the slave device. The data field, occupying 0-252 bytes, has its length and content determined by the function code. The checksum, occupying two bytes, ensures correct message transmission.
[0045] Please refer to Figure 3 , Figure 3 An exemplary hardware structure diagram of an electronic device is shown. For example... Figure 3 As shown, the electronic device 100 includes a processor 10, a memory 20, and a communication interface 30. The processor 10, memory 20, and communication interface 30 are connected via lines. Figure 3 In the embodiment shown, the processor 10, memory 20, and communication interface 30 are connected to each other via a bus.
[0046] 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 the operating system and application programs required for at least one function; the data storage area may store data created based on the use of the electronic device, etc.
[0047] The memory 20 can be a read-only memory (ROM), or other types of static storage devices that can store static information and instructions, or random access memory (RAM), or other types of dynamic storage devices that can store information and instructions, or electrically erasable programmable read-only memory (EEPROM). The specific type is not limited here.
[0048] For example, the aforementioned memory 20 can be Double Data Rate Synchronous Dynamic Random Access Memory (DDR SDRAM). This memory 20 can exist independently but is connected to the processor 10. Optionally, the memory 20 can also be integrated with the processor 10, for example, integrated within one or more chips.
[0049] In some embodiments, memory 20 may optionally include memory remotely located relative to processor 10, and this remote memory may be connected to the electronic device via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0050] The processor 10 connects various parts of the electronic device 100 using various interfaces and lines. By running or executing software programs stored in the memory 20 and calling data stored in the memory 20, it performs various functions of the electronic device and processes data, such as implementing the methods described in any embodiment of this application.
[0051] The processor 10 can be a field programmable gate array (FPGA), a digital signal processor (DSP), a central processing unit (CPU), or the like.
[0052] Processor 10 can be a single-core processor or a multi-core processor. For example, processor 10 can be composed of multiple FPGAs or multiple DSPs. Furthermore, processor 10 can refer to one or more devices, circuits, and / or processing cores for processing data (e.g., computer program instructions). Processor 10 can be a standalone semiconductor chip or integrated with other circuits into a single semiconductor chip. For example, it can form a system-on-a-chip (SoC) with other circuits (such as codec circuits, hardware acceleration circuits, or various bus and interface circuits), or it can be integrated as a built-in processor within an application-specific integrated circuit (ASIC). This ASIC with integrated processor can be packaged separately or together with other circuits.
[0053] The communication interface 30 can use a transceiver device, such as a transceiver, to enable communication between the electronic device and other devices or communication networks.
[0054] Data between IoT devices typically includes real-time data, historical data, and configuration parameters. Conventional Modbus-based transmission methods are limited by specific semaphore requirements, incrementally expanding the data point table (or register) as needed, resulting in an indefinite increase in table length. This hinders efficient collaboration (e.g., reducing latency and throughput) with other IoT communication protocols across different physical layer media and MCUs based on the same data. To address these issues, this application uses a file concept to aggregate various data types, establishing a mapping between files and file indexes. During file transmission, files and file indexes are encapsulated into data blocks using different encapsulation formats. Data block transmission is completed through Modbus standard multi-byte reads and writes, decoupling the transmission protocol from the transmitted content. This allows the Modbus protocol to efficiently collaborate with other IoT communication protocols across different physical layer media and MCUs based on the same data.
[0055] The documents in this application are formed by combining data with interrelated content in a preset format according to the requirements of the demand party. For example, in the field of energy storage systems, various types of record files can be formed according to the data content that different demand parties (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.
[0056] Files have attributes such as filename, folder, directory, file content, and file length. A directory can contain different folders and folder levels, and a folder can contain different files and subfolders. Directory and filename are used to distinguish the meaning of different files, and a file may have different file lengths and file contents depending on specific needs.
[0057] To accommodate data transmission of various sizes, this application adopts a data block-based transmission method. When the file to be transmitted is large, it can be split into multiple data blocks for transmission, and the data block to be transmitted each time can be specified by the data offset, thereby realizing the function of resuming interrupted transmission.
[0058] Furthermore, based on the use case of file transfer, data block types are divided into two categories: index type and content type. Index type data blocks mainly include the directory and filename, encapsulated using a first encapsulation format, and are used to indicate the directory to be queried or the target file to be read. Content type data blocks mainly include the target index content or target file content, encapsulated using a second encapsulation format, and are used to provide feedback to the requesting party regarding the queried target index content or read target file content. For example, the file read data block and file query data block in the following text are index type data blocks, while the index content data block and file content data block are content type data blocks.
[0059] The implementation process of the file transfer method based on the Modbus protocol will be described in detail from the perspectives of the requesting party (i.e., the host computer below) and the providing party (i.e., the slave computer below).
[0060] Please refer to Figure 4 , Figure 4 An exemplary flowchart illustrates a file transfer method based on the Modbus protocol applied to a host computer, including:
[0061] Step S401: Encapsulate the target file index into a file reading data block according to a preset first encapsulation format, and initiate a multi-byte write request to the lower-level machine based on the first preset address space corresponding to the first encapsulation format and the file reading data block.
[0062] Specifically, the target file index includes the target file's directory and filename. In one embodiment, the data block encapsulated using the first encapsulation format includes index information, data offset information, and extended information. The index information includes directory and filename information. In one embodiment, the directory and filename information is represented using ASCII strings.
[0063] Please refer to Figure 6 , Figure 6 An exemplary structural diagram of the first encapsulation format is shown. For example... Figure 6As shown, in the first encapsulation format, the index information field occupies 10 bytes. Bytes 1-2 represent the directory, and bytes 3-10 represent the filename. Assuming the first byte is ' / ' and the second byte is the second-level directory name, it can represent 61 types of second-level directories under the root directory: '1'~'9', 'A'~'Z', 'a'~'z'. For example, " / Z012345678" represents the file "012345678" under the second-level directory 'Z'. Directory and filenames are at most 10 bytes long; if the length is insufficient, it is padded with '\0'. When all 10 bytes are '\0', it indicates reading the root directory; when the second-level directory is not '\0' and the filename is all '\0', it indicates reading the file and folder information (i.e., the target index content) under the second-level directory; when the second-level directory is '\0' and the filename is not '\0', it indicates reading the target file (i.e., the target file content) under the root directory; when neither the second-level directory nor the filename is '\0', it indicates reading the target file (i.e., the target file content) under the second-level directory. The host computer notifies the slave computer to perform corresponding operations by filling different content into the index information field.
[0064] 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 data block size after encapsulation in the first encapsulation format shown is a total of 16 bytes, and the corresponding first preset address space is M~M+7, where M can be any specified value.
[0065] Please refer to Figure 8 , Figure 8 This example illustrates the format of a Modbus protocol standard multibyte write request frame and multibyte write response frame. Specifically, the host computer fills the slave device address into the device address field of the multibyte write request frame, "0x10" into the function code field, the start address field of the data field with the start address of the first preset address space, and the register count field of the data field with the size of the first preset address space (e.g., ...). Figure 6 As shown in Figure 8), fill the byte number segment of the data field with the total number of bytes read from the data block (e.g., ...). Figure 6 (As shown in the diagram, this is a 16-byte block). The register value field in the data field is filled sequentially with the data to read the data block's contents. The checksum field is filled with the CRC checksum values of all data in the data field. The host computer sends the filled multi-byte write request frame to the communication bus to initiate a multi-byte write request to the slave computer. The Modbus protocol supports communication buses including RS-232, RS-485, RS-422, and Ethernet.
[0066] Step S402: After receiving the write normal response from the lower-level machine, initiate a multi-byte read request to the lower-level machine based on the second preset address space corresponding to the preset second encapsulation format.
[0067] After receiving a multi-byte write request frame from the communication bus, the lower-level machine returns a multi-byte write response frame to the upper-level machine based on the received data. Specifically, the lower-level machine fills the lower-level machine address in the device address field, "0x10" in the function code field, the start address of the first preset address space in the start address field of the data field, the size of the first preset address space in the register quantity field of the data field, and the CRC checksum of the received data in the checksum field. After filling the multi-byte write response frame, the lower-level machine sends it to the communication bus. If the lower-level machine verifies the data in the data field of the multi-byte write request frame and finds it correct, it returns a write success response. Further, the lower-level machine parses the file read data block in the multi-byte write request frame to obtain the target file index, determines the corresponding target file based on the target file index, and encapsulates the target file content into a file content data block based on a preset second encapsulation format, waiting for the upper-level machine to initiate a multi-byte read request.
[0068] After receiving a write success response from the slave device, the host computer initiates a multi-byte read request to the slave device based on the second preset address space corresponding to the preset second encapsulation format. In one embodiment, the data block encapsulated using the second encapsulation format includes data category, total data count, current data transmission length, data offset information, and current load data.
[0069] Please refer to Figure 7 , Figure 7 An exemplary structural diagram of the second encapsulation format is shown. For example... Figure 7As shown, in the second encapsulation format, the data category field occupies 2 bytes and is used to indicate the data category of the current payload data. 0 indicates that the current payload data is target index content, and 1 indicates that the current payload data is target file content. The total data field occupies 4 bytes; when the data category is 0, it indicates the total number of bytes in the target index content; when the data category is 1, it indicates the total number of bytes in the target file content. The current data transmission length field occupies 2 bytes and indicates the number of bytes of the target index content or target file content being transmitted. The data offset information field occupies 4 bytes and indicates the data offset of the current payload data within the target index content or target file content. The current payload data field is used to indicate the valid data being uploaded. For example, if the maximum length of a single payload is 128 bytes, when transmitting a 200-byte target file content, 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 a current data transmission length of 128 bytes as an example, the total size of the data block after encapsulation using the second encapsulation format is 140 bytes, and the corresponding second preset address space size is N~N+70. It should be noted that the first and second preset address spaces do not overlap.
[0070] Please refer to Figure 9 , Figure 9 This example illustrates the format of a Modbus protocol standard multibyte read request frame and multibyte read response frame. Specifically, the host computer fills the slave device's address into the device address field of the multibyte read request frame, "0x03" into the function code field, the start address field of the data field with the start address of the second preset address space, the register count field of the data field with the size of the second preset address space, and the checksum field with the CRC checksum value of all data in the data field. The host computer then sends the filled multibyte read request frame to the communication bus to initiate a multibyte read request to the slave device.
[0071] Step S403: Receive the response from the lower-level machine containing a data block of file content, parse the data block of file content based on the second encapsulation format, and obtain the target file content.
[0072] After receiving a multi-byte read request frame from the communication bus, the lower-level device returns a multi-byte read response frame to the upper-level device based on the received data. Specifically, the lower-level device fills the device address field with its own address, the function code field with "0x03", the byte number field of the data field with twice the size of the second preset address space, the register value field of the data field with the contents of the file content data blocks in sequence, and the checksum field with the CRC checksum values of all data in the data field. The lower-level device then sends the filled multi-byte read response frame to the communication bus.
[0073] After receiving the response containing file content data blocks from the slave device, the host computer parses the file content data blocks based on the second encapsulation format to obtain data classification, total data count, current data transmission length, data offset information, and current load data. When the target file content to be transmitted is large, the method of this application supports encapsulating the target file content into several file content data blocks and transmitting them to the host computer sequentially. Specifically, the host computer determines whether the target file has been completely transmitted based on the total data count, current data transmission length, and data offset information. If not, it determines new data offset information based on the current data transmission length and data offset information, encapsulates the target file index and the new data offset information into a new file read data block according to the first encapsulation format, and initiates a new round of multi-byte write requests to the slave computer based on the first preset address space and the new file read data block. After receiving a write success response from the slave computer, it initiates a new round of multi-byte read requests to the slave computer based on the second preset address space, and receives a response from the slave computer containing a new file content data block. Then, it parses the new file content data block according to the second encapsulation format to obtain the total data count, current data transmission length, data offset information, and current load data. The above steps are repeated until all target file content is transmitted.
[0074] Once the entire target file content has been received, the host computer concatenates the multiple received target file contents into a single complete file according to the data offset information. Furthermore, the host computer can parse the corresponding signal information based on the target file content encoding rules pre-agreed with the slave computer. For example, if the target file is formed by sequentially sorting current signals, voltage signals, and electrical quantity signals, the host computer can parse the values of the current signal, voltage signal, and electrical quantity signal according to this rule.
[0075] In one embodiment, the host computer can also send a query directory index to the slave computer to obtain file or folder information under the query directory index. Specifically, the file transfer method based on the Modbus protocol applied to the host computer further includes: encapsulating the query directory index into a file query data block according to a first encapsulation format; initiating a multi-byte write request to the slave computer based on the first preset address space and the file query data block; after receiving a write success response from the slave computer, initiating a multi-byte read request to the slave computer based on a second preset address space; receiving a response from the slave computer containing an index content data block; parsing the index content data block based on the second encapsulation format to obtain the target index content. The query directory index includes query directory information (see the previous description for details), and the target index content includes file or folder information at a preset level under the query directory. For example, if the preset level is 2, the target index content includes files and folders under the query directory, as well as file and folder information under the query directory folder.
[0076] Please refer to Figure 5 , Figure 5 An exemplary flowchart illustrates a file transfer method based on the Modbus protocol applied to a lower-level machine, including:
[0077] Step S501: After receiving the 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, return a normal write response to the host computer based on the first preset address space.
[0078] Step S502: Parse the file reading data block based on the first encapsulation format to obtain the target file index, determine the corresponding target file based on the target file index, and encapsulate the target file content into a file content data block based on the preset second encapsulation format.
[0079] Step S503: 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, return a response containing a file content data block to the host computer based on the second preset address space.
[0080] In one embodiment, before returning a response containing blocks of file content data, the lower-level device determines the maximum payload length for a single transmission (e.g., the total number of bytes in the target file and the maximum payload length for a single transmission) based on the target file's total size and the maximum payload length for a single transmission. Figure 7 The target file content is split into 128 bytes, 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.
[0081] For example, after parsing the file read data block, the lower-level machine obtains the target file index as " / z123.txt0" with an offset of 0. Based on this target file index, the target file can be determined to be the "123.txt" file located in the second-level directory "z". If the size of the "123.txt" file is 200 bytes, and the maximum payload length for a single transmission is 128 bytes, then the "123.txt" file can be split into two parts and encapsulated into two file content data blocks. The first file content data block's current payload data starts from the beginning of the "123.txt" file and has a length of 128 bytes; the second file content data block's current payload data 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 payload data in the first file content data block can be less than 128 bytes.
[0082] In one embodiment, the lower-level machine can also obtain the query directory index sent by the upper-level machine and return the file or folder information under the query directory index to the upper-level machine. Specifically, the file transfer method based on the Modbus protocol applied to the lower-level machine further includes: after receiving a multi-byte write request initiated by the upper-level machine based on a first preset address space and a file query data block, returning a write normal response to the upper-level machine based on the first preset address space; parsing the file query data block based on a first encapsulation format to obtain the query directory index, determining the corresponding target index content based on the query directory index, and encapsulating the target index content into an index content data block based on a second encapsulation format; after receiving a multi-byte read request initiated by the upper-level machine based on a second preset address space, returning the index content data block to the upper-level machine based on the second preset address space. It can be understood that when the target index content data is large, the method of this application also supports encapsulating the target index content into several index content data blocks, and the upper-level machine and the lower-level machine can obtain the entire target index content through multiple multi-byte write requests and multi-byte read requests.
[0083] Please refer to Figure 10 , Figure 10 This example illustrates a complete file transfer process based on the Modbus protocol. Specifically, the entire file transfer process includes the following four steps:
[0084] Step 1001: The host computer sends the query directory index to the slave computer by writing a multi-byte message to the first preset address space, and the slave computer returns a write success response.
[0085] Specifically, the host computer sends the query directory index by writing a multi-byte address in the range [M~M+7]. For example, if the query directory index is the root directory, then the index information field in 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.
[0086] Step 1002: The host computer queries the target index content of the slave computer by reading the second preset address space through multi-byte read, and the slave computer returns a response containing the target index content.
[0087] Specifically, the host computer obtains the target index content through a multi-byte read method [address N~N+69]. The slave computer responds to the host computer's directory query by constructing a response frame containing a data block of index content and returning it to the host computer. If there is only one file named "alarm" in the queried directory, the data category 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 payload data is "alarm" + 123 "0"s (a total of 128 bytes).
[0088] Step 1003: The host computer sends the target file index to the slave computer by writing to the first preset address space via multi-byte write, and the slave computer returns a write normal response.
[0089] Specifically, the host computer sends the target file index by writing multiple bytes to the address space [M~M+7]. For example, if the target file index is the "alarm" file in the root directory, then the index information field in the address space [M~M+7] will be filled with "00alarm000", the data offset information field will be filled with 0, and the extended information field will be filled with 0.
[0090] Step 1004: The host computer queries the target file content of the slave computer by reading the second preset address space via multi-byte read, and the slave computer returns a response containing the target file content.
[0091] Specifically, the host computer obtains the target file content using the multi-byte read method [address N~N+69]. The slave computer responds to the host computer's file query by constructing a response frame containing a block of file content data and returning it to the host computer. For example, if the total number of bytes in the "alarm" file is 64 bytes, then the data category of the constructed file content data block is 1, the total number of bytes in the target file content is 64, the current data transmission length is 64, the data offset is 0, and the current payload data is 64 bytes of the "alarm" file content plus 64 zeros.
[0092] The implementation methods for steps 1003 and 1004 are similar to those for steps 1001 and 1002. When the content of the target file exceeds the maximum load length for a single transmission, steps 1003 and 1004 can be repeated multiple times until all target file content has been transmitted; when the content of the target index exceeds the maximum load length for a single transmission, steps 1001 and 1002 can be repeated multiple times until all target index content has been transmitted.
[0093] The file transfer method based on the Modbus protocol provided in this application uses the concept of files to summarize various types of data and establishes a mapping relationship between files and file indexes. When transferring files, files and file indexes are encapsulated into data blocks using different encapsulation formats. The data blocks are transferred through multi-byte read and multi-byte write of the Modbus standard, which decouples the transmission protocol from the transmission content. This enables the Modbus protocol to work efficiently with other IoT communication protocols based on the same data across different physical layer media and MCUs.
[0094] According to an embodiment of this application, a computer-readable storage medium of the type described above is provided. The computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the processor performs the steps of the file transfer method based on the Modbus protocol described above.
[0095] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and not to limit them; under the concept of this application, the technical features of the above embodiments or different embodiments can also be combined, the steps can be implemented in any order, and there are many other variations of different aspects of this application as described above, which are not provided in detail for the sake of brevity; although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the foregoing embodiments, or make equivalent substitutions for some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of this application.
Claims
1. A file transfer method based on the Modbus protocol, characterized in that, Applied to a host computer, the method includes: The target file index is encapsulated into a file reading data block according to a preset first encapsulation format, and a multi-byte write request is initiated to the lower-level machine based on the first preset address space corresponding to the first encapsulation format and the file reading data block. After receiving a write success response from the lower-level machine, a multi-byte read request is initiated to the lower-level machine based on the second preset address space corresponding to the preset second encapsulation format. The system receives a response from the lower-level machine containing a block of file content data, and parses the block of file content data based on the second encapsulation format to obtain the target file content. The data block encapsulated using the first encapsulation format includes index information and data offset information. The data block encapsulated using the second encapsulation format includes data category, total number of data, current data transmission length, data offset information, and current load data. The data offset information included in the first encapsulation format is used to identify the read offset position based on the target file or target index. The data offset information included in the second encapsulation format is used to identify the data offset of the current load data in the target file content or target index content. The step of initiating a multi-byte write request to the lower-level machine based on the first preset address space corresponding to the first encapsulation format and the file read data block includes: filling the start address field of the data field of the multi-byte write request frame with the start address of the first preset address space, filling the register number field of the data field of the multi-byte write request frame with the size of the first preset address space, filling the byte number field of the data field of the multi-byte write request frame with the total number of bytes of the file read data block, and filling the register value field of the data field of the multi-byte write request frame with the contents of the file read data block in sequence; The step of initiating a multi-byte read request to the lower-level machine based on the second preset address space corresponding to the preset second encapsulation format includes: filling the starting address field of the data field of the multi-byte read request frame with the starting address of the second preset address space, and filling the register number field of the data field of the multi-byte read request frame with the size of the second preset address space; The response containing the file content data block is encapsulated in the format of a multi-byte read response frame. The byte number field of the data field of the multi-byte read response frame is filled with twice the size of the second preset address space, and the register value field of the data field of the multi-byte read response frame is sequentially filled with the content of the file content data block.
2. The method according to claim 1, characterized in that, The index information field includes the directory and file name.
3. The method according to claim 1, characterized in that, The data parsed from the file content data blocks based on the second encapsulation format includes the total number of data, the current data transmission length, data offset information, and the current payload data. The method further includes: Determine whether the target file has been completely transmitted based on the total number of data, the current data transmission length, and the data offset information; If not, then determine new data offset information based on the current data transmission length and the data offset information; encapsulate the target file index and the new data offset information into a new file read data block according to the first encapsulation format; initiate a new round of multi-byte write requests to the lower-level machine based on the first preset address space and the new file read data block; after receiving a write normal response from the lower-level machine, initiate a new round of multi-byte read requests to the lower-level machine based on the second preset address space; and receive a response from the lower-level machine containing a new file content data block; then parse the new file content data block 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 have been transferred.
4. The method according to any one of claims 1 to 3, characterized in that, The method further includes: The query directory index is encapsulated into a file query data block according to the first encapsulation format, and a multi-byte write request is initiated to the lower-level machine based on the first preset address space and the file query data block. After receiving a write success response from the lower-level machine, a multi-byte read request is initiated to the lower-level machine based on the second preset address space; The system receives a response from the lower-level machine containing a data block of index content, and parses the data block of index content based on the second encapsulation format to obtain the target index content.
5. A file transfer method based on the Modbus protocol, characterized in that, Applied to a lower-level machine, the method includes: After receiving a multi-byte write request initiated by the host computer based on the first preset address space and file read data block corresponding to the first preset encapsulation format, return a write normal response to the host computer. The file reading data block is parsed based on the first encapsulation format to obtain the target file index. The target file corresponding to the target file is determined based on the target file index, and the content of the target file is encapsulated into file content data blocks based on the 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, the system returns a response containing the file content data block to the host computer based on the second preset address space. The data block encapsulated using the first encapsulation format includes index information and data offset information. The data block encapsulated using the second encapsulation format includes data category, total number of data, current data transmission length, data offset information, and current load data. The data offset information included in the first encapsulation format is used to identify the read offset position based on the target file or target index. The data offset information included in the second encapsulation format is used to identify the data offset of the current load data in the target file content or target index content. The multi-byte write request initiated based on the first preset address space and file read data block corresponding to the preset first encapsulation format includes: filling the start address field of the data field of the multi-byte write request frame with the start address of the first preset address space, filling the register number field of the data field of the multi-byte write request frame with the size of the first preset address space, filling the byte number field of the data field of the multi-byte write request frame with the total number of bytes of the file read data block, and filling the register value field of the data field of the multi-byte write request frame with the contents of the file read data block in sequence; The multi-byte read request initiated based on the second preset address space corresponding to the second encapsulation format includes: filling the start address field of the data field of the multi-byte read request frame with the start address of the second preset address space, and filling the register number field of the data field of the multi-byte read request frame with the size of the second preset address space; The step of returning a response containing the file content data block to the host computer based on the second preset address space includes: filling the byte number field of the data field of the multibyte read response frame with twice the size of the second preset address space, and sequentially filling the register value field of the data field of the multibyte read response frame with the content of the file content data block.
6. The method according to claim 5, characterized in that, The method further includes: Upon receiving a multi-byte write request initiated by the host computer based on the first preset address space and file query data block, return a write normal response to the host computer. The file query data block is parsed based on the first encapsulation format to obtain the query directory index. The target index content corresponding to the query directory index is determined, and the target index content is encapsulated into an index content data block based on the second encapsulation format. Upon receiving a 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.
7. 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 to enable the at least one processor to perform the method as described in any one of claims 1 to 6.
8. A computer storage medium, characterized in that, The computer storage medium stores instructions or programs that, when executed by at least one processor, cause the at least one processor to perform the method as described in any one of claims 1 to 6.