DEV Community

Masafumi Saito
Masafumi Saito

Posted on

Modbus Protocol - A Python Beginner's Summary

Modbus Protocol - A Python Beginner's Summary

I work in Tokyo as a B2B sales rep for wastewater treatment equipment. Recently, there's been growing demand not just for automated equipment itself, but also for remote monitoring and IoT integration of facilities.

How can I respond to these needs? This question is what motivated me to start learning Python, AWS, AI, and ML.

Today, I want to leave some notes about the Modbus Protocol, which is the foundation of Python's Pymodbus library, from a beginner's perspective.


What is the Modbus Protocol?

The Modbus Protocol is a set of communication rules for industrial machines to talk to each other. Originally developed by Modicon (a PLC manufacturer) in 1979.

The protocol specifications are publicly available and anyone can use it commercially for free.

Due to its simplicity, many industrial devices support the Modbus Protocol.


How Modbus Protocol Communication Works

Modbus Protocol uses a master/slave communication method.

Master (Client): PC, monitoring systems

Slave (Server): Sensors, actuators, PLCs

The actual process works like this:

  1. Master sends a command (request)
  2. Only the specified slave executes that command
  3. Slave sends back the result (response)
  4. Master receives the result

Internal Structure of Modbus-Compatible Devices

1. Memory Space (Data Areas)

Modbus Protocol-compatible devices have 4 types of memory spaces for storing information, just like PC memory. These are called data areas.

  1. Coil: Stores digital values (ON/OFF or True/False), read/write access
  2. Discrete Input: Stores digital values (ON/OFF or True/False), read-only access
  3. Input Register: Stores analog values (numerical data), read-only access
  4. Holding Register: Stores analog values (numerical data), read/write access
Item Digital Values Analog Values
Data Nature Discrete (ON/OFF) Continuous (numerical)
Representation True/False, 1/0 0~65535 numbers
Data Size 1 bit 16 bits
Use Cases Switches, relays, LEDs Temperature, pressure, flow, voltage
Modbus Data Areas • Coil
• Discrete Input
• Input Register
• Holding Register

Key Point

Each manufacturer and model stores different things in different areas, so you must always check the manual.

Example: Temperature Controller

Data Area Data Type Access Stored Information
Coil Digital Read/Write Heater control, alarm reset
Discrete Input Digital Read-only Alarm status, operation status
Input Register Analog Read-only Current temperature, sensor values
Holding Register Analog Read/Write Set temperature, control parameters

2. Types of Modbus Communication

Type Communication Method Data Format Connection Features
Modbus RTU Serial communication Binary RS-485 cable • Most common
• Fast & efficient
• Compact data
Modbus ASCII Serial communication ASCII characters RS-485 cable • Human-readable format
• Easy to debug
• Double data volume
Modbus TCP/IP Ethernet Binary LAN cable • LAN cable connection
• Internet capable
• High-speed, long-distance

Modern PLCs (Mitsubishi Electric, Omron, Keyence, etc.) have standard support for Modbus TCP/IP communication, making it mainstream for new systems.


Summary

I didn't get to write much code today, but I gained some fundamental knowledge about the technology that supports IoT.

Next time, I want to try writing actual Modbus communication code using the Pymodbus library.

See you tomorrow! 👋

Top comments (0)