Firstly, we need to be clear that Modbus is a master-slave protocol on a serial link, where only one host can exist on the communication line and there will be no situation where multiple hosts exist. Although there is only one host, there can be multiple slaves.
The communication process of Modbus is initiated by the host, and the slave responds after receiving the host's request. The slave does not actively send data. And the slave machines will not send data to each other.
1. Modbus Address Rules
Each slave in Modbus has an address, also known as a slave node address, in order to be recognized by the master during communication.
The node address must be unique for the slave device, and there can only be one unique device address on the bus, meaning that the addresses of the slave devices cannot conflict on the bus.
The slave has a unique device address, while the host itself does not have an address.
Modbus supports 256 addressing spaces, as follows:
From the above figure, it can be seen that the address range of 1~247248~255 used for child nodes is reserved.A very important point is that 0 is the broadcast address, and all child nodes must be able to recognize the broadcast address.
2. Modbus communication mode
There are two communication modes for Modbus: unicast mode and broadcast mode.
(1) Unicast mode
Unicast mode: When a host wants to access a child node, it is a one-to-one access method based on the address of the child node. After receiving a request from the master node and completing the corresponding processing according to the type of request, the child node will return a message to the master node, which is commonly known as a "response". The schematic diagram is as follows:
From the above figure, it can be seen that the master node sends a request command to the slave node. The slave node receives the request from the master node on the bus, parses the request instruction from the master node, completes the corresponding action according to the instruction, and then responds to the master node's request, completing a unicast communication process.
In fact, you should also be able to see that in unicast mode, a Modbus transaction contains two messages: one is a "request" message initiated by the host, and the other is a "reply" message returned by the slave.
And the host can prepare to access a slave through the unique address of the slave, and the slave identifies whether the instructions issued by the host are sent to its own side through the unique address of the slave.
(2) Broadcast mode
Broadcast mode is an instruction used by the host to send requests to all slaves on the bus.
The host's broadcast instructions are generally used to write commands, and the slave does not need to respond to broadcast instructions from the host. The schematic diagram of the broadcasting mode is as follows:
From the figure, it can be seen that in broadcast mode, the host sends broadcast instructions in a unidirectional manner, while the slave no longer responds to the broadcast instructions, and the data flow only flows in one direction.
Attention: All slaves on the bus must be able to recognize the host's broadcast instructions, and address 0 is used for broadcasting and cannot be used as a slave address.
3. State characteristics of master-slave machines
Since Modbus is a master-slave communication mode, it is necessary to have a clear understanding of the status between the master-slave devices during the communication process, and to understand the communication process of Modbus through the status.(1) Status characteristics of the hostAs shown in the figure below:
According to the above figure, some information about the communication process of the host can be analyzed:
1) 'Idle'=No waiting requests. This is usually the state it is in after initial power on. The host can only send requests when it is in an 'idle' state. After sending a request, the host will leave the "idle" state and enter the "busy" state, at which point no other requests can be sent.
2) When a unicast request is sent to a slave, the host will enter a "waiting for response" state, and a critical timeout timer counter will be activated. This timeout is called a 'response timeout'. It avoids the master node from always being in a 'waiting for response' state. The response timeout time depends on the actual application scenario.
3) When receiving a response, the master node verifies the response before processing the data. In some cases, the results of the inspection may be incorrect. If a response is received from an unexpected child node or if the received frame is incorrect, the response timeout will continue to count; When a frame error is detected, a retry can be performed.
4) When the response times out but no response is received, an error is generated. So the master node will re-enter the "idle" state and issue a retry request. The maximum number of retries depends on the settings of the primary node.
5) When a broadcast request is sent to the serial bus, no response is returned from the child node. However, the master node needs to delay in order for the child nodes to complete the current request when sending new requests. This delay is called the 'conversion delay'. Therefore, the master node will enter the "waiting for transition delay" state before returning to the "idle" state where it can send another request.
6) In unicast mode, the response timeout must be set to a sufficient length so that any child node can process the request and return the response. The broadcast conversion delay must have sufficient length so that any child node can only process requests and receive new requests. Therefore, the conversion delay should be shorter than the response timeout.
Common frame errors include parity checking for each character and redundancy checking for the entire frame.
(2) The state characteristics of the slave machineAs shown in the figure below:
From the above figure, it can be seen that:
1) 'Idle'=No waiting requests. This is usually the state after initial power on.
2) When receiving a request, the child node must first verify the packet before processing the requested action in the request. When an error is detected, a response must be sent to the master node to inform of the error. (Errors include: incorrect format of request, illegal action, illegal access address...).
3) After the required action is completed, the unicast message requires a response to the master node in the format.
4) If the child node detects an error in the received frame, no response is returned to the main node.
5) Any child node should define and manage Modbus diagnostic counters to provide diagnostic information. By using Modbus diagnostic function codes, these count values can be obtained. Finally, add a communication timing diagram for the master-slave system:
The sequence diagram above describes the master-slave state characteristics mentioned above, and by comparing them, we can understand what it means.
Leave a Reply