RS485 bus modbus automatic address configuration method and system

By initiating address allocation broadcasts from the master device and using a chain-like hierarchical allocation mechanism, the problem of error-prone address configuration for slave devices in the Modbus protocol is solved. This enables automated address allocation and plug-and-play functionality, improving system stability and deployment efficiency, and simplifying the operation and maintenance process.

CN122348940APending Publication Date: 2026-07-07GUANGDONG YADA ELECTRONICS

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
GUANGDONG YADA ELECTRONICS
Filing Date
2026-05-15
Publication Date
2026-07-07

AI Technical Summary

Technical Problem

The existing Modbus protocol relies on manual operation for device address configuration, which is prone to errors, leading to address conflicts, low deployment efficiency, and manual intervention required for device replacement. It cannot adapt to the addition or removal of devices or topology changes. The existing automatic solution lacks stability and efficiency in complex environments.

Method used

The master device initiates address allocation broadcasts, and through an independently hardware-isolated RS485 transceiver and a chain-like hierarchical allocation mechanism, it enables automatic address configuration of slave devices, supports device replacement without manual intervention, and is compatible with the Modbus RTU protocol.

Benefits of technology

It avoids address conflicts, improves deployment efficiency and system stability, simplifies operation and maintenance processes, enables plug-and-play functionality, reduces operation and maintenance costs, and enhances the system's ability to adapt to complex industrial environments.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122348940A_ABST
    Figure CN122348940A_ABST
Patent Text Reader

Abstract

The application discloses a kind of RS485 bus Modbus automatic address configuration method and system, comprising: main equipment initialization address pool, and sends Modbus address allocation broadcast instruction;First slave device power-on enters configuration mode, detects the connection state of main equipment and next level slave device;First slave device allocates address in combination with itself unique ID and storage, after address configuration is completed, confirmation message is sent, and address allocation instruction is forwarded to next level;All slave device address allocation is completed gradually, and the slave device that address configuration is completed is automatically switched to normal communication mode;When slave device is replaced, it is automatically detected by the next level device, and the new device is automatically configured as the address of the original device, realizing plug and play replacement.The application realizes that slave device is plug and play, automatically obtains unique Modbus address, without manually dialing code or software pre-configuration, solves the problem of low deployment efficiency of multiple device networking address conflict.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to an address configuration method, specifically an RS485 bus Modbus plug-and-play automatic address configuration method. Background Technology

[0002] In industrial automation control systems, the Modbus protocol, as a widely used communication standard, relies on a one-to-many serial architecture of the RS485 bus to achieve data exchange between master and slave devices. This architecture requires each slave device to be configured with a unique communication address to ensure that the master device can accurately identify and poll the target device. Traditional address configuration methods rely entirely on manual operation, such as setting the slave device address one by one through physical DIP switches or dedicated software tools. This manual configuration mechanism has several drawbacks: First, due to operator negligence or errors, multiple slave devices are easily assigned the same address, leading to address conflicts, communication interruptions, data loss, or system paralysis, seriously threatening the continuous and stable operation of the industrial site. Second, during system deployment and debugging, engineers need to spend a lot of time setting the address for each device, a cumbersome and inefficient process, especially in large-scale device networks, significantly extending project cycles and increasing labor costs. Third, when a slave device fails and needs to be replaced, the newly connected device must be manually reconfigured to its original address; otherwise, the master device cannot recognize its identity. This requires on-site intervention by technicians or notifying the host to re-initiate the allocation process, causing additional downtime and increasing maintenance complexity and response delays. Fourth, the master device usually needs to pre-fix the number of slave devices and cannot dynamically sense the total number of devices actually connected on-site, making it difficult to adapt to the flexible needs of adding or removing devices or topology changes. Some existing automatic address allocation schemes attempt to alleviate the above problems, but most of them use a bus contention mechanism, allowing multiple slave devices to respond to address requests simultaneously, resulting in frequent collisions of messages during transmission on the bus, impairing data integrity and significantly reducing system reliability. These solutions fail to fully utilize the hardware isolation features of the master and slave ports in the RS485 bus, such as the physical isolation of the chain communication path achieved through independent transceivers. Therefore, in complex industrial environments such as high noise or long-distance cabling scenarios, they cannot effectively avoid conflicts, resulting in insufficient stability and efficiency, difficulty in achieving true plug-and-play functionality, and weak system adaptability to changes in the field. Summary of the Invention

[0003] An RS485 Modbus automatic address configuration method and system is disclosed, which can automatically complete the address allocation of all slave devices, avoid address conflicts caused by manual configuration, reduce manual operation, improve deployment efficiency, and eliminate the need for manual intervention and master device to re-initiate allocation when replacing faulty slave devices. It can automatically complete the address configuration of new devices, achieve plug-and-play, significantly reduce operation and maintenance costs, and improve system communication reliability.

[0004] This application provides an RS485 bus Modbus automatic address configuration method, the technical solution of which is as follows: Includes the following steps: S1, the master device initializes the address pool and sends a Modbus address allocation broadcast command; S2, the first slave device powers on and enters configuration mode to detect the connection status between the master device and the next-level slave device; S3: The first slave device assigns and stores an address based on its own unique ID. After the address configuration is completed, it sends an acknowledgment message and forwards the address assignment instruction to the next level. S4 completes the address allocation for all slave devices step by step, and the slave devices that have completed the address configuration automatically switch to normal communication mode. When replacing a device, S5 requires no intervention from the host device or a re-initiation of an allocation command from the host device. The upstream device automatically detects the device and automatically configures the new device with the address of the original device, achieving plug-and-play replacement.

[0005] Furthermore, this application also proposes that in step S1, the address pool is initialized to a range of 1 to 247, and the master device sends a Modbus broadcast address allocation instruction through its own RS485 port. The instruction includes the address allocation command code, the starting address, the address step size, and the timeout period.

[0006] Furthermore, this application also proposes that the slave device includes a master port and a slave port, integrating two independent hardware-isolated RS485 transceivers; After the device powers on and enters address configuration mode, both the master port and the slave port are in listening state; The first slave device receives a broadcast command from its master port to configure its address. During the configuration phase, the slave port is closed by default, so that the broadcast command is only transmitted on the current link to prevent the spread of broadcast messages and bus conflicts. If the slave device verifies the legitimacy of the broadcast command, and the slave device is in an unconfigured state, the current link is locked, and the downstream device is prohibited from receiving the broadcast command.

[0007] Furthermore, this application also proposes that in step S3, when the slave device receives a broadcast instruction from the master device, it extracts the starting address from the broadcast instruction, generates an address and a check code by combining its own ID, and stores the address and check code. The slave device sends an address confirmation message to the master device through the master port, which includes a unique ID, the assigned address, and a checksum. The master device receives the acknowledgment message, verifies the checksum, and if it is valid, marks the address as allocated and updates the address pool; if no message is received within the timeout, it resends the broadcast command. After the first slave device completes the address configuration, the slave port switches from closed isolation to open, forwards the address allocation command to the lower-level slave devices, increments the address step by 1, and automatically switches to normal communication mode. After all slave devices complete the address configuration, the master device polls each slave device according to the Modbus RTU protocol, and the slave devices respond with the assigned unique address to start communication.

[0008] Furthermore, this application also proposes that, during the address configuration process, a chain-like hierarchical distribution method be adopted, with the master device maintaining an available address pool, and each level allowing only one slave device to receive and occupy the address; After each slave device is configured with an address, the master device records the assigned address and updates the address pool.

[0009] Furthermore, this application also proposes that, specifically during equipment replacement: After the upstream device fails to read the target device multiple times in a row, it sends an address configuration command to the newly connected device via broadcast, directly setting the new device to the address of the original failed device, so that the overall address sequence remains continuous and available.

[0010] Furthermore, this application also proposes that the master device can detect in real time whether all slave devices are currently online or offline, and if offline, the master device can automatically reclaim the addresses of offline devices; When a new device is connected to an offline device, the master device will reassign the address of the reclaimed offline device to the newly connected device. After the address configuration is completed, the device will join the network without needing to restart the system.

[0011] Furthermore, this application proposes that the master device automatically identifies the total number of slave devices on the bus and establishes a complete device mapping table by polling at each level and identifying cross-port messages.

[0012] Furthermore, this application also proposes that address allocation uses a custom Modbus function code, which is compatible with the standard Modbus RTU protocol after allocation and can communicate directly with general PLCs and configuration software.

[0013] Furthermore, this application also proposes an RS485 bus Modbus automatic address configuration system, the system comprising: The master device is used to establish the address pool and send Modbus address allocation broadcast commands. The slave device has a master port and a slave port, two independent hardware-isolated RS485 transceivers, used to receive signals and configure addresses from the upper-level device, and send instructions to the lower-level device at the same time. The data acquisition module is used to collect the status of all devices, including whether they are offline or online; The address configuration module is used to configure the address of the device. The port control module is used to open or close the master port and slave port of the isolated device.

[0014] The present invention has the following beneficial technical effects: By initiating address allocation broadcasts from the master device, slave devices automatically complete address allocation step by step. When a slave device is replaced, the upper level automatically detects and configures the new device with the original address. This solves the problems of error-prone and inefficient manual address configuration, the need for manual intervention when replacing devices, and the tendency for packet conflicts in existing automatic allocation schemes. It has the advantages of being able to automatically complete address allocation for all slave devices, avoiding address conflicts caused by manual configuration, reducing manual operations, improving system deployment efficiency, and automatically completing address configuration for new devices without manual intervention or re-initiating allocation by the master device when replacing faulty slave devices, achieving plug-and-play functionality, significantly reducing operation and maintenance costs, and improving system communication reliability. Attached Figure Description

[0015] Figure 1 This is a schematic diagram showing the connection between the master device and the slave device of the present invention. Detailed Implementation

[0016] Embodiments of the present invention are described in detail below, examples of which are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present invention, and should not be construed as limiting the present invention.

[0017] like Figure 1 As shown, an RS485 bus Modbus automatic address configuration method includes the following steps: S1, the master device initializes the address pool and sends a Modbus address allocation broadcast command; S2, the first slave device powers on and enters configuration mode to detect the connection status between the master device and the next-level slave device; S3: The first slave device assigns and stores an address based on its own unique ID. After the address configuration is completed, it sends an acknowledgment message and forwards the address assignment instruction to the next level. S4 completes the address allocation for all slave devices step by step, and the slave devices that have completed the address configuration automatically switch to normal communication mode. When replacing a device, S5 requires no intervention from the host device or a re-initiation of an allocation command from the host device. The upstream device automatically detects the device and automatically configures the new device with the address of the original device, achieving plug-and-play replacement.

[0018] RS485 bus is a serial communication interface standard widely used in industrial control. It is characterized by long transmission distance, strong anti-interference ability, and support for multi-point communication.

[0019] Modbus is a serial communication protocol used to connect industrial electronic devices. This protocol defines the format and rules for data exchange between devices, typically employing a master-slave architecture for communication.

[0020] In a Modbus communication system, the master device plays the role of initiating communication requests, sending instructions to slave devices and receiving their responses.

[0021] In a Modbus communication system, the slave device responds to the master device's requests, performs the corresponding operations, and returns data.

[0022] The address pool is a set of available addresses maintained internally by the master device, used to assign unique communication addresses to slave devices.

[0023] The Modbus address allocation broadcast command is a special command sent by the master device to notify the slave devices on the bus to configure addresses.

[0024] Configuration mode is a special working state that a slave device enters when it receives an address allocation instruction. In this mode, the slave device will perform address allocation and storage operations.

[0025] A unique ID is an inherent, unchangeable identifier for each slave device, used to distinguish different slave devices during address allocation.

[0026] An acknowledgment message is a message sent by the device to the master device after the address configuration is completed, to inform the master device that the address allocation has been successfully completed.

[0027] Normal communication mode is the normal working state that the slave device enters after address configuration. In this mode, the slave device will communicate with the master device according to the assigned address.

[0028] The master device is configured to initialize an address pool, which can be preset with a series of available communication addresses, initially set to 1–247. Subsequently, the master device sends a Modbus address allocation broadcast command through its communication interface. This command can be a Modbus message containing a specific function code and data fields, used to instruct slave devices on the bus to configure addresses.

[0029] After the first slave device powers on, its internal program is designed to automatically enter address configuration mode. In this mode, the slave device continuously listens to its communication port to detect broadcast commands sent by the master device. Simultaneously, the slave device can detect the ports it is connected to in order to determine if a downstream slave device is connected. This can be done, for example, by detecting the electrical status of the port or the presence of a data signal.

[0030] Specifically, when the first slave device receives a broadcast command from the master device, it generates a communication address by combining its own unique ID and stores this address in its internal non-volatile memory. After address configuration, the slave device sends an acknowledgment message to the master device, which may contain the slave device's unique ID and the assigned address, to inform the master device that the address allocation was successful. Subsequently, the slave device forwards the address allocation command to its next-level slave device to initiate the address configuration process for that device.

[0031] Once a slave device is successfully configured and its address is stored, its internal program will automatically switch from configuration mode to normal communication mode. In normal communication mode, the slave device will use its assigned unique address to perform regular data exchange with the master device.

[0032] Through a collaborative mechanism between master and slave devices, automated allocation and management of slave device addresses are achieved. This method effectively avoids address conflicts caused by traditional manual configuration, significantly improving the efficiency of system deployment and debugging. In device replacement scenarios, no manual intervention or host reassignment is required. Through automatic detection and configuration of the upstream device, plug-and-play replacement of devices is achieved, greatly simplifying operation and maintenance complexity and improving the system's adaptability and stability in complex industrial environments.

[0033] In step S1 above, the address pool is initialized to the range of 1 to 247. The master device sends a Modbus broadcast address allocation command through its own RS485 port. The command includes the address allocation command code, starting address, address step size and timeout.

[0034] The address pool is initialized to a range of 1 to 247, strictly adhering to the slave device address range specification in the Modbus RTU protocol. This address pool is the set of available addresses used by the master device to manage and allocate slave device addresses, ensuring that all addresses assigned to slave devices are legal and usable, avoiding the allocation of addresses outside the protocol range, and thus guaranteeing normal communication between slave devices and the master device.

[0035] The master device sends Modbus broadcast address allocation commands through its own RS485 port. The RS485 port is the physical interface, and the Modbus broadcast address allocation command is a data message carrying the address allocation logic. The master device uses its physical RS485 interface to send the encapsulated Modbus broadcast address allocation command onto the bus. The broadcast command means that all slave devices connected to the bus can receive this command, which is the basis for automatic address configuration.

[0036] This instruction includes an address allocation command code, a starting address, an address step size, and a timeout period. The address allocation command code is a custom Modbus function code used to identify that the message is an address allocation request, ensuring that the slave device can recognize it as a special address allocation request, rather than a regular Modbus data read / write request, thus entering address configuration mode. The starting address provides a clear starting point for address allocation for the slave device, especially in chained allocation, guiding the first slave device to obtain its address. The address step size is the increment from the starting address for each address allocation, used in chained allocation to ensure that each slave device obtains a unique, incrementing address, maintaining address continuity and uniqueness. The timeout period sets the maximum allowed time for the slave device to wait for a response from the master device or to proceed with the next operation after receiving the instruction, preventing indefinite waiting of the address allocation process due to slave device failure or network latency, thereby improving system robustness and efficiency.

[0037] The slave device includes a master port and a slave port, integrating two independent, hardware-isolated RS485 transceivers. Specifically, the slave device is designed with two independent RS485 communication interfaces, referred to as the master port and the slave port. Each of these ports integrates an independent RS485 transceiver, and these transceivers are electrically isolated from each other through hardware circuitry. The master port is typically used to communicate with the upstream device (such as a master device or an upstream slave device), while the slave port is used to communicate with the downstream slave device. This dual-port, hardware-isolated design allows the slave device to independently control the communication links in both directions, thereby achieving precise management and isolation of the data flow at the physical layer. This effectively avoids signal interference and bus conflicts, providing a foundation for subsequent link control.

[0038] After the slave device powers on and enters address configuration mode, both the master and slave ports are in a listening state. Specifically, when the slave device powers on for the first time or is reset, its internal control logic puts it into a special address configuration mode. In this mode, the RS485 transceivers on both the master and slave ports are activated and set to listening mode. This means that both ports can receive data signals from the bus, but the slave device does not actively send data; instead, it waits to receive specific address allocation instructions. This listening state ensures that the slave device can fully perceive communication activity on the bus during the initial address configuration phase, preparing for subsequent instruction reception and processing.

[0039] The first slave device receives a broadcast command on its master port for address configuration. During the configuration phase, the slave port is closed by default, ensuring that the broadcast command is transmitted only on the current link to prevent broadcast message propagation and bus collisions. Specifically, during address configuration, when the master device sends a Modbus address allocation broadcast command, the first slave device closest to the master device on the link receives the broadcast command through its master port. During the receiving and processing phase, the slave port of this first slave device is set to a closed state by default, meaning its RS485 transceiver is inactive or in a high-impedance state, and it does not forward any data. This mechanism ensures that the broadcast command issued by the master device is limited to transmission on the link between the current master device and the first slave device, and does not propagate to lower-level slave devices. By limiting the transmission range of broadcast messages, it effectively prevents unordered propagation of broadcast messages across the entire bus, thereby avoiding potential bus collisions and ensuring the order and exclusivity of address allocation.

[0040] If a slave device verifies the legitimacy of a broadcast command, and is in an unconfigured state, it locks the current link, preventing downstream devices from receiving the broadcast command. Specifically, upon receiving a broadcast command, the slave device verifies its legitimacy, checking the command format, command code, checksum, etc., to ensure they conform to preset specifications. If the command is valid, and the slave device detects that it is currently in an unconfigured address state (i.e., has not yet been assigned a Modbus address), it will perform the "lock current link" operation. This means the slave device will further ensure its slave port remains closed or disabled, physically preventing the broadcast command from continuing to be transmitted to downstream slave devices. This locking mechanism ensures that in a chained topology, only one unconfigured slave device can respond to and process the address allocation command at a time, effectively avoiding multiple slave devices simultaneously competing for addresses or receiving duplicate commands, thus guaranteeing the uniqueness and reliability of the address allocation process.

[0041] When a slave device receives a broadcast command from the master device, it extracts the starting address from the broadcast command, combines it with its own unique ID to generate an address and a checksum, and stores the generated address and checksum. Subsequently, the slave device sends an address acknowledgment message to the master device through the master port. This message contains the slave device's own unique ID, the assigned address, and the checksum. Upon receiving the acknowledgment message, the master device verifies the checksum. If the checksum is valid, the master device marks the address as assigned and updates its internal address pool. If the master device does not receive an acknowledgment message within the set timeout period, it resends the broadcast command. After the first slave device completes its address configuration, its slave port switches from a closed / isolated state to an open state, forwards the address allocation command to lower-level slave devices, increments the address step by 1, and automatically switches to normal communication mode. Once all slave devices have completed their address configurations, the master device polls each slave device according to the Modbus RTU protocol. The slave devices respond with their assigned unique addresses, thus initiating communication.

[0042] After successfully generating and storing the address and checksum, the slave device immediately sends an address acknowledgment message to the master device via its master port. This acknowledgment message is unicast, not broadcast, to ensure that only the master device receives it. The message contains the slave device's unique ID, the Modbus address that was just assigned and stored, and a checksum used to verify message integrity. The purpose of this step is to explicitly inform the master device that the slave device has successfully completed address allocation and provided the necessary identity and address information, providing the master device with a basis for updating the address pool.

[0043] After sending a broadcast address allocation command, the master device enters a state of waiting for acknowledgment messages from slave devices and starts a timeout timer. Once the master device receives the address acknowledgment message from the slave device, it first verifies the checksum in the message to ensure the integrity of the message and that it has not been tampered with. If the checksum is valid, the master device marks the allocated address reported by the slave device as "allocated" and removes or updates the status of the address from its internally maintained address pool, ensuring that the address is not allocated to other devices. If the master device fails to receive any acknowledgment message within the set timeout period, or if the received message fails verification, the master device determines that the address allocation may have failed and resends the broadcast address allocation command to attempt address allocation again, thereby improving the success rate of allocation and the robustness of the system.

[0044] Once the first slave device successfully completes its address configuration and sends an acknowledgment message to the master device, its slave port switches from a closed / isolated state to an open state. At this point, the slave device forwards address allocation commands to its subordinate slave devices. During forwarding, to ensure that subordinate devices obtain consecutive and unique addresses, the slave device increments the "address step" parameter in the command by 1, thus providing a new starting address for the next slave device. Simultaneously, the slave device automatically switches from address configuration mode to normal communication mode, preparing to receive Modbus RTU protocol commands from the master device. Once all slave devices have completed address configuration and switched to normal communication mode, the master device can communicate with each slave device via polling according to the standard Modbus RTU protocol. Each slave device responds to the master device's queries using its assigned unique address, thereby enabling normal data exchange across the entire bus network.

[0045] During the address configuration process, a chain-like hierarchical distribution is adopted. The master device maintains an available address pool, and only one slave device is allowed to receive and occupy the address at each level. After each slave device is configured with an address, the master device records the allocated address and updates the address pool.

[0046] "Only one slave device is allowed to receive and occupy an address at each level" means that at a certain stage of address allocation or on a certain physical link segment, only one slave device is authorized or designed to receive and respond to address allocation instructions, thereby acquiring and occupying an address. This can be achieved through the control mechanism of the slave device's port. For example, when a slave device's master port receives a broadcast instruction and begins configuration, its slave ports will be temporarily closed or isolated to prevent the broadcast instruction from propagating downstream until the current slave device completes address configuration and is ready to forward the instruction. This mechanism effectively prevents address conflicts and duplicate allocations.

[0047] Updating the address pool refers to the process by which a master device removes an allocated address from its maintained pool of available addresses after successfully allocating and recording it. This is done to ensure that the address pool always reflects currently available address resources, preventing the reassignment of already occupied addresses and thus guaranteeing the uniqueness of address allocations and system stability.

[0048] When a higher-level device fails to read the target device multiple times consecutively, it broadcasts an address configuration command to newly connected devices, directly setting the new device to the address of the previously failed device, thus maintaining the continuity and availability of the overall address sequence. In a chained Modbus network, each slave device (except the device at the end of the chain) communicates with its downstream slave devices. When the downstream "target device" fails or is removed, the "upper-level device" periodically attempts to communicate with the "target device," for example, by sending Modbus read commands. If these communication attempts fail within a preset time window or after reaching a preset number of retries (i.e., "consecutive read failures"), the "upper-level device" determines that the "target device" is offline or has failed. This mechanism makes fault detection distributed and adaptive, eliminating the need for a global scan by the master device.

[0049] When a slave device needs to be replaced, the system achieves a high degree of automation and plug-and-play functionality. The upstream device can intelligently detect the fault or offline status of the downstream device and proactively intervene, quickly configuring the newly connected slave device with the address of the original faulty device. This significantly reduces the need for manual intervention, avoids the complexity of the master device re-initiating the address allocation process, thereby greatly shortening system maintenance time and improving system availability. More importantly, by directly reusing the original address, the continuity and integrity of the Modbus bus address sequence are ensured, avoiding address confusion or upper-layer application logic interruptions caused by device replacement, enabling the entire network to operate smoothly and efficiently.

[0050] The master device can detect and identify the online and offline status of all slave devices in real time. Specifically, the master device periodically sends query messages (such as Modbus RTU protocol read register commands) to all slave devices on the bus and determines their online status based on the slave devices' responses. If no valid response is received from a slave device within a preset timeout period, the master device can determine that the slave device is offline. In addition, the master device can also monitor the activity of slave devices in real time by listening to specific heartbeat messages or status reports on the bus. This real-time detection mechanism ensures that the master device can accurately and promptly grasp the operating status of all slave devices on the bus.

[0051] If the master device detects that a slave device is offline, it automatically reclaims the address of that offline device. Specifically, the master device marks the address corresponding to the offline slave device in its address pool as "available" or "idle," thereby removing the address from the list of allocated addresses. This reclamation process requires no manual intervention, ensuring dynamic management and efficient utilization of address resources and avoiding long-term occupation and waste of address resources.

[0052] When a new device is connected to an offline device, the master device reassigns the reclaimed offline device address to the newly connected device. When a new slave device connects to the bus, and that connection location previously corresponded to an offline device, the master device will prioritize assigning the previously reclaimed offline device address to the newly connected device. The master device can identify and assign addresses to new devices by detecting new device connection events on the bus (e.g., specific request messages sent after a new device powers on) and combining this with its internally maintained device mapping table or address status information. This allocation strategy ensures the continuity of the address sequence, avoids address fragmentation, and maintains the stability of the system topology.

[0053] After address configuration is complete, newly connected slave devices can join the network without requiring a system restart. Once the newly connected slave device receives the address assigned by the master device and completes its internal configuration, it can immediately switch to normal communication mode and join the Modbus network for data exchange. The entire process does not require a restart of the master device or the entire system.

[0054] The master device automatically identifies the total number of slave devices on the bus through step-by-step polling and cross-port message identification, and establishes a complete device mapping table for easy querying.

[0055] The master device can actively probe for slave devices on the bus using a tiered polling mechanism. Combined with cross-port message recognition capabilities, it can accurately identify and count the number of all slave devices on the bus, even in multi-level serial or complex topologies. Based on this, the master device can establish a complete device mapping table, recording the address and status of each slave device in real time. This allows the master device to fully grasp the network status without manual intervention after slave device addresses are automatically configured, or when slave devices are replaced or the network topology changes. This ensures communication stability and data transmission accuracy, greatly improving the adaptability and management efficiency of the RS485 Modbus network.

[0056] In addition, address allocation uses custom Modbus function codes, and after allocation, it is compatible with the standard Modbus RTU protocol and can communicate directly with general PLCs and configuration software.

[0057] The address allocation uses a custom Modbus function code. A Modbus function code is a byte in the Modbus protocol used to identify a specific operation type. The standard Modbus protocol defines a series of function codes for reading and writing data, controlling devices, etc. A custom Modbus function code refers to a function code whose meaning and operational logic are defined by the system designer according to specific needs, within the scope of function codes not defined or reserved in the standard Modbus protocol. By using a custom function code, the specific management operation of address allocation can be distinguished from the existing standard data read and write operations in the Modbus protocol, thereby avoiding function code conflicts with existing Modbus devices or systems during the address allocation process. For example, a less widely used or reserved function code (such as 0x65-0x6F or 0x90-0x9F, etc.) can be selected from the function code field of the Modbus message and defined as the address allocation command code. When the master device sends a Modbus address allocation broadcast command, it places this custom function code in the function code field of the message. After receiving a message from the device, the function code is first parsed. If it is identified as a custom address allocation function code, the address configuration logic is entered; otherwise, it is processed according to the standard Modbus protocol.

[0058] Furthermore, the device is compatible with the standard Modbus RTU protocol after address allocation. The Modbus RTU protocol is an implementation of the Modbus protocol on serial communication lines, widely used in industrial automation due to its efficiency and simplicity. Compatibility with the standard Modbus RTU protocol means that after address allocation and switching to normal communication mode, the slave device's communication behavior will fully comply with the Modbus RTU protocol specifications. This includes message format, function code processing, data frame structure, and CRC checksums. The slave device will be able to respond to standard Modbus RTU read and write commands and interact with data according to the standard protocol without any special protocol conversion or adaptation.

[0059] Based on this, direct communication with general-purpose PLCs and configuration software is possible. General-purpose PLCs (Programmable Logic Controllers) are core devices in industrial control, used to implement automated control logic. Configuration software (such as SCADA or HMI software) provides a human-machine interface for monitoring and operating industrial processes. Since slave devices are fully compatible with the standard Modbus RTU protocol after address allocation, any general-purpose PLC or configuration software that supports the standard Modbus RTU protocol can directly communicate with these slave devices. This means that users do not need to develop custom communication drivers or interfaces for the slave devices in their system; they only need to configure standard Modbus RTU communication parameters (such as slave address, baud rate, data bits, stop bits, parity bits, etc.) in the PLC or configuration software to read, write, and control data from the slave devices just like any other standard Modbus device.

[0060] This application also discloses an RS485 bus Modbus automatic address configuration system, which includes: The master device is used to establish the address pool and send Modbus address allocation broadcast commands. The slave device has a master port and a slave port, two independent hardware-isolated RS485 transceivers, used to receive signals and configure addresses from the upper-level device, and send instructions to the lower-level device at the same time. The data acquisition module is used to collect the status of all devices, including whether they are offline or online; The address configuration module is used to configure the address of the device. The port control module is used to open or close the master port and slave port of the isolated device.

[0061] By designing the slave devices with independently hardware-isolated RS485 transceivers for both the master and slave ports, and combining this with a chain-like hierarchical address allocation mechanism, message collisions caused by bus contention are avoided. This enables plug-and-play replacement without host intervention during device replacement, improving system deployment efficiency, simplifying maintenance complexity, and enhancing adaptability to industrial environments. Specifically, after initializing the address pool, the master device sends a Modbus address allocation broadcast command. The first slave device powers on and enters configuration mode, receiving commands and checking connection status through the master port. During the configuration phase, the slave port is closed by default to prevent the spread of broadcast messages, ensuring that commands are transmitted only on the current link. The slave device allocates and stores an address based on its unique ID. After completion, it sends an acknowledgment message to the master device through the master port. Simultaneously, the slave port switches to open and forwards commands to lower-level devices, achieving hierarchical address allocation. Based on this, when a device is replaced, the upstream device automatically detects the fault through continuous read failures and directly sends the original address configuration command to the newly connected device without the host re-initiating the allocation command; the data acquisition module monitors the online status of the device in real time, the master device automatically reclaims the offline device address and reassigns it to the new device, and the port control module precisely manages the open or closed isolation status of the master port and slave port, thereby ensuring the continuous availability of the address sequence.

[0062] It should be noted that the above are merely preferred embodiments of the present invention and are not intended to limit the present invention. Although the present invention has been described in detail with reference to the embodiments, those skilled in the art can still modify the technical solutions described in the foregoing embodiments or make equivalent substitutions for some of the technical features. However, any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.

Claims

1. A Modbus automatic address configuration method for RS485 bus, characterized in that, Includes the following steps: S1, the master device initializes the address pool and sends a Modbus address allocation broadcast command; S2, the first slave device powers on and enters configuration mode to detect the connection status between the master device and the next-level slave device; S3: The first slave device assigns and stores an address based on its own unique ID. After the address configuration is completed, it sends an acknowledgment message and forwards the address assignment instruction to the next level. S4 completes the address allocation for all slave devices step by step, and the slave devices that have completed the address configuration automatically switch to normal communication mode. When replacing a device, S5 requires no intervention from the host device or a re-initiation of an allocation command from the host device. The upstream device automatically detects the device and automatically configures the new device with the address of the original device, achieving plug-and-play replacement.

2. The method according to claim 1, characterized in that, In step S1, the address pool is initialized to a range of 1 to 247. The master device sends a Modbus broadcast address allocation instruction through its own RS485 port. The instruction includes the address allocation command code, starting address, address step size, and timeout.

3. The method according to claim 2, characterized in that, The slave device includes a master port and a slave port, and integrates two independent hardware-isolated RS485 transceivers; After the device powers on and enters address configuration mode, both the master port and the slave port are in listening state; The first slave device receives a broadcast command from its master port to configure its address. During the configuration phase, the slave port is closed by default, so that the broadcast command is only transmitted on the current link to prevent the spread of broadcast messages and bus conflicts. If the slave device verifies the legitimacy of the broadcast command, and the slave device is in an unconfigured state, the current link is locked, and the downstream device is prohibited from receiving the broadcast command.

4. The method according to claim 3, characterized in that, In step S3, when the slave device receives a broadcast instruction from the master device, it extracts the starting address from the broadcast instruction, combines it with its own ID to generate an address and a verification code, and stores the address and verification code. The slave device sends an address confirmation message to the master device through the master port, which includes a unique ID, the assigned address, and a checksum. The master device receives the confirmation message, verifies the checksum, and if it is valid, marks the address as allocated and updates the address pool. If no message is received within the timeout period, resend the broadcast command; After the first slave device completes the address configuration, the slave port switches from closed isolation to open, forwards the address allocation command to the lower-level slave devices, increments the address step by 1, and automatically switches to normal communication mode. After all slave devices complete the address configuration, the master device polls each slave device according to the Modbus RTU protocol, and the slave devices respond with the assigned unique address to start communication.

5. The method according to claim 4, characterized in that, During the address configuration process, a chain-like hierarchical distribution is adopted. The master device maintains an available address pool, and each level only allows one slave device to receive and occupy the address. After each slave device is configured with an address, the master device records the assigned address and updates the address pool.

6. The method according to claim 5, characterized in that, The specific steps during device replacement are as follows: After the upstream device fails to read the target device multiple times in a row, it sends an address configuration command to the newly connected device via broadcast, directly setting the new device to the address of the original failed device, so that the overall address sequence remains continuous and available.

7. The method according to claim 6, characterized in that, The master device monitors in real time and identifies whether all slave devices are currently online or offline. If a device is offline, the master device automatically reclaims the address of the offline device. When a new device is connected to an offline device, the master device will reassign the address of the reclaimed offline device to the newly connected device. After the address configuration is completed, the device will join the network without needing to restart the system.

8. The method according to claim 6, characterized in that, The master device automatically identifies the total number of slave devices on the bus and establishes a complete device mapping table by using step-by-step polling and cross-port message identification.

9. The method according to claim 1, characterized in that, The address allocation uses a custom Modbus function code, and after allocation, it is compatible with the standard Modbus RTU protocol and can communicate directly with general PLCs and configuration software.

10. An RS485 bus Modbus automatic address configuration system, characterized in that, The system includes: The master device is used to establish the address pool and send Modbus address allocation broadcast commands. The slave device has a master port and a slave port, two independent hardware-isolated RS485 transceivers, used to receive signals and configure addresses from the upper-level device, and send instructions to the lower-level device at the same time. The data acquisition module is used to collect the status of all devices, including whether they are offline or online; The address configuration module is used to configure the address of the device. The port control module is used to open or close the master port and slave port of the isolated device.