store — Datastore for Modbus Server Context

API Documentation

Modbus Server Datastore

For each server, you will create a ModbusServerContext and pass in the default address space for each data access. The class will create and manage the data.

Further modification of said data accesses should be performed with [get,set][access]Values(address, count)

Datastore Implementation

There are two ways that the server datastore can be implemented. The first is a complete range from ‘address’ start to ‘count’ number of indecies. This can be thought of as a straight array:

data = range(1, 1 + count)
[1,2,3,...,count]

The other way that the datastore can be implemented (and how many devices implement it) is a associate-array:

data = {1:'1', 3:'3', ..., count:'count'}
[1,3,...,count]

The difference between the two is that the latter will allow arbitrary gaps in its datastore while the former will not. This is seen quite commonly in some modbus implementations. What follows is a clear example from the field:

Say a company makes two devices to monitor power usage on a rack. One works with three-phase and the other with a single phase. The company will dictate a modbus data mapping such that registers:

n:      phase 1 power
n+1:    phase 2 power
n+2:    phase 3 power

Using this, layout, the first device will implement n, n+1, and n+2, however, the second device may set the latter two values to 0 or will simply not implmented the registers thus causing a single read or a range read to fail.

I have both methods implemented, and leave it up to the user to change based on their preference.

class pymodbus.datastore.store.BaseModbusDataBlock

Base class for a modbus datastore

Derived classes must create the following fields:
@address The starting address point @defult_value The default value of the datastore @values The actual datastore values
Derived classes must implemented the following methods:
validate(self, address, count=1) getValues(self, address, count=1) setValues(self, address, values)
__iter__()

Iterater over the data block data

Returns:An iterator of the data block data
__str__()

Build a representation of the datastore

Returns:A string representation of the datastore
default(count, value=False)

Used to initialize a store to one value

Parameters:
  • count – The number of fields to set
  • value – The default value to set to the fields
getValues(address, count=1)

Returns the requested values from the datastore

Parameters:
  • address – The starting address
  • count – The number of values to retrieve
Returns:

The requested values from a:a+c

reset()

Resets the datastore to the initialized default value

setValues(address, values)

Returns the requested values from the datastore

Parameters:
  • address – The starting address
  • values – The values to store
validate(address, count=1)

Checks to see if the request is in range

Parameters:
  • address – The starting address
  • count – The number of values to test for
Returns:

True if the request in within range, False otherwise

class pymodbus.datastore.store.ModbusSequentialDataBlock(address, values)

Creates a sequential modbus datastore

__init__(address, values)

Initializes the datastore

Parameters:
  • address – The starting address of the datastore
  • values – Either a list or a dictionary of values
classmethod create(klass)

Factory method to create a datastore with the full address space initialized to 0x00

Returns:An initialized datastore
getValues(address, count=1)

Returns the requested values of the datastore

Parameters:
  • address – The starting address
  • count – The number of values to retrieve
Returns:

The requested values from a:a+c

setValues(address, values)

Sets the requested values of the datastore

Parameters:
  • address – The starting address
  • values – The new values to be set
validate(address, count=1)

Checks to see if the request is in range

Parameters:
  • address – The starting address
  • count – The number of values to test for
Returns:

True if the request in within range, False otherwise

class pymodbus.datastore.store.ModbusSparseDataBlock(values)

Creates a sparse modbus datastore

__init__(values)

Initializes the datastore

Using the input values we create the default datastore value and the starting address

Parameters:values – Either a list or a dictionary of values
classmethod create(klass)

Factory method to create a datastore with the full address space initialized to 0x00

Returns:An initialized datastore
getValues(address, count=1)

Returns the requested values of the datastore

Parameters:
  • address – The starting address
  • count – The number of values to retrieve
Returns:

The requested values from a:a+c

setValues(address, values)

Sets the requested values of the datastore

Parameters:
  • address – The starting address
  • values – The new values to be set
validate(address, count=1)

Checks to see if the request is in range

Parameters:
  • address – The starting address
  • count – The number of values to test for
Returns:

True if the request in within range, False otherwise