Learning Modbus basics: Even beginners can understand it

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

Learning Modbus basics: Even beginners can understand it缩略图

Modbus is like a postman, helping your electronic devices - such as smart home devices, industrial control systems, etc. - transmit information. Just like a postman delivering your mail from the sender to the recipient, Modbus can also help your devices communicate with each other and share information.

Imagine having some smart light bulbs in your home. Each light bulb is like a person with a name, and each name (which we call an address) is unique. In this way, when you want to control a light bulb, you can find it by its name (address). In Modbus, each device has a unique address that we use to send commands or retrieve information.

Your smart light bulb can do two things: it can tell you its current status (such as brightness, color, etc.), and it can also change its status according to your commands. In Modbus, we refer to these states that can be read and written as registers. You can imagine that a register is like a mailbox, from which you can retrieve letters (read data) or put letters into it (write data).

In Modbus, we have two basic operations: read and write. If you want to know the current status of your light bulb, you can send it a read command, such as writing a note: "Light bulb 1, please tell me what color you are currently in." After receiving this command, the light bulb will reply to you with its current color. Similarly, if you want to change the color of a light bulb to red, you can send a write command: "Light bulb 1, please change your color to red." After receiving this command, the light bulb will change its color and reply that it has completed the operation.

To communicate with your device via Modbus, you need a "wire" (such as a data or network cable) to connect the device and a tool (such as Modbus software) that can send and receive Modbus commands. Just like you need a phone and a phone number to make calls to others. After setting up these tools, you can start communicating with your device.

To ensure the correctness of communication, Modbus uses a technique called CRC to check the integrity of commands. You can imagine it as a way for a postman to check if a letter is damaged or missing a part. Every time a message is sent, it is accompanied by a verification code, which is like the postmark of a letter, used to confirm whether the letter is intact and undamaged.

There are two common forms of Modbus protocol

There are two common forms of Modbus protocol: Modbus RTU and Modbus TCP. Modbus RTU is often used for serial communication (like communication over a telephone line), while Modbus TCP is often used for network communication (like sending e-mail over the Internet). The choice of which form mainly depends on which communication method your device supports.

We can use some Modbus software for testing and communication. For example, Modbus Poll and ModScan are both like a phone that can help you communicate with your device. In this software, you only need to enter the device address, the operation you want to perform (read or write), and the register address you want to operate, and then click the "send" button, just like making a phone call.

For example, if you want to read the color of bulb 1, you can enter the address of the bulb (1) in the software, select the "Read" operation, enter the address of the color register, and then click "Send". The software will automatically generate corresponding Modbus commands for you and send them to the light bulb. After receiving the command, the light bulb will reply with its current color. If you see the color information replied by the light bulb, congratulations, you have successfully had a conversation with your light bulb.

Similarly, if you want to change the color of bulb 1 to red, you can enter the address of the bulb (1) in the software, select the "write" operation, enter the address of the color register, enter the color you want to set (red), and then click "send". The software will automatically generate corresponding Modbus commands for you and send them to the light bulb. After receiving the command, the light bulb will change its color and reply that it has completed the operation. If you see the response of the light bulb and the color of the light bulb has changed to red, then you have successfully changed the color of the light bulb.

Overall, Modbus is like a postman, helping you communicate with your devices. Although initial contact may be a bit complicated, as long as you are familiar with the basic concepts and operations, you can start a conversation with your devices and make them work according to your ideas. Just like how you can communicate with your friends through your phone, Modbus can help you communicate with your devices, making your life and work more convenient.

Let's go back to the example of a smart light bulb. Suppose we have a light bulb device and we want to check its brightness and change its color. In Modbus, we use two types of function codes: 03 (read hold register) and 06 (write single register).

1. * * Use function code 03 to read and hold registers**

Firstly, we want to read the brightness of the light bulb. In our device, brightness information is stored in the hold register at address 30001. We will input the following information in Modbus software:

Device address: 1 (assuming the address of our light bulb is 1)
Function code: 03 (representing read hold register)
Register Address: 30001
Then, we click the "Send" button, and the software will automatically generate the corresponding Modbus command and send it to the device. This process is like writing a note: "Light bulb 1, please tell me your current brightness." After receiving this command, the light bulb will reply with its current brightness.

2. * * Use function code 06 to write a single register**

Now, we want to change the color of the light bulb to red. In our device, color information can be changed by writing to the hold register at address 40001. We will input the following information in Modbus software:

Device address: 1 (address of our light bulb)
Function code: 06 (representing writing a single register)
Register Address: 40001
Data: Red (assuming the code corresponding to red is 2)
Then, we click the "Send" button, and the software will automatically generate the corresponding Modbus command and send it to the device. This process is like writing a note: "Light bulb 1, please change your color to red." After receiving this command, the light bulb will change its color and reply that it has completed the operation.

By using these function codes, we can read and change the status of the device, just like having a conversation with the device. In practical operation, you may need to consult the user manual of your device to understand the device's address and the register addresses corresponding to various status information. After understanding this information, you can start communicating with your devices and make them work according to your ideas.

Write multiple registers

**Use function code 16 to control the air conditioning**

Assuming you have an intelligent air conditioner, you want to adjust its on/off status, working mode, air volume, and other parameters simultaneously. In this case, we can use Modbus function code 16 (write multiple registers).

Device address: 1 (assuming the address of the air conditioner is 1)
Function code: 16 (representing writing multiple registers)
Register address: 400014000240003 (assuming 40001 is for controlling power on/off, 40002 is for adjusting working mode, and 40003 is the register address for adjusting air volume)
Data: 1,2,3 (assuming 1 represents power on, 2 represents cooling mode, and 3 represents maximum air volume)

It's like you wrote a letter to the air conditioner: 'Dear air conditioner, please turn on the power, adjust to cooling mode, and increase the air volume to maximum.' Then, you sent the letter to the air conditioner through a postman (i.e. Modbus).

Then, we click the "Send" button, and the Modbus software will automatically generate the corresponding Modbus command and send it to the air conditioner. After receiving the command, the air conditioner will adjust its on/off status, working mode, and air volume simultaneously, and reply that it has completed this operation.

By using function code 16, we can adjust multiple parameters of the air conditioner simultaneously, avoiding the hassle of only adjusting one parameter at a time..

Read multiple registers

**Read multiple status information of the air conditioner using function code 03**

Now, suppose we want to read the on/off status, working mode, air volume, and other information of the air conditioner all at once. In this case, we can use Modbus function code 03 (read hold register).

Device address: 1 (assuming the address of the air conditioner is 1)
Function code: 03 (representing read hold register)
Starting register address: 30001 (assuming 30001 is the register address that records the power on/off status)
Number of registers read: 3 (The information we want to read includes power on/off status, working mode, and air volume size, which are stored in registers 30001, 30002, and 30003 respectively)

It's like sending a message to the air conditioner: "Dear air conditioner, please tell me your current power on/off status, working mode, and air volume." Then, the message is sent to the air conditioner through a courier (i.e. Modbus).

Then, we click the "Send" button, and the Modbus software will automatically generate the corresponding Modbus command and send it to the air conditioner. After receiving the command, the air conditioner will tell you its current on/off status, working mode, and air volume.

By using function code 03, we can read multiple status information of the air conditioner at once, avoiding the trouble of only being able to read one status information at a time. In practical operation, you need to consult the user manual of your air conditioner to understand the address of the air conditioner, as well as the register address corresponding to the on/off status, working mode, air volume, and other information. After understanding this information, you can start communicating with your air conditioner through Modbus to learn about its working status.

Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.