Automatic allocation method of slave addresses in master-slave communication, electronic equipment and medium

By introducing an automatic allocation method of slave address in the smart home system, the problem of cumbersome and error-prone problems of manual input of addresses is solved, and the host automatically allocates slave addresses is realized, which improves the system flexibility and reliability, and ensures the correct identification and communication of slave devices.

CN119788644APending Publication Date: 2025-04-08SHENZHEN C&D ELECTRONICS
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202411827310.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-12
Publication Date
2025-04-08

AI Technical Summary

Technical Problem

In smart home systems, manually entering slave addresses is cumbersome and prone to errors, especially when there are a large number of slave devices, which brings difficulties to the installation and use of users and products.

Method used

It provides an automatic allocation method for slave addresses in master-slave communication. After the host enters the automatic allocation mode, it issues an address reading command to the slave connected to the communication bus, and the slave generates a random address and sends it to the host. The host generates and allocates the slave addresses according to the number and order of random addresses received.

Benefits of technology

The host automatically allocates addresses to slaves, reducing the cumbersomeness and human errors of manual operations, improving the flexibility, reliability and maintenance of the system, avoiding address conflicts, and ensuring that each slave device can correctly identify and communicate.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119788644A_ABST
    Figure CN119788644A_ABST
Patent Text Reader

Abstract

The present application relates to master-slave communication technology, and discloses a method, electronic device, and medium for automatically allocating slave addresses in master-slave communication, including: when the host enters the automatic allocation mode of the slave address, an address read command is issued to the slave connected to the communication bus; when each slave receives the address read command, a random address is generated and sent to the host; the host generates a corresponding number of slave addresses according to the number of random addresses received; the host allocates and sends corresponding slave addresses to the slaves corresponding to each random address according to the order of the received random addresses; each slave saves the received slave address for master-slave communication with the host. The present application also discloses a control device. The present application aims to realize that in a system based on master-slave communication, the host can automatically allocate corresponding addresses to each slave.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of master-slave communication, and in particular to a method for automatically allocating slave addresses in master-slave communication, a control device, an electronic device, and a computer-readable storage medium. Background Art

[0002] In smart home systems, master-slave communication is a common communication method (such as master-slave communication based on RS485). In this communication mode, the host sends instructions to the slave through the bus, and the slave feeds back the execution results and reports relevant information through the same bus. In order to ensure that the host can accurately identify each slave device, each slave device needs to have a unique address.

[0003] At present, all slave addresses are generally entered into the host system by users or relevant engineers through manual input. In practical applications, when there are a large number of slave devices, manual input is not only cumbersome but also prone to errors, which brings great trouble to the installation and use of user products.

[0004] The above contents are only used to assist in understanding the technical solution of the present application and do not constitute an admission that the above contents are prior art. Summary of the invention

[0005] The main purpose of the present application is to provide a method, a control device, an electronic device and a computer-readable storage medium for automatically allocating slave addresses in master-slave communication, aiming to realize that in a system based on master-slave communication, the host can automatically allocate corresponding addresses to each slave.

[0006] To achieve the above object, the present application provides a method for automatically allocating slave addresses in master-slave communication, comprising the following steps:

[0007] When the host enters the automatic allocation mode of the slave address, it sends an address reading command to the slave connected to the communication bus;

[0008] When each slave receives an address read command, it generates a random address and sends it to the host;

[0009] The host generates a corresponding number of slave addresses based on the number of random addresses received;

[0010] The host allocates and sends the corresponding slave address to the slave corresponding to each random address according to the order of the received random addresses;

[0011] Each slave saves the received slave address for use in master-slave communication with the host.

[0012] Optionally, the slave starts timing after generating a random address, and sends the random address to the host when the timing meets the time-sharing reporting condition.

[0013] Optionally, the host sends address read commands to each communication interface of the communication bus at different timings, and the timing interval is greater than or equal to the time limit feedback time for the slave to feedback a random address.

[0014] Optionally, the method for automatically allocating slave addresses in master-slave communication further includes:

[0015] If the slave fails to successfully feedback the random address from the time it receives the address read command until the limited feedback time ends, it will wait until the total receiving and sending timing of the address read command ends before actively feedback the random address to the host.

[0016] The total receiving and sending timing starts from the issuance time of the first address read command and ends from the time-limited feedback time of the last address read command.

[0017] Optionally, after the step of each slave storing the received slave address for use in master-slave communication with the host, the step further includes:

[0018] When the host detects that a slave is connected to an idle communication interface, it allocates and sends a slave address to the currently connected slave based on the originally allocated slave address;

[0019] The host detects whether a slave is connected to the communication interface based on the electrical detection of the communication interface.

[0020] Optionally, when the host detects that a slave is connected to an idle communication interface, the step of allocating and sending a slave address to the currently connected slave based on the originally allocated slave address includes:

[0021] When the host detects that a slave is connected to an idle communication interface, and previously detected that a slave has been unplugged from the communication interface, it will ask the currently connected slave whether it has a slave address.

[0022] If the host receives the slave address fed back by the currently connected slave, it will bind the slave address to the currently connected communication interface;

[0023] If the host does not receive the random address fed back by the currently connected slave, it allocates and sends a slave address for the currently connected slave based on the originally allocated slave address.

[0024] Optionally, after the step of each slave storing the received slave address for use in master-slave communication with the host, the following steps are included:

[0025] When the slave needs to allocate a sub-slave address to the sub-slave, the slave is used as the host and the sub-slave is used as the slave, and the process returns to the step of sending an address read command to the slave connected to the communication bus when the host enters the automatic allocation mode of the slave address;

[0026] The sub-slave addresses are divided based on the slave addresses.

[0027] To achieve the above object, the present application also provides a control device, comprising:

[0028] The reading module is used to send an address reading command to the slave connected to the communication bus when the host enters the automatic allocation mode of the slave address;

[0029] The reporting module is used to generate a random address and send it to the host when each slave receives an address read command;

[0030] A generation module, used for the host to generate a corresponding number of slave addresses according to the number of random addresses received;

[0031] The allocation module is used for the host to allocate and send corresponding slave addresses to the slaves corresponding to each random address according to the order of the received random addresses;

[0032] The storage module is used for each slave to store the received slave address for use in master-slave communication with the host.

[0033] To achieve the above-mentioned purpose, the present application also provides an electronic device, which includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, and when the computer program is executed by the processor, the steps of the method for automatically allocating slave addresses in the master-slave communication as described above are implemented.

[0034] To achieve the above objectives, the present application also provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the method for automatically allocating slave addresses in the master-slave communication described above are implemented.

[0035] The automatic allocation method, control device, electronic device and computer-readable storage medium of slave addresses in master-slave communication provided by the present application can realize that the host can automatically allocate corresponding addresses to each slave in the system based on master-slave communication without manual allocation and entry, which not only reduces the tediousness of manual operation and simplifies the system deployment process, but also reduces the problems caused by human errors, and significantly improves the flexibility, reliability and maintainability of the system. At the same time, through a reasonable address generation and allocation mechanism, address conflicts can be effectively avoided to ensure that each slave device can correctly identify and communicate. BRIEF DESCRIPTION OF THE DRAWINGS

[0036] Figure 1 This is a schematic diagram of the steps of a method for automatically allocating slave addresses in master-slave communication in one embodiment of the present application;

[0037] Figure 2This is a schematic diagram of a control device in an embodiment of the present application;

[0038] Figure 3 A schematic diagram of the internal structure of an electronic device according to an embodiment of the present application.

[0039] The realization of the purpose, functional features and advantages of this application will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION

[0040] The embodiments of the present application are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the present application, and should not be construed as limiting the present application. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without creative work are within the scope of protection of the present application.

[0041] In addition, if the description of "first", "second", etc. is involved in this application, it is only used for descriptive purposes (such as for distinguishing the same or similar features), and cannot be understood as indicating or suggesting their relative importance or implicitly indicating the number of technical features indicated. Therefore, the features defined as "first" and "second" may explicitly or implicitly include at least one of the features. In addition, the technical solutions between the various embodiments can be combined with each other, but it must be based on the ability of ordinary technicians in this field to implement. When the combination of technical solutions is contradictory or cannot be implemented, it should be deemed that such combination of technical solutions does not exist and is not within the scope of protection required by this application.

[0042] Reference Figure 1 In one embodiment, a method for automatically allocating slave addresses in master-slave communication includes:

[0043] Step S10: When the host enters the automatic allocation mode of the slave address, an address read command is sent to the slave connected to the communication bus;

[0044] Step S20: When each slave receives an address read command, it generates a random address and sends it to the host;

[0045] Step S30, the host generates a corresponding number of slave addresses according to the number of random addresses received;

[0046] Step S40: the host allocates and sends corresponding slave addresses to slaves corresponding to each random address according to the order of the received random addresses;

[0047] Step S50: Each slave saves the received slave address for use in master-slave communication with the host.

[0048] In this embodiment, the execution terminal of the embodiment may be an electronic device, or other device or apparatus (such as a control device) for controlling the electronic device. Among them, the host and the slave in the smart home system based on master-slave communication may be corresponding electronic devices.

[0049] As described in step S10, the host may enter the slave address automatic allocation mode by hardware setting, software setting, status flag, etc.

[0050] The hardware setting may be on a hardware interface of the host (such as a pin or switch of a microcontroller), and may enter the automatic allocation mode through an external switch or jumper setting.

[0051] Among them, the software setting can be in the software system of the host, and the automatic allocation mode can be entered through a specific key combination, menu option or remote command (such as through a serial port or a network).

[0052] The status flag may be in a non-volatile memory (such as EEPROM) of the host, and may store a status flag indicating whether the host is currently in the automatic allocation mode. When the host is powered on, the software reads the flag and decides whether to enter the automatic allocation mode accordingly.

[0053] The host sends the address read command in broadcast mode through the communication bus (such as RS485). This means that the host sends the message to all slave devices on the bus. The address read command can contain the following parts:

[0054] Start byte: used to mark the beginning of a data frame, usually a specific byte or byte sequence;

[0055] Command code: used to identify the specific command type, such as address read command;

[0056] Data length: indicates the length of the subsequent data;

[0057] Check field: such as CRC check, used to ensure the correctness of data transmission;

[0058] End byte: used to mark the end of the data frame.

[0059] The specific content of the address read command may include the following information:

[0060] Instruction type: identifies this as an address read command;

[0061] Response requirement: Indicates that after receiving this command, the slave device needs to generate a random address and send it back to the host within the limited feedback time.

[0062] The host sends the address read command to the RS485 bus through the hardware serial port or UART module. In the bus driver circuit, the host controls the sending and receiving modes by controlling the DE (Driver Enable) and RE (Receiver Enable) signals. After sending the command, the host releases the bus control right and waits for the response of the slave device.

[0063] As described in step S20, the slave device receives the address read command from the host through a communication interface (such as UART). The protocol parsing module inside the slave is responsible for parsing various fields of the command, such as the start byte, command code, data length and check field.

[0064] Optionally, the slave can verify the validity of the received address read command, which includes checking whether the start byte is correct, whether the command code matches the address read command, whether the data length is reasonable, and whether the check field is consistent with the calculated check value. If the command verification passes, the slave switches the internal state to the ready response mode and starts the corresponding processing flow.

[0065] Optionally, the slave device contains a random number generator to generate a unique random address. The random number generator can be a software-based pseudo-random number generator (such as the rand() function in C language) or a hardware-based random number generator (such as using an internal clock or an external noise source).

[0066] The generated random address needs to be within a predefined range, which can be between the minimum and maximum values ​​of the slave address (such as between 1 and 255, assuming an 8-bit address is used). This ensures that the generated address is within a reasonable range and does not conflict with the address of other devices.

[0067] Optionally, the slave encapsulates the generated random address into a data frame to facilitate sending it back to the host via the communication bus. The data frame can contain the following parts:

[0068] Start byte: marks the beginning of the data frame;

[0069] Command code: identifies this as a random address response frame;

[0070] Data length: indicates the length of the subsequent data, usually the length of the random address;

[0071] Random address: A random address is generated as the payload of the data frame;

[0072] Check field: such as CRC check, used to ensure the correctness of data transmission;

[0073] End byte: marks the end of the data frame;

[0074] Frame construction: The slave device combines the above fields into a complete data frame according to the predefined protocol format.

[0075] Optionally, in a multi-slave system, in order to avoid bus conflicts caused by multiple slaves responding at the same time, the slave device can design a simple arbitration mechanism. For example, after generating a random address, each slave can randomly delay for a short period of time (such as a few milliseconds to tens of milliseconds) before sending a response.

[0076] The slave device sends the encapsulated data frame to the RS485 bus through the communication interface. The slave controls the sending and receiving modes by controlling the DE and RE signals. After the sending is completed, the slave releases the bus control right and waits for the next command from the host.

[0077] As described in step S30, the host receives the random address data frame sent by the slave device through the communication interface. The protocol parsing module of the host is responsible for parsing the received data frame, and the parsing process includes verifying the start byte, command code, data length and check field, and extracting the random address.

[0078] The host stores the parsed random address in a temporary data structure (such as an array or a linked list) for subsequent processing.

[0079] The master counts the random addresses received to calculate the total number of slave devices. This can be achieved by traversing a temporary data structure that stores random addresses.

[0080] Optionally, the host generates corresponding slave addresses based on the number of random addresses received. At the same time, the host generates a list of slave addresses, each address corresponding to a slave device. The address list can be stored in the host's memory for subsequent sending to the slave device.

[0081] As described in step S40, the host determines the order of slave address allocation according to the order of the received random addresses, which usually means that the first slave to respond will obtain the first slave address, the second slave to respond will obtain the second slave address, and so on.

[0082] The host sends an address assignment command to the slaves corresponding to each random address through the communication bus.

[0083] As described in step S50, the slave receives the address assignment command sent by the host through the communication interface. The protocol parsing module of the slave is responsible for parsing the received data frame, and the parsing process includes verifying the start byte, command code, data length and check field, and extracting the slave address.

[0084] Optionally, the slave saves the received slave address to its internal memory to ensure that the address information is not lost after power failure. The slave updates its internal state to reflect the new slave address so that the new address can be used in subsequent communications.

[0085] In this way, the host automatically identifies and assigns address numbers to the slaves, and the host automatically stores all slave address numbers, without the need for the user to manually enter the slave address numbers into the host. Among them, the slave automatically generates a random address number to solve the problem of no address number reported for the first time. In addition, the slave can automatically report the random address number in time-sharing to avoid bus conflicts; the host then renames the random address number of the slave to form the total slave address number.

[0086] In one embodiment, in a system based on master-slave communication, the host can automatically allocate corresponding addresses to each slave without manual allocation and entry, which not only reduces the tediousness of manual operation and simplifies the system deployment process, but also reduces the problems caused by human errors, significantly improving the flexibility, reliability and maintainability of the system. At the same time, through a reasonable address generation and allocation mechanism, address conflicts can be effectively avoided to ensure that each slave device can be correctly identified and communicated.

[0087] Automatic address allocation simplifies system deployment and user operations, reduces the risk of address conflicts, optimizes communication efficiency, and enhances system stability and security. It is particularly suitable for systems that need to handle a large number of slave devices and complex communication environments (such as smart home systems).

[0088] In one embodiment, based on the above embodiment, the slave starts timing after generating a random address, and sends the random address to the host when the timing meets the time-sharing reporting condition.

[0089] In this embodiment, in the master-slave communication system, after the slave device generates a random address, in order to avoid communication bus conflicts caused by multiple slaves responding at the same time, a time-sharing reporting mechanism can be used.

[0090] After the slave generates a random address, it starts an internal timer immediately. The purpose of the timer is to ensure that the slave does not send a response immediately after generating the address, but waits for a random period of time before sending.

[0091] The initial value of the timer may be a random short time interval (eg, a few milliseconds to tens of milliseconds), and the specific time interval may be set through system configuration.

[0092] The timer inside the slave continues to run until the timing reaches the preset time-sharing reporting condition. The time-sharing reporting condition can be a fixed threshold or a dynamically adjusted value, depending on the needs of the system.

[0093] When the timer reaches the time-sharing reporting condition, the slave determines whether it needs to send a random address. If the condition is met (for example, the timer value is greater than a certain threshold), the slave is ready to send the random address.

[0094] The slave sends the encapsulated data frame to the RS485 bus. After the transmission is completed, the slave releases the bus control right and waits for the next command from the host.

[0095] The mechanism that the slave starts timing after generating a random address and sends the random address to the host when the timing meets the time-sharing reporting conditions can effectively avoid bus conflicts caused by multiple slaves responding at the same time. This mechanism ensures that the slave sends random addresses at different time points, optimizes the utilization of the communication bus, and improves the stability and reliability of the system. It is particularly suitable for systems that need to handle a large number of slave devices and complex communication environments, such as smart home systems and industrial control systems.

[0096] In one embodiment, based on the above embodiment, the host sends address read commands to each communication interface of the communication bus at different timings, and the timing interval is greater than or equal to the time limit feedback time of the slave to feedback the random address.

[0097] In this embodiment, the master and the slave are connected via a physical communication interface (a communication bus is connected to each interface).

[0098] In a master-slave communication system, in order to avoid communication bus conflicts caused by multiple slaves responding at the same time, the host can adopt a strategy of issuing address read commands in time and step by step.

[0099] The host generates an address read command according to system requirements. The command structure includes the start byte, command code, data length, and checksum field to ensure the integrity and correctness of the command. The host can communicate with multiple slave devices through multiple communication interfaces (such as different network segments of the RS485 bus), and each communication interface is connected to a different slave.

[0100] Optionally, when the host sends the address read command, it can allocate different sending timings to each communication interface according to the priority of each communication interface or the preset timing rules. The timing planning can be static or dynamically adjusted, depending on the needs of the system.

[0101] The host sends address read commands to each communication interface in turn according to the timing plan. Each communication interface receives commands at different times, and ensures that the command sending time interval between different interfaces is greater than or equal to the limited feedback time of the random address feedback from the slave.

[0102] After receiving the command, the slave needs to send a random address to the host within the time limit. The time limit is preset to ensure that the slave has enough time to parse the command and generate a random address.

[0103] The host sends the address read command in time and step by step, and the slave devices connected to different communication interfaces receive the command at different time points. The time points when the slave generates and sends the random address are dispersed, thereby avoiding bus conflicts caused by multiple slaves responding at the same time. The timing interval between the address read commands sent by the host and the different communication interfaces is greater than or equal to the limited feedback time of the random address feedback from the slave, ensuring that each slave has enough time to process the command and send the random address without overlapping the response time of other slaves.

[0104] The host sends address read commands to each communication interface of the communication bus at different timings, and the timing interval is greater than or equal to the limited feedback time of the random address fed back by the slave, which can effectively avoid the bus conflict caused by multiple slaves responding at the same time. This mechanism ensures that slave devices connected to different communication interfaces receive commands and send random addresses at different time points, optimizes the utilization of the communication bus, and improves the stability and reliability of the system. It is particularly suitable for systems that need to handle a large number of slave devices and complex communication environments, such as smart home systems and industrial control systems.

[0105] In one embodiment, based on the above embodiment, the method for automatically allocating slave addresses in master-slave communication further includes:

[0106] If the slave fails to successfully feedback the random address from the time it receives the address read command until the limited feedback time ends, it will wait until the total receiving and sending timing of the address read command ends before actively feedback the random address to the host.

[0107] The total receiving and sending timing starts from the issuance time of the first address read command and ends from the time-limited feedback time of the last address read command.

[0108] In this embodiment, if the slave can successfully generate a random address and send it to the master within the limited feedback time, the slave address is successfully allocated. The slave retains the address and uses it in subsequent communications.

[0109] If the slave still fails to successfully feedback the random address at the end of the limited feedback time (for example, due to bus conflict or other communication problems), the slave enters the waiting state. After the total transmission and reception timing is completed, the slave actively feedbacks the random address to the host.

[0110] The total receiving and sending timing starts from the time when the first address read command is issued. The timing timing includes the sending and receiving process of all address read commands until the time-limited feedback time of the last address read command ends.

[0111] After waiting for the total receiving and sending sequence to end, the slave actively sends the previously generated random address to the master. At this time, other operations on the communication bus may have been completed, so bus conflicts can be avoided.

[0112] Through the total transceiver timing and time-limited feedback mechanism, the slave device receives commands and sends random addresses at different time points to avoid bus conflicts caused by multiple slaves responding at the same time. After the total transceiver timing ends, the slave actively feeds back the random address to the host to ensure that address allocation is completed without bus conflicts.

[0113] In one embodiment, based on the above embodiment, after the step of each slave storing the received slave address for use in the master-slave communication with the host, the step further includes:

[0114] When the host detects that a slave is connected to an idle communication interface, it allocates and sends a slave address to the currently connected slave based on the originally allocated slave address;

[0115] The host detects whether a slave is connected to the communication interface based on the electrical detection of the communication interface.

[0116] In this embodiment, the host monitors the electrical status of the communication interface periodically or continuously to detect whether a new slave device is connected.

[0117] When the host detects that a slave is connected to an idle communication interface, the host will identify the newly connected slave device. The host assigns a new slave address to the currently connected slave based on the originally assigned slave address. This usually means that the host will check the already assigned slave addresses to find an unused address range. The address can be assigned in increments based on the original address to ensure that it does not conflict with the existing slave address.

[0118] The host sends the assigned new slave address to the newly connected slave through the communication bus. This process is similar to the initial address assignment, including constructing a data frame containing the new address and sending it to the slave through the communication interface.

[0119] The newly connected slave device receives the new slave address sent by the master through the communication interface. The slave's protocol parsing module is responsible for parsing the received data frame, extracting the newly assigned slave address, and saving it to the internal memory. The slave updates its internal state to reflect the new slave address so that the new address can be used in subsequent communications.

[0120] In this way, the function of dynamically detecting and assigning addresses to newly connected slaves enables the system to flexibly respond to the dynamic access and removal of slaves, reducing the workload of system maintenance. Through this dynamic detection and address allocation method, when a new slave is connected, there is no need to re-assign addresses to all slaves. This is particularly important in systems that need to handle a large number of slave devices and complex communication environments, such as smart home systems or industrial automation systems. It reduces the complexity and maintenance costs of the system and can significantly improve the flexibility, reliability and maintainability of the system.

[0121] In one embodiment, based on the above embodiment, when the host detects that a slave is connected to an idle communication interface, the step of allocating and sending a slave address to the currently connected slave based on the originally allocated slave address includes:

[0122] When the host detects that a slave is connected to an idle communication interface, and previously detected that a slave has been unplugged from the communication interface, it will ask the currently connected slave whether it has a slave address.

[0123] If the host receives the slave address fed back by the currently connected slave, it will bind the slave address to the currently connected communication interface;

[0124] If the host receives the random address fed back by the currently connected slave, it will allocate and send a slave address for the currently connected slave based on the originally allocated slave address.

[0125] In this embodiment, the host detects whether a new slave device is connected to the idle communication interface by monitoring the electrical state of the communication interface. Meanwhile, if the host previously detects that a slave device has been unplugged from the communication interface, it knows that a slave address may be released.

[0126] Optionally, the host sends an inquiry command to the currently connected slave to inquire whether the slave already has a slave address. The inquiry command may include a specific identifier or query instruction for instructing the slave to respond with its current address information.

[0127] If the slave already has a slave address, it will feed that address back to the master via the communication interface. If the slave does not have a preset address, it can feed back a random address or a specific "no address" flag.

[0128] Optionally, if the host receives a valid slave address, the host binds the slave address to the currently connected communication interface and updates the system address table. For example, when a slave device is changed from one communication interface to another idle communication interface, its slave address should remain unchanged to ensure the continuity of communication and the correctness of data. The host binds the received slave address to the currently connected communication interface and records the address in the system address table to ensure that the slave device uses the original slave address to communicate on the new communication interface.

[0129] Optionally, if a random address or a "no address" flag is received, the host assigns a new slave address to the currently connected slave based on the originally assigned slave address. The address assignment may be incremented based on the original address to ensure that there is no conflict with the existing slave address. The host sends the assigned new slave address to the currently connected slave via the communication bus. This process includes constructing a data frame containing the new address and sending it to the slave via the communication interface. The protocol parsing module of the slave is responsible for parsing the received data frame, extracting the newly assigned slave address, and saving it to the internal memory.

[0130] By distinguishing the scenarios of binding the original address and the new address, address conflicts can be effectively avoided, ensuring that the address of each slave device in the system is unique. For the scenario where the original slave device changes its interface, binding the original address can maintain communication continuity, avoid data loss and system interruption. For the scenario where a new slave device is connected, binding the new address can flexibly respond to the addition of new devices, ensuring the scalability and maintainability of the system.

[0131] This method is particularly important in systems that need to handle a large number of slave devices and complex communication environments, such as smart home systems or industrial automation systems, and can significantly improve the flexibility, reliability and maintainability of the system. There is no need to re-assign addresses to all slaves, reducing system complexity and maintenance costs.

[0132] In one embodiment, based on the above embodiment, the step of each slave storing the received slave address for use in the master-slave communication with the host includes:

[0133] When the slave needs to allocate a sub-slave address to the sub-slave, the slave is used as the host and the sub-slave is used as the slave, and the process returns to the step of sending an address read command to the slave connected to the communication bus when the host enters the automatic allocation mode of the slave address;

[0134] The sub-slave addresses are divided based on the slave addresses.

[0135] In this embodiment, in the automatic address allocation method of the master-slave communication system, in addition to the master allocating addresses to the slaves, the slave can also act as a "master" to allocate addresses to the sub-slaves it manages.

[0136] When the slave needs to assign a sub-slave address to the sub-slave it manages, the slave is used as the "master" and the sub-slave is used as the "slave", and the process returns to the step of assigning an address from the master to the slave.

[0137] The slave simulates the role of the master in its internal logic and starts the address assignment process. This process is similar to the process of the master assigning addresses to the slave, but the specific operation is completed by the slave.

[0138] Among them, the sub-slave addresses are divided based on the slave addresses to ensure the uniqueness and hierarchical structure of the addresses. The slave, as the "master", sends address read commands to the sub-slaves it manages. The sub-slaves generate random addresses and send them back to the slaves. The slave generates corresponding sub-slave addresses based on the number and order of random addresses received. These addresses are usually part of the slave address to ensure the hierarchical structure of the address.

[0139] The slave allocates and sends the corresponding sub-slave addresses to the sub-slaves corresponding to each random address according to the order of the random addresses received. The slave sends the corresponding sub-slave addresses to each sub-slave in turn according to the order of the random addresses received. These addresses are divided based on the slave addresses to ensure the uniqueness of the addresses in the entire system.

[0140] After receiving the sub-slave address sent by the slave, the slave saves it to local storage. These addresses will be used for subsequent communication with the slave to ensure that the sub-slave can correctly respond to the slave's data transmission request.

[0141] In this way, the slave can act as a "master" to assign addresses to the slaves it manages, further expanding the hierarchical structure and flexibility of the system. Through this hierarchical address allocation method, the system can support more complex multi-level master-slave communication structures, improving the scalability and management efficiency of the system.

[0142] In addition, refer to Figure 2 In an embodiment of the present application, a control device Z10 is further provided, comprising:

[0143] The reading module Z11 is used to send an address reading command to the slave connected to the communication bus when the host enters the automatic allocation mode of the slave address;

[0144] Reporting module Z12, used for generating random addresses and sending them to the host when each slave receives an address read command;

[0145] A generating module Z13, used for the host to generate a corresponding number of slave addresses according to the number of random addresses received;

[0146] The allocation module Z14 is used for the host to allocate and send the corresponding slave address to the slave corresponding to each random address according to the order of the received random addresses;

[0147] The storage module Z15 is used for each slave to store the received slave address for use in the master-slave communication with the host.

[0148] Optionally, the control device Z10 may be a virtual control device (such as a virtual machine) or a physical device (such as a physical device other than an electronic device that can execute the corresponding method).

[0149] In addition, an electronic device is also provided in an embodiment of the present application. The internal structure of the electronic device can be as follows: Figure 3 As shown, it includes a processor, a memory, a communication interface and an input interface connected by a system bus. The processor is used to provide computing and control capabilities. The memory includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database is used to store data called by the computer program. The communication interface is used to communicate data with an external terminal. The input interface is used to receive a signal input by an external device. When the computer program is executed by the processor, a method for automatically allocating slave addresses in master-slave communication as described in the above embodiment is implemented.

[0150] Those skilled in the art will understand that Figure 3 The structure shown in the figure is only a block diagram of a part of the structure related to the present application scheme, and does not constitute a limitation on the electronic device to which the present application scheme is applied. For example, in some optional embodiments, the electronic device may also include an output interface (not shown in the figure), and the output interface is also connected to the system bus and is used to output corresponding signals to the peripheral device.

[0151] In addition, the present application also proposes a computer-readable storage medium, the computer-readable storage medium includes a computer program, and when the computer program is executed by a processor, the steps of the method for automatically allocating slave addresses in master-slave communication as described in the above embodiment are implemented. It can be understood that the computer-readable storage medium in this embodiment can be a volatile readable storage medium or a non-volatile readable storage medium.

[0152] In summary, the automatic allocation method, control device, electronic device and computer-readable storage medium for slave addresses in master-slave communication provided in the embodiments of the present application can realize that the host can automatically allocate corresponding addresses to each slave in a system based on master-slave communication without manual allocation and entry, which not only reduces the tediousness of manual operation and simplifies the system deployment process, but also reduces the problems caused by human errors, and significantly improves the flexibility, reliability and maintainability of the system. At the same time, through a reasonable address generation and allocation mechanism, address conflicts can be effectively avoided to ensure that each slave device can correctly identify and communicate.

[0153] Those of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media provided in this application and used in the embodiments may include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (SSRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.

[0154] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, device, article or method including a series of elements includes not only those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, device, article or method. In the absence of further restrictions, an element defined by the sentence "includes a ..." does not exclude the presence of other identical elements in the process, device, article or method including the element.

[0155] The above description is only a preferred embodiment of the present application, and does not limit the patent scope of the present application. Any equivalent structure or equivalent process transformation made using the contents of the present application specification and drawings, or directly or indirectly used in other related technical fields, are also included in the patent protection scope of the present application.

Claims

1. A method for automatically allocating slave addresses in master-slave communication, characterized in that: include: When the host enters the automatic allocation mode of the slave address, it sends an address reading command to the slave connected to the communication bus; When each slave receives an address read command, it generates a random address and sends it to the host; The host generates a corresponding number of slave addresses based on the number of random addresses received; The host allocates and sends the corresponding slave address to the slave corresponding to each random address according to the order of the received random addresses; Each slave saves the received slave address for use in master-slave communication with the host.

2. The method for automatically allocating slave addresses in master-slave communication according to claim 1, characterized in that: The slave starts timing after generating the random address, and sends the random address to the host when the timing meets the time-sharing reporting condition.

3. The method for automatically allocating slave addresses in master-slave communication according to claim 1, characterized in that: The host sends address read commands to each communication interface of the communication bus at different timings, and the timing interval is greater than or equal to the time limit feedback time of the slave to feedback the random address.

4. The method for automatically allocating slave addresses in master-slave communication according to claim 3, characterized in that: The method for automatically allocating slave addresses in the master-slave communication also includes: If the slave fails to successfully feedback the random address from the time it receives the address read command until the limited feedback time ends, it will wait until the total receiving and sending timing of the address read command ends before actively feedback the random address to the host. The total receiving and sending timing starts from the issuance time of the first address read command and ends from the time-limited feedback time of the last address read command.

5. The method for automatically allocating slave addresses in master-slave communication according to claim 1, characterized in that: After the step of each slave storing the received slave address for use in the master-slave communication with the host, the method further includes: When the host detects that a slave is connected to an idle communication interface, it allocates and sends a slave address to the currently connected slave based on the originally allocated slave address; The host detects whether a slave is connected to the communication interface based on the electrical detection of the communication interface.

6. The method for automatically allocating slave addresses in master-slave communication according to claim 5, characterized in that: When the host detects that a slave is connected to the idle communication interface, the step of allocating and sending a slave address to the currently connected slave based on the originally allocated slave address includes: When the host detects that a slave is connected to an idle communication interface, and previously detected that a slave has been unplugged from the communication interface, it will ask the currently connected slave whether it has a slave address. If the host receives the slave address fed back by the currently connected slave, it will bind the slave address to the currently connected communication interface; If the host does not receive the random address fed back by the currently connected slave, it allocates and sends a slave address for the currently connected slave based on the originally allocated slave address.

7. The method for automatically allocating slave addresses in master-slave communication according to any one of claims 1 to 6, characterized in that: The step of each slave storing the received slave address for use in master-slave communication with the host includes: When the slave needs to allocate a sub-slave address to the sub-slave, the slave is used as the host and the sub-slave is used as the slave, and the process returns to the step of sending an address read command to the slave connected to the communication bus when the host enters the automatic allocation mode of the slave address; The sub-slave addresses are divided based on the slave addresses.

8. A control device, characterized in that: include: The reading module is used to send an address reading command to the slave connected to the communication bus when the host enters the automatic allocation mode of the slave address; The reporting module is used to generate a random address and send it to the host when each slave receives an address read command; A generation module, used for the host to generate a corresponding number of slave addresses according to the number of random addresses received; The allocation module is used for the host to allocate and send corresponding slave addresses to the slaves corresponding to each random address according to the order of the received random addresses; The storage module is used for each slave to store the received slave address for use in master-slave communication with the host.

9. An electronic device, characterized in that: The electronic device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the computer program is executed by the processor, the steps of the method for automatically allocating slave addresses in master-slave communication as described in any one of claims 1 to 7 are implemented.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the method for automatically allocating slave addresses in master-slave communication according to any one of claims 1 to 7 are implemented.

Citation Information

Cited By

  • Electronic load slave address automatic allocation method based on CAN bus

    CN120547158A

  • Communication method, electronic device, computer program product and storage medium

    CN120934929A