Modbus protocol-based self-discovery method and electronic equipment

By introducing the self-discovery method into the Modbus protocol, the host automatically negotiates and assigns slave addresses, solving the problems of low manual configuration efficiency and high error rate, and achieving efficient and accurate device configuration.

CN119363511APending Publication Date: 2025-01-24ANHUI RONDS SCI & TECH INC CO
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411629790.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-14
Publication Date
2025-01-24

AI Technical Summary

Technical Problem

When deploying a large number of Modbus devices, manual reconfiguration of devices has problems of inefficiency and error-proneness.

Method used

Adopting the spontaneous discounting method based on the MODBUS protocol, the host uses the broadcast address to negotiate and read requests and write requests, automatically negotiate and distribute the address of the slave machine, and realize the automatic reading of the port register table through the expansion function code.

Benefits of technology

The automatic address negotiation and configuration of the MODBUS device has been implemented, artificial intervention is reduced, the configuration efficiency is improved, and the probability of errors is reduced.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119363511A_ABST
    Figure CN119363511A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a modbus protocol-based self-discovery method and electronic equipment, and relates to the technical field of communication. The host broadcasts an address negotiation reading request to each slave, each slave replies a first response message comprising a first function code to the host according to the address negotiation reading request, and the host performs negotiation address allocation on the slave corresponding to each first response message in a waiting time window according to each received first response message, and respectively sending an address negotiation write request comprising the second function code to each slave, rewriting the current address of each slave according to each address negotiation write request, replying a second response message comprising the second function code to the host, and entering an address negotiation silence period. The host sends the negotiation request through the expanded first function code and the second function code to realize automatic change of the address of the slave and complete negotiation between the host and the slave, so that the slave does not need to be configured manually, the error probability is reduced, and the efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of communication technology, and in particular to a self-discovery method and electronic equipment based on the Modbus protocol. Background Art

[0002] Modbus has become the industry standard for industrial communication protocols and is now a common way to connect industrial electronic devices. The Modbus register table is the most important data in the application process. It specifies and records the data, capabilities, and services that the slave can provide. The common usage method in the industry is top-down, where users or engineering maintenance personnel configure Modbus devices according to the register table document.

[0003] However, when the number and types of deployed Modbus devices increase, manually de-duplicating configuration of each Modbus device will lead to problems such as low efficiency and proneness to errors. Summary of the invention

[0004] In view of this, an object of an embodiment of the present invention is to provide a self-discovery method and electronic device based on the modbus protocol to at least partially improve the above-mentioned problem.

[0005] In order to achieve the above purpose, the technical solution adopted by the embodiment of the present invention is as follows:

[0006] In a first aspect, an embodiment of the present invention provides a self-discovery method based on the modbus protocol, the method comprising:

[0007] The host broadcasts an address negotiation read request to each slave; the address negotiation read request includes a first function code and a waiting time window;

[0008] Each of the slaves replies to the host with a first response message including a first function code according to the address negotiation read request;

[0009] The host performs negotiation address allocation for the slaves corresponding to each first response message within the waiting time window according to each received first response message, and sends a negotiated write request including a second function code address to each slave;

[0010] Each slave rewrites its own current address according to the address negotiation write request, and replies to the host with a second response message including a second function code, and then enters the address negotiation silent period; during the address negotiation silent period, the slave no longer responds to the address negotiation read request of the host.

[0011] Optionally, the host negotiates address allocation for the slaves corresponding to each of the first response messages within the waiting time window according to each of the received first response messages, and sends an address negotiation write request to each of the slaves, including:

[0012] The host determines whether at least one first response message including the first function code is received within the waiting time window;

[0013] If yes, the host negotiates address allocation for the slaves corresponding to each of the first response messages according to each of the first response messages, sets the number of response timeouts to 0, and re-broadcasts an address negotiation read request to each of the slaves; wherein the waiting time window is reduced compared to the previous waiting time window;

[0014] If not, the host adds 1 to the response timeout number, and determines whether the response timeout number is greater than the preset timeout number;

[0015] If the number of response timeouts is greater than the preset number of timeouts, the host stops broadcasting the address negotiation read request and ends the address negotiation.

[0016] Optionally, the first response message further includes a unique identifier of the slave and a current address of the slave, and the host performs negotiation address allocation for the slave corresponding to each of the first response messages according to each of the first response messages, including:

[0017] The host determines the target slaves to be assigned the negotiation addresses according to the received first response messages, and stores the unique identifiers and the current addresses corresponding to the target slaves;

[0018] The host performs negotiation address allocation on each of the target slaves to obtain a negotiation address corresponding to each of the target slaves;

[0019] The host sends an address negotiation write request to each of the target slaves in turn, and changes the current address of each of the target slaves.

[0020] Optionally, the host performs negotiation address allocation on each of the target slaves to obtain a negotiation address corresponding to each of the target slaves, including:

[0021] The host traverses each of the target slaves to determine whether the maximum value of each currently allocated negotiation address plus a preset value is greater than 247;

[0022] If not, the host adds the maximum value of the currently allocated negotiation addresses plus the preset value as the negotiation address of the target slave;

[0023] If so, the host sets the negotiation address of the target slave to 0.

[0024] Optionally, each of the slaves replies to the host with a first response message including a first function code according to the address negotiation read request, including:

[0025] When each of the slaves receives the address negotiation read request, it calculates its own reply delay time;

[0026] Each of the slaves replies to the host with a first response message including a first function code according to its own reply delay time; the first response message also includes a unique identifier of the slave and a current address of the slave.

[0027] Optionally, when each of the slaves receives the address negotiation read request, calculating its own reply delay time includes:

[0028] When each of the slaves receives the address negotiation read request, determining whether it is within the address negotiation silent period;

[0029] If not, calculate its own reply delay.

[0030] Optionally, the address negotiation read request also includes a maximum number of slaves allowed, and the calculation formula for the reply delay time is:

[0031] t=rand(time)*Hash(ID) / N*T

[0032] Wherein, N is the maximum number of slaves allowed, T is the waiting time window, ID is the unique identifier of the slave, and time is the current time.

[0033] Optionally, the method further comprises:

[0034] The host sends a register table read request including a third function code to the slave, and obtains a register version and at least one record information of the register table of the slave;

[0035] The host sends a register read request to the slave based on the register version and the required record information to obtain the required device information of the slave.

[0036] Optionally, the host sends a register table read request including a third function code to the slave, and obtains a register version of the register table of the slave and at least one record information, including:

[0037] The host sends a header read request including a third function code to the slave to obtain the register version and the number of files in the register table;

[0038] The host sends a record quantity read request including a third function code to the slave in sequence according to the number of files to obtain the number of records of each file;

[0039] The host sends a record read request including a third function code to the slave in sequence according to each of the files and the number of each record, so as to obtain each record information of the register table.

[0040] In a second aspect, an embodiment of the present invention provides a self-discovery method based on the modbus protocol, which is applied to a host of a communication system, wherein the communication system further includes at least one slave, and the method includes:

[0041] Sending a register table read request including a third function code to the slave, and obtaining a register version of the register table of the slave and at least one record information;

[0042] Based on the register version and the required record information, a register read request is sent to the slave to obtain the required device information of the slave.

[0043] Optionally, the sending a register table read request including a third function code to the slave to obtain a register version of the register table of the slave and at least one piece of record information includes:

[0044] Sending a header read request including a third function code to the slave to obtain the register version and the number of files in the register table;

[0045] According to the number of files, sequentially sending a record number reading request including a third function code to the slave to obtain the number of records of each file;

[0046] According to each of the files and the number of each record, a record reading request including a third function code is sent to the slave in sequence to obtain each record information of the register table.

[0047] In a third aspect, an embodiment of the present invention provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the method described in any one of the first aspect and / or any one of the second aspect is implemented.

[0048] The embodiment of the present invention provides a self-discovery method and electronic device based on the modbus protocol, wherein the host broadcasts an address negotiation read request to each slave, and each slave replies to the host with a first response message including a first function code according to the address negotiation read request. The host negotiates address allocation for the slaves corresponding to each first response message within a waiting time window according to each received first response message, and sends an address negotiation write request including a second function code to each slave, and each slave rewrites its current address according to each address negotiation write request, and replies to the host with a second response message including a second function code, and then enters an address negotiation silent period. The host sends a negotiation request through the extended first function code and the second function code to realize automatic address change of the slave, complete the address negotiation between the host and the slave, and can also automatically read the device information of the slaves in the register table of different versions through the extended third function code.

[0049] In order to make the above-mentioned objects, features and advantages of the present application more obvious and easy to understand, preferred embodiments are specifically cited below and described in detail with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments are briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.

[0051] Figure 1 A schematic structural block diagram of an electronic device provided by an embodiment of the present invention;

[0052] Figure 2 A schematic diagram of a structure block diagram of a host-slave communication connection provided by an embodiment of the present invention;

[0053] Figure 3 A flow chart of a self-discovery method based on the modbus protocol provided by an embodiment of the present invention;

[0054] Figure 4 A schematic diagram of a process of host address negotiation provided by an embodiment of the present invention;

[0055] Figure 5 A schematic diagram of a process flow of slave address negotiation provided by an embodiment of the present invention;

[0056] Figure 6 A schematic diagram of a process for reading register table records provided by an embodiment of the present invention;

[0057] Figure 7Another flow chart of a self-discovery method based on the modbus protocol provided by an embodiment of the present invention.

[0058] Icon: 100 - electronic device; 101 - memory; 102 - communication interface; 103 - processor; 104 - bus; 21 - host; 22 - slave. DETAILED DESCRIPTION

[0059] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Generally, the components of the embodiments of the present invention described and shown in the drawings here can be arranged and designed in various different configurations.

[0060] Therefore, the following detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the invention claimed for protection, but merely represents selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0061] It should be noted that similar reference numerals and letters represent similar items in the following drawings, so once an item is defined in one drawing, it does not need to be further defined and explained in the subsequent drawings. At the same time, in the description of the present invention, the terms "first", "second", etc. are only used to distinguish the description and cannot be understood as indicating or implying relative importance.

[0062] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.

[0063] As described in the background technology, Modbus has become the industry standard for communication protocols in the industrial field, and is now a commonly used connection method between industrial electronic devices. The Modbus register table is the most important data in the application process. It specifies and records the data, capabilities, and services that the slave can provide. The common usage method in the industry is top-down, where users or engineering maintenance personnel configure Modbus devices according to the register table document. Generally, the addresses of Modbus devices are the same when they leave the factory, which means that construction and installation personnel need to use specific tools to configure the addresses of Modbus devices so that there will be no address conflicts when they are connected to the bus.

[0064] Based on the above situation, an embodiment of the present invention provides a self-discovery method and electronic device based on the modbus protocol. The host sends a negotiation request through the extended first function code and the second function code to automatically change the address of the slave and complete the address negotiation between the host and the slave.

[0065] To implement the process steps and functions of each example of the present invention, please refer to Figure 1 , Figure 1 A schematic structural block diagram of an electronic device provided by an embodiment of the present invention. The electronic device 100 includes a memory 101 and a processor 103, and the memory 101 and the processor 103 are electrically connected to each other directly or indirectly to realize data transmission or interaction. For example, these elements can be electrically connected to each other through one or more communication buses 104 or signal lines. The memory 101 can be used to store software programs and modules, and the processor 103 executes the software programs and modules stored in the memory 101 to perform various functional applications and data processing.

[0066] The electronic device 100 may be, but is not limited to, a personal computer (PC), a server, a distributed computer, etc. It is understandable that the electronic device 100 is not limited to a physical server, but may also be a virtual machine on a physical server, a virtual machine built on a cloud platform, etc., which can provide the same functions as the server or virtual machine. The operating system of the electronic device 100 may be, but is not limited to, a Windows system, a Linux system, etc.

[0067] Among them, the memory 101 can be, but is not limited to, a random access memory (RAM), a read only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable read-only memory (EEPROM), etc.

[0068] The communication connection between the electronic device 100 and an external device is achieved through at least one communication interface 102 (which can be wired or wireless).

[0069] The processor 103 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the embodiment of the present invention can be completed by the hardware integrated logic circuit in the processor 103 or the instructions in the form of software. The processor 103 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components.

[0070] Understandably, Figure 1 The structure shown is for illustration only. The electronic device 100 may also include Figure 1 More or fewer components as shown, or with Figure 1 Different configurations are shown. Figure 1 Each component shown in the figure can be implemented by hardware, software or a combination thereof.

[0071] The following is an exemplary description of the self-discovery method based on the modbus protocol provided by the present invention. Specifically, Figure 2 A schematic diagram of a structure block diagram of a host-slave communication connection provided by an embodiment of the present invention, Figure 3 A flow chart of a self-discovery method based on the modbus protocol provided by an embodiment of the present invention is shown in FIG. Figure 2 , Figure 3 The host 21 can communicate with multiple slaves 22, and the execution body of the method can be the above Figure 1 The electronic device 100 shown in FIG. 1 includes: Figure 3 The following steps are shown:

[0072] S210: The host broadcasts an address negotiation read request to each slave; wherein the address negotiation read request includes a first function code and a waiting time window.

[0073] In a system based on the Modbus protocol, the host's address field is 0x00. The host broadcasts an address negotiation read request to read the slave's unique identifier and current address. The address of the Modbus device when it leaves the factory is often 0x01.

[0074] The first function code is an extended function code, for example, may be 0x12, and is used to identify an address negotiation read request; the waiting time window is used to receive a response from the slave within the event window.

[0075] Optionally, the structure of the request message of the address negotiation read request may be as shown in Table 1:

[0076] Table 1

[0077] Protocol Field Number of bytes Numerical range address 1 0x00 Function code 1 0x12 Waiting time window 2 0x0000~0xFFFF check 2 0x0000~0xFFFF

[0078] For example, the request message can be as shown in Table 2:

[0079] Table 2

[0080]

[0081] The address 0x00 indicates that the message is sent by the host, the function code 0x12 indicates that the message is an address negotiation read request, and the waiting time window 0x00 and 0x3C indicates waiting for the response message from the slave in 0 to 60 seconds.

[0082] S220: Each slave device replies to the host with a first response message including a first function code according to the address negotiation read request.

[0083] When the slave receives the address negotiation read request, it replies to the host with a first response message, and the first response message may include the device information of the slave itself.

[0084] Optionally, the structure of the first response message may be as shown in Table 3:

[0085] Table 3

[0086]

[0087]

[0088] For example, the first response message may be as shown in Table 4:

[0089] Table 4

[0090]

[0091] Address 0xFF indicates that the message is sent by the slave, function code 0x12 indicates that the message is a response to an address negotiation read request, unique identifier indicates the unique identifier of the slave, current address indicates the current communication address, and the current address is usually the same when leaving the factory.

[0092] S230: The host performs negotiation address allocation for the slaves corresponding to each first response message within the waiting time window according to each received first response message, and sends a negotiated write request including a second function code address to each slave.

[0093] After receiving the first response message from one or more slaves, the host reallocates the current address of the slave so that the addresses of the slaves do not conflict, and sends the allocated address to the slave via a negotiated write request including the second function code address.

[0094] Optionally, the structure of the address negotiation write request message may be as shown in Table 5:

[0095] Table 5

[0096]

[0097]

[0098] For example, the request message can be as shown in Table 6:

[0099] Table 6

[0100]

[0101] Address 0x00 indicates that the message is sent by the host, function code 0x13 indicates that the message is an address negotiation write request, and device address 0x02 indicates that the message needs to modify the device address of the slave with the unique identifier 0x31 0x32 0x30x34 to 0x02.

[0102] S240: Each slave rewrites its own current address according to each address negotiation write request, and replies to the host with a second response message including a second function code, and then enters an address negotiation silent period; wherein, during the address negotiation silent period, the slave no longer responds to the host's address negotiation read request.

[0103] Each slave modifies its current address to the device address in the corresponding request according to the received address negotiation write request, and replies to the host with a second response message, entering the address negotiation silent period, indicating that the address of the slave is successfully modified. During the address negotiation silent period, the host may send an address negotiation read request, but the slave will not respond to the address negotiation read request, so the host does not need to process it multiple times.

[0104] In this method, the host sends an address negotiation read request through an extended first function code to obtain a first response message from the slave, and sends an address negotiation write request through an extended second function code to achieve automatic address change of the slave, complete the address negotiation between the host and the slave, thereby eliminating the need to manually configure each slave, reducing the probability of error and improving efficiency.

[0105] There are many ways for the host to allocate addresses. The host can broadcast an address negotiation read request once, set a longer waiting time window, and allocate addresses after receiving replies from each slave. The host can also broadcast multiple times to allocate addresses. In a possible implementation, step S230 may include the following steps:

[0106] S231: The host determines whether at least one first response message including a first function code is received within the waiting time window. If yes, execute step S232; if not, execute step S233.

[0107] The host broadcasts an address negotiation read request and sets a waiting time window. After broadcasting, the host waits for a reply from the slave in the waiting time window, and first determines whether there is a reply to the first response message including the first function code within the waiting time window.

[0108] S232: The host negotiates address allocation for the slaves corresponding to each first response message according to each first response message, sets the response timeout number to 0, and re-broadcasts the address negotiation read request to each slave; wherein the waiting time window is reduced compared to the previous waiting time window.

[0109] If the host receives the first response message from one or more hosts, it will negotiate address allocation for these slaves. If the first response message from the slave is received during this broadcast, it means there is no timeout. Then, the response timeout count is set to 0, and the address negotiation read request is broadcast again to continue reading the device information of the slaves that have not responded.

[0110] Among them, the waiting time window is reduced compared with the previous waiting time window. For example, the response time of each slave is about 100ms. In the modbus protocol, 0 is the host address, 1-247 is the slave address, and 248-255 is the reserved address. Therefore, the maximum number of slaves is often 247. When the first broadcast address negotiation read request is made, the waiting time window can be set to 24700ms. When the second broadcast address negotiation read request is made, you can first see how many slave replies were received last time. If there are 150 slave replies, set the waiting time window to 24700-15000=9700ms. This method can be used to reduce the waiting time, and so on. In order to ensure the response of all slaves, the minimum waiting time window can be set to 1000ms, which is 10 times the response time of a single slave.

[0111] S233: The host increases the response timeout count by 1, and determines whether the response timeout count is greater than the preset timeout count.

[0112] When the host broadcasts the address negotiation read request for the first time, the response timeout number can be set to 0. If the host does not receive the first response message, the response timeout number is increased by 1. After the times are added, it is determined whether the response timeout number is greater than the preset timeout number. Optionally, the preset timeout number can be 10.

[0113] S234: If the number of response timeouts is greater than the preset number of timeouts, the host stops broadcasting the address negotiation read request and ends the address negotiation.

[0114] If the response timeout times are greater than the preset timeout times, it can be considered that all slaves have responded, and the address negotiation process ends. If the response timeout times are not greater than the preset timeout times, the address negotiation read request continues to be broadcast.

[0115] Optionally, the first response message further includes a unique identifier of the slave and a current address of the slave. The host negotiates address allocation for the slaves corresponding to each first response message according to each first response message, which may include:

[0116] S2321: The host determines the target slaves to be assigned the negotiation addresses according to the received first response messages, and stores the unique identifiers and current addresses of the target slaves.

[0117] The host receives the first response message and can determine the slave that needs to be assigned an address as the target slave, and stores the unique identifier and current address in the first response message. This storage method can select a storage medium that does not lose data when power is off.

[0118] S2322: The host performs negotiation address allocation on each target slave to obtain the negotiation address corresponding to each target slave.

[0119] Optionally, there may be too many slaves to be connected to the master, so this step may include:

[0120] The host traverses each target slave to determine whether the maximum value of each currently allocated negotiation address plus the preset value is greater than 247.

[0121] The address of each target slave can be allocated by increasing the preset value from 1. The preset value can be 1. For example, if the maximum value of the currently allocated negotiation address is 100, the negotiation address to be allocated to the next extension is 101. If the maximum value is 247, the negotiation address to be allocated to the next extension is 248.

[0122] If not, the host adds the maximum value of the currently allocated negotiation addresses plus the preset value as the negotiation address of the target slave.

[0123] As shown above, the maximum value of the previously allocated negotiation addresses plus the preset value is 101, which is less than 247, so 101 is used as the negotiation address of the target slave.

[0124] If so, the host sets the negotiation address of the target slave to 0.

[0125] As shown above, if the maximum value of the previously allocated negotiation addresses plus the preset value is 248 and is greater than 247, 0 is used as the negotiation address of the target slave.

[0126] S2323: The host sends an address negotiation write request to each target slave in turn to change the current address of each target slave.

[0127] After the negotiation addresses of the various slaves are assigned, address negotiation write requests are sent to the target slaves in turn.

[0128] In a possible implementation, the slave may delay replying according to a certain rule, and step S220 may include the following steps:

[0129] S221: When each slave receives an address negotiation read request, it calculates its own response delay time.

[0130] The address negotiation read request may also include the maximum number of slaves allowed, and the calculation formula for the reply delay time is:

[0131] t=rand(time)*Hash(ID) / N*T

[0132] Among them, N is the maximum number of slaves allowed, T is the waiting time window, ID is the unique identifier of the slave, and time is the current time. Through this formula, the reply time points of each slave can be staggered so that the host can process it.

[0133] Optionally, before calculating the own reply delay time, the following may also be included:

[0134] When each slave receives an address negotiation read request, it determines whether it is within the address negotiation silent period.

[0135] If not, calculate its own reply delay.

[0136] If yes, it means that the address of the slave has been modified successfully and no further response to the address negotiation read request is required.

[0137] S222: Each slave replies to the host with a first response message including a first function code according to its own reply delay time; wherein the first response message also includes a unique identifier of the slave and a current address of the slave.

[0138] In another possible implementation of step S232, in a broadcast address negotiation read request, negotiation address allocation may not be performed, and negotiation address allocation may be performed after all slave device information is acquired.

[0139] Now, the present invention will illustrate the method from the host side and the slave side by example. Figure 4 , Figure 4 A flowchart of host address negotiation provided in an embodiment of the present invention is provided. The host starts to enter address negotiation, first sets the response timeout number n=0, broadcasts the address negotiation read request, and determines whether there is a slave reply within the waiting time. If so, set n=0, assign the slave address, send an address negotiation write request to the slave, and continue to repeat the broadcast address negotiation read request; if not, add 1 to n, determine whether n is greater than the preset number of times, if not, repeat the broadcast address negotiation read request; if so, end the negotiation.

[0140] See also Figure 5 , Figure 5 A flow chart of host address negotiation provided for an embodiment of the present invention, wherein a slave receives a message and determines whether it is a message including a first function code and a second function code. If not, the slave processes other function code messages and continues to receive messages. If so, the slave determines whether it is a message with a first function code. If not, the slave modifies the local address according to the second function code message, and continues to receive messages after replying to a second response message. If it is a message with a first function code, the slave determines whether it is in a negotiation silence period. If not, the slave continues to receive messages. If it is in a negotiation silence period, the slave calculates a reply delay time, replies to a first response message, and continues to receive messages.

[0141] Furthermore, in order to adapt to different versions of register tables of various slaves, in a possible implementation, the method may further include:

[0142] S250: The host sends a register table read request including a third function code to the slave, and obtains a register version of the register table of the slave and at least one record information.

[0143] The structure of each record information corresponding to each register version is different. For example, the structure of the record information of register table version V1 is as shown in Table 7:

[0144] Table 7

[0145] address length type Permissions 2Byte 2Byte 1 Byte 1 Byte

[0146] The structure of the record information of register table version V2 is shown in Table 8:

[0147] Table 8

[0148] address length type Permissions Additional Types Gain value unit Name (UTF8-4) 2Byte 2Byte 1 Byte 1 Byte 2Byte 4Byte 8Byte 20Byte

[0149] Each record in a register table is the same, and the host can parse the slave register information based on different register table versions.

[0150] S260: The host sends a register read request to the slave based on the register version and the required record information to obtain the required device information of the slave.

[0151] For example, if you need to query the serial number of the slave first, you will find the record information named serial number. If the record information is at address 0x02, length 8, and named serial number, then the slave register starts at address 0x02, reads 8 registers, gets 16 bytes of information, and finally gets the serial number information.

[0152] Optionally, the step of reading the register table may include:

[0153] S251: The host sends a header read request including a third function code to the slave to obtain a register version and the number of files in the register table.

[0154] The third function code is an extended function code, which can be 0x14, and is used to read the information of the register table. For example, its message information can be the slave address 0x01, function code 0x14, file number 0x00, and record number 0x00. The message information means reading record 0 of file 0, which records the register version and the number of files.

[0155] S252: The host sends a record quantity read request including a third function code to the slave in sequence according to the number of files, and obtains the number of records of each file.

[0156] For example, if the table header reads out that there are 10 files, now we need to read the number of records contained in each file. The message information for reading the first file is slave address 0x01, function code 0x14, file number 0x00, record number 0x01, which means reading record 1 of file 0 and recording the number of records in file 1. The message information for reading the second file is slave address 0x01, function code 0x14, file number 0x00, record number 0x02, which means reading record 2 of file 0 and recording the number of records in file 2.

[0157] S253: The host sends a record read request including the third function code to the slave in sequence according to each file and each record quantity, and obtains each record information of the register table.

[0158] For example, if the number of records in file 1 is 10, the message information for reading the 0th record in file 1 is the slave address 0x01, function code 0x14, file number 0x01, and record number 0x00. This record is the record information of the register table, which is a description of the register. The data structure of this record information is different in each version of the register table.

[0159] The steps of reading the register table are actually to read its records in a loop. Please refer to Figure 6 , to explain again, first read file 0 record 0 to get the total number of files N, then read the number of records M of files 1-N in sequence, and then read each record information of files 1-N in sequence.

[0160] Furthermore, the embodiment of the present invention also provides a self-discovery method based on the modbus protocol, which is applied to a host of a communication system, wherein the communication system also includes at least one slave, see Figure 7 , the method comprising:

[0161] S310: Send a register table read request including a third function code to the slave, and obtain a register version of the register table of the slave and at least one record information.

[0162] S320: Based on the register version and the required record information, a register read request is sent to the slave to obtain the required device information of the slave.

[0163] The method can adaptively read various device information of each slave through the version information and record information of the register table, thereby eliminating the need to manually repeatedly configure the register table and configure register tables with different contents, thereby improving efficiency, reducing errors, and making the system easier to use.

[0164] Optionally, the step of reading the register table may include:

[0165] S311: Sending a header read request including a third function code to the slave device to obtain a register version and a number of files in the register table;

[0166] S312: according to the number of files, sequentially sending a record number reading request including the third function code to the slave machine to obtain the number of records of each file;

[0167] S313: According to each file and each record quantity, a record reading request including a third function code is sent to the slave in sequence to obtain each record information of the register table.

[0168] In summary, the embodiment of the present invention provides a self-discovery method and electronic device based on the modbus protocol, the host broadcasts an address negotiation read request to each slave, each slave replies to the host with a first response message including a first function code according to the address negotiation read request, the host negotiates address allocation for the slaves corresponding to each first response message within the waiting time window according to each received first response message, sends an address negotiation write request including a second function code to each slave, each slave rewrites its current address according to each address negotiation write request, and replies to the host with a second response message including a second function code, and then enters the address negotiation silent period. The host sends a negotiation request through the extended first function code and the second function code to realize the automatic change of the address of the slave, and completes the address negotiation between the host and the slave. The host can also automatically read the device information of the slave in the register table of different versions through the extended third function code, realize the automatic change of the address of the slave, and complete the address negotiation between the host and the slave, so that there is no need to manually configure each slave, reduce the error probability, and improve efficiency.

[0169] In the embodiments provided by the present invention, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely schematic. For example, the flowcharts and block diagrams in the accompanying drawings show the possible architecture, functions and operations of the devices, methods and computer program products according to multiple embodiments of the present invention. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of the code, and a module, a program segment or a part of the code contains one or more executable instructions for implementing the specified logical functions. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flowchart, and the combination of boxes in the block diagram and / or flowchart can be implemented with a dedicated hardware-based system that performs a specified function or action, or can be implemented with a combination of dedicated hardware and computer instructions.

[0170] In addition, the functional modules in the various embodiments of the present invention may be integrated together to form an independent part, or each module may exist independently, or two or more modules may be integrated to form an independent part.

[0171] If the function is implemented in the form of a software function module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a computer-readable storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods of each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.

[0172] The above are only preferred embodiments of the present invention and are not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

[0173] It will be apparent to those skilled in the art that the invention is not limited to the details of the exemplary embodiments described above and that the invention can be implemented in other specific forms without departing from the spirit or essential features of the invention. Therefore, the embodiments should be considered exemplary and non-limiting in all respects, and the scope of the invention is defined by the appended claims rather than the foregoing description, and it is intended that all variations falling within the meaning and scope of the equivalent elements of the claims be included in the invention. Any reference numeral in a claim should not be considered as limiting the claim to which it relates.

Claims

1. A self-discovery method based on the modbus protocol, characterized in that: The method comprises: The host broadcasts an address negotiation read request to each slave; the address negotiation read request includes a first function code and a waiting time window; Each of the slaves replies to the host with a first response message including a first function code according to the address negotiation read request; The host performs negotiation address allocation for the slaves corresponding to each first response message within the waiting time window according to each received first response message, and sends a negotiated write request including a second function code address to each slave; Each slave rewrites its own current address according to the address negotiation write request, and replies to the host with a second response message including a second function code, and then enters the address negotiation silent period; during the address negotiation silent period, the slave no longer responds to the address negotiation read request of the host.

2. The method according to claim 1, characterized in that: The host performs negotiation address allocation for the slaves corresponding to each first response message within the waiting time window according to each received first response message, and sends an address negotiation write request to each slave respectively, including: The host determines whether at least one first response message including the first function code is received within the waiting time window; If yes, the host negotiates address allocation for the slaves corresponding to each of the first response messages according to each of the first response messages, sets the number of response timeouts to 0, and re-broadcasts an address negotiation read request to each of the slaves; wherein the waiting time window is reduced compared to the previous waiting time window; If not, the host adds 1 to the response timeout number, and determines whether the response timeout number is greater than the preset timeout number; If the number of response timeouts is greater than the preset number of timeouts, the host stops broadcasting the address negotiation read request and ends the address negotiation.

3. The method according to claim 2, characterized in that The first response message also includes the unique identifier of the slave and the current address of the slave, and the host negotiates address allocation for the slave corresponding to each of the first response messages according to each of the first response messages, including: The host determines the target slaves to be assigned the negotiation addresses according to the received first response messages, and stores the unique identifiers and the current addresses corresponding to the target slaves; The host performs negotiation address allocation on each of the target slaves to obtain a negotiation address corresponding to each of the target slaves; The host sends an address negotiation write request to each of the target slaves in turn, and changes the current address of each of the target slaves.

4. The method according to claim 3, characterized in that The host performs negotiation address allocation on each of the target slaves to obtain a negotiation address corresponding to each of the target slaves, including: The host traverses each of the target slaves to determine whether the maximum value of each currently allocated negotiation address plus a preset value is greater than 247; If not, the host adds the maximum value of the currently allocated negotiation addresses plus the preset value as the negotiation address of the target slave; If so, the host sets the negotiation address of the target slave to 0.

5. The method according to claim 1, characterized in that Each of the slaves replies to the host with a first response message including a first function code according to the address negotiation read request, including: When each of the slaves receives the address negotiation read request, it calculates its own reply delay time; Each of the slaves replies to the host with a first response message including a first function code according to its own reply delay time; the first response message also includes a unique identifier of the slave and a current address of the slave.

6. The method according to claim 5, characterized in that When each of the slaves receives the address negotiation read request, calculating its own reply delay time includes: When each of the slaves receives the address negotiation read request, determining whether it is within the address negotiation silent period; If not, calculate its own reply delay.

7. The method according to claim 5, characterized in that The address negotiation read request also includes the maximum number of slaves allowed, and the calculation formula of the reply delay time is: t=rand(time)*Hash(ID) / N*T Wherein, N is the maximum number of slaves allowed, T is the waiting time window, ID is the unique identifier of the slave, and time is the current time.

8. The method according to claim 1, characterized in that The method further comprises: The host sends a register table read request including a third function code to the slave, and obtains a register version and at least one record information of the register table of the slave; The host sends a register read request to the slave based on the register version and the required record information to obtain the required device information of the slave.

9. The method according to claim 8, characterized in that The host sends a register table read request including a third function code to the slave, and obtains a register version of the register table of the slave and at least one record information, including: The host sends a header read request including a third function code to the slave to obtain the register version and the number of files in the register table; The host sends a record quantity read request including a third function code to the slave in sequence according to the number of files to obtain the number of records of each file; The host sends a record read request including a third function code to the slave in sequence according to each of the files and the number of each record, so as to obtain each record information of the register table.

10. A self-discovery method based on modbus protocol, characterized in that: A host applied to a communication system, wherein the communication system further comprises at least one slave, wherein the method comprises: Sending a register table read request including a third function code to the slave, and obtaining a register version of the register table of the slave and at least one record information; Based on the register version and the required record information, a register read request is sent to the slave to obtain the required device information of the slave.

11. The method according to claim 10, characterized in that The sending of a register table read request including a third function code to the slave to obtain a register version of the register table of the slave and at least one piece of record information includes: Sending a header read request including a third function code to the slave to obtain the register version and the number of files in the register table; According to the number of files, sequentially sending a record number reading request including a third function code to the slave to obtain the number of records of each file; According to each of the files and the number of each record, a record reading request including a third function code is sent to the slave in sequence to obtain each record information of the register table.

12. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the method according to any one of claims 1 to 9 and / or any one of claims 10 or 11 is implemented.