Everything you need to know about Modbus protocols
What’s a Modbus protocol? We can define the Modbus protocol as a way to establish master-slave communication among smart devices.
What’s a Modbus protocol?
We can define the Modbus protocol as a way to establish master-slave communication among smart devices. Developed by Modicon in 1979, it has seen wide use in a variety of segments.
Furthermore, as an independent protocol, it allows you to have a multibranded network, with devices from different vendors communicating in the same network. You’ll see Modbus TCP/IP, Modbus RTU, and ASCII on the market, so we’ll explain the differences shortly.
The organization that maintains the protocol could use a website update, but you can still find a lot of information there.
Where do you use it?
When you need field devices (slaves) to send data to a controller (master) for analysis and action, the Modbus protocol can connect them for you.
You should know that Modbus works as more than just an industrial protocol. You can also find it in security, energy, transportation, and other types of automation. Its simple configuration and use make it flexible for all these scenarios and more.
In point-to-point communication, you can use Modbus RS232. If you have many slaves on the network, then you can apply Modbus TCP/IP or RS485. Modbus pairs well with Arduino or similar solutions too. On the industrial side, many field devices offer Modbus as an option.
How does it work?
Let’s start with the protocol in general and then dive into the types, explaining how they work and how they differ.
You apply this protocol when you need field devices to send data to a control system or other data-gathering system. For instance, if you have meters measuring flow, then they’ll send their numbers through the Modbus to the control system.
You can also use Modbus with a computer to set up or collect data from a field device. You can have serial communication with the Modbus RTU and ASCII or by using the Ethernet as a medium with the Modbus TCP/IP.
Okay, now let’s dig more into the details.
If you decide to apply Modbus using RS-232, you have a maximum distance between the master and slave of 15 meters. And you can have only one master and one slave, known as a point-to-point or peer-to-peer connection.
RS-422 and RS-485
Now, if you use RS-422 or RS-485, then you’ll get more flexibility. Here you can have one master and many slaves. The network has a cable structure of Tx+/Tx- to transmit and Rx+/Rx – and a ground to receive.
You’ll see more RS-485 than RS-422, as the former allows up to 32 slaves without a repeater. However, you can get up to 247 slaves if you apply repeaters in the network (maximum length = 1200 meters).
Setup in an RS-485
When you add devices to a network, you need all devices in the same baud or bit rate, because the protocol is asynchronous. Usually, you can get a normal speed between 9600 to 19200, but you can set up faster speeds from 300 to 100000.
The protocol sends a start condition from the master to the slaves. This message has 28 bits or 3.5 characters. Then, the master sends the address, which the slaves receive as an 8-bit functional code.
This list shows the most common functional codes:
- 1 – Read coil status
- 2 – Read input status
- 3 – Read holding registers
- 4 – Read input registers
- 5- Write single-coil status
- 6- Write single register
- 15- Write multiple-coil status
- 16 – Write multiple registers
After the function codes, the master will send data to the slaves, also in 8-bit code. (The quantity will depend on the type of slave, but you can get the details from your manual.) Next, the master sends an error check in 16 bits, and finally, a stop condition in 28 bits.
On the other side, the slaves answer queries from the master with echoes, remaining silent otherwise.
Data types and address spaces
Here we’ll clarify the types of function codes and how they apply to the Modbus protocol.
With a coil, you can read and write a 1-bit message with an address from 00001 to 09999. Discrete input is only read-only, with the size of the message still one bit and the address from 10001 to 19999.
Now, with an input register, then you have read-only access but a 16-bit message and an address from 30001 to 39999. And a holding register gives you read/write access, with a 16-bit message and an address from 40001 to 49999.
To communicate a message of 32 bits, you need two addresses of 16 bits to send together. The master or slave will read the two messages as a 32-bit unit.
Modbus ASCII (American Standard Code for Information Interchange)
You can set masters up to communicate with ASCII or RTU. In ASCII, each 8-bit unit transmits as two ASCII characters. ASCII can have up to one second between characters without creating an error.
Modbus RTU (Remote Terminal Unit)
If you set the master up with RTU, each unit contains two 4-bit hexadecimal characters. RTU offers better data throughput than ASCII because of its excellent character density.
In 1999, Modbus TCP/IP emerged to handle the new reality of Ethernet protocols in industrial applications. TCP/IP uses the internet to transport data, and the Modbus instructions come through the TCP/IP.
This protocol was developed for the internet and offers interoperability among vendors.
What’s a Modbus map?
A Modbus map simply defines the data you’ll find in a slave – what kind (flow, pressure, etc.), what location (table and address), and what format (byte, word order, and so on).
You can find devices on the market with pre-defined maps, but for most of them you can create your own to communicate with the master.
And that’s all we have for Modbus! Any questions? Leave them in the comments!