Method for automatically distributing slave addresses by RS485 bus according to half-byte competition

By using the method of automatically allocating slave addresses on the RS485 bus, the problem of median competition in the existing technology is solved, and the address allocation efficiency is improved, especially in the case of multiple slaves, which significantly shortens the address Allocate time.

CN119127733APending Publication Date: 2024-12-13XIJIA (ZHEJIANG) INTELLIGENT TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411263595.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-10
Publication Date
2024-12-13

AI Technical Summary

Technical Problem

In the existing RS485 bus address allocation method, the bit competition-based method takes a long time and is inefficient when the number of slaves is large.

Method used

The method of automatically allocating slave addresses by nibble competition is adopted. Through clock synchronization between the master and slave, the allocation address is generated and sent in a mask form. The slave starts competing with the last few digits of its own unique identification code and determines that the competition is successful. quantity, adjust competition methods to improve efficiency.

Benefits of technology

The efficiency of slave address allocation is improved and the time consumed for address allocation is reduced. Especially when the number of slaves is large, the address allocation time is significantly shortened through nibble competition.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119127733A_ABST
    Figure CN119127733A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of address allocation, and discloses a method for automatically allocating slave addresses by an RS485 bus according to half-byte competition, which is characterized by comprising the following steps of: S1, performing clock synchronization between a host and a plurality of slaves; s2, the host generates an allocation address; s3, the host sends the distribution address in the S2 to an RS-485 bus in a mask mode, the slave starts to compete with the last (1 + n) bit of the unique identification code of the slave according to effective half bytes of the mask, and n is initially 0; address allocation is carried out in a half-byte competition mode, compared with a bit competition mode, the efficiency of address allocation between the host and the slave is improved, and the time consumed by address allocation is shortened; and when the number of the slaves is less than or equal to a, address allocation is carried out in a bit competition mode, so that the time consumed by address allocation can be further shortened.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of address allocation, and more particularly to a method for automatically allocating slave addresses according to half-byte competition on an RS485 bus. Background Art

[0002] At present, the control system composed of RS-485 bus generally adopts the master-slave communication mode. Usually a control system consists of a host and several slave devices. The master-slave communication mode is always initiated by the host. After receiving the command, the slave obtains the bus control right and sends information to the bus. After the transmission is completed, the bus control right is returned to the host.

[0003] The host needs to identify several slave devices mounted on the RS-485 bus. The simplest way is to distinguish each slave device by the address of the slave device.

[0004] In the invention patent with application number CN202211729174.2, an efficient and repeatable RS-485 bus address allocation method based on the bit competition mechanism is disclosed. The bit competition mechanism is executed according to whether the number of the slave's own UUID sub-bit is 0 or 1. For a large number of slaves, more time resources are consumed through bit competition. Summary of the invention

[0005] In view of the shortcomings of the prior art, the present invention aims to provide a method for automatically allocating slave addresses according to half-byte competition on an RS485 bus, so that the slave can obtain a device address from the bus through half-byte competition.

[0006] To achieve the above object, the present invention provides the following technical solution: A method for automatically allocating slave addresses according to half-byte competition on RS485 bus, comprising the following steps: S1, clock synchronization between the host and multiple slaves; S2, the host generates an assigned address; S3, the host sends the assigned address in S2 to the RS-485 bus in mask form, and the slave competes for the valid half byte of the mask starting with the last (1+n) bit of its own unique identification code, where n is initially 0; S4. Determine whether the number of slaves that have successfully competed is 1: if it is greater than 1, n is increased by 1, and S3 is repeated for the slaves that have successfully competed; if it is 1, an address is allocated to the slave, and S2 is repeated for the remaining slaves; if it is 0, S2 is repeated.

[0007] As a further improvement of the present invention, in S2, the host randomly obtains a unique identification code of a slave device and generates an allocation address according to a CRC32 checksum algorithm.

[0008] As a further improvement of the present invention, in S4, if the number of slaves that have successfully competed is 1, an address is allocated to the slave, and after the host receives a response from the slave that has successfully competed, S2 is repeated for the remaining slaves.

[0009] As a further improvement of the present invention, S2.1, located between S2 and S3, determines whether the number of slaves is less than or equal to a. If the number of slaves is less than or equal to a, the slaves perform address allocation in a bit competition manner; if the number of slaves is greater than a, enter S3.

[0010] As a further improvement of the present invention, it also includes S0, the host and the slaves are powered on, the slaves powered on in different time periods are grouped, and the slaves enter S1 in the order of the groups.

[0011] As a further improvement of the present invention, S2.2, determine whether the number of slaves in the current group is less than or equal to b. If the number of slaves in the current group is less than or equal to b, add slaves in the next group, and b is greater than a.

[0012] The beneficial effects of the present invention are as follows: in the present invention, address allocation is performed by half-byte competition, which improves the efficiency of address allocation between the host and the slaves and reduces the time consumed by address allocation compared to the bit competition method; when the number of slaves is less than or equal to a, address allocation is performed by changing to the bit competition mode, which can further shorten the time consumed by address allocation. BRIEF DESCRIPTION OF THE DRAWINGS

[0013] Figure 1 It is a schematic diagram of the connection method between the host and several slaves in the present invention; Figure 2 It is a flow chart of address allocation according to half-byte competition in the present invention; Figure 3 It is a demonstration diagram of the half-byte competition method in the present invention; Figure 4 It is a schematic diagram of the time-consuming steps in the present invention. DETAILED DESCRIPTION

[0014] The present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. The same parts are represented by the same reference numerals. It should be noted that the words "front", "rear", "left", "right", "upper" and "lower" used in the following description refer to directions in the accompanying drawings, and the words "bottom surface" and "top surface", "inner" and "outer" refer to directions toward or away from the geometric center of a specific component, respectively.

[0015] Reference Figures 1 to 4 As shown, the method for automatically allocating slave addresses according to half-byte competition of the RS485 bus of this embodiment comprises the following steps: S1. Clock synchronization is performed between the host and multiple slaves; step calibration is performed between the host and the slaves; the clock error between the host and the slave is small enough to ensure that each step is synchronized during address allocation and will not be affected by the clock error; S2. The host generates an allocation address. Each slave device has a unique identifier. The host randomly obtains a unique identifier of a slave device and generates a slave allocation address according to the CRC32 check algorithm. S3, the host sends the assigned address in S2 to the RS-485 bus in mask form, and the slave competes for the valid half byte of the mask starting with the last (1+n) bit of its own unique identification code, where n is initially 0; S4. Determine whether the number of slaves that have successfully competed is 1: if it is greater than 1, n is increased by 1, and S3 is repeated for the slaves that have successfully competed; if it is 1, an address is allocated to the slave, and S2 is repeated for the remaining slaves; if it is 0, S2 is repeated.

[0016] Among them, if there are 3 slave devices, their respective unique identifiers are: Slave-1's SN is 516126486725, Slave-2's SN is 231545648966, and Slave-3's SN3 is 818115162656. The host sends a command to the slave, and randomly obtains the unique identifier of one of the slave devices (for example, the SN of Slave-3 is 231545648966). The host verifies the unique identifier of the slave device according to the CRC32 verification algorithm, and the result is 0xEECD8956, and uses this value as the assigned address of the slave; Then the above assigned address is sent to the RS-485 bus in the form of a mask. The three slave devices mounted on the bus begin to compete according to the valid half byte on the mask. The specific competition steps are as follows: Figure 3 As shown; If there are multiple slave devices in the first competition result, then continue to jump to S3, repeat the S3 operation, and realize the effective half-byte competition for the slave device that successfully competed for the first time, until only one slave device is competed out, and obtain the currently newly generated allocation address to the only slave device that successfully competed; If there is no slave device as a result of the competition, the host will generate a new assigned address.

[0017] For example: when allocating addresses to 100 slaves by bit competition, the clock reference is set to T=1us (HC32L17X series MCU, the clock frequency is up to 48MHz); 1. Initially, the synchronization clock calibration time between the master and slave is: 50T*500+pause time 1000T=26ms; 2. 100 devices compete bit by bit. The maximum time for one competition is 2T. The waiting time for the next competition is 500T. The maximum number of competitions for 100 devices is (1 / 2)*(1+100)*100=5050 times. The time for 100 slave devices to allocate addresses is: 5050*(2T+500T)=2535100ms. The time required for all slave devices to successfully assign addresses is 26ms+2535100ms=2535126ms.

[0018] When allocating addresses to 100 slaves based on half-byte competition: the clock reference is set to T=1us (HC32L17X series MCU, the clock frequency is up to 48MHz); 1. Initially, the synchronization clock calibration time between the master and slave is: 50T*500+pause time 1000T=26ms; 2. Assuming that the time for sending a single command through the serial port is T, the maximum time for a half-byte competition is 16T, the time to wait for the next competition is 500T, and the maximum number of competitions per device is 20 times (roughly assumed to be this value based on the number of half-byte competitions), the time for allocating addresses to 100 slave devices is: 100*20*(16T+500T) = 1032000ms; Compared with the bit-by-bit competition of 100 devices, the above-mentioned time consumption greatly improves the efficiency of address allocation and reduces the time of address allocation.

[0019] Reference Figure 1 As shown, in S2, the host randomly obtains a unique identification code of a slave device and generates an allocation address according to a CRC32 checksum algorithm.

[0020] Reference Figure 1 As shown, in S4, if the number of slaves that successfully compete is 1, an address is allocated to the slave. After the host receives a response from the slave that successfully competes, S2 is repeated for the remaining slaves to ensure that the slave obtains the address.

[0021] Reference Figure 1 As shown, S2.1 is located between S2 and S3, and determines whether the number of slaves is less than or equal to a. If the number of slaves is less than or equal to a, the slaves perform address allocation in a bit competition manner; if the number of slaves is greater than a, enter S3, wherein a is a natural number other than 0, and preferably, a can be any natural number from 5 to 10. When the number of slaves is small, the bit competition manner is adopted, which can reduce the number of cycles during competition, avoid the situation that the competing half bytes do not correspond to the mask for many times during half-byte competition, and can also improve the address allocation efficiency; If the last 10 devices compete bit by bit, that is, a is 10, the maximum time for one competition is 2T, and the time to wait for the next competition is 500T. However, the number of devices that successfully compete each time increases. The maximum number of times 10 slave devices compete is (1 / 2)*(1+10)*10=55 times. Then the time for the 10 slave devices to allocate addresses is: 55*(2T+500T)=27610ms.

[0022] In addition, the time for 90 half-byte competitions can also improve efficiency compared to the time consumed by address allocation with all half-byte competitions.

[0023] When the number of slave devices is small, the time spent on bit contention is short; when the number of slave devices is large, the time spent on half-byte contention is short.

[0024] Reference Figure 1 , Figure 2 As shown, it also includes S0, the host and the slave are powered on, the slaves powered on in different time periods are grouped, and enter S1 in the order of the groups, wherein there may be intervals between the slave power-on times. In order to avoid a long wait for the host and the slave, a power-on time period t1 is preset. Usually, the power-on time period is divided into two sections, and the time period t1 is a fixed time period. If all slaves are powered on within the time period t1, they enter S1 after the specific power-on time; If the power-on time of the slave exceeds t1, then when the t1 time period is reached, the addresses of the slaves powered on within the time period are allocated, and the subsequent slaves are no longer waiting for power-on. The subsequent slaves continue to be powered on, and the time period for the subsequent slaves to be powered on is t2.

[0025] It can reduce the time that the host and slave devices wait for power-on, allocate addresses to the slave devices that are powered on first, and improve the efficiency of address allocation.

[0026] S2.2, located after S2.1, determines whether the number of slaves in the current group is less than or equal to b. If the number of slaves in the current group is less than or equal to b, then the slaves in the next group are added. b is greater than a. In the process of allocating addresses to the slaves in the t1 time period, the number of slaves in the t1 time period is continuously reduced. When the number is reduced to the value of b, the slaves in the t2 time period are added and the addresses are allocated together with the slaves that have not completed the address allocation in the t1 time period; to improve the efficiency of the half-byte competition; Wherein, b is a natural number greater than a. If the value of a is 10, the value of b can be 30, 40 or 50. The specific value can be set according to the number of initial slaves.

[0027] The above are only preferred embodiments of the present invention. The protection scope of the present invention is not limited to the above embodiments. All technical solutions under the concept of the present invention belong to the protection scope of the present invention. It should be pointed out that for ordinary technicians in this technical field, some improvements and modifications without departing from the principle of the present invention should also be regarded as the protection scope of the present invention.

Claims

1. A method for automatically allocating slave addresses based on half-byte competition on RS485 bus, characterized in that: The following steps are involved: S1, clock synchronization between the host and multiple slaves; S2, the host generates an assigned address; S3, the host sends the assigned address in S2 to the RS-485 bus in mask form, and the slave competes for the valid half byte of the mask starting with the last (1+n) bit of its own unique identification code, where n is initially 0; S4, determine whether the number of slaves that successfully compete is 1: if it is greater than 1, then n is incremented by 1, and S3 is repeated for the slaves that successfully compete; If it is 1, the address is assigned to the slave and S2 is repeated for the remaining slaves; if it is 0, S2 is repeated.

2. The method of automatically allocating slave addresses by half-byte competition of RS485 bus according to claim 1, characterized in that: In S2, the host randomly obtains a unique identification code of a slave device and generates an allocation address according to the CRC32 checksum algorithm.

3. The method for automatically allocating slave addresses by half-byte competition of RS485 bus according to claim 1 is characterized in that: In S4, if the number of slaves that have successfully competed is 1, an address is allocated to the slave. After the host receives a response from the slave that has successfully competed, S2 is repeated for the remaining slaves.

4. The method of automatically allocating slave addresses by half-byte competition of RS485 bus according to claim 1 is characterized in that: S2.1, located between S2 and S3, determines whether the number of slaves is less than or equal to a. If the number of slaves is less than or equal to a, the slaves perform address allocation in a bit competition manner; if the number of slaves is greater than a, enter S3.

5. The method for automatically allocating slave addresses by half-byte competition of RS485 bus according to claim 1 is characterized in that: It also includes powering on S0, the host and the slaves, grouping the slaves powered on in different time periods, and entering S1 in the order of the groups.

6. The method for automatically allocating slave addresses by half-byte competition of RS485 bus according to claim 5 is characterized in that: S2.2, determine whether the number of slaves in the current group is less than or equal to b. If the number of slaves in the current group is less than or equal to b, add the slaves of the next group, and b is greater than a.