Address allocation method, master-slave communication system, and computer-readable storage medium

The slave is guided to automatically compare virtual addresses and generate real addresses through host instructions, which solves the problem of manually allocating slave addresses and making errors prone to errors, and improves address allocation efficiency.

CN120263771BActive Publication Date: 2025-08-22DELIXI GROUP INSTRUMENT CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202510749413.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-06-06
Publication Date
2025-08-22
Estimated Expiration
2045-06-06

AI Technical Summary

Technical Problem

In the prior art, manual allocation of slave addresses is complicated and error-prone, especially when the number of slaves is large, address conflicts are prone to occur.

Method used

The host broadcasts command information, instructs the slave machine with unassigned addresses on the slave side to enter the address allocation mode, compares the virtual address sizes between slaves, automatically determines the maximum virtual address and generates a unique real address. The host is only responsible for broadcasting instructions and does not participate in the address allocation logic processing.

Benefits of technology

Automatic allocation of slave addresses is realized, avoiding the cumbersomeness of manual operations and error rates, improving address allocation efficiency, and reducing frequent handshakes and data exchanges between the host and slave.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120263771B_ABST
    Figure CN120263771B_ABST
Patent Text Reader

Abstract

The present application provides an address allocation method, a master-slave communication system, and a computer-readable storage medium, relating to the field of communication technology. The address allocation method includes: upon receiving a first instruction message broadcast by a host, each first target slave among multiple slaves that has not been assigned an address enters an address allocation mode and sends a first response message to the host, the first response message being used to instruct the host to broadcast a second instruction message; based on the received second instruction message broadcast by the host, the first target slave determines whether its own virtual address is the maximum virtual address among the virtual addresses of all the slaves among the multiple slaves that have not been assigned an address; if the virtual address of the first target slave is the maximum virtual address, the first target slave generates a unique target real address and sets its own real address as the target real address. This method achieves automatic allocation of slave addresses, avoiding the cumbersome and error-prone manual allocation of slave addresses.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of communication technology, and in particular to an address allocation method, a master-slave communication system, and a computer-readable storage medium. Background Art

[0002] Bus communication networks (such as RS485) are common in smart grid systems and smart home systems. These networks typically use a master-slave communication method. By assigning addresses to each slave, the host can communicate with the slaves based on their addresses. To ensure the host can accurately identify each slave, each slave must be assigned a unique address.

[0003] In the related art, the slave addresses are allocated by manually setting the addresses. This method is easily affected by human factors. Especially when there are a large number of slaves, manual allocation is not only cumbersome but also prone to errors. Summary of the Invention

[0004] The present application provides an address allocation method, a master-slave communication system and a computer-readable storage medium to solve the problem that manual allocation of slave addresses is tedious and error-prone, and realizes automatic allocation of slave addresses.

[0005] In a first aspect, the present application provides an address allocation method, which is applied to a slave in a master-slave communication system, wherein the master-slave communication system includes a master and a plurality of slaves communicatively connected to the master; the address allocation method includes:

[0006] Upon receiving the first instruction information broadcast by the host, the first target slave enters an address allocation mode and sends a first response information to the host; wherein the first response information is used to instruct the host to broadcast the second instruction information; the first target slave is each slave in the plurality of slaves to which an address is not allocated;

[0007] The first target slave determines, based on the received second instruction information broadcast by the host, whether its own virtual address is the largest virtual address among the virtual addresses of all slaves to which addresses are not allocated in the plurality of slaves;

[0008] In a case where the virtual address of the first target slave is the maximum virtual address, the first target slave generates a unique target real address and sets its own real address as the target real address.

[0009] Optionally, the first target slave determines, based on the received second instruction information broadcast by the host, whether its own virtual address is the largest virtual address among the virtual addresses of all slaves to which addresses are not assigned in the multiple slaves, including:

[0010] Each time the first target slave receives the second instruction information broadcast by the host, the first target slave increases the synchronization count corresponding to the first target slave by 1; the synchronization count is 0 in an initialized state;

[0011] When the first target slave is in the address allocation mode, determining a target bit of the virtual address of the first target slave based on the number of synchronizations and the number of bits of the virtual address of the first target slave;

[0012] When the target bit of the virtual address of the first target slave is 1, the first target slave broadcasts third instruction information; wherein the third instruction information is used to instruct a sub-target slave in the second target slave to exit the address allocation mode; the second target slave is a slave other than the first target slave in the address allocation mode, and the sub-target slave is a slave for which the target bit of the virtual address is 0;

[0013] Repeat the above steps until the first target slave machine is still in the address allocation mode after traversing all bits from the highest bit to the lowest bit of its own virtual address, and then determine that its own virtual address is the maximum virtual address among the virtual addresses of all slave machines with unassigned addresses among the multiple slave machines.

[0014] Optionally, determining the target bit position of the virtual address of the first target slave machine based on the number of synchronization times and the number of bits of the virtual address of the first target slave machine includes:

[0015] Subtract 1 from the synchronization number to obtain a first difference;

[0016] Taking the modulus of the first difference value and the number of bits of the virtual address of the first target slave machine to obtain a modulo result;

[0017] Subtract 1 from the number of bits of the virtual address of the first target slave machine to obtain a second difference;

[0018] A third difference between the second difference and the modulo result is determined, and the third difference is determined as the target bit of the virtual address of the first target slave.

[0019] Optionally, the address allocation method further includes:

[0020] The first target slave exits the address allocation mode when the target bit of the virtual address of the first target slave is 0 and the first target slave receives the third instruction information broadcasted by the second target slave.

[0021] Optionally, the first target slave generates a unique target real address, including:

[0022] The first target slave obtains a current value of the synchronization number;

[0023] The first target slave machine rounds the current value to the number of bits of the virtual address of the first target slave machine, and determines the rounding result as the unique target real address of the first target slave machine.

[0024] Optionally, before the first target slave receives the first instruction information broadcast by the master, the address allocation method further includes:

[0025] Upon receiving the fourth instruction information broadcast by the host, the multiple slaves initialize their respective real addresses to 0 and randomly generate their respective corresponding virtual addresses;

[0026] The virtual address is not 0, and the number of bits of each virtual address is the same.

[0027] In a second aspect, the present application provides an address allocation method, which is applied to a host in a master-slave communication system, wherein the master-slave communication system includes a host and multiple slaves communicatively connected to the host; the address allocation method includes:

[0028] Broadcasting a first instruction message to the plurality of slaves; the first instruction message is used to instruct a first target slave among the plurality of slaves to enter an address allocation mode, and to instruct the first target slave to send a first response message to the host; wherein the first target slave is each slave among the plurality of slaves to which an address is not allocated;

[0029] Upon receiving the first response information, a second instruction information is broadcast; the second instruction information is used to instruct the first target slave machine to determine whether its own virtual address is the maximum virtual address among the virtual addresses of all slaves with unassigned addresses among the multiple slaves based on the second instruction information, generate a unique target real address when the virtual address of the first target slave machine is the maximum virtual address, and set the real address of the first target slave machine to the target real address.

[0030] Optionally, the address allocation method further includes:

[0031] Broadcasting fourth instruction information; the fourth instruction information is used to instruct the multiple slaves to initialize their respective real addresses to 0 and randomly generate their respective corresponding virtual addresses;

[0032] The virtual address is not 0, and the number of bits of each virtual address is the same.

[0033] In a third aspect, the present application provides a master-slave communication system, comprising a master and a plurality of slaves communicatively connected to the master;

[0034] The slave device is configured to execute the steps of the address allocation method according to any one of the first aspects above;

[0035] The host is used to execute the steps of the address allocation method as described in any one of the second aspects above.

[0036] In a fourth aspect, the present application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the address allocation method as described in any one of the first aspects above, or implements the steps of the address allocation method as described in any one of the second aspects above.

[0037] The address allocation method, master-slave communication system, and computer-readable storage medium provided by the present application are as follows: for each first target slave in a plurality of slaves in the master-slave communication system that has not been assigned an address, upon receiving a first instruction message broadcast by a host, the first target slave enters an address allocation mode and sends a first response message to the host, instructing the host to broadcast a second instruction message through the first response message; based on the second instruction message received from the host, the first target slave determines whether its own virtual address is the maximum virtual address among the virtual addresses of all the slaves in the plurality of slaves that have not been assigned an address; if so, the first target slave generates a unique target real address and sets its own real address to the target real address, thereby achieving automatic allocation of slave addresses and avoiding the cumbersome and error-prone operation of manual slave address allocation. Moreover, the host is only responsible for broadcasting the instruction message and does not participate in the logical processing of address allocation. The logical processing of address allocation is mainly completed on the slave side, reducing the frequent handshakes and data exchanges between the host and the slave, and improving the efficiency of address allocation. BRIEF DESCRIPTION OF THE DRAWINGS

[0038] Figure 1 One of the flowcharts of the address allocation method provided in the embodiment of the present application;

[0039] Figure 2 The second flowchart of the address allocation method provided in the embodiment of the present application;

[0040] Figure 3 The third flowchart of the address allocation method provided in the embodiment of the present application;

[0041] Figure 4 This is a logical diagram of comparing the virtual addresses of four slaves in an embodiment of the present application;

[0042] Figure 5 A schematic diagram of the structure of the master-slave communication system provided in an embodiment of the present application. DETAILED DESCRIPTION

[0043] In this application, "at least one" means one or more, and "more" means two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships can exist. For example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. Among them, A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following items" or similar expressions refers to any combination of these items, including any combination of single items or plural items. For example, at least one of a alone, b alone, or c alone can mean: a alone, b alone, c alone, a and b combined, a and c combined, b and c combined, or a, b, and c combined. Among them, a, b, and c can be single or plural. In addition, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance.

[0044] Bus communication networks (such as RS485) are common in smart grid systems and smart home systems. These networks typically use a master-slave communication method. By assigning addresses to each slave, the host can communicate with the slaves based on their addresses. To ensure the host can accurately identify each slave, each slave must be assigned a unique address.

[0045] In the related art, slave addresses can be assigned by manually setting addresses. In this method, the slave addresses are preset by the manufacturer during production, which is less flexible and easily affected by human factors. Especially when there are a large number of slaves, manual assignment is not only cumbersome but also prone to errors and a high possibility of address conflicts.

[0046] In order to solve the problem of manual allocation of slave addresses being cumbersome and prone to errors, a method of automatically allocating slave addresses can be used in the related art, whereby the host automatically allocates addresses to the slaves. For example, the host can issue instructions to the slaves according to an allocation mechanism. After the slaves report relevant data according to the instructions, the host performs logical processing on the data to generate corresponding slave addresses. Then, according to the allocation mechanism, the corresponding slave addresses are allocated to each slave in sequence. Although this method realizes the automatic allocation of slave addresses, the allocation of slave addresses depends on the participation of the host. The logical processing of address allocation is completed by the host, and frequent handshakes and data exchanges between the host and slaves are required, which is time-consuming and inefficient. Moreover, during the address allocation process, communication conflicts between slaves need to be avoided, that is, only one slave can respond at a time.

[0047] In view of this, an embodiment of the present application provides an address allocation method, in which the host broadcasts a first instruction message to instruct each first target slave on the slave side that has not been assigned an address to enter an address allocation mode. These first target slaves compare the sizes of each other's virtual addresses through active communication between the slaves. In one round of comparison, the slave with the largest virtual address is determined. This slave can assign an address to itself and no longer participates in the virtual address comparison of the slaves that have not been assigned an address in the next round. This process is repeated until all slaves have assigned a unique address to themselves. In this way, the automatic allocation of slave addresses can be completed on the slave side, avoiding the problem of cumbersome and error-prone operations when manually allocating slave addresses. Moreover, the host is only responsible for broadcasting instruction information and does not participate in the logical processing of address allocation. The logical processing of address allocation is mainly completed on the slave side, reducing the frequent handshakes and data exchanges between the host and the slave, and improving the efficiency of address allocation.

[0048] The following combination Figures 1 to 4 The address allocation method provided in the embodiment of the present application is described in detail.

[0049] Figure 1 One of the flow charts of the address allocation method provided in the embodiment of the present application is shown. The address allocation method can be applied to a slave in a master-slave communication system, which includes a master and multiple slaves connected to the master. Figure 1 As shown, the address allocation method may include the following steps 110 to 130.

[0050] Step 110: Upon receiving the first instruction information broadcast by the host, the first target slave enters the address allocation mode and sends a first response information to the host.

[0051] Among them, the first instruction information is used to instruct the slave machine without an assigned address to enter the address allocation mode; the first response information is used to instruct the host to broadcast the second instruction information, and the second instruction information can be used to instruct the first target slave machine in the address allocation mode to judge the virtual address size; the first target slave machine is each slave machine without an assigned address among the multiple slave machines.

[0052] Specifically, a master and slave in a master-slave communication system may be connected via a communication bus. The master may broadcast first instruction information to each slave via the communication bus. Upon receiving the first instruction information broadcast by the master, a first target slave on the slave side that has not yet been assigned an address enters an address allocation mode and simultaneously sends a first response information to the master. Upon receiving the first response information, the master may determine that there are still slaves on the slave side that have not yet been assigned an address. In this case, the master may broadcast second instruction information to the slave via the communication bus.

[0053] For example, for each slave, when its real address ADD=0, it can be determined that the slave is a slave with no assigned address.

[0054] For example, the first response information may be, for example, "0xFF", but is not limited thereto.

[0055] For example, before step 110, multiple slaves in the master-slave communication system that are communicatively connected to the master can be initialized. Specifically, before the first target slave receives the first instruction information broadcast by the master, the address allocation method can further include: upon receiving the fourth instruction information broadcast by the master, the multiple slaves initialize their respective real addresses to 0 and randomly generate corresponding virtual addresses. The virtual addresses are not 0, and the number of bits of each virtual address is the same.

[0056] For example, a master-slave communication system includes a host and slaves 1, slave 2, slave 3 and slave 4 connected to the host via a communication bus. The host can broadcast fourth instruction information to each slave via the communication bus. After receiving the fourth instruction information, each slave initializes its current real address ADD to 0. At the same time, it randomly generates its corresponding non-zero virtual address, such as RADD1, RADD2, RADD3 and RADD4, respectively. Among them, RADD1, RADD2, RADD3 and RADD4 are different, but the number of bits is the same, for example, they are all 11 bits in length.

[0057] Step 120: The first target slave determines, based on the received second instruction information broadcasted by the host, whether its own virtual address is the largest virtual address among the virtual addresses of all the slaves to which addresses are not allocated among the multiple slaves.

[0058] The second instruction information may be used to instruct the first target slaves in the address allocation mode to determine the virtual address size.

[0059] In an optional embodiment, after receiving the first response information sent by the first target slave, the host determines that there are still unassigned slave addresses among the multiple slaves in communication with it. In this case, second instruction information can be broadcast to the slave side via the communication bus. For each first target slave with an unassigned address on the slave side, after receiving the second instruction information, the first target slave compares the size of its virtual address with that of other first target slaves to determine whether its virtual address is the largest virtual address among the virtual addresses of all the first target slaves with unassigned addresses.

[0060] In another optional embodiment, after receiving a first response message from a first target slave, the host determines that there are still unassigned addresses among the multiple slaves connected to it. In this case, a preset number of second instruction messages can be periodically broadcast to the slave side via the communication bus, where the preset number is equal to the number of bits in the slave's virtual address. The first target slave in address allocation mode can compare the virtual address from the highest bit to the lowest bit based on the second instruction message. Each time the host broadcasts the second instruction message, each first target slave in address allocation mode compares the same bit. A bit of 1 indicates a larger virtual address, and a bit of 0 indicates a smaller virtual address. The first target slave with a larger virtual address controls the first target slave with a smaller virtual address to exit the address allocation operation. When all bits are compared, there will be a first target slave still in address allocation mode. This first target slave is the slave with the largest virtual address. This completes the screening of the slave with the largest virtual address. The address of this slave can be assigned and it will no longer participate in the next round of address allocation as the first target slave. This is repeated until all slaves are assigned unique addresses.

[0061] Specifically, step 120 may include the following steps 121 to 124.

[0062] Step 121 : Each time the first target slave receives the second instruction information broadcast by the host, the first target slave increases the value of the synchronization number corresponding to the first target slave by 1.

[0063] The synchronization count is initialized to 0. For example, when initializing the real address of a slave in a master-slave communication system, the synchronization count CNT can also be initialized to 0. This synchronization count can record the number of times a first target slave, which has not been assigned an address, receives the second instruction information broadcast by the master from the time it first receives it until it is assigned an address. During bit comparison, the comparison bits of each first target slave can be synchronized to ensure that the same bit is compared.

[0064] Each time the host broadcasts the second instruction information, the first target slave increases the value of the corresponding synchronization count CNT by 1 after receiving the second instruction information.

[0065] Step 122 : When the first target slave is in the address allocation mode, the first target slave determines the target bit of the virtual address of the first target slave based on the number of synchronizations and the number of bits of the virtual address of the first target slave.

[0066] The synchronization count CNT can be used to synchronize the bits to be compared when comparing the virtual addresses of each first target slave. For the first target slave in the address allocation mode, after adding 1 to the corresponding synchronization count, the target bit to be compared can be located using the synchronization count after adding 1 and the number of bits in the virtual address of the first target slave.

[0067] For example, determining the target bit position of the virtual address of the first target slave machine based on the number of synchronization times and the number of bits of the virtual address of the first target slave machine may include: subtracting 1 from the number of synchronization times to obtain a first difference; taking the modulus of the first difference by the number of bits of the virtual address of the first target slave machine to obtain a modulo result; subtracting 1 from the number of bits of the virtual address of the first target slave machine to obtain a second difference; determining a third difference between the second difference and the modulo result, and determining the third difference as the target bit position of the virtual address of the first target slave machine.

[0068] Specifically, for the first target slave in the address allocation mode, the target bit of the virtual address can be determined using the following formula (1):

[0069] (1)

[0070] Among them, BT represents the target bit, M represents the number of bits of the virtual address, CNT represents the number of synchronizations, and the symbol “%” represents the remainder operation.

[0071] Step 123 : When the target bit of the virtual address of the first target slave is 1, the first target slave broadcasts the third instruction information.

[0072] Among them, the third instruction information is used to instruct the sub-target slave in the second target slave to exit the address allocation mode; the second target slave is the remaining slave in the address allocation mode except the first target slave, and the sub-target slave is the slave whose target bit of the virtual address is 0.

[0073] After determining the target bit of its virtual address to be compared, the first target slave can determine whether the target bit is 1 or 0. If it is 1, it is determined that its own virtual address may be a larger virtual address. At this time, the third instruction information can be broadcast through the communication bus to notify the other slaves currently in the address allocation mode, that is, the second target slave. For the second target slave, if the target bit of its virtual address is 0, upon receiving the third instruction information broadcast by the first target slave, it exits the address allocation mode and re-enters the address allocation mode when the next round of address allocation is started, that is, upon receiving the first instruction information broadcast by the host again, it enters the address allocation mode.

[0074] It is understandable that the first target slave may also become the second target slave of other first target slaves. When the target bit of its virtual address is 0, it may not respond. At this time, if it receives the third instruction information broadcast by other slaves, it can determine that it is not the slave with the largest virtual address and directly exit the address allocation mode.

[0075] Specifically, after executing step 122 , the address allocation method may further include: when the target bit of the virtual address of the first target slave is 0 and the first target slave receives the third instruction information broadcasted by the second target slave, exiting the address allocation mode.

[0076] Step 124: Repeat the above steps until the first target slave is still in the address allocation mode after traversing all bits from the highest bit to the lowest bit of its own virtual address, and then determine that its own virtual address is the maximum virtual address among the virtual addresses of all slaves with unassigned addresses in multiple slaves.

[0077] After the first target slave broadcasts the third instruction information and uses the third instruction information to instruct the second target slave whose target bit of the virtual address is 0 to exit the address allocation mode, if all bits of the virtual address of the first target slave have not been compared, the above steps 121 to 123 are repeated until the first target slave traverses all bits from the highest bit to the lowest bit of its own virtual address and is still in the address allocation mode. At this time, it can be considered that the virtual address of the first target slave is the maximum virtual address among the virtual addresses of all slaves without assigned addresses, and the first target slave is the only slave screened out from all slaves without assigned addresses, and the first target slave can be assigned an address.

[0078] Among them, the second instruction information can be used to control the bits of the virtual address to traverse from the highest bit to the lowest bit in sequence. During a round of maximum virtual address judgment, a preset number of second instruction information can be broadcast. The preset number is the same as the number of bits of the virtual address. Each time the second instruction information is broadcast, the bit of the virtual address can be shifted right by one bit.

[0079] For example, the third instruction information may be “0xFF”, but is not limited thereto.

[0080] According to the embodiment corresponding to the above steps 121 to 124, for example, taking the communication connection between the host and four slaves as an example, assuming that there are currently three slaves that have not yet been assigned addresses, such as slave A, slave B, and slave D, after the host broadcasts the second instruction information through the communication bus, slave A, slave B, and slave D will increase the value of their respective synchronization times CNT by 1. Assuming that slaves A, slave B, and slave D are all in the address allocation mode at this time, according to their respective synchronization times CNT, the target bit to be compared by each slave can be determined using the above formula (1). Assuming that the binary value of the target bit of slave A and slave D is 1, and the binary value of the target bit of slave B is 0, then slaves A and slave D can broadcast the third instruction information through the communication bus, and slave B does not respond. At this time, slave B will receive the third instruction information broadcast by slaves A and slave D, and then exit the address allocation mode. If the bits of the virtual address have not been compared at this time, the host will continue to broadcast the second instruction information, and slaves A, B, and D will continue to increase the value of their respective synchronization counts CNT by 1. At this time, only slaves A and D are still in the address allocation mode, and the above formula (1) can be used to determine the target bits to be compared between slaves A and D. Assuming that the binary value of the target bit of slave A is 1 and the binary value of the target bit of slave D is 0, slave A can broadcast the third instruction information through the communication bus, and slave D will not respond. At this time, slave D will receive the third instruction information broadcast by slave A and exit the address allocation mode. At this time, only slave A remains in the address allocation mode. If the bits of the virtual address have been compared at this time, slave A is the slave with the largest virtual address selected; if the bits of the virtual address have not been compared at this time, the host will continue to broadcast the second instruction information, and slaves A, B, and D will continue to increase the value of their respective synchronization counts CNT by 1. This cycle continues until all bits of the virtual address are compared. Slave A will remain in the address allocation mode, and then slave A will be the slave with the largest virtual address selected.

[0081] Step 130 : When the virtual address of the first target slave is the maximum virtual address, the first target slave generates a unique target real address and sets its own real address as the target real address.

[0082] If the virtual address of the first target slave is the maximum virtual address, the first target slave can be allocated an address. At this time, the first target slave generates a unique target real address and sets its own real address as the target real address to complete the allocation of its own address.

[0083] In an optional embodiment, the first target slave may generate its own unique target real address based on the current synchronization count CNT. For example, the first target slave generating the unique target real address may include: the first target slave obtaining a current value of the synchronization count; the first target slave rounding the current value to the number of bits of the first target slave's virtual address, and determining the rounded result as its own unique target real address.

[0084] Specifically, the first target slave can generate the target real address using the following formula (2):

[0085] (2)

[0086] Among them, ADD represents the real address, M represents the number of bits of the virtual address, CNT represents the number of synchronizations, and the symbol “ / ” represents a rounding operation.

[0087] The address allocation method provided by the embodiment of the present application is as follows: for each first target slave in a plurality of slaves in a master-slave communication system that has not been assigned an address, upon receiving a first instruction message broadcast by a host, the method enters an address allocation mode and sends a first response message to the host, instructing the host to broadcast a second instruction message through the first response message; based on the second instruction message broadcast by the host, the first target slave determines whether its own virtual address is the maximum virtual address among the virtual addresses of all the slaves in the plurality of slaves that have not been assigned an address; if so, the first target slave generates a unique target real address and sets its own real address to the target real address, thereby achieving automatic allocation of slave addresses and avoiding the cumbersome and error-prone operation when manually allocating slave addresses. Moreover, the host is only responsible for broadcasting instruction information and does not participate in the logical processing of address allocation. The logical processing of address allocation is mainly completed on the slave side, reducing the frequent handshakes and data exchanges between the host and the slave, and improving the efficiency of address allocation.

[0088] Figure 2 The second flow chart of the address allocation method provided by the embodiment of the present application is shown. The address allocation method can be applied to the host in a master-slave communication system, which includes a host and multiple slaves connected to the host. Figure 2 As shown, the address allocation method may include the following steps 210 to 220.

[0089] Step 210: Broadcast first instruction information to multiple slaves.

[0090] The first instruction information is used to instruct a first target slave among the multiple slaves to enter an address allocation mode and to instruct the first target slave to send a first response information to the host; the first target slave is each slave among the multiple slaves to which an address is not allocated.

[0091] The host may broadcast first instruction information to multiple slaves in the master-slave communication system via the communication bus to determine whether there are any slaves to which addresses have not been assigned among the multiple slaves on the slave side.

[0092] Specifically, the host can broadcast a first instruction message to multiple slaves in the master-slave communication system through the communication bus. For the first target slave to which an address is not assigned among the multiple slaves, after receiving the first instruction message, it enters the address allocation mode and sends a first response message to the host, using the first response message to notify the host that there are still slaves to which an address is not assigned.

[0093] For example, for each slave, when its real address ADD=0, it can be determined that the slave is a slave with no assigned address.

[0094] For example, the first response information may be "0xFF", which is not limited in the embodiments of the present application.

[0095] For example, before assigning addresses to the slaves, a pre-address assignment initialization operation may be performed on each slave on the slave side. Specifically, the address assignment method may further include: the master broadcasting a fourth instruction message. The fourth instruction message is used to instruct the multiple slaves to initialize their respective real addresses to 0 and randomly generate corresponding virtual addresses; wherein the virtual addresses are not 0 and each virtual address has the same number of bits.

[0096] Step 220: When the first response information is received, broadcast the second instruction information.

[0097] Among them, the second instruction information is used to instruct the first target slave to determine whether its own virtual address is the maximum virtual address among the virtual addresses of all slaves without assigned addresses among multiple slaves based on the second instruction information, generate a unique target real address when the virtual address of the first target slave is the maximum virtual address, and set the real address of the first target slave as the target real address.

[0098] After receiving the first response information, the host can confirm that there are still slaves with unassigned addresses on the slave side. At this time, a new round of address allocation on the slave side is started, and the second instruction information can be broadcast through the communication bus.

[0099] Specifically, the host can periodically broadcast a preset number of second instruction information, where the preset number is equal to the number of bits of the virtual address of the slave. The first target slave in the address allocation mode can compare the virtual address from the highest bit to the lowest bit according to the second instruction information. Each time the host broadcasts the second instruction information, each first target slave in the address allocation mode compares the same bit. A bit of 1 indicates a large virtual address, and a bit of 0 indicates a small virtual address. The first target slave with a large virtual address controls the first target slave with a small virtual address to exit the address allocation operation until all bits are compared. There will be a first target slave still in the address allocation mode. This first target slave is the slave with the largest virtual address. In this way, the screening of a slave with the largest virtual address is completed, and the address of this slave can be allocated, and it will no longer participate in the next round of address allocation as the first target slave. Repeat this process until all slaves are assigned unique addresses.

[0100] The address allocation method provided in the embodiment of the present application is that the host starts a new round of address allocation on the slave side by broadcasting a first instruction message to the slave, instructs the first target slave with an unassigned address among multiple slaves to enter the address allocation mode, and instructs the first target slave to send a first response message to the host, and determines whether there are still slaves with unassigned addresses on the slave side through the first response message; when the host receives the first response message, it confirms that there are still slaves with unassigned addresses on the slave side, and then continues to broadcast a second instruction message, instructing the first target slave to determine whether its own virtual address is the largest virtual address among the virtual addresses of all slaves with unassigned addresses among multiple slaves based on the second instruction message, generates a unique target real address when the virtual address of the first target slave is the largest virtual address, and sets the real address of the first target slave to the target real address, thereby realizing automatic allocation of slave addresses and avoiding the problem of cumbersome and error-prone operation when manually allocating slave addresses. The host is only responsible for broadcasting instruction information to instruct the address allocation of the slave and does not participate in the logical processing of address allocation. The logical processing of address allocation is mainly completed on the slave side, reducing the frequent handshakes and data exchanges between the host and slave and improving the efficiency of address allocation.

[0101] based on Figure 1 and Figure 2 The address allocation method of the corresponding embodiment, Figure 3 The third flow chart of the address allocation method provided in the embodiment of the present application is shown. The address allocation method can be applied to a master-slave communication system, which includes a master and multiple slaves connected to the master. Figure 3 As shown, the address allocation method may include the following steps 301 to 314.

[0102] Step 301: The host broadcasts the fourth instruction information.

[0103] The host may broadcast fourth instruction information to the slaves in the master-slave communication system via the communication bus to instruct the slaves to perform an initialization operation.

[0104] Step 302: The slave initializes the real address and synchronization times, and generates the corresponding virtual addresses.

[0105] Upon receiving the fourth instruction information broadcast by the master, all slaves initialize their respective real addresses ADD to 0 and randomly generate a corresponding set of virtual addresses that are not 0. At the same time, their corresponding synchronization times CNT may be initialized to 0.

[0106] Step 303: The host broadcasts the first instruction information.

[0107] The host may broadcast first instruction information to the slave side via the communication bus to confirm whether there is a slave on the slave side with an unassigned address, and instruct the slave on the slave side with an unassigned address to enter an address assignment mode.

[0108] Step 304: The first target slave device that has not been assigned an address enters an address assignment mode and sends a first response message to the host.

[0109] On the slave side, after receiving the first instruction information, the first target slave device that has not been assigned an address (i.e., the slave device whose real address ADD is 0) enters the address assignment mode and sends a first response information to the master. The first response information may be, for example, "0xFF", but is not limited thereto.

[0110] Step 305: The host determines whether the first response message has been received. If not, it indicates that all slaves on the slave side have been assigned addresses, and step 306 is executed. Otherwise, it indicates that there are still slaves on the slave side that have not been assigned addresses, and step 307 is executed.

[0111] Step 306: Exit the address allocation operation.

[0112] Step 307: The host broadcasts the second command information.

[0113] Step 308: The first target slave to which an address is not allocated increases the value of the corresponding synchronization count by 1.

[0114] After receiving the second instruction information, the first target slave machine to which an address is not allocated increases the value of the corresponding synchronization count CNT by 1.

[0115] Step 309 : The first target slave in the address allocation mode determines whether the target bit of its own virtual address is 1.

[0116] The first target slave in the address allocation mode can determine the target bit of its own virtual address based on its corresponding synchronization count CNT and the number of bits of the virtual address. Specifically, the method for determining the target bit can be found in the description of step 122 above and will not be repeated here.

[0117] After determining the target bit, the binary value of the target bit is obtained to determine whether the binary value is 1. If it is 1, step 310 is executed; if it is not 1, that is, the binary value is 0, step 311 is executed.

[0118] Step 310: The first target slave in the address allocation mode broadcasts the third instruction information. After that, the process may proceed to step 313.

[0119] If the first target slave in the address allocation mode determines that the target bit is 1, it broadcasts the third instruction information via the communication bus. The third instruction information may be, for example, "0xFF", but is not limited thereto.

[0120] Step 311: The first target slave in the address allocation mode determines whether the third instruction information broadcasted by other slaves is received.

[0121] If received, it indicates that the first target slave is not the slave with the largest virtual address among the slaves in the address allocation mode, and step 312 is executed; if not received, it indicates that the target bit of the virtual address of other slaves is also 0, and step 313 is executed.

[0122] Step 312: The first target slave in the address allocation mode exits the address allocation mode.

[0123] Step 313: Check whether the bits of the virtual address of the first target slave in the address allocation mode have been traversed and compared. If yes, go to step 314; otherwise, go to step 307.

[0124] Step 314: The first target slave currently in the address allocation mode generates a unique target real address and sets its own real address as the target real address.

[0125] If the bits of the virtual address of the first target slave in the address allocation mode are compared, and only one first target address is in the address allocation mode, then the first target slave currently in the address allocation mode can be assigned an address. The first target slave can use the above formula (2) to generate a unique target real address and set its own real address ADD to the target real address. Then, the process continues with step 303.

[0126] according to Figure 3In the address allocation method of the corresponding embodiment, a master-slave communication system includes a master and four slaves connected to the master, the number of bits of the virtual address is 11, and the third instruction information is 0xFF. Assume that the virtual address of slave 1 is "11110101010", the virtual address of slave 2 is "10110111111", the virtual address of slave 3 is "11110101101", and the virtual address of slave 4 is "11010111111". Figure 4 A schematic diagram showing the comparison of the virtual addresses of the four slaves is shown. Figure 4 As shown, each slave can compare the synchronization bits based on the second instruction message broadcast by the master, comparing the virtual address sizes starting from the highest bit. Slaves whose target bit value is 1 will transmit 0xFF, while slaves whose target bit value is 0 will only receive 0xFF. After receiving 11 second instruction messages from the master, only slave 3 remains in address allocation mode. After the master broadcasts the first second instruction message, it compares the highest bit. Since the highest bit of the virtual addresses of all slaves is 1, all slaves respond, and no slave exits address allocation mode. Similarly, after the master broadcasts the fifth second instruction message, it compares the sixth bit of the virtual address. In this case, the sixth bit of the virtual address is all 0. In this case, neither slave 1 nor slave 3 will receive 0xFF and will not exit address allocation mode.

[0127] Figure 4 In the example above, the virtual address is 11 bits long, with a range of values ​​from 1 to 2047, but this is not limited to this. The more bits a virtual address has, the less likely it is to have the same random address. However, the more bits a virtual address has, the longer it takes to determine the virtual address size. Therefore, in practice, the appropriate number of bits should be selected based on actual needs.

[0128] In the embodiment of the present application, the first instruction information and the fourth instruction information can be customized by the user according to needs, and the complexity of the information can be increased to avoid accidentally entering the address allocation function. The second instruction information can be as simple as possible, for example, the length can be only one byte, such as 0xAA, which can achieve a faster virtual address comparison speed.

[0129] For example, Figure 4 In the example, the second command message can be set to one byte in length. Every 22 bytes sent completes a virtual address comparison, selecting the slave with the largest virtual address. At a baud rate of 9600, this takes approximately 25 milliseconds. Adjusting the interval between the host broadcasting the second command message can easily adjust the address allocation speed.

[0130] In this embodiment of the present application, for inter-slave communication, when a slave enters the receive completion interrupt routine, it can directly select, based on the virtual address, whether to assign the third instruction information (e.g., 0xFF) to the transmit buffer register. This ensures synchronous data transmission and reception between the slaves, minimizes the response time differences between different slaves, and enables multiple slaves to respond with the same data.

[0131] The address allocation method provided by the embodiment of the present application is that the host sends instruction information, and the slave side compares the size of the virtual address through active communication between slaves. In each round of virtual address comparison, the slave with the larger virtual address controls the slave with the smaller virtual address to exit the address allocation operation until a slave with the largest virtual address is screened out. The slave can assign a unique address to itself, and this cycle is repeated until all slaves are assigned unique addresses, thereby realizing automatic allocation of slave addresses without the need for manual address setting. The host is mainly used to start the address allocation function and provide a synchronization signal, and the synchronization signal is used to synchronize the transmission timing of the slave. The address allocation is mainly completed on the slave side. In this way, there is no frequent handshake and data exchange between the host and the slave, which improves the efficiency and speed of address allocation. Moreover, the data between the slaves is a one-way interaction, and the amount of data is very small, so the integrity of the received data is not required to be high, and the protocol is simple. In addition, no special hardware design is required, and the address allocation on the slave side can be achieved only through software control, which has high versatility.

[0132] The present application also provides a master-slave communication system, which can be a multi-power meter system, a multi-electrical instrument system, or a multi-user electric energy meter system. For example, a multi-user electric energy meter system can include multiple slaves (i.e., electric energy meters) and a master (i.e., a controller). The multiple slaves are connected to the master for data transmission, and the master can manage the multiple slaves.

[0133] Figure 5 The structure diagram of the master-slave communication system provided by the embodiment of the present application is shown as an example. Figure 5 As shown, the master-slave communication system may include a master and a plurality of slaves connected to the master for communication, such as Figure 5 The four slaves shown, namely slave 1 , slave 2 , slave 3 and slave 4 , can be communicatively connected to a host 51 via a communication bus 50 .

[0134] Each slave can be used to execute the above Figure 1 The steps of the address allocation method of the corresponding embodiment. The host 51 can be used to perform the above Figure 2 The steps of the address allocation method of the corresponding embodiment. It is understandable that the host 51 and each slave can also perform the above Figure 3The relevant method steps in the corresponding embodiments can be found in the above method embodiments for specific steps, which will not be repeated here.

[0135] For example, the communication bus 50 may be, for example, an RS485 communication bus, an RS232 communication bus, etc., but is not limited thereto.

[0136] The master-slave communication system provided in the embodiment of the present application has similar implementation principles and beneficial effects to the address allocation method provided in the above embodiment, and will not be repeated here.

[0137] Based on the address allocation method described in any of the above embodiments, embodiments of the present application further provide a computer-readable storage medium. For example, the non-transitory computer-readable storage medium may be a read-only memory (ROM), a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, or an optical data storage device. The storage medium stores computer instructions for executing the address allocation method described in any of the above embodiments, which will not be further described herein.

[0138] Those skilled in the art will understand that all or part of the steps to implement the above embodiments may be accomplished by hardware, or may be accomplished by a program to instruct the relevant hardware, and the program may be stored in a computer-readable storage medium, and the above-mentioned storage medium may be a read-only memory, a disk or an optical disk, etc.

[0139] Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the contents disclosed herein. This application is intended to cover any variations, uses, or adaptations herein that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The description and examples are to be considered merely exemplary, and the true scope and spirit of this application are indicated by the claims.

Claims

1. An address allocation method, characterized in that: The invention is applied to a slave in a master-slave communication system, wherein the master-slave communication system includes a master and a plurality of slaves communicatively connected to the master; the address allocation method includes: Upon receiving the first instruction information broadcast by the host, the first target slave enters an address allocation mode and sends a first response information to the host; wherein the first response information is used to instruct the host to broadcast the second instruction information; the first target slave is each slave in the plurality of slaves to which an address is not allocated; The first target slave determines, based on the received second instruction information broadcast by the host, whether its own virtual address is the largest virtual address among the virtual addresses of all slaves to which addresses are not allocated in the plurality of slaves; In a case where the virtual address of the first target slave is the maximum virtual address, the first target slave generates a unique target real address and sets its own real address as the target real address; The first target slave determines, based on the received second instruction information broadcast by the host, whether its own virtual address is the largest virtual address among the virtual addresses of all slaves to which addresses are not allocated in the plurality of slaves, including: Each time the first target slave receives the second instruction information broadcast by the host, the first target slave increases the synchronization count corresponding to the first target slave by 1; the synchronization count is 0 in an initialized state; When the first target slave is in the address allocation mode, determining a target bit of the virtual address of the first target slave based on the number of synchronizations and the number of bits of the virtual address of the first target slave; When the target bit of the virtual address of the first target slave is 1, the first target slave broadcasts third instruction information; wherein the third instruction information is used to instruct a sub-target slave in the second target slave to exit the address allocation mode; the second target slave is a slave other than the first target slave in the address allocation mode, and the sub-target slave is a slave for which the target bit of the virtual address is 0; Repeat the above steps until the first target slave machine is still in the address allocation mode after traversing all bits from the highest bit to the lowest bit of its own virtual address, and then determine that its own virtual address is the maximum virtual address among the virtual addresses of all slave machines with unassigned addresses among the multiple slave machines.

2. The address allocation method according to claim 1, wherein: The determining the target bit position of the virtual address of the first target slave machine based on the number of synchronization times and the number of bits of the virtual address of the first target slave machine includes: Subtract 1 from the synchronization number to obtain a first difference; Taking the modulus of the first difference value and the number of bits of the virtual address of the first target slave machine to obtain a modulo result; Subtract 1 from the number of bits of the virtual address of the first target slave machine to obtain a second difference; A third difference between the second difference and the modulo result is determined, and the third difference is determined as the target bit of the virtual address of the first target slave.

3. The address allocation method according to claim 1, wherein: The address allocation method further includes: The first target slave exits the address allocation mode when the target bit of the virtual address of the first target slave is 0 and the first target slave receives the third instruction information broadcasted by the second target slave.

4. The address allocation method according to claim 1, wherein: The first target slave generates a unique target real address, including: The first target slave obtains a current value of the synchronization number; The first target slave machine rounds the current value to the number of bits of the virtual address of the first target slave machine, and determines the rounding result as the unique target real address of the first target slave machine.

5. The address allocation method according to any one of claims 1 to 4, characterized in that: Before the first target slave receives the first instruction information broadcast by the host, the address allocation method further includes: Upon receiving the fourth instruction information broadcast by the host, the multiple slaves initialize their respective real addresses to 0 and randomly generate their respective corresponding virtual addresses; The virtual address is not 0, and the number of bits of each virtual address is the same.

6. An address allocation method, characterized in that: A host computer used in a master-slave communication system, wherein the master-slave communication system comprises a host computer and a plurality of slave computers communicatively connected to the host computer; The address allocation method comprises: Broadcasting a first instruction message to the plurality of slaves; the first instruction message is used to instruct a first target slave among the plurality of slaves to enter an address allocation mode, and to instruct the first target slave to send a first response message to the host; wherein the first target slave is each slave among the plurality of slaves to which an address is not allocated; upon receiving the first response information, broadcasting second instruction information; the second instruction information being used to instruct the first target slave to determine, based on the second instruction information, whether its own virtual address is the largest virtual address among the virtual addresses of all slaves to which addresses are not allocated in the plurality of slaves, generate a unique target real address if the virtual address of the first target slave is the largest virtual address, and set the real address of the first target slave to the target real address; The first target slave determines, based on the second instruction information, whether its own virtual address is the largest virtual address among the virtual addresses of all slaves to which addresses are not allocated in the plurality of slaves, including: Each time the first target slave receives the second instruction information broadcast by the host, the first target slave increases the synchronization count corresponding to the first target slave by 1; the synchronization count is 0 in an initialized state; When the first target slave is in the address allocation mode, determining a target bit of the virtual address of the first target slave based on the number of synchronizations and the number of bits of the virtual address of the first target slave; When the target bit of the virtual address of the first target slave is 1, the first target slave broadcasts third instruction information; wherein the third instruction information is used to instruct a sub-target slave in the second target slave to exit the address allocation mode; the second target slave is a slave other than the first target slave in the address allocation mode, and the sub-target slave is a slave for which the target bit of the virtual address is 0; Repeat the above steps until the first target slave machine is still in the address allocation mode after traversing all bits from the highest bit to the lowest bit of its own virtual address, and then determine that its own virtual address is the maximum virtual address among the virtual addresses of all slave machines with unassigned addresses among the multiple slave machines.

7. The address allocation method according to claim 6, wherein: The address allocation method further includes: Broadcasting fourth instruction information; the fourth instruction information is used to instruct the multiple slaves to initialize their respective real addresses to 0 and randomly generate their respective corresponding virtual addresses; The virtual address is not 0, and the number of bits of each virtual address is the same.

8. A master-slave communication system, characterized in that: The system comprises a host and a plurality of slaves communicatively connected to the host; The slave device is used to perform the steps of the address allocation method according to any one of claims 1 to 5; The host is configured to execute the steps of the address allocation method according to claim 6 or 7.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the computer program implements the steps of the address allocation method according to any one of claims 1 to 5, or implements the steps of the address allocation method according to claim 6 or 7.

Citation Information

Patent Citations

  • Efficient and repeatable RS-485 bus address allocation method based on bit competition mechanism

    CN116016447A