Skip to main content

Simple Modbus RTU implementation for Python

Project description

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 $.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

MinimalModbus-0.2.zip (27.9 kB view details)

Uploaded Source

MinimalModbus-0.2.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

MinimalModbus-0.2.win32.exe (216.1 kB view details)

Uploaded Source

File details

Details for the file MinimalModbus-0.2.zip.

File metadata

  • Download URL: MinimalModbus-0.2.zip
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for MinimalModbus-0.2.zip
Algorithm Hash digest
SHA256 dab5d9a5ed3da6daa062d1814774de0cc4661af75e8c0859830d6cfa76624504
MD5 34c8a08608ec771f2be2648532e222c1
BLAKE2b-256 56b4449602df042e2ad71220e1b47bd35635a01fbe9e10080d74634c68a1ccfe

See more details on using hashes here.

File details

Details for the file MinimalModbus-0.2.tar.gz.

File metadata

  • Download URL: MinimalModbus-0.2.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for MinimalModbus-0.2.tar.gz
Algorithm Hash digest
SHA256 068ecee2d3d8a29725ebc2b38f709a57c0111871441b9af7b5b0c8ce38c6452a
MD5 554bdd4c72446ed697879b947bb95ee7
BLAKE2b-256 f5632940bc849a3e6094257c5e4dc719f11d00d40e2053b19b5a2e65221e0c94

See more details on using hashes here.

File details

Details for the file MinimalModbus-0.2.win32.exe.

File metadata

  • Download URL: MinimalModbus-0.2.win32.exe
  • Upload date:
  • Size: 216.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for MinimalModbus-0.2.win32.exe
Algorithm Hash digest
SHA256 e0b6f7ae06b67484c09cef2f4b3eba432b5fc4ffe5d8af5846a2a9073e6d0d65
MD5 3fed0a0fe29ba3b00e0490f02bcd1aa8
BLAKE2b-256 f8b44dec465f707d2820b76be2db157a7693aa2025cc014f3acbc16c5e697e6c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page