Method, System, Device and Electronic Equipment for Allocating Network Addresses
The host competition and slave allocation are carried out through the CAN bus, and the network arbitration and number of response instructions and time stamps are used to determine the network address, which solves the problem of cumbersome and duplicate network address allocation in the prior art, and achieves fast and low-cost address allocation.
Patent Information
- Application Number
- CN202310312461.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-27
- Publication Date
- 2025-08-01
- Estimated Expiration
- 2043-03-27
AI Technical Summary
In the prior art, the allocation of network addresses through dial switches or random number generation algorithms has problems such as high labor costs, cumbersome processing procedures and prone to duplication.
The controller LAN bus (CAN bus) is used to compete and slave allocation, the host is determined through network arbitration, and the slave network address is determined based on the number of response instructions and timestamps to ensure that the address is not duplicated.
It realizes the rapid and convenient allocation of non-repetitive networking addresses to each functional unit, reducing labor costs and processing complexity, and avoiding the problem of address duplication.
Smart Images

Figure CN116319683B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communications, and in particular, to a method, system, device, and electronic device for allocating network addresses for networking. Background Art
[0002] In a system integrated with multiple functions, there are often multiple functional units. For example, in a high-power charging pile, there are often multiple power units inside. In practical applications, these power units need to be controlled separately. However, since the network addresses of each power unit cannot be determined, the power units cannot be controlled separately. In related technologies, to solve the above problems, generally, a DIP switch can be used to assign a network address to each power unit, or a network address can be randomly generated for each power unit according to the serial numbers of different power units. The first method requires manual operation and has a high cost. The second method, although it does not require manual participation, requires a relatively complex random number generation algorithm to participate, the processing process is relatively complex, and there may sometimes be duplicate random numbers.
[0003] In view of the above problems, no effective solution has been proposed yet. Summary of the Invention
[0004] Embodiments of the present application provide a method, system, device, and electronic device for allocating network addresses for networking, so as to at least solve the technical problems of high labor costs, cumbersome processing processes, and duplicate random numbers caused by using DIP switches or random number generation algorithms to generate network addresses in related technologies.
[0005] According to one aspect of the embodiments of the present application, a method for allocating network addresses for networking is provided, including: determining a plurality of functional units connected based on a Controller Area Network (CAN) bus in a target system, selecting a target unit as a host from the plurality of functional units through network arbitration, and determining other units except the target unit among the plurality of functional units as slaves; and determining a first network address of the host in the target system; sending a slave address instruction to each slave through the host at every predetermined period, where the slave address instruction carries a second network address, and the second network address and the first network address are non-repeating addresses encoded based on a predetermined order; determining the number of response instructions returned by each slave to the host within the predetermined period, where the response instruction is an instruction generated by the slave in response to the slave address instruction; and determining at least based on the number of response instructions the slaves to which the second network address is to be allocated.
[0006] Optionally, determining at least based on the number of response instructions the slaves to which the second network address is to be allocated includes: when it is determined that only one response instruction is received, determining to allocate the second network address to the slave that sends the response instruction.
[0007] Optionally, determining the slave devices to which the second network addresses are to be assigned based at least on the number of response instructions includes: when it is determined that there are multiple received response instructions, obtaining first timestamps carried in the response instructions returned by each slave device to the master device, where each slave device corresponds to one response instruction; determining the slave device corresponding to the minimum timestamp among the first timestamps as the slave device to which the second network address is to be assigned, and sending a slave device allocation instruction to the slave device to which the second network address is to be assigned, for allocating the second network address to the slave device, where the slave device allocation instruction carries at least the second network address.
[0008] Optionally, after sending the slave device allocation instruction to the slave device to which the second network address is to be assigned, the method further includes: obtaining the minimum timestamp carried in the slave device allocation instruction; determining the first timestamp carried in the response instruction corresponding to the slave device to which the second network address is to be assigned, and judging whether the first timestamp is equal to the minimum timestamp. If the judgment result indicates that the first timestamp is equal to the minimum timestamp, it is determined that the second network address is successfully allocated; if the judgment result indicates that the first timestamp is not equal to the minimum timestamp, the network address of the slave device is restored to the default network address.
[0009] Optionally, after allocating the second network address to the slave device, the method further includes: determining that the starting address of the pre-allocated address of the slave device is obtained by adding a predetermined value to the second network address, where the pre-allocated address is the network address to be allocated to the slave device.
[0010] Optionally, selecting one target unit as the master device from multiple functional units through network arbitration includes: Step 1-1: Starting a delay countdown simultaneously when powering on, and controlling the multiple functional units to send master device competition instructions to the controller area network at the end of the delay countdown; Step 1-2: Detecting that after the delay countdown ends, the units that have not sent master device competition instructions to the controller area network and have received master device competition instructions from other units withdraw from the master device competition; Step 1-3: Detecting that after the delay countdown ends, the units that have sent master device competition instructions to the controller area network and have received master device competition instructions from other units start the delay countdown again, and continue to send master device competition instructions to the controller area network at the end of the delay countdown; otherwise, determining that the unit that has sent master device competition instructions to the controller area network and has not received master device competition instructions from other units after the delay countdown ends is the target unit, and determining the target unit as the master device; Step 1-4: Through the above Steps 1-1 to 1-3, if the master device is not determined from the multiple functional units, repeat Steps 1-1 to 1-3 until a master device is generated.
[0011] Optionally, the method further includes deleting an offline slave in the following manner: Step 2-1: Detect the offline duration of each slave. When the offline duration is greater than a preset duration, send a slave deletion instruction to the slave to be deleted, where the slave deletion instruction carries a third networking address of the slave to be deleted; Step 2-2: Obtain the fourth networking address corresponding to the slave to be deleted, compare the third networking address with the fourth networking address. When the fourth networking address is less than the third networking address, no action is taken; when the fourth networking address is equal to the third networking address, delete the slave to be deleted; when the fourth networking address is greater than the third networking address, change the slave address corresponding to the slave to be deleted to the fourth networking address minus a predetermined value; Repeat Step 2-1 to Step 2-2 until all offline slaves are deleted.
[0012] Optionally, the method further includes deleting an offline master in the following manner: Detect the offline duration of the master. When the offline duration is greater than a preset duration, delete the master and determine a slave adjacent to the networking address of the master as the new master.
[0013] Optionally, the predetermined order includes: when the networking address is a numerical value, it is determined that the first networking address corresponding to the master is the smallest, the networking addresses corresponding to the slaves increase in sequence, and the difference between any two adjacent networking addresses is equal.
[0014] According to another aspect of the embodiments of the present application, there is also provided a system for allocating networking addresses, including: a plurality of functional units, which are used to communicate with each other through a controller area network bus. Among them, the master in the plurality of functional units is used to send a slave address instruction to the slaves in the plurality of functional units. After receiving the slave address instruction, the slave returns a response instruction to the master, and the slave address instruction carries a second networking address; a controller, which is electrically connected to the plurality of functional units based on the controller area network bus, is used to select a target unit as the master from the plurality of functional units through network arbitration, determine other units except the target unit in the plurality of functional units as slaves, and allocate a first networking address to the master. The second networking address and the first networking address are non-repeating addresses encoded based on a predetermined order; the controller is further used to determine the number of response instructions returned by each slave to the master within a predetermined period, and determine at least the slaves to which the second networking address is to be allocated based on the number of response instructions.
[0015] According to another aspect of the embodiments of the present application, there is also provided an apparatus for allocating network addresses, including: a first determination module, configured to determine a plurality of functional units connected based on a controller area network (CAN) bus in a target system, select a target unit as a host from the plurality of functional units through network arbitration, and determine other units except the target unit among the plurality of functional units as slaves; and determine a first network address of the host in the target system; a sending module, configured to send a slave address instruction to each slave through the host at a predetermined period interval, where the slave address instruction carries a second network address, and the second network address and the first network address are non-repeating addresses encoded based on a predetermined order; a second determination module, configured to determine the number of response instructions returned by each slave to the host within the predetermined period, where the response instruction is an instruction generated by the slave in response to the slave address instruction; a third determination module, configured to determine the slaves to which the second network address is to be allocated at least based on the number of response instructions.
[0016] According to another aspect of the embodiments of the present application, there is also provided a non-volatile storage medium, where the storage medium includes a stored program, and when the program runs, it controls the device where the storage medium is located to execute any method for allocating network addresses.
[0017] According to another aspect of the embodiments of the present application, there is also provided an electronic device, including: a processor; a memory for storing instructions executable by the processor; where the processor is configured to execute the instructions to implement any method for allocating network addresses.
[0018] In the embodiments of the present application, a method of host competition and slave allocation is adopted through a controller area network (CAN) bus. By determining each functional unit connected based on the CAN bus in the current system, determining a host through network arbitration from each functional unit, allocating a first network address to the host, then sending a slave address instruction to other slaves based on the host, and determining the second network address corresponding to the slave according to the response instruction of the slave, the purpose of allocating non-repeating network addresses to each functional unit is achieved, thereby realizing the technical effect of reducing labor costs and generating network addresses more quickly and conveniently, and further solving the technical problems of high labor costs, cumbersome processing procedures, and duplicate random numbers caused by using a DIP switch or a random number generation algorithm to generate network addresses in the related art. Description of the Drawings
[0019] The drawings described herein are used to provide a further understanding of the present application, and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation to the present application. In the drawings:
[0020] Figure 1It is a schematic flowchart of an optional method for allocating network addresses according to an embodiment of the present application;
[0021] Figure 2 It is a network architecture diagram based on the Controller Area Network (CAN) bus in an embodiment of the present application;
[0022] Figure 3 It is a schematic flowchart of the overall process of slave allocation in an embodiment of the present application;
[0023] Figure 4 It is a schematic flowchart of slave confirmation based on the minimum timestamp in an embodiment of the present application;
[0024] Figure 5 It is a schematic flowchart of the arbitration host in an embodiment of the present application;
[0025] Figure 6 It is a framework schematic diagram of deleting the functional unit corresponding to the slave in an embodiment of the present application;
[0026] Figure 7 It is a framework schematic diagram of deleting the host unit in an embodiment of the present application;
[0027] Figure 8 It is a schematic structural diagram of an optional device for allocating network addresses according to an embodiment of the present application;
[0028] Figure 9 It is a schematic structural diagram of an optional system for allocating network addresses according to an embodiment of the present application. Detailed implementation manners
[0029] In order to enable those skilled in the art to better understand the solution of the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present application.
[0030] It should be noted that the terms "first", "second", etc. in the description, claims and above-mentioned drawings of this application are used to distinguish similar objects, and do not necessarily describe a specific order or sequence. It should be understood that the data used in this way can be interchanged under appropriate circumstances, so that the embodiments of the present application described here can be implemented in an order other than those illustrated or described here. In addition, the terms "comprising" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device comprising a series of steps or units does not necessarily have to be limited to those steps or units clearly listed, but may include other steps or units not clearly listed or inherent to these processes, methods, products or devices.
[0031] For the convenience of those skilled in the art to better understand the related embodiments of this application, the following explanations are made for the technical terms involved in this application:
[0032] CAN-BUS is the CAN bus technology, and its full name is "Controller Area Network - BUS". The Can-Bus bus technology was first used for communication in weapon electronic systems such as airplanes and tanks. The application of this technology to civilian vehicles originated in Europe, and this bus network is used for the transmission of various sensor data on vehicles. CAN is a serial communication protocol bus for real-time applications. It can use twisted pairs to transmit signals and is one of the most widely used field buses in the world. The features of the CAN protocol include complete serial data communication, providing real-time support, a transmission rate of up to 1 Mb / s, and having 11-bit addressing and error detection capabilities.
[0033] According to an embodiment of the present application, an embodiment of a method for allocating network addresses is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. And although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order than here.
[0034] Figure 1 is a method for allocating network addresses according to an embodiment of the present application, as Figure 1 shown, the method includes the following steps:
[0035] Step S102, determine multiple functional units connected based on the controller area network bus in the target system, select a target unit as the host from the multiple functional units through network arbitration, and determine the other units except the target unit among the multiple functional units as slaves; and determine the first network address of the host in the target system;
[0036] Through the technical solution of the above step S102, a host can be determined from multiple functional units, and a networking address can be assigned to the host.
[0037] Optionally, the above functional units include but are not limited to: terminals, devices, and various functional modules, etc.
[0038] For example, in the case of a high-power charging pile, there are often multiple power units inside, and in actual applications, it is necessary to control each power unit separately.
[0039] Figure 2 It is a network architecture diagram based on the Controller Area Network (CAN) bus in the embodiment of the present application. As Figure 2 shown, the CAN bus is connected to each functional unit, and each functional unit can communicate through the CAN bus.
[0040] Step S104, send a slave address instruction to each slave through the host at every predetermined period, where the slave address instruction carries a second networking address, and the second networking address and the first networking address are non-repeating addresses encoded based on a predetermined order;
[0041] Through the technical solution of the above step S104, after the host and the slaves are determined, corresponding networking addresses can be assigned to each slave.
[0042] Optionally, the above predetermined period can be 100 ms. For example, the host can send a slave address instruction to the functional unit corresponding to each slave every 100 ms. It can be understood that in order to make the assignment speed faster and the efficiency better, the above predetermined period can be set to a shorter duration, for example, 80 ms.
[0043] Optionally, the second networking address and the first networking address are non-repeating addresses encoded based on a predetermined order. For example, if the first networking address is represented by A and the second networking address is represented by B, then A = 0 and B = 1.
[0044] Step S106, determine the number of response instructions returned by each slave to the host within a predetermined period, where the response instruction is an instruction generated by the slave in response to the slave address instruction;
[0045] Through the technical solution of the above step S106, the number of response instructions returned by each slave within a predetermined period can be obtained, and the assignment method of the networking address corresponding to the slave can be determined according to this number in the subsequent steps.
[0046] Optionally, the above predetermined period can be 100 ms. For example, the number of response instructions returned by each slave to the host within 100 ms can be obtained.
[0047] Step S108: Determine the slave devices to which the second network addresses are to be assigned based at least on the number of response instructions.
[0048] Through the technical solutions of the above steps S102 to S108, by adopting the method of host competition and slave assignment through the control area network (CAN) bus, by determining each functional unit connected based on the CAN bus in the current system, determining a host through network arbitration from each functional unit, and assigning a first network address to the host, then sending a slave address instruction to other slave devices based on the host, and determining the second network address corresponding to the slave device according to the response instruction of the slave device, the purpose of assigning non-repeating network addresses to each functional unit is achieved, thereby realizing the technical effect of reducing labor costs and generating network addresses more quickly and conveniently. Furthermore, the technical problems in the related art, such as the high labor costs caused by using DIP switches or random number generation algorithms to generate network addresses, the cumbersome processing process, and the repetition of obtained random numbers, are solved.
[0049] As an optional implementation manner, determining the slave devices to which the second network addresses are to be assigned based at least on the number of response instructions includes: when it is determined that there is only one received response instruction, determining to assign the second network address to the slave device that sends the response instruction. That is, when it is determined that there is only one response instruction received by the host, then due to its uniqueness, the network address can be directly assigned to this slave device.
[0050] It can be understood that in the actual application scenario, there may be multiple response instructions received within a predetermined period, and in this case, the slave devices no longer have uniqueness. Therefore, as another optional implementation manner, determining the slave devices to which the second network addresses are to be assigned based at least on the number of response instructions can also, when it is determined that there are multiple received response instructions, obtain the first timestamps carried by the response instructions returned by each slave device to the host, where the slave device and the response instruction are in one-to-one correspondence; determine the slave device corresponding to the minimum timestamp in the first timestamps as the slave device to be assigned the second network address, and send a slave assignment instruction to the slave device to be assigned the second network address, for use in assigning the second network address to the slave device, where the slave assignment instruction carries at least the second network address. It is easy to note that the above minimum timestamp means the shortest time. Therefore, through the above implementation manner, the slave device with the fastest response within the predetermined period can be used as the target slave device and then the network address is assigned, while ignoring the requests of other slave devices to apply for network addresses. Through the above technical solutions, even if multiple slave devices return response instructions to the host within the predetermined period, multiple non-unique slave devices can also be converted into a single determined and unique slave device.
[0051] In order to further ensure the accurate allocation of networking addresses and avoid misallocation, in some embodiments of the present application, after sending a slave allocation instruction to a slave to be allocated a second networking address, the minimum timestamp carried by the slave allocation instruction can be obtained; determine the first timestamp carried by the response instruction corresponding to the slave to be allocated the second networking address, and determine whether the first timestamp is equal to the minimum timestamp. If the judgment result indicates that the first timestamp is equal to the minimum timestamp, it is determined that the second networking address is successfully allocated; if the judgment result indicates that the first timestamp is not equal to the minimum timestamp, the networking address of the slave is restored to the default networking address. Through the above implementation method, the timestamp carried by the response instruction returned by the slave to the host can be compared with the timestamp stored by the slave itself. If the comparison is consistent, it can be determined that the networking address batch is correct. Since in the above embodiment, the host allocates the networking address to the slave corresponding to the minimum timestamp, it is necessary to compare the timestamp stored by the slave corresponding to the minimum timestamp with this minimum timestamp to ensure accurate allocation.
[0052] Figure 3 and Figure 4 are respectively the schematic diagram of the overall process of slave allocation and the schematic diagram of slave confirmation based on the minimum timestamp in an embodiment of the present application. As Figure 3 、 Figure 4 shown, slave allocation and slave confirmation based on the minimum timestamp mainly include:
[0053] S3.1. The host sends a broadcast slave address instruction (0X2B), which is broadcast once every 100 ms and carries the pre-allocated address A.
[0054] S3.2. For a slave with a default address of 0XFF, if it receives the broadcast slave address instruction and does not receive other slave response instructions (exits the competition for this slave address), it responds to the broadcast slave instruction and carries its own response timestamp T1. At this time, this slave address is the pre-slave address A broadcast by the host.
[0055] S3.3. When the host finishes receiving the slave response instructions within 100 ms, if only one frame of response instruction is received, the current slave address is successfully allocated, and the pre-slave address is incremented by 1; if the number of received response frames is greater than 1, select a slave with the minimum timestamp T2 from the received response frames and send a specified slave allocation instruction (0X2C). The current slave address is successfully allocated, and the pre-slave address is incremented by 1; if no response frame is received, jump to S2.1.
[0056] S3.4. Determine whether the timestamp T2 carried by the received instruction slave allocation instruction is equal to T1 for a specific slave. If they are equal, it is considered that the slave has successfully obtained the slave address A; otherwise, the default slave address 0XFF is restored.
[0057] S3.5. Jump to S3.1.
[0058] In some embodiments of the present application, after the second network address is assigned to the slave device, it can be determined that the starting address of the pre-assigned address of the slave device is the second network address plus a predetermined value, where the pre-assigned address is the network address to be assigned to the slave device.
[0059] For example, if the first network address corresponding to the master device is 0 and the second network address is 1, the starting address of the pre-assigned address available for subsequent slave devices will be 2. Similarly, after the network address 2 is assigned to a certain slave device, the starting address of the pre-assigned address will be 3, and so on, which will not be elaborated here.
[0060] In order to ensure the uniqueness of the determined master device and avoid generating multiple master devices, resulting in chaotic network address allocation, in some alternative embodiments of the present application, a target unit is selected from multiple functional units as the master device through network arbitration, which can be achieved through the following steps. Specifically:
[0061] Step 1-1: Start a delay countdown while powering on, and control multiple functional units to send a master device competition instruction to the Controller Area Network when the delay countdown ends.
[0062] It should be noted that the above delay countdown can be set to 1S, and after the 1S countdown ends, multiple functional units are allowed to send a master device competition instruction to the control local area network.
[0063] Step 1-2: Detect that after the delay countdown ends, the unit that has not sent a master device competition instruction to the Controller Area Network and has received a master device competition instruction from other units withdraws from the master device election;
[0064] That is, at the end of the delay countdown, the functional unit that has not sent a master device competition instruction can be considered to have given up the election for the master device.
[0065] Step 1-3: Detect that after the delay countdown ends, the unit that has sent a master device competition instruction to the Controller Area Network and has received a master device competition instruction from other units starts a new delay countdown, and continues to send a master device competition instruction to the Controller Area Network when the delay countdown ends; otherwise, determine that the unit that has sent a master device competition instruction to the Controller Area Network and has not received a master device competition instruction from other units at the end of the delay countdown is the target unit, and determine the target unit as the master device;
[0066] Step 1-4: Through the above steps 1-1 to 1-3, if no master device is determined from multiple functional units, repeat steps 1-1 to 1-3 until a master device is generated.
[0067] Figure 5It is a schematic flow diagram of an arbitration host in an embodiment of the present application. As Figure 5 shown, the process includes:
[0068] S5.1: After power-on, all functional units send a host competition instruction (0X2A) to the CAN network after a 1-second delay.
[0069] S5.2: A unit that has not had time to send a host competition instruction and has received any network formation instruction on the bus will withdraw from the host competition.
[0070] S5.3: A unit that has sent a host competition instruction and has received a host competition instruction again will continue to send the host competition instruction after a 1S delay, otherwise it will become the host.
[0071] S5.4: If no host is generated, steps S5.1 to S5.3 are continuously repeated.
[0072] In an actual scenario, some functional units may go offline (no longer in use, or need to be updated and suspended). At this time, since these functional units still occupy network formation addresses, it will cause waste of network formation address resources. Therefore, in some optional embodiments of the present application, the offline slave can be deleted through the following steps. Specifically:
[0073] Step 2-1: Detect the offline duration of each slave. When the offline duration is greater than the preset duration, send a slave deletion instruction to the slave to be deleted. The slave deletion instruction carries the third network formation address of the slave to be deleted.
[0074] Optionally, the above preset duration can be set to 300 ms. Therefore, when the offline duration of a certain slave is greater than 300 ms, a slave deletion instruction can be sent to delete the slave and reclaim the network formation address of the slave.
[0075] Step 2-2: Obtain the fourth network formation address corresponding to the slave to be deleted, compare the third network formation address with the fourth network formation address. If the fourth network formation address is less than the third network formation address, no action is taken; if the fourth network formation address is equal to the third network formation address, the slave to be deleted is deleted; if the fourth network formation address is greater than the third network formation address, change the slave address corresponding to the slave to be deleted to the fourth network formation address minus a predetermined value.
[0076] Through the above technical solution, the slave to be deleted can be deleted, the network formation address of the deleted slave can be reclaimed, and then the network formation addresses of each slave can be reallocated.
[0077] For example, C represents the third network address, and D represents the fourth network address. When D < C, D can be left unchanged (i.e., the network address of the slave remains the same). When D = C, it means that the slave is the one to be deleted and its network address is to be reclaimed. Then, the slave is deleted and the network address corresponding to the slave is reclaimed. Similarly, when D > C, the slave address needs to be changed to D - 1 (i.e., the slave to be deleted is before the slave corresponding to D. Since the previous slave has been deleted, the subsequent slave needs to fill the position. Therefore, the slave address is changed to D - 1).
[0078] Repeat steps 2-1 to 2-2 until all offline slaves are deleted.
[0079] Similarly, for the master, the master can also be deleted according to the offline duration of the master. Optionally, the offline master can be deleted in the following way: Detect the offline duration of the master. When the offline duration is greater than the preset duration, the master is deleted, and the slave adjacent to the network address of the master is determined as the new master.
[0080] It should be noted that the predetermined order includes: when the network address is a numerical value, it is determined that the first network address corresponding to the master is the smallest, and the respective network addresses of the slaves increase in sequence, and the difference between any two adjacent network addresses is equal. For example, if the first network address corresponding to the master is 0, the pre-allocated slave addresses of the slaves can be 1, 2, 3, 4, 5, 6...
[0081] It also should be noted that when the network address is in other non-numerical forms, for example, the network addresses are A, B, C, D, E, F, G..., the above-mentioned first network address can be A, and the pre-allocated slave addresses can be B, C, D, E, F, G.... That is, the above-mentioned network addresses are addresses encoded according to a certain rule. This rule can be a numerical relationship from small to large, or an order relationship from front to back in a certain data table (such as the alphabet).
[0082] For example, the above-mentioned function unit corresponding to the deleted slave (abbreviated as the slave unit) can be implemented through the following steps:
[0083] When the master discovers that a certain slave is offline for more than 300 ms, it issues a command (0X2D) to delete the slave and carries the slave address C of the slave to be deleted. Then, the pre-allocated slave address A = A - 1.
[0084] S6.1. When the slave receives the command 0X2D, it obtains the slave address C of the slave to be deleted and compares it with its own slave address D. If D < C, no action is taken; if D > C, the slave address is changed to D - 1.
[0085] S6.2. Continuously repeat S6.1 and S6.2 until all offline slaves are deleted. Figure 6 It is a schematic framework diagram of the functional unit corresponding to the slave being deleted in the embodiment of the present application. As Figure 6 shown, the gray part (the part with gray scale) is the slave unit to be deleted. The above S6.1 and S6.2 are executed twice in total, and slave units 2 and 4 are deleted.
[0086] For example, the deletion of the functional unit corresponding to the master (abbreviated as the master unit) can be achieved through the following steps:
[0087] S7.1. When a slave unit detects that the master is offline for more than 300 ms, the slave address closest to the master address will become the new master (assuming the master address is 0, the closest slave address is 1. If the slave corresponding to slave address 1 is also offline, it is slave address 2, and so on). Other slaves are not processed.
[0088] S7.2. The new master executes the steps of deleting the slave unit, that is, the above steps S6.1 to S6.2 to delete the slaves to be deleted, and updates the networking addresses of each slave. Figure 7 It is a schematic framework diagram of the master unit being deleted in the embodiment of the present application. As Figure 7 shown, the gray part (the part with gray scale) is the master unit and slave units to be deleted. The above S7.1 is executed once to delete master unit 0, and the steps S6.1 to S6.2 are executed twice to delete slave units 1 and 2.
[0089] Figure 8 It is a device for allocating networking addresses according to the embodiment of the present application. As Figure 8 shown, the device includes:
[0090] The first determination module 80 is configured to determine multiple functional units connected based on a controller area network bus in the target system, select a target unit as the master from the multiple functional units through network arbitration, determine the other units except the target unit among the multiple functional units as slaves; and determine the first networking address of the master in the target system;
[0091] The sending module 82 is configured to send a slave address instruction to each slave through the master at every predetermined period. The slave address instruction carries a second networking address, and the second networking address and the first networking address are non-repeating addresses encoded based on a predetermined order;
[0092] The second determination module 84 is configured to determine the number of response instructions returned by each slave to the master within a predetermined period. The response instruction is an instruction generated by the slave in response to the slave address instruction;
[0093] A third determination module 86, configured to determine a slave device to which a second network address is to be allocated, based at least on the number of response instructions.
[0094] In the device for allocating network addresses, a first determination module 80 is configured to determine multiple functional units connected via a controller area network bus in a target system, select a target unit as a host from the multiple functional units through network arbitration, and determine other units except the target unit among the multiple functional units as slave devices; and determine a first network address of the host in the target system; a sending module 82 is configured to send a slave device address instruction to each slave device through the host at each predetermined period, where the slave device address instruction carries a second network address, and the second network address and the first network address are non-repeating addresses encoded based on a predetermined order; a second determination module 84 is configured to determine the number of response instructions returned by each slave device to the host within a predetermined period, where the response instruction is an instruction generated by the slave device in response to the slave device address instruction; a third determination module 86 is configured to determine a slave device to which a second network address is to be allocated, based at least on the number of response instructions, achieving the purpose of allocating non-repeating network addresses to each functional unit, thereby realizing the technical effect of reducing labor costs and generating network addresses more quickly and conveniently, and further solving the technical problems of high labor costs, cumbersome processing procedures, and repetition of random numbers caused by using a DIP switch or a random number generation algorithm to generate network addresses in the related art.
[0095] Figure 9 It is a schematic structural diagram of a system for allocating network addresses according to an embodiment of the present application, as Figure 9 shown, the system includes:
[0096] Multiple functional units 90, which are used to communicate with each other via a controller area network bus. Among them, the host in the multiple functional units is used to send a slave device address instruction to the slave devices in the multiple functional units. After receiving the slave device address instruction, the slave device returns a response instruction to the host, and the slave device address instruction carries a second network address;
[0097] A controller 92 is electrically connected to the multiple functional units based on the controller area network bus, and is configured to select a target unit as a host from the multiple functional units through network arbitration, determine other units except the target unit among the multiple functional units as slave devices, and allocate a first network address to the host. The second network address and the first network address are non-repeating addresses encoded based on a predetermined order; the controller is further configured to determine the number of response instructions returned by each slave device to the host within a predetermined period, and determine a slave device to which a second network address is to be allocated, based at least on the number of response instructions.
[0098] In this system, there are multiple functional units 90. The multiple functional units are used to communicate with each other via a Controller Area Network (CAN) bus. Among them, the host in the multiple functional units is used to send a slave address instruction to the slaves in the multiple functional units. After receiving the slave address instruction, the slave returns a response instruction to the host. The slave address instruction carries a second network address. The controller 92 is electrically connected to the multiple functional units based on the CAN bus. It is used to select a target unit as the host from the multiple functional units through network arbitration, and determine the other units except the target unit in the multiple functional units as slaves. And it assigns a first network address to the host. The second network address and the first network address are non-repeating addresses encoded based on a predetermined order. The controller is also used to determine the number of response instructions returned by each slave to the host within a predetermined period, and determine at least the slaves to which the second network address is to be assigned based on the number of response instructions. This achieves the purpose of assigning non-repeating network addresses to each functional unit, thus realizing the technical effect of reducing labor costs and generating network addresses more quickly and conveniently. Furthermore, it solves the technical problems in the related art, such as the high labor costs caused by using DIP switches or random number generation algorithms to generate network addresses, the cumbersome processing process, and the repetition of random numbers obtained.
[0099] According to another aspect of the embodiments of the present application, there is also provided a non-volatile storage medium. The storage medium includes a stored program. When the program runs, it controls the device where the storage medium is located to execute any method for allocating network addresses.
[0100] Specifically, the above storage medium is used to store program instructions with the following functions to implement the following functions:
[0101] Determine multiple functional units connected based on the CAN bus in the target system, select a target unit as the host from the multiple functional units through network arbitration, and determine the other units except the target unit in the multiple functional units as slaves; and determine the first network address of the host in the target system; send a slave address instruction to each slave through the host every predetermined period. The slave address instruction carries a second network address. The second network address and the first network address are non-repeating addresses encoded based on a predetermined order; determine the number of response instructions returned by each slave to the host within a predetermined period. The response instruction is an instruction generated by the slave in response to the slave address instruction; determine at least the slaves to which the second network address is to be assigned based on the number of response instructions.
[0102] Optionally, in this embodiment, the above storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices, or equipment, or any suitable combination of the above. More specific examples of the above storage medium would include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the above.
[0103] According to an embodiment of the present application, an electronic device is provided, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein, the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the method of allocating a networking address in any of the above.
[0104] Optionally, the above electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the above processor, and the input device and the output device are connected to the above processor.
[0105] The serial numbers of the above embodiments of the present application are only for description and do not represent the advantages or disadvantages of the embodiments.
[0106] In the above embodiments of the present application, the descriptions of each embodiment have their own emphases. For parts not detailed in a certain embodiment, reference may be made to the relevant descriptions of other embodiments.
[0107] In several embodiments provided by the present application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only illustrative. For example, the division of the units can be a logical function division. In actual implementation, there can be other division methods. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed couplings or direct couplings or communication connections to each other can be through some interfaces. The indirect couplings or communication connections of the units or modules can be in electrical or other forms.
[0108] The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they can be located in one place, or distributed to multiple units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0109] In addition, each functional unit in various embodiments of the present application may be integrated into one processing unit, or multiple functional units may exist physically alone, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of a software functional unit.
[0110] If the above-mentioned integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it may be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, may be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in various embodiments of the present application. The foregoing storage medium includes: various media such as USB flash drives, read-only memories (ROMs), random access memories (RAMs), mobile hard disks, magnetic disks, or optical discs that can store program codes.
[0111] The above are only the preferred embodiments of the present application. It should be noted that for those of ordinary skill in the art, without departing from the principle of the present application, several improvements and refinements can be made, and these improvements and refinements should also be regarded as the protection scope of the present application.
Claims
1. A method for allocating network addresses, characterized in that, Including: Determine multiple functional units connected based on a Controller Area Network (CAN) bus in the target system, select one target unit as the host from the multiple functional units through network arbitration, and determine the other units except the target unit among the multiple functional units as slaves; And determine the first networking address of the host in the target system; Send slave address instructions to each slave through the host at every predetermined period, where the slave address instructions carry a second networking address, and the second networking address and the first networking address are non-repeating addresses encoded based on a predetermined order; Determine the number of instructions of the response instructions returned by each slave to the host within the predetermined period, where the response instructions are instructions generated by the slave in response to the slave address instructions; Determine the slave to which the second networking address is to be allocated based at least on the number of instructions of the response instructions.
2. The method according to claim 1, wherein Determine the slave to which the second networking address is to be allocated based at least on the number of instructions of the response instructions, including: In the case where it is determined that only one response instruction is received, determine to allocate the second networking address to the slave that sends the response instruction.
3. The method according to claim 1, wherein Determine the slave to which the second networking address is to be allocated based at least on the number of instructions of the response instructions, including: In the case where it is determined that multiple response instructions are received, obtain the first timestamps carried by the response instructions returned by each slave to the host, where the slave corresponds to the response instruction one by one; Determine the slave corresponding to the minimum timestamp among the first timestamps as the slave to be allocated the second networking address, and send a slave allocation instruction to the slave to be allocated the second networking address for allocating the second networking address to the slave, where the slave allocation instruction carries at least the second networking address.
4. The method according to claim 3, characterized in that After sending the slave allocation instruction to the slave to be allocated the second networking address, the method further includes: Obtain the minimum timestamp carried by the slave allocation instruction; Determine the first timestamp carried by the response instruction corresponding to the slave to be allocated the second networking address, and determine whether the first timestamp is equal to the minimum timestamp. In the case where the determination result indicates that the first timestamp is equal to the minimum timestamp, it is determined that the second networking address is successfully allocated; In the case where the determination result indicates that the first timestamp is not equal to the minimum timestamp, restore the networking address of the slave to the default networking address.
5. The method according to claim 3, characterized in that After allocating the second networking address to the slave, the method further includes: Determine that the starting address of the pre-allocated address of the slave is obtained by adding a predetermined value to the second networking address, where the pre-allocated address is the networking address to be allocated to the slave.
6. The method according to claim 1, characterized in that Selecting one target unit as the host from the multiple functional units through network arbitration includes: Step 1-1: Start a delay countdown while powering on, and control the multiple functional units to send host competition instructions to the Controller Area Network when the delay countdown ends; Step 1-2: Detect that after the delay countdown ends, the unit that has not sent the host competition instruction to the Controller Area Network (CAN) and has received the host competition instruction from other units exits the host election; Step 1-3: Detect that after the delay countdown ends, the unit that has sent the host competition instruction to the CAN and has received the host competition instruction sent by other units restarts the delay countdown. After the delay countdown ends, continue to send the host competition instruction to the CAN; otherwise, determine that the unit that has sent the host competition instruction to the CAN and has not received the host competition instruction sent by other units after the delay countdown ends is the target unit, and determine that the target unit is the host; Step 1-4: Through the above steps 1-1 to 1-3, if the host is not determined from the multiple functional units, repeat steps 1-1 to 1-3 until the host is generated.
7. The method according to claim 1, characterized in that The method further includes deleting offline slaves in the following manner: Step 2-1: Detect the offline duration of each slave. When the offline duration is greater than the preset duration, send a slave deletion instruction to the slave to be deleted, where the slave deletion instruction carries the third networking address of the slave to be deleted; Step 2-2: Obtain the fourth networking address corresponding to the slave to be deleted, compare the third networking address with the fourth networking address. If the fourth networking address is less than the third networking address, do nothing; if the fourth networking address is equal to the third networking address, delete the slave to be deleted; if the fourth networking address is greater than the third networking address, change the slave address corresponding to the slave to be deleted to the fourth networking address minus a predetermined value; Repeat steps 2-1 to 2-2 until all offline slaves are deleted.
8. The method according to claim 1, wherein The method further includes deleting offline hosts in the following manner: Detect the offline duration of the host. When the offline duration is greater than the preset duration, delete the host and determine the slave adjacent to the networking address of the host as the new host.
9. The method according to any one of claims 1 to 8, characterized in that, The predetermined order includes: When the networking address is a numerical value, it is determined that the first networking address corresponding to the host is the smallest, the networking addresses corresponding to the slaves increase in sequence, and the difference between any two adjacent networking addresses is equal.
10. A system for allocating network addresses, characterized in that, Includes: Multiple functional units, which are used to communicate with each other through a Controller Area Network (CAN) bus. Among them, the host in the multiple functional units is used to send a slave address instruction to the slaves in the multiple functional units. After receiving the slave address instruction, the slave returns a response instruction to the host, and the slave address instruction carries the second networking address; A controller, electrically connected to the multiple functional units based on a Controller Area Network (CAN) bus, is configured to select a target unit from the multiple functional units as the host through network arbitration, determine the other units among the multiple functional units except the target unit as the slaves, and assign a first network address to the host. The second network address and the first network address are non-repeating addresses encoded based on a predetermined order. The controller is further configured to determine the number of instructions of the response instructions returned by each slave to the host within the predetermined period, and determine the slave to which the second network address is to be assigned at least based on the number of instructions of the response instructions.
11. A device for allocating network addresses, characterized in that, It includes: A first determination module, configured to determine multiple functional units connected based on a Controller Area Network (CAN) bus in a target system, select a target unit from the multiple functional units as the host through network arbitration, and determine the other units among the multiple functional units except the target unit as the slaves; and determine the first network address of the host in the target system; A sending module, configured to send a slave address instruction to each slave through the host at intervals of a predetermined period, where the slave address instruction carries a second network address, and the second network address and the first network address are non-repeating addresses encoded based on a predetermined order; A second determination module, configured to determine the number of instructions of the response instructions returned by each slave to the host within the predetermined period, where the response instruction is an instruction generated by the slave in response to the slave address instruction; A third determination module, configured to determine the slave to which the second network address is to be assigned at least based on the number of instructions of the response instructions.
12. A non-volatile storage medium, characterized in that, The storage medium includes a stored program, where when the program runs, it controls the device where the storage medium is located to execute the method for assigning network addresses according to any one of claims 1 to 9.
13. An electronic device, characterized in that, It includes: A processor; A memory for storing executable instructions of the processor; wherein the processor is configured to execute the instructions to implement the method for assigning network addresses according to any one of claims 1 to 9.
Citation Information
Patent Citations
Adaptive address determining networking circuit suitable for RS485 bus and method thereof
CN109150680A
Automatic equipment adding method, device and system, equipment and storage medium
CN113946369A