← Back to list

PyModbus

Introduction

Rahul Murali · 2026-06-03 01:55 · 2 claps · 2.2 min read
#technical #modbus #pymodbus #industry #automation
Open on Medium ↗

PyModbus

Introduction

The PyModbus library is a Python library that allows one to turn their own laptop into a virtual controller that works on the Modbus Architecture. This is useful when designing simulations and HMI‘s at a research or primary implementation level.

The Modbus Basics

All data is classified into one of 4 types:

  1. Coils — Digital Outputs : Read/Write, Binary

  2. Discrete Inputs — Digital Inputs : Read, Binary

  3. Input Registers — Analog Inputs : Read, 16 bit numbers

  4. Holding Registers — Analog Outputs : Read/Write, 16 bit numbers

How Can It Work As A Controller (Client / Master)?

PyModbus working as a Master allows the device to be able to control the end devices.

This can be broken down into 4 successive stages:

1. Initiation

Here, your Python script opens a connection to the physical device that you wish to communicate with via an IP Address (Modbus TCP) or a serial port (Modbus RTU / Modbus ASCII). At this stage, which device is the Master and which device is the Slave will be decided.

2. Request

Here, the Master will send a request for a command to be executed (Read / Write) by calling the Slave_ID.

3. Processing

Here, PyModbus will take the Request Message, serialises it into the message packet structure that the end hardware will be expecting.

4. Response

The device gets the request and responds with the data that the master requires.

How Can It Work As a Simulator (Server / Slave)?

Here, the device can be made to work as a PLC.

This can again be broken down into 4 stages:

1. Memory Setup

A virtual data store is defined in Python. PyModbus allocates virtual memory for Coils, Holding Registers, Input Registers, Discrete Inputs, etc.

2. Starting the Server

This launches a background loop that listens on the Modbus Port 502 using Pythons asyncio.

3. Simulation Loop

A parallel Python thread is initiated, and this thread simulates the real-world behaviour of the device (like a PLC).

4. External Interaction

Here, any external hardware can connect to the Python Script, and reads the registers.

In legacy technology, the controller was called the master and the simulator was called the slave, analogous to master slave circuits that were popular at the time. In modern connectivity, the controller is called the client and the simulator is called the server, analogous to modern networking paradigms.

Why PyModbus?

PyModbus is a versatile way to simulate what industrial automation of various hardware devices like PLC’s would be. There are several advantages to using PyModbus in simulation programs.

Asynchronous Core : Built on asyncio, meaning a single simulator can handle thousands of concurrent connections effortlessly.

Dual Role : A single Python script can act as both a client (gatherer) and a server (deliverer).

Abstraction : CRC and Checksums Are handled by the library, so no extra load on frame formatting.

To see just how abstracted and simple this is, here are the three lines of code required to connect to a local server and read a register:

from pymodbus.client import ModbusTcpClient

client = ModbusTcpClient('127.0.0.1', port=5020)
client.connect()
result = client.read_holding_registers(0, 1) # Read register 0

You can refer one of my projects to see how it can be used in simulations.

GitHub Link

Further Reading

Please do refer the below link for more technical details on this topic:

https://github.com/pymodbus-dev/pymodbus

Thank you for reading all the way till the end!


메타데이터
post_id
4b41a8849cdf
slug
pymodbus-4b41a8849cdf
url
https://medium.com/@muralirahul6210/pymodbus-4b41a8849cdf
canonical_url
https://medium.com/@muralirahul6210/pymodbus-4b41a8849cdf
author_url
https://medium.com/@muralirahul6210
status
ok
fetched_at
2026-07-11 22:52:18