Address allocation method, master-slave communication system and computer readable storage medium
Through the method of guiding the slave to independently compare virtual addresses and generate real addresses, the problem of manually allocating slave addresses is solved, and the problem of cumbersome and error-prone to manual allocation of slave addresses is realized, efficient automatic allocation of slave addresses is reduced, and the interaction between the host and slave is improved, and allocation efficiency is improved.
Patent Information
- Application Number
- CN202510749413.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-06
- Publication Date
- 2025-07-04
- Estimated Expiration
- 2045-06-06
AI Technical Summary
In the prior art, manual allocation of slave addresses is complicated and error-prone when the number of slaves is large, and the existing automatic allocation method depends on host processing, which is not efficient and takes a long time.
By broadcasting command information by the host, the slave is guided to enter the address allocation mode, and the virtual address size comparison is performed between slaves, the maximum virtual address is determined independently and a unique real address is generated. The host is only responsible for command broadcasting, and the logical processing is completed on the slave side.
Automatic allocation of slave addresses is realized, avoiding the cumbersomeness of manual operations and error rates, reducing frequent handshakes and data exchanges between the host and slave, and improving address allocation efficiency.
Smart Images

Figure CN120263771A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of communication technologies, and in particular, to an address allocation method, a master-slave communication system, and a computer-readable storage medium. Background Art
[0002] In fields such as smart grid systems and smart home systems, a bus communication network (such as RS485) is a common communication network, which generally adopts a master-slave communication method. By allocating addresses to each slave, the master can communicate with the slaves according to the addresses of each slave. To ensure that the master can accurately identify each slave, a unique address needs to be allocated to each slave.
[0003] In the related art, the address of the slave is allocated by manually setting the address. This method is easily affected by human factors. Especially when the number of slaves is large, manual allocation is not only cumbersome but also error-prone. Summary of the Invention
[0004] This application provides an address allocation method, a master-slave communication system, and a computer-readable storage medium to solve the problems of cumbersome operation and easy error in manually allocating the address of the slave, and realizes the automatic allocation of the address of the slave.
[0005] In a first aspect, this application provides an address allocation method, which is applied to a slave in a master-slave communication system. The master-slave communication system includes a master and a plurality of slaves communicatively connected to the master; the address allocation method includes: When a first target slave receives the first instruction information broadcast by the master, it enters the address allocation mode and sends a first response information to the master; wherein, the first response information is used to instruct the master to broadcast second instruction information; the first target slave is each slave among the plurality of slaves that has not been allocated an address; The first target slave determines whether its virtual address is the maximum virtual address among the virtual addresses of all the slaves that have not been allocated addresses among the plurality of slaves based on the second instruction information broadcast by the master received; 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 to the target real address.
[0006] Optionally, the first target slave determines whether its virtual address is the maximum virtual address among the virtual addresses of all the slaves that have not been allocated addresses among the plurality of slaves based on the second instruction information broadcast by the master received, including: When the first target slave receives the second instruction information broadcast by the master each time, it increments the value of the synchronization count corresponding to the first target slave by 1; the value of the synchronization count is 0 in the initialization state; When the first target slave is in the address allocation mode, it determines the target bit of the virtual address of the first target slave based on the synchronization count 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 the remaining slaves other than the first target slave among the slaves in the address allocation mode, and the sub-target slave is the slave whose target bit of the virtual address is 0; Repeat the above steps until the first target slave is still in the address allocation mode when traversing all bits from the highest bit to the lowest bit of its own virtual address, then determine its own virtual address as the largest virtual address among the virtual addresses of all slaves that have not been assigned addresses among the multiple slaves.
[0007] Optionally, determining the target bit of the virtual address of the first target slave based on the synchronization count and the number of bits of the virtual address of the first target slave includes: Subtract 1 from the synchronization count to obtain a first difference; Take the remainder of the first difference with respect to the number of bits of the virtual address of the first target slave to obtain a remainder result; Subtract 1 from the number of bits of the virtual address of the first target slave to obtain a second difference; Determine a third difference between the second difference and the remainder result, and determine the third difference as the target bit of the virtual address of the first target slave.
[0008] Optionally, the address allocation method further includes: 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 broadcast by the second target slave, it exits the address allocation mode.
[0009] Optionally, the first target slave generates a unique target real address, including: The first target slave obtains the current value of the synchronization count; The first target slave rounds the current value with respect to the number of bits of the virtual address of the first target slave, and determines the rounding result as its own unique target real address.
[0010] Optionally, before the first target slave receives the first instruction information broadcast by the master, the address allocation method further includes: When the multiple slaves receive the fourth instruction information broadcast by the master, they initialize their respective real addresses to 0 and randomly generate their respective corresponding virtual addresses; Wherein, the virtual address is not 0, and the number of bits of each virtual address is the same.
[0011] In a second aspect, the present application provides an address allocation method applied to a master in a master-slave communication system. The master-slave communication system includes one master and multiple slaves communicatively connected to the master; the address allocation method includes: Broadcasting first instruction information to the multiple slaves; the first instruction information is used to instruct a first target slave among the multiple slaves to enter an address allocation mode and instruct the first target slave to send first response information to the master; wherein, the first target slave is each slave among the multiple slaves that has not been assigned an address; When receiving the first response information, broadcasting second instruction information; the second instruction information is used to instruct the first target slave to determine whether its virtual address is the maximum virtual address among the virtual addresses of all slaves among the multiple slaves that have not been assigned an address based on the second instruction information. When the virtual address of the first target slave is the maximum virtual address, generate a unique target real address and set the real address of the first target slave to the target real address.
[0012] Optionally, 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; Wherein, the virtual address is not 0, and the number of bits of each virtual address is the same.
[0013] In a third aspect, the present application provides a master-slave communication system, including one master and multiple slaves communicatively connected to the master; The slave is used to execute the steps of the address allocation method described in any one of the above first aspects; The master is used to execute the steps of the address allocation method described in any one of the above second aspects.
[0014] In a fourth aspect, the present application provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, it implements the steps of the address allocation method described in any one of the above first aspects, or implements the steps of the address allocation method described in any one of the above second aspects.
[0015] The address allocation method, master-slave communication system and computer-readable storage medium provided by the present application, for each first target slave that is not assigned an address among multiple slaves of the master-slave communication system, when receiving the first instruction information broadcast by the host, enter the address allocation mode, and send a first response information to the host, and instruct the host to broadcast the second instruction information through the first response information; the first target slave determines whether its own virtual address is the maximum virtual address among the virtual addresses of all slaves that are not assigned an address among multiple slaves based on the second instruction information broadcast by the host received, and if so, the first target slave generates a unique target real address and sets its own real address to the target real address, thereby realizing the automatic allocation of slave addresses and avoiding the problem of 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. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 One of the flowcharts of the address allocation method provided in the embodiment of the present application; Figure 2 The second flowchart of the address allocation method provided in the embodiment of the present application; Figure 3 The third flowchart of the address allocation method provided in the embodiment of the present application; Figure 4 A logical diagram of virtual address comparison of four slaves in an embodiment of the present application; Figure 5 A schematic diagram of the structure of a master-slave communication system provided in an embodiment of the present application. DETAILED DESCRIPTION
[0017] In this application, "at least one" means one or more, and "a plurality" means two or more. "And / or" describes the relationship between associated objects and indicates that three relationships can exist. For example, A and / or B can represent: A exists alone, A and B exist simultaneously, or B exists alone. Here, A and B can be singular or plural. The character " / " generally indicates that the associated objects before and after are in an "or" relationship. "At least one (item)" or a similar expression refers to any combination of these items, including any combination of a single item or multiple items. For example, at least one of a alone, b alone, or c alone can represent: a alone, b alone, c alone, the combination of a and b, the combination of a and c, the combination of b and c, or the combination of a, b, and c. Here, a, b, and c can be single or multiple. In addition, the terms "first" and "second" are only used for descriptive purposes and cannot be construed as indicating or implying relative importance.
[0018] In fields such as smart grid systems and smart home systems, bus communication networks (such as RS485) are common communication networks. They generally adopt a master-slave communication method. By assigning addresses to each slave, the master can communicate with the slaves according to the addresses of the slaves. To ensure that the master can accurately identify each slave, a unique address needs to be assigned to each slave.
[0019] In the related art, the address assignment of slaves can be achieved by manually setting the address. In this method, the address of the slave is preset by the manufacturer during production, with poor flexibility and being easily affected by human factors. Especially when the number of slaves is large, manual assignment is not only cumbersome but also error-prone, and the possibility of address conflicts is relatively high.
[0020] To solve the problems of cumbersome operation and easy error when manually assigning slave addresses, in the related art, an automatic slave address assignment method can be adopted, where the master automatically assigns addresses to the slaves. For example, the master can send instructions to the slaves according to the assignment mechanism. After the slaves report relevant data according to the instructions, the master performs logical processing on this data to generate corresponding slave addresses, and then assigns the corresponding slave addresses to each slave in turn according to the assignment mechanism. Although this method realizes the automatic assignment of slave addresses, the assignment of slave addresses depends on the participation of the master. The logical processing of address assignment is completed by the master, and frequent handshakes and data exchanges between the master and the slaves are required, which takes a long time and the efficiency of slave address assignment is not high. Moreover, during the address assignment process, communication conflicts between slaves need to be avoided, that is, only one slave can respond at a time.
[0021] In view of this, an embodiment of the present application provides an address allocation method. The host broadcasts first instruction information to instruct each first target slave that has not been allocated an address on the slave side to enter the address allocation mode. These first target slaves compare the sizes of virtual addresses with each other through active communication between slaves, and determine the slave with the largest virtual address in one round of comparison. This slave can allocate an address for itself and no longer participate in the virtual address comparison of the slaves that have not been allocated addresses in the next round. This process is repeated until all slaves have allocated unique addresses for themselves. In this way, the automatic allocation of slave addresses can be completed on the slave side, avoiding the problems of cumbersome operation and easy error when manually allocating slave addresses. Moreover, the host only broadcasts 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 slaves, and improving the efficiency of address allocation.
[0022] Next, in conjunction with Figures 1 to 4 a specific description of the address allocation method provided by the embodiment of the present application will be given.
[0023] Figure 1 FIG. 1 shows one of the flow diagrams of the address allocation method provided by the embodiment of the present application. This address allocation method can be applied to the slaves in a master-slave communication system, and this master-slave communication system includes a host and multiple slaves communicatively connected to the host. Referring to Figure 1 as shown, this address allocation method may include the following steps 110 to 130.
[0024] Step 110: When the first target slave receives the first instruction information broadcast by the host, it enters the address allocation mode and sends a first response message to the host.
[0025] Among them, the first instruction information is used to instruct the slaves that have not been allocated addresses to enter the address allocation mode; the first response message is used to instruct the host to broadcast second instruction information, and this second instruction information can be used to instruct the first target slaves in the address allocation mode to judge the sizes of virtual addresses; the first target slave is each slave among the multiple slaves that has not been allocated an address.
[0026] Specifically, the host and the slaves in the master-slave communication system can be connected through a communication bus. The host can broadcast the first instruction information to each slave through the communication bus. When the first target slave that has not been allocated an address on the slave side receives the first instruction information broadcast by the host, it enters the address allocation mode and simultaneously sends a first response message to the host. If the host receives this first response message, it can determine that there are still slaves on the slave side that have not been allocated addresses, and at this time, it can broadcast the second instruction information to the slave side through the communication bus.
[0027] Exemplarily, for each slave device, when its actual address ADD = 0, it can be determined that the slave device is a slave device with an unassigned address.
[0028] Exemplarily, the first response message can be, for example, "0xFF", but is not limited thereto.
[0029] Exemplarily, before step 110, multiple slave devices communicatively connected to the master device in the master-slave communication system can be initialized first. Specifically, before the first target slave device receives the first instruction message broadcast by the master device, the address allocation method can further include: when the multiple slave devices receive the fourth instruction message broadcast by the master device, they initialize their respective actual addresses to 0 and randomly generate their respective corresponding virtual addresses. Among them, the virtual address is not 0, and the number of bits of each virtual address is the same.
[0030] For example, in the master-slave communication system, there is a master device and slave devices 1, 2, 3, and 4 communicatively connected to the master device through a communication bus. The master device can broadcast the fourth instruction message to each slave device through the communication bus. After each slave device receives the fourth instruction message, it initializes its current actual address ADD to 0. At the same time, it randomly generates its respective corresponding non-zero virtual addresses, such as RADD1, RADD2, RADD3, and RADD4 respectively. Among them, RADD1, RADD2, RADD3, and RADD4 are all different, but the number of bits is the same, for example, the length of all of them is 11-bit.
[0031] Step 120: Based on the second instruction message broadcast by the master device received by the first target slave device, determine whether its virtual address is the largest virtual address among the virtual addresses of all slave devices with unassigned addresses among the multiple slave devices.
[0032] Among them, the second instruction message can be used to instruct the first target slave devices in the address allocation mode to judge the size of the virtual addresses.
[0033] In an optional embodiment, after the master device receives the first response message sent by the first target slave device and determines that there are still slave devices with unassigned addresses among the multiple slave devices communicatively connected to it, at this time, it can broadcast the second instruction message to the slave device side through the communication bus. For each first target slave device with an unassigned address on the slave device side, after receiving the second instruction message, the first target slave device compares the size of its virtual address with that of other first target slave devices to determine whether its virtual address is the largest virtual address among the virtual addresses of all first target slave devices with these unassigned addresses.
[0034] In another alternative embodiment, after the host receives the first response information sent by the first target slave, if it determines that there are still slaves among the multiple slaves communicatively connected to it that have not been assigned addresses, at this time, a preset number of second instruction messages can be periodically broadcast to the slave side through the communication bus, where the preset number is equal to the number of bits of the virtual address of the slaves. The first target slave in the address assignment mode can compare the virtual address bit by bit from the highest significant bit to the lowest significant bit according to the second instruction message. Each time the host broadcasts the second instruction message, each first target slave in the address assignment mode makes a comparison of the same bit position. A bit value of 1 indicates a larger virtual address, and a bit value 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 assignment operation. When all bit positions have been compared, there will be one first target slave still in the address assignment mode, and this first target slave is the slave with the largest virtual address. In this way, the screening of the slave with the largest virtual address is completed, and the address of this slave can be assigned and will no longer participate in the next round of address assignment as a first target slave. Repeat this process until all slaves have been assigned unique addresses.
[0035] Specifically, step 120 may include the following steps 121 to 124.
[0036] Step 121: Each time the first target slave receives the second instruction message broadcast by the host, increment the value of the synchronization count corresponding to the first target slave by 1.
[0037] Among them, the value of the synchronization count is 0 in the initialization state. For example, when initializing the real addresses of the slaves in the master-slave communication system, the synchronization count CNT can also be initialized to 0. This synchronization count can record the number of second instruction messages received by the first target slave without an assigned address from the first time it receives the second instruction message broadcast by the host until it is assigned an address, and can synchronize the comparison bit positions for comparison by each first target slave during the bit position comparison to ensure that the comparison is of the same bit position.
[0038] Each time the host broadcasts the second instruction message, after the first target slave receives this second instruction message, increment the value of the corresponding synchronization count CNT by 1.
[0039] Step 122: When the first target slave is in the address assignment mode, determine the target bit position of the virtual address of the first target slave based on the synchronization count and the number of bits of the virtual address of the first target slave.
[0040] The synchronization count CNT can be used to synchronize the bits to be compared when the first target slaves perform virtual address comparison. For the first target slaves in the address allocation mode, after adding 1 to the value of the corresponding synchronization count, the target bit to be compared can be located using the incremented synchronization count and the number of bits of the virtual address of the first target slave.
[0041] Exemplarily, determining the target bit of the virtual address of the first target slave based on the synchronization count and the number of bits of the virtual address of the first target slave may include: subtracting 1 from the synchronization count to obtain a first difference; taking the remainder of the first difference with respect to the number of bits of the virtual address of the first target slave to obtain a remainder result; subtracting 1 from the number of bits of the virtual address of the first target slave to obtain a second difference; determining a third difference between the second difference and the remainder result, and determining the third difference as the target bit of the virtual address of the first target slave.
[0042] 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): (1) where BT represents the target bit, M represents the number of bits of the virtual address, CNT represents the synchronization count, and the symbol "%" represents the remainder operation.
[0043] Step 123: When the target bit of the virtual address of the first target slave is 1, the first target slave broadcasts third instruction information.
[0044] The third instruction information is used to instruct the sub-target slaves in the second target slaves to exit the address allocation mode; the second target slaves are the remaining slaves in the slaves in the address allocation mode except the first target slave, and the sub-target slaves are the slaves whose target bits of the virtual address are 0.
[0045] After determining the target bit to be compared of its virtual address, the first target slave can determine whether the target bit is 1 or 0. If it is 1, it can determine 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 other slaves currently in the address allocation mode, that is, the second target slaves. For the second target slaves, if the target bit of their virtual address is 0, when receiving the third instruction information broadcast by the first target slave, they exit the address allocation mode and re-enter the address allocation mode when starting the next round of address allocation, that is, when receiving the first instruction information broadcast by the host again, they enter the address allocation mode.
[0046] It can be understood 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 the third instruction information broadcast by other slaves is received, it can be determined that itself is not the slave with the largest virtual address, and it directly exits the address allocation mode.
[0047] Specifically, after step 122 is executed, the address allocation method may further include: when the target bit of the virtual address of the first target slave is 0 and the third instruction information broadcast by the second target slave is received, the first target slave exits the address allocation mode.
[0048] Step 124: Repeat the above steps until the first target slave is still in the address allocation mode when traversing all bits from the highest bit to the lowest bit of its own virtual address. Then, it is determined that its virtual address is the largest virtual address among the virtual addresses of all slaves that have not been allocated addresses.
[0049] After the first target slave broadcasts the third instruction information to instruct the slave with the target bit of the virtual address being 0 in the second target slaves to exit the address allocation mode, if all bits of the virtual address of the first target slave have not been compared yet, repeat steps 121 to 123 above until the first target slave is still in the address allocation mode when traversing all bits from the highest bit to the lowest bit of its own virtual address. At this time, it can be considered that the virtual address of the first target slave is the largest virtual address among the virtual addresses of all slaves that have not been allocated addresses, and the first target slave is the only slave selected from all slaves that have not been allocated addresses. This first target slave can be allocated an address.
[0050] Among them, the second instruction information can be used to control the bits of the virtual address to be traversed in sequence from the highest bit to the lowest bit. In the process of judging the largest virtual address in one round, 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 bits of the virtual address can be shifted one bit to the right.
[0051] Exemplarily, the third instruction information can be, for example, "0xFF", but it is not limited thereto.
[0052] According to the embodiments corresponding to the above steps 121 to 124, for example, taking the communication connection between a host and 4 slave devices as an example, assuming that currently 3 slave devices have not been assigned addresses, such as slave device A, slave device B, and slave device D. When the host broadcasts the second instruction message through the communication bus, the values of the synchronization count CNT of slave device A, slave device B, and slave device D will each be incremented by 1. Assuming that at this time, slave device A, slave device B, and slave device D are all in the address assignment mode, according to their respective synchronization counts CNT, the target bit positions to be compared for each slave device can be determined using the above formula (1). Assuming that the binary values of the target bit positions of slave device A and slave device D are 1, while the binary value of the target bit position of slave device B is 0, then slave device A and slave device D can broadcast the third instruction message through the communication bus, and slave device B does not make a response. At this time, slave device B will receive the third instruction message broadcast by slave device A and slave device D, and then exit the address assignment mode. If the bit positions of the virtual address have not been completely compared at this time, the host will continue to broadcast the second instruction message, and the values of the synchronization count CNT of slave device A, slave device B, and slave device D will continue to be incremented by 1. At this time, only slave device A and slave device D are still in the address assignment mode, so the target bit positions to be compared for slave device A and slave device D can continue to be determined using the above formula (1). Assuming that the binary value of the target bit position of slave device A is 1, while the binary value of the target bit position of slave device D is 0, then slave device A can broadcast the third instruction message through the communication bus, and slave device D does not make a response. At this time, slave device D will receive the third instruction message broadcast by slave device A and exit the address assignment mode. At this time, only slave device A is still in the address assignment mode. If the bit positions of the virtual address have been completely compared at this time, then slave device A is the slave device with the largest virtual address selected; if the bit positions of the virtual address have not been completely compared at this time, the host will continue to broadcast the second instruction message, and the values of the synchronization count CNT of slave device A, slave device B, and slave device D will continue to be incremented by 1. This cycle continues until the bit positions of the virtual address are completely compared. Slave device A will always be in the address assignment mode, and then slave device A is the slave device with the largest virtual address selected.
[0053] Step 130: In the case where the virtual address of the first target slave device is the largest virtual address, the first target slave device generates a unique target real address and sets its own real address to the target real address.
[0054] If the virtual address of the first target slave device is the largest virtual address, then the first target slave device can be assigned an address. At this time, the first target slave device generates a unique target real address by itself and sets its own real address to the target real address, completing the assignment of its own address.
[0055] In an alternative embodiment, the first target slave can generate its own unique target real address based on the current synchronization count CNT. Exemplarily, the first target slave generating a unique target real address may include: the first target slave obtaining the current value of the synchronization count; the first target slave taking the integer of the current value with respect to the number of bits of the virtual address of the first target slave, and determining the integer result as its own unique target real address.
[0056] Specifically, the first target slave can generate the target real address using the following formula (2): (2) where ADD represents the real address, M represents the number of bits of the virtual address, CNT represents the synchronization count, and the symbol " / " represents the integer operation.
[0057] For each first target slave among multiple slaves in the master-slave communication system that has not been assigned an address, the address allocation method provided by the embodiments of the present application, in the case of receiving the first instruction information broadcast by the master, enters the address allocation mode and sends the first response information to the master, and indicates to the master to broadcast the second instruction information through the first response information; the first target slave determines whether its virtual address is the largest virtual address among the virtual addresses of all slaves that have not been assigned addresses among the multiple slaves based on the received second instruction information broadcast by the master. If so, the first target slave generates a unique target real address and sets its real address to this target real address, realizing the automatic allocation of slave addresses and avoiding the problems of cumbersome operation and easy error when manually allocating slave addresses. Moreover, the master only broadcasts the 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 master and the slaves and improving the efficiency of address allocation.
[0058] Figure 2 FIG. 2 shows a second schematic flow chart of the address allocation method provided by the embodiments of the present application. This address allocation method can be applied to the master in the master-slave communication system. The master-slave communication system includes a master and multiple slaves communicatively connected to the master. Referring to Figure 2 As shown, this address allocation method may include the following steps 210 to 220.
[0059] Step 210: Broadcast the first instruction information to multiple slaves.
[0060] Among them, the first instruction information is used to instruct the first target slave among the multiple slaves to enter the address allocation mode and to instruct the first target slave to send the first response information to the master; the first target slave is each slave among the multiple slaves that has not been assigned an address.
[0061] The host can broadcast first instruction information to multiple slaves in the master-slave communication system through the communication bus to determine whether there are still slaves on the slave side that have not been assigned addresses.
[0062] Specifically, the host can broadcast first instruction information to multiple slaves in the master-slave communication system. For the first target slave among the multiple slaves that has not been assigned an address, after receiving the first instruction information, it enters the address assignment mode and sends a first response message to the host, using the first response message to notify the host that there are still slaves that have not been assigned addresses.
[0063] Exemplarily, for each slave, when its real address ADD = 0, it can be determined that the slave is a slave that has not been assigned an address.
[0064] Exemplarily, the first response message can be, for example, "0xFF", and the embodiments of the present application do not make any limitations in this regard.
[0065] Exemplarily, before performing the address assignment of the slaves, initialization operations before address assignment can also be performed on each slave on the slave side. Specifically, the address assignment method can further include: the host broadcasts fourth instruction information. Among them, the fourth instruction information is used to instruct multiple slaves to initialize their respective real addresses to 0 and randomly generate their respective corresponding virtual addresses; among them, the virtual addresses are not 0, and the number of bits of each virtual address is the same.
[0066] Step 220: Broadcast second instruction information when the first response message is received.
[0067] Among them, the second instruction information is used to instruct the first target slave to determine whether its virtual address is the maximum virtual address among the virtual addresses of all slaves on the slave side that have not been assigned addresses based on the second instruction information. When the virtual address of the first target slave is the maximum virtual address, generate a unique target real address and set the real address of the first target slave to the target real address.
[0068] After the host receives the first response message, it can confirm that there are still slaves on the slave side that have not been assigned addresses. At this time, a new round of address assignment on the slave side is started, and the second instruction information can be broadcast through the communication bus.
[0069] Specifically, the host can periodically broadcast a preset number of second instruction messages, 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 bit by bit from the highest bit to the lowest bit according to the second instruction message. Each time the host broadcasts the second instruction message, each first target slave in the address allocation mode makes a comparison of 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 have been compared, there will be one first target slave still in the address allocation mode, and this first target slave is the slave with the largest virtual address. In this way, the screening of the slave with the largest virtual address is completed, and the address of this slave can be allocated and will no longer participate in the next round of address allocation as a first target slave. Repeat this process until all slaves are allocated unique addresses.
[0070] In the address allocation method provided by the embodiments of the present application, the host starts a new round of address allocation on the slave side by broadcasting the first instruction message, instructs the first target slaves without allocated addresses among multiple slaves to enter the address allocation mode, and instructs the first target slaves to send the first response message to the host. Whether there are still slaves without allocated addresses on the slave side is determined through the first response message. When the host receives the first response message and confirms that there are still slaves without allocated addresses on the slave side, it continues to broadcast the second instruction message, instructs the first target slaves to determine whether their virtual addresses are the largest virtual addresses among the virtual addresses of all slaves without allocated addresses. When the virtual address of the first target slave is the largest virtual address, a unique target real address is generated, and the real address of the first target slave is set as the target real address, realizing the automatic allocation of slave addresses and avoiding the problems of cumbersome operation and easy error when manually allocating slave addresses. The host only broadcasts instruction messages to instruct the address allocation of slaves 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 slaves and improving the efficiency of address allocation.
[0071] Based on Figure 1 and Figure 2 the corresponding embodiment of the address allocation method, Figure 3 FIG. 3 shows a schematic flowchart of the address allocation method provided by the embodiments of the present application. The address allocation method can be applied to a master-slave communication system, which includes a host and multiple slaves communicatively connected to the host. Referring to Figure 3 as shown, the address allocation method may include the following steps 301 to step 314.
[0072] Step 301: The host broadcasts the fourth instruction message.
[0073] The host can broadcast the fourth instruction information to the slaves in the master-slave communication system through the communication bus to instruct the slaves to perform initialization operations.
[0074] Step 302: The slaves initialize their real addresses and the number of synchronization times, and generate their respective corresponding virtual addresses.
[0075] When all slaves receive the fourth instruction information broadcast by the host, they initialize their respective real addresses ADD to 0, and randomly generate a set of non-zero virtual addresses corresponding to themselves. At the same time, the number of synchronization times CNT corresponding to each slave can be initialized to 0.
[0076] Step 303: The host broadcasts the first instruction information.
[0077] The host can broadcast the first instruction information to the slave side through the communication bus to confirm whether there are slaves with unallocated addresses on the slave side, and instruct the slaves with unallocated addresses on the slave side to enter the address allocation mode.
[0078] Step 304: The first target slave with an unallocated address enters the address allocation mode and sends a first response message to the host once.
[0079] On the slave side, the first target slave with an unallocated address (i.e., the slave with a real address ADD of 0) enters the address allocation mode after receiving the first instruction information and sends a first response message to the host once. The first response message can be, for example, "0xFF", but it is not limited to this.
[0080] Step 305: The host determines whether it has received the first response message. If not, it means that all slaves on the slave side have been allocated addresses, and at this time, step 306 is executed; otherwise, it means that there are still slaves on the slave side with unallocated addresses, and at this time, step 307 is executed.
[0081] Step 306: Exit the address allocation operation.
[0082] Step 307: The host broadcasts the second instruction information.
[0083] Step 308: The first target slave with an unallocated address increments the value of the corresponding synchronization times by 1.
[0084] After receiving the second instruction information, the first target slave with an unallocated address increments the value of the corresponding synchronization times CNT by 1.
[0085] Step 309: The first target slave in the address allocation mode determines whether the target bit of its virtual address is 1.
[0086] The first target slave in the address allocation mode can determine the target bit position of its own virtual address based on its corresponding synchronization count CNT and the number of bits of the virtual address. Specifically, for the method of determining the target bit position, reference can be made to the description in step 122 above, which will not be elaborated here.
[0087] After determining the target bit position, obtain the binary value of the target bit position and determine whether the binary value is 1. If it is 1, execute step 310; if it is not 1, that is, the binary value is 0, execute step 311.
[0088] Step 310: The first target slave in the address allocation mode broadcasts the third instruction information. After that, step 313 can be continued.
[0089] If the first target slave in the address allocation mode determines that the target bit position is 1, it broadcasts the third instruction information through the communication bus. The third instruction information can be, for example, "0xFF", but is not limited thereto.
[0090] Step 311: The first target slave in the address allocation mode determines whether it receives the third instruction information broadcast by other slaves.
[0091] If it receives, it means 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 it does not receive, it means that the target bit position of the virtual addresses of other slaves is also 0, and step 313 is executed.
[0092] Step 312: The first target slave in the address allocation mode exits the address allocation mode.
[0093] Step 313: Whether the bit positions of the virtual address of the first target slave in the address allocation mode have been traversed and compared. If so, step 314 is executed; otherwise, step 307 is executed.
[0094] Step 314: The first target slave currently in the address allocation mode generates a unique target real address and sets its own real address to the target real address.
[0095] If the bit positions of the virtual address of the first target slave in the address allocation mode have been compared, at this time, only one first target address is in the address allocation mode, then the first target slave currently in the address allocation mode can be allocated 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. After that, step 303 is continued.
[0096] According to Figure 3Address allocation method for corresponding embodiments. Taking a master-slave communication system including one master and 4 slaves communicatively connected to the master, the number of bits of the virtual address being 11 bits, and the third instruction information being 0xFF as an example, assume 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 shows a logical schematic diagram of the comparison of the virtual addresses of 4 slaves. Referring to Figure 4 as shown, each slave can compare the synchronization bit according to the second instruction information broadcast by the master, and compare the size of the virtual address starting from the highest bit in sequence. The slave whose value of the currently compared target bit is 1 will send 0xFF, and the slave whose value of the currently compared target bit is 0 only receives 0xFF. After receiving the 11th second instruction information from the master, finally only slave 3 is still in the address allocation mode. After the master broadcasts the first second instruction information, the comparison of the highest bit is performed. Since the highest bits of the virtual addresses of all slaves are 1, all slaves respond and no slave exits the address allocation mode. Similarly, after the master broadcasts the 5th second instruction information, the comparison of the 6th bit of the virtual address is performed. At this time, there is a situation where the 6th bits of the virtual addresses are all 0. At this time, neither slave 1 nor slave 3 will receive 0xFF and will not exit the address allocation mode.
[0097] Figure 4 In the example, the virtual address is 11 bits, and its value range is from 1 to 2047, but it is not limited to this. For the virtual address, the more bits it has, the less likely it is to have the same random address. However, the more bits there are, the more time it takes to judge the size of the virtual address. Therefore, in actual applications, an appropriate number of bits can be selected according to actual needs.
[0098] In the embodiments 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 also 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, so as to achieve a faster virtual address comparison speed.
[0099] For example, in Figure 4 , the second instruction information can be set to a length of one byte. Every 22 bytes are sent, a round of virtual address comparison can be completed, and the slave with the largest virtual address can be screened out. Calculated at a baud rate of 9600, it takes about 25 milliseconds. By adjusting the sending interval of the second instruction information broadcast by the master, the address allocation speed can be easily adjusted.
[0100] In the embodiments of the present application, for the mutual communication between slave devices, when a slave device enters the receive completion interrupt program, it can directly perform a hardware operation of selecting whether to assign third instruction information (such as 0xFF) to the transmit buffer register according to the virtual address. This can ensure the synchronous data transmission and reception of the slave devices, minimize the response time difference between different slave devices as much as possible, and enable multiple slave devices to respond to the same data.
[0101] The address allocation method provided by the embodiments of the present application is as follows: The host device issues instruction information, and the slave devices actively communicate with each other to compare the sizes of virtual addresses. In each round of virtual address comparison, the slave device with a larger virtual address controls the slave device with a smaller virtual address to exit the address allocation operation until a slave device with the largest virtual address is selected. This slave device can assign a unique address to itself. This process is repeated until all slave devices are assigned unique addresses, achieving automatic allocation of slave device addresses without manual setting. Moreover, the host device mainly functions as a start address allocation function and provides a synchronization signal to synchronize the transmit timing of the slave devices. The address allocation is mainly completed on the slave device side. In this way, there is no frequent handshake and data exchange between the host device and the slave devices, improving the efficiency and speed of address allocation. In addition, the data between the slave devices is unidirectional and the data volume is extremely small, with low requirements for the integrity of received data and a simple protocol. Also, no special hardware design is required, and the address allocation on the slave device side can be achieved only through software control, with high versatility.
[0102] The embodiments of the present application further provide a master-slave communication system, which can be a multi-power meter system, a multi-electrical instrument system, a multi-user electricity meter system, etc. Taking the multi-user electricity meter system as an example, it can include multiple slave devices (i.e., electricity meters) and a host device (i.e., a controller). The multiple slave devices are respectively connected to the host device for data transmission, and the host device can manage the multiple slave devices.
[0103] Figure 5 The structural schematic diagram of the master-slave communication system provided by the embodiments of the present application is exemplified. Referring to Figure 5 as shown, the master-slave communication system can include a host device and multiple slave devices communicatively connected to the host device. For example, it includes 4 slave devices as shown in Figure 5 i.e., slave device 1, slave device 2, slave device 3, and slave device 4. Each slave device can be communicatively connected to the host device 51 through a communication bus 50.
[0104] Among them, each slave device can be used to execute the steps of the address allocation method corresponding to the above Figure 1 corresponding embodiment. The host device 51 can be used to execute the steps of the address allocation method corresponding to the above Figure 2 corresponding embodiment. It can be understood that the host device 51 and each slave device can also execute the above Figure 3The method steps related to the corresponding embodiments. For the specific steps, reference may be made to the above method embodiments, which will not be elaborated here.
[0105] By way of example, the communication bus 50 can be, for example, an RS485 communication bus, an RS232 communication bus, etc., but is not limited thereto.
[0106] The implementation principle and beneficial effects of the master-slave communication system provided in the embodiments of the present application are similar to those of the address allocation method provided in the above embodiments, which will not be elaborated here.
[0107] Based on the address allocation method described in any of the above embodiments, the embodiments of the present application further provide a computer-readable storage medium. For example, a non-transitory computer-readable storage medium can be a read-only memory (ROM), a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, and an optical data storage device, etc. Computer instructions are stored on the storage medium for executing the address allocation method described in any of the above embodiments, which will not be elaborated here.
[0108] Those skilled in the art can understand that all or part of the steps for implementing the above embodiments can be completed by hardware, or can be completed by a program instructing relevant hardware. The program can be stored in a computer-readable storage medium, and the above-mentioned storage medium can be a read-only memory, a magnetic disk, or an optical disc, etc.
[0109] After considering the specification and practicing the content disclosed herein, those skilled in the art will readily think of other implementation schemes of the present application. The present application aims to cover any variations, uses, or adaptive changes herein. These variations, uses, or adaptive changes follow the general principles of the present application and include common general knowledge or conventional technical means in the technical field not disclosed in the present application. The specification and embodiments are only regarded as exemplary, and the true scope and spirit of the present application are pointed out by the claims.
Claims
1. An address allocation method, characterized in that, A slave device applied to a master-slave communication system, the master-slave communication system including a master device and a plurality of slave devices communicatively connected to the master device; the address allocation method includes: When a first target slave device receives the first instruction information broadcast by the master device, it enters the address allocation mode and sends a first response information to the master device; wherein, the first response information is used to instruct the master device to broadcast second instruction information; the first target slave device is each slave device among the plurality of slave devices that has not been allocated an address. The first target slave device determines whether its virtual address is the maximum virtual address among the virtual addresses of all the slave devices that have not been allocated addresses among the plurality of slave devices based on the second instruction information broadcast by the master device received. When the virtual address of the first target slave device is the maximum virtual address, the first target slave device generates a unique target real address and sets its real address to the target real address.
2. The address allocation method according to claim 1, wherein The first target slave device determines whether its virtual address is the maximum virtual address among the virtual addresses of all the slave devices that have not been allocated addresses among the plurality of slave devices based on the second instruction information broadcast by the master device received, including: When the first target slave device receives the second instruction information broadcast by the master device each time, it increments the value of the synchronization count corresponding to the first target slave device by 1; the value of the synchronization count is 0 in the initialization state. When the first target slave device is in the address allocation mode, it determines the target bit position of the virtual address of the first target slave device based on the synchronization count and the number of bits of the virtual address of the first target slave device. When the target bit position of the virtual address of the first target slave device is 1, the first target slave device broadcasts third instruction information; wherein, the third instruction information is used to instruct a sub-target slave device among second target slave devices to exit the address allocation mode; the second target slave device is the remaining slave devices among the slave devices in the address allocation mode except the first target slave device, and the sub-target slave device is the slave device whose target bit position of the virtual address is 0. Repeat the above steps until the first target slave device is still in the address allocation mode when traversing all bits from the highest bit to the lowest bit of its virtual address, then it is determined that its virtual address is the maximum virtual address among the virtual addresses of all the slave devices that have not been allocated addresses among the plurality of slave devices.
3. The address allocation method according to claim 2, wherein, Determining the target bit position of the virtual address of the first target slave device based on the synchronization count and the number of bits of the virtual address of the first target slave device includes: Subtract 1 from the synchronization count to obtain a first difference. Take the remainder of the first difference with respect to the number of bits of the virtual address of the first target slave device to obtain a remainder result. Subtract 1 from the number of bits of the virtual address of the first target slave device to obtain a second difference. Determine a third difference between the second difference and the remainder result, and determine the third difference as the target bit position of the virtual address of the first target slave device.
4. The address allocation method according to claim 2, wherein The address allocation method further includes: 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 broadcast by the second target slave, the first target slave exits the address allocation mode.
5. The address allocation method according to claim 2, characterized in that The first target slave generates a unique target real address, including: The first target slave obtains the current value of the synchronization count; The first target slave rounds the current value to the number of bits of the virtual address of the first target slave, and determines the rounding result as its own unique target real address.
6. The address allocation method according to any one of claims 1 to 5, characterized in that Before the first target slave receives the first instruction information broadcast by the host, the address allocation method further includes: When the plurality of slaves receive the fourth instruction information broadcast by the host, they initialize their respective real addresses to 0 and randomly generate their respective corresponding virtual addresses; Wherein, the virtual address is not 0, and the number of bits of each virtual address is the same.
7. An address allocation method, characterized in that, Applied to a host in a master-slave communication system, the master-slave communication system includes one host and a plurality of slaves communicatively connected to the host; The address allocation method includes: Broadcasting first instruction information to the plurality of slaves; the first instruction information 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 first response information to the host; wherein, the first target slave is each slave among the plurality of slaves that has not been assigned an address; When the first response information is received, broadcasting second instruction information; the second instruction information is used to instruct the first target slave to determine whether its virtual address is the maximum virtual address among the virtual addresses of all the slaves that have not been assigned addresses based on the second instruction information. When the virtual address of the first target slave is the maximum virtual address, generate a unique target real address, and set the real address of the first target slave to the target real address.
8. The address allocation method according to claim 7, characterized in that, The address allocation method further includes: Broadcasting fourth instruction information; the fourth instruction information is used to instruct the plurality of slaves to initialize their respective real addresses to 0 and randomly generate their respective corresponding virtual addresses; Wherein, the virtual address is not 0, and the number of bits of each virtual address is the same.
9. A master-slave communication system, characterized in that, Includes one host and a plurality of slaves communicatively connected to the host; The slave is used to execute the steps of the address allocation method according to any one of claims 1 to 6; The host is used to execute the steps of the address allocation method according to claim 7 or 8.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the address allocation method according to any one of claims 1 to 6, or implements the steps of the address allocation method according to claim 7 or 8.
Citation Information
Patent Citations
IT equipment information acquisition method and device
CN111563055A
Slave address allocation method and device and communication address acquisition method and device
CN115037721A
Full-automatic address allocation system and method
CN115297089A
Ad-hoc network method and device based on RS485 communication, computer equipment and medium
CN115442333A
Efficient and repeatable RS-485 bus address allocation method based on bit competition mechanism
CN116016447A