Cross-Platform Modbus Data Logger

Modbus Data Logger

From programmable logic controllers (PLCs) to sensors of all types, the Modbus protocol can be found everywhere.  A number of Modbus utilities exist, mostly for reading/writing values to/from SCADA systems and for troubleshooting Modbus setups.  Sometimes though, it is useful to be able to quickly setup data logging so that you can view data over a period of time.  That’s where this Modbus data logging script comes in.  This utility will poll a Modbus TCP data source at a set interval and log the data to a CSV file or files.  That logged data can then be loaded into Excel for analysis or a database for archival.  Best of all it is lightweight and free!

Why Create Another Modbus Utility?

There are plenty of heavy-weight data logger tools available already.  The problem is if you just need a few datapoints or you just want to log for a short time, most of these tools are either not worth the hassle of setup, or are too expensive.  That’s why I wrote this script…well that and it was fun!

What Can I Do with Modbus Data Logger?

First of all, the script is written in Python (uncompiled) which immediately provides two benefits:

Cross platform — Regardless of your operating system, you should be able to run my Modbus data logger.  All you need to do is make sure Python is installed as well as a couple of extra librarys and you are ready to go.  It should be noted that the script has only been tested on Linux, but it should run on other platforms with little, to no tweaking.

Open source  — If you are troubleshooting a problem or just want to see what the script is doing, the source easily read and edited with a text editor.  Or, if you need to add some additional functionality, have at it!  If you add something useful, send it over and I’ll consider adding your feature to the script so that others can make use of it as well.

How Do I Start Logging Data?

Installation/setup is very straight forward.  The following steps should get you logging data in no time:

  1. Make sure you have Python 3 installed
  2. Install the required Modbus library
    pip install pymodbus
  3. Save the data logging script to your computer.  You can get it here, or if you use Git, you can clone the repository.
    git clone
    cd modbus-data-logger
    chmod +x
  4. Log some data
    ./ 01,02
    This will cause coils 1 and 2 to read from a device at once every second and displayed on the screen.

What Do All the Options Mean?

While Modbus Data Logger is simple, it is also flexible.  Using the various options detailed here, you should be able to tweak your data collection to work exactly how you need it to.  You can get help on these options by running:

./ -h
usage: [-h] [-v] [-u UNIT] [-p PORT] [-i INTERVAL] [-f FILE] [-s SEPARATOR] ip_address registers

positional arguments:
ip_address IP address of Modbus TCP server
registers Comma seperated string of registers to read, the first
character of each address indicates the type of
register (0=Coil, 1=Discreet Input, 3=Input Register, 4=Holding Register)

optional arguments:
-h, --help show this help message and exit
-v, --verbose Increase output verbosity
-u UNIT, --unit UNIT Modbus unit id (default is 1)
-p PORT, --port PORT Modbus port to connect to (default is 502)
-i INTERVAL, --interval INTERVAL
Read interval in milliseconds (default is 1000)
-f FILE, --file FILE Filename to output data to
Separator character (default is ,)

Required Parameters

IP Address:  The IP address of the Modbus device you will be polling.

Registers:  A string of register addresses to read from your Modbus device.  This part isn’t hard, but it does need a brief explanation.  The format is a list of Modbus addresses seperated by commas and each address is prefixed with a single digit to indicate the type of register to read.  A prefixes are as follows:

0 = Coil
1 = Discreet Input
3 = Input Register
4 = Holding Register

So for example, if I wanted to read coil 10, I would use 010 for the address.  Or if I wanted to read holding register 105, I would use 4105.

Optional Parameters

Unit:  The Modbus unit id defaults to 1.  An id 1 should cover most cases, but if you need to use a different ID, this option will allow you to use any Modbus unit id you want.

Port:  The Modbus device’s TCP port.  The default is the standard Modbus port of 502, however, the option is available for those using a non-standard configuration.

Interval:  The polling interval defaults to 1000 milliseconds (or 1 second).  You can poll at any rate your Modbus device and network will support.  This option allows you to specify you polling rate in milliseconds.

File:  Specifying a file path/name here will cause polled data to be saved to a CSV file.  If this option is omitted, polled data will only be shown on the screen as it is read.  Date/time placeholders can be included in the specified file name so that files can be named according to the date/time they were created.  By using date and time elements in your filenames, you can create a ring buffer of files so that logging for long periods of time won’t fill up your storage device.

For example, the filename:

Will create files named by day.  Each ‘day’ file will be overwritten every month.

Seperator:  The default value seperator in a CSV file is a comma, but if you want to use a different character, you can set that with this option.