Skip to main content

Introduction

MinimalModbus is an easy-to-use Python module for talking to instruments (slaves) from a computer (master) using the Modbus protocol. Example code includes drivers for Eurotherm process controllers. The only dependence is the pySerial module. This software supports the ‘Modbus RTU’ serial communication version of the protocol, and is intended for use on Linux and Windows platforms.

General on Modbus protocol

Modbus is a serial communications protocol published by Modicon in 1979, according to http://en.wikipedia.org/wiki/Modbus. It is often used to communicate with industrial electronic devices.

There are several types of Modbus protocols:

Modbus RTU

A serial protocol that uses binary representation of the data. Supported by this software.

Modbus ASCII

A serial protocol that uses ASCII representation of the data. Not supported by this software.

Modbus TCP/IP and variants

A protocol for communication over TCP/IP networks. Not supported by this software.

For full documentation on the Modbus protocol, see http://www.modbus.com/. One important document is ‘MODBUS over serial line specification and implementation guide V1.02’, found at http://www.modbus.com/docs/Modbus_over_serial_line_V1_02.pdf

Typical hardware

The application for which I wrote this software is to read and write data from Eurotherm process controllers. These come with different types of communication protocols, but the controllers I prefer use the Modbus RTU protocol. MinimalModbus is intended for general communication using the Modbus RTU protocol (using a serial link), so there should be lots of applications.

As an example on the usage of MinimialModbus, the driver I use for an Eurotherm 3504 process controller is included. It uses the MinimalModbus Python module for its communication.

There can be several instruments (slaves) on a single bus, and the slaves have addresses in the range 1 to 247. In the Modbus RTU protocol, only the master can initiate communication. The physical layer is most often the serial bus RS485, which is described at http://en.wikipedia.org/wiki/Rs485.

To connect your computer to the RS485 bus, a serial port is required. There are direct USB-to-RS485 converters, but I use a USB-to-RS232 converter together with an industrial RS232-to-RS485 converter. This has the advantage that the latter is galvanically isolated using opto-couplers, and has transient supression. This software has been tested using a Westermo MDW-45 RS232-to-RS485 converter.

Typical usage

The instrument is typically connected via a serial port, and a USB-to-serial adaptor should be used on most modern computers. How to configure such a serial port is described on the pySerial page: http://pyserial.sourceforge.net/

For example, consider an instrument(slave) with address number 1 to which we are to communicate via a serial port with the name /dev/ttyUSB1. The instrument stores the measured temperature in register 289. For this instrument a temperature of 77.2 C is stored as 772, why we use 1 decimal. To read this data from the instrument:

#!/usr/bin/env python
import minimalmodbus

instrument = minimalmodbus.Instrument('/dev/ttyUSB1', 1) # port name, slave address

## Read temperature (PV = ProcessValue) ##
temperature = instrument.read_register( 289, 1 ) # Registernumber, number of decimals
print temperature

## Change temperature setpoint (SP) ##
NEW_TEMPERATURE = 95
instrument.write_register(24, NEW_TEMPERATURE, 1) # Registernumber, value, number of decimals for storage

The full API for minimalmodbus is available on http://minimalmodbus.sourceforge.net/apiminimalmodbus.html, and the documentation in PDF format is found on http://minimalmodbus.sourceforge.net/minimalmodbus.pdf

Subclassing

It is better to put the details in a driver for the specific instrument. An example driver for Eurotherm3500 is included in this library, and it is recommended to have a look at its source code. To get the process value (PV from loop1):

#!/usr/bin/env python
import eurotherm3500

heatercontroller = eurotherm3500.Eurotherm3500('/dev/ttyUSB1', 1)  # port name, slave address

## Read temperature (PV) ##
temperature = heatercontroller.get_pv_loop1()
print temperature

## Change temperature setpoint (SP) ##
NEW_TEMPERATURE = 95.0
heatercontroller.set_sp_loop1(NEW_TEMPERATURE)

Default values

Most of the serial port parameters have the default values which are defined in the Modbus standard:

instrument.serial.port          # this is the serial port name
instrument.serial.baudrate = 19200   # Baud
instrument.serial.parity   = serial.PARITY_NONE
instrument.serial.bytesize = 8
instrument.serial.stopbits = 1
instrument.serial.timeout  = 0.05   # seconds

instrument.address     # this is the slave address number

These can be overridden:

instrument.serial.timeout = 0.2

For details on the allowed parity values, see http://pyserial.sourceforge.net/pyserial_api.html#serial.PARITY_NONE.

Dependencies

This module relies on pySerial to do the heavy lifting, and it is the only dependency. You can find it at the Python package index: http://pypi.python.org/pypi/pyserial

Python version 2.6 and 2.7 have been used to develop this software, but it is probably compatible with Python 3 (according to the 2to3 tool).

Download and installation

From command line (if you have the pip installer, available at http://pypi.python.org/pypi/pip):

pip install minimalmodbus

or manually download the compressed source files from http://pypi.python.org/pypi/MinimalModbus/. There are compressed source files for Unix/Linux (.tar.gz) and Windows (.zip). Uncompress it, and run:

python setup.py install

There is also a Windows installer (.win32.exe) available. Just start it and follow the instructions.

Licence

Apache License, Version 2.0

Home page

Home page with full API documentation

http://minimalmodbus.sourceforge.net/ (this page if viewed on sourceforge.net).

Python package index

http://pypi.python.org/pypi/MinimalModbus/ (this page if viewed on python.org. Note that no API is available).

The SourceForge project page

http://sourceforge.net/projects/minimalmodbus/ with Subversion repository, bug tracker and mailing list.

Author

Jonas Berg, pyhys@users.sourceforge.net

References

Text revision

This README file was changed (committed) at $Date: 2011-08-19 11:43:37 +0200 (Fri, 19 Aug 2011) $, which was $Revision: 55 $.

Metadata

Release files for minimalmodbus 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distributions (sdists)

Source distribution for minimalmodbus 0.2
File Size Uploaded Provenance Actions
MinimalModbus-0.2.zip 27.9 kB Without provenance Details for MinimalModbus-0.2.zip
MinimalModbus-0.2.tar.gz 20.8 kB Without provenance Details for MinimalModbus-0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for minimalmodbus 0.2
File Interpreter ABI Platform Actions
MinimalModbus-0.2.win32.exe Details for MinimalModbus-0.2.win32.exe

Total release size: 264.8 kB

Release files / MinimalModbus-0.2.zip

Download URL MinimalModbus-0.2.zip
Size 27.9 kB
Tags Source
SHA-256 checksum
How to use checksums
dab5d9a5ed3da6daa062d1814774de0cc4661af75e8c0859830d6cfa76624504
BLAKE2b-256 checksum
How to use checksums
56b4449602df042e2ad71220e1b47bd35635a01fbe9e10080d74634c68a1ccfe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / MinimalModbus-0.2.tar.gz

Download URL MinimalModbus-0.2.tar.gz
Size 20.8 kB
Tags Source
SHA-256 checksum
How to use checksums
068ecee2d3d8a29725ebc2b38f709a57c0111871441b9af7b5b0c8ce38c6452a
BLAKE2b-256 checksum
How to use checksums
f5632940bc849a3e6094257c5e4dc719f11d00d40e2053b19b5a2e65221e0c94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / MinimalModbus-0.2.win32.exe

Download URL MinimalModbus-0.2.win32.exe
Size 216.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e0b6f7ae06b67484c09cef2f4b3eba432b5fc4ffe5d8af5846a2a9073e6d0d65
BLAKE2b-256 checksum
How to use checksums
f8b44dec465f707d2820b76be2db157a7693aa2025cc014f3acbc16c5e697e6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for minimalmodbus 0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page